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; |
|
use IPC::DirQueue; |
|
|
use File::Slurp; |
|
21 |
use Encode; |
use Encode; |
22 |
|
|
23 |
=head1 NAME |
=head1 NAME |
57 |
ircname => 'Anna the bot: try /msg irc-logger help', |
ircname => 'Anna the bot: try /msg irc-logger help', |
58 |
}; |
}; |
59 |
|
|
|
my $queue_dir = './queue'; |
|
|
|
|
60 |
my $HOSTNAME = `hostname -f`; |
my $HOSTNAME = `hostname -f`; |
61 |
chomp($HOSTNAME); |
chomp($HOSTNAME); |
62 |
|
|
109 |
GetOptions( |
GetOptions( |
110 |
'import-dircproxy:s' => \$import_dircproxy, |
'import-dircproxy:s' => \$import_dircproxy, |
111 |
'log:s' => \$log_path, |
'log:s' => \$log_path, |
|
'queue:s' => \$queue_dir, |
|
112 |
'debug!' => \$debug, |
'debug!' => \$debug, |
113 |
); |
); |
114 |
|
|
122 |
|
|
123 |
open(STDOUT, '>', $log_path) && warn "log to $log_path: $!\n"; |
open(STDOUT, '>', $log_path) && warn "log to $log_path: $!\n"; |
124 |
|
|
|
# queue |
|
|
|
|
|
if ( ! -d $queue_dir ) { |
|
|
warn "## creating queue directory $queue_dir"; |
|
|
mkdir $queue_dir or die "can't create queue directory $queue_dir: $!"; |
|
|
} |
|
|
|
|
|
my $dq = IPC::DirQueue->new({ dir => $queue_dir }); |
|
125 |
|
|
126 |
# HTML formatters |
# HTML formatters |
127 |
|
|
670 |
=cut |
=cut |
671 |
|
|
672 |
sub rss_parse_xml { |
sub rss_parse_xml { |
673 |
my ($args) = @_; |
my ($kernel,$args) = @_; |
674 |
|
|
675 |
warn "## rss_parse_xml ",dump( @_ ) if $debug; |
warn "## rss_parse_xml ",dump( @_ ) if $debug; |
676 |
|
|
678 |
my $send_rss_msgs = $args->{send_rss_msgs}; |
my $send_rss_msgs = $args->{send_rss_msgs}; |
679 |
$send_rss_msgs = 1 if ! defined $send_rss_msgs; |
$send_rss_msgs = 1 if ! defined $send_rss_msgs; |
680 |
|
|
681 |
_log "RSS fetch first $send_rss_msgs items from", $args->{url}; |
warn "## RSS fetch first $send_rss_msgs items from", $args->{url} if $debug; |
682 |
|
|
683 |
my $feed = XML::Feed->parse( \$args->{xml} ); |
my $feed = XML::Feed->parse( \$args->{xml} ); |
684 |
if ( ! $feed ) { |
if ( ! $feed ) { |
738 |
my ( $type, $to ) = ( 'notice', $args->{channel} ); |
my ( $type, $to ) = ( 'notice', $args->{channel} ); |
739 |
( $type, $to ) = ( 'privmsg', $args->{nick} ) if $args->{private}; |
( $type, $to ) = ( 'privmsg', $args->{nick} ) if $args->{private}; |
740 |
|
|
741 |
_log("RSS generated $type to $to:", $msg); |
_log(">> RSS $type to $to:", $msg); |
742 |
# XXX enqueue message to send later |
$kernel->post( $irc => $type => $to => $msg ); |
|
sub enqueue_post { |
|
|
my $post = dump( @_ ); |
|
|
warn "## queue_post $post\n" if $debug; |
|
|
$dq->enqueue_string( $post ); |
|
|
} |
|
|
enqueue_post( $type => $to => $msg ); |
|
743 |
|
|
744 |
$updates++; |
$updates++; |
745 |
} |
} |
750 |
$sql .= qq{where id = } . $args->{id}; |
$sql .= qq{where id = } . $args->{id}; |
751 |
eval { $dbh->do( $sql ) }; |
eval { $dbh->do( $sql ) }; |
752 |
|
|
753 |
_log "RSS got $total items of which $updates new from", $args->{url}; |
_log "RSS $updates/$total new items from", $args->{url}; |
754 |
|
|
755 |
return $updates; |
return $updates; |
756 |
} |
} |
793 |
$_stat->{rss}->{last_poll} = time(); |
$_stat->{rss}->{last_poll} = time(); |
794 |
_log rss_fetch_all( $kernel ); |
_log rss_fetch_all( $kernel ); |
795 |
} |
} |
|
# XXX send queue messages |
|
|
while ( my $job = $dq->pickup_queued_job() ) { |
|
|
my $data = read_file( $job->get_data_path ) || die "can't load ", $job->get_data_path, ": $!"; |
|
|
my @data = eval $data; |
|
|
_log "IRC post from queue:", @data; |
|
|
$kernel->post( $irc => @data ); |
|
|
$job->finish; |
|
|
warn "## done queued job: ",dump( @data ) if $debug; |
|
|
} |
|
796 |
} |
} |
797 |
|
|
798 |
POE::Session->create( inline_states => { |
POE::Session->create( inline_states => { |
1005 |
|
|
1006 |
my $sql = { |
my $sql = { |
1007 |
add => qq{ insert into feeds (url,name,channel,nick,private) values (?,?,?,?,?) }, |
add => qq{ insert into feeds (url,name,channel,nick,private) values (?,?,?,?,?) }, |
1008 |
# remove => qq{ delete from feeds where url = ? and name = ? }, |
remove => qq{ delete from feeds where url = ? and nick = ? }, |
1009 |
start => qq{ update feeds set active = true where url = ? }, |
start => qq{ update feeds set active = true where url = ? }, |
1010 |
stop => qq{ update feeds set active = false where url = ? }, |
stop => qq{ update feeds set active = false where url = ? }, |
1011 |
clean => qq{ update feeds set last_update = now() - delay where url = ? }, |
clean => qq{ update feeds set last_update = now() - delay where url = ? }, |
1018 |
my @data = ( $url ); |
my @data = ( $url ); |
1019 |
if ( $command eq 'add' ) { |
if ( $command eq 'add' ) { |
1020 |
push @data, ( $arg, $channel, $nick, $sub eq 'private' ? 1 : 0 ); |
push @data, ( $arg, $channel, $nick, $sub eq 'private' ? 1 : 0 ); |
1021 |
|
} elsif ( $command eq 'remove' ) { |
1022 |
|
push @data, $nick; |
1023 |
} |
} |
1024 |
warn "## $command SQL $q with ",dump( @data ),"\n"; |
warn "## $command SQL $q with ",dump( @data ),"\n"; |
1025 |
eval { $sth->execute( @data ) }; |
eval { $sth->execute( @data ) }; |
1026 |
if ($@) { |
if ($@) { |
1027 |
$res = "ERROR: $@"; |
$res = "ERROR: $@"; |
1028 |
} else { |
} else { |
1029 |
$res = "OK, RSS executed $command " . ( $sub ? "-$sub" : '' ) ."on $channel url $url"; |
$res = "OK, RSS executed $command" . |
1030 |
|
( $sub ? "-$sub " : ' ' ) . |
1031 |
|
( $channel ? "on $channel " : '' ) . |
1032 |
|
"url $url"; |
1033 |
if ( $command eq 'clean' ) { |
if ( $command eq 'clean' ) { |
1034 |
my $seen = $_stat->{rss}->{seen} || die "no seen?"; |
my $seen = $_stat->{rss}->{seen} || die "no seen?"; |
1035 |
my $want_link = $_stat->{rss}->{url2link}->{$url} || warn "no url2link($url)"; |
my $want_link = $_stat->{rss}->{url2link}->{$url} || warn "no url2link($url)"; |
1041 |
_log "RSS removed seen $c $url $link"; |
_log "RSS removed seen $c $url $link"; |
1042 |
} |
} |
1043 |
} |
} |
1044 |
|
} elsif ( $command eq 'add' ) { |
1045 |
|
rss_fetch_all( $_[KERNEL] ); |
1046 |
} |
} |
1047 |
} |
} |
1048 |
} else { |
} else { |
1052 |
# this makes sense because we didn't catch rss-clean http://... before! |
# this makes sense because we didn't catch rss-clean http://... before! |
1053 |
$_stat->{rss} = undef; |
$_stat->{rss} = undef; |
1054 |
$dbh->do( qq{ update feeds set last_update = now() - delay } ); |
$dbh->do( qq{ update feeds set last_update = now() - delay } ); |
1055 |
$res = "OK, cleaned RSS cache"; |
$res = rss_fetch_all( $_[KERNEL] ); |
1056 |
} |
} |
1057 |
|
|
1058 |
if ($res) { |
if ($res) { |
1140 |
my $row = delete( $_stat->{rss}->{fetch}->{ $request_object->uri } ); |
my $row = delete( $_stat->{rss}->{fetch}->{ $request_object->uri } ); |
1141 |
if ( $row ) { |
if ( $row ) { |
1142 |
$row->{xml} = $response_object->content; |
$row->{xml} = $response_object->content; |
1143 |
rss_parse_xml( $row ); |
rss_parse_xml( $_[KERNEL], $row ); |
1144 |
} else { |
} else { |
1145 |
warn "## can't find rss->fetch for ", $request_object->uri; |
warn "## can't find rss->fetch for ", $request_object->uri; |
1146 |
} |
} |