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

Revision 1814 (checked in by miyagawa, 14 years ago)

CustomFeed?::POP3: be more liberal to handle Date format

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
70     if (my $date = $email->header('Date')) {
71         my $dt = eval { Plagger::Date->parse($format, $date) }
72             || Plagger::Date->parse_dwim($date);
73         $entry->date($dt) if $dt;
74     }
75
76     $entry->body($self->get_body($email));
77
78     $feed->add_entry($entry);
79
80     return $feed;
81 }
82
83 sub get_body {
84     my ($self, $email) = @_;
85
86     my $body_part;
87     for my $part ($email->parts) {
88         if ($part->content_type =~ m[text/html] or ($part->content_type =~ m[text/plain] and !$body_part)) {
89             $body_part = $part;
90         }
91     }
92     $body_part ||= $email;
93
94     $body_part->content_type =~  /charset=(['"]?)([\w-]+)\1/;
95     my $encoding = $2 || 'utf-8';
96
97     if ($body_part->content_type =~ m[text/html]) {
98         return decode($encoding, $body_part->body);
99     } else {
100         return '<pre>'.encode_entities(decode($encoding, $body_part->body)).'</pre>';
101     }
102 }
103
104 1;
105 __END__
106
107 =head1 NAME
108
109 Plagger::Plugin::CustomFeed::POP3 - Custom feed for POP3
110
111 =head1 SYNOPSIS
112
113   - module: CustomFeed::POP3
114     config:
115         host: example.com
116         username: tokuhirom
117         password: PASSW0RD
118         #delete: 1
119
120 =head1 TODO
121      support $entry->enclosures
122
123 =head1 AUTHOR
124
125 Tokuhiro Matsuno <tokuhiro at mobilefactory.jp>
126
127 =head1 THANKS TO
128
129     grzm
130
131 =head1 SEE ALSO
132
133 L<Plagger>
134
135 =cut
136
Note: See TracBrowser for help on using the browser.