/[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 50 by dpavlin, Sun Mar 18 15:37:05 2007 UTC revision 68 by dpavlin, Sat Sep 29 14:11:55 2007 UTC
# Line 73  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 URI::Escape;
77  use Data::Dump qw/dump/;  use Data::Dump qw/dump/;
78    use DateTime::Format::ISO8601;
79    use Carp qw/confess/;
80    
81    my $use_twitter = 1;
82    eval { require Net::Twitter; };
83    $use_twitter = 0 if ($@);
84    
85  my $import_dircproxy;  my $import_dircproxy;
86  my $log_path;  my $log_path;
# Line 82  GetOptions( Line 89  GetOptions(
89          'log:s' => \$log_path,          'log:s' => \$log_path,
90  );  );
91    
92    $SIG{__DIE__} = sub {
93            confess "fatal error";
94    };
95    
96  open(STDOUT, '>', $log_path) || warn "can't redirect log to $log_path: $!";  open(STDOUT, '>', $log_path) || warn "can't redirect log to $log_path: $!";
97    
98  sub _log {  sub _log {
# Line 224  C<me>, C<nick> and C<message> keys. Line 235  C<me>, C<nick> and C<message> keys.
235  sub get_from_log {  sub get_from_log {
236          my $args = {@_};          my $args = {@_};
237    
238          $args->{fmt} ||= {          if ( ! $args->{fmt} ) {
239                  date => '[%s] ',                  $args->{fmt} = {
240                  time => '{%s} ',                          date => '[%s] ',
241                  time_channel => '{%s %s} ',                          time => '{%s} ',
242                  nick => '%s: ',                          time_channel => '{%s %s} ',
243                  me_nick => '***%s ',                          nick => '%s: ',
244                  message => '%s',                          me_nick => '***%s ',
245          };                          message => '%s',
246                    };
247            }
248    
249          my $sql_message = qq{          my $sql_message = qq{
250                  select                  select
# Line 254  sub get_from_log { Line 267  sub get_from_log {
267    
268          my $sql = $context ? $sql_context : $sql_message;          my $sql = $context ? $sql_context : $sql_message;
269    
270          $sql .= " where message ilike ? or nick ilike ? " if ($args->{search});          sub check_date {
271          $sql .= " where id in (" . join(",", @{ $tags->{ $args->{tag} } }) . ") " if ($args->{tag} && $tags->{ $args->{tag} });                  my $date = shift || return;
272          $sql .= " where date(time) = ? " if ($args->{date});                  my $new_date = eval { DateTime::Format::ISO8601->parse_datetime( $date )->ymd; };
273          $sql .= " order by log.time desc";                  if ( $@ ) {
274          $sql .= " limit " . $args->{limit} if ($args->{limit});                          warn "invalid date $date\n";
275                            $new_date = DateTime->now->ymd;
276                    }
277                    return $new_date;
278            }
279    
280            my @where;
281            my @args;
282    
         my $sth = $dbh->prepare( $sql );  
283          if (my $search = $args->{search}) {          if (my $search = $args->{search}) {
284                  $search =~ s/^\s+//;                  $search =~ s/^\s+//;
285                  $search =~ s/\s+$//;                  $search =~ s/\s+$//;
286                  $sth->execute( ( '%' . $search . '%' ) x 2 );                  push @where, 'message ilike ? or nick ilike ?';
287                  _log "search for '$search' returned ", $sth->rows, " results ", $context || '';                  push @args, ( ( '%' . $search . '%' ) x 2 );
288          } elsif (my $tag = $args->{tag}) {                  _log "search for '$search'";
                 $sth->execute();  
                 _log "tag '$tag' returned ", $sth->rows, " results ", $context || '';  
         } elsif (my $date = $args->{date}) {  
                 $sth->execute($date);  
                 _log "found ", $sth->rows, " messages for date $date ", $context || '';  
         } else {  
                 $sth->execute();  
289          }          }
290    
291            if ($args->{tag} && $tags->{ $args->{tag} }) {
292                    push @where, 'id in (' . join(',', @{ $tags->{ $args->{tag} } }) . ')';
293                    _log "search for tags $args->{tag}";
294            }
295    
296            if (my $date = $args->{date} ) {
297                    $date = check_date( $date );
298                    push @where, 'date(time) = ?';
299                    push @args, $date;
300                    _log "search for date $date";
301            }
302    
303            $sql .= " where " . join(" and ", @where) if @where;
304    
305            $sql .= " order by log.time desc";
306            $sql .= " limit " . $args->{limit} if ($args->{limit});
307    
308            #warn "### sql: $sql ", dump( @args );
309    
310            my $sth = $dbh->prepare( $sql );
311            $sth->execute( @args );
312    
313          my $last_row = {          my $last_row = {
314                  date => '',                  date => '',
315                  time => '',                  time => '',
# Line 545  POE::Session->create( inline_states => Line 580  POE::Session->create( inline_states =>
580    
581                  save_message( channel => $channel, me => 1, nick => $nick, msg => $msg);                  save_message( channel => $channel, me => 1, nick => $nick, msg => $msg);
582    
583                  if ( my $twitter = ( $nick, $channel, 'twitter' ) ) {                  if ( $use_twitter ) {
584                          _log("FIXME: send twitter for $nick on $channel [$twitter]");                          if ( my $twitter = meta( $nick, $NICK, 'twitter' ) ) {
585                                    my ($login,$passwd) = split(/\s+/,$twitter,2);
586                                    _log("sending twitter for $nick/$login on $channel ");
587                                    my $bot = Net::Twitter->new( username=>$login, password=>$passwd );
588                                    $bot->update("<${channel}> $msg");
589                            }
590                  }                  }
591    
592      },      },
# Line 593  POE::Session->create( inline_states => Line 633  POE::Session->create( inline_states =>
633    
634                          my $sth = $dbh->prepare(qq{                          my $sth = $dbh->prepare(qq{
635                                  select                                  select
636                                          nick,                                          trim(both '_' from nick) as nick,
637                                          count(*) as count,                                          count(*) as count,
638                                          sum(length(message)) as len                                          sum(length(message)) as len
639                                  from log                                  from log
640                                  group by nick                                  group by trim(both '_' from nick)
641                                  order by len desc,count desc                                  order by len desc,count desc
642                                  limit $nr                                  limit $nr
643                          });                          });
# Line 669  POE::Session->create( inline_states => Line 709  POE::Session->create( inline_states =>
709    
710                  } elsif ($msg =~ m/^ping/) {                  } elsif ($msg =~ m/^ping/) {
711                          $res = "ping = " . dump( $ping );                          $res = "ping = " . dump( $ping );
712                  } elsif ($msg =~ m/^(?:twitter)\s+(\S+)\s+(.*?)/) {                  } elsif ($msg =~ m/^conf(?:ig)*\s*(last-size|twitter)*\s*(.*)/) {
                         if ( defined( $2 ) ) {  
                                 meta($nick, $channel, 'twitter', "$1\t$2");  
                                 $res = "saved twitter auth for $1 -- /me on $channel will auto-update twitter status";  
                         } else {  
                                 meta($nick, $channel, 'twitter', '' );  
                                 $res = "removed twitter status update for /me on $channel";  
                         }  
                 } elsif ($msg =~ m/^conf(?:ig)*\s*(last-size)*\s*(\d*)/) {  
713                          if ( ! defined( $1 ) ) {                          if ( ! defined( $1 ) ) {
714                                  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 = ? });
715                                  $sth->execute( $nick, $channel );                                  $sth->execute( $nick, $channel );
716                                  $res = "config for $nick ";                                  $res = "config for $nick on $channel";
717                                  while ( my ($n,$v) = $sth->fetchrow_array ) {                                  while ( my ($n,$v) = $sth->fetchrow_array ) {
718                                          $res .= "| $n = $v";                                          $res .= " | $n = $v";
719                                  }                                  }
720                          } elsif ( defined( $2 ) ) {                          } elsif ( ! $2 ) {
                                 meta( $nick, $channel, $1, $2 );  
                                 $res = "saved $1 = $2";  
                         } else {  
721                                  my $val = meta( $nick, $channel, $1 );                                  my $val = meta( $nick, $channel, $1 );
722                                  $res = "current $1 = " . ( $val ? $val : 'undefined' );                                  $res = "current $1 = " . ( $val ? $val : 'undefined' );
723                            } else {
724                                    my $validate = {
725                                            'last-size' => qr/^\d+/,
726                                            'twitter' => qr/^\w+\s+\w+/,
727                                    };
728    
729                                    my ( $op, $val ) = ( $1, $2 );
730    
731                                    if ( my $regex = $validate->{$op} ) {
732                                            if ( $val =~ $regex ) {
733                                                    meta( $nick, $channel, $op, $val );
734                                                    $res = "saved $op = $val";
735                                            } else {
736                                                    $res = "config option $op = $val doesn't validate against $regex";
737                                            }
738                                    } else {
739                                            $res = "config option $op doesn't exist";
740                                    }
741                          }                          }
742                  }                  }
743    
# Line 819  p { margin: 0; padding: 0.1em; } Line 866  p { margin: 0; padding: 0.1em; }
866  .nick { color: #000000; font-size: 80%; padding: 2px; font-family: courier, courier new, monospace ; }  .nick { color: #000000; font-size: 80%; padding: 2px; font-family: courier, courier new, monospace ; }
867  .message { color: #000000; font-size: 100%; }  .message { color: #000000; font-size: 100%; }
868  .search { float: right; }  .search { float: right; }
869    a:link.tag, a:visited.tag { border: 1px dashed #ccc; backgound: #ccc; text-decoration: none }
870    a:hover.tag { border: 1px solid #eee }
871    hr { border: 1px dashed #ccc; height: 1px; clear: both; }
872    /*
873  .col-0 { background: #ffff66 }  .col-0 { background: #ffff66 }
874  .col-1 { background: #a0ffff }  .col-1 { background: #a0ffff }
875  .col-2 { background: #99ff99 }  .col-2 { background: #99ff99 }
876  .col-3 { background: #ff9999 }  .col-3 { background: #ff9999 }
877  .col-4 { background: #ff66ff }  .col-4 { background: #ff66ff }
878  a:link.tag, a:visited.tag { border: 1px dashed #ccc; backgound: #ccc; text-decoration: none }  */
879  a:hover.tag { border: 1px solid #eee }  .calendar { border: 1px solid red; width: 100%; }
880  hr { border: 1px dashed #ccc; height: 1px; clear: both; }  .month { border: 0px; width: 100%; }
881  _END_OF_STYLE_  _END_OF_STYLE_
882    
883  my $max_color = 4;  my $max_color = 4;
884    
885    my @cols = qw(
886            #ffcccc #ccffe6 #ccccff #e6ccff #ffccff #ffcce6 #ff9999 #ffcc99 #ffff99
887            #ccff99 #99ff99 #99ffcc #99ccff #9999ff #cc99ff #ff6666 #ffb366 #ffff66
888            #66ff66 #66ffb3 #66b3ff #6666ff #ff3333 #33ff33 #3399ff #3333ff #ff3399
889            #a0a0a0 #ff0000 #ffff00 #80ff00 #0000ff #8000ff #ff00ff #ff0080 #994d00
890            #999900 #009900 #cc0066 #c0c0c0 #ccff99 #99ff33 #808080 #660033 #ffffff
891    );
892    
893    $max_color = 0;
894    foreach my $c (@cols) {
895            $style .= ".col-${max_color} { background: $c }\n";
896            $max_color++;
897    }
898    warn "defined $max_color colors for users...\n";
899    
900  my %nick_enumerator;  my %nick_enumerator;
901    
902  sub root_handler {  sub root_handler {
# Line 864  sub root_handler { Line 930  sub root_handler {
930                  qq{<p>};                  qq{<p>};
931          if ($request->url =~ m#/history#) {          if ($request->url =~ m#/history#) {
932                  my $sth = $dbh->prepare(qq{                  my $sth = $dbh->prepare(qq{
933                          select date(time) as date,count(*) as nr                          select date(time) as date,count(*) as nr,sum(length(message)) as len
934                                  from log                                  from log
935                                  group by date(time)                                  group by date(time)
936                                  order by date(time) desc                                  order by date(time) desc
937                  });                  });
938                  $sth->execute();                  $sth->execute();
939                  my ($l_yyyy,$l_mm) = (0,0);                  my ($l_yyyy,$l_mm) = (0,0);
940                    $html .= qq{<table class="calendar"><tr>};
941                  my $cal;                  my $cal;
942                    my $ord = 0;
943                  while (my $row = $sth->fetchrow_hashref) {                  while (my $row = $sth->fetchrow_hashref) {
944                          # this is probably PostgreSQL specific, expects ISO date                          # this is probably PostgreSQL specific, expects ISO date
945                          my ($yyyy,$mm,$dd) = split(/-/, $row->{date});                          my ($yyyy,$mm,$dd) = split(/-/, $row->{date});
946                          if ($yyyy != $l_yyyy || $mm != $l_mm) {                          if ($yyyy != $l_yyyy || $mm != $l_mm) {
947                                  $html .= $cal->as_HTML() if ($cal);                                  if ( $cal ) {
948                                            $html .= qq{<td valign="top">} . $cal->as_HTML() . qq{</td>};
949                                            $ord++;
950                                            $html .= qq{</tr><tr>} if ( $ord % 3 == 0 );
951                                    }
952                                  $cal = new HTML::CalendarMonthSimple('month'=>$mm,'year'=>$yyyy);                                  $cal = new HTML::CalendarMonthSimple('month'=>$mm,'year'=>$yyyy);
953                                  $cal->border(2);                                  $cal->border(1);
954                                    $cal->width('30%');
955                                    $cal->cellheight('5em');
956                                    $cal->tableclass('month');
957                                    #$cal->cellclass('day');
958                                    $cal->sunday('SUN');
959                                    $cal->saturday('SAT');
960                                    $cal->weekdays('MON','TUE','WED','THU','FRI');
961                                  ($l_yyyy,$l_mm) = ($yyyy,$mm);                                  ($l_yyyy,$l_mm) = ($yyyy,$mm);
962                          }                          }
963                          $cal->setcontent($dd, qq{                          $cal->setcontent($dd, qq{
964                                  <a href="/?date=$row->{date}">$row->{nr}</a>                                  <a href="/?date=$row->{date}">$row->{nr}</a><br/>$row->{len}
965                          });                          });
966                            
967                  }                  }
968                  $html .= $cal->as_HTML() if ($cal);                  $html .= qq{<td valign="top">} . $cal->as_HTML() . qq{</td></tr></table>};
969    
970          } else {          } else {
971                  $html .= join("</p><p>",                  $html .= join("</p><p>",
972                          get_from_log(                          get_from_log(
973                                  limit => $q->param('last') || $q->param('date') ? undef : 100,                                  limit => ( $q->param('last') || $q->param('date') ) ? undef : 100,
974                                  search => $search || undef,                                  search => $search || undef,
975                                  tag => $q->param('tag') || undef,                                  tag => $q->param('tag') || undef,
976                                  date => $q->param('date') || undef,                                  date => $q->param('date') || undef,
# Line 908  sub root_handler { Line 988  sub root_handler {
988                                  filter => {                                  filter => {
989                                          message => sub {                                          message => sub {
990                                                  my $m = shift || return;                                                  my $m = shift || return;
991    
992                                                    # protect HTML from wiki modifications
993                                                    sub e {
994                                                            my $t = shift;
995                                                            return 'uri_unescape{' . uri_escape($t) . '}';
996                                                    }
997    
998                                                  $m =~ s/($escape_re)/$escape{$1}/gs;                                                  $m =~ s/($escape_re)/$escape{$1}/gs;
999                                                  $m =~ s#($RE{URI}{HTTP})#<a href="$1">$1</a>#gs;                                                  $m =~ s#($RE{URI}{HTTP})#e(qq{<a href="$1">$1</a>})#egs;
1000                                                  $m =~ s#$tag_regex#<a href="?tag=$1" class="tag">$1</a>#g;                                                  $m =~ s#$tag_regex#e(qq{<a href="?tag=$1" class="tag">$1</a>})#egs;
1001                                                    $m =~ s#\*(\w+)\*#<b>$1</b>#gs;
1002                                                    $m =~ s#_(\w+)_#<u>$1</u>#gs;
1003                                                    $m =~ s#\/(\w+)\/#<i>$1</i>#gs;
1004    
1005                                                    $m =~ s#uri_unescape{([^}]+)}#uri_unescape($1)#egs;
1006                                                  return $m;                                                  return $m;
1007                                          },                                          },
1008                                          nick => sub {                                          nick => sub {

Legend:
Removed from v.50  
changed lines
  Added in v.68

  ViewVC Help
Powered by ViewVC 1.1.26