/[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 103 by dpavlin, Sun Mar 9 00:26:49 2008 UTC revision 109 by dpavlin, Sun Mar 9 21:13:15 2008 UTC
# Line 47  log all conversation on irc channel Line 47  log all conversation on irc channel
47    
48  ## CONFIG  ## CONFIG
49    
50    my $irc_config = {
51            nick => 'irc-logger',
52            server => 'irc.freenode.net',
53            port => 6667,
54            ircname => 'Anna the bot: try /msg irc-logger help',
55    };
56    
57  my $HOSTNAME = `hostname -f`;  my $HOSTNAME = `hostname -f`;
58  chomp($HOSTNAME);  chomp($HOSTNAME);
59    
60  my $NICK = 'irc-logger';  
 $NICK .= '-dev' if ($HOSTNAME =~ m/llin/);  
 my $CONNECT = {  
         Server => 'irc.freenode.net',  
         Nick => $NICK,  
         Ircname => "try /msg $NICK help",  
 };  
61  my $CHANNEL = '#razmjenavjestina';  my $CHANNEL = '#razmjenavjestina';
 $CHANNEL = '#irc-logger' if ($HOSTNAME =~ m/llin/);  
 my $IRC_ALIAS = "log";  
62    
63  if ( $HOSTNAME =~ m/lugarin/ ) {  if ( $HOSTNAME =~ m/llin/ ) {
64          $CONNECT->{Server} = 'irc.carnet.hr';          $irc_config->{nick} = 'irc-logger-dev';
65    #       $irc_config = {
66    #               nick => 'irc-logger-dev',
67    #               server => 'localhost',
68    #               port => 6668,
69    #       };
70            $CHANNEL = '#irc-logger';
71    } elsif ( $HOSTNAME =~ m/lugarin/ ) {
72            $irc_config->{server} = 'irc.carnet.hr';
73          $CHANNEL = '#riss';          $CHANNEL = '#riss';
74  }  }
75    
76  warn dump( $HOSTNAME, $CONNECT );  my @channels = ( $CHANNEL );
77    
78    warn "# config = ", dump( $irc_config ), $/;
79    
80    my $NICK = $irc_config->{nick} or die "no nick?";
81    
82  my $DSN = 'DBI:Pg:dbname=' . $NICK;  my $DSN = 'DBI:Pg:dbname=' . $NICK;
83    
# Line 465  sub get_from_log { Line 476  sub get_from_log {
476  #                       $row->{nick} = $nick;  #                       $row->{nick} = $nick;
477  #               }  #               }
478    
479                    $append = 0 if $row->{me};
480    
481                  if ($last_row->{nick} ne $nick) {                  if ($last_row->{nick} ne $nick) {
482                          # obfu way to find format for me_nick if needed or fallback to default                          # obfu way to find format for me_nick if needed or fallback to default
483                          my $fmt = $row->{me} ? ( $args->{fmt}->{me_nick} || $args->{fmt}->{nick} ) : $args->{fmt}->{nick};                          my $fmt = $row->{me} ? ( $args->{fmt}->{me_nick} || $args->{fmt}->{nick} ) : $args->{fmt}->{nick};
# Line 628  if ($import_dircproxy) { Line 641  if ($import_dircproxy) {
641  # RSS follow  # RSS follow
642  #  #
643    
644  my $_rss;  my $_stat;
645    
646    
647  sub rss_fetch {  sub rss_fetch {
# Line 650  sub rss_fetch { Line 663  sub rss_fetch {
663                  $total++;                  $total++;
664    
665                  # seen allready?                  # seen allready?
666                  next if $_rss->{$args->{channel}}->{$feed->link}->{$entry->id}++ > 0;                  next if $_stat->{rss}->{seen}->{$args->{channel}}->{$feed->link}->{$entry->id}++ > 0;
667    
668                  sub prefix {                  sub prefix {
669                          my ($txt,$var) = @_;                          my ($txt,$var) = @_;
# Line 679  sub rss_fetch { Line 692  sub rss_fetch {
692    
693                  if ( $args->{kernel} && $send_rss_msgs ) {                  if ( $args->{kernel} && $send_rss_msgs ) {
694                          $send_rss_msgs--;                          $send_rss_msgs--;
695                          $sth_insert_log->execute( $args->{channel}, 1, $NICK, $msg, 'now()' );                          if ( ! $args->{private} ) {
696                                    # FIXME bug! should be save_message
697    #                               save_message( channel => $args->{channel}, me => 1, nick => $NICK, message => $msg );
698                                    $sth_insert_log->execute( $args->{channel}, 1, $NICK, $msg, 'now()' );
699                            }
700                          my ( $type, $to ) = ( 'notice', $args->{channel} );                          my ( $type, $to ) = ( 'notice', $args->{channel} );
701                          ( $type, $to ) = ( 'privmsg', $args->{nick} ) if $args->{private};                          ( $type, $to ) = ( 'privmsg', $args->{nick} ) if $args->{private};
702                          _log(">> $type $to |", $msg);                          _log(">> $type $to |", $msg);
703                          $args->{kernel}->post( $IRC_ALIAS => $type => $to, $msg );                          $args->{kernel}->post( irc => $type => $to, $msg );
704                          $updates++;                          $updates++;
705                  }                  }
706          }          }
# Line 721  sub rss_fetch_all { Line 738  sub rss_fetch_all {
738    
739  sub rss_check_updates {  sub rss_check_updates {
740          my $kernel = shift;          my $kernel = shift;
741          $_rss->{last_poll} ||= time();          $_stat->{rss}->{last_poll} ||= time();
742          my $dt = time() - $_rss->{last_poll};          my $dt = time() - $_stat->{rss}->{last_poll};
743          warn "## rss_check_updates $dt > $rss_min_delay\n";          warn "## rss_check_updates $dt > $rss_min_delay\n";
744          if ( $dt > $rss_min_delay ) {          if ( $dt > $rss_min_delay ) {
745                  $_rss->{last_poll} = time();                  $_stat->{rss}->{last_poll} = time();
746                  _log rss_fetch_all( $kernel );                  _log rss_fetch_all( $kernel );
747          }          }
748  }  }
# Line 737  _log rss_fetch_all; Line 754  _log rss_fetch_all;
754  # POE handing part  # POE handing part
755  #  #
756    
757  my $ping;                                               # ping stats  my $poe_irc = POE::Component::IRC->spawn( %$irc_config ) or
758            die "can't start ", dump( $irc_config ), ": $!";
759    
760  POE::Component::IRC->new($IRC_ALIAS);  my $irc = $poe_irc->session_id();
761    _log "session_id $irc";
762    
763  POE::Session->create( inline_states => {  POE::Session->create( inline_states => {
764          _start => sub {                _start => sub {      
765                  $_[KERNEL]->post($IRC_ALIAS => register => 'all');                  $_[KERNEL]->post( $irc => register => 'all' );
766                  $_[KERNEL]->post($IRC_ALIAS => connect => $CONNECT);                  $_[KERNEL]->post( $irc => connect => {} );
767      },      },
768            irc_001 => sub {
769                    my ($kernel,$sender) = @_[KERNEL,SENDER];
770                    my $poco_object = $sender->get_heap();
771                    _log "connected to",$poco_object->server_name();
772                    $kernel->post( $sender => join => $_ ) for @channels;
773                    undef;
774            },
775      irc_255 => sub {    # server is done blabbing      irc_255 => sub {    # server is done blabbing
776                  $_[KERNEL]->post($IRC_ALIAS => join => $CHANNEL);                  $_[KERNEL]->post( $irc => join => $CHANNEL);
777                  $_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "IDENTIFY $NICK" );                  $_[KERNEL]->post( $irc => privmsg => 'nickserv', "IDENTIFY $NICK" );
778      },      },
779      irc_public => sub {      irc_public => sub {
780                  my $kernel = $_[KERNEL];                  my $kernel = $_[KERNEL];
# Line 780  POE::Session->create( inline_states => { Line 806  POE::Session->create( inline_states => {
806      },      },
807          irc_ping => sub {          irc_ping => sub {
808                  _log( "pong ", $_[ARG0] );                  _log( "pong ", $_[ARG0] );
809                  $ping->{ $_[ARG0] }++;                  $_stat->{ping}->{ $_[ARG0] }++;
810                  rss_check_updates( $_[KERNEL] );                  rss_check_updates( $_[KERNEL] );
811          },          },
812          irc_invite => sub {          irc_invite => sub {
# Line 790  POE::Session->create( inline_states => { Line 816  POE::Session->create( inline_states => {
816    
817                  _log "invited to $channel by $nick";                  _log "invited to $channel by $nick";
818    
819                  $_[KERNEL]->post( $IRC_ALIAS => privmsg => $nick, "how nice of you to invite me to $channel, I'll be right there..." );                  $_[KERNEL]->post( $irc => privmsg => $nick, "how nice of you to invite me to $channel, I'll be right there..." );
820                  $_[KERNEL]->post($IRC_ALIAS => join => $channel);                  $_[KERNEL]->post( $irc => 'join' => $channel );
821    
822          },          },
823          irc_msg => sub {          irc_msg => sub {
# Line 812  POE::Session->create( inline_states => { Line 838  POE::Session->create( inline_states => {
838                  } elsif ($msg =~ m/^msg\s+(\S+)\s+(.*)$/i) {                  } elsif ($msg =~ m/^msg\s+(\S+)\s+(.*)$/i) {
839    
840                          _log ">> /msg $1 $2";                          _log ">> /msg $1 $2";
841                          $_[KERNEL]->post( $IRC_ALIAS => privmsg => $1, $2 );                          $_[KERNEL]->post( $irc => privmsg => $1, $2 );
842                          $res = '';                          $res = '';
843    
844                  } elsif ($msg =~ m/^stat.*?\s*(\d*)/i) {                  } elsif ($msg =~ m/^stat.*?\s*(\d*)/i) {
# Line 842  POE::Session->create( inline_states => { Line 868  POE::Session->create( inline_states => {
868    
869                          foreach my $res (get_from_log( limit => $limit )) {                          foreach my $res (get_from_log( limit => $limit )) {
870                                  _log "last: $res";                                  _log "last: $res";
871                                  $_[KERNEL]->post( $IRC_ALIAS => privmsg => $nick, $res );                                  $_[KERNEL]->post( $irc => privmsg => $nick, $res );
872                          }                          }
873    
874                          $res = '';                          $res = '';
# Line 856  POE::Session->create( inline_states => { Line 882  POE::Session->create( inline_states => {
882                                          search => $what,                                          search => $what,
883                                  )) {                                  )) {
884                                  _log "search [$what]: $res";                                  _log "search [$what]: $res";
885                                  $_[KERNEL]->post( $IRC_ALIAS => privmsg => $nick, $res );                                  $_[KERNEL]->post( $irc => privmsg => $nick, $res );
886                          }                          }
887    
888                          $res = '';                          $res = '';
# Line 891  POE::Session->create( inline_states => { Line 917  POE::Session->create( inline_states => {
917                                  " : " . ( $stat->{vote}->{'-'} || 0 ) . " --" .                                  " : " . ( $stat->{vote}->{'-'} || 0 ) . " --" .
918                                  " from " . ( join(", ", @nicks) || 'nobody' );                                  " from " . ( join(", ", @nicks) || 'nobody' );
919    
920                          $_[KERNEL]->post( $IRC_ALIAS => notice => $nick, $res );                          $_[KERNEL]->post( $irc => notice => $nick, $res );
921    
922                  } elsif ($msg =~ m/^ping/) {                  } elsif ($msg =~ m/^ping/) {
923                          $res = "ping = " . dump( $ping );                          $res = "ping = " . dump( $_stat->{ping} );
924                  } elsif ($msg =~ m/^conf(?:ig)*\s*(last-size|twitter)*\s*(.*)/) {                  } elsif ($msg =~ m/^conf(?:ig)*\s*(last-size|twitter)*\s*(.*)/) {
925                          if ( ! defined( $1 ) ) {                          if ( ! defined( $1 ) ) {
926                                  my $sth = $dbh->prepare(qq{ select name,value,changed from meta where nick = ? and channel = ? });                                  my $sth = $dbh->prepare(qq{ select name,value,changed from meta where nick = ? and channel = ? });
# Line 928  POE::Session->create( inline_states => { Line 954  POE::Session->create( inline_states => {
954                  } elsif ($msg =~ m/^rss-update/) {                  } elsif ($msg =~ m/^rss-update/) {
955                          $res = rss_fetch_all( $_[KERNEL] );                          $res = rss_fetch_all( $_[KERNEL] );
956                  } elsif ($msg =~ m/^rss-clean/) {                  } elsif ($msg =~ m/^rss-clean/) {
957                          $_rss = undef;                          $_stat->{rss} = undef;
958                          $dbh->do( qq{ update feeds set last_update = now() - delay } );                          $dbh->do( qq{ update feeds set last_update = now() - delay } );
959                          $res = "OK, cleaned RSS cache";                          $res = "OK, cleaned RSS cache";
960                  } elsif ($msg =~ m/^rss-list/) {                  } elsif ($msg =~ m/^rss-list/) {
961                          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 });
962                          $sth->execute;                          $sth->execute;
963                          while (my @row = $sth->fetchrow_array) {                          while (my @row = $sth->fetchrow_array) {
964                                  $_[KERNEL]->post( $IRC_ALIAS => privmsg => $nick, join(' | ',@row) );                                  $_[KERNEL]->post( $irc => privmsg => $nick, join(' | ',@row) );
965                          }                          }
966                          $res = '';                          $res = '';
967                  } elsif ($msg =~ m!^rss-(add|remove|stop|start)(?:-(private))?\s+(http://\S+)\s*(.*)!) {                  } elsif ($msg =~ m!^rss-(add|remove|stop|start)(?:-(private))?\s+(http://\S+)\s*(.*)!) {
# Line 973  POE::Session->create( inline_states => { Line 999  POE::Session->create( inline_states => {
999    
1000                  if ($res) {                  if ($res) {
1001                          _log ">> [$nick] $res";                          _log ">> [$nick] $res";
1002                          $_[KERNEL]->post( $IRC_ALIAS => privmsg => $nick, $res );                          $_[KERNEL]->post( $irc => privmsg => $nick, $res );
1003                  }                  }
1004    
1005                  rss_check_updates( $_[KERNEL] );                  rss_check_updates( $_[KERNEL] );
1006          },          },
1007            irc_372 => sub {
1008                    _log "<< motd",$_[ARG0],$_[ARG1];
1009            },
1010            irc_375 => sub {
1011                    _log "<< motd", $_[ARG0], "start";
1012            },
1013            irc_376 => sub {
1014                    _log "<< motd", $_[ARG0], "end";
1015            },
1016          irc_477 => sub {          irc_477 => sub {
1017                  _log "<< irc_477: ",$_[ARG1];                  _log "<< irc_477: ",$_[ARG1];
1018                  $_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "register $NICK" );                  $_[KERNEL]->post( $irc => privmsg => 'nickserv', "register $NICK" );
1019          },          },
1020          irc_505 => sub {          irc_505 => sub {
1021                  _log "<< irc_505: ",$_[ARG1];                  _log "<< irc_505: ",$_[ARG1];
1022                  $_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "register $NICK" );                  $_[KERNEL]->post( $irc => privmsg => 'nickserv', "register $NICK" );
1023  #               $_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "set hide email on" );  #               $_[KERNEL]->post( $irc => privmsg => 'nickserv', "set hide email on" );
1024  #               $_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "set email dpavlin\@rot13.org" );  #               $_[KERNEL]->post( $irc => privmsg => 'nickserv', "set email dpavlin\@rot13.org" );
1025          },          },
1026          irc_registered => sub {          irc_registered => sub {
1027                  _log "## registrated $NICK, /msg nickserv IDENTIFY $NICK";                  _log "<< registered $NICK";
                 $_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "IDENTIFY $NICK" );  
1028          },          },
1029          irc_disconnected => sub {          irc_disconnected => sub {
1030                  _log "## disconnected.. sleeping for $sleep_on_error seconds and reconnecting again";                  _log "## disconnected.. sleeping for $sleep_on_error seconds and reconnecting again";
1031                  sleep($sleep_on_error);                  sleep($sleep_on_error);
1032                  $_[KERNEL]->post( $IRC_ALIAS => connect => $CONNECT);                  $_[KERNEL]->post( $irc => connect => {} );
1033          },          },
1034          irc_socketerr => sub {          irc_socketerr => sub {
1035                  _log "## socket error... sleeping for $sleep_on_error seconds and retry";                  _log "## socket error... sleeping for $sleep_on_error seconds and retry";
1036                  sleep($sleep_on_error);                  sleep($sleep_on_error);
1037                  $_[KERNEL]->post( $IRC_ALIAS => connect => $CONNECT);                  $_[KERNEL]->post( $irc => connect => {} );
1038          },          },
1039  #       irc_433 => sub {  #       irc_433 => sub {
1040  #               print "# irc_433: ",$_[ARG1], "\n";  #               print "# irc_433: ",$_[ARG1], "\n";
1041  #               warn "## indetify $NICK\n";  #               warn "## indetify $NICK\n";
1042  #               $_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "IDENTIFY $NICK" );  #               $_[KERNEL]->post( $irc => privmsg => 'nickserv', "IDENTIFY $NICK" );
1043  #       },  #       },
1044  #       irc_451 # please register  #       irc_451 # please register
1045            irc_notice => sub {
1046                    _log "<< notice",$_[ARG0];
1047                    if ( $_[ARG0] =~ m!/msg\s+NickServ\s+IDENTIFY!i ) {
1048                            $_[KERNEL]->post( $irc => privmsg => 'nickserv', "IDENTIFY $NICK" );
1049                    }
1050            },
1051          irc_snotice => sub {          irc_snotice => sub {
1052                  _log "<< snotice",$_[ARG0];                  _log "<< snotice",$_[ARG0];
1053                  if ( $_[ARG0] =~ m!/(QUOTE)\s+(PASS\s+\d+)!i ) {                  if ( $_[ARG0] =~ m!/(QUOTE)\s+(PASS\s+\d+)!i ) {
1054                          warn ">> $1 | $2\n";                          warn ">> $1 | $2\n";
1055                          $_[KERNEL]->post( $IRC_ALIAS => lc($1) => $2);                          $_[KERNEL]->post( $irc => lc($1) => $2);
1056                  }                  }
1057          },          },
1058      _child => sub {},      _child => sub {},
# Line 1097  sub root_handler { Line 1137  sub root_handler {
1137          }          }
1138    
1139          my $search = $q->param('search') || $q->param('grep') || '';          my $search = $q->param('search') || $q->param('grep') || '';
1140            my $r_url = $request->url;
1141    
1142            my @commands = qw( tags last-tag follow stat );
1143            my $commands_re = join('|',@commands);
1144    
1145          if ($request->url =~ m#/rss(?:/(tags|last-tag|follow.*)\w*(?:=(\d+))?)?#i) {          if ($r_url =~ m#/rss(?:/($commands_re.*)\w*(?:=(\d+))?)?#i) {
1146                  my $show = lc($1);                  my $show = lc($1);
1147                  my $nr = $2;                  my $nr = $2;
1148    
# Line 1112  sub root_handler { Line 1156  sub root_handler {
1156                  my $feed = XML::Feed->new( $type );                  my $feed = XML::Feed->new( $type );
1157                  $feed->link( $url );                  $feed->link( $url );
1158    
1159                    my $rc = RC_OK;
1160    
1161                  if ( $show eq 'tags' ) {                  if ( $show eq 'tags' ) {
1162                          $nr ||= 50;                          $nr ||= 50;
1163                          $feed->title( "tags from $CHANNEL" );                          $feed->title( "tags from $CHANNEL" );
# Line 1181  sub root_handler { Line 1227  sub root_handler {
1227                                  $feed->add_entry( $feed_entry );                                  $feed->add_entry( $feed_entry );
1228                          }                          }
1229    
1230                    } elsif ( $show =~ m/^stat/ ) {
1231    
1232                          my $feed_entry = XML::Feed::Entry->new($type);                          my $feed_entry = XML::Feed::Entry->new($type);
1233                          $feed_entry->title( "Internal stats" );                          $feed_entry->title( "Internal stats" );
1234                          $feed_entry->content(                          $feed_entry->content(
1235                                  '<![CDATA[<pre>' . dump( $_rss ) . '</pre>]]>'                                  '<![CDATA[<pre>' . dump( $_stat ) . '</pre>]]>'
1236                          );                          );
1237                          $feed->add_entry( $feed_entry );                          $feed->add_entry( $feed_entry );
1238    
1239                  } else {                  } else {
1240                          _log "unknown rss request ",$request->url;                          _log "unknown rss request $r_url";
1241                          return RC_DENY;                          $feed->title( "unknown $r_url" );
1242                            foreach my $c ( @commands ) {
1243                                    my $feed_entry = XML::Feed::Entry->new($type);
1244                                    $feed_entry->title( "rss/$c" );
1245                                    $feed_entry->link( "$url/rss/$c" );
1246                                    $feed->add_entry( $feed_entry );
1247                            }
1248                            $rc = RC_DENY;
1249                  }                  }
1250    
1251                  $response->content( $feed->as_xml );                  $response->content( $feed->as_xml );
1252                  return RC_OK;                  return $rc;
1253          }          }
1254    
1255          if ( $@ ) {          if ( $@ ) {

Legend:
Removed from v.103  
changed lines
  Added in v.109

  ViewVC Help
Powered by ViewVC 1.1.26