line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
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
|
|
|
|
|
|
|
our $VERSION = '0.03'; |
10
|
|
|
|
|
|
|
|
11
|
1
|
|
|
1
|
|
23827
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
32
|
|
12
|
1
|
|
|
1
|
|
6
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
70
|
|
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
# fail on 5.8.0 |
15
|
|
|
|
|
|
|
#use Exporter 'import'; |
16
|
|
|
|
|
|
|
#our @EXPORT = qw/per_message/; |
17
|
|
|
|
|
|
|
require Exporter; |
18
|
|
|
|
|
|
|
our @ISA = qw(Exporter); |
19
|
|
|
|
|
|
|
our @EXPORT = qw(per_message); # symbols to export on request |
20
|
|
|
|
|
|
|
|
21
|
1
|
|
|
1
|
|
7004
|
use Params::Validate; |
|
1
|
|
|
|
|
53076
|
|
|
1
|
|
|
|
|
128
|
|
22
|
1
|
|
|
1
|
|
7003
|
use Scalar::Defer; |
|
1
|
|
|
|
|
39700
|
|
|
1
|
|
|
|
|
9
|
|
23
|
1
|
|
|
1
|
|
110133
|
use Net::POP3; |
|
1
|
|
|
|
|
256978
|
|
|
1
|
|
|
|
|
81
|
|
24
|
1
|
|
|
1
|
|
105063
|
use Net::POP3::PerMsgHandler::Control; |
|
1
|
|
|
|
|
4
|
|
|
1
|
|
|
|
|
10
|
|
25
|
1
|
|
|
1
|
|
48103
|
use Net::POP3::PerMsgHandler::Message; |
|
1
|
|
|
|
|
4
|
|
|
1
|
|
|
|
|
10
|
|
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
|
|
|
|
|
|
|
sub per_message { |
110
|
0
|
|
|
0
|
1
|
|
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
|
0
|
|
|
|
|
push @new_args, (Timeout => $p{timeout}) if exists $p{timeout}; |
124
|
0
|
0
|
|
|
|
|
push @new_args, (ResvPort => $p{port}) if exists $p{port}; |
125
|
0
|
0
|
|
|
|
|
push @new_args, (Debug => $p{debug}) if exists $p{debug}; |
126
|
|
|
|
|
|
|
|
127
|
0
|
|
|
|
|
|
my $pop = Net::POP3->new(@new_args); |
128
|
0
|
0
|
|
|
|
|
die "connection failed." unless defined $pop; |
129
|
0
|
|
|
|
|
|
my $count = $pop->login($p{username}, $p{password}); |
130
|
|
|
|
|
|
|
|
131
|
0
|
0
|
|
|
|
|
die "authentication failed." unless defined $count; |
132
|
0
|
0
|
|
|
|
|
return $count if $count == 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
|
|
|
0
|
|
|
$msg->{size} = lazy { $msgnums->{$msgnum} }; |
|
0
|
|
|
|
|
|
|
140
|
0
|
|
|
0
|
|
|
$msg->{array_ref} = lazy { $pop->get($msgnum) }; |
|
0
|
|
|
|
|
|
|
141
|
0
|
|
|
0
|
|
|
$msg->{rfc2822} = lazy { join("", @{ $msg->array_ref }) }; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
$msg->{email_mime} = lazy { |
143
|
0
|
0
|
|
0
|
|
|
require Email::MIME or die; |
144
|
0
|
|
|
|
|
|
Email::MIME->new($msg->rfc2822); |
145
|
0
|
|
|
|
|
|
}; |
146
|
|
|
|
|
|
|
$msg->{email_mime_stripped} = lazy { |
147
|
0
|
|
|
0
|
|
|
require Email::MIME::Attachment::Stripper; |
148
|
0
|
|
|
|
|
|
Email::MIME::Attachment::Stripper->new($msg->rfc2822)->message; |
149
|
0
|
|
|
|
|
|
}; |
150
|
|
|
|
|
|
|
$msg->{mail_message} = lazy { |
151
|
0
|
0
|
|
0
|
|
|
require Mail::Message or die; |
152
|
0
|
|
|
|
|
|
Mail::Message->read($msg->array_ref); |
153
|
0
|
|
|
|
|
|
}; |
154
|
|
|
|
|
|
|
$msg->{mail_message_stripped} = lazy { |
155
|
0
|
0
|
|
0
|
|
|
require Mail::Message::Attachment::Stripper or die; |
156
|
0
|
|
|
|
|
|
Mail::Message::Attachment::Stripper |
157
|
|
|
|
|
|
|
->new($msg->mail_message)->message; |
158
|
0
|
|
|
|
|
|
}; |
159
|
|
|
|
|
|
|
|
160
|
0
|
|
|
|
|
|
$p{handler}->($msg, $ctl); |
161
|
|
|
|
|
|
|
|
162
|
0
|
0
|
|
|
|
|
$pop->delete($msgnum) if $ctl->delete; |
163
|
0
|
0
|
|
|
|
|
last if $ctl->quit; |
164
|
|
|
|
|
|
|
} |
165
|
|
|
|
|
|
|
|
166
|
0
|
|
|
|
|
|
$pop->quit; |
167
|
|
|
|
|
|
|
|
168
|
0
|
|
|
|
|
|
return $count; |
169
|
|
|
|
|
|
|
} |
170
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
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, L, L, L, L, L |
216
|
|
|
|
|
|
|
|
217
|
|
|
|
|
|
|
=head1 AUTHOR |
218
|
|
|
|
|
|
|
|
219
|
|
|
|
|
|
|
bokutin, C<< >> |
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
|
|
|
|
|
|
|
1; |