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/; |
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 |
|
|
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, |
'queue:s' => \$queue_dir, |
116 |
|
'debug!' => \$debug, |
117 |
); |
); |
118 |
|
|
119 |
#$SIG{__DIE__} = sub { |
#$SIG{__DIE__} = sub { |
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 |
|
|
696 |
|
|
697 |
my $seen_times = $_stat->{rss}->{seen}->{$args->{channel}}->{$feed->link}->{$entry->id}++; |
my $seen_times = $_stat->{rss}->{seen}->{$args->{channel}}->{$feed->link}->{$entry->id}++; |
698 |
# seen allready? |
# seen allready? |
699 |
warn "## $seen_times ",$feed->link if $debug; |
warn "## $seen_times ",$entry->id if $debug; |
700 |
next if $seen_times > 0; |
next if $seen_times > 0; |
701 |
|
|
702 |
sub prefix { |
sub prefix { |
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 |
|
|
743 |
_log(">> $type $to", $msg); |
_log("RSS generated $type to $to:", $msg); |
|
# $args->{kernel}->post( $irc => $type => $to, $msg ); |
|
744 |
# XXX enqueue message to send later |
# XXX enqueue message to send later |
745 |
sub enqueue_post { |
sub enqueue_post { |
746 |
my $post = dump( @_ ); |
my $post = dump( @_ ); |
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 |
|
|
804 |
my $data = read_file( $job->get_data_path ) || die "can't load ", $job->get_data_path, ": $!"; |
my $data = read_file( $job->get_data_path ) || die "can't load ", $job->get_data_path, ": $!"; |
805 |
# $kernel->post( $irc => $type => $to, $msg ); |
# $kernel->post( $irc => $type => $to, $msg ); |
806 |
my @data = eval $data; |
my @data = eval $data; |
807 |
_log ">> post from queue ", $irc, @data; |
_log "IRC post from queue:", @data; |
808 |
$kernel->post( $irc => @data ); |
$kernel->post( $irc => @data ); |
809 |
$job->finish; |
$job->finish; |
810 |
warn "## done queued job: ",dump( @data ) if $debug; |
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 if ! $debug; |
|
|
|
|
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]; |
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 |
|
|