Plagger/Plugin/Publish/IMAP.pm untested

filter: Statementstmt 0.0% 0 / 72 Branchbran 0.0% 0 / 6 Conditioncond 0.0% 0 / 17 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 0.0% 0 / 16 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 125 CC 4 SCAR 13.9
CC
4
Cov
0%
initialize
12.0
finalize
6.0
register
2.0
CRAP 4.0
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 'plugin.init' => \&initialize,
20 'publish.entry' => \&store_entry,
21 'publish.finalize' => \&finalize,
22 );
23 }
24  
25 sub rule_hook { 'publish.entry' }
26  
27 sub initialize {
28 my($self, $context, $args) = @_;
29 my $cfg = $self->conf;
30 $self->{imap} = Mail::IMAPClient->new(
31 User => $cfg->{username},
32 Password => $cfg->{password},
33 Server => $cfg->{host} || 'localhost',
34 Port => $cfg->{port} || 143,
35 )
36 or die $context->log(error => "Cannot connect; $@");
37 $context->log(debug => "Connected IMAP-SERVER (" . $cfg->{host} . ")");
38 if ($cfg->{folder} && !$self->{imap}->exists($cfg->{folder})) {
39 $self->{imap}->create($cfg->{folder})
40 or die $context->log(error => "Could not create $cfg->{folder}: $@");
41 $context->log(info => "Create new folder ($cfg->{folder})");
42 }
43 if (!$cfg->{mailfrom}) {
44 $cfg->{mailfrom} = 'plagger';
45 }
46 }
47  
48 sub finalize {
49 my($self, $context, $args) = @_;
50 my $cfg = $self->{conf};
51 $self->{imap}->disconnect();
52 if (my $msg_count = $self->{msg}) {
53 $context->log(info => "Store $msg_count Message(s)");
54 }
55 $context->log(debug => "Disconnected IMAP-SERVER (" . $cfg->{host} . ")");
56 }
57  
58 sub store_entry {
59 my($self, $context, $args) = @_;
60 my $cfg = $self->conf;
61 my $msg;
62 my $entry = $args->{entry};
63 my $feed_title = $args->{feed}->title;
64 $feed_title =~ tr/,//d;
65 my $subject = $entry->title || '(no-title)';
66 my $body =
67 $self->templatize('mail.tt',
68 { entry => $args->{entry}, feed => $args->{feed} });
69 my $now = Plagger::Date->now(timezone => $context->conf->{timezone});
70 $msg = MIME::Lite->new(
71 Date => $now->format('Mail'),
72 From => encode('MIME-Header', qq("$feed_title" <$cfg->{mailfrom}>)),
73 To => $cfg->{mailto},
74 Subject => encode('MIME-Header', $subject),
75 Type => 'multipart/related',
76 );
77 $body = encode("utf-8", $body);
78 $msg->attach(
79 Type => 'text/html; charset=utf-8',
80 Data => $body,
81 Encoding => 'quoted-printable',
82 );
83 $msg->add('X-Tags', encode('MIME-Header', join(' ', @{ $entry->tags })));
84 my $xmailer =
85 "MIME::Lite (Publish::Maildir Ver.$self->{version} in plagger)";
86 $msg->replace('X-Mailer', $xmailer);
87 $msg->add('In-Reply-To', "<" . md5_hex($entry->id_safe) . '@localhost>');
88 store_maildir($self, $context, $msg->as_string());
89 $self->{msg} += 1;
90 }
91  
92 sub store_maildir {
93 my($self, $context, $msg) = @_;
94 my $folder = $self->conf->{folder} || 'INBOX';
95 my $uid = $self->{imap}->append_string($folder, $msg)
96 or die $context->log(error => "Could not append: $@");
97 }
98  
99 1;
100  
101 =head1 NAME
102  
103 Plagger::Plugin::Publish::IMAP - Transmits IMAP server
104  
105 =head1 SYNOPSIS
106  
107 - module: Publish::IMAP
108 config:
109 username: user
110 password: passwd
111 folder: plagger
112 mailfrom: plagger@localhost
113  
114 =head1 DESCRIPTION
115  
116 This plug-in changes an entry into e-mail, and transmits it to an IMAP server.
117  
118 =head1 AUTHOR
119  
120 Nobuhito Sato
121  
122 =head1 SEE ALSO
123  
124 L<Plagger>
125  
126 =cut
127