root/trunk/plagger/lib/Plagger/Plugin/CustomFeed/POP3.pm

Revision 1172 (checked in by tokuhirom, 14 years ago)

CustomFeed?::POP3: sort return the entries in message number order rather than unordered

Line 
1 package Plagger::Plugin::CustomFeed::POP3;
2 use strict;
3 use base qw( Plagger::Plugin );
4
5 use Net::POP3;
6 use Encode;
7 use HTML::Entities qw/encode_entities/;
8 use Email::MIME;
9
10 sub register {
11     my($self, $context) = @_;
12     $context->register_hook(
13         $self,
14         'subscription.load' => \&load,
15     );
16 }
17
18 sub load {
19     my($self, $context) = @_;
20
21     my $feed = Plagger::Feed->new;
22        $feed->aggregator(sub { $self->aggregate(@_) });
23     $context->subscription->add($feed);
24 }
25
26 sub aggregate {
27     my($self, $context, $args) = @_;
28
29     my $host = $self->conf->{host};
30     my $pop = Net::POP3->new($host);
31
32     unless ($pop->login($self->conf->{username}, $self->conf->{password})) {
33         $context->log(error => "Login to $host failed.");
34         return;
35     }
36
37     $context->log(info => "Login to pop3 server($host) succeeded.");
38
39     my $msgnums = $pop->list;
40     for my $msgnum (sort { $b <=> $a } keys %$msgnums) {
41         $context->log(debug => "get the message : $msgnum");
42
43         my $msg = $pop->get($msgnum);
44         my $feed = $self->mail2feed(join '', @$msg);
45         $context->update->add($feed);
46
47         if ($self->conf->{delete}) {
48             $context->log(info => "delete message : $msgnum");
49             $pop->delete($msgnum)
50         }
51     }
52
53     $pop->quit;
54 }
55
56 sub mail2feed {
57     my ($self, $message) = @_;
58
59     my $entry  = Plagger::Entry->new;
60     my $email  = Email::MIME->new($message);
61     my $format = DateTime::Format::Mail->new->loose;
62
63     my $feed = Plagger::Feed->new;
64     $feed->type('pop3');
65     $feed->title($email->header('Subject'));
66
67     $entry->title($email->header('Subject'));
68     $entry->author($email->header('From'));
69     $entry->date(Plagger::Date->parse($format, $email->header('Date'))) if $email->header('Date');
70     $entry->body($self->get_body($email));
71
72     $feed->add_entry($entry);
73
74     return $feed;
75 }
76
77 sub get_body {
78     my ($self, $email) = @_;
79
80     my $body_part;
81     for my $part ($email->parts) {
82         if ($part->content_type =~ m[text/html] or ($part->content_type =~ m[text/plain] and !$body_part)) {
83             $body_part = $part;
84         }
85     }
86     $body_part ||= $email;
87
88     $body_part->content_type =~  /charset=(['"]?)([\w-]+)\1/;
89     my $encoding = $2 || 'utf-8';
90
91     if ($body_part->content_type =~ m[text/html]) {
92         return decode($encoding, $body_part->body);
93     } else {
94         return '<pre>'.encode_entities(decode($encoding, $body_part->body)).'</pre>';
95     }
96 }
97
98 1;
99 __END__
100
101 =head1 NAME
102
103 Plagger::Plugin::CustomFeed::POP3 - Custom feed for POP3
104
105 =head1 SYNOPSIS
106
107   - module: CustomFeed::POP3
108     config:
109         host: example.com
110         username: tokuhirom
111         password: PASSW0RD
112         #delete: 1
113
114 =head1 TODO
115      support $entry->enclosures
116
117 =head1 AUTHOR
118
119 Tokuhiro Matsuno <tokuhiro at mobilefactory.jp>
120
121 =head1 THANKS TO
122
123     grzm
124
125 =head1 SEE ALSO
126
127 L<Plagger>
128
129 =cut
130
Note: See TracBrowser for help on using the browser.