--- trunk/bin/irc-logger.pl 2008/03/14 13:49:28 121 +++ trunk/bin/irc-logger.pl 2008/03/14 14:37:46 122 @@ -2,7 +2,7 @@ use strict; $|++; -use POE qw(Component::IRC Component::Server::HTTP); +use POE qw(Component::IRC Component::Server::HTTP Component::Client::HTTP); use HTTP::Status; use DBI; use Regexp::Common qw /URI/; @@ -81,7 +81,7 @@ my @channels = ( $CHANNEL ); -warn "# config = ", dump( $irc_config ), $/; +warn "## config = ", dump( $irc_config ) if $debug; my $NICK = $irc_config->{nick} or die "no nick?"; @@ -113,6 +113,7 @@ 'import-dircproxy:s' => \$import_dircproxy, 'log:s' => \$log_path, 'queue:s' => \$queue_dir, + 'debug!' => \$debug, ); #$SIG{__DIE__} = sub { @@ -666,18 +667,24 @@ my $_stat; +POE::Component::Client::HTTP->spawn( + Alias => 'rss-fetch', + Timeout => 30, +); -sub rss_fetch { +sub rss_parse_xml { my ($args) = @_; + warn "## rss_parse_xml ",dump( @_ ) if $debug; + # how many messages to send out when feed is seen for the first time? my $send_rss_msgs = 1; _log "RSS fetch", $args->{url}; - my $feed = XML::Feed->parse(URI->new( $args->{url} )); + my $feed = XML::Feed->parse( \$args->{xml} ); if ( ! $feed ) { - _log("can't fetch RSS ", $args->{url}); + _log "can't fetch RSS ", $args->{url}, XML::Feed->errstr; return; } @@ -689,7 +696,7 @@ my $seen_times = $_stat->{rss}->{seen}->{$args->{channel}}->{$feed->link}->{$entry->id}++; # seen allready? - warn "## $seen_times ",$feed->link if $debug; + warn "## $seen_times ",$entry->id if $debug; next if $seen_times > 0; sub prefix { @@ -733,8 +740,7 @@ my ( $type, $to ) = ( 'notice', $args->{channel} ); ( $type, $to ) = ( 'privmsg', $args->{nick} ) if $args->{private}; - _log(">> $type $to", $msg); -# $args->{kernel}->post( $irc => $type => $to, $msg ); + _log("RSS generated $type to $to:", $msg); # XXX enqueue message to send later sub enqueue_post { my $post = dump( @_ ); @@ -765,16 +771,22 @@ where active is true }; # limit to newer feeds only if we are not sending messages out - $sql .= qq{ and last_update + delay < now() } if $kernel; + $sql .= qq{ and last_update + delay < now() } if defined ( $_stat->{rss}->{fetch} ); my $sth = $dbh->prepare( $sql ); $sth->execute(); warn "# ",$sth->rows," active RSS feeds\n"; my $count = 0; while (my $row = $sth->fetchrow_hashref) { - $row->{kernel} = $kernel if $kernel; - $count += rss_fetch( $row ); + warn "## queued rss-fetch for ", $row->{url} if $debug; + $_stat->{rss}->{fetch}->{ $row->{url} } = $row; + $kernel->post( + 'rss-fetch', + 'request', + 'rss_response', + HTTP::Request->new( GET => $row->{url} ), + ); } - return "OK, fetched $count posts from " . $sth->rows . " feeds"; + return "OK, scheduled " . $sth->rows . " feeds for refresh"; } @@ -792,16 +804,13 @@ my $data = read_file( $job->get_data_path ) || die "can't load ", $job->get_data_path, ": $!"; # $kernel->post( $irc => $type => $to, $msg ); my @data = eval $data; - _log ">> post from queue ", $irc, @data; + _log "IRC post from queue:", @data; $kernel->post( $irc => @data ); $job->finish; warn "## done queued job: ",dump( @data ) if $debug; } } -# seed rss seen cache so we won't send out all items on startup -_log rss_fetch_all if ! $debug; - POE::Session->create( inline_states => { _start => sub { $_[KERNEL]->post( $irc => register => 'all' ); @@ -812,11 +821,13 @@ my $poco_object = $sender->get_heap(); _log "connected to",$poco_object->server_name(); $kernel->post( $sender => join => $_ ) for @channels; + # seen RSS cache + _log rss_fetch_all( $kernel ); undef; }, - irc_255 => sub { # server is done blabbing - $_[KERNEL]->post( $irc => join => $CHANNEL); - }, +# irc_255 => sub { # server is done blabbing +# $_[KERNEL]->post( $irc => join => $CHANNEL); +# }, irc_public => sub { my $kernel = $_[KERNEL]; my $nick = (split /!/, $_[ARG0])[0]; @@ -1130,6 +1141,19 @@ ""; 0; # false for signals }, + rss_response => sub { + my ($request_packet, $response_packet) = @_[ARG0, ARG1]; + my $request_object = $request_packet->[0]; + my $response_object = $response_packet->[0]; + + my $row = delete( $_stat->{rss}->{fetch}->{ $request_object->uri } ); + if ( $row ) { + $row->{xml} = $response_object->content; + rss_parse_xml( $row ); + } else { + warn "## can't find rss->fetch for ", $request_object->uri; + } + }, }, );