Plagger::Plugin::Filter::JiftyCommit

結構前からつくってあったんですが、YAPC::NAも終わったので公開。Jifty CommitのメールをIRCに投げるボットって意味あるのか微妙だけど、それはそれ。

package Plagger::Plugin::Filter::JiftyCommit;

use strict;
use base qw( Plagger::Plugin );

our $VERSION = '0.01';

use DateTime::Format::Strptime;

sub register {
    my ($self, $context) = @_;
    $context->register_hook(
        $self,
        'update.feed.fixup' => \&filter,
    );
}

sub filter {
    my ($self, $context, $args) = @_;

    $args->{feed}->title('Jifty-commit');

    my $strp = DateTime::Format::Strptime->new(
        pattern => '%a %b %d %T %Y',
        locale  => 'en_US',
    );
    foreach my $entry ($args->{feed}->entries) {
        my $body  = $entry->body_text;

        my $title = '';
        my $flag  = 0;
        my $dt;
        foreach my $line (split(/[\n\r]/, $body)) {
            if ($line =~ /^Log:/) { $flag = 1; next; }
            if ($line =~ /^$/)    { $flag = 0; next; }
            if ($line =~ /^Date: (.+)$/) {
                $dt = $strp->parse_datetime($1);
            }
            next unless $flag;
            $title .= $line;
        }

        open my $fh, '<+', $self->assets_dir.'counter';
        my $num = <$fh>;
        $num++;
        seek $fh, 0, 0;
        print $fh, $num;
        close $fh;

        $entry->link(
            sprintf('%s/%d-%s/%06d.html',
                'http://lists.bestpractical.com/pipermail/jifty-commit',
                $dt->year,
                $dt->month_name,
                $num,
            )
        ) if defined $dt;

        $entry->title( $title );
    }
}

1;

__END__

=head1 NAME

Plagger::Plugin::Filter::JiftyCommit - format Jifty's commit log for ircbot

=head1 SYNOPSIS

  - module: CustomFeed::POP3
    config:
        host: mail.example.com
        username: jifty@example.com
        password: hogehoge
        delete: 1

  - module: Filter::JiftyCommit

  - module: Notify::IRC
    config:
      daemon_port: 9991
      nickname: jiftybot
      server_host: chat.freenode.net
      server_port: 6667
      server_channels:
        - #jifty
      charset: utf-8
      announce: action

=head1 CONFIG

none

=head1 AUTHOR

Kenichi Ishigaki

=head1 SEE ALSO

L<Plagger>

=cut