/[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 108 by dpavlin, Sun Mar 9 20:13:46 2008 UTC revision 112 by dpavlin, Mon Mar 10 13:02:32 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 149  my $filter = { Line 160  my $filter = {
160          },          },
161  };  };
162    
163    # POE IRC
164    my $poe_irc = POE::Component::IRC->spawn( %$irc_config ) or
165            die "can't start ", dump( $irc_config ), ": $!";
166    
167    my $irc = $poe_irc->session_id();
168    _log "IRC session_id $irc";
169    
170  my $dbh = DBI->connect($DSN,"","", { RaiseError => 1, AutoCommit => 1 }) || die $DBI::errstr;  my $dbh = DBI->connect($DSN,"","", { RaiseError => 1, AutoCommit => 1 }) || die $DBI::errstr;
171  $dbh->do( qq{ set client_encoding = 'UTF-8' } );  $dbh->do( qq{ set client_encoding = 'UTF-8' } );
172    
# Line 688  sub rss_fetch { Line 706  sub rss_fetch {
706                          }                          }
707                          my ( $type, $to ) = ( 'notice', $args->{channel} );                          my ( $type, $to ) = ( 'notice', $args->{channel} );
708                          ( $type, $to ) = ( 'privmsg', $args->{nick} ) if $args->{private};                          ( $type, $to ) = ( 'privmsg', $args->{nick} ) if $args->{private};
709                          _log(">> $type $to |", $msg);                          _log(">> $type $to", $msg);
710                          $args->{kernel}->post( $IRC_ALIAS => $type => $to, $msg );                          $args->{kernel}->post( $irc => $type => $to, $msg );
711                          $updates++;                          $updates++;
712                  }                  }
713          }          }
# Line 739  sub rss_check_updates { Line 757  sub rss_check_updates {
757  # 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
758  _log rss_fetch_all;  _log rss_fetch_all;
759    
 #  
 # POE handing part  
 #  
   
 my $ping;                                               # ping stats  
   
 POE::Component::IRC->new($IRC_ALIAS);  
   
