PerMsgHandler.pm

filter: Statementstmt 41.5% 27 / 65 Branchbran 0.0% 0 / 22 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 46.6% 7 / 15 Podpod 100% 1 / 1 totaltotal 33.9% 35 / 103 CC 13 SCAR 37.5
CC
13
Cov
31%
per_message
110.0
__ANON__
6.0
__ANON__
6.0
CRAP 42.6
↑ Summary PerMsgHandler/Control.pm »
1 package Net::POP3::PerMsgHandler;
2  
3 =head1 NAME
4  
5 Net::POP3::PerMsgHandler - subroutine for per message from POP3 server
6  
7 =cut
8  
9 2 our $VERSION = '0.03';
10  
11 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 # fail on 5.8.0
15 #use Exporter 'import';
16 #our @EXPORT = qw/per_message/;
17 2 require Exporter;
18 2 our @ISA = qw(Exporter);
19 2 our @EXPORT = qw(per_message); # symbols to export on request
20  
21 2 ▶use Params::Validate;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 2 ▶use Scalar::Defer;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 2 ▶use Net::POP3;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 2 ▶use Net::POP3::PerMsgHandler::Control;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 2 ▶use Net::POP3::PerMsgHandler::Message;
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 =head1 SYNOPSIS
28  
29 Quick summary of what the module does.
30  
31 Perhaps a little code snippet.
32  
33 use Net::POP3::PerMsgHandler;
34 use YAML::Syck;
35 use Perl6::Say;
36  
37 my $cfg = LoadFile('config.yml');
38  
39 eval {
40 my $count = per_message(
41 username => $cfg->{username},
42 password => $cfg->{password},
43 host => $cfg->{host},
44 handler => sub {
45 my ($msg, $ctl) = @_;
46  
47 my $email = $msg->email_mime; # Email::MIME object.
48 say "Subject: ".$email->header('Subject');
49  
50 $ctl->delete(0) # default
51 $ctl->quit(0) # default
52 },
53 );
54 };
55  
56 say $@ if $@; # connection failed etc...
57  
58 # Subject: Re: Spam collection
59 # Subject: Congratulations, You're a finalist
60 # Subject: Software Secret: WARNING Reading this could change your life
61 # ...
62  
63 =head1 EXPORT FUNCTIONS
64  
65 =head2 per_message
66  
67 =head1 OPTIONS
68  
69 =over 4
70  
71 =item username
72  
73 required.
74  
75 =item password
76  
77 required.
78  
79 =item host
80  
81 required.
82  
83 =item port
84  
85 optional.
86  
87 =item timeout
88  
89 optional.
90  
91 =item handler
92  
93 code reference required.
94  
95 The callback is given two arguments.
96 The first is a Net::POP3::PerMsgHandler::Message object.
97 The second is a Net::POP3::PerMsgHandler::Control object.
98  
99 Executes the callback for each message.
100  
101 =item debug
102  
103 optional.
104  
105 =back
106  
107 =cut
108  
109 0 ▶sub per_message {
Subroutine per_message: not called CC=10 SCAR=47.0
Pod documented
110 0 my %p = validate(@_,
111 {
112 username => 1,
113 password => 1,
114 host => 1,
115 port => 0,
116 handler => 1,
117 timeout => 0,
118 debug => 0,
119 }
120 );
121  
122 0 my @new_args = ($p{host});
123 0 ▶ push @new_args, (Timeout => $p{timeout}) if exists $p{timeout};
Branch: 1 branch
BranchTrueFalseTotal
if exists $p{"timeout"} 0 0 0
124 0 ▶ push @new_args, (ResvPort => $p{port}) if exists $p{port};
Branch: 1 branch
BranchTrueFalseTotal
if exists $p{"port"} 0 0 0
125 0 ▶ push @new_args, (Debug => $p{debug}) if exists $p{debug};
Branch: 1 branch
BranchTrueFalseTotal
if exists $p{"debug"} 0 0 0
126  
127 0 my $pop = Net::POP3->new(@new_args);
128 0 ▶ die "connection failed." unless defined $pop;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pop 0 0 0
129 0 my $count = $pop->login($p{username}, $p{password});
130  
131 0 ▶ die "authentication failed." unless defined $count;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $count 0 0 0
132 0 ▶ return $count if $count == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $count == 0 0 0 0
133  
134 0 my $msgnums = $pop->list;
135 0 foreach my $msgnum (keys %$msgnums) {
136 0 my $ctl = Net::POP3::PerMsgHandler::Control->new({delete=>0, quit=>0});
137  
138 0 my $msg = Net::POP3::PerMsgHandler::Message->new({});
139 0 ▶ $msg->{size} = lazy { $msgnums->{$msgnum} };
Subroutine __ANON__: not called CC=1 SCAR=6.9
140 0 ▶ $msg->{array_ref} = lazy { $pop->get($msgnum) };
Subroutine __ANON__: not called CC=1 SCAR=6.9
141 0 ▶ $msg->{rfc2822} = lazy { join("", @{ $msg->array_ref }) };
Subroutine __ANON__: not called CC=1 SCAR=6.9
142 0 ▶ $msg->{email_mime} = lazy {
Subroutine __ANON__: not called CC=2 SCAR=17.9
143 0 ▶ require Email::MIME or die;
Branch: 1 branch
BranchTrueFalseTotal
require Email::MIME or die 0 0 0
144 0 Email::MIME->new($msg->rfc2822);
145 0 };
146 0 ▶ $msg->{email_mime_stripped} = lazy {
Subroutine __ANON__: not called CC=1 SCAR=6.9
147 0 require Email::MIME::Attachment::Stripper;
148 0 Email::MIME::Attachment::Stripper->new($msg->rfc2822)->message;
149 0 };
150 0 ▶ $msg->{mail_message} = lazy {
Subroutine __ANON__: not called CC=2 SCAR=17.9
151 0 ▶ require Mail::Message or die;
Branch: 1 branch
BranchTrueFalseTotal
require Mail::Message or die 0 0 0
152 0 Mail::Message->read($msg->array_ref);
153 0 };
154 0 ▶ $msg->{mail_message_stripped} = lazy {
Subroutine __ANON__: not called CC=2 SCAR=17.9
155 0 ▶ require Mail::Message::Attachment::Stripper or die;
Branch: 1 branch
BranchTrueFalseTotal
require Mail::Message::Attachment::Stripper or die 0 0 0
156 0 Mail::Message::Attachment::Stripper
157 ->new($msg->mail_message)->message;
158 0 };
159  
160 0 $p{handler}->($msg, $ctl);
161  
162 0 ▶ $pop->delete($msgnum) if $ctl->delete;
Branch: 1 branch
BranchTrueFalseTotal
if $ctl->delete 0 0 0
163 0 ▶ last if $ctl->quit;
Branch: 1 branch
BranchTrueFalseTotal
if $ctl->quit 0 0 0
164 }
165  
166 0 $pop->quit;
167  
168 0 return $count;
169 }
170  
171 2 1;
172  
173 =head1 EXAMPLES
174  
175 =head2 ex1 - delete message subject starting with SPAM
176  
177 my $count = per_message(
178 username => $cfg->{username},
179 password => $cfg->{password},
180 host => $cfg->{host},
181 handler => sub {
182 my ($msg, $ctl) = @_;
183  
184 my $email = $msg->email_mime;
185 my $is_spam = $email->header('Subject') =~ m/^SPAM/;
186  
187 $ctl->delete(1) if $is_spam;
188 },
189 );
190  
191 =head2 ex2 - find specified message and save attached files and delete.
192  
193 my $count = per_message(
194 username => $cfg->{username},
195 password => $cfg->{password},
196 host => $cfg->{host},
197 handler => sub {
198 my ($msg, $ctl) = @_;
199  
200 my $email = $msg->email_mime;
201 return unless $email->body =~ m/\AUUID: 12345/sm;
202  
203 for my $part ($email->parts) {
204 next unless defined $part->filename;
205 $part->body > io( $part->filename );
206 }
207  
208 $ctl->delete(1);
209 $ctl->quit(1);
210 },
211 );
212  
213 =head1 SEE ALSO
214  
215 L<Net::POP3::PerMsgHandler>, L<Net::POP3>, L<Email::MIME>, L<Email::MIME::Attachment::Stripper>, L<Mail::Message>, L<Mail::Message::Attachment::Stripper>
216  
217 =head1 AUTHOR
218  
219 bokutin, C<< <bokutin at cpan.org> >>
220  
221 =head1 COPYRIGHT & LICENSE
222  
223 Copyright 2007 bokutin, all rights reserved.
224  
225 This program is free software; you can redistribute it and/or modify it
226 under the same terms as Perl itself.
227  
228 =cut
229  
230 2 1;
↑ Summary PerMsgHandler/Control.pm »