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/; |
20 |
use DateTime::Format::Flexible; |
use DateTime::Format::Flexible; |
21 |
use IPC::DirQueue; |
use IPC::DirQueue; |
22 |
use File::Slurp; |
use File::Slurp; |
23 |
|
use Encode; |
24 |
|
|
25 |
=head1 NAME |
=head1 NAME |
26 |
|
|
82 |
|
|
83 |
my @channels = ( $CHANNEL ); |
my @channels = ( $CHANNEL ); |
84 |
|
|
85 |
warn "# config = ", dump( $irc_config ), $/; |
warn "## config = ", dump( $irc_config ) if $debug; |
86 |
|
|
87 |
my $NICK = $irc_config->{nick} or die "no nick?"; |
my $NICK = $irc_config->{nick} or die "no nick?"; |
88 |
|
|
114 |
'import-dircproxy:s' => \$import_dircproxy, |
'import-dircproxy:s' => \$import_dircproxy, |
115 |
'log:s' => \$log_path, |
'log:s' => \$log_path, |
116 |
'queue:s' => \$queue_dir, |
'queue:s' => \$queue_dir, |
117 |
|
'debug!' => \$debug, |
118 |
); |
); |
119 |
|
|
120 |
#$SIG{__DIE__} = sub { |
#$SIG{__DIE__} = sub { |
668 |
|
|
669 |
my $_stat; |
my $_stat; |
670 |
|
|
671 |
|
POE::Component::Client::HTTP->spawn( |
672 |
|
Alias => 'rss-fetch', |
673 |
|
Timeout => 30, |
674 |
|
); |
675 |
|
|
676 |
sub rss_fetch { |
sub rss_parse_xml { |
677 |
my ($args) = @_; |
my ($args) = @_; |
678 |
|
|
679 |
|
warn "## rss_parse_xml ",dump( @_ ) if $debug; |
680 |
|
|
681 |
# 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? |
682 |
my $send_rss_msgs = 1; |
my $send_rss_msgs = 1; |
683 |
|
|
684 |
_log "RSS fetch", $args->{url}; |
_log "RSS fetch", $args->{url}; |
685 |
|
|
686 |
my $feed = XML::Feed->parse(URI->new( $args->{url} )); |
my $feed = XML::Feed->parse( \$args->{xml} ); |
687 |
if ( ! $feed ) { |
if ( ! $feed ) { |
688 |
_log("can't fetch RSS ", $args->{url}); |
_log "can't fetch RSS ", $args->{url}, XML::Feed->errstr; |
689 |
return; |
return; |
690 |
} |
} |
691 |
|
|
695 |
for my $entry ($feed->entries) { |
for my $entry ($feed->entries) { |
696 |
$total++; |
$total++; |
697 |
|
|
698 |
|
my $seen_times = $_stat->{rss}->{seen}->{$args->{channel}}->{$feed->link}->{$entry->id}++; |
699 |
# seen allready? |
# seen allready? |
700 |
next if $_stat->{rss}->{seen}->{$args->{channel}}->{$feed->link}->{$entry->id}++ > 0; |
warn "## $seen_times ",$entry->id if $debug; |
701 |
|
next if $seen_times > 0; |
702 |
|
|
703 |
sub prefix { |
sub prefix { |
704 |
my ($txt,$var) = @_; |
my ($txt,$var) = @_; |
731 |
$msg .= prefix( ' ' , $tags ); |
$msg .= prefix( ' ' , $tags ); |
732 |
} |
} |
733 |
|
|
734 |
if ( $args->{kernel} && $send_rss_msgs ) { |
if ( $seen_times == 0 && $send_rss_msgs ) { |
735 |
$send_rss_msgs--; |
$send_rss_msgs--; |
736 |
if ( ! $args->{private} ) { |
if ( ! $args->{private} ) { |
737 |
# FIXME bug! should be save_message |
# FIXME bug! should be save_message |
741 |
my ( $type, $to ) = ( 'notice', $args->{channel} ); |
my ( $type, $to ) = ( 'notice', $args->{channel} ); |
742 |
( $type, $to ) = ( 'privmsg', $args->{nick} ) if $args->{private}; |
( $type, $to ) = ( 'privmsg', $args->{nick} ) if $args->{private}; |
743 |
|
|
744 |
_log(">> $type $to", $msg); |
_log("RSS generated $type to $to:", $msg); |
|
# $args->{kernel}->post( $irc => $type => $to, $msg ); |
|
745 |
# XXX enqueue message to send later |
# XXX enqueue message to send later |
746 |
sub enqueue_post { |
sub enqueue_post { |
747 |
my $post = dump( @_ ); |
my $post = dump( @_ ); |
772 |
where active is true |
where active is true |
773 |
}; |
}; |
774 |
# limit to newer feeds only if we are not sending messages out |
# limit to newer feeds only if we are not sending messages out |
775 |
$sql .= qq{ and last_update + delay < now() } if $kernel; |
$sql .= qq{ and last_update + delay < now() } if defined ( $_stat->{rss}->{fetch} ); |
776 |
my $sth = $dbh->prepare( $sql ); |
my $sth = $dbh->prepare( $sql ); |
777 |
$sth->execute(); |
$sth->execute(); |
778 |
warn "# ",$sth->rows," active RSS feeds\n"; |
warn "# ",$sth->rows," active RSS feeds\n"; |
779 |
my $count = 0; |
my $count = 0; |
780 |
while (my $row = $sth->fetchrow_hashref) { |
while (my $row = $sth->fetchrow_hashref) { |
781 |
$row->{kernel} = $kernel if $kernel; |
warn "## queued rss-fetch for ", $row->{url} if $debug; |
782 |
$count += rss_fetch( $row ); |
$_stat->{rss}->{fetch}->{ $row->{url} } = $row; |
783 |
|
$kernel->post( |
784 |
|
'rss-fetch', |
785 |
|
'request', |
786 |
|
'rss_response', |
787 |
|
HTTP::Request->new( GET => $row->{url} ), |
788 |
|
); |
789 |
} |
} |
790 |
return "OK, fetched $count posts from " . $sth->rows . " feeds"; |
return "OK, scheduled " . $sth->rows . " feeds for refresh"; |
791 |
} |
} |
792 |
|
|
793 |
|
|
805 |
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, ": $!"; |
806 |
# $kernel->post( $irc => $type => $to, $msg ); |
# $kernel->post( $irc => $type => $to, $msg ); |
807 |
my @data = eval $data; |
my @data = eval $data; |
808 |
_log ">> post from queue ", $irc, @data; |
_log "IRC post from queue:", @data; |
809 |
$kernel->post( $irc => @data ); |
$kernel->post( $irc => @data ); |
810 |
$job->finish; |
$job->finish; |
811 |
warn "## done queued job: ",dump( @data ) if $debug; |
warn "## done queued job: ",dump( @data ) if $debug; |
812 |
} |
} |
813 |
} |
} |
814 |
|
|
|
# seed rss seen cache so we won't send out all items on startup |
|
|
_log rss_fetch_all if ! $debug; |
|
|
|
|
815 |
POE::Session->create( inline_states => { |
POE::Session->create( inline_states => { |
816 |
_start => sub { |
_start => sub { |
817 |
$_[KERNEL]->post( $irc => register => 'all' ); |
$_[KERNEL]->post( $irc => register => 'all' ); |
822 |
my $poco_object = $sender->get_heap(); |
my $poco_object = $sender->get_heap(); |
823 |
_log "connected to",$poco_object->server_name(); |
_log "connected to",$poco_object->server_name(); |
824 |
$kernel->post( $sender => join => $_ ) for @channels; |
$kernel->post( $sender => join => $_ ) for @channels; |
825 |
|
# seen RSS cache |
826 |
|
_log rss_fetch_all( $kernel ); |
827 |
undef; |
undef; |
828 |
}, |
}, |
829 |
irc_255 => sub { # server is done blabbing |
# irc_255 => sub { # server is done blabbing |
830 |
$_[KERNEL]->post( $irc => join => $CHANNEL); |
# $_[KERNEL]->post( $irc => join => $CHANNEL); |
831 |
}, |
# }, |
832 |
irc_public => sub { |
irc_public => sub { |
833 |
my $kernel = $_[KERNEL]; |
my $kernel = $_[KERNEL]; |
834 |
my $nick = (split /!/, $_[ARG0])[0]; |
my $nick = (split /!/, $_[ARG0])[0]; |
1142 |
""; |
""; |
1143 |
0; # false for signals |
0; # false for signals |
1144 |
}, |
}, |
1145 |
|
rss_response => sub { |
1146 |
|
my ($request_packet, $response_packet) = @_[ARG0, ARG1]; |
1147 |
|
my $request_object = $request_packet->[0]; |
1148 |
|
my $response_object = $response_packet->[0]; |
1149 |
|
|
1150 |
|
my $row = delete( $_stat->{rss}->{fetch}->{ $request_object->uri } ); |
1151 |
|
if ( $row ) { |
1152 |
|
$row->{xml} = $response_object->content; |
1153 |
|
rss_parse_xml( $row ); |
1154 |
|
} else { |
1155 |
|
warn "## can't find rss->fetch for ", $request_object->uri; |
1156 |
|
} |
1157 |
|
}, |
1158 |
}, |
}, |
1159 |
); |
); |
1160 |
|
|
1433 |
<p>See <a href="/history">history</a> of all messages.</p> |
<p>See <a href="/history">history</a> of all messages.</p> |
1434 |
</body></html>}; |
</body></html>}; |
1435 |
|
|
1436 |
$response->content( $html ); |
$response->content( decode('utf-8',$html) ); |
1437 |
warn "<< ", $request->method, " ", $request->uri, " created ", length($html), " bytes\n"; |
warn "<< ", $request->method, " ", $request->uri, " created ", length($html), " bytes\n"; |
1438 |
return RC_OK; |
return RC_OK; |
1439 |
} |
} |