/[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 41 by dpavlin, Tue Oct 24 12:51:49 2006 UTC revision 45 by dpavlin, Sat Feb 3 12:18:04 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 67  use POSIX qw/strftime/; Line 73  use POSIX qw/strftime/;
73  use HTML::CalendarMonthSimple;  use HTML::CalendarMonthSimple;
74  use Getopt::Long;  use Getopt::Long;
75  use DateTime;  use DateTime;
76    use Data::Dump qw/dump/;
77    
78  my $import_dircproxy;  my $import_dircproxy;
79    my $log_path;
80  GetOptions(  GetOptions(
81          'import-dircproxy:s' => \$import_dircproxy,          'import-dircproxy:s' => \$import_dircproxy,
82            'log:s' => \$log_path,
83  );  );
84    
85    open(STDOUT, '>', $log_path) || warn "can't redirect log to $log_path: $!";
86    
87    sub _log {
88            print strftime($TIMESTAMP,localtime()), ' ', join(" ",@_), $/;
89    }
90    
91  my $dbh = DBI->connect($DSN,"","", { RaiseError => 1, AutoCommit => 1 }) || die $DBI::errstr;  my $dbh = DBI->connect($DSN,"","", { RaiseError => 1, AutoCommit => 1 }) || die $DBI::errstr;
92    
93  eval {  eval {
# Line 128  my $tag_regex = '\b([\w-_]+)//'; Line 143  my $tag_regex = '\b([\w-_]+)//';
143                  }                  }
144          },          },
145          context => 5,          context => 5,
146            full_rows => 1,
147   );   );
148    
149  Order is important. Fields are first passed through C<filter> (if available) and  Order is important. Fields are first passed through C<filter> (if available) and
# Line 135  then throgh C<< sprintf($fmt->{message}, Line 151  then throgh C<< sprintf($fmt->{message},
151    
152  C<context> defines number of messages around each search hit for display.  C<context> defines number of messages around each search hit for display.
153    
154    C<full_rows> will return database rows for each result with C<date>, C<time>, C<channel>,
155    C<me>, C<nick> and C<message> keys.
156    
157  =cut  =cut
158    
159  sub get_from_log {  sub get_from_log {
# Line 181  sub get_from_log { Line 200  sub get_from_log {
200                  $search =~ s/^\s+//;                  $search =~ s/^\s+//;
201                  $search =~ s/\s+$//;                  $search =~ s/\s+$//;
202                  $sth->execute( ( '%' . $search . '%' ) x 2 );                  $sth->execute( ( '%' . $search . '%' ) x 2 );
203                  warn "search for '$search' returned ", $sth->rows, " results ", $context || '', "\n";                  _log "search for '$search' returned ", $sth->rows, " results ", $context || '';
204          } elsif (my $tag = $args->{tag}) {          } elsif (my $tag = $args->{tag}) {
205                  $sth->execute();                  $sth->execute();
206                  warn "tag '$tag' returned ", $sth->rows, " results ", $context || '', "\n";                  _log "tag '$tag' returned ", $sth->rows, " results ", $context || '';
207          } elsif (my $date = $args->{date}) {          } elsif (my $date = $args->{date}) {
208                  $sth->execute($date);                  $sth->execute($date);
209                  warn "found ", $sth->rows, " messages for date $date ", $context || '', "\n";                  _log "found ", $sth->rows, " messages for date $date ", $context || '';
210          } else {          } else {
211                  $sth->execute();                  $sth->execute();
212          }          }
# Line 204  sub get_from_log { Line 223  sub get_from_log {
223                  unshift @rows, $row;                  unshift @rows, $row;
224          }          }
225    
226            # normalize nick names
227            map {
228                    $_->{nick} =~ s/^_*(.*?)_*$/$1/
229            } @rows;
230    
231            return @rows if ($args->{full_rows});
232    
233          my @msgs = (          my @msgs = (
234                  "Showing " . ($#rows + 1) . " messages..."                  "Showing " . ($#rows + 1) . " messages..."
235          );          );
# Line 260  sub get_from_log { Line 286  sub get_from_log {
286                  my $append = 1;                  my $append = 1;
287    
288                  my $nick = $row->{nick};                  my $nick = $row->{nick};
289                  if ($nick =~ s/^_*(.*?)_*$/$1/) {  #               if ($nick =~ s/^_*(.*?)_*$/$1/) {
290                          $row->{nick} = $nick;  #                       $row->{nick} = $nick;
291                  }  #               }
292    
293                  if ($last_row->{nick} ne $nick) {                  if ($last_row->{nick} ne $nick) {
294                          # 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
# Line 367  sub save_message { Line 393  sub save_message {
393          $a->{me} ||= 0;          $a->{me} ||= 0;
394          $a->{time} ||= strftime($TIMESTAMP,localtime());          $a->{time} ||= strftime($TIMESTAMP,localtime());
395    
396          print          _log
                 $a->{time}, " ",  
397                  $a->{channel}, " ",                  $a->{channel}, " ",
398                  $a->{me} ? "***" . $a->{nick} : "<" . $a->{nick} . ">",                  $a->{me} ? "***" . $a->{nick} : "<" . $a->{nick} . ">",
399                  " " . $a->{msg} . "\n";                  " " . $a->{msg};
400    
401          from_to($a->{msg}, 'UTF-8', $ENCODING);          from_to($a->{msg}, 'UTF-8', $ENCODING);
402    
# Line 406  if ($import_dircproxy) { Line 431  if ($import_dircproxy) {
431                          ) if ($nick !~ m/^-/);                          ) if ($nick !~ m/^-/);
432    
433                  } else {                  } else {
434                          warn "can't parse: $_\n";                          _log "can't parse: $_";
435                  }                  }
436          }          }
437          close($l);          close($l);
# Line 421  if ($import_dircproxy) { Line 446  if ($import_dircproxy) {
446    
447  my $SKIPPING = 0;               # if skipping, how many we've done  my $SKIPPING = 0;               # if skipping, how many we've done
448  my $SEND_QUEUE;                 # cache  my $SEND_QUEUE;                 # cache
449    my $ping;                                               # ping stats
450    
451  POE::Component::IRC->new($IRC_ALIAS);  POE::Component::IRC->new($IRC_ALIAS);
452    
# Line 452  POE::Session->create( inline_states => Line 478  POE::Session->create( inline_states =>
478    
479                  save_message( channel => $channel, me => 1, nick => $nick, msg => $msg);                  save_message( channel => $channel, me => 1, nick => $nick, msg => $msg);
480      },      },
481            irc_ping => sub {
482                    warn "pong ", $_[ARG0], $/;
483                    $ping->{$_[ARG0]++};
484            },
485            irc_invite => sub {
486                    my $kernel = $_[KERNEL];
487                    my $nick = (split /!/, $_[ARG0])[0];
488                    my $channel = $_[ARG1];
489                    
490    
491                    warn "invited to $channel by $nick";
492    
493                    $_[KERNEL]->post( $IRC_ALIAS => privmsg => $nick, "how nice of you to invite me to $channel, I'll be right there..." );
494                    $_[KERNEL]->post($IRC_ALIAS => join => $channel);
495    
496            },
497          irc_msg => sub {          irc_msg => sub {
498                  my $kernel = $_[KERNEL];                  my $kernel = $_[KERNEL];
499                  my $nick = (split /!/, $_[ARG0])[0];                  my $nick = (split /!/, $_[ARG0])[0];
# Line 461  POE::Session->create( inline_states => Line 503  POE::Session->create( inline_states =>
503                  my $res = "unknown command '$msg', try /msg $NICK help!";                  my $res = "unknown command '$msg', try /msg $NICK help!";
504                  my @out;                  my @out;
505    
506                  print "<< $msg\n";                  _log "<< $msg";
507    
508                  if ($msg =~ m/^help/i) {                  if ($msg =~ m/^help/i) {
509    
# Line 469  POE::Session->create( inline_states => Line 511  POE::Session->create( inline_states =>
511    
512                  } elsif ($msg =~ m/^msg\s+(\S+)\s+(.*)$/i) {                  } elsif ($msg =~ m/^msg\s+(\S+)\s+(.*)$/i) {
513    
514                          print ">> /msg $1 $2\n";                          _log ">> /msg $1 $2";
515                          $_[KERNEL]->post( $IRC_ALIAS => privmsg => $1, $2 );                          $_[KERNEL]->post( $IRC_ALIAS => privmsg => $1, $2 );
516                          $res = '';                          $res = '';
517    
# Line 497  POE::Session->create( inline_states => Line 539  POE::Session->create( inline_states =>
539                  } elsif ($msg =~ m/^last.*?\s*(\d*)/i) {                  } elsif ($msg =~ m/^last.*?\s*(\d*)/i) {
540    
541                          foreach my $res (get_from_log( limit => ($1 || 100) )) {                          foreach my $res (get_from_log( limit => ($1 || 100) )) {
542                                  print "last: $res\n";                                  _log "last: $res";
543                                  from_to($res, $ENCODING, 'UTF-8');                                  from_to($res, $ENCODING, 'UTF-8');
544                                  $_[KERNEL]->post( $IRC_ALIAS => privmsg => $nick, $res );                                  $_[KERNEL]->post( $IRC_ALIAS => privmsg => $nick, $res );
545                          }                          }
# Line 512  POE::Session->create( inline_states => Line 554  POE::Session->create( inline_states =>
554                                          limit => 20,                                          limit => 20,
555                                          search => $what,                                          search => $what,
556                                  )) {                                  )) {
557                                  print "search [$what]: $res\n";                                  _log "search [$what]: $res";
558                                  from_to($res, $ENCODING, 'UTF-8');                                  from_to($res, $ENCODING, 'UTF-8');
559                                  $_[KERNEL]->post( $IRC_ALIAS => privmsg => $nick, $res );                                  $_[KERNEL]->post( $IRC_ALIAS => privmsg => $nick, $res );
560                          }                          }
561    
562                          $res = '';                          $res = '';
563    
564                    } elsif ($msg =~ m/^(?:count|poll)\s+(.*)(?:\s+(\d+))?\s*$/i) {
565    
566                            my ($what,$limit) = ($1,$2);
567                            $limit ||= 100;
568    
569                            my $stat;
570    
571                            foreach my $res (get_from_log(
572                                            limit => $limit,
573                                            search => $what,
574                                            full_rows => 1,
575                                    )) {
576                                    while ($res->{message} =~ s/\Q$what\E(\+|\-)//) {
577                                            $stat->{vote}->{$1}++;
578                                            $stat->{from}->{ $res->{nick} }++;
579                                    }
580                            }
581    
582                            my @nicks;
583                            foreach my $nick (sort { $stat->{from}->{$a} <=> $stat->{from}->{$b} } keys %{ $stat->{from} }) {
584                                    push @nicks, $nick . ( $stat->{from}->{$nick} == 1 ? '' :
585                                            "(" . $stat->{from}->{$nick} . ")"
586                                    );
587                            }
588    
589                            $res =
590                                    "$what ++ " . ( $stat->{vote}->{'+'} || 0 ) .
591                                    " : " . ( $stat->{vote}->{'-'} || 0 ) . " --" .
592                                    " from " . ( join(", ", @nicks) || 'nobody' );
593    
594                            $_[KERNEL]->post( $IRC_ALIAS => notice => $nick, $res );
595    
596                    } elsif ($msg =~ m/^ping/) {
597                            $res = "ping = " . dump( $ping );
598                  }                  }
599    
600                  if ($res) {                  if ($res) {
601                          print ">> [$nick] $res\n";                          _log ">> [$nick] $res";
602                          from_to($res, $ENCODING, 'UTF-8');                          from_to($res, $ENCODING, 'UTF-8');
603                          $_[KERNEL]->post( $IRC_ALIAS => privmsg => $nick, $res );                          $_[KERNEL]->post( $IRC_ALIAS => privmsg => $nick, $res );
604                  }                  }
605    
606          },          },
607          irc_477 => sub {          irc_477 => sub {
608                  print "# irc_477: ",$_[ARG1], "\n";                  _log "# irc_477: ",$_[ARG1];
609                  $_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "register $NICK" );                  $_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "register $NICK" );
610          },          },
611          irc_505 => sub {          irc_505 => sub {
612                  print "# irc_505: ",$_[ARG1], "\n";                  _log "# irc_505: ",$_[ARG1];
613                  $_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "register $NICK" );                  $_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "register $NICK" );
614  #               $_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "set hide email on" );  #               $_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "set hide email on" );
615  #               $_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "set email dpavlin\@rot13.org" );  #               $_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "set email dpavlin\@rot13.org" );
616          },          },
617          irc_registered => sub {          irc_registered => sub {
618                  warn "## indetify $NICK\n";                  _log "## registrated $NICK";
619                  $_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "IDENTIFY $NICK" );                  $_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "IDENTIFY $NICK" );
620          },          },
621          irc_disconnected => sub {          irc_disconnected => sub {
622                  warn "## disconnected, reconnecting again\n";                  _log "## disconnected, reconnecting again";
623                  $_[KERNEL]->post($IRC_ALIAS => connect => $CONNECT);                  $_[KERNEL]->post($IRC_ALIAS => connect => $CONNECT);
624          },          },
625          irc_socketerr => sub {          irc_socketerr => sub {
626                  warn "## socket error... sleeping for $sleep_on_error seconds and retry";                  _log "## socket error... sleeping for $sleep_on_error seconds and retry";
627                  sleep($sleep_on_error);                  sleep($sleep_on_error);
628                  $_[KERNEL]->post($IRC_ALIAS => connect => $CONNECT);                  $_[KERNEL]->post($IRC_ALIAS => connect => $CONNECT);
629          },          },
# Line 558  POE::Session->create( inline_states => Line 634  POE::Session->create( inline_states =>
634  #       },  #       },
635      _child => sub {},      _child => sub {},
636      _default => sub {      _default => sub {
637                  printf "%s #%s %s %s\n",                  _log sprintf "sID:%s %s %s",
638                          strftime($TIMESTAMP,localtime()), $_[SESSION]->ID, $_[ARG0],                          $_[SESSION]->ID, $_[ARG0],
639                          ref($_[ARG1]) eq "ARRAY"        ?       join(",", map { ref($_) eq "ARRAY" ? join(";", @{$_}) : $_ } @{ $_[ARG1] })     :                          ref($_[ARG1]) eq "ARRAY"        ?       join(",", map { ref($_) eq "ARRAY" ? join(";", @{$_}) : $_ } @{ $_[ARG1] })     :
640                          $_[ARG1]                                        ?       $_[ARG1]                                        :                          $_[ARG1]                                        ?       $_[ARG1]                                        :
641                          "";                          "";

Legend:
Removed from v.41  
changed lines
  Added in v.45

  ViewVC Help
Powered by ViewVC 1.1.26