Simple.pm

filter: Statementstmt 5.3% 14 / 262 Branchbran 0.9% 1 / 104 Conditioncond 0.0% 0 / 29 MC/DCmcdc 0.0% 0 / 17 Subroutinesub 4.6% 4 / 87 Podpod 100% 25 / 25 totaltotal 8.4% 44 / 524 CC 68 SCAR 35.0
CC
68
Cov
4%
new
210.0
_process_cmd
90.0
__ANON__
42.0
CRAP 33.1
↑ Summary imap.pl »
1 package Net::IMAP::Simple;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use IO::Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use vars qw[$VERSION];
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶$VERSION = $1 if('$Id: Simple.pm,v 1.16_1 2006/10/02 11:16:37 cfaber Exp $' =~ /,v ([\d_.]+) /);
Branch: 1 branch
BranchTrueFalseTotal
if "\$Id: Simple.pm,v 1.16_1 2006/10/02 11:16:37 cfaber Exp \$" =~ /,v ([\d_.]+) / 1 0 1
8  
9 =head1 NAME
10  
11 Net::IMAP::Simple - Perl extension for simple IMAP account handling.
12  
13 =head1 SYNOPSIS
14  
15 # Duh
16 use Net::IMAP::Simple;
17 use Email::Simple;
18  
19 # Create the object
20 my $imap = Net::IMAP::Simple->new('imap.example.com') ||
21 die "Unable to connect to IMAP: $Net::IMAP::Simple::errstr\n";
22  
23 # Log on
24 if(!$imap->login('user','pass')){
25 print STDERR "Login failed: " . $imap->errstr . "\n";
26 exit(64);
27 }
28  
29 # Print the subject's of all the messages in the INBOX
30 my $nm = $imap->select('INBOX');
31  
32 for(my $i = 1; $i <= $nm; $i++){
33 if($imap->seen($i)){
34 print "*";
35 } else {
36 print " ";
37 }
38  
39 my $es = Email::Simple->new(join '', @{ $imap->top($i) } );
40  
41 printf("[%03d] %s\n", $i, $es->header('Subject'));
42 }
43  
44 $imap->quit;
45  
46 =head1 DESCRIPTION
47  
48 This module is a simple way to access IMAP accounts.
49  
50 =head1 OBJECT CREATION METHOD
51  
52 =over 4
53  
54 =item new
55  
56 my $imap = Net::IMAP::Simple->new( $server [ :port ]); OR my $imap = Net::IMAP::Simple->new( $server [, option_name => option_value ] );
57  
58  
59 This class method constructs a new C<Net::IMAP::Simple> object. It takes one required parameter which is the server to connect to, and additional optional parameters.
60  
61 The server parameter may specify just the server, or both the server and port number. To specify an alternate port, seperate it from the server with a colon (C<:>), C<example.com:5143>.
62  
63 On success an object is returned. On failure, nothing is returned and an error message is set to $Net::IMAP::Simple.
64  
65 =head2 OPTIONS:
66  
67 Options are provided as a hash to new()
68  
69 =item port => int
70  
71 Assign the port number (default: 143)
72  
73 =item timeout => int (default: 90)
74  
75 Connection timeout in seconds.
76  
77 =item retry => int (default: 1)
78  
79 Attempt to retry the connection attmpt (x) times before giving up
80  
81 =item retry_delay => int (default: 5)
82  
83 Wait (x) seconds before retrying a connection attempt
84  
85 =item use_v6 => BOOL
86  
87 If set to true, attempt to use IPv6 sockets rather than IPv4 sockets.
88  
89 This option requires the IO::Socket::INET6 module
90  
91 =item bindaddr => str
92  
93 Assign a local address to bind
94  
95 =item use_select_cache => BOOL
96  
97 Enable select() caching internally
98  
99 =item select_cache_ttl => int
100  
101 The number of seconds to allow a select cache result live before running $imap->select() again.
102  
103 =item debug => BOOL | \*HANDLE
104  
105 Enable debugging output. If \*HANDLE is a valid file handle, debugging will be written to it. Otherwise debugging will be written to STDOUT
106  
107 =cut
108  
109 sub new {
110 0 ▶ my ( $class, $server, %opts) = @_;
Subroutine new: not called CC=14 SCAR=53.5
Pod documented
111  
112 0 my $self = bless {
113 count => -1,
114 } => $class;
115  
116 0 my ($srv, $prt) = split(/:/, $server, 2);
117 0 ▶ $prt ||= ($opts{port} ? $opts{port} : $self->_port);
Branch: 1 branch
BranchTrueFalseTotal
$opts{'port'} ? : 0 0 0
Condition0%0/3
$prt ||= $opts{"port"} ? $opts{"port"} : $self->_port
l!l&&r!l&&!r
000
MC/DC0%0/2
$prt ||= $opts{"port"} ? $opts{"port"} : $self->_port
$prt $opts{"port"} ? $opts{"port"} : $self->_port
Truth table0%0/3
A ||= B
A= $prtB= $opts{"port"} ? $opts{"port"} : $self->_port
ABresult
1X1
011
000
118  
119 0 $self->{server} = $srv;
120 0 $self->{port} = $prt;
121 0 ▶ $self->{timeout} = ($opts{timeout} ? $opts{timeout} : $self->_timeout);
Branch: 1 branch
BranchTrueFalseTotal
$opts{'timeout'} ? : 0 0 0
122 0 ▶ $self->{use_v6} = ($opts{use_v6} ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$opts{'use_v6'} ? : 0 0 0
123 0 ▶ $self->{retry} = ($opts{retry} ? $opts{retry} : $self->_retry);
Branch: 1 branch
BranchTrueFalseTotal
$opts{'retry'} ? : 0 0 0
124 0 ▶ $self->{retry_delay} = ($opts{retry_delay} ? $opts{retry_delay} : $self->_retry_delay);
Branch: 1 branch
BranchTrueFalseTotal
$opts{'retry_delay'} ? : 0 0 0
125 0 $self->{bindaddr} = $opts{bindaddr};
126 0 $self->{use_select_cache} = $opts{use_select_cache};
127 0 $self->{select_cache_ttl} = $opts{select_cache_ttl};
128 0 $self->{debug} = $opts{debug};
129
130  
131 # Pop the port off the address string if it's not an IPv6 IP address
132 0 ▶ if(!$self->{use_v6} && $self->{server} =~ /^[A-Fa-f0-9]{4}:[A-Fa-f0-9]{4}:/ && $self->{server} =~ s/:(\d+)$//g){
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{"use_v6"} and $self->{"server"} =~ /^[A-Fa-f0-9]{4}:[A-Fa-f0-9]{4}:/ and $self->{"server"} =~ s/:(\d+)$//g) 0 0 0
Condition0%0/6
not $self->{"use_v6"} and $self->{"server"} =~ /^[A-Fa-f0-9]{4}:[A-Fa-f0-9]{4}:/ and $self->{"server"} =~ s/:(\d+)$//g
!ll&&!rl&&r
000
not $self->{"use_v6"} and $self->{"server"} =~ /^[A-Fa-f0-9]{4}:[A-Fa-f0-9]{4}:/
!ll&&!rl&&r
000
MC/DC0%0/3
not $self->{"use_v6"} and $self->{"server"} =~ /^[A-Fa-f0-9]{4}:[A-Fa-f0-9]{4}:/ and $self->{"server"} =~ s/:(\d+)$//g
not $self->{"use_v6"} $self->{"server"} =~ /^[A-Fa-f0-9]{4}:[A-Fa-f0-9]{4}:/ $self->{"server"} =~ s/:(\d+)$//g
Truth table0%0/4
not(A) and B and C
A= not $self->{"use_v6"}B= $self->{"server"} =~ /^[A-Fa-f0-9]{4}:[A-Fa-f0-9]{4}:/C= $self->{"server"} =~ s/:(\d+)$//g
ABCresult
0XX0
10X0
1100
1111
133 0 $self->{port} = $1;
134 }
135
136 0 my $c;
137 0 ▶ for(my $i = 0; $i <= $self->{retry}; $i++){
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $self->{"retry"}) 0 0 0
138 0 ▶ if($self->{sock} = $self->_connect){
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'sock'} = $self->_connect) { } 0 0 0
elsif ($i < $self->{'retry'}) { } 0 0 0
139 0 $c = 1;
140 0 last;
141 } elsif ($i < $self->{retry}) {
142 0 select(undef, undef, undef, $self->{retry_delay});
143 }
144 }
145  
146 0 ▶ if(!$c){
Branch: 1 branch
BranchTrueFalseTotal
unless ($c) 0 0 0
147 0 $@ =~ s/IO::Socket::INET6?: //g;
148 0 $Net::IMAP::Simple::errstr = "connection failed $@";
149 0 return;
150 }
151  
152  
153 0 $self->_sock->getline();
154  
155 0 return $self;
156 }
157  
158 sub _connect {
159 0 ▶ my ($self) = @_;
Subroutine _connect: not called CC=3 SCAR=24.8
160 0 my $sock;
161 0 ▶ if($self->{use_v6}){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"use_v6"}) 0 0 0
162 0 require 'IO::Socket::INET6';
163 0 import IO::Socket::INET6;
164 }
165  
166 $sock = $self->_sock_from->new(
167 PeerAddr => $self->{server},
168 PeerPort => $self->{port},
169 Timeout => $self->{timeout},
170 Proto => 'tcp',
171 0 ▶ ($self->{bindaddr} ? { LocalAddr => $self->{bindaddr} } : ())
Branch: 1 branch
BranchTrueFalseTotal
$self->{'bindaddr'} ? : 0 0 0
172 );
173  
174 0 return $sock;
175 }
176  
177 0 ▶sub _port { 143 }
Subroutine _port: not called CC=1 SCAR=6.9
178 0 ▶sub _sock { $_[0]->{sock} }
Subroutine _sock: not called CC=1 SCAR=6.9
179 0 ▶sub _count { $_[0]->{count} }
Subroutine _count: not called CC=1 SCAR=6.9
180 0 ▶sub _last { $_[0]->{last} }
Subroutine _last: not called CC=1 SCAR=6.9
181 0 ▶sub _timeout { 90 }
Subroutine _timeout: not called CC=1 SCAR=6.9
182 0 ▶sub _retry { 1 }
Subroutine _retry: not called CC=1 SCAR=6.9
183 0 ▶sub _retry_delay { 5 }
Subroutine _retry_delay: not called CC=1 SCAR=6.9
184 0 ▶sub _sock_from { $_[0]->{use_v6} ? 'IO::Socket::INET6' : 'IO::Socket::INET' }
Subroutine _sock_from: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{'use_v6'} ? : 0 0 0
185  
186 =pod
187  
188 =head1 METHODS
189  
190 =item login
191  
192 my $inbox_msgs = $imap->login($user, $passwd);
193  
194 This method takes two required parameters, a username and password. This pair
195 is authenticated against the server. If authentication is successful TRUE (1) will be returned
196  
197 Nothing is returned on failure and the errstr() error handler is set with the error message.
198  
199 =cut
200  
201 sub login {
202 0 ▶ my ( $self, $user, $pass ) = @_;
Subroutine login: not called CC=1 SCAR=6.9
Pod documented
203  
204 return $self->_process_cmd (
205 cmd => [LOGIN => qq[$user "$pass"]],
206 0 ▶ final => sub { 1 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
207 ▶ process => sub { },
Subroutine __ANON__: not called CC=1 SCAR=0.0
208 0 );
209 }
210  
211 =pod
212  
213 =item select
214  
215 my $num_messages = $imap->select($folder);
216  
217 Selects a folder named in the single required parameter. The number of messages in that folder is returned on success. On failure, nothing is returned and the errstr() error handler is set with the error message.
218  
219 =cut
220  
221 sub select {
222 0 ▶ my ( $self, $mbox ) = @_;
Subroutine select: not called CC=5 SCAR=34.0
Pod documented
223  
224 0 ▶ $mbox = 'INBOX' unless $mbox;
Branch: 1 branch
BranchTrueFalseTotal
unless $mbox 0 0 0
225  
226 0 $self->{working_box} = $mbox;
227  
228 0 ▶ if($self->{use_select_cache} && (time - $self->{BOXES}->{ $mbox }->{proc_time}) <= $self->{select_cache_ttl}){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"use_select_cache"} and time - $self->{"BOXES"}{$mbox}{"proc_time"} <= $self->{"select_cache_ttl"}) 0 0 0
Condition0%0/3
$self->{"use_select_cache"} and time - $self->{"BOXES"}{$mbox}{"proc_time"} <= $self->{"select_cache_ttl"}
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"use_select_cache"} and time - $self->{"BOXES"}{$mbox}{"proc_time"} <= $self->{"select_cache_ttl"}
$self->{"use_select_cache"} time - $self->{"BOXES"}{$mbox}{"proc_time"} <= $self->{"select_cache_ttl"}
Truth table0%0/3
A and B
A= $self->{"use_select_cache"}B= time - $self->{"BOXES"}{$mbox}{"proc_time"} <= $self->{"select_cache_ttl"}
ABresult
0X0
100
111
229 0 return $self->{BOXES}->{$mbox}->{messages};
230 }
231  
232 0 $self->{BOXES}->{$mbox}->{proc_time} = time;
233  
234 0 my $t_mbox = $mbox;
235  
236 $self->_process_cmd(
237 cmd => [SELECT => _escape($t_mbox)],
238 0 ▶ final => sub { $self->{last} = $self->{BOXES}->{$mbox}->{messages} },
Subroutine __ANON__: not called CC=1 SCAR=6.9
239 process => sub {
240 0 ▶ if($_[0] =~ /^\*\s+(\d+)\s+EXISTS/i){
Subroutine __ANON__: not called CC=6 SCAR=37.4
Branch: 4 branches
BranchTrueFalseTotal
if ($_[0] =~ /^\*\s+(\d+)\s+EXISTS/i) { } 0 0 0
elsif ($_[0] =~ /^\*\s+FLAGS\s+\((.*?)\)/i) { } 0 0 0
elsif ($_[0] =~ /^\*\s+(\d+)\s+RECENT/i) { } 0 0 0
elsif ($_[0] =~ /^\*\s+OK\s+\[(.*?)\s+(.*?)\]/i) { } 0 0 0
241 0 $self->{BOXES}->{$mbox}->{messages} = $1;
242 } elsif($_[0] =~ /^\*\s+FLAGS\s+\((.*?)\)/i){
243 0 $self->{BOXES}->{$mbox}->{flags} = [ split(/\s+/, $1) ];
244 } elsif($_[0] =~ /^\*\s+(\d+)\s+RECENT/i){
245 0 $self->{BOXES}->{$mbox}->{recent} = $1;
246 } elsif($_[0] =~ /^\*\s+OK\s+\[(.*?)\s+(.*?)\]/i){
247 0 my ($flag, $value) = ($1, $2);
248 0 ▶ if($value =~ /\((.*?)\)/){
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /\((.*?)\)/) { } 0 0 0
249 0 $self->{BOXES}->{$mbox}->{sflags}->{$flag} = [split(/\s+/, $1)];
250 } else {
251 0 $self->{BOXES}->{$mbox}->{oflags}->{$flag} = $value;
252 }
253 }
254 },
255 0 ▶ ) || return;
Branch: 1 branch
BranchTrueFalseTotal
$self->_process_cmd("cmd", ["SELECT", _escape($t_mbox)], "final", sub { $self->{"last"} = $self->{"BOXES"}{$mbox}{"messages"}; } , "process", sub { if ($_[0] =~ /^\*\s+(\d+)\s+EXISTS/i) { $self->{"BOXES"}{$mbox}{"messages"} = $1; } elsif ($_[0] =~ /^\*\s+FLAGS\s+\((.*?)\)/i) { $self->{"BOXES"}{$mbox}{"flags"} = [split(/\s+/, $1, 0)]; } elsif ($_[0] =~ /^\*\s+(\d+)\s+RECENT/i) { $self->{"BOXES"}{$mbox}{"recent"} = $1; } elsif ($_[0] =~ /^\*\s+OK\s+\[(.*?)\s+(.*?)\]/i) { my($flag, $value) = ($1, $2); if ($value =~ /\((.*?)\)/) { $self->{"BOXES"}{$mbox}{"sflags"}{$flag} = [split(/\s+/, $1, 0)]; } else { $self->{"BOXES"}{$mbox}{"oflags"}{$flag} = $value; } ; } ; } ) or return 0 0 0
256  
257 return $self->{last}
258 0 }
259  
260 =pod
261  
262 =item messages
263  
264 print "Messages in Junk Mail -- " . $imap->messages("INBOX.Junk Mail") . "\n";
265  
266 This method is an alias for $imap->select
267  
268 =cut
269  
270 sub messages {
271 0 ▶ my ($self, $folder) = @_;
Subroutine messages: not called CC=1 SCAR=6.9
Pod documented
272 0 return $self->select($folder);
273 }
274  
275 =pod
276  
277 =item flags
278  
279 print "Avaliable server flags: " . join(", ", $imap->flags) . "\n";
280  
281 This method accepts an optional folder name and returns the current avaliable server flags as a list, for the selected folder. If no folder name is provided the last folder $imap->select'ed will be used.
282  
283 This method uses caching.
284  
285 =cut
286  
287 sub flags {
288 0 ▶ my ($self, $folder) = @_;
Subroutine flags: not called CC=1 SCAR=6.9
Pod documented
289  
290 0 $self->select($folder);
291 0 return @{ $self->{BOXES}->{ $self->current_box }->{flags} };
292 }
293  
294  
295 =pod
296  
297 =item recent
298  
299 print "Recent messages value: " . $imap->recent . "\n";
300  
301 This method accepts an optional folder name and returns the 'RECENT' value provided durning a SELECT result set. If no folder name is provided the last folder $imap->select'ed will be used.
302  
303 This method uses caching.
304  
305 =cut
306  
307 sub recent {
308 0 ▶ my ($self, $folder) = @_;
Subroutine recent: not called CC=1 SCAR=6.9
Pod documented
309  
310 0 $self->select($folder);
311 0 return $self->{BOXES}->{ $self->current_box }->{recent};
312 }
313  
314  
315 =pod
316  
317 =item current_box
318  
319 print "Current Mail Box folder: " . $imap->current_box . "\n";
320  
321 This method returns the current working mail box folder name.
322  
323 =cut
324  
325 sub current_box {
326 0 ▶ my ($self) = @_;
Subroutine current_box: not called CC=2 SCAR=17.9
Pod documented
327 0 ▶ return ($self->{working_box} ? $self->{working_box} : 'INBOX');
Branch: 1 branch
BranchTrueFalseTotal
$self->{'working_box'} ? : 0 0 0
328 }
329  
330 =pod
331  
332 =item top
333  
334 my $header = $imap->top( $message_number );
335 print for @{$header};
336  
337 This method accepts a message number as its required parameter. That message
338 will be retrieved from the currently selected folder. On success this method
339 returns a list reference containing the lines of the header. Nothing is
340 returned on failure and the errstr() error handler is set with the error message.
341  
342 =cut
343  
344 sub top {
345 0 ▶ my ( $self, $number ) = @_;
Subroutine top: not called CC=1 SCAR=6.9
Pod documented
346
347 0 my @lines;
348 $self->_process_cmd(
349 cmd => [FETCH => qq[$number rfc822.header]],
350 0 ▶ final => sub { \@lines },
Subroutine __ANON__: not called CC=1 SCAR=6.9
351 0 ▶ process => sub { push @lines, $_[0] if $_[0] =~ /^(?: \s+\S+ | [^:]+: )/x },
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] =~ /^(?: \s+\S+ | [^:]+: )/x 0 0 0
352 0 );
353 }
354  
355 =pod
356  
357 =item seen
358  
359 print "Seen it!" if $imap->seen( $message_number );
360  
361 A message number is the only required parameter for this method. The message's
362 C<\Seen> flag will be examined and if the message has been seen a true
363 value is returned. All other failures return a false value and the errstr() error handler is set with the error message.
364  
365 =cut
366  
367 sub seen {
368 0 ▶ my ( $self, $number ) = @_;
Subroutine seen: not called CC=1 SCAR=6.9
Pod documented
369
370 0 my $lines = '';
371 $self->_process_cmd(
372 cmd => [FETCH=> qq[$number (FLAGS)]],
373 0 ▶ final => sub { $lines =~ /\\Seen/i },
Subroutine __ANON__: not called CC=1 SCAR=6.9
374 0 ▶ process => sub { $lines .= $_[0] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
375 0 );
376 }
377  
378 =pod
379  
380 =item list
381  
382 my $message_size = $imap->list($message_number);
383 my $mailbox_sizes = $imap->list;
384  
385 This method returns size information for a message, as indicated in the
386 single optional parameter, or all messages in a mailbox. When querying a
387 single message a scalar value is returned. When listing the entire
388 mailbox a hash is returned. On failure, nothing is returned and the errstr() error handler is set with the error message.
389  
390 =cut
391  
392 sub list {
393 0 ▶ my ( $self, $number ) = @_;
Subroutine list: not called CC=2 SCAR=17.9
Pod documented
394  
395 0 ▶ my $messages = $number || '1:' . $self->_last;
Condition0%0/2
$number || '1:' . $self->_last
l!l
00
MC/DC0%0/1
$number || '1:' . $self->_last
$number
Truth table0%0/2
A
A= $number
Aresult
11
00
396 0 my %list;
397 $self->_process_cmd(
398 cmd => [FETCH => qq[$messages RFC822.SIZE]],
399 0 ▶ final => sub { $number ? $list{$number} : \%list },
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
$number ? : 0 0 0
400 process => sub {
401 0 ▶ if ($_[0] =~ /^\*\s+(\d+).*RFC822.SIZE\s+(\d+)/i) {
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0] =~ /^\*\s+(\d+).*RFC822.SIZE\s+(\d+)/i) 0 0 0
402 0 $list{$1} = $2;
403 }
404 },
405 0 );
406 }
407  
408 =pod
409  
410 =item get
411  
412 my $message = $imap->get( $message_number );
413 print for @{$message};
414  
415 This method fetches a message and returns its lines in a list reference.
416 On failure, nothing is returned and the errstr() error handler is set with the error message.
417  
418 =cut
419  
420 sub get {
421 0 ▶ my ( $self, $number ) = @_;
Subroutine get: not called CC=1 SCAR=6.9
Pod documented
422  
423 0 my @lines;
424 $self->_process_cmd(
425 cmd => [FETCH => qq[$number rfc822]],
426 0 ▶ final => sub { pop @lines; \@lines },
Subroutine __ANON__: not called CC=1 SCAR=6.9
427 process => sub {
428 0 ▶ if($_[0] !~ /^\* \d+ FETCH/){
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
if (not $_[0] =~ /^\* \d+ FETCH/) 0 0 0
429 0 push @lines, join(' ', @_);
430 }
431 },
432 0 );
433  
434 }
435  
436 =pod
437  
438 =item getfh
439  
440 my $file = $imap->getfh( $message_number );
441 print <$file>;
442  
443 On success this method returns a file handle pointing to the message
444 identified by the required parameter. On failure, nothing is returned and the errstr() error handler is set with the error message.
445  
446 =cut
447  
448 sub getfh {
449 0 ▶ my ( $self, $number ) = @_;
Subroutine getfh: not called CC=1 SCAR=6.9
Pod documented
450
451 0 my $file = IO::File->new_tmpfile;
452 0 my $buffer;
453 $self->_process_cmd(
454 cmd => [FETCH => qq[$number rfc822]],
455 0 ▶ final => sub { seek $file, 0, 0; $file },
Subroutine __ANON__: not called CC=1 SCAR=6.9
456 process => sub {
457 0 ▶ if($_[0] !~ /^\* \d+ FETCH/){
Subroutine __ANON__: not called CC=3 SCAR=24.8
Branch: 1 branch
BranchTrueFalseTotal
if (not $_[0] =~ /^\* \d+ FETCH/) 0 0 0
458 0 ▶ defined($buffer) and print $file $buffer;
Branch: 1 branch
BranchTrueFalseTotal
defined $buffer and print $file $buffer 0 0 0
459 0 $buffer = $_[0];
460 }
461 },
462 0 );
463 }
464  
465 =pod
466  
467 =item quit
468  
469 $imap->quit;
470  
471 OR
472  
473 $imap->quit(BOOL);
474  
475 This method logs out of the IMAP server, expunges the selected mailbox,
476 and closes the connection. No error message will ever be returned from this method.
477  
478 Optionally if BOOL is TRUE (1) then a hard quit is preformed which closes the socket connection. This hard quit will still issue both EXPUNGE and LOGOUT commands however the response is ignored and the socket is closed after issuing the commands.
479  
480 =cut
481  
482 sub quit {
483 0 ▶ my ( $self, $hq ) = @_;
Subroutine quit: not called CC=2 SCAR=17.9
Pod documented
484 0 $self->_send_cmd('EXPUNGE');
485  
486 0 ▶ if(!$hq){
Branch: 1 branch
BranchTrueFalseTotal
if (not $hq) { } 0 0 0
487 0 ▶ $self->_process_cmd(cmd => ['LOGOUT'], final => sub {}, process => sub{});
Subroutine __ANON__: not called CC=1 SCAR=6.9
488 } else {
489 0 $self->_send_cmd('LOGOUT');
490 }
491  
492 0 $self->_sock->close;
493 0 return 1;
494 }
495  
496 =pod
497  
498 =item last
499  
500 my $message_number = $imap->last;
501  
502 This method retuns the message number of the last message in the selected
503 mailbox, since the last time the mailbox was selected. On failure, nothing
504 is returned and the errstr() error handler is set with the error message.
505  
506 =cut
507  
508 0 ▶sub last { shift->_last }
Subroutine last: not called CC=1 SCAR=6.9
Pod documented
509  
510 =pod
511  
512 =item delete
513  
514 print "Gone!" if $imap->delete( $message_number );
515  
516 This method deletes a message from the selected mailbox. On success it
517 returns true. False on failure and the errstr() error handler is set with the error message.
518  
519 =cut
520  
521 sub delete {
522 0 ▶ my ( $self, $number ) = @_;
Subroutine delete: not called CC=1 SCAR=6.9
Pod documented
523
524 $self->_process_cmd(
525 cmd => [STORE => qq[$number +FLAGS (\\Deleted)]],
526 0 ▶ final => sub { 1 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
527 ▶ process => sub { },
Subroutine __ANON__: not called CC=1 SCAR=0.0
528 0 );
529 }
530  
531 sub _process_list {
532 0 ▶ my ($self, $line) = @_;
Subroutine _process_list: not called CC=6 SCAR=37.4
533 0 ▶ $self->_debug(caller, __LINE__, '_process_list', $line) if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
534  
535 0 my @list;
536 0 ▶ if ( $line =~ /^\*\s+(LIST|LSUB).*\s+\{\d+\}\s*$/i ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($line =~ /^\*\s+(LIST|LSUB).*\s+\{\d+\}\s*$/i) { } 0 0 0
elsif ($line =~ /^\*\s+(LIST|LSUB).*\s+(\".*?\")\s*$/i or $line =~ /^\*\s+(LIST|LSUB).*\s+(\S+)\s*$/i) { } 0 0 0
537 0 chomp( my $res = $self->_sock->getline );
538 0 $res =~ s/\r//;
539 0 _escape($res);
540 0 push @list, $res;
541  
542 0 ▶ $self->_debug(caller, __LINE__, '_process_list', $res) if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
543 ▶ } elsif ( $line =~ /^\*\s+(LIST|LSUB).*\s+(\".*?\")\s*$/i ||
Condition0%0/3
$line =~ /^\*\s+(LIST|LSUB).*\s+(\".*?\")\s*$/i or $line =~ /^\*\s+(LIST|LSUB).*\s+(\S+)\s*$/i
l!l&&r!l&&!r
000
MC/DC0%0/2
$line =~ /^\*\s+(LIST|LSUB).*\s+(\".*?\")\s*$/i or $line =~ /^\*\s+(LIST|LSUB).*\s+(\S+)\s*$/i
$line =~ /^\*\s+(LIST|LSUB).*\s+(\".*?\")\s*$/i $line =~ /^\*\s+(LIST|LSUB).*\s+(\S+)\s*$/i
Truth table0%0/3
A or B
A= $line =~ /^\*\s+(LIST|LSUB).*\s+(\".*?\")\s*$/iB= $line =~ /^\*\s+(LIST|LSUB).*\s+(\S+)\s*$/i
ABresult
1X1
011
000
544 $line =~ /^\*\s+(LIST|LSUB).*\s+(\S+)\s*$/i ) {
545 0 push @list, $2;
546 }
547 0 @list;
548 }
549  
550 =pod
551  
552 =item mailboxes
553  
554 my @boxes = $imap->mailboxes;
555 my @folders = $imap->mailboxes("Mail/%");
556 my @lists = $imap->mailboxes("lists/perl/*", "/Mail/");
557  
558 This method returns a list of mailboxes. When called with no arguments it
559 recurses from the IMAP root to get all mailboxes. The first optional
560 argument is a mailbox path and the second is the path reference. RFC 3501
561 section 6.3.8 has more information.
562  
563 On failure nothing is returned and the errstr() error handler is set with the error message.
564  
565 =cut
566  
567 sub mailboxes {
568 0 ▶ my ( $self, $box, $ref ) = @_;
Subroutine mailboxes: not called CC=3 SCAR=24.8
Pod documented
569
570 0 ▶ $ref ||= '""';
Condition0%0/2
$ref ||= "\"\""
l!l
00
MC/DC0%0/1
$ref ||= "\"\""
$ref
Truth table0%0/2
A
A= $ref
Aresult
11
00
571 0 my @list;
572 0 ▶ if ( ! defined $box ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $box) { } 0 0 0
573 # recurse, should probably follow
574 # RFC 2683: 3.2.1.1. Listing Mailboxes
575 return $self->_process_cmd(
576 cmd => [LIST => qq[$ref *]],
577 0 ▶ final => sub { _unescape($_) for @list; @list },
Subroutine __ANON__: not called CC=2 SCAR=17.9
578 0 ▶ process => sub { push @list, $self->_process_list($_[0]);},
Subroutine __ANON__: not called CC=1 SCAR=6.9
579 0 );
580 } else {
581 return $self->_process_cmd(
582 cmd => [LIST => qq[$ref $box]],
583 0 ▶ final => sub { _unescape($_) for @list; @list },
Subroutine __ANON__: not called CC=2 SCAR=17.9
584 0 ▶ process => sub { push @list, $self->_process_list($_[0]) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
585 0 );
586 }
587 }
588  
589 =pod
590  
591 =item mailboxes_subscribed
592  
593 my @boxes = $imap->mailboxes_subscribed;
594 my @folders = $imap->mailboxes_subscribed("Mail/%");
595 my @lists = $imap->mailboxes_subscribed("lists/perl/*", "/Mail/");
596  
597 This method returns a list of mailboxes subscribed to. When called with no
598 arguments it recurses from the IMAP root to get all mailboxes. The first optional
599 argument is a mailbox path and the second is the path reference. RFC 3501
600 has more information.
601  
602 On failure nothing is returned and the errstr() error handler is set with the error message.
603  
604 =cut
605  
606 sub mailboxes_subscribed {
607 0 ▶ my ( $self, $box, $ref ) = @_;
Subroutine mailboxes_subscribed: not called CC=3 SCAR=24.8
Pod documented
608
609 0 ▶ $ref ||= '""';
Condition0%0/2
$ref ||= "\"\""
l!l
00
MC/DC0%0/1
$ref ||= "\"\""
$ref
Truth table0%0/2
A
A= $ref
Aresult
11
00
610 0 my @list;
611 0 ▶ if ( ! defined $box ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $box) { } 0 0 0
612 # recurse, should probably follow
613 # RFC 2683: 3.2.2. Subscriptions
614 return $self->_process_cmd(
615 cmd => [LSUB => qq[$ref *]],
616 0 ▶ final => sub { _unescape($_) for @list; @list },
Subroutine __ANON__: not called CC=2 SCAR=17.9
617 0 ▶ process => sub { push @list, $self->_process_list($_[0]) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
618 0 );
619 } else {
620 return $self->_process_cmd(
621 cmd => [LSUB => qq[$ref $box]],
622 0 ▶ final => sub { _unescape($_) for @list; @list },
Subroutine __ANON__: not called CC=2 SCAR=17.9
623 0 ▶ process => sub { push @list, $self->_process_list($_[0]) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
624 0 );
625 }
626 }
627  
628 =pod
629  
630 =item create_mailbox
631  
632 print "Created" if $imap->create_mailbox( "/Mail/lists/perl/advocacy" );
633  
634 This method creates the mailbox named in the required argument. Returns true
635 on success, false on failure and the errstr() error handler is set with the error message.
636  
637 =cut
638  
639 sub create_mailbox {
640 0 ▶ my ( $self, $box ) = @_;
Subroutine create_mailbox: not called CC=1 SCAR=6.9
Pod documented
641 0 _escape( $box );
642
643 return $self->_process_cmd(
644 cmd => [CREATE => $box],
645 0 ▶ final => sub { 1 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
646 ▶ process => sub { },
Subroutine __ANON__: not called CC=1 SCAR=0.0
647 0 );
648 }
649  
650 =pod
651  
652 =item expunge_mailbox
653  
654 print "Expunged" if $imap->expunge_mailbox( "/Mail/lists/perl/advocacy" );
655  
656 This method removes all mail marked as deleted in the mailbox named in
657 the required argument. Returns true on success, false on failure and the errstr() error handler is set with the error message.
658  
659 =cut
660  
661 sub expunge_mailbox {
662 0 ▶ my ($self, $box) = @_;
Subroutine expunge_mailbox: not called CC=2 SCAR=17.9
Pod documented
663 0 ▶ return if !$self->select($box);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->select($box) 0 0 0
664  
665 return $self->_process_cmd(
666 cmd => ['EXPUNGE'],
667 0 ▶ final => sub { 1 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
668 ▶ process => sub { },
Subroutine __ANON__: not called CC=1 SCAR=0.0
669 0 );
670 }
671  
672 =pod
673  
674 =item delete_mailbox
675  
676 print "Deleted" if $imap->delete_mailbox( "/Mail/lists/perl/advocacy" );
677  
678 This method deletes the mailbox named in the required argument. Returns true
679 on success, false on failure and the errstr() error handler is set with the error message.
680  
681 =cut
682  
683 sub delete_mailbox {
684 0 ▶ my ( $self, $box ) = @_;
Subroutine delete_mailbox: not called CC=1 SCAR=6.9
Pod documented
685 0 _escape( $box );
686
687 return $self->_process_cmd(
688 cmd => [DELETE => $box],
689 0 ▶ final => sub { 1 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
690 ▶ process => sub { },
Subroutine __ANON__: not called CC=1 SCAR=0.0
691 0 );
692 }
693  
694 =pod
695  
696 =item rename_mailbox
697  
698 print "Renamed" if $imap->rename_mailbox( $old => $new );
699  
700 This method renames the mailbox in the first required argument to the
701 mailbox named in the second required argument. Returns true on success,
702 false on failure and the errstr() error handler is set with the error message.
703  
704 =cut
705  
706 sub rename_mailbox {
707 0 ▶ my ( $self, $old_box, $new_box ) = @_;
Subroutine rename_mailbox: not called CC=1 SCAR=6.9
Pod documented
708 0 _escape( $old_box );
709 0 _escape( $new_box );
710
711 return $self->_process_cmd(
712 cmd => [RENAME => qq[$old_box $new_box]],
713 0 ▶ final => sub { 1 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
714 ▶ process => sub { },
Subroutine __ANON__: not called CC=1 SCAR=0.0
715 0 );
716 }
717  
718 =pod
719  
720 =item folder_subscribe
721  
722 print "Subscribed" if $imap->folder_subscribe( "/Mail/lists/perl/advocacy" );
723  
724 This method subscribes to the folder. Returns true on success, false on failure
725 and the errstr() error handler is set with the error message.
726  
727 =cut
728  
729 sub folder_subscribe {
730 0 ▶ my ($self, $box) = @_;
Subroutine folder_subscribe: not called CC=1 SCAR=6.9
Pod documented
731 0 $self->select($box); # XXX does it matter if this fails?
732 0 _escape($box);
733
734 return $self->_process_cmd(
735 cmd => [SUBSCRIBE => $box],
736 0 ▶ final => sub { 1 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
737 ▶ process => sub { },
Subroutine __ANON__: not called CC=1 SCAR=0.0
738 0 );
739 }
740  
741 =pod
742  
743 =item folder_unsubscribe
744  
745 print "Unsubscribed" if $imap->folder_unsubscribe( "/Mail/lists/perl/advocacy" );
746  
747 This method unsubscribes to the folder. Returns true on success, false on failure
748 and the errstr() error handler is set with the error message.
749  
750 =cut
751  
752 sub folder_unsubscribe {
753 0 ▶ my ($self, $box) = @_;
Subroutine folder_unsubscribe: not called CC=1 SCAR=6.9
Pod documented
754 0 $self->select($box);
755 0 _escape($box);
756
757 return $self->_process_cmd(
758 cmd => [UNSUBSCRIBE => $box],
759 0 ▶ final => sub { 1 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
760 ▶ process => sub { },
Subroutine __ANON__: not called CC=1 SCAR=0.0
761 0 );
762 }
763  
764 =pod
765  
766 =item copy
767  
768 print "copied" if $imap->copy( $message_number, $mailbox );
769  
770 This method copies the message number in the currently seleted mailbox to
771 the fold specified in the second argument. Both arguments are required. On
772 success this method returns true. Returns false on failure and the errstr() error handler is set with the error message.
773  
774 =cut
775  
776 sub copy {
777 0 ▶ my ( $self, $number, $box ) = @_;
Subroutine copy: not called CC=1 SCAR=6.9
Pod documented
778 0 _escape( $box );
779
780 return $self->_process_cmd(
781 cmd => [COPY => qq[$number $box]],
782 0 ▶ final => sub { 1 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
783 ▶ process => sub { },
Subroutine __ANON__: not called CC=1 SCAR=0.0
784 0 );
785 }
786  
787 =pod
788  
789 =item errstr
790  
791 print "Login ERROR: " . $imap->errstr . "\n" if !$imap->login($user, $pass);
792  
793 Return the last error string captured for the last operation which failed.
794  
795 =cut
796  
797 sub errstr {
798 0 ▶ return $_[0]->{_errstr};
Subroutine errstr: not called CC=1 SCAR=6.9
Pod documented
799 }
800  
801 0 ▶sub _nextid { ++$_[0]->{count} }
Subroutine _nextid: not called CC=1 SCAR=6.9
802  
803 sub _escape {
804 0 ▶ $_[0] =~ s/\\/\\\\/g;
Subroutine _escape: not called CC=1 SCAR=6.9
805 0 $_[0] =~ s/\"/\\\"/g;
806 0 $_[0] = "\"$_[0]\"";
807 }
808  
809 sub _unescape {
810 0 ▶ $_[0] =~ s/^"//g;
Subroutine _unescape: not called CC=1 SCAR=6.9
811 0 $_[0] =~ s/"$//g;
812 0 $_[0] =~ s/\\\"/\"/g;
813 0 $_[0] =~ s/\\\\/\\/g;
814 }
815  
816 sub _send_cmd {
817 0 ▶ my ( $self, $name, $value ) = @_;
Subroutine _send_cmd: not called CC=3 SCAR=24.8
818 0 my $sock = $self->_sock;
819 0 my $id = $self->_nextid;
820 0 ▶ my $cmd = "$id $name" . ($value ? " $value" : "") . "\r\n";
Branch: 1 branch
BranchTrueFalseTotal
$value ? : 0 0 0
821  
822 0 ▶ $self->_debug(caller, __LINE__, '_send_cmd', $cmd) if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
823  
824 0 { local $\; print $sock $cmd; }
825 0 return ($sock => $id);
826 }
827  
828 sub _cmd_ok {
829 0 ▶ my ($self, $res) = @_;
Subroutine _cmd_ok: not called CC=5 SCAR=34.0
830 0 my $id = $self->_count;
831  
832 0 ▶ $self->_debug(caller, __LINE__, '_send_cmd', $res) if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
833  
834 0 ▶ if($res =~ /^$id\s+OK/i){
Branch: 2 branches
BranchTrueFalseTotal
if ($res =~ /^$id\s+OK/i) { } 0 0 0
elsif ($res =~ /^$id\s+(?:NO|BAD)(?:\s+(.+))?/i) { } 0 0 0
835 0 return 1;
836 } elsif($res =~ /^$id\s+(?:NO|BAD)(?:\s+(.+))?/i){
837 0 ▶ $self->_seterrstr($1 || 'unknown error');
Condition0%0/2
$1 || 'unknown error'
l!l
00
MC/DC0%0/1
$1 || 'unknown error'
$1
Truth table0%0/2
A
A= $1
Aresult
11
00
838 0 return 0;
839 } else {
840 0 $self->_seterrstr("warning unknown return string: $res");
841 0 return;
842 }
843 }
844  
845 sub _read_multiline {
846 0 ▶ my ($self, $sock, $count) = @_;
Subroutine _read_multiline: not called CC=4 SCAR=30.0
847
848 0 my @lines;
849 0 my $read_so_far = 0;
850 0 ▶ while ($read_so_far < $count) {
Branch: 1 branch
BranchTrueFalseTotal
if ($read_so_far < $count) 0 0 0
851 0 push @lines, $sock->getline;
852 0 $read_so_far += length($lines[-1]);
853 }
854 0 ▶ if($self->{debug}){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"debug"}) 0 0 0
855 0 ▶ for(my $i = 0; $i < @lines; $i++){
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @lines) 0 0 0
856 0 $self->_debug(caller, __LINE__, '_read_multiline', "[$i] $lines[$i]");
857 }
858 }
859  
860 0 return @lines;
861 }
862  
863 sub _process_cmd {
864 0 ▶ my ($self, %args) = @_;
Subroutine _process_cmd: not called CC=9 SCAR=45.0
865 0 my ($sock, $id) = $self->_send_cmd(@{$args{cmd}});
866  
867 0 my $res;
868 0 ▶ while ( $res = $sock->getline ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res = $sock->getline) 0 0 0
869 0 ▶ $self->_debug(caller, __LINE__, '_process_cmd', $res) if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
870  
871 0 ▶ if ( $res =~ /^\*.*\{(\d+)\}$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res =~ /^\*.*\{(\d+)\}$/) { } 0 0 0
872 0 $args{process}->($res);
873 0 $args{process}->($_) foreach $self->_read_multiline($sock, $1);
874 } else {
875 0 my $ok = $self->_cmd_ok($res);
876 0 ▶ if ( defined($ok) && $ok == 1 ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $ok and $ok == 1) { } 0 0 0
elsif (defined $ok and not $ok) { } 0 0 0
Condition0%0/3
defined $ok and $ok == 1
!ll&&!rl&&r
000
MC/DC0%0/2
defined $ok and $ok == 1
defined $ok $ok == 1
Truth table0%0/3
A and B
A= defined $okB= $ok == 1
ABresult
0X0
100
111
877 0 return $args{final}->($res);
878 ▶ } elsif ( defined($ok) && ! $ok ) {
Condition0%0/3
defined $ok and not $ok
!ll&&!rl&&r
000
MC/DC0%0/2
defined $ok and not $ok
defined $ok not $ok
Truth table0%0/3
A and not(B)
A= defined $okB= not $ok
ABresult
0X0
100
111
879 0 return;
880 } else {
881 0 $args{process}->($res);
882 }
883 }
884 }
885 }
886  
887 sub _seterrstr {
888 0 ▶ my ($self, $err) = @_;
Subroutine _seterrstr: not called CC=2 SCAR=17.9
889 0 $self->{_errstr} = $err;
890 0 ▶ $self->_debug(caller, __LINE__, '_seterrstr', $err) if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
891 0 return;
892 }
893  
894 sub _debug {
895 0 ▶ my ($self, $package, $filename, $line, $dline, $routine, $str) = @_;
Subroutine _debug: not called CC=2 SCAR=17.9
896  
897 0 $str =~ s/\n/\\n/g;
898 0 $str =~ s/\r/\\r/g;
899 0 $str =~ s/\cM/^M/g;
900  
901 0 my $line = "[$package :: $filename :: $line\@$dline -> $routine] $str\n";
902 0 ▶ if(ref($self->{debug}) eq 'GLOB'){
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->{'debug'} eq 'GLOB') { } 0 0 0
903 0 write($self->{debug}, $line);
904 } else {
905 0 print STDOUT $line;
906 }
907 }
908  
909 =pod
910  
911 =back
912  
913 =cut
914  
915 1 1;
916  
917 __END__
↑ Summary imap.pl »