POP3/Folder/mbox/parse_to_disk.pm

filter: Statementstmt 97.8% 90 / 92 Branchbran 67.8% 19 / 28 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 14 / 14 Podpod 100% 5 / 5 totaltotal 92.0% 128 / 139 CC 16 SCAR 12.2
CC
16
Cov
91%
_start_new_message
5.9
top
5.0
retrieve
4.0
CRAP 3.4
« POP3/Folder/webscrape.pm ↑ Summary POP3/Folder.pm »
1 package Mail::POP3::Folder::mbox::parse_to_disk;
2  
3 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use Fcntl ':flock';
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 4 ▶use vars qw(@ISA);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 @ISA = qw(Mail::POP3::Folder::mbox);
9  
10 4 my $CRLF = "\015\012";
11  
12 2 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
13 my (
14 2 $class,
15 $user_name,
16 $password,
17 $user_id,
18 $mailgroup,
19 $spoolfile,
20 $message_start,
21 $message_end,
22 $tmpdir,
23 $debug,
24 ) = @_;
25 2 my $self = $class->SUPER::new(
26 $user_name,
27 $password,
28 $user_id,
29 $mailgroup,
30 $spoolfile,
31 $message_start,
32 $message_end,
33 );
34 2 $self->{TMPDIR} = $tmpdir;
35 2 $self->{DEBUG} = $debug;
36 2 $self;
37 }
38  
39 6 ▶sub _start_new_message {
Subroutine _start_new_message: called CC=5 SCAR=17.8
40 6 my ($self, $newmessageno, $fromline) = @_;
41 6 $self->SUPER::_start_new_message($newmessageno, $fromline);
42 6 $> = 0;
43 6 ▶ unless (-d $self->{TMPDIR}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $self->{"TMPDIR"}) 0 6 6
44 0 ▶ mkdir $self->{TMPDIR}, 0700
Branch: 1 branch
BranchTrueFalseTotal
mkdir $self->{"TMPDIR"}, 448 or die qq[Couldn't create spool dir '$self->{"TMPDIR"}': $!\n] 0 0 0
45 or die "Couldn't create spool dir '$self->{TMPDIR}': $!\n";
46 }
47 6 my $file = $self->_tmpdir_lockfile;
48 6 $self->{TMPDIRLOCK} = IO::File->new(">$file");
49 6 ▶ unless (flock $self->{TMPDIRLOCK}, LOCK_EX|LOCK_NB) {
Branch: 1 branch
BranchTrueFalseTotal
unless (flock $self->{"TMPDIRLOCK"}, 6) 0 6 6
50 0 die "Could not flock $file\n";
51 }
52 6 $file = $self->_msg2filename($newmessageno);
53 6 ▶ $self->{MESSAGE_FH} = IO::File->new(">$file")
Branch: 1 branch
BranchTrueFalseTotal
$self->{"MESSAGE_FH"} = "IO::File"->new(">$file") or die "Couldn't create spool file: $!\n" 6 0 6
54 or die "Couldn't create spool file: $!\n";
55 6 chmod 0600, $file;
56 6 $> = $self->{CLIENT_USER_ID};
57 }
58  
59 8 ▶sub _close_old_message {
Subroutine _close_old_message: called CC=3 SCAR=11.0
60 8 my ($self, $oldmessageno, $messageuidl, $messageoctets) = @_;
61 8 ▶ return if $oldmessageno <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $oldmessageno <= 0 2 6 8
62 6 $self->SUPER::_close_old_message(
63 $oldmessageno, $messageuidl, $messageoctets
64 );
65 6 $> = 0;
66 6 ▶ close $self->{MESSAGE_FH} if $self->{MESSAGE_FH};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"MESSAGE_FH"} 6 0 6
67 6 $> = $self->{CLIENT_USER_ID};
68 }
69  
70 2 ▶sub lock_release {
Subroutine lock_release: called CC=1 SCAR=0.0
Pod documented
71 2 my $self = shift;
72 2 close $self->{TMPDIRLOCK};
73 2 unlink $self->_tmpdir_lockfile;
74 2 $self->SUPER::lock_release;
75 }
76  
77 # Build message arrays or write the next line to disk
78 68 ▶sub _push_message {
Subroutine _push_message: called CC=1 SCAR=0.0
79 68 my ($self, $messagecnt, $data) = @_;
80 68 $> = 0;
81 68 $self->{MESSAGE_FH}->print("$data\n");
82 68 $> = $self->{CLIENT_USER_ID};
83 }
84  
85 8 ▶sub _tmpdir_lockfile {
Subroutine _tmpdir_lockfile: called CC=1 SCAR=0.0
86 8 my ($self, $message) = @_;
87 8 "$self->{TMPDIR}/.mpopd.lock";
88 }
89  
90 20 ▶sub _msg2filename {
Subroutine _msg2filename: called CC=1 SCAR=0.0
91 20 my ($self, $message) = @_;
92 20 "$self->{TMPDIR}/$message";
93 }
94  
95 # $message starts at 1
96 # returns number of bytes
97 2 ▶sub top {
Subroutine top: called CC=5 SCAR=16.1
Pod documented
98 2 my ($self, $message, $output_fh, $body_lines) = @_;
99 2 my $top_bytes = 0;
100 2 $> = 0;
101 2 local *MSG;
102 2 open MSG, $self->_msg2filename($message);
103 # print the headers
104 2 ▶ while (<MSG>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline MSG)) 20 0 20
105 20 chomp;
106 20 $self->_lock_update;
107 20 my $out = "$_$CRLF";
108 20 $output_fh->print($out);
109 20 $top_bytes += length($out);
110 20 ▶ last if /^\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
if /^\s*$/ 2 18 20
111 }
112 2 my $cnt = 0;
113 # print the TOP arg number of body lines
114 2 ▶ while (<MSG>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline MSG)) 6 0 6
115 6 chomp;
116 6 ▶ last if ++$cnt > $body_lines;
Branch: 1 branch
BranchTrueFalseTotal
if ++$cnt > $body_lines 2 4 6
117 # byte-stuff lines starting with .
118 4 s/^\./\.\./o;
119 4 $self->_lock_update;
120 4 my $out = "$_$CRLF";
121 4 $output_fh->print($out);
122 4 $top_bytes += length($out);
123 }
124 2 close MSG;
125 2 $> = $self->{CLIENT_USER_ID};
126 2 $top_bytes;
127 }
128  
129 2 ▶sub flush_delete {
Subroutine flush_delete: called CC=3 SCAR=11.1
Pod documented
130 2 my ($self) = @_;
131 2 $self->SUPER::flush_delete;
132 2 ▶ if (not $self->{DEBUG}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{"DEBUG"}) 2 0 2
133 2 foreach my $cnt (1..$self->{MESSAGECNT}) {
134 6 unlink $self->_msg2filename($cnt);
135 }
136 }
137 }
138  
139 6 ▶sub retrieve {
Subroutine retrieve: called CC=4 SCAR=13.9
Pod documented
140 6 my ($self, $message, $output_fh, $mbox_destined) = @_;
141 6 $> = 0;
142 6 local *MSG;
143 6 open MSG, $self->_msg2filename($message);
144 6 ▶ while (<MSG>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline MSG)) 76 6 82
145 76 chomp;
146 76 ▶ s/^\./\.\./o unless $mbox_destined;
Branch: 1 branch
BranchTrueFalseTotal
unless $mbox_destined 28 48 76
147 76 $self->_lock_update;
148 76 ▶ my $line = $mbox_destined ? "$_\n" : "$_$CRLF";
Branch: 1 branch
BranchTrueFalseTotal
$mbox_destined ? : 48 28 76
149 76 $output_fh->print($line);
150 }
151 6 close MSG;
152 6 $> = $self->{CLIENT_USER_ID};
153 }
154  
155 4 1;
156  
157 __END__
« POP3/Folder/webscrape.pm ↑ Summary POP3/Folder.pm »