/[irc-logger]/trunk/bin/irc-logger.pl
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /trunk/bin/irc-logger.pl

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 118 by dpavlin, Wed Mar 12 18:21:03 2008 UTC revision 122 by dpavlin, Fri Mar 14 14:37:46 2008 UTC
# Line 2  Line 2 
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/;
# Line 18  use DateTime::Format::ISO8601; Line 18  use DateTime::Format::ISO8601;
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    
# Line 47  log all conversation on irc channel Line 49  log all conversation on irc channel
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',
# Line 54  my $irc_config = { Line 58  my $irc_config = {
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    
# Line 75  if ( $HOSTNAME =~ m/llin/ ) { Line 81  if ( $HOSTNAME =~ m/llin/ ) {
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    
# Line 106  my $log_path; Line 112  my $log_path;
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 {
# Line 118  sub _log { Line 126  sub _log {
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 = ('<'=>'&lt;', '>'=>'&gt;', '&'=>'&amp;', '"'=>'&quot;');  my %escape = ('<'=>'&lt;', '>'=>'&gt;', '&'=>'&amp;', '"'=>'&quot;');
# Line 650  if ($import_dircproxy) { Line 667  if ($import_dircproxy) {
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    
# Line 671  sub rss_fetch { Line 694  sub rss_fetch {
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) = @_;
# Line 705  sub rss_fetch { Line 730  sub rss_fetch {
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          }          }
# Line 738  sub rss_fetch_all { Line 771  sub rss_fetch_all {
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    
# Line 755  sub rss_check_updates { Line 794  sub rss_check_updates {
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' );
# Line 775  POE::Session->create( inline_states => { Line 821  POE::Session->create( inline_states => {
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];
# Line 829  POE::Session->create( inline_states => { Line 877  POE::Session->create( inline_states => {
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;
# Line 1073  POE::Session->create( inline_states => { Line 1122  POE::Session->create( inline_states => {
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 {
# Line 1092  POE::Session->create( inline_states => { Line 1141  POE::Session->create( inline_states => {
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    

Legend:
Removed from v.118  
changed lines
  Added in v.122

  ViewVC Help
Powered by ViewVC 1.1.26