| 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($context, $args); |
|---|
| 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 templatize { |
|---|
| 87 |
my ($self, $context, $args) = @_; |
|---|
| 88 |
my $tt = $context->template(); |
|---|
| 89 |
$tt->process( 'mail.tt', { |
|---|
| 90 |
entry => $args->{entry}, |
|---|
| 91 |
feed => $args->{feed}, |
|---|
| 92 |
}, \my $out ) or $context->error($tt->error); |
|---|
| 93 |
$out; |
|---|
| 94 |
} |
|---|
| 95 |
|
|---|
| 96 |
sub store_maildir { |
|---|
| 97 |
my($self,$context,$msg) = @_; |
|---|
| 98 |
my $folder = $self->conf->{folder} || 'INBOX'; |
|---|
| 99 |
my $uid = $self->{imap}->append_string($folder,$msg) |
|---|
| 100 |
or die $context->log(error => "Could not append: $@"); |
|---|
| 101 |
} |
|---|
| 102 |
|
|---|
| 103 |
1; |
|---|
| 104 |
|
|---|
| 105 |
=head1 NAME |
|---|
| 106 |
|
|---|
| 107 |
Plagger::Plugin::Publish::IMAP - Transmits IMAP server |
|---|
| 108 |
|
|---|
| 109 |
=head1 SYNOPSIS |
|---|
| 110 |
|
|---|
| 111 |
- module: Publish::IMAP |
|---|
| 112 |
config: |
|---|
| 113 |
username: user |
|---|
| 114 |
password: passwd |
|---|
| 115 |
folder: plagger |
|---|
| 116 |
mailfrom: plagger@localhost |
|---|
| 117 |
|
|---|
| 118 |
=head1 DESCRIPTION |
|---|
| 119 |
|
|---|
| 120 |
This plug-in changes an entry into e-mail, and transmits it to an IMAP server. |
|---|
| 121 |
|
|---|
| 122 |
=head1 AUTHOR |
|---|
| 123 |
|
|---|
| 124 |
Nobuhito Sato |
|---|
| 125 |
|
|---|
| 126 |
=head1 SEE ALSO |
|---|
| 127 |
|
|---|
| 128 |
L<Plagger> |
|---|
| 129 |
|
|---|
| 130 |
=cut |
|---|