| 1 | package Plagger::Plugin::Publish::Gmail; | ||
| 2 | use strict; | ||
| 3 | use base qw( Plagger::Plugin ); | ||
| 4 | |||
| 5 | our $VERSION = '0.10'; | ||
| 6 | |||
| 7 | use DateTime; | ||
| 8 | use DateTime::Format::Mail; | ||
| 9 | use Encode; | ||
| 10 | use Encode::MIME::Header; | ||
| 11 | use HTML::Entities; | ||
| 12 | use HTML::Parser; | ||
| 13 | use MIME::Lite; | ||
| 14 | |||
| 15 | our %TLSConn; | ||
| 16 | |||
| 17 | sub rule_hook { 'publish.feed' } | ||
| 18 | |||
| 19 | sub register { | ||
| 20 | my($self, $context) = @_; | ||
| 21 | $context->register_hook( | ||
| 22 | $self, | ||
| 23 | 'publish.init' => \&initialize, | ||
| 24 | 'publish.feed' => \¬ify, | ||
| 25 | ); | ||
| 26 | } | ||
| 27 | |||
| 28 | sub init { | ||
| 29 | my $self = shift; | ||
| 30 | $self->SUPER::init(@_); | ||
| 31 | |||
| 32 | $self->conf->{mailto} or Plagger->context->error("mailto is required"); | ||
| 33 | $self->conf->{mailfrom} ||= 'plagger@localhost'; | ||
| 34 | } | ||
| 35 | |||
| 36 | sub initialize { | ||
| 37 | my($self,$context) = @_; | ||
| 38 | |||
| 39 | # authenticate POP before SMTP | ||
| 40 | if (my $conf = $self->conf->{pop3}) { | ||
| 41 | require Net::POP3; | ||
| 42 | my $pop = Net::POP3->new($conf->{host}); | ||
| 43 | if ($pop->apop($conf->{username}, $conf->{password})) { | ||
| 44 | $context->log(info => 'APOP login succeed'); | ||
| 45 | } elsif ($pop->login($conf->{username}, $conf->{password})) { | ||
| 46 | $context->log(info => 'POP3 login succeed'); | ||
| 47 | } else { | ||
| 48 | $context->log(error => 'POP3 login error'); | ||
| 49 | } | ||
| 50 | $pop->quit; | ||
| 51 | } | ||
| 52 | } | ||
| 53 | |||
| 54 | sub notify { | ||
| 55 | my($self, $context, $args) = @_; | ||
| 56 | |||
| 57 | return if $args->{feed}->count == 0; | ||
| 58 | |||
| 59 | my $feed = $args->{feed}; | ||
| 60 | my $subject = $feed->title || '(no-title)'; | ||
| 61 | |||
| 62 | my @enclosure_cb; | ||
| 63 | if ($self->conf->{attach_enclosures}) { | ||
| 64 | for my $entry ($args->{feed}->entries) { | ||
| 65 | push @enclosure_cb, $self->prepare_enclosures($entry); | ||
| 66 | } | ||
| 67 | } | ||
| 68 | |||
| 69 | my $encoding = $self->conf->{encoding} || 'utf-8'; | ||
| 70 | my $body = $self->templatize('gmail_notify.tt', { feed => $feed, encoding => $encoding }); | ||
| 71 | |||
| 72 | my $cfg = $self->conf; | ||
| 73 | $context->log(info => "Sending $subject to $cfg->{mailto}"); | ||
| 74 | |||
| 75 | my $feed_title = $feed->title; | ||
| 76 | $feed_title =~ tr/,//d; | ||
| 77 | |||
| 78 | my $now = Plagger::Date->now(timezone => $context->conf->{timezone}); | ||
| 79 | |||
| 80 | my $msg = MIME::Lite->new( | ||
| 81 | Date => $now->format('Mail'), | ||
| 82 | From => encode('MIME-Header', qq("$feed_title" <$cfg->{mailfrom}>)), | ||
| 83 | To => $cfg->{mailto}, | ||
| 84 | Subject => encode('MIME-Header', $subject), | ||
| 85 | Type => 'multipart/related', | ||
| 86 | ); | ||
| 87 | $msg->replace("X-Mailer" => "Plagger/$Plagger::VERSION"); | ||
| 88 | |||
| 89 | $msg->attach( | ||
| 90 | Type => "text/html; charset=$encoding", | ||
| 91 | Data => encode($encoding, $body, Encode::FB_HTMLCREF), | ||
| 92 | Encoding => 'quoted-printable', | ||
| 93 | ); | ||
| 94 | |||
| 95 | for my $cb (@enclosure_cb) { | ||
| 96 | $cb->($msg); | ||
| 97 | } | ||
| 98 | |||
| 99 | my $route = $cfg->{mailroute} || { via => 'smtp', host => 'localhost' }; | ||
| 100 | $route->{via} ||= 'smtp'; | ||
| 101 | |||
| 102 | eval { | ||
| 103 | if ($route->{via} eq 'smtp_tls') { | ||
| 104 | $self->{tls_args} = [ | ||
| 105 | $route->{host}, | ||
| 106 | User => $route->{username}, | ||
| 107 | Password => $route->{password}, | ||
| 108 | Port => $route->{port} || 587, | ||
| 109 | Timeout => $route->{timeout} || 300, | ||
| 110 | ]; | ||
| 111 | $msg->send_by_smtp_tls(@{ $self->{tls_args} }); | ||
| 112 | } elsif ($route->{via} eq 'sendmail') { | ||
| 113 | my %param = (FromSender => "<$cfg->{mailfrom}>"); | ||
| 114 | $param{Sendmail} = $route->{command} if defined $route->{command}; | ||
| 115 | $msg->send('sendmail', %param); | ||
| 116 | } else { | ||
| 117 | my @args = $route->{host} ? ($route->{host}) : (); | ||
| 118 | $msg->send($route->{via}, @args); | ||
| 119 | } | ||
| 120 | }; | ||
| 121 | |||
| 122 | if ($@) { | ||
| 123 | $context->log(error => "Error while sending emails: $@"); | ||
| 124 | } | ||
| 125 | } | ||
| 126 | |||
| 127 | sub prepare_enclosures { | ||
| 128 | my($self, $entry) = @_; | ||
| 129 | |||
| 130 | if (grep $_->is_inline, $entry->enclosures) { | ||
| 131 | # replace inline enclosures to cid: entities | ||
| 132 | my %url2enclosure = map { $_->url => $_ } $entry->enclosures; | ||
| 133 | |||
| 134 | my $output; | ||
| 135 | my $p = HTML::Parser->new(api_version => 3); | ||
| 136 | $p->handler( default => sub { $output .= $_[0] }, "text" ); | ||
| 137 | $p->handler( start => sub { | ||
| 138 | my($tag, $attr, $attrseq, $text) = @_; | ||
| 139 | # TODO: use HTML::Tagset? | ||
| 140 | if (my $url = $attr->{src}) { | ||
| 141 | if (my $enclosure = $url2enclosure{$url}) { | ||
| 142 | $attr->{src} = "cid:" . $self->enclosure_id($enclosure); | ||
| 143 | } | ||
| 144 | $output .= $self->generate_tag($tag, $attr, $attrseq); | ||
| 145 | } else { | ||
| 146 | $output .= $text; | ||
| 147 | } | ||
| 148 | }, "tag, attr, attrseq, text"); | ||
| 149 | $p->parse($entry->body); | ||
| 150 | $p->eof; | ||
| 151 | |||
| 152 | $entry->body($output); | ||
| 153 | } | ||
| 154 | |||
| 155 | return sub { | ||
| 156 | my $msg = shift; | ||
| 157 | |||
| 158 | for my $enclosure (grep $_->local_path, $entry->enclosures) { | ||
| 159 | if (!-e $enclosure->local_path) { | ||
| 160 | Plagger->context->log(warning => $enclosure->local_path . " doesn't exist. Skip"); | ||
| 161 | next; | ||
| 162 | } | ||
| 163 | |||
| 164 | my %param = ( | ||
| 165 | Type => $enclosure->type, | ||
| 166 | Path => $enclosure->local_path, | ||
| 167 | Filename => $enclosure->filename, | ||
| 168 | ); | ||
| 169 | |||
| 170 | if ($enclosure->is_inline) { | ||
| 171 | $param{Id} = '<' . $self->enclosure_id($enclosure) . '>'; | ||
| 172 | $param{Disposition} = 'inline'; | ||
| 173 | } else { | ||
| 174 | $param{Disposition} = 'attachment'; | ||
| 175 | } | ||
| 176 | |||
| 177 | $msg->attach(%param); | ||
| 178 | } | ||
| 179 | } | ||
| 180 | } | ||
| 181 | |||
| 182 | sub generate_tag { | ||
| 183 | my($self, $tag, $attr, $attrseq) = @_; | ||
| 184 | |||
| 185 | return "<$tag " . | ||
| 186 | join(' ', map { $_ eq '/' ? '/' : sprintf qq(%s="%s"), $_, encode_entities($attr->{$_}, q(<>"')) } @$attrseq) . | ||
| 187 | '>'; | ||
| 188 | } | ||
| 189 | |||
| 190 | sub enclosure_id { | ||
| 191 | my($self, $enclosure) = @_; | ||
| 192 | return Digest::MD5::md5_hex($enclosure->url->as_string) . '@Plagger'; | ||
| 193 | } | ||
| 194 | |||
| 195 | sub DESTORY { | ||
| 196 | my $self = shift; | ||
| 197 | return unless $self->{tls_args}; | ||
| 198 | |||
| 199 | my $conn_key = join "|", @{ $self->{tls_args} }; | ||
| 200 | eval { | ||
| 201 | local $SIG{__WARN__} = sub { }; | ||
| 202 | $TLSConn{$conn_key} && $TLSConn{$conn_key}->quit; | ||
| 203 | }; | ||
| 204 | |||
| 205 | # known error from Gmail SMTP | ||
| 206 | if ($@ && $@ !~ /An error occurred disconnecting from the mail server/) { | ||
| 207 | warn $@; | ||
| 208 | } | ||
| 209 | } | ||
| 210 | |||
| 211 | # hack MIME::Lite to support TLS Authentication | ||
| 212 | *MIME::Lite::send_by_smtp_tls = sub { | ||
| 213 | my($self, @args) = @_; | ||
| 214 | |||
| 215 | ### We need the "From:" and "To:" headers to pass to the SMTP mailer: | ||
| 216 | my $hdr = $self->fields(); | ||
| 217 | my($from) = MIME::Lite::extract_addrs( $self->get('From') ); | ||
| 218 | my $to = $self->get('To'); | ||
| 219 | |||
| 220 | ### Sanity check: | ||
| 221 | defined($to) or Carp::croak "send_by_smtp_tls: missing 'To:' address\n"; | ||
| 222 | |||
| 223 | ### Get the destinations as a simple array of addresses: | ||
| 224 | my @to_all = MIME::Lite::extract_addrs($to); | ||
| 225 | if ($MIME::Lite::AUTO_CC) { | ||
| 226 | foreach my $field (qw(Cc Bcc)) { | ||
| 227 | my $value = $self->get($field); | ||
| 228 | push @to_all, MIME::Lite::extract_addrs($value) if defined($value); | ||
| 229 | } | ||
| 230 | } | ||
| 231 | |||
| 232 | ### Create SMTP TLS client: | ||
| 233 | require Net::SMTP::TLS; | ||
| 234 | |||
| 235 | my $conn_key = join "|", @args; | ||
| 236 | my $smtp; | ||
| 237 | unless ($smtp = $TLSConn{$conn_key}) { | ||
| 238 | $smtp = $TLSConn{$conn_key} = MIME::Lite::SMTP::TLS->new(@args) | ||
| 239 | or Carp::croak("Failed to connect to mail server: $!\n"); | ||
| 240 | } | ||
| 241 | $smtp->mail($from); | ||
| 242 | $smtp->to(@to_all); | ||
| 243 | $smtp->data(); | ||
| 244 | |||
| 245 | ### MIME::Lite can print() to anything with a print() method: | ||
| 246 | $self->print_for_smtp($smtp); | ||
| 247 | $smtp->dataend(); | ||
| 248 | |||
| 249 | 1; | ||
| 250 | }; | ||
| 251 | |||
| 252 | @MIME::Lite::SMTP::TLS::ISA = qw( Net::SMTP::TLS ); | ||
| 253 | sub MIME::Lite::SMTP::TLS::print { shift->datasend(@_) } | ||
| 254 | |||
| 255 | 1; | ||
| 256 | |||
| 257 | __END__ |