/[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 112 by dpavlin, Mon Mar 10 13:02:32 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 61  chomp($HOSTNAME); Line 67  chomp($HOSTNAME);
67  my $CHANNEL = '#razmjenavjestina';  my $CHANNEL = '#razmjenavjestina';
68    
69  if ( $HOSTNAME =~ m/llin/ ) {  if ( $HOSTNAME =~ m/llin/ ) {
70          $irc_config->{nick} = 'irc-logger-dev';          $irc_config->{nick} = 'irc-logger-llin';
71  #       $irc_config = {  #       $irc_config = {
72  #               nick => 'irc-logger-dev',  #               nick => 'irc-logger-llin',
73  #               server => 'localhost',  #               server => 'localhost',
74  #               port => 6668,  #               port => 6668,
75  #       };  #       };
# 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 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 696  sub rss_fetch { Line 716  sub rss_fetch {
716                  $msg .= prefix( ' | ' , $entry->title );                  $msg .= prefix( ' | ' , $entry->title );
717                  $msg .= prefix( ' | ' , $link );                  $msg .= prefix( ' | ' , $link );
718  #               $msg .= prefix( ' id ' , $entry->id );  #               $msg .= prefix( ' id ' , $entry->id );
719                    if ( my $tags = $entry->category ) {
720                            $tags =~ s!^\s+!!;
721                            $tags =~ s!\s*$! !;
722                            $tags =~ s!,?\s+!// !g;
723                            $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 747  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 821  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 949  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 960  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 971  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 986  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 1009  POE::Session->create( inline_states => { Line 1069  POE::Session->create( inline_states => {
1069          irc_376 => sub {          irc_376 => sub {
1070                  _log "<< motd", $_[ARG0], "end";                  _log "<< motd", $_[ARG0], "end";
1071          },          },
1072    #       irc_433 => sub {
1073    #               print "# irc_433: ",$_[ARG1], "\n";
1074    #               warn "## indetify $NICK\n";
1075    #               $_[KERNEL]->post( $irc => privmsg => 'nickserv', "IDENTIFY $NICK" );
1076    #       },
1077    #       irc_451 # please register
1078          irc_477 => sub {          irc_477 => sub {
1079                  _log "<< irc_477: ",$_[ARG1];                  _log "<< irc_477: ",$_[ARG1];
1080                  $_[KERNEL]->post( $irc => privmsg => 'nickserv', "register $NICK" );                  _log ">> IDENTIFY $NICK";
1081                    $_[KERNEL]->post( $irc => privmsg => 'NickServ', "IDENTIFY $NICK" );
1082          },          },
1083          irc_505 => sub {          irc_505 => sub {
1084                  _log "<< irc_505: ",$_[ARG1];                  _log "<< irc_505: ",$_[ARG1];
1085                  $_[KERNEL]->post( $irc => privmsg => 'nickserv', "register $NICK" );                  _log ">> register $NICK";
1086                    $_[KERNEL]->post( $irc => privmsg => 'NickServ', "register $NICK" );
1087    #               $_[KERNEL]->post( $irc => privmsg => 'nickserv', "IDENTIFY $NICK" );
1088  #               $_[KERNEL]->post( $irc => privmsg => 'nickserv', "set hide email on" );  #               $_[KERNEL]->post( $irc => privmsg => 'nickserv', "set hide email on" );
1089  #               $_[KERNEL]->post( $irc => privmsg => 'nickserv', "set email dpavlin\@rot13.org" );  #               $_[KERNEL]->post( $irc => privmsg => 'nickserv', "set email dpavlin\@rot13.org" );
1090          },          },
# Line 1032  POE::Session->create( inline_states => { Line 1101  POE::Session->create( inline_states => {
1101                  sleep($sleep_on_error);                  sleep($sleep_on_error);
1102                  $_[KERNEL]->post( $irc => connect => {} );                  $_[KERNEL]->post( $irc => connect => {} );
1103          },          },
 #       irc_433 => sub {  
 #               print "# irc_433: ",$_[ARG1], "\n";  
 #               warn "## indetify $NICK\n";  
 #               $_[KERNEL]->post( $irc => privmsg => 'nickserv', "IDENTIFY $NICK" );  
 #       },  
 #       irc_451 # please register  
1104          irc_notice => sub {          irc_notice => sub {
1105                  _log "<< notice",$_[ARG0];                  _log "<< notice from ", $_[ARG0], $_[ARG1], $_[ARG2];
1106                  if ( $_[ARG0] =~ m!/msg\s+NickServ\s+IDENTIFY!i ) {                  my $m = $_[ARG2];
1107                    if ( $m =~ m!/msg.*(NickServ).*(IDENTIFY)!i ) {
1108                            _log ">> suggested to $1 $2";
1109                            $_[KERNEL]->post( $irc => privmsg => $1, "$2 $NICK" );
1110                    } elsif ( $m =~ m!\Q$NICK\E.*registered!i ) {
1111                            _log ">> registreted, so IDENTIFY";
1112                          $_[KERNEL]->post( $irc => privmsg => 'nickserv', "IDENTIFY $NICK" );                          $_[KERNEL]->post( $irc => privmsg => 'nickserv', "IDENTIFY $NICK" );
1113                    } else {
1114                            warn "## ignore $m\n" if $debug;
1115                  }                  }
1116          },          },
1117          irc_snotice => sub {          irc_snotice => sub {
1118                  _log "<< snotice",$_[ARG0];                  _log "<< snotice", $_[ARG0]; #dump( $_[ARG0],$_[ARG1], $_[ARG2] );
1119                  if ( $_[ARG0] =~ m!/(QUOTE)\s+(PASS\s+\d+)!i ) {                  if ( $_[ARG0] =~ m!/(QUOTE)\s+(PASS\s+\d+)!i ) {
1120                          warn ">> $1 | $2\n";                          warn ">> $1 | $2\n";
1121                          $_[KERNEL]->post( $irc => lc($1) => $2);                          $_[KERNEL]->post( $irc => lc($1) => $2);
# Line 1065  POE::Session->create( inline_states => { Line 1135  POE::Session->create( inline_states => {
1135    
1136  # http server  # http server
1137    
1138    _log "WEB archive at $url";
1139    
1140  my $httpd = POE::Component::Server::HTTP->new(  my $httpd = POE::Component::Server::HTTP->new(
1141          Port => $http_port,          Port => $http_port,
1142          PreHandler => {          PreHandler => {
# Line 1111  foreach my $c (@cols) { Line 1183  foreach my $c (@cols) {
1183          $style .= ".col-${max_color} { background: $c }\n";          $style .= ".col-${max_color} { background: $c }\n";
1184          $max_color++;          $max_color++;
1185  }  }
1186  warn "defined $max_color colors for users...\n";  _log "WEB defined $max_color colors for users...";
1187    
1188  sub root_handler {  sub root_handler {
1189          my ($request, $response) = @_;          my ($request, $response) = @_;
# Line 1233  sub root_handler { Line 1305  sub root_handler {
1305                          $feed->add_entry( $feed_entry );                          $feed->add_entry( $feed_entry );
1306    
1307                  } else {                  } else {
1308                          _log "unknown rss request $r_url";                          _log "WEB unknown rss request $r_url";
1309                          $feed->title( "unknown $r_url" );                          $feed->title( "unknown $r_url" );
1310                          foreach my $c ( @commands ) {                          foreach my $c ( @commands ) {
1311                                  my $feed_entry = XML::Feed::Entry->new($type);                                  my $feed_entry = XML::Feed::Entry->new($type);

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

  ViewVC Help
Powered by ViewVC 1.1.26