こちらのサイトで公開されているPlaggerのプラグイン。
フィードを携帯とかに送りたい時、PlainTextのメールで欲しいとかMIMEがmixedなのは嫌だって時に便利な以下の二つのプラグインだが、上手く動かなかったりスペル間違いがあったりしたので自分なりにがんばって修正してみた。
Filter::FormatText
package Plagger::Plugin::Filter::FormatText; use strict; use base qw( Plagger::Plugin ); use HTML::TreeBuilder; use HTML::FormatText; use Encode; sub register { my($self, $context) = @_; $context->register_hook( $self, 'update.entry.fixup' => &filter, ); } sub filter { my($self, $context, $args) = @_; my $cfg = $self->conf; my $entry = $args->{entry}; my $left = $cfg->{left_margin} || 0; my $right = $cfg->{right_margin} || 72; my $tree = HTML::TreeBuilder->new()->parse($entry->body); my $formatter = HTML::FormatText->new( leftmargin => $left, rightmargin => $right, ); my $body = $formatter->format($tree); my $len = length($body); my $start = $cfg->{start} || 0; my $end = $cfg->{length} || $len; if ($cfg->{start} || $cfg->{length}) { my $more = ''; $more = ' ...' if $len > $end; $body = substr($body, $start, $end) . $more; } # $entry->body('</pre><pre>'.$body.'</pre>'); $entry->body($body); $context->log(info => "format $entry->{link}") if $entry->{link}; } 1; __END__
Publish::Iso_2022_jp_mail
package Plagger::Plugin::Publish::Iso_2022_jp_mail; use strict; use base qw( Plagger::Plugin ); use DateTime; use DateTime::Format::Mail; use Encode; use Encode::MIME::Header; use Jcode; use MIME::Lite; our %TLSConn; sub rule_hook { 'publish.entry.fixup' } sub register { my($self, $context) = @_; $context->autoload_plugin({ module => "Filter::FormatText" }); $context->register_hook( $self, 'publish.entry.fixup' => ¬ify, ); } sub init { my $self = shift; $self->SUPER::init(@_); $self->conf->{mailto} or Plagger->context->error("mailto is required"); $self->conf->{mailfrom} ||= 'plagger@localhost'; print "....n"; } sub notify { my ($self, $context, $args) = @_; return if $args->{feed}->count == 0; my $cnf = $self->conf; my $now = Plagger::Date->now(timezone => $context->conf->{timezone}); my $subject = $args->{feed}->title || '(no-title)'; my $from = $cnf->{mailfrom}; my $msg = MIME::Lite->new( Date => $now->format('Mail'), From => encode('MIME-Header-ISO_2022_JP', $from), To => encode('MIME-Header-ISO_2022_JP', $cnf->{mailto}), Subject => encode('MIME-Header-ISO_2022_JP', $subject), Type => 'text/plain; charset=ISO-2022-JP', Encoding => '7bit', Data => encode_body($args->{entry}->body), ); $msg->send(); $context->log(info => "Sending $subject to $cnf->{mailto}"); } sub encode_body { my $str = shift; $str = remove_utf8_flag($str); $str =~ s/x0Dx0A/n/g; $str =~ tr/r/n/; return Jcode->new($str, guess_encoding($str))->jis; } sub guess_encoding { my $str = shift; my $enc = Jcode::getcode($str) || 'euc'; $enc = 'euc' if $enc eq 'ascii' || $enc eq 'binary'; return $enc; } sub remove_utf8_flag { pack 'C0A*', $_[0] } 1;