18 |
use Carp qw/confess/; |
use Carp qw/confess/; |
19 |
use XML::Feed; |
use XML::Feed; |
20 |
use DateTime::Format::Flexible; |
use DateTime::Format::Flexible; |
21 |
|
use IPC::DirQueue; |
22 |
|
use File::Slurp; |
23 |
|
|
24 |
=head1 NAME |
=head1 NAME |
25 |
|
|
49 |
|
|
50 |
## CONFIG |
## CONFIG |
51 |
|
|
52 |
|
my $debug = 0; |
53 |
|
|
54 |
my $irc_config = { |
my $irc_config = { |
55 |
nick => 'irc-logger', |
nick => 'irc-logger', |
56 |
server => 'irc.freenode.net', |
server => 'irc.freenode.net', |
58 |
ircname => 'Anna the bot: try /msg irc-logger help', |
ircname => 'Anna the bot: try /msg irc-logger help', |
59 |
}; |
}; |
60 |
|
|
61 |
|
my $queue_dir = './queue'; |
62 |
|
|
63 |
my $HOSTNAME = `hostname -f`; |
my $HOSTNAME = `hostname -f`; |
64 |
chomp($HOSTNAME); |
chomp($HOSTNAME); |
65 |
|
|
112 |
GetOptions( |
GetOptions( |
113 |
'import-dircproxy:s' => \$import_dircproxy, |
'import-dircproxy:s' => \$import_dircproxy, |
114 |
'log:s' => \$log_path, |
'log:s' => \$log_path, |
115 |
|
'queue:s' => \$queue_dir, |
116 |
); |
); |
117 |
|
|
118 |
#$SIG{__DIE__} = sub { |
#$SIG{__DIE__} = sub { |
125 |
|
|
126 |
open(STDOUT, '>', $log_path) && warn "log to $log_path: $!\n"; |
open(STDOUT, '>', $log_path) && warn "log to $log_path: $!\n"; |
127 |
|
|
128 |
|
# queue |
129 |
|
|
130 |
|
if ( ! -d $queue_dir ) { |
131 |
|
warn "## creating queue directory $queue_dir"; |
132 |
|
mkdir $queue_dir or die "can't create queue directory $queue_dir: $!"; |
133 |
|
} |
134 |
|
|
135 |
|
my $dq = IPC::DirQueue->new({ dir => $queue_dir }); |
136 |
|
|
137 |
# HTML formatters |
# HTML formatters |
138 |
|
|
139 |
my %escape = ('<'=>'<', '>'=>'>', '&'=>'&', '"'=>'"'); |
my %escape = ('<'=>'<', '>'=>'>', '&'=>'&', '"'=>'"'); |
670 |
sub rss_fetch { |
sub rss_fetch { |
671 |
my ($args) = @_; |
my ($args) = @_; |
672 |
|
|
673 |
|
|
674 |
# how many messages to send out when feed is seen for the first time? |
# how many messages to send out when feed is seen for the first time? |
675 |
my $send_rss_msgs = 1; |
my $send_rss_msgs = 1; |
676 |
|
|
682 |
return; |
return; |
683 |
} |
} |
684 |
|
|
685 |
|
$_stat->{rss}->{url2link}->{ $args->{url} } = $feed->link; |
686 |
|
|
687 |
my ( $total, $updates ) = ( 0, 0 ); |
my ( $total, $updates ) = ( 0, 0 ); |
688 |
for my $entry ($feed->entries) { |
for my $entry ($feed->entries) { |
689 |
$total++; |
$total++; |
718 |
if ( my $tags = $entry->category ) { |
if ( my $tags = $entry->category ) { |
719 |
$tags =~ s!^\s+!!; |
$tags =~ s!^\s+!!; |
720 |
$tags =~ s!\s*$! !; |
$tags =~ s!\s*$! !; |
721 |
$tags =~ s!\s+!// !g; |
$tags =~ s!,?\s+!// !g; |
722 |
$msg .= prefix( ' ' , $tags ); |
$msg .= prefix( ' ' , $tags ); |
723 |
} |
} |
724 |
|
|
726 |
$send_rss_msgs--; |
$send_rss_msgs--; |
727 |
if ( ! $args->{private} ) { |
if ( ! $args->{private} ) { |
728 |
# FIXME bug! should be save_message |
# FIXME bug! should be save_message |
729 |
# save_message( channel => $args->{channel}, me => 1, nick => $NICK, message => $msg ); |
save_message( channel => $args->{channel}, me => 1, nick => $NICK, message => $msg ); |
730 |
$sth_insert_log->execute( $args->{channel}, 1, $NICK, $msg, 'now()' ); |
# $sth_insert_log->execute( $args->{channel}, 1, $NICK, $msg, 'now()' ); |
731 |
} |
} |
732 |
my ( $type, $to ) = ( 'notice', $args->{channel} ); |
my ( $type, $to ) = ( 'notice', $args->{channel} ); |
733 |
( $type, $to ) = ( 'privmsg', $args->{nick} ) if $args->{private}; |
( $type, $to ) = ( 'privmsg', $args->{nick} ) if $args->{private}; |
734 |
|
|
735 |
_log(">> $type $to", $msg); |
_log(">> $type $to", $msg); |
736 |
$args->{kernel}->post( $irc => $type => $to, $msg ); |
# $args->{kernel}->post( $irc => $type => $to, $msg ); |
737 |
|
# XXX enqueue message to send later |
738 |
|
sub enqueue_post { |
739 |
|
my $post = dump( @_ ); |
740 |
|
warn "## queue_post $post\n" if $debug; |
741 |
|
$dq->enqueue_string( $post ); |
742 |
|
} |
743 |
|
enqueue_post( $type => $to => $msg ); |
744 |
|
|
745 |
$updates++; |
$updates++; |
746 |
} |
} |
747 |
} |
} |
781 |
my $kernel = shift; |
my $kernel = shift; |
782 |
$_stat->{rss}->{last_poll} ||= time(); |
$_stat->{rss}->{last_poll} ||= time(); |
783 |
my $dt = time() - $_stat->{rss}->{last_poll}; |
my $dt = time() - $_stat->{rss}->{last_poll}; |
|
warn "## rss_check_updates $dt > $rss_min_delay\n"; |
|
784 |
if ( $dt > $rss_min_delay ) { |
if ( $dt > $rss_min_delay ) { |
785 |
|
warn "## rss_check_updates $dt > $rss_min_delay\n"; |
786 |
$_stat->{rss}->{last_poll} = time(); |
$_stat->{rss}->{last_poll} = time(); |
787 |
_log rss_fetch_all( $kernel ); |
_log rss_fetch_all( $kernel ); |
788 |
} |
} |
789 |
|
# XXX send queue messages |
790 |
|
while ( my $job = $dq->pickup_queued_job() ) { |
791 |
|
my $data = read_file( $job->get_data_path ) || die "can't load ", $job->get_data_path, ": $!"; |
792 |
|
# $kernel->post( $irc => $type => $to, $msg ); |
793 |
|
my @data = eval $data; |
794 |
|
_log ">> post from queue ", $irc, @data; |
795 |
|
$kernel->post( $irc => @data ); |
796 |
|
$job->finish; |
797 |
|
warn "## done queued job: ",dump( @data ) if $debug; |
798 |
|
} |
799 |
} |
} |
800 |
|
|
801 |
# seed rss seen cache so we won't send out all items on startup |
# seed rss seen cache so we won't send out all items on startup |
802 |
_log rss_fetch_all; |
_log rss_fetch_all if ! $debug; |
803 |
|
|
804 |
POE::Session->create( inline_states => { |
POE::Session->create( inline_states => { |
805 |
_start => sub { |
_start => sub { |
865 |
my $nick = (split /!/, $_[ARG0])[0]; |
my $nick = (split /!/, $_[ARG0])[0]; |
866 |
my $msg = $_[ARG2]; |
my $msg = $_[ARG2]; |
867 |
my $channel = $_[ARG1]->[0]; |
my $channel = $_[ARG1]->[0]; |
868 |
|
warn "# ARG = ",dump( @_[ARG0,ARG1,ARG2] ) if $debug; |
869 |
|
|
870 |
my $res = "unknown command '$msg', try /msg $NICK help!"; |
my $res = "unknown command '$msg', try /msg $NICK help!"; |
871 |
my @out; |
my @out; |
994 |
} |
} |
995 |
} elsif ($msg =~ m/^rss-update/) { |
} elsif ($msg =~ m/^rss-update/) { |
996 |
$res = rss_fetch_all( $_[KERNEL] ); |
$res = rss_fetch_all( $_[KERNEL] ); |
|
} elsif ($msg =~ m/^rss-clean/) { |
|
|
$_stat->{rss} = undef; |
|
|
$dbh->do( qq{ update feeds set last_update = now() - delay } ); |
|
|
$res = "OK, cleaned RSS cache"; |
|
997 |
} elsif ($msg =~ m/^rss-list/) { |
} elsif ($msg =~ m/^rss-list/) { |
998 |
my $sth = $dbh->prepare(qq{ select url,name,last_update,active,channel,nick,private from feeds }); |
my $sth = $dbh->prepare(qq{ select url,name,last_update,active,channel,nick,private from feeds }); |
999 |
$sth->execute; |
$sth->execute; |
1001 |
$_[KERNEL]->post( $irc => privmsg => $nick, join(' | ',@row) ); |
$_[KERNEL]->post( $irc => privmsg => $nick, join(' | ',@row) ); |
1002 |
} |
} |
1003 |
$res = ''; |
$res = ''; |
1004 |
} elsif ($msg =~ m!^rss-(add|remove|stop|start)(?:-(private))?\s+(http://\S+)\s*(.*)!) { |
} elsif ($msg =~ m!^rss-(add|remove|stop|start|clean)(?:-(private))?\s+(http://\S+)\s*(.*)!) { |
1005 |
my ( $command, $sub, $url, $arg ) = ( $1,$2,$3,$4 ); |
my ( $command, $sub, $url, $arg ) = ( $1,$2,$3,$4 ); |
1006 |
|
|
1007 |
my $channel = $1 if ( $arg =~ s/\s*(#\S+)\s*// ); |
my $channel = $1 if ( $arg =~ s/\s*(#\S+)\s*// ); |
1012 |
# remove => qq{ delete from feeds where url = ? and name = ? }, |
# remove => qq{ delete from feeds where url = ? and name = ? }, |
1013 |
start => qq{ update feeds set active = true where url = ? }, |
start => qq{ update feeds set active = true where url = ? }, |
1014 |
stop => qq{ update feeds set active = false where url = ? }, |
stop => qq{ update feeds set active = false where url = ? }, |
1015 |
|
clean => qq{ update feeds set last_update = now() - delay where url = ? }, |
1016 |
}; |
}; |
1017 |
|
|
1018 |
if ( $command eq 'add' && ! $channel ) { |
if ( $command eq 'add' && ! $channel ) { |
1028 |
if ($@) { |
if ($@) { |
1029 |
$res = "ERROR: $@"; |
$res = "ERROR: $@"; |
1030 |
} else { |
} else { |
1031 |
$res = "OK, RSS [$command|$sub|$url|$arg]"; |
$res = "OK, RSS executed $command " . ( $sub ? "-$sub" : '' ) ."on $channel url $url"; |
1032 |
|
if ( $command eq 'clean' ) { |
1033 |
|
my $seen = $_stat->{rss}->{seen} || die "no seen?"; |
1034 |
|
my $want_link = $_stat->{rss}->{url2link}->{$url} || warn "no url2link($url)"; |
1035 |
|
foreach my $c ( keys %$seen ) { |
1036 |
|
my $c_hash = $seen->{$c} || die "no seen->{$c}"; |
1037 |
|
die "not HASH with rss links but ", dump($c_hash) unless ref($c_hash) eq 'HASH'; |
1038 |
|
foreach my $link ( keys %$c_hash ) { |
1039 |
|
next unless $link eq $want_link; |
1040 |
|
_log "RSS removed seen $c $url $link"; |
1041 |
|
} |
1042 |
|
} |
1043 |
|
} |
1044 |
} |
} |
1045 |
} else { |
} else { |
1046 |
$res = "ERROR: don't know what to do with: $msg"; |
$res = "ERROR: don't know what to do with: $msg"; |
1047 |
} |
} |
1048 |
|
} elsif ($msg =~ m/^rss-clean/) { |
1049 |
|
# this makes sense because we didn't catch rss-clean http://... before! |
1050 |
|
$_stat->{rss} = undef; |
1051 |
|
$dbh->do( qq{ update feeds set last_update = now() - delay } ); |
1052 |
|
$res = "OK, cleaned RSS cache"; |
1053 |
} |
} |
1054 |
|
|
1055 |
if ($res) { |
if ($res) { |
1110 |
_log ">> registreted, so IDENTIFY"; |
_log ">> registreted, so IDENTIFY"; |
1111 |
$_[KERNEL]->post( $irc => privmsg => 'nickserv', "IDENTIFY $NICK" ); |
$_[KERNEL]->post( $irc => privmsg => 'nickserv', "IDENTIFY $NICK" ); |
1112 |
} else { |
} else { |
1113 |
warn "## ignore $m\n"; |
warn "## ignore $m\n" if $debug; |
1114 |
} |
} |
1115 |
}, |
}, |
1116 |
irc_snotice => sub { |
irc_snotice => sub { |