/[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

trunk/irc-logger.pl revision 43 by dpavlin, Fri Feb 2 22:27:36 2007 UTC trunk/bin/irc-logger.pl revision 58 by dpavlin, Sat Apr 7 21:08:34 2007 UTC
# Line 18  irc-logger.pl Line 18  irc-logger.pl
18    
19  Import log from C<dircproxy> to C<irc-logger> database  Import log from C<dircproxy> to C<irc-logger> database
20    
21    =item --log=irc-logger.log
22    
23    Name of log file
24    
25    =back
26    
27  =head1 DESCRIPTION  =head1 DESCRIPTION
28    
29  log all conversation on irc channel  log all conversation on irc channel
# Line 69  use Getopt::Long; Line 75  use Getopt::Long;
75  use DateTime;  use DateTime;
76  use Data::Dump qw/dump/;  use Data::Dump qw/dump/;
77    
78    my $use_twitter = 1;
79    eval { require Net::Twitter; };
80    $use_twitter = 0 if ($@);
81    
82  my $import_dircproxy;  my $import_dircproxy;
83    my $log_path;
84  GetOptions(  GetOptions(
85          'import-dircproxy:s' => \$import_dircproxy,          'import-dircproxy:s' => \$import_dircproxy,
86            'log:s' => \$log_path,
87  );  );
88    
89  my $dbh = DBI->connect($DSN,"","", { RaiseError => 1, AutoCommit => 1 }) || die $DBI::errstr;  open(STDOUT, '>', $log_path) || warn "can't redirect log to $log_path: $!";
90    
91  eval {  sub _log {
92          $dbh->do(qq{ select count(*) from log });          print strftime($TIMESTAMP,localtime()), ' ', join(" ",@_), $/;
93  };  }
94    
95  if ($@) {  my $dbh = DBI->connect($DSN,"","", { RaiseError => 1, AutoCommit => 1 }) || die $DBI::errstr;
         warn "creating database table in $DSN\n";  
         $dbh->do(<<'_SQL_SCHEMA_');  
96    
97    my $sql_schema = {
98            log => '
99  create table log (  create table log (
100          id serial,          id serial,
101          time timestamp default now(),          time timestamp default now(),
# Line 97  create table log ( Line 109  create table log (
109  create index log_time on log(time);  create index log_time on log(time);
110  create index log_channel on log(channel);  create index log_channel on log(channel);
111  create index log_nick on log(nick);  create index log_nick on log(nick);
112            ',
113            meta => '
114    create table meta (
115            nick text not null,
116            channel text not null,
117            name text not null,
118            value text,
119            changed timestamp default now(),
120            primary key(nick,channel,name)
121    );
122            ',
123    };
124    
125    foreach my $table ( keys %$sql_schema ) {
126    
127  _SQL_SCHEMA_          eval {
128                    $dbh->do(qq{ select count(*) from $table });
129            };
130    
131            if ($@) {
132                    warn "creating database table $table in $DSN\n";
133                    $dbh->do( $sql_schema->{ $table } );
134            }
135  }  }
136    
137    
138    =head2 meta
139    
140    Set or get some meta data into database
141    
142            meta('nick','channel','var_name', $var_value );
143    
144            $var_value = meta('nick','channel','var_name');
145            ( $var_value, $changed ) = meta('nick','channel','var_name');
146    
147    =cut
148    
149    sub meta {
150            my ($nick,$channel,$name,$value) = @_;
151    
152            # normalize channel name
153            $channel =~ s/^#//;
154    
155            if (defined($value)) {
156    
157                    my $sth = $dbh->prepare(qq{ update meta set value = ?, changed = now() where nick = ? and channel = ? and name = ? });
158    
159                    eval { $sth->execute( $value, $nick, $channel, $name ) };
160    
161                    # error or no result
162                    if ( $@ || ! $sth->rows ) {
163                            $sth = $dbh->prepare(qq{ insert into meta (value,nick,channel,name,changed) values (?,?,?,?,now()) });
164                            $sth->execute( $value, $nick, $channel, $name );
165                            _log "created $nick/$channel/$name = $value";
166                    } else {
167                            _log "updated $nick/$channel/$name = $value ";
168                    }
169    
170                    return $value;
171    
172            } else {
173    
174                    my $sth = $dbh->prepare(qq{ select value,changed from meta where nick = ? and channel = ? and name = ? });
175                    $sth->execute( $nick, $channel, $name );
176                    my ($v,$c) = $sth->fetchrow_array;
177                    _log "fetched $nick/$channel/$name = $v [$c]";
178                    return ($v,$c) if wantarray;
179                    return $v;
180    
181            }
182    }
183    
184    
185    
186  my $sth = $dbh->prepare(qq{  my $sth = $dbh->prepare(qq{
187  insert into log  insert into log
188          (channel, me, nick, message, time)          (channel, me, nick, message, time)
189  values (?,?,?,?,?)  values (?,?,?,?,?)
190  });  });
191    
192    
193  my $tags;  my $tags;
194  my $tag_regex = '\b([\w-_]+)//';  my $tag_regex = '\b([\w-_]+)//';
195    
# Line 186  sub get_from_log { Line 269  sub get_from_log {
269                  $search =~ s/^\s+//;                  $search =~ s/^\s+//;
270                  $search =~ s/\s+$//;                  $search =~ s/\s+$//;
271                  $sth->execute( ( '%' . $search . '%' ) x 2 );                  $sth->execute( ( '%' . $search . '%' ) x 2 );
272                  warn "search for '$search' returned ", $sth->rows, " results ", $context || '', "\n";                  _log "search for '$search' returned ", $sth->rows, " results ", $context || '';
273          } elsif (my $tag = $args->{tag}) {          } elsif (my $tag = $args->{tag}) {
274                  $sth->execute();                  $sth->execute();
275                  warn "tag '$tag' returned ", $sth->rows, " results ", $context || '', "\n";                  _log "tag '$tag' returned ", $sth->rows, " results ", $context || '';
276          } elsif (my $date = $args->{date}) {          } elsif (my $date = $args->{date}) {
277                  $sth->execute($date);                  $sth->execute($date);
278                  warn "found ", $sth->rows, " messages for date $date ", $context || '', "\n";                  _log "found ", $sth->rows, " messages for date $date ", $context || '';
279          } else {          } else {
280                  $sth->execute();                  $sth->execute();
281          }          }
# Line 379  sub save_message { Line 462  sub save_message {
462          $a->{me} ||= 0;          $a->{me} ||= 0;
463          $a->{time} ||= strftime($TIMESTAMP,localtime());          $a->{time} ||= strftime($TIMESTAMP,localtime());
464    
465          print          _log
                 $a->{time}, " ",  
466                  $a->{channel}, " ",                  $a->{channel}, " ",
467                  $a->{me} ? "***" . $a->{nick} : "<" . $a->{nick} . ">",                  $a->{me} ? "***" . $a->{nick} : "<" . $a->{nick} . ">",
468                  " " . $a->{msg} . "\n";                  " " . $a->{msg};
469    
470          from_to($a->{msg}, 'UTF-8', $ENCODING);          from_to($a->{msg}, 'UTF-8', $ENCODING);
471    
# Line 392  sub save_message { Line 474  sub save_message {
474                  message => $a->{msg});                  message => $a->{msg});
475  }  }
476    
477    
478  if ($import_dircproxy) {  if ($import_dircproxy) {
479          open(my $l, $import_dircproxy) || die "can't open $import_dircproxy: $!";          open(my $l, $import_dircproxy) || die "can't open $import_dircproxy: $!";
480          warn "importing $import_dircproxy...\n";          warn "importing $import_dircproxy...\n";
# Line 418  if ($import_dircproxy) { Line 501  if ($import_dircproxy) {
501                          ) if ($nick !~ m/^-/);                          ) if ($nick !~ m/^-/);
502    
503                  } else {                  } else {
504                          warn "can't parse: $_\n";                          _log "can't parse: $_";
505                  }                  }
506          }          }
507          close($l);          close($l);
# Line 456  POE::Session->create( inline_states => Line 539  POE::Session->create( inline_states =>
539                  my $msg = $_[ARG2];                  my $msg = $_[ARG2];
540    
541                  save_message( channel => $channel, me => 0, nick => $nick, msg => $msg);                  save_message( channel => $channel, me => 0, nick => $nick, msg => $msg);
542                    meta( $nick, $channel, 'last-msg', $msg );
543      },      },
544      irc_ctcp_action => sub {      irc_ctcp_action => sub {
545                  my $kernel = $_[KERNEL];                  my $kernel = $_[KERNEL];
# Line 464  POE::Session->create( inline_states => Line 548  POE::Session->create( inline_states =>
548                  my $msg = $_[ARG2];                  my $msg = $_[ARG2];
549    
550                  save_message( channel => $channel, me => 1, nick => $nick, msg => $msg);                  save_message( channel => $channel, me => 1, nick => $nick, msg => $msg);
551    
552                    if ( $use_twitter ) {
553                            if ( my $twitter = meta( $nick, $NICK, 'twitter' ) ) {
554                                    my ($login,$passwd) = split(/\s+/,$twitter,2);
555                                    _log("sending twitter for $nick/$login on $channel ");
556                                    my $bot = Net::Twitter->new( username=>$login, password=>$passwd );
557                                    $bot->update("<${channel}> $msg");
558                            }
559                    }
560    
561      },      },
562          irc_ping => sub {          irc_ping => sub {
563                  warn "pong ", $_[ARG0], $/;                  warn "pong ", $_[ARG0], $/;
564                  $ping->{$_[ARG0]++};                  $ping->{ $_[ARG0] }++;
565          },          },
566          irc_invite => sub {          irc_invite => sub {
567                  my $kernel = $_[KERNEL];                  my $kernel = $_[KERNEL];
568                  my $nick = (split /!/, $_[ARG0])[0];                  my $nick = (split /!/, $_[ARG0])[0];
569                  my $channel = $_[ARG1];                  my $channel = $_[ARG1];
                   
570    
571                  warn "invited to $channel by $nick";                  warn "invited to $channel by $nick";
572    
# Line 485  POE::Session->create( inline_states => Line 578  POE::Session->create( inline_states =>
578                  my $kernel = $_[KERNEL];                  my $kernel = $_[KERNEL];
579                  my $nick = (split /!/, $_[ARG0])[0];                  my $nick = (split /!/, $_[ARG0])[0];
580                  my $msg = $_[ARG2];                  my $msg = $_[ARG2];
581                    my $channel = $_[ARG1]->[0];
582                  from_to($msg, 'UTF-8', $ENCODING);                  from_to($msg, 'UTF-8', $ENCODING);
583    
584                  my $res = "unknown command '$msg', try /msg $NICK help!";                  my $res = "unknown command '$msg', try /msg $NICK help!";
585                  my @out;                  my @out;
586    
587                  print "<< $msg\n";                  _log "<< $msg";
588    
589                  if ($msg =~ m/^help/i) {                  if ($msg =~ m/^help/i) {
590    
# Line 498  POE::Session->create( inline_states => Line 592  POE::Session->create( inline_states =>
592    
593                  } elsif ($msg =~ m/^msg\s+(\S+)\s+(.*)$/i) {                  } elsif ($msg =~ m/^msg\s+(\S+)\s+(.*)$/i) {
594    
595                          print ">> /msg $1 $2\n";                          _log ">> /msg $1 $2";
596                          $_[KERNEL]->post( $IRC_ALIAS => privmsg => $1, $2 );                          $_[KERNEL]->post( $IRC_ALIAS => privmsg => $1, $2 );
597                          $res = '';                          $res = '';
598    
# Line 508  POE::Session->create( inline_states => Line 602  POE::Session->create( inline_states =>
602    
603                          my $sth = $dbh->prepare(qq{                          my $sth = $dbh->prepare(qq{
604                                  select                                  select
605                                          nick,                                          trim(both '_' from nick) as nick,
606                                          count(*) as count,                                          count(*) as count,
607                                          sum(length(message)) as len                                          sum(length(message)) as len
608                                  from log                                  from log
609                                  group by nick                                  group by trim(both '_' from nick)
610                                  order by len desc,count desc                                  order by len desc,count desc
611                                  limit $nr                                  limit $nr
612                          });                          });
# Line 525  POE::Session->create( inline_states => Line 619  POE::Session->create( inline_states =>
619                          $res .= join(" | ", @users);                          $res .= join(" | ", @users);
620                  } elsif ($msg =~ m/^last.*?\s*(\d*)/i) {                  } elsif ($msg =~ m/^last.*?\s*(\d*)/i) {
621    
622                          foreach my $res (get_from_log( limit => ($1 || 100) )) {                          my $limit = $1 || meta( $nick, $channel, 'last-size' ) || 10;
623                                  print "last: $res\n";  
624                            foreach my $res (get_from_log( limit => $limit )) {
625                                    _log "last: $res";
626                                  from_to($res, $ENCODING, 'UTF-8');                                  from_to($res, $ENCODING, 'UTF-8');
627                                  $_[KERNEL]->post( $IRC_ALIAS => privmsg => $nick, $res );                                  $_[KERNEL]->post( $IRC_ALIAS => privmsg => $nick, $res );
628                          }                          }
# Line 541  POE::Session->create( inline_states => Line 637  POE::Session->create( inline_states =>
637                                          limit => 20,                                          limit => 20,
638                                          search => $what,                                          search => $what,
639                                  )) {                                  )) {
640                                  print "search [$what]: $res\n";                                  _log "search [$what]: $res";
641                                  from_to($res, $ENCODING, 'UTF-8');                                  from_to($res, $ENCODING, 'UTF-8');
642                                  $_[KERNEL]->post( $IRC_ALIAS => privmsg => $nick, $res );                                  $_[KERNEL]->post( $IRC_ALIAS => privmsg => $nick, $res );
643                          }                          }
# Line 582  POE::Session->create( inline_states => Line 678  POE::Session->create( inline_states =>
678    
679                  } elsif ($msg =~ m/^ping/) {                  } elsif ($msg =~ m/^ping/) {
680                          $res = "ping = " . dump( $ping );                          $res = "ping = " . dump( $ping );
681                    } elsif ($msg =~ m/^conf(?:ig)*\s*(last-size|twitter)*\s*(.*)/) {
682                            if ( ! defined( $1 ) ) {
683                                    my $sth = $dbh->prepare(qq{ select name,value,changed from meta where nick = ? and channel = ? });
684                                    $sth->execute( $nick, $channel );
685                                    $res = "config for $nick on $channel";
686                                    while ( my ($n,$v) = $sth->fetchrow_array ) {
687                                            $res .= " | $n = $v";
688                                    }
689                            } elsif ( ! $2 ) {
690                                    my $val = meta( $nick, $channel, $1 );
691                                    $res = "current $1 = " . ( $val ? $val : 'undefined' );
692                            } else {
693                                    my $validate = {
694                                            'last-size' => qr/^\d+/,
695                                            'twitter' => qr/^\w+\s+\w+/,
696                                    };
697    
698                                    my ( $op, $val ) = ( $1, $2 );
699    
700                                    if ( my $regex = $validate->{$op} ) {
701                                            if ( $val =~ $regex ) {
702                                                    meta( $nick, $channel, $op, $val );
703                                                    $res = "saved $op = $val";
704                                            } else {
705                                                    $res = "config option $op = $val doesn't validate against $regex";
706                                            }
707                                    } else {
708                                            $res = "config option $op doesn't exist";
709                                    }
710                            }
711                  }                  }
712    
713                  if ($res) {                  if ($res) {
714                          print ">> [$nick] $res\n";                          _log ">> [$nick] $res";
715                          from_to($res, $ENCODING, 'UTF-8');                          from_to($res, $ENCODING, 'UTF-8');
716                          $_[KERNEL]->post( $IRC_ALIAS => privmsg => $nick, $res );                          $_[KERNEL]->post( $IRC_ALIAS => privmsg => $nick, $res );
717                  }                  }
718    
719          },          },
720          irc_477 => sub {          irc_477 => sub {
721                  print "# irc_477: ",$_[ARG1], "\n";                  _log "# irc_477: ",$_[ARG1];
722                  $_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "register $NICK" );                  $_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "register $NICK" );
723          },          },
724          irc_505 => sub {          irc_505 => sub {
725                  print "# irc_505: ",$_[ARG1], "\n";                  _log "# irc_505: ",$_[ARG1];
726                  $_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "register $NICK" );                  $_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "register $NICK" );
727  #               $_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "set hide email on" );  #               $_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "set hide email on" );
728  #               $_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "set email dpavlin\@rot13.org" );  #               $_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "set email dpavlin\@rot13.org" );
729          },          },
730          irc_registered => sub {          irc_registered => sub {
731                  warn "## indetify $NICK\n";                  _log "## registrated $NICK";
732                  $_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "IDENTIFY $NICK" );                  $_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "IDENTIFY $NICK" );
733          },          },
734          irc_disconnected => sub {          irc_disconnected => sub {
735                  warn "## disconnected, reconnecting again\n";                  _log "## disconnected, reconnecting again";
736                  $_[KERNEL]->post($IRC_ALIAS => connect => $CONNECT);                  $_[KERNEL]->post($IRC_ALIAS => connect => $CONNECT);
737          },          },
738          irc_socketerr => sub {          irc_socketerr => sub {
739                  warn "## socket error... sleeping for $sleep_on_error seconds and retry";                  _log "## socket error... sleeping for $sleep_on_error seconds and retry";
740                  sleep($sleep_on_error);                  sleep($sleep_on_error);
741                  $_[KERNEL]->post($IRC_ALIAS => connect => $CONNECT);                  $_[KERNEL]->post($IRC_ALIAS => connect => $CONNECT);
742          },          },
# Line 621  POE::Session->create( inline_states => Line 747  POE::Session->create( inline_states =>
747  #       },  #       },
748      _child => sub {},      _child => sub {},
749      _default => sub {      _default => sub {
750                  printf "%s #%s %s %s\n",                  _log sprintf "sID:%s %s %s",
751                          strftime($TIMESTAMP,localtime()), $_[SESSION]->ID, $_[ARG0],                          $_[SESSION]->ID, $_[ARG0],
752                          ref($_[ARG1]) eq "ARRAY"        ?       join(",", map { ref($_) eq "ARRAY" ? join(";", @{$_}) : $_ } @{ $_[ARG1] })     :                          ref($_[ARG1]) eq "ARRAY"        ?       join(",", map { ref($_) eq "ARRAY" ? join(";", @{$_}) : $_ } @{ $_[ARG1] })     :
753                          $_[ARG1]                                        ?       $_[ARG1]                                        :                          $_[ARG1]                                        ?       $_[ARG1]                                        :
754                          "";                          "";

Legend:
Removed from v.43  
changed lines
  Added in v.58

  ViewVC Help
Powered by ViewVC 1.1.26