FormatTextとIso_2022_jp_mail

こちらのサイトで公開されている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' => &notify,
    );
}

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;

How do I remove installed Perl modules?

CPANによってインストールしたPerlモジュールを簡単に削除する方法。
CPAN Faqから消えててWeb Archiveから掘り出した。
また要るかもしれないのでここにメモ。

#!/usr/local/bin/perl -w

use ExtUtils::Packlist;
use ExtUtils::Installed;

$ARGV[0] or die "Usage: $0 Module::Namen";

my $mod = $ARGV[0]; 
my $inst = ExtUtils::Installed->new();

foreach my $item (sort($inst->files($mod))) {
     print "removing $itemn";
     unlink $item;
}

my $packfile = $inst->packlist($mod)->packlist_file();
print "removing $packfilen";
     unlink $packfile;

こいつをremove.plなどとして保存。

$perl ./remove.pl モジュール名

で削除実行。

Hip-hop club

I went to the hip-hop club in all night.
I had ever been to a hip-hop club, so I was a little looking forward to that.
My friend recently became a club freak, or so they say.
Surely when the music has started playing, she went to be psychedelic.
Oh everyone started dance and got crazy!!!!!!!!!!!!
I felt there is a free.
I had a quite groovy nite..
Then I found a nice artist called Olive Oil from Oil Works.
He plays very mellow sound but grooveful. I like it.

Da-Tube

思い込みってのは恐ろしくて簡単に自分の世界を変えてしまう。
今まで生きてきて染まってしまった色が正しいのか間違っているのかさえ分からない。
正しい色って何だ?

I’m not sure what the correct color is…