root/trunk/plagger/lib/Plagger/Plugin/Publish/IMAP.pm

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

Merge from refactoring-planet. YAY!

Line 
1 package Plagger::Plugin::Publish::IMAP;
2 use strict;
3 use base qw( Plagger::Plugin );
4
5 use DateTime;
6 use DateTime::Format::Mail;
7 use Encode qw/ from_to encode/;
8 use Encode::MIME::Header;
9 use MIME::Lite;
10 use IO::File;
11 use Mail::IMAPClient;
12 use Digest::MD5 qw/ md5_hex /;;
13
14 sub register {
15     my($self, $context) = @_;
16     $self->{version} = '0.1';
17     $context->register_hook(
18       $self,
19       'publish.init' => \&initialize,
20       'publish.entry.fixup' => \&store_entry,
21       'publish.finalize' => \&finalize,
22     );
23 }
24
25 sub initialize {
26   my ($self, $context, $args) = @_;
27   my $cfg = $self->conf;
28   $self->{imap} = Mail::IMAPClient->new(
29     User     => $cfg->{username},
30     Password => $cfg->{password},
31     Server   => $cfg->{host} || 'localhost',
32     Port     => $cfg->{port} || 143,
33   ) or die $context->log(error => "Cannot connect; $@");
34   $context->log(debug => "Connected IMAP-SERVER (".$cfg->{host}.")");
35   if ($cfg->{folder} && !$self->{imap}->exists($cfg->{folder})) {
36     $self->{imap}->create($cfg->{folder})
37       or die $context->log(error => "Could not create $cfg->{folder}: $@");
38     $context->log(info => "Create new folder ($cfg->{folder})");
39   }
40   if (!$cfg->{mailfrom}) {
41     $cfg->{mailfrom} = 'plagger';
42   }
43 }
44
45 sub finalize {
46   my ($self, $context, $args) = @_;
47   my $cfg  = $self->{conf};
48   $self->{imap}->disconnect();
49   if (my $msg_count = $self->{msg}) {
50     $context->log(info => "Store $msg_count Message(s)");
51   }
52   $context->log(debug => "Disconnected IMAP-SERVER (".$cfg->{host}.")");
53 }
54
55 sub store_entry {
56   my($self, $context, $args) = @_;
57   my $cfg = $self->conf;
58   my $msg;
59   my $entry = $args->{entry};
60   my $feed_title = $args->{feed}->title;
61      $feed_title =~ tr/,//d;
62   my $subject    = $entry->title || '(no-title)';
63   my $body       = $self->templatize('mail.tt', { entry => $args->{entry}, feed => $args->{feed} });
64   my $now = Plagger::Date->now(timezone => $context->conf->{timezone});
65   $msg = MIME::Lite->new(
66     Date    => $now->format('Mail'),
67     From    => encode('MIME-Header', qq("$feed_title" <$cfg->{mailfrom}>)),
68     To      => $cfg->{mailto},
69     Subject => encode('MIME-Header', $subject),
70     Type    => 'multipart/related',
71   );
72   $body = encode("utf-8", $body);
73   $msg->attach(
74     Type => 'text/html; charset=utf-8',
75     Data => $body,
76     Encoding => 'quoted-printable',
77   );
78   $msg->add('X-Tags', encode('MIME-Header',join(' ',@{$entry->tags})));
79   my $xmailer = "MIME::Lite (Publish::Maildir Ver.$self->{version} in plagger)";
80   $msg->replace('X-Mailer',$xmailer);
81   $msg->add('In-Reply-To',"<".md5_hex($entry->id_safe).'@localhost>');
82   store_maildir($self, $context,$msg->as_string());
83   $self->{msg} += 1;
84 }
85
86 sub store_maildir {
87   my($self,$context,$msg) = @_;
88   my $folder = $self->conf->{folder} || 'INBOX';
89   my $uid = $self->{imap}->append_string($folder,$msg)
90     or die $context->log(error => "Could not append: $@");
91 }
92
93 1;
94
95 =head1 NAME
96
97 Plagger::Plugin::Publish::IMAP - Transmits IMAP server
98
99 =head1 SYNOPSIS
100
101   - module: Publish::IMAP
102     config:
103       username: user
104       password: passwd
105       folder: plagger
106       mailfrom: plagger@localhost
107
108 =head1 DESCRIPTION
109
110 This plug-in changes an entry into e-mail, and transmits it to an IMAP server.
111
112 =head1 AUTHOR
113
114 Nobuhito Sato
115
116 =head1 SEE ALSO
117
118 L<Plagger>
119
120 =cut
Note: See TracBrowser for help on using the browser.