1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
|
#-*- perl -*-
#
# Copyright (C) 2005,2006,2007,2008 Ken'ichi Fukamachi
# All rights reserved. This program is free software; you can
# redistribute it and/or modify it under the same terms as Perl itself.
#
# $FML: POP3.pm,v 1.8 2007/01/16 12:16:52 fukachan Exp $
#
package FML::MUA::POP3;
use strict;
use vars qw(@ISA @EXPORT @EXPORT_OK $AUTOLOAD);
use Carp;
use FileHandle;
# optional queue class.
my $opt_class = [ "article_post", "command_mail", "error_mail_analyzer" ];
=head1 NAME
FML::MUA::POP3 - retrieve a message by pop3 protocol.
=head1 SYNOPSIS
use FML::MUA::POP3;
my $mua = new FML::MUA::POP3 $curproc;
MUA:
for my $server (@$servers) {
if (defined $mua) {
$mua->login({
server => $server,
username => $username,
password => $password,
});
}
else {
$curproc->logerror("object undefined.");
}
if ($mua->error()) {
$curproc->logerror($mua->error());
next MUA;
}
}
if ($mua->error()) {
$curproc->logerror($mua->error());
$curproc->stop_this_process();
return;
}
$mua->retrieve( { class => $class } );
if ($mua->error()) {
$curproc->logerror($mua->error());
$curproc->stop_this_process();
return;
}
$mua->quit();
if ($mua->error()) {
$curproc->logerror($mua->error());
}
=head1 DESCRIPTION
This class provides POP3 protocol interface.
It behaves a MUA.
=head1 METHODS
=head2 new()
constructor.
=cut
# Descriptions: constructor.
# Arguments: OBJ($self) OBJ($curproc)
# Side Effects: none
# Return Value: OBJ
sub new
{
my ($self, $curproc) = @_;
my ($type) = ref($self) || $self;
my $me = { _curproc => $curproc };
return bless $me, $type;
}
=head2 login($r_args)
login to pop3 server.
=cut
# Descriptions: login to pop3 server.
# Arguments: OBJ($self) HASH_REF($r_args);
# Side Effects: none
# Return Value: OBJ
sub login
{
my ($self, $r_args) = @_;
my $server = $r_args->{ server } || '';
my $username = $r_args->{ username } || '';
my $password = $r_args->{ password } || '';
my $timeout = $r_args->{ timeout } || 60;
if ($server && $username && $password) {
use Net::POP3;
my $pop = Net::POP3->new($server, Timeout => $timeout);
if (defined $pop) {
my $status = undef;
my $capa = $pop->capa();
# 1. try APOP if APOP is supported (we can know it via CAPA).
# 2. try ordinary POP if APOP fails or not supported.
if ($capa->{ APOP }) {
$status = $pop->apop($username, $password);
}
unless ($status) {
$status = $pop->login($username, $password);
}
if (defined $status) {
$self->{ _pop } = $pop;
return $pop;
}
else {
$self->error_set("login failed.");
}
}
else {
$self->error_set("undefined object.");
}
}
else {
$self->error_set("invalid login arguments");
}
}
=head2 retrieve($r_args)
retrieve messages.
=cut
# Descriptions: retrieve messages.
# Arguments: OBJ($self) HASH_REF($r_args);
# Side Effects: none
# Return Value: OBJ
sub retrieve
{
my ($self, $r_args) = @_;
my $curproc = $self->{ _curproc };
my $pop = $self->{ _pop } || undef;
my $class = $r_args->{ class } || undef;
my $tmp_queue = "incoming";
# ASSERT
unless (defined $pop) {
$self->error_set("invalid state");
return undef;
}
unless (defined $class) {
$self->error_set("invalid class");
return undef;
}
my $msgnums = $pop->list; # hashref of msgnum => size
MSG:
foreach my $msgnum (keys %$msgnums) {
my $q = $self->_new_queue_file($r_args);
if (defined $q) {
my $wh = $q->open($tmp_queue, { mode => "w" });
if (defined $wh) {
$wh->autoflush(1);
$wh->clearerr();
$pop->get($msgnum, $wh);
if ($wh->error()) {
$curproc->logerror("failed to retrieve NUM=$msgnum.");
$q->remove();
}
else {
my $id = $q->id();
$q->dup_content($tmp_queue, $class);
$q->remove();
$pop->delete($msgnum);
$curproc->log("fetched: qid=$id");
}
$wh->close();
}
else {
last MSG;
}
}
else {
$curproc->logerror("queue not prepared");
}
}
}
=head2 quit($r_args)
close pop session.
=cut
# Descriptions: close pop session.
# Arguments: OBJ($self) HASH_REF($r_args)
# Side Effects: close pop session.
# Return Value: none
sub quit
{
my ($self, $r_args) = @_;
my $pop = $self->{ _pop };
if (defined $pop) {
$pop->quit();
}
}
=head1 UTILITY
=cut
# Descriptions: create a new queue and return the object.
# Arguments: OBJ($self) HASH_REF($r_args);
# Side Effects: none
# Return Value: OBJ
sub _new_queue_file
{
my ($self, $r_args) = @_;
my $curproc = $self->{ _curproc };
my $config = $curproc->config();
my $queue_dir = $config->{ fetchfml_queue_dir } || '';
my $class = $r_args->{ class } || '';
# ASSERT
unless ($class) {
$self->error_set("invalid class");
return undef;
}
unless ($queue_dir) {
$self->error_set("queue_dir undefined");
return undef;
}
use Mail::Delivery::Queue;
my $queue = new Mail::Delivery::Queue {
directory => $queue_dir,
local_class => $opt_class,
};
return $queue;
}
=head2 pickup_queue($r_args)
=cut
# Descriptions: pick up one queue and return the queue id.
# Arguments: OBJ($self) HASH_REF($r_args)
# Side Effects: none
# Return Value: OBJ
sub pickup_queue
{
my ($self, $r_args) = @_;
my $curproc = $self->{ _curproc };
my $config = $curproc->config();
my $class = $r_args->{ class } || $opt_class->[ 0 ] || '';
my $queue_dir = $config->{ fetchfml_queue_dir } || '';
# ASSERT
unless ($class) {
$self->error_set("invalid class");
return undef;
}
unless ($queue_dir) {
$self->error_set("queue_dir undefined");
return undef;
}
use Mail::Delivery::Queue;
my $queue = new Mail::Delivery::Queue {
directory => $queue_dir,
local_class => $opt_class,
};
my $list = $queue->list($class, "oldest");
my $queue_id = $list->[ 0 ] || '';
if (defined $queue_id && $queue_id) {
my $queue = new Mail::Delivery::Queue {
id => $queue_id,
directory => $queue_dir,
local_class => $opt_class,
};
return $queue;
}
else {
return undef;
}
}
=head1 ERROR HADNLING
=head2 error_set($reason)
save error reason.
=head2 error()
return the last error reason.
=cut
# Descriptions: save error reason.
# Arguments: OBJ($self) STR($reason)
# Side Effects: update $self.
# Return Value: none
sub error_set
{
my ($self, $reason) = @_;
$self->{ _error_reason } = $reason || '';
}
# Descriptions: return the last error reason.
# Arguments: OBJ($self)
# Side Effects: none
# Return Value: STR
sub error
{
my ($self) = @_;
return( $self->{ _error_reason } || '' );
}
#
# debug
#
if ($0 eq __FILE__) {
my ($username, $password, $buf) = ();
my $server = shift @ARGV || croak("usage: $0 server\n");
use Term::ReadLine;
my $term = new Term::ReadLine 'Simple Perl calc';
my $prompt = "Username: ";
LINE:
while ( defined ($buf = $term->readline($prompt)) ) {
system "stty -echo";
eval( $username = $buf );
last LINE if $@;
last LINE if $username;
}
$username =~ s/\s*$//;
$prompt = "Password: ";
LINE:
while ( defined ($buf = $term->readline($prompt)) ) {
system "stty -echo";
eval( $password = $buf );
last LINE if $@;
last LINE if $password;
}
$password =~ s/\s*$//;
print "\n";
if ($username && $password) {
use Net::POP3;
my $pop = Net::POP3->new($server, Timeout => 60);
my $capa = $pop->capa();
my $status = 0;
if ($capa->{ APOP }) {
print "try apop ...\n";
$status = $pop->apop($username, $password) || undef;
}
unless ($status) {
print "try usual login ...\n";
$status = $pop->login($username, $password) || undef;
}
if (defined $status) {
if ($status > 0) {
my $msgnums = $pop->list; # hashref of msgnum => size
foreach my $msgnum (keys %$msgnums) {
print ">>> $msgnum\n";
$pop->get($msgnum, \*STDOUT);
# $pop->delete($msgnum);
}
}
}
else {
croak("login failed.");
}
$pop->quit;
}
else {
croak("username and password unspecified.");
}
}
=head1 CODING STYLE
See C<http://www.fml.org/software/FNF/> on fml coding style guide.
=head1 AUTHOR
Ken'ichi Fukamachi
=head1 COPYRIGHT
Copyright (C) 2005,2006,2007,2008 Ken'ichi Fukamachi
All rights reserved. This program is free software; you can
redistribute it and/or modify it under the same terms as Perl itself.
=head1 HISTORY
FML::MUA::POP3 appeared in fml8 mailing list driver package.
See C<http://www.fml.org/> for more details.
=cut
1;
|