760  POE::Session->create( inline_states => {  POE::Session->create( inline_states => {
761          _start => sub {                _start => sub {      
762                  $_[KERNEL]->post($IRC_ALIAS => register => 'all');                  $_[KERNEL]->post( $irc => register => 'all' );
763                  $_[KERNEL]->post($IRC_ALIAS => connect => $CONNECT);                  $_[KERNEL]->post( $irc => connect => {} );
764      },      },
765            irc_001 => sub {
766                    my ($kernel,$sender) = @_[KERNEL,SENDER];
767                    my $poco_object = $sender->get_heap();
768                    _log "connected to",$poco_object->server_name();
769                    $kernel->post( $sender => join => $_ ) for @channels;
770                    undef;
771            },
772      irc_255 => sub {    # server is done blabbing      irc_255 => sub {    # server is done blabbing
773                  $_[KERNEL]->post($IRC_ALIAS => join => $CHANNEL);                  $_[KERNEL]->post( $irc => join => $CHANNEL);
                 $_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "IDENTIFY $NICK" );  
774      },      },
775      irc_public => sub {      irc_public => sub {
776                  my $kernel = $_[KERNEL];                  my $kernel = $_[KERNEL];
# Line 786  POE::Session->create( inline_states => { Line 802  POE::Session->create( inline_states => {
802      },      },
803          irc_ping => sub {          irc_ping => sub {
804                  _log( "pong ", $_[ARG0] );                  _log( "pong ", $_[ARG0] );
805                  $ping->{ $_[ARG0] }++;                  $_stat->{ping}->{ $_[ARG0] }++;
806                  rss_check_updates( $_[KERNEL] );                  rss_check_updates( $_[KERNEL] );
807          },          },
808          irc_invite => sub {          irc_invite => sub {
# Line 796  POE::Session->create( inline_states => { Line 812  POE::Session->create( inline_states => {
812    
813                  _log "invited to $channel by $nick";                  _log "invited to $channel by $nick";
814    
815                  $_[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..." );
816                  $_[KERNEL]->post($IRC_ALIAS => join => $channel);                  $_[KERNEL]->post( $irc => 'join' => $channel );
817    
818          },          },
819          irc_msg => sub {          irc_msg => sub {
# Line 815  POE::Session->create( inline_states => { Line 831  POE::Session->create( inline_states => {
831    
832                          $res = "usage: /msg $NICK comand | commands: stat - user/message stat | last - show backtrace | grep foobar - find foobar";                          $res = "usage: /msg $NICK comand | commands: stat - user/message stat | last - show backtrace | grep foobar - find foobar";
833    
834                  } elsif ($msg =~ m/^msg\s+(\S+)\s+(.*)$/i) {                  } elsif ($msg =~ m/^(privmsg|notice)\s+(\S+)\s+(.*)$/i) {
835    
836                          _log ">> /msg $1 $2";                          _log ">> /$1 $2 $3";
837                          $_[KERNEL]->post( $IRC_ALIAS => privmsg => $1, $2 );                          $_[KERNEL]->post( $irc => $1 => $2, $3 );
838                          $res = '';                          $res = '';
839    
840                  } elsif ($msg =~ m/^stat.*?\s*(\d*)/i) {                  } elsif ($msg =~ m/^stat.*?\s*(\d*)/i) {
# Line 848  POE::Session->create( inline_states => { Line 864  POE::Session->create( inline_states => {
864    
865                          foreach my $res (get_from_log( limit => $limit )) {                          foreach my $res (get_from_log( limit => $limit )) {
866                                  _log "last: $res";                                  _log "last: $res";
867                                  $_[KERNEL]->post( $IRC_ALIAS => privmsg => $nick, $res );                                  $_[KERNEL]->post( $irc => privmsg => $nick, $res );
868                          }                          }
869    
870                          $res = '';                          $res = '';
# Line 862  POE::Session->create( inline_states => { Line 878  POE::Session->create( inline_states => {
878                                          search => $what,                                          search => $what,
879                                  )) {                                  )) {
880                                  _log "search [$what]: $res";                                  _log "search [$what]: $res";
881                                  $_[KERNEL]->post( $IRC_ALIAS => privmsg => $nick, $res );                                  $_[KERNEL]->post( $irc => privmsg => $nick, $res );
882                          }                          }
883    
884                          $res = '';                          $res = '';
# Line 897  POE::Session->create( inline_states => { Line 913  POE::Session->create( inline_states => {
913                                  " : " . ( $stat->{vote}->{'-'} || 0 ) . " --" .                                  " : " . ( $stat->{vote}->{'-'} || 0 ) . " --" .
914                                  " from " . ( join(", ", @nicks) || 'nobody' );                                  " from " . ( join(", ", @nicks) || 'nobody' );
915    
916                          $_[KERNEL]->post( $IRC_ALIAS => notice => $nick, $res );                          $_[KERNEL]->post( $irc => notice => $nick, $res );
917    
918                  } elsif ($msg =~ m/^ping/) {                  } elsif ($msg =~ m/^ping/) {
919                          $res = "ping = " . dump( $ping );                          $res = "ping = " . dump( $_stat->{ping} );
920                  } elsif ($msg =~ m/^conf(?:ig)*\s*(last-size|twitter)*\s*(.*)/) {                  } elsif ($msg =~ m/^conf(?:ig)*\s*(last-size|twitter)*\s*(.*)/) {
921                          if ( ! defined( $1 ) ) {                          if ( ! defined( $1 ) ) {
922                                  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 941  POE::Session->create( inline_states => { Line 957  POE::Session->create( inline_states => {
957                          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 });
958                          $sth->execute;                          $sth->execute;
959                          while (my @row = $sth->fetchrow_array) {                          while (my @row = $sth->fetchrow_array) {
960                                  $_[KERNEL]->post( $IRC_ALIAS => privmsg => $nick, join(' | ',@row) );                                  $_[KERNEL]->post( $irc => privmsg => $nick, join(' | ',@row) );
961                          }                          }
962                          $res = '';                          $res = '';
963                  } 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 979  POE::Session->create( inline_states => { Line 995  POE::Session->create( inline_states => {
995    
996                  if ($res) {                  if ($res) {
997                          _log ">> [$nick] $res";                          _log ">> [$nick] $res";
998                          $_[KERNEL]->post( $IRC_ALIAS => privmsg => $nick, $res );                          $_[KERNEL]->post( $irc => privmsg => $nick, $res );
999                  }                  }
1000    
1001                  rss_check_updates( $_[KERNEL] );                  rss_check_updates( $_[KERNEL] );
# Line 995  POE::Session->create( inline_states => { Line 1011  POE::Session->create( inline_states => {
1011          },          },
1012          irc_477 => sub {          irc_477 => sub {
1013                  _log "<< irc_477: ",$_[ARG1];                  _log "<< irc_477: ",$_[ARG1];
1014                  $_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "register $NICK" );                  $_[KERNEL]->post( $irc => privmsg => 'nickserv', "register $NICK" );
1015          },          },
1016          irc_505 => sub {          irc_505 => sub {
1017                  _log "<< irc_505: ",$_[ARG1];                  _log "<< irc_505: ",$_[ARG1];
1018                  $_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "register $NICK" );                  $_[KERNEL]->post( $irc => privmsg => 'nickserv', "register $NICK" );
1019  #               $_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "set hide email on" );  #               $_[KERNEL]->post( $irc => privmsg => 'nickserv', "set hide email on" );
1020  #               $_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "set email dpavlin\@rot13.org" );  #               $_[KERNEL]->post( $irc => privmsg => 'nickserv', "set email dpavlin\@rot13.org" );
1021          },          },
1022          irc_registered => sub {          irc_registered => sub {
1023                  _log "## registrated $NICK, /msg nickserv IDENTIFY $NICK";                  _log "<< registered $NICK";
                 $_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "IDENTIFY $NICK" );  
1024          },          },
1025          irc_disconnected => sub {          irc_disconnected => sub {
1026                  _log "## disconnected.. sleeping for $sleep_on_error seconds and reconnecting again";                  _log "## disconnected.. sleeping for $sleep_on_error seconds and reconnecting again";
1027                  sleep($sleep_on_error);                  sleep($sleep_on_error);
1028                  $_[KERNEL]->post( $IRC_ALIAS => connect => $CONNECT);                  $_[KERNEL]->post( $irc => connect => {} );
1029          },          },
1030          irc_socketerr => sub {          irc_socketerr => sub {
1031                  _log "## socket error... sleeping for $sleep_on_error seconds and retry";                  _log "## socket error... sleeping for $sleep_on_error seconds and retry";
1032                  sleep($sleep_on_error);                  sleep($sleep_on_error);
1033                  $_[KERNEL]->post( $IRC_ALIAS => connect => $CONNECT);                  $_[KERNEL]->post( $irc => connect => {} );
1034          },          },
1035  #       irc_433 => sub {  #       irc_433 => sub {
1036  #               print "# irc_433: ",$_[ARG1], "\n";  #               print "# irc_433: ",$_[ARG1], "\n";
1037  #               warn "## indetify $NICK\n";  #               warn "## indetify $NICK\n";
1038  #               $_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "IDENTIFY $NICK" );  #               $_[KERNEL]->post( $irc => privmsg => 'nickserv', "IDENTIFY $NICK" );
1039  #       },  #       },
1040  #       irc_451 # please register  #       irc_451 # please register
1041            irc_notice => sub {
1042                    _log "<< notice",$_[ARG0];
1043                    if ( $_[ARG0] =~ m!/msg\s+NickServ\s+IDENTIFY!i ) {
1044                            $_[KERNEL]->post( $irc => privmsg => 'nickserv', "IDENTIFY $NICK" );
1045                    }
1046            },
1047          irc_snotice => sub {          irc_snotice => sub {
1048                  _log "<< snotice",$_[ARG0];                  _log "<< snotice",$_[ARG0];
1049                  if ( $_[ARG0] =~ m!/(QUOTE)\s+(PASS\s+\d+)!i ) {                  if ( $_[ARG0] =~ m!/(QUOTE)\s+(PASS\s+\d+)!i ) {
1050                          warn ">> $1 | $2\n";                          warn ">> $1 | $2\n";
1051                          $_[KERNEL]->post( $IRC_ALIAS => lc($1) => $2);                          $_[KERNEL]->post( $irc => lc($1) => $2);
1052                  }                  }
1053          },          },
1054      _child => sub {},      _child => sub {},

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

  ViewVC Help
Powered by ViewVC 1.1.26