2 |
use strict; |
use strict; |
3 |
$|++; |
$|++; |
4 |
|
|
5 |
use POE qw(Component::IRC Component::Server::HTTP); |
use POE qw(Component::IRC Component::Server::HTTP Component::Client::HTTP); |
6 |
use HTTP::Status; |
use HTTP::Status; |
7 |
use DBI; |
use DBI; |
8 |
use Regexp::Common qw /URI/; |
use Regexp::Common qw /URI/; |
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 |
|
|
81 |
|
|
82 |
my @channels = ( $CHANNEL ); |
my @channels = ( $CHANNEL ); |
83 |
|
|
84 |
warn "# config = ", dump( $irc_config ), $/; |
warn "## config = ", dump( $irc_config ) if $debug; |
85 |
|
|
86 |
my $NICK = $irc_config->{nick} or die "no nick?"; |
my $NICK = $irc_config->{nick} or die "no nick?"; |
87 |
|
|
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 |
|
'debug!' => \$debug, |
117 |
); |
); |
118 |
|
|
119 |
#$SIG{__DIE__} = sub { |
#$SIG{__DIE__} = sub { |
126 |
|
|
127 |
open(STDOUT, '>', $log_path) && warn "log to $log_path: $!\n"; |
open(STDOUT, '>', $log_path) && warn "log to $log_path: $!\n"; |
128 |
|
|
129 |
|
# queue |
130 |
|
|
131 |
|
if ( ! -d $queue_dir ) { |
132 |
|
warn "## creating queue directory $queue_dir"; |
133 |
|
mkdir $queue_dir or die "can't create queue directory $queue_dir: $!"; |
134 |
|
} |
135 |
|
|
136 |
|
my $dq = IPC::DirQueue->new({ dir => $queue_dir }); |
137 |
|
|
138 |
# HTML formatters |
# HTML formatters |
139 |
|
|
140 |
my %escape = ('<'=>'<', '>'=>'>', '&'=>'&', '"'=>'"'); |
my %escape = ('<'=>'<', '>'=>'>', '&'=>'&', '"'=>'"'); |
667 |
|
|
668 |
my $_stat; |
my $_stat; |
669 |
|
|
670 |
|
POE::Component::Client::HTTP->spawn( |
671 |
|
Alias => 'rss-fetch', |
672 |
|
Timeout => 30, |
673 |
|
); |
674 |
|
|
675 |
sub rss_fetch { |
sub rss_parse_xml { |
676 |
my ($args) = @_; |
my ($args) = @_; |
677 |
|
|
678 |
|
warn "## rss_parse_xml ",dump( @_ ) if $debug; |
679 |
|
|
680 |
# 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? |
681 |
my $send_rss_msgs = 1; |
my $send_rss_msgs = 1; |
682 |
|
|
683 |
_log "RSS fetch", $args->{url}; |
_log "RSS fetch", $args->{url}; |
684 |
|
|
685 |
my $feed = XML::Feed->parse(URI->new( $args->{url} )); |
my $feed = XML::Feed->parse( \$args->{xml} ); |
686 |
if ( ! $feed ) { |
if ( ! $feed ) { |
687 |
_log("can't fetch RSS ", $args->{url}); |
_log "can't fetch RSS ", $args->{url}, XML::Feed->errstr; |
688 |
return; |
return; |
689 |
} |
} |
690 |
|
|
694 |
for my $entry ($feed->entries) { |
for my $entry ($feed->entries) { |
695 |
$total++; |
$total++; |
696 |
|
|
697 |
|
my $seen_times = $_stat->{rss}->{seen}->{$args->{channel}}->{$feed->link}->{$entry->id}++; |
698 |
# seen allready? |
# seen allready? |
699 |
next if $_stat->{rss}->{seen}->{$args->{channel}}->{$feed->link}->{$entry->id}++ > 0; |
warn "## $seen_times ",$entry->id if $debug; |
700 |
|
next if $seen_times > 0; |
701 |
|
|
702 |
sub prefix { |
sub prefix { |
703 |
my ($txt,$var) = @_; |
my ($txt,$var) = @_; |
730 |
$msg .= prefix( ' ' , $tags ); |
$msg .= prefix( ' ' , $tags ); |
731 |
} |
} |
732 |
|
|
733 |
if ( $args->{kernel} && $send_rss_msgs ) { |
if ( $seen_times == 0 && $send_rss_msgs ) { |
734 |
$send_rss_msgs--; |
$send_rss_msgs--; |
735 |
if ( ! $args->{private} ) { |
if ( ! $args->{private} ) { |
736 |
# FIXME bug! should be save_message |
# FIXME bug! should be save_message |
737 |
# save_message( channel => $args->{channel}, me => 1, nick => $NICK, message => $msg ); |
save_message( channel => $args->{channel}, me => 1, nick => $NICK, message => $msg ); |
738 |
$sth_insert_log->execute( $args->{channel}, 1, $NICK, $msg, 'now()' ); |
# $sth_insert_log->execute( $args->{channel}, 1, $NICK, $msg, 'now()' ); |
739 |
} |
} |
740 |
my ( $type, $to ) = ( 'notice', $args->{channel} ); |
my ( $type, $to ) = ( 'notice', $args->{channel} ); |
741 |
( $type, $to ) = ( 'privmsg', $args->{nick} ) if $args->{private}; |
( $type, $to ) = ( 'privmsg', $args->{nick} ) if $args->{private}; |
742 |
_log(">> $type $to", $msg); |
|
743 |
$args->{kernel}->post( $irc => $type => $to, $msg ); |
_log("RSS generated $type to $to:", $msg); |
744 |
|
# XXX enqueue message to send later |
745 |
|
sub enqueue_post { |
746 |
|
my $post = dump( @_ ); |
747 |
|
warn "## queue_post $post\n" if $debug; |
748 |
|
$dq->enqueue_string( $post ); |
749 |
|
} |
750 |
|
enqueue_post( $type => $to => $msg ); |
751 |
|
|
752 |
$updates++; |
$updates++; |
753 |
} |
} |
754 |
} |
} |
771 |
where active is true |
where active is true |
772 |
}; |
}; |
773 |
# limit to newer feeds only if we are not sending messages out |
# limit to newer feeds only if we are not sending messages out |
774 |
$sql .= qq{ and last_update + delay < now() } if $kernel; |
$sql .= qq{ and last_update + delay < now() } if defined ( $_stat->{rss}->{fetch} ); |
775 |
my $sth = $dbh->prepare( $sql ); |
my $sth = $dbh->prepare( $sql ); |
776 |
$sth->execute(); |
$sth->execute(); |
777 |
warn "# ",$sth->rows," active RSS feeds\n"; |
warn "# ",$sth->rows," active RSS feeds\n"; |
778 |
my $count = 0; |
my $count = 0; |
779 |
while (my $row = $sth->fetchrow_hashref) { |
while (my $row = $sth->fetchrow_hashref) { |
780 |
$row->{kernel} = $kernel if $kernel; |
warn "## queued rss-fetch for ", $row->{url} if $debug; |
781 |
$count += rss_fetch( $row ); |
$_stat->{rss}->{fetch}->{ $row->{url} } = $row; |
782 |
|
$kernel->post( |
783 |
|
'rss-fetch', |
784 |
|
'request', |
785 |
|
'rss_response', |
786 |
|
HTTP::Request->new( GET => $row->{url} ), |
787 |
|
); |
788 |
} |
} |
789 |
return "OK, fetched $count posts from " . $sth->rows . " feeds"; |
return "OK, scheduled " . $sth->rows . " feeds for refresh"; |
790 |
} |
} |
791 |
|
|
792 |
|
|
794 |
my $kernel = shift; |
my $kernel = shift; |
795 |
$_stat->{rss}->{last_poll} ||= time(); |
$_stat->{rss}->{last_poll} ||= time(); |
796 |
my $dt = time() - $_stat->{rss}->{last_poll}; |
my $dt = time() - $_stat->{rss}->{last_poll}; |
|
warn "## rss_check_updates $dt > $rss_min_delay\n"; |
|
797 |
if ( $dt > $rss_min_delay ) { |
if ( $dt > $rss_min_delay ) { |
798 |
|
warn "## rss_check_updates $dt > $rss_min_delay\n"; |
799 |
$_stat->{rss}->{last_poll} = time(); |
$_stat->{rss}->{last_poll} = time(); |
800 |
_log rss_fetch_all( $kernel ); |
_log rss_fetch_all( $kernel ); |
801 |
} |
} |
802 |
|
# XXX send queue messages |
803 |
|
while ( my $job = $dq->pickup_queued_job() ) { |
804 |
|
my $data = read_file( $job->get_data_path ) || die "can't load ", $job->get_data_path, ": $!"; |
805 |
|
# $kernel->post( $irc => $type => $to, $msg ); |
806 |
|
my @data = eval $data; |
807 |
|
_log "IRC post from queue:", @data; |
808 |
|
$kernel->post( $irc => @data ); |
809 |
|
$job->finish; |
810 |
|
warn "## done queued job: ",dump( @data ) if $debug; |
811 |
|
} |
812 |
} |
} |
813 |
|
|
|
# seed rss seen cache so we won't send out all items on startup |
|
|
_log rss_fetch_all; |
|
|
|
|
814 |
POE::Session->create( inline_states => { |
POE::Session->create( inline_states => { |
815 |
_start => sub { |
_start => sub { |
816 |
$_[KERNEL]->post( $irc => register => 'all' ); |
$_[KERNEL]->post( $irc => register => 'all' ); |
821 |
my $poco_object = $sender->get_heap(); |
my $poco_object = $sender->get_heap(); |
822 |
_log "connected to",$poco_object->server_name(); |
_log "connected to",$poco_object->server_name(); |
823 |
$kernel->post( $sender => join => $_ ) for @channels; |
$kernel->post( $sender => join => $_ ) for @channels; |
824 |
|
# seen RSS cache |
825 |
|
_log rss_fetch_all( $kernel ); |
826 |
undef; |
undef; |
827 |
}, |
}, |
828 |
irc_255 => sub { # server is done blabbing |
# irc_255 => sub { # server is done blabbing |
829 |
$_[KERNEL]->post( $irc => join => $CHANNEL); |
# $_[KERNEL]->post( $irc => join => $CHANNEL); |
830 |
}, |
# }, |
831 |
irc_public => sub { |
irc_public => sub { |
832 |
my $kernel = $_[KERNEL]; |
my $kernel = $_[KERNEL]; |
833 |
my $nick = (split /!/, $_[ARG0])[0]; |
my $nick = (split /!/, $_[ARG0])[0]; |
877 |
my $nick = (split /!/, $_[ARG0])[0]; |
my $nick = (split /!/, $_[ARG0])[0]; |
878 |
my $msg = $_[ARG2]; |
my $msg = $_[ARG2]; |
879 |
my $channel = $_[ARG1]->[0]; |
my $channel = $_[ARG1]->[0]; |
880 |
|
warn "# ARG = ",dump( @_[ARG0,ARG1,ARG2] ) if $debug; |
881 |
|
|
882 |
my $res = "unknown command '$msg', try /msg $NICK help!"; |
my $res = "unknown command '$msg', try /msg $NICK help!"; |
883 |
my @out; |
my @out; |
1122 |
_log ">> registreted, so IDENTIFY"; |
_log ">> registreted, so IDENTIFY"; |
1123 |
$_[KERNEL]->post( $irc => privmsg => 'nickserv', "IDENTIFY $NICK" ); |
$_[KERNEL]->post( $irc => privmsg => 'nickserv', "IDENTIFY $NICK" ); |
1124 |
} else { |
} else { |
1125 |
warn "## ignore $m\n"; |
warn "## ignore $m\n" if $debug; |
1126 |
} |
} |
1127 |
}, |
}, |
1128 |
irc_snotice => sub { |
irc_snotice => sub { |
1141 |
""; |
""; |
1142 |
0; # false for signals |
0; # false for signals |
1143 |
}, |
}, |
1144 |
|
rss_response => sub { |
1145 |
|
my ($request_packet, $response_packet) = @_[ARG0, ARG1]; |
1146 |
|
my $request_object = $request_packet->[0]; |
1147 |
|
my $response_object = $response_packet->[0]; |
1148 |
|
|
1149 |
|
my $row = delete( $_stat->{rss}->{fetch}->{ $request_object->uri } ); |
1150 |
|
if ( $row ) { |
1151 |
|
$row->{xml} = $response_object->content; |
1152 |
|
rss_parse_xml( $row ); |
1153 |
|
} else { |
1154 |
|
warn "## can't find rss->fetch for ", $request_object->uri; |
1155 |
|
} |
1156 |
|
}, |
1157 |
}, |
}, |
1158 |
); |
); |
1159 |
|
|