/[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 114 by dpavlin, Mon Mar 10 21:52:49 2008 UTC revision 119 by dpavlin, Fri Mar 14 00:17:49 2008 UTC
# 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 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  );  );
117    
118  #$SIG{__DIE__} = sub {  #$SIG{__DIE__} = sub {
119  #       confess "fatal error";  #       confess "fatal error";
120  #};  #};
121    
 open(STDOUT, '>', $log_path) || warn "can't redirect log to $log_path: $!";  
   
122  sub _log {  sub _log {
123          print strftime($TIMESTAMP,localtime()) . ' ' . join(" ",@_) . $/;          print strftime($TIMESTAMP,localtime()) . ' ' . join(" ",map { ref($_) ? dump( $_ ) : $_ } @_) . $/;
124    }
125    
126    open(STDOUT, '>', $log_path) && warn "log to $log_path: $!\n";
127    
128    # queue
129    
130    if ( ! -d $queue_dir ) {
131            warn "## creating queue directory $queue_dir";
132            mkdir $queue_dir or die "can't create queue directory $queue_dir: $!";
133  }  }
134    
135    my $dq = IPC::DirQueue->new({ dir => $queue_dir });
136    
137  # HTML formatters  # HTML formatters
138    
139  my %escape = ('<'=>'&lt;', '>'=>'&gt;', '&'=>'&amp;', '"'=>'&quot;');  my %escape = ('<'=>'&lt;', '>'=>'&gt;', '&'=>'&amp;', '"'=>'&quot;');
# Line 654  my $_stat; Line 670  my $_stat;
670  sub rss_fetch {  sub rss_fetch {
671          my ($args) = @_;          my ($args) = @_;
672    
673    
674          # 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?
675          my $send_rss_msgs = 1;          my $send_rss_msgs = 1;
676    
# Line 665  sub rss_fetch { Line 682  sub rss_fetch {
682                  return;                  return;
683          }          }
684    
685            $_stat->{rss}->{url2link}->{ $args->{url} } = $feed->link;
686    
687          my ( $total, $updates ) = ( 0, 0 );          my ( $total, $updates ) = ( 0, 0 );
688          for my $entry ($feed->entries) {          for my $entry ($feed->entries) {
689                  $total++;                  $total++;
# Line 699  sub rss_fetch { Line 718  sub rss_fetch {
718                  if ( my $tags = $entry->category ) {                  if ( my $tags = $entry->category ) {
719                          $tags =~ s!^\s+!!;                          $tags =~ s!^\s+!!;
720                          $tags =~ s!\s*$! !;                          $tags =~ s!\s*$! !;
721                          $tags =~ s!\s+!// !g;                          $tags =~ s!,?\s+!// !g;
722                          $msg .= prefix( ' ' , $tags );                          $msg .= prefix( ' ' , $tags );
723                  }                  }
724    
# Line 707  sub rss_fetch { Line 726  sub rss_fetch {
726                          $send_rss_msgs--;                          $send_rss_msgs--;
727                          if ( ! $args->{private} ) {                          if ( ! $args->{private} ) {
728                                  # FIXME bug! should be save_message                                  # FIXME bug! should be save_message
729  #                               save_message( channel => $args->{channel}, me => 1, nick => $NICK, message => $msg );                                  save_message( channel => $args->{channel}, me => 1, nick => $NICK, message => $msg );
730                                  $sth_insert_log->execute( $args->{channel}, 1, $NICK, $msg, 'now()' );  #                               $sth_insert_log->execute( $args->{channel}, 1, $NICK, $msg, 'now()' );
731                          }                          }
732                          my ( $type, $to ) = ( 'notice', $args->{channel} );                          my ( $type, $to ) = ( 'notice', $args->{channel} );
733                          ( $type, $to ) = ( 'privmsg', $args->{nick} ) if $args->{private};                          ( $type, $to ) = ( 'privmsg', $args->{nick} ) if $args->{private};
734    
735                          _log(">> $type $to", $msg);                          _log(">> $type $to", $msg);
736                          $args->{kernel}->post( $irc => $type => $to, $msg );  #                       $args->{kernel}->post( $irc => $type => $to, $msg );
737                            # XXX enqueue message to send later
738                            sub enqueue_post {
739                                    my $post = dump( @_ );
740                                    warn "## queue_post $post\n" if $debug;
741                                    $dq->enqueue_string( $post );
742                            }
743                            enqueue_post( $type => $to => $msg );
744    
745                          $updates++;                          $updates++;
746                  }                  }
747          }          }
# Line 753  sub rss_check_updates { Line 781  sub rss_check_updates {
781          my $kernel = shift;          my $kernel = shift;
782          $_stat->{rss}->{last_poll} ||= time();          $_stat->{rss}->{last_poll} ||= time();
783          my $dt = time() - $_stat->{rss}->{last_poll};          my $dt = time() - $_stat->{rss}->{last_poll};
         warn "## rss_check_updates $dt > $rss_min_delay\n";  
784          if ( $dt > $rss_min_delay ) {          if ( $dt > $rss_min_delay ) {
785                    warn "## rss_check_updates $dt > $rss_min_delay\n";
786                  $_stat->{rss}->{last_poll} = time();                  $_stat->{rss}->{last_poll} = time();
787                  _log rss_fetch_all( $kernel );                  _log rss_fetch_all( $kernel );
788          }          }
789            # XXX send queue messages
790            while ( my $job = $dq->pickup_queued_job() ) {
791                    my $data = read_file( $job->get_data_path ) || die "can't load ", $job->get_data_path, ": $!";
792    #               $kernel->post( $irc => $type => $to, $msg );
793                    my @data = eval $data;
794                    _log ">> post from queue ", $irc, @data;
795                    $kernel->post( $irc => @data );
796                    $job->finish;
797                    warn "## done queued job: ",dump( @data ) if $debug;
798            }
799  }  }
800    
801  # seed rss seen cache so we won't send out all items on startup  # seed rss seen cache so we won't send out all items on startup
802  _log rss_fetch_all;  _log rss_fetch_all if ! $debug;
803    
804  POE::Session->create( inline_states => {  POE::Session->create( inline_states => {
805          _start => sub {                _start => sub {      
# Line 827  POE::Session->create( inline_states => { Line 865  POE::Session->create( inline_states => {
865                  my $nick = (split /!/, $_[ARG0])[0];                  my $nick = (split /!/, $_[ARG0])[0];
866                  my $msg = $_[ARG2];                  my $msg = $_[ARG2];
867                  my $channel = $_[ARG1]->[0];                  my $channel = $_[ARG1]->[0];
868                    warn "# ARG = ",dump( @_[ARG0,ARG1,ARG2] ) if $debug;
869    
870                  my $res = "unknown command '$msg', try /msg $NICK help!";                  my $res = "unknown command '$msg', try /msg $NICK help!";
871                  my @out;                  my @out;
# Line 955  POE::Session->create( inline_states => { Line 994  POE::Session->create( inline_states => {
994                          }                          }
995                  } elsif ($msg =~ m/^rss-update/) {                  } elsif ($msg =~ m/^rss-update/) {
996                          $res = rss_fetch_all( $_[KERNEL] );                          $res = rss_fetch_all( $_[KERNEL] );
                 } elsif ($msg =~ m/^rss-clean/) {  
                         $_stat->{rss} = undef;  
                         $dbh->do( qq{ update feeds set last_update = now() - delay } );  
                         $res = "OK, cleaned RSS cache";  
997                  } elsif ($msg =~ m/^rss-list/) {                  } elsif ($msg =~ m/^rss-list/) {
998                          my $sth = $dbh->prepare(qq{ select url,name,last_update,active,channel,nick,private from feeds });                          my $sth = $dbh->prepare(qq{ select url,name,last_update,active,channel,nick,private from feeds });
999                          $sth->execute;                          $sth->execute;
# Line 966  POE::Session->create( inline_states => { Line 1001  POE::Session->create( inline_states => {
1001                                  $_[KERNEL]->post( $irc => privmsg => $nick, join(' | ',@row) );                                  $_[KERNEL]->post( $irc => privmsg => $nick, join(' | ',@row) );
1002                          }                          }
1003                          $res = '';                          $res = '';
1004                  } elsif ($msg =~ m!^rss-(add|remove|stop|start)(?:-(private))?\s+(http://\S+)\s*(.*)!) {                  } elsif ($msg =~ m!^rss-(add|remove|stop|start|clean)(?:-(private))?\s+(http://\S+)\s*(.*)!) {
1005                          my ( $command, $sub, $url, $arg ) = ( $1,$2,$3,$4 );                          my ( $command, $sub, $url, $arg ) = ( $1,$2,$3,$4 );
1006    
1007                          my $channel = $1 if ( $arg =~ s/\s*(#\S+)\s*// );                          my $channel = $1 if ( $arg =~ s/\s*(#\S+)\s*// );
# Line 977  POE::Session->create( inline_states => { Line 1012  POE::Session->create( inline_states => {
1012  #                               remove  => qq{ delete from feeds                                where url = ? and name = ? },  #                               remove  => qq{ delete from feeds                                where url = ? and name = ? },
1013                                  start   => qq{ update feeds set active = true   where url = ? },                                  start   => qq{ update feeds set active = true   where url = ? },
1014                                  stop    => qq{ update feeds set active = false  where url = ? },                                  stop    => qq{ update feeds set active = false  where url = ? },
1015                                    clean   => qq{ update feeds set last_update = now() - delay where url = ? },
1016                          };                          };
1017    
1018                          if ( $command eq 'add' && ! $channel ) {                          if ( $command eq 'add' && ! $channel ) {
# Line 992  POE::Session->create( inline_states => { Line 1028  POE::Session->create( inline_states => {
1028                                  if ($@) {                                  if ($@) {
1029                                          $res = "ERROR: $@";                                          $res = "ERROR: $@";
1030                                  } else {                                  } else {
1031                                          $res = "OK, RSS [$command|$sub|$url|$arg]";                                          $res = "OK, RSS executed $command " . ( $sub ? "-$sub" : '' ) ."on $channel url $url";
1032                                            if ( $command eq 'clean' ) {
1033                                                    my $seen = $_stat->{rss}->{seen} || die "no seen?";
1034                                                    my $want_link = $_stat->{rss}->{url2link}->{$url} || warn "no url2link($url)";
1035                                                    foreach my $c ( keys %$seen ) {
1036                                                            my $c_hash = $seen->{$c} || die "no seen->{$c}";
1037                                                            die "not HASH with rss links but ", dump($c_hash) unless ref($c_hash) eq 'HASH';
1038                                                            foreach my $link ( keys %$c_hash ) {
1039                                                                    next unless $link eq $want_link;
1040                                                                    _log "RSS removed seen $c $url $link";
1041                                                            }
1042                                                    }
1043                                            }
1044                                  }                                  }
1045                          } else {                          } else {
1046                                  $res = "ERROR: don't know what to do with: $msg";                                  $res = "ERROR: don't know what to do with: $msg";
1047                          }                          }
1048                    } elsif ($msg =~ m/^rss-clean/) {
1049                            # this makes sense because we didn't catch rss-clean http://... before!
1050                            $_stat->{rss} = undef;
1051                            $dbh->do( qq{ update feeds set last_update = now() - delay } );
1052                            $res = "OK, cleaned RSS cache";
1053                  }                  }
1054    
1055                  if ($res) {                  if ($res) {
# Line 1048  POE::Session->create( inline_states => { Line 1101  POE::Session->create( inline_states => {
1101                  $_[KERNEL]->post( $irc => connect => {} );                  $_[KERNEL]->post( $irc => connect => {} );
1102          },          },
1103          irc_notice => sub {          irc_notice => sub {
1104                  _log "<< notice",$_[ARG0],dump($_[ARG1]);                  _log "<< notice from ", $_[ARG0], $_[ARG1], $_[ARG2];
1105                  if ( $_[ARG0] =~ m!/msg\s+NickServ\s+IDENTIFY!i ) {                  my $m = $_[ARG2];
1106                          _log ">> IDENTIFY";                  if ( $m =~ m!/msg.*(NickServ).*(IDENTIFY)!i ) {
1107                            _log ">> suggested to $1 $2";
1108                            $_[KERNEL]->post( $irc => privmsg => $1, "$2 $NICK" );
1109                    } elsif ( $m =~ m!\Q$NICK\E.*registered!i ) {
1110                            _log ">> registreted, so IDENTIFY";
1111                          $_[KERNEL]->post( $irc => privmsg => 'nickserv', "IDENTIFY $NICK" );                          $_[KERNEL]->post( $irc => privmsg => 'nickserv', "IDENTIFY $NICK" );
1112                    } else {
1113                            warn "## ignore $m\n" if $debug;
1114                  }                  }
1115          },          },
1116          irc_snotice => sub {          irc_snotice => sub {
1117                  _log "<< snotice",$_[ARG0];                  _log "<< snotice", $_[ARG0]; #dump( $_[ARG0],$_[ARG1], $_[ARG2] );
1118                  if ( $_[ARG0] =~ m!/(QUOTE)\s+(PASS\s+\d+)!i ) {                  if ( $_[ARG0] =~ m!/(QUOTE)\s+(PASS\s+\d+)!i ) {
1119                          warn ">> $1 | $2\n";                          warn ">> $1 | $2\n";
1120                          $_[KERNEL]->post( $irc => lc($1) => $2);                          $_[KERNEL]->post( $irc => lc($1) => $2);

Legend:
Removed from v.114  
changed lines
  Added in v.119

  ViewVC Help
Powered by ViewVC 1.1.26