POP3/Folder/maildir.pm

filter: Statementstmt 6.7% 9 / 134 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 10.5% 2 / 19 Podpod 100% 15 / 15 totaltotal 12.9% 26 / 201 CC 19 SCAR 24.5
CC
19
Cov
5%
top
30.0
retrieve
20.0
uidl_list
12.0
CRAP 11.6
« POP3/Server.pm ↑ Summary POP3/Daemon.pm »
1 package Mail::POP3::Folder::maildir;
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 Fcntl ':flock';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 4 my $CRLF = "\015\012";
9  
10 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
Pod documented
11 0 my ($class, $user_name, $password, $user_id, $maildir) = @_;
12 0 my $self = {};
13 0 bless $self, $class;
14 0 $self->{CLIENT_USER_ID} = $user_id;
15 0 $self->{MAILDIR} = $maildir;
16 0 $self->{MESSAGECNT} = 0;
17 0 $self->{MSG2OCTETS} = {};
18 0 $self->{MSG2UIDL} = {};
19 0 $self->{TOTALOCTETS} = 0;
20 0 $self->{DELETE} = {};
21 0 $self->{DELMESSAGECNT} = 0;
22 0 $self->{DELTOTALOCTETS} = 0;
23 0 $self;
24 }
25  
26 # class method
27 0 ▶sub find_maildir {
Subroutine find_maildir: not called CC=2 SCAR=17.9
Pod documented
28 0 my ($class, $user_name) = @_;
29 0 my $maildir;
30 0 my $home = (getpwnam $user_name)[7];
31 0 local *QMAIL;
32 0 ▶ if (open QMAIL, "$home/.qmail") {
Branch: 1 branch
BranchTrueFalseTotal
if (open QMAIL, "$home/.qmail") { } 0 0 0
33 0 $maildir = <QMAIL>;
34 0 close QMAIL;
35 0 chomp $maildir;
36 0 $maildir =~ s#/$##;
37 0 $maildir =~ s#^\.##;
38 0 $maildir =~ s#^/##;
39 0 $maildir = "$home/$maildir";
40 } else {
41 0 $maildir = "$home/Maildir";
42 }
43 0 $maildir;
44 }
45  
46 0 ▶sub uidl_list {
Subroutine uidl_list: not called CC=3 SCAR=24.8
Pod documented
47 0 my ($self, $output_fh) = @_;
48 0 for (1..$self->{MESSAGECNT}) {
49 0 $self->lock_refresh;
50 0 ▶ if (!$self->is_deleted($_)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->is_deleted($_)) 0 0 0
51 0 $output_fh->print("$_ $self->{MSG2UIDL}->{$_}$CRLF");
52 }
53 }
54 0 $output_fh->print(".$CRLF");
55 }
56  
57 # find relevant info about available messages
58 0 ▶sub _list_messages {
Subroutine _list_messages: not called CC=2 SCAR=17.9
59 0 my $self = shift;
60 0 local *MAILDIR;
61 0 opendir MAILDIR, "$self->{MAILDIR}/new";
62 0 $self->{MAILDIR_FILES} = [ sort grep !/^\./, readdir MAILDIR ];
63 0 closedir MAILDIR;
64 # Get the number and size of messages in a Maildir mailbox
65 0 my $cnt = 0;
66 0 foreach (@{ $self->{MAILDIR_FILES} }) {
67 0 $cnt++;
68 # check/create the unique ID code
69 0 my $file = "$self->{MAILDIR}/new/$_";
70 0 my $octets = -s $file;
71 0 $self->{MSG2OCTETS}->{$cnt} = $octets;
72 0 $self->{MSG2UIDL}->{$cnt} = $_;
73 0 $self->{TOTALOCTETS} += $octets;
74 }
75 0 $self->{MESSAGECNT} = $cnt;
76 }
77  
78 # takes a message-number, which starts from 1
79 0 ▶sub _msg2filename {
Subroutine _msg2filename: not called CC=1 SCAR=6.9
80 0 my ($self, $msg) = @_;
81 0 "$self->{MAILDIR}/new/$self->{MAILDIR_FILES}->[$msg - 1]";
82 }
83  
84 # Slightly paranoid mailbox locking...
85 0 ▶sub lock_acquire {
Subroutine lock_acquire: not called CC=3 SCAR=24.8
Pod documented
86 0 my $self = shift;
87 0 ▶ if (-f "$self->{MAILDIR}/new/.mpopd.lock") {
Branch: 1 branch
BranchTrueFalseTotal
if (-f qq[$self->{"MAILDIR"}/new/.mpopd.lock]) 0 0 0
88 0 die "Maildir/new lockfile already exists... :|(";
89 }
90 0 local *MAILDIRLOCK;
91 0 open MAILDIRLOCK, ">$self->{MAILDIR}/new/.mpopd.lock";
92 0 ▶ unless (flock MAILDIRLOCK, LOCK_EX|LOCK_NB) {
Branch: 1 branch
BranchTrueFalseTotal
unless (flock MAILDIRLOCK, 6) 0 0 0
93 0 unlink "$self->{MAILDIR}/new/.mpopd.lock";
94 0 return 0;
95 }
96 0 $self->{LOCK_FH} = \*MAILDIRLOCK;
97 0 $self->_list_messages;
98 }
99  
100 0 ▶sub is_valid {
Subroutine is_valid: not called CC=3 SCAR=24.8
Pod documented
101 0 my ($self, $msg) = @_;
102 0 $self->lock_refresh;
103 0 ▶ $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) 0 0 0
Condition0%0/3
$msg > 0 and $msg <= $self->{"MESSAGECNT"}
!ll&&!rl&&r
000
MC/DC0%0/2
$msg > 0 and $msg <= $self->{"MESSAGECNT"}
$msg > 0 $msg <= $self->{"MESSAGECNT"}
Truth table0%0/3
A and B
A= $msg > 0B= $msg <= $self->{"MESSAGECNT"}
ABresult
0X0
100
111
104 }
105  
106 0 ▶sub lock_release {
Subroutine lock_release: not called CC=1 SCAR=6.9
Pod documented
107 0 my $self = shift;
108 0 eval { $self->{LOCK_FH}->close; };
109 0 unlink "$self->{MAILDIR}/new/.mpopd.lock";
110 }
111  
112 # $message starts at 1
113 0 ▶sub retrieve {
Subroutine retrieve: not called CC=4 SCAR=30.0
Pod documented
114 0 my ($self, $message, $output_fh, $mbox_destined) = @_;
115 # $self->{MAILDIR} is the full /path/file and starts at 0 !
116 0 local *MSG;
117 0 open MSG, "$self->{MAILDIR}/new/$self->{MAILDIR_FILES}->[$message - 1]";
118 0 ▶ while (<MSG>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline MSG)) 0 0 0
119 0 chomp;
120 # byte-stuff lines starting with .
121 0 ▶ s/^\./\.\./o unless $mbox_destined;
Branch: 1 branch
BranchTrueFalseTotal
unless $mbox_destined 0 0 0
122 0 ▶ my $line = $mbox_destined ? "$_\n" : "$_$CRLF";
Branch: 1 branch
BranchTrueFalseTotal
$mbox_destined ? : 0 0 0
123 0 $output_fh->print($line);
124 0 $self->lock_update;
125 }
126 0 close MSG;
127 }
128  
129 # $message starts at 1
130 # returns number of bytes
131 0 ▶sub top {
Subroutine top: not called CC=5 SCAR=34.0
Pod documented
132 0 my ($self, $message, $output_fh, $body_lines) = @_;
133 0 my $top_bytes = 0;
134 0 local *MSG;
135 0 open MSG, $self->_msg2filename($message);
136 # print the headers
137 0 ▶ while (<MSG>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline MSG)) 0 0 0
138 0 chomp;
139 0 $self->lock_update;
140 0 my $out = "$_$CRLF";
141 0 $output_fh->print($out);
142 0 $top_bytes += length($out);
143 0 ▶ last if /^\s+$/;
Branch: 1 branch
BranchTrueFalseTotal
if /^\s+$/ 0 0 0
144 }
145 0 my $cnt = 0;
146 # print the TOP arg number of body lines
147 0 ▶ while (<MSG>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline MSG)) 0 0 0
148 0 ++$cnt;
149 0 ▶ last if $cnt > $body_lines;
Branch: 1 branch
BranchTrueFalseTotal
if $cnt > $body_lines 0 0 0
150 # byte-stuff lines starting with .
151 0 s/^\./\.\./o;
152 0 $self->lock_update;
153 0 chomp;
154 0 my $out = "$_$CRLF";
155 0 $output_fh->print($out);
156 0 $top_bytes += length($out);
157 }
158 0 close MSG;
159 0 $output_fh->print(".$CRLF");
160 0 $top_bytes;
161 }
162  
163 0 ▶sub is_deleted {
Subroutine is_deleted: not called CC=1 SCAR=6.9
Pod documented
164 0 my ($self, $message) = @_;
165 0 return $self->{DELETE}->{$message};
166 }
167  
168 0 ▶sub delete {
Subroutine delete: not called CC=1 SCAR=6.9
Pod documented
169 0 my ($self, $message) = @_;
170 0 $self->lock_refresh;
171 0 $self->{DELETE}->{$message} = 1;
172 0 $self->{DELMESSAGECNT} += 1;
173 0 $self->{DELTOTALOCTETS} += $self->{OCTETS}->{$message};
174 }
175  
176 0 ▶sub flush_delete {
Subroutine flush_delete: not called CC=3 SCAR=24.8
Pod documented
177 0 my $self = shift;
178 0 for (1..$self->{MESSAGECNT}) {
179 0 ▶ if ($self->{MAILBOX}->is_deleted($_)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"MAILBOX"}->is_deleted($_)) 0 0 0
180 0 unlink $self->_msg2filename($_);
181 }
182 }
183 }
184  
185 0 ▶sub reset {
Subroutine reset: not called CC=1 SCAR=6.9
Pod documented
186 0 my $self = shift;
187 0 $self->lock_refresh;
188 0 $self->{DELETE} = {};
189 0 $self->{DELMESSAGECNT} = 0;
190 0 $self->{DELTOTALOCTETS} = 0;
191 }
192  
193 0 ▶sub octets {
Subroutine octets: not called CC=2 SCAR=17.9
Pod documented
194 0 my ($self, $message) = @_;
195 0 ▶ if (defined $message) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $message) { } 0 0 0
196 0 $self->{MSG2OCTETS}->{$message};
197 } else {
198 0 $self->{TOTALOCTETS} - $self->{DELTOTALOCTETS};
199 }
200 }
201  
202 0 ▶sub messages {
Subroutine messages: not called CC=1 SCAR=6.9
Pod documented
203 0 my ($self) = @_;
204 0 $self->{MESSAGECNT} - $self->{DELMESSAGECNT};
205 }
206  
207 0 ▶sub uidl {
Subroutine uidl: not called CC=1 SCAR=6.9
Pod documented
208 0 my ($self, $message) = @_;
209 0 $self->{MSG2UIDL}->{$message};
210 }
211  
212 4 1;
213  
214 __END__
« POP3/Server.pm ↑ Summary POP3/Daemon.pm »