blib/lib/Mail/IMAPQueue.pm

filter: Statementstmt 87.7% 93 / 106 Branchbran 57.4% 31 / 54 Conditioncond 12.5% 1 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 100% 14 / 14 Podpod 100% 9 / 9 totaltotal 75.5% 148 / 196 CC 31 SCAR 23.3
CC
31
Cov
74%
attempt_idle
18.7
reload_messages
18.6
ensure_messages
5.0
CRAP 10.3
↑ Summary
1 =head1 LICENSE AND COPYRIGHT
2  
3 THIS PROGRAM IS SUBJECT TO THE TERMS OF THE ARTISTIC LICENSE, VERSION 2.0.
4  
5 THE FOLLOWING DISCLAIMER APPLIES TO ALL SOFTWARE CODE AND OTHER MATERIALS
6 CONTRIBUTED IN CONNECTION WITH THIS PROGRAM:
7  
8 THIS SOFTWARE IS LICENSED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
9 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
10 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE AND
11 ANY WARRANTY OF NON-INFRINGEMENT, ARE DISCLAIMED. IN NO EVENT SHALL THE
12 COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
13 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
14 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
15 BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
16 IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
17 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
18 POSSIBILITY OF SUCH DAMAGE. THIS SOFTWARE MAY BE REDISTRIBUTED TO OTHERS ONLY
19 BY EFFECTIVELY USING THIS OR ANOTHER EQUIVALENT DISCLAIMER IN ADDITION TO ANY
20 OTHER REQUIRED LICENSE TERMS.
21  
22 ONLY THE SOFTWARE CODE AND OTHER MATERIALS CONTRIBUTED IN CONNECTION WITH THIS
23 SOFTWARE, IF ANY, THAT ARE ATTACHED TO (OR OTHERWISE ACCOMPANY) THIS SUBMISSION
24 (AND ORDINARY COURSE CONTRIBUTIONS OF FUTURES PATCHES THERETO) ARE TO BE
25 CONSIDERED A CONTRIBUTION. NO OTHER SOFTWARE CODE OR MATERIALS ARE A
26 CONTRIBUTION.
27  
28 Copyright (c) 2012 Contributor
29 All rights reserved.
30 =cut
31  
32 3 ▶use 5.008_001;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
35  
36 package Mail::IMAPQueue;
37  
38 =head1 NAME
39  
40 Mail::IMAPQueue - IMAP client extension to watch and process a mailbox as a queue
41  
42 =head1 VERSION
43  
44 Version 0.04
45  
46 =cut
47  
48 3 our $VERSION = '0.04';
49  
50 3 ▶use List::Util qw(max);
Subroutine BEGIN: called CC=1 SCAR=0.0
51 3 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
52  
53 =head1 SYNOPSIS
54  
55 =head2 Basic usage
56  
57 use Mail::IMAPClient;
58 use Mail::IMAPQueue;
59
60 my $imap = Mail::IMAPClient->new(
61 ... # See Mail::IMAPClient documentation
62 ) or die $@;
63
64 $imap->select('INBOX') or die $@;
65
66 my $queue = Mail::IMAPQueue->new(
67 client => $imap
68 ) or die $@;
69
70 while (defined(my $msg = $queue->dequeue_message())) {
71 # Do something with $msg (sequence number or UID)
72 }
73
74 $imap->close();
75  
76 =head1 DESCRIPTION
77  
78 This module provides a way to access a mailbox with IMAP protocol,
79 regarding the mailbox as a FIFO queue so that the client code can
80 continuously process incoming email messages.
81  
82 The module utilizes L<Mail::IMAPClient> as an IMAP client interface.
83  
84 The instance of C<Mail::IMAPQueue> maintains a buffer internally,
85 and loads the message sequence numbers (or UIDs) into the buffer as necessary.
86 When there are no messages in the mailbox while the buffer is empty,
87 it will wait until new messages are received in the mailbox.
88  
89 For the purpose of this module, one single mailbox (or a I<folder>) must be
90 selected at all times (C<Mail::IMAPClient::select()>).
91  
92 It is assumed that the UID assigned to each message is I<strictly ascending>
93 as stated in RFC 3501 2.3.1.1. and that the order for any messages to start
94 appearing in the result of the C<SEARCH> command is always consistent with
95 the order of UIDs.
96  
97 It is also assumed that the IMAP server provides the C<IDLE> extension (RFC 2177),
98 for real-time updates from the server.
99  
100 =head1 EXAMPLES
101  
102 =head2 Dumping messages into files
103  
104 while (defined(my $msg = $queue->dequeue_message())) {
105 $imap->message_to_file("/tmp/mails/$msg", $msg) or die $@;
106 $imap->delete_message($msg) or die $@;
107 $imap->expunge() or die $@ if $queue->is_empty;
108 }
109  
110 =head2 Managing messages with each buffer
111  
112 while (my $msg_list = $queue->dequeue_messages()) {
113 for my $msg (@$msg_list) {
114 # Do something with $msg
115 $imap->delete_message($msg) or die $@;
116 }
117 $imap->expunge() or die $@;
118 }
119  
120 =head2 Controlling timing of fetching and waiting
121  
122 while ($queue->reload_messages()) { # non-blocking
123 my $msg_list = $queue->peek_messages or die $@; # non-blocking
124 if (@$msg_list) {
125 for my $msg (@$msg_list) {
126 # Do something with $msg
127 }
128 } else {
129 $queue->attempt_idle or die $@;
130 # blocking wait for new messages, up to 30 sec.
131 }
132 }
133  
134 =head1 METHODS
135  
136 =head2 $class->new(client => $imap, ...)
137  
138 Instanciate a queue object, with the required field C<client> set to a
139 L<Mail::IMAPClient> object.
140  
141 my $queue = Mail::IMAPQueue->new(
142 client => $imap,
143 uidnext => $known_next_uid, # default = undef
144 skip_initial => $true_or_false, # default = 0
145 idle_timeout => $seconds, # default = 30
146 ) or die $@;
147  
148 No IMAP requests are invoked with the C<client> object during the initialization.
149 The buffer maintained by this object is initially empty.
150  
151 =over 4
152  
153 =item * client => $imap
154  
155 The underlying client object. It is assumed to be an instance of L<Mail::IMAPClient>,
156 although the type of the object is not enforced.
157  
158 =item * uidnext => $known_next_uid
159  
160 If the next message UID (the smallest UID to be used) is known (e.g. from a previous execution),
161 specify the value here.
162  
163 =item * skip_initial => $true_or_false
164  
165 Specify a true value to skip all the messages initially in the mailbox.
166 If C<uidnext> option is set, this option will be ignored effectively.
167  
168 =item * idle_timeout => $seconds
169  
170 Specify the timeout in seconds for the IDLE command (RFC 2177), which allows the IMAP client
171 to receive updates from the server in real-time.
172 It does I<not> mean the method call will give up when there are no updates after the timeout,
173 but it means how frequently it will reset the IDLE command (with any blocking methods except
174 for C<attempt_idle()> method, which is for one timeout round).
175  
176 =back
177  
178 =cut
179  
180 5 ▶sub new {
Subroutine new: called CC=2 SCAR=7.6
Pod documented
181 5 my $class = shift;
182
183 5 my $self = bless {
184 client => undef,
185 buffer => [],
186 index => 0,
187 uidnext => undef,
188 skip_initial => 0,
189 idle_timeout => 30,
190 @_
191 }, $class;
192
193 5 my $imap = $self->{client};
194
195 5 ▶ unless (blessed($imap)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($imap)) 0 5 5
196 0 $@ = "Parameter 'client' must be given (Mail::IMAPClient)";
197 0 return undef;
198 }
199
200 5 return $self;
201 }
202  
203 =head2 $queue->is_empty()
204  
205 Return 1 if the current buffer is empty, and 0 otherwise.
206  
207 =cut
208  
209 126 ▶sub is_empty {
Subroutine is_empty: called CC=1 SCAR=0.0
Pod documented
210 126 my ($self) = @_;
211 126 return $self->{index} >= @{$self->{buffer}};
212 }
213  
214 =head2 $queue->dequeue_message()
215  
216 Dequeue the next message from the mailbox.
217 If the current buffer is non-empty, the next message will be removed from the buffer and returned.
218 Otherwise, the call will be blocked until there is at least one message found in the mailbox,
219 and then the first message will be removed from the loaded buffer and returned.
220  
221 The method returns the sequence number of the message
222 (or UID if the C<Uid> option is turned on for the underlying client).
223 C<undef> is returned if the attempt to load the messages was failed.
224  
225 =cut
226  
227 23 ▶sub dequeue_message {
Subroutine dequeue_message: called CC=2 SCAR=7.0
Pod documented
228 23 my ($self) = @_;
229 23 $self->ensure_messages;
230 23 ▶ return undef if $self->is_empty;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_empty 0 23 23
231
232 23 my $index = $self->{index};
233 23 my $buffer = $self->{buffer};
234
235 23 my $message = $buffer->[$index];
236 23 $self->{index}++;
237
238 23 return $message;
239 }
240  
241 =head2 $queue->dequeue_messages()
242  
243 Dequeue the next list of messages.
244 If the current buffer is non-empty, all the messages will be removed from the buffer and returned.
245 Otherwise, the call will be blocked until there is at least one message found in the mailbox,
246 and then all the loaded messages will be removed and returned.
247  
248 In the list context, the method returns an array of the message sequence numbers
249 (or UIDs if the C<Uid> option is turned on for the underlying client).
250 In the scalar context, a reference to the array is returned.
251 C<undef> is returned if the attempt to load the messages was failed.
252  
253 =cut
254  
255 2 ▶sub dequeue_messages {
Subroutine dequeue_messages: called CC=3 SCAR=11.1
Pod documented
256 2 my ($self) = @_;
257 2 $self->ensure_messages;
258 2 ▶ return undef if $self->is_empty;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_empty 0 2 2
259
260 2 my $index = $self->{index};
261 2 my $buffer = $self->{buffer};
262
263 2 my $messages = [@$buffer[$index..$#$buffer]];
264 2 $self->{index} = @$buffer;
265
266 2 ▶ return wantarray ? @$messages : $messages;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 2 2
267 }
268  
269 =head2 $queue->peek_message()
270  
271 Retrieve the first message in the current buffer without removing the message.
272  
273 The method returns the sequence number of the message
274 (or UID if the C<Uid> option is turned on for the underlying client).
275 C<undef> is returned if the current buffer is empty.
276  
277 =cut
278  
279 17 ▶sub peek_message {
Subroutine peek_message: called CC=2 SCAR=6.9
Pod documented
280 17 my ($self) = @_;
281 17 ▶ return undef if $self->is_empty;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_empty 7 10 17
282
283 10 my $index = $self->{index};
284 10 my $buffer = $self->{buffer};
285
286 10 return $buffer->[$index];
287 }
288  
289 =head2 $queue->peek_messages()
290  
291 Retrieve all the messages in the current buffer without removing the messages.
292  
293 In the list context, the method returns an array of the message sequence numbers
294 (or UIDs if the C<Uid> option is turned on for the underlying client).
295 In the scalar context, a reference to the array is returned.
296  
297 =cut
298  
299 18 ▶sub peek_messages {
Subroutine peek_messages: called CC=3 SCAR=11.0
Pod documented
300 18 my ($self) = @_;
301 18 ▶ return [] if $self->is_empty;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_empty 9 9 18
302
303 9 my $index = $self->{index};
304 9 my $buffer = $self->{buffer};
305
306 9 my $messages = [@$buffer[$index..$#$buffer]];
307
308 9 ▶ return wantarray ? @$messages : $messages;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 9 9
309 }
310  
311 =head2 $queue->ensure_messages()
312  
313 The call is blocked until there is at least one message loaded into the buffer.
314  
315 The method returns the object itself if successful, and C<undef> otherwise.
316  
317 =cut
318  
319 30 ▶sub ensure_messages {
Subroutine ensure_messages: called CC=5 SCAR=16.2
Pod documented
320 30 my ($self) = @_;
321
322 30 ▶ if ($self->is_empty) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_empty) 11 19 30
323 11 while (1) {
324 15 ▶ $self->reload_messages or return undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->reload_messages or return undef 15 0 15
325
326 15 ▶ if ($self->is_empty) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_empty) { } 4 11 15
327 4 ▶ $self->attempt_idle() or return undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->attempt_idle or return undef 4 0 4
328 } else {
329 # success
330 11 return $self;
331 }
332 }
333 }
334
335 19 return $self;
336 }
337  
338 =head2 $queue->attempt_idle()
339  
340 Attempt the IDLE command so that the call is blocked until there are any updates in the mailbox
341 or the timeout (default = 30 sec.) has elapsed.
342  
343 The method returns the object itself if successful, and C<undef> otherwise.
344 If the timeout has elapsed gracefully, it is considered to be a success.
345  
346 =cut
347  
348 4 ▶sub attempt_idle {
Subroutine attempt_idle: called CC=8 SCAR=29.3
Pod documented
349 4 my ($self) = @_;
350 4 my $imap = $self->{client};
351 4 ▶ my $idle_timeout = $self->{idle_timeout} || 30;
Condition50%1/2
$self->{'idle_timeout'} || 30
l!l
40
MC/DC0%0/1
$self->{'idle_timeout'} || 30
$self->{'idle_timeout'}
Truth table50%1/2
A
A= $self->{'idle_timeout'}
Aresult
11
00
352
353 4 eval {
354 4 ▶ my $idle_tag = $imap->idle or die $imap;
Branch: 1 branch
BranchTrueFalseTotal
my $idle_tag = $imap->idle or die $imap 4 0 4
355
356 4 my $idle_data = $imap->idle_data($idle_timeout);
357 # do not die even if this fails; always send DONE anyway
358
359 4 ▶ $imap->done($idle_tag) or die $imap;
Branch: 1 branch
BranchTrueFalseTotal
$imap->done($idle_tag) or die $imap 4 0 4
360 };
361
362 4 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 4 4
363 0 ▶ if (ref $@ && $@ == $imap) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $@ and $@ == $imap) { } 0 0 0
Condition0%0/3
ref $@ and $@ == $imap
!ll&&!rl&&r
000
MC/DC0%0/2
ref $@ and $@ == $imap
ref $@ $@ == $imap
Truth table0%0/3
A and B
A= ref $@B= $@ == $imap
ABresult
0X0
100
111
364 0 ▶ $imap->reconnect or do {
Branch: 1 branch
BranchTrueFalseTotal
$imap->reconnect or do { $@ = "Disconnected while attempting IDLE"; return undef } 0 0 0
365 0 $@ = "Disconnected while attempting IDLE";
366 0 return undef;
367 };
368 } else {
369 0 return undef;
370 }
371 }
372
373 4 return $self;
374 }
375  
376 =head2 $queue->reload_messages()
377  
378 Discard the current buffer, and attempt to load any messages from the mailbox to the buffer.
379 The call is not blocked (except for the usual socket wait for any server response).
380  
381 The method returns the object itself if successful, and C<undef> otherwise.
382  
383 Note:
384 Even if no new messages are loaded, it is a success as long as the server has responded properly.
385 In order to test the last result of loading, the C<is_empty()> method can be used.
386  
387 =cut
388  
389 18 ▶sub reload_messages {
Subroutine reload_messages: called CC=13 SCAR=29.2
Pod documented
390 18 my ($self) = @_;
391
392 18 my $uidnext = $self->{uidnext};
393 18 my $buffer = [];
394
395 TRY: {
396 18 my $imap = $self->{client};
397
398 18 ▶ unless ($imap->IsSelected) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($imap->IsSelected) 0 18 18
399 0 $@ = "Folder must be selected";
400 0 return undef;
401 }
402
403 18 eval {
404 18 my $loaded = 0;
405
406 18 ▶ unless (defined $uidnext) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $uidnext) 5 13 18
407 # Initially $uidnext is undef (except it was set explicitly)
408 5 ▶ if ($self->{skip_initial}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'skip_initial'}) { } 1 4 5
409 1 ▶ $uidnext = $imap->uidnext($imap->Folder) or die $imap;
Branch: 1 branch
BranchTrueFalseTotal
$uidnext = $imap->uidnext($imap->Folder) or die $imap 1 0 1
410 1 $self->{uidnext} = $uidnext;
411 } else {
412 4 ▶ $buffer = $imap->messages or die $imap;
Branch: 1 branch
BranchTrueFalseTotal
$buffer = $imap->messages or die $imap 4 0 4
413 4 $loaded = 1;
414 }
415 }
416
417 18 ▶ unless ($loaded) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($loaded) 14 4 18
418 14 ▶ $buffer = $imap->search("UID $uidnext:*") or die $imap;
Branch: 1 branch
BranchTrueFalseTotal
$buffer = $imap->search("UID ${uidnext}:*") or die $imap 14 0 14
419 14 $buffer = [grep {$uidnext <= $_} @$buffer];
420 }
421 };
422
423 18 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 18 18
424 0 ▶ if (ref $@ && $@ == $imap) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $@ and $@ == $imap) { } 0 0 0
Condition0%0/3
ref $@ and $@ == $imap
!ll&&!rl&&r
000
MC/DC0%0/2
ref $@ and $@ == $imap
ref $@ $@ == $imap
Truth table0%0/3
A and B
A= ref $@B= $@ == $imap
ABresult
0X0
100
111
425 0 ▶ $imap->reconnect or return undef;
Branch: 1 branch
BranchTrueFalseTotal
$imap->reconnect or return undef 0 0 0
426 0 redo TRY;
427 } else {
428 0 return undef;
429 }
430 }
431 }
432
433 18 ▶ if (@$buffer > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$buffer > 0) 12 6 18
434 12 $uidnext = max(@$buffer) + 1;
435 12 $self->{uidnext} = $uidnext;
436 }
437
438 18 $self->{buffer} = $buffer;
439 18 $self->{index} = 0;
440
441 18 return $self;
442 }
443  
444 =head1 AUTHOR
445  
446 Mahiro Ando, C<< <mahiro at cpan.org> >>
447  
448 =head1 BUGS
449  
450 Please report any bugs or feature requests to C<bug-mail-imapqueue at rt.cpan.org>, or through
451 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Mail-IMAPQueue>. I will be notified, and then you'll
452 automatically be notified of progress on your bug as I make changes.
453  
454 =head1 SUPPORT
455  
456 You can find documentation for this module with the perldoc command.
457  
458 perldoc Mail::IMAPQueue
459  
460 You can also look for information at:
461  
462 =over 4
463  
464 =item * GitHub repository (report bugs here)
465  
466 L<https://github.com/mahiro/perl-Mail-IMAPQueue>
467  
468 =item * RT: CPAN's request tracker (report bugs here, alternatively)
469  
470 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Mail-IMAPQueue>
471  
472 =item * AnnoCPAN: Annotated CPAN documentation
473  
474 L<http://annocpan.org/dist/Mail-IMAPQueue>
475  
476 =item * CPAN Ratings
477  
478 L<http://cpanratings.perl.org/d/Mail-IMAPQueue>
479  
480 =item * Search CPAN
481  
482 L<http://search.cpan.org/dist/Mail-IMAPQueue/>
483  
484 =back
485  
486 =head1 ACKNOWLEDGEMENTS
487  
488 The initial package was created by L<Module::Starter> v1.58.
489  
490 This module utilizes L<Mail::IMAPClient> as a client library interface for IMAP.
491  
492 =cut
493  
494 3 1; # End of Mail::IMAPQueue
↑ Summary