/[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 116 by dpavlin, Wed Mar 12 17:21:07 2008 UTC revision 120 by dpavlin, Fri Mar 14 13:37:45 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 {
# Line 118  sub _log { Line 125  sub _log {
125    
126  open(STDOUT, '>', $log_path) && warn "log to $log_path: $!\n";  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 665  sub rss_fetch { Line 681  sub rss_fetch {
681                  return;                  return;
682          }          }
683    
684            $_stat->{rss}->{url2link}->{ $args->{url} } = $feed->link;
685    
686          my ( $total, $updates ) = ( 0, 0 );          my ( $total, $updates ) = ( 0, 0 );
687          for my $entry ($feed->entries) {          for my $entry ($feed->entries) {
688                  $total++;                  $total++;
689    
690                    my $seen_times = $_stat->{rss}->{seen}->{$args->{channel}}->{$feed->link}->{$entry->id}++;
691                  # seen allready?                  # seen allready?
692                  next if $_stat->{rss}->{seen}->{$args->{channel}}->{$feed->link}->{$entry->id}++ > 0;                  warn "## $seen_times ",$feed->link if $debug;
693                    next if $seen_times > 0;
694    
695                  sub prefix {                  sub prefix {
696                          my ($txt,$var) = @_;                          my ($txt,$var) = @_;
# Line 699  sub rss_fetch { Line 719  sub rss_fetch {
719                  if ( my $tags = $entry->category ) {                  if ( my $tags = $entry->category ) {
720                          $tags =~ s!^\s+!!;                          $tags =~ s!^\s+!!;
721                          $tags =~ s!\s*$! !;                          $tags =~ s!\s*$! !;
722                          $tags =~ s!\s+!// !g;                          $tags =~ s!,?\s+!// !g;
723                          $msg .= prefix( ' ' , $tags );                          $msg .= prefix( ' ' , $tags );
724                  }                  }
725    
726                  if ( $args->{kernel} && $send_rss_msgs ) {                  if ( $seen_times == 0 && $send_rss_msgs ) {
727                          $send_rss_msgs--;                          $send_rss_msgs--;
728                          if ( ! $args->{private} ) {                          if ( ! $args->{private} ) {
729                                  # FIXME bug! should be save_message                                  # FIXME bug! should be save_message
730  #                               save_message( channel => $args->{channel}, me => 1, nick => $NICK, message => $msg );                                  save_message( channel => $args->{channel}, me => 1, nick => $NICK, message => $msg );
731                                  $sth_insert_log->execute( $args->{channel}, 1, $NICK, $msg, 'now()' );  #                               $sth_insert_log->execute( $args->{channel}, 1, $NICK, $msg, 'now()' );
732                          }                          }
733                          my ( $type, $to ) = ( 'notice', $args->{channel} );                          my ( $type, $to ) = ( 'notice', $args->{channel} );
734                          ( $type, $to ) = ( 'privmsg', $args->{nick} ) if $args->{private};                          ( $type, $to ) = ( 'privmsg', $args->{nick} ) if $args->{private};
735    
736                          _log(">> $type $to", $msg);                          _log(">> $type $to", $msg);
737                          $args->{kernel}->post( $irc => $type => $to, $msg );  #                       $args->{kernel}->post( $irc => $type => $to, $msg );
738                            # XXX enqueue message to send later
739                            sub enqueue_post {
740                                    my $post = dump( @_ );
741                                    warn "## queue_post $post\n" if $debug;
742                                    $dq->enqueue_string( $post );
743                            }
744                            enqueue_post( $type => $to => $msg );
745    
746                          $updates++;                          $updates++;
747                  }                  }
748          }          }
# Line 753  sub rss_check_updates { Line 782  sub rss_check_updates {
782          my $kernel = shift;          my $kernel = shift;
783          $_stat->{rss}->{last_poll} ||= time();          $_stat->{rss}->{last_poll} ||= time();
784          my $dt = time() - $_stat->{rss}->{last_poll};          my $dt = time() - $_stat->{rss}->{last_poll};
         warn "## rss_check_updates $dt > $rss_min_delay\n";  
785          if ( $dt > $rss_min_delay ) {          if ( $dt > $rss_min_delay ) {
786                    warn "## rss_check_updates $dt > $rss_min_delay\n";
787                  $_stat->{rss}->{last_poll} = time();                  $_stat->{rss}->{last_poll} = time();
788                  _log rss_fetch_all( $kernel );                  _log rss_fetch_all( $kernel );
789          }          }
790            # XXX send queue messages
791            while ( my $job = $dq->pickup_queued_job() ) {
792                    my $data = read_file( $job->get_data_path ) || die "can't load ", $job->get_data_path, ": $!";
793    #               $kernel->post( $irc => $type => $to, $msg );
794                    my @data = eval $data;
795                    _log ">> post from queue ", $irc, @data;
796                    $kernel->post( $irc => @data );
797                    $job->finish;
798                    warn "## done queued job: ",dump( @data ) if $debug;
799            }
800  }  }
801    
802  # 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
803  _log rss_fetch_all;  _log rss_fetch_all if ! $debug;
804    
805  POE::Session->create( inline_states => {  POE::Session->create( inline_states => {
806          _start => sub {                _start => sub {      
# Line 827  POE::Session->create( inline_states => { Line 866  POE::Session->create( inline_states => {
866                  my $nick = (split /!/, $_[ARG0])[0];                  my $nick = (split /!/, $_[ARG0])[0];
867                  my $msg = $_[ARG2];                  my $msg = $_[ARG2];
868                  my $channel = $_[ARG1]->[0];                  my $channel = $_[ARG1]->[0];
869                    warn "# ARG = ",dump( @_[ARG0,ARG1,ARG2] ) if $debug;
870    
871                  my $res = "unknown command '$msg', try /msg $NICK help!";                  my $res = "unknown command '$msg', try /msg $NICK help!";
872                  my @out;                  my @out;
# Line 955  POE::Session->create( inline_states => { Line 995  POE::Session->create( inline_states => {
995                          }                          }
996                  } elsif ($msg =~ m/^rss-update/) {                  } elsif ($msg =~ m/^rss-update/) {
997                          $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";  
998                  } elsif ($msg =~ m/^rss-list/) {                  } elsif ($msg =~ m/^rss-list/) {
999                          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 });
1000                          $sth->execute;                          $sth->execute;
# Line 966  POE::Session->create( inline_states => { Line 1002  POE::Session->create( inline_states => {
1002                                  $_[KERNEL]->post( $irc => privmsg => $nick, join(' | ',@row) );                                  $_[KERNEL]->post( $irc => privmsg => $nick, join(' | ',@row) );
1003                          }                          }
1004                          $res = '';                          $res = '';
1005                  } 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*(.*)!) {
1006                          my ( $command, $sub, $url, $arg ) = ( $1,$2,$3,$4 );                          my ( $command, $sub, $url, $arg ) = ( $1,$2,$3,$4 );
1007    
1008                          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 1013  POE::Session->create( inline_states => {
1013  #                               remove  => qq{ delete from feeds                                where url = ? and name = ? },  #                               remove  => qq{ delete from feeds                                where url = ? and name = ? },
1014                                  start   => qq{ update feeds set active = true   where url = ? },                                  start   => qq{ update feeds set active = true   where url = ? },
1015                                  stop    => qq{ update feeds set active = false  where url = ? },                                  stop    => qq{ update feeds set active = false  where url = ? },
1016                                    clean   => qq{ update feeds set last_update = now() - delay where url = ? },
1017                          };                          };
1018    
1019                          if ( $command eq 'add' && ! $channel ) {                          if ( $command eq 'add' && ! $channel ) {
# Line 992  POE::Session->create( inline_states => { Line 1029  POE::Session->create( inline_states => {
1029                                  if ($@) {                                  if ($@) {
1030                                          $res = "ERROR: $@";                                          $res = "ERROR: $@";
1031                                  } else {                                  } else {
1032                                          $res = "OK, RSS [$command|$sub|$url|$arg]";                                          $res = "OK, RSS executed $command " . ( $sub ? "-$sub" : '' ) ."on $channel url $url";
1033                                            if ( $command eq 'clean' ) {
1034                                                    my $seen = $_stat->{rss}->{seen} || die "no seen?";
1035                                                    my $want_link = $_stat->{rss}->{url2link}->{$url} || warn "no url2link($url)";
1036                                                    foreach my $c ( keys %$seen ) {
1037                                                            my $c_hash = $seen->{$c} || die "no seen->{$c}";
1038                                                            die "not HASH with rss links but ", dump($c_hash) unless ref($c_hash) eq 'HASH';
1039                                                            foreach my $link ( keys %$c_hash ) {
1040                                                                    next unless $link eq $want_link;
1041                                                                    _log "RSS removed seen $c $url $link";
1042                                                            }
1043                                                    }
1044                                            }
1045                                  }                                  }
1046                          } else {                          } else {
1047                                  $res = "ERROR: don't know what to do with: $msg";                                  $res = "ERROR: don't know what to do with: $msg";
1048                          }                          }
1049                    } elsif ($msg =~ m/^rss-clean/) {
1050                            # this makes sense because we didn't catch rss-clean http://... before!
1051                            $_stat->{rss} = undef;
1052                            $dbh->do( qq{ update feeds set last_update = now() - delay } );
1053                            $res = "OK, cleaned RSS cache";
1054                  }                  }
1055    
1056                  if ($res) {                  if ($res) {
# Line 1057  POE::Session->create( inline_states => { Line 1111  POE::Session->create( inline_states => {
1111                          _log ">> registreted, so IDENTIFY";                          _log ">> registreted, so IDENTIFY";
1112                          $_[KERNEL]->post( $irc => privmsg => 'nickserv', "IDENTIFY $NICK" );                          $_[KERNEL]->post( $irc => privmsg => 'nickserv', "IDENTIFY $NICK" );
1113                  } else {                  } else {
1114                          warn "## ignore $m\n";                          warn "## ignore $m\n" if $debug;
1115                  }                  }
1116          },          },
1117          irc_snotice => sub {          irc_snotice => sub {

Legend:
Removed from v.116  
changed lines
  Added in v.120

  ViewVC Help
Powered by ViewVC 1.1.26