| 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__ |