POP3/Folder/mbox.pm

filter: Statementstmt 75.1% 136 / 181 Branchbran 56.8% 25 / 44 Conditioncond 66.6% 6 / 9 MC/DCmcdc 50.0% 3 / 6 Subroutinesub 73.3% 22 / 30 Podpod 100% 14 / 14 totaltotal 72.5% 206 / 284 CC 31 SCAR 19.2
CC
31
Cov
71%
top
30.0
retrieve
20.0
_list_messages
8.0
CRAP 6.8
« POP3/Daemon.pm ↑ Summary POP3/Security/User.pm »
1 package Mail::POP3::Folder::mbox;
2  
3 4 our @ISA = qw(Mail::POP3::Folder);
4  
5 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use Fcntl ':flock';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 4 my $CRLF = "\015\012";
10  
11 2 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
12 my (
13 2 $class,
14 $user_name,
15 $password,
16 $user_id,
17 $mailgroup,
18 $spoolfile,
19 $message_start,
20 $message_end,
21 ) = @_;
22 2 my $self = {};
23 2 bless $self, $class;
24 2 $self->{CLIENT_USER_ID} = $user_id;
25 2 $self->{MAILGROUP} = $mailgroup;
26 2 $self->{SPOOLFILE} = $spoolfile;
27 2 $self->{MESSAGE_START} = $message_start;
28 2 $self->{MESSAGE_END} = $message_end;
29 2 $self->{MESSAGECNT} = 0;
30 2 $self->{MSG2OCTETS} = {};
31 2 $self->{MSG2UIDL} = {};
32 2 $self->{MSG2FROMLINE} = {};
33 2 $self->{MSG2TEXT} = {};
34 2 $self->{TOTALOCTETS} = 0;
35 2 $self->{DELMESSAGECNT} = 0;
36 2 $self->{DELTOTALOCTETS} = 0;
37 2 $self;
38 }
39  
40 6 ▶sub _start_new_message {
Subroutine _start_new_message: called CC=1 SCAR=0.0
41 6 my ($self, $newmessageno, $fromline) = @_;
42 # Hold the "From ..." line to put back if message is not retrieved
43 6 $self->{MSG2FROMLINE}->{$newmessageno} = $fromline;
44 }
45  
46 6 ▶sub _close_old_message {
Subroutine _close_old_message: called CC=2 SCAR=7.0
47 6 my ($self, $oldmessageno, $messageuidl, $messageoctets) = @_;
48 6 ▶ return if $oldmessageno <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $oldmessageno <= 0 0 6 6
49 6 $self->{TOTALOCTETS} += $messageoctets;
50 6 $self->{MSG2OCTETS}->{$oldmessageno} = $messageoctets;
51 6 $self->{MSG2UIDL}->{$oldmessageno} = $messageuidl;
52 }
53  
54 # assume that mbox is good, no error-checking at start
55 # not sure the message_end is necessary, is that for MMDF?
56 2 ▶sub _list_messages {
Subroutine _list_messages: called CC=8 SCAR=20.8
57 2 my $self = shift;
58 2 ▶ if (!-s $self->_spoolfile) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-s $self->_spoolfile) 0 2 2
59 # no mail
60 0 return;
61 }
62 2 my $seen_message_end = 1; # the end of fake "message 0"...
63 2 my $messagecnt = 0;
64 2 my $messageoctets = 0;
65 2 my $messageuidl = '';
66 2 local *MDROP;
67 2 open MDROP, $self->_spoolfile;
68 2 ▶ while (<MDROP>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline MDROP)) 74 2 76
69 74 $self->_lock_update;
70 74 s/\r|\n//g;
71 74 ▶ if ($seen_message_end and /$self->{MESSAGE_START}/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($seen_message_end and /$self->{'MESSAGE_START'}/) { } 6 68 74
Condition100%3/3
$seen_message_end and /$self->{"MESSAGE_START"}/
!ll&&!rl&&r
6086
MC/DC100%2/2
$seen_message_end and /$self->{"MESSAGE_START"}/
$seen_message_end /$self->{"MESSAGE_START"}/
Truth table100%3/3
A and B
A= $seen_message_endB= /$self->{"MESSAGE_START"}/
ABresult
0X0
100
111
72 # tick over
73 6 $self->_close_old_message(
74 $messagecnt,
75 $messageuidl,
76 $messageoctets,
77 );
78 6 $messagecnt++;
79 6 $messageuidl = '';
80 6 $messageoctets = 0;
81 6 $self->_start_new_message($messagecnt, $_);
82 } else {
83 68 $seen_message_end = 0;
84 68 ▶ if (/$self->{MESSAGE_END}/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/$self->{"MESSAGE_END"}/) 8 60 68
85 8 $seen_message_end = 1;
86 }
87 68 ▶ if (/^Message-Id:\s*(.+)/ and not $messageuidl) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^Message-Id:\s*(.+)/ and not $messageuidl) 6 62 68
Condition66%2/3
/^Message-Id:\s*(.+)/ and not $messageuidl
!ll&&!rl&&r
6206
MC/DC50%1/2
/^Message-Id:\s*(.+)/ and not $messageuidl
/^Message-Id:\s*(.+)/ not $messageuidl
Truth table66%2/3
A and not(B)
A= /^Message-Id:\s*(.+)/B= not $messageuidl
ABresult
0X0
100
111
88 # only take first Message-ID; cf such a header appearing in body
89 6 $messageuidl = $1;
90 }
91 68 $self->_push_message($messagecnt, $_);
92 68 $messageoctets += length ($_.$CRLF);
93 }
94 }
95 # catch the last one
96 2 $self->_close_old_message($messagecnt, $messageuidl, $messageoctets);
97 2 $self->{MESSAGECNT} = $messagecnt;
98 }
99  
100 10 ▶sub _spoolfile {
Subroutine _spoolfile: called CC=1 SCAR=0.0
101 10 my $self = shift;
102 10 $self->{SPOOLFILE};
103 }
104  
105 2 ▶sub lock_acquire {
Subroutine lock_acquire: called CC=5 SCAR=16.6
Pod documented
106 2 my $self = shift;
107 2 my $lockfile = $self->_lock_filename;
108 2 ▶ return if -f $lockfile;
Branch: 1 branch
BranchTrueFalseTotal
if -f $lockfile 0 2 2
109 2 $self->{LINE} = 0;
110 2 ▶ $self->{LOCK_FH} = IO::File->new(
Branch: 1 branch
BranchTrueFalseTotal
$self->{"LOCK_FH"} = "IO::File"->new(">$lockfile") or die "open >${lockfile}: $!\n" 2 0 2
111 ">$lockfile"
112 ) or die "open >$lockfile: $!\n";
113 2 ▶ unless (flock $self->{LOCK_FH}, LOCK_EX|LOCK_NB) {
Branch: 1 branch
BranchTrueFalseTotal
unless (flock $self->{"LOCK_FH"}, 6) 0 2 2
114 0 unlink $lockfile;
115 0 return;
116 }
117 2 chmod 0600, $lockfile;
118 2 chown $self->{CLIENT_USER_ID}, $self->{MAILGROUP}, $lockfile;
119 2 my $oldfh = select $self->{LOCK_FH};
120 2 $| = 1;
121 2 select $oldfh;
122 2 $self->_lock_refresh;
123 # stat the file to get its size, this is checked before closing
124 # the mailbox.
125 # If the size has changed the lock may have been compromised, so a
126 # backup is then made.
127 2 my @filestat = stat $self->_spoolfile;
128 2 $self->{MAILBOX_TIMESTAMP_OPEN} = $filestat[9];
129 # set effective UID to user for the rest of the session;
130 2 $> = $self->{CLIENT_USER_ID};
131 2 $self->_list_messages;
132 2 ▶die unless $self->{LOCK_FH};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"LOCK_FH"} 0 2 2
133 2 1;
134 }
135  
136 2 ▶sub lock_release {
Subroutine lock_release: called CC=1 SCAR=0.0
Pod documented
137 2 my $self = shift;
138 2 close $self->{LOCK_FH};
139 2 $> = 0;
140 2 unlink $self->_lock_filename;
141 2 $> = $self->{CLIENT_USER_ID};
142 }
143  
144 4 ▶sub _lock_filename {
Subroutine _lock_filename: called CC=1 SCAR=0.0
145 4 my ($self) = @_;
146 4 "$self->{SPOOLFILE}.lock";
147 }
148  
149 25 ▶sub _lock_refresh {
Subroutine _lock_refresh: called CC=2 SCAR=7.0
150 # This is to update the m time on the <user>.lock mbox lock file.
151 # It may seem paranoid but I have seen lock files removed by impatient
152 # MDA's, so the file is written-to, unbuffered, as often as is
153 # practicable.
154 25 my $self = shift;
155 25 $> = 0;
156 25 ▶die unless $self->{LOCK_FH};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"LOCK_FH"} 0 25 25
157 25 seek $self->{LOCK_FH}, 0, SEEK_SET;
158 25 $self->{LOCK_FH}->print("\0");
159 25 $> = $self->{CLIENT_USER_ID};
160 }
161  
162 174 ▶sub _lock_update {
Subroutine _lock_update: called CC=2 SCAR=9.2
163 174 my $self = shift;
164 174 ▶ if (++$self->{LINE} == 1000) {
Branch: 1 branch
BranchTrueFalseTotal
if (++$self->{"LINE"} == 1000) 0 174 174
165 0 $self->_lock_refresh;
166 0 $self->{LINE} = 0;
167 }
168 }
169  
170 0 ▶sub _push_message_line {
Subroutine _push_message_line: not called CC=1 SCAR=6.9
171 0 my ($self, $messagecnt, $data) = @_;
172 0 push @{ $self->{MSG2TEXT}->{$messagecnt} }, "$data$CRLF";
173 }
174  
175 0 ▶sub _set_message_line {
Subroutine _set_message_line: not called CC=1 SCAR=6.9
176 0 my ($self, $messagecnt, $lineno, $data) = @_;
177 0 $self->{MSG2TEXT}->{$messagecnt}->[$lineno] = $data;
178 }
179  
180 0 ▶sub _get_message_line {
Subroutine _get_message_line: not called CC=1 SCAR=6.9
181 0 my ($self, $messagecnt, $lineno) = @_;
182 0 $self->{MSG2TEXT}->{$messagecnt}->[$lineno];
183 }
184  
185 0 ▶sub _unshift_message_line {
Subroutine _unshift_message_line: not called CC=1 SCAR=6.9
186 0 my ($self, $messagecnt, $data) = @_;
187 0 unshift @{ $self->{MSG2TEXT}->{$messagecnt} }, "$data$CRLF";
188 }
189  
190 0 ▶sub _list_message_line {
Subroutine _list_message_line: not called CC=1 SCAR=6.9
191 0 my ($self, $messagecnt) = @_;
192 0 @{ $self->{MSG2TEXT}->{$messagecnt} };
193 }
194  
195 4 ▶sub octets {
Subroutine octets: called CC=2 SCAR=6.9
Pod documented
196 4 my ($self, $message) = @_;
197 4 ▶ if (defined $message) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $message) { } 2 2 4
198 2 $self->{MSG2OCTETS}->{$message};
199 } else {
200 2 $self->{TOTALOCTETS} - $self->{DELTOTALOCTETS};
201 }
202 }
203  
204 2 ▶sub messages {
Subroutine messages: called CC=1 SCAR=0.0
Pod documented
205 2 my ($self) = @_;
206 2 $self->{MESSAGECNT} - $self->{DELMESSAGECNT};
207 }
208  
209 # Build message arrays or write the next line to disk
210 0 ▶sub _push_message {
Subroutine _push_message: not called CC=1 SCAR=6.9
211 0 my ($self, $messagecnt, $data) = @_;
212 0 $self->_push_message_line($messagecnt, $data);
213 }
214  
215 # $message starts at 1
216 # returns number of bytes
217 0 ▶sub top {
Subroutine top: not called CC=5 SCAR=34.0
Pod documented
218 0 my ($self, $message, $output_fh, $body_lines) = @_;
219 0 my $top_bytes = 0;
220 0 my $rows = (scalar $self->_list_message_line($message)) -1;
221 0 ▶ $body_lines = $rows if $body_lines > $rows;
Branch: 1 branch
BranchTrueFalseTotal
if $body_lines > $rows 0 0 0
222 0 my $cnt = 0;
223 0 for my $line ($self->_list_message_line($message)) {
224 0 $top_bytes += length($line);
225 0 ++$cnt;
226 0 $self->_lock_update;
227 0 $output_fh->print($line);
228 0 ▶ last if $line =~ /^\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^\s*$/ 0 0 0
229 }
230 0 for my $lineno ($cnt..(($cnt + $body_lines) -1)) {
231 0 $self->_lock_update;
232 0 my $line = $self->_get_message_line($message, $lineno);
233 0 $top_bytes += length($line);
234 0 $line =~ s/^\./\.\./o;
235 0 $output_fh->print($line);
236 }
237 0 $top_bytes;
238 }
239  
240 2 ▶sub flush_delete {
Subroutine flush_delete: called CC=5 SCAR=16.4
Pod documented
241 2 my ($self) = @_;
242 2 my $spool_mtime = (stat $self->_spoolfile)[9];
243 2 ▶ if ($spool_mtime != $self->{MAILBOX_TIMESTAMP_OPEN}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spool_mtime != $self->{"MAILBOX_TIMESTAMP_OPEN"}) 0 2 2
244 0 die "spool lock error\n";
245 }
246 2 my $spoolfile = $self->_spoolfile;
247 2 ▶ open MDROP, '>' . $spoolfile or die "open >$spoolfile: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open MDROP, '>' . $spoolfile or die "open >${spoolfile}: $!\n" 2 0 2
248 2 foreach my $cnt (1..$self->{MESSAGECNT}) {
249 6 ▶ if (!$self->is_deleted($cnt)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->is_deleted($cnt)) 4 2 6
250 4 print MDROP "$self->{MSG2FROMLINE}->{$cnt}\n";
251 4 $self->retrieve($cnt, \*MDROP, 1);
252 }
253 }
254 2 close MDROP;
255 }
256  
257 0 ▶sub retrieve {
Subroutine retrieve: not called CC=4 SCAR=30.0
Pod documented
258 0 my ($self, $message, $output_fh, $mbox_destined) = @_;
259 0 for my $line ($self->_list_message_line($message)) {
260 0 ▶ $line =~ s/^\./\.\./o unless $mbox_destined;
Branch: 1 branch
BranchTrueFalseTotal
unless $mbox_destined 0 0 0
261 0 ▶ $line =~ s/\r$// if $mbox_destined;
Branch: 1 branch
BranchTrueFalseTotal
if $mbox_destined 0 0 0
262 0 $self->_lock_update;
263 0 $output_fh->print($line);
264 }
265 }
266  
267 1 ▶sub uidl {
Subroutine uidl: called CC=1 SCAR=0.0
Pod documented
268 1 my ($self, $message) = @_;
269 1 $self->{MSG2UIDL}->{$message};
270 }
271  
272 4 ▶sub uidl_list {
Subroutine uidl_list: called CC=3 SCAR=11.0
Pod documented
273 4 my ($self, $output_fh) = @_;
274 4 for (1..$self->{MESSAGECNT}) {
275 12 $self->_lock_refresh;
276 12 ▶ if (!$self->is_deleted($_)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->is_deleted($_)) 10 2 12
277 10 $output_fh->print("$_ $self->{MSG2UIDL}->{$_}$CRLF");
278 }
279 }
280 4 $output_fh->print(".$CRLF");
281 }
282  
283 27 ▶sub is_deleted {
Subroutine is_deleted: called CC=1 SCAR=0.0
Pod documented
284 27 my ($self, $message) = @_;
285 27 $self->{DELETE}->{$message};
286 }
287  
288 3 ▶sub delete {
Subroutine delete: called CC=1 SCAR=0.0
Pod documented
289 3 my ($self, $message) = @_;
290 3 $self->_lock_refresh;
291 3 $self->{DELETE}->{$message} = 1;
292 3 $self->{DELMESSAGECNT} += 1;
293 3 $self->{DELTOTALOCTETS} += $self->{MSG2OCTETS}->{$message};
294 }
295  
296 7 ▶sub is_valid {
Subroutine is_valid: called CC=3 SCAR=12.5
Pod documented
297 7 my ($self, $msg) = @_;
298 7 $self->_lock_refresh;
299 7 ▶ $msg > 0 and $msg <= $self->{MESSAGECNT} and !$self->is_deleted($msg);
Branch: 1 branch
BranchTrueFalseTotal
$msg > 0 and $msg <= $self->{"MESSAGECNT"} and not $self->is_deleted($msg) 7 0 7
Condition33%1/3
$msg > 0 and $msg <= $self->{"MESSAGECNT"}
!ll&&!rl&&r
007
MC/DC0%0/2
$msg > 0 and $msg <= $self->{"MESSAGECNT"}
$msg > 0 $msg <= $self->{"MESSAGECNT"}
Truth table33%1/3
A and B
A= $msg > 0B= $msg <= $self->{"MESSAGECNT"}
ABresult
0X0
100
111
300 }
301  
302 1 ▶sub reset {
Subroutine reset: called CC=1 SCAR=0.0
Pod documented
303 1 my $self = shift;
304 1 $self->_lock_refresh;
305 1 $self->{DELETE} = {};
306 1 $self->{DELMESSAGECNT} = 0;
307 1 $self->{DELTOTALOCTETS} = 0;
308 }
309  
310 4 1;
311  
312 __END__
« POP3/Daemon.pm ↑ Summary POP3/Security/User.pm »