/[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 23 by dpavlin, Sun Mar 26 01:01:10 2006 UTC revision 30 by dpavlin, Fri Jun 16 21:28:46 2006 UTC
# Line 18  log all conversation on irc channel Line 18  log all conversation on irc channel
18    
19  ## CONFIG  ## CONFIG
20    
21  my $NICK = 'irc-logger-dev';  my $NICK = 'irc-logger';
22  my $CONNECT =  my $CONNECT =
23    {Server => 'irc.freenode.net',    {Server => 'irc.freenode.net',
24     Nick => $NICK,     Nick => $NICK,
# Line 44  my $ENCODING = 'ISO-8859-2'; Line 44  my $ENCODING = 'ISO-8859-2';
44  use POE qw(Component::IRC Wheel::FollowTail Component::Server::HTTP);  use POE qw(Component::IRC Wheel::FollowTail Component::Server::HTTP);
45  use HTTP::Status;  use HTTP::Status;
46  use DBI;  use DBI;
47  use Encode qw/from_to/;  use Encode qw/from_to is_utf8/;
48  use Regexp::Common qw /URI/;  use Regexp::Common qw /URI/;
49  use CGI::Simple;  use CGI::Simple;
50    use HTML::TagCloud;
51    
52  my $dbh = DBI->connect($DSN,"","", { RaiseError => 1, AutoCommit => 1 }) || die $DBI::errstr;  my $dbh = DBI->connect($DSN,"","", { RaiseError => 1, AutoCommit => 1 }) || die $DBI::errstr;
53    
# Line 82  insert into log Line 82  insert into log
82  values (?,?,?,?)  values (?,?,?,?)
83  });  });
84    
85    my $tags;
86    
87  =head2 get_from_log  =head2 get_from_log
88    
89   my @messages = get_from_log(   my @messages = get_from_log(
# Line 116  sub get_from_log { Line 118  sub get_from_log {
118          $args->{limit} ||= 10;          $args->{limit} ||= 10;
119    
120          $args->{fmt} ||= {          $args->{fmt} ||= {
121                    date => '[%s] ',
122                  time => '{%s} ',                  time => '{%s} ',
123                  time_channel => '{%s %s} ',                  time_channel => '{%s %s} ',
124                  nick => '%s: ',                  nick => '%s: ',
# Line 144  sub get_from_log { Line 147  sub get_from_log {
147    
148          my $sql = $context ? $sql_context : $sql_message;          my $sql = $context ? $sql_context : $sql_message;
149    
150          $sql .= " where message ilike ? " if ($args->{search});          $sql .= " where message ilike ? or nick ilike ? " if ($args->{search});
151            $sql .= " where id in (" . join(",", @{ $tags->{ $args->{tag} } }) . ") " if ($args->{tag} && $tags->{ $args->{tag} });
152          $sql .= " order by log.time desc";          $sql .= " order by log.time desc";
153          $sql .= " limit " . $args->{limit};          $sql .= " limit " . $args->{limit};
154    
# Line 152  sub get_from_log { Line 156  sub get_from_log {
156          if (my $search = $args->{search}) {          if (my $search = $args->{search}) {
157                  $search =~ s/^\s+//;                  $search =~ s/^\s+//;
158                  $search =~ s/\s+$//;                  $search =~ s/\s+$//;
159                  $sth->execute( '%' . $search . '%' );                  $sth->execute( ( '%' . $search . '%' ) x 2 );
160                  warn "search for '$search' returned ", $sth->rows, " results ", $context || '', "\n";                  warn "search for '$search' returned ", $sth->rows, " results ", $context || '', "\n";
161            } elsif (my $tag = $args->{tag}) {
162                    $sth->execute();
163                    warn "tag '$tag' returned ", $sth->rows, " results ", $context || '', "\n";
164          } else {          } else {
165                  $sth->execute();                  $sth->execute();
166          }          }
# Line 202  sub get_from_log { Line 209  sub get_from_log {
209    
210                  $row->{time} =~ s#\.\d+##;                  $row->{time} =~ s#\.\d+##;
211    
                 my $t;  
                 $t = $row->{date} . ' ' if ($last_row->{date} ne $row->{date});  
                 $t .= $row->{time};  
   
212                  my $msg = '';                  my $msg = '';
213    
214                    $msg = sprintf($args->{fmt}->{date}, $row->{date}) . ' ' if ($last_row->{date} ne $row->{date});
215                    my $t = $row->{time};
216    
217                  if ($last_row->{channel} ne $row->{channel}) {                  if ($last_row->{channel} ne $row->{channel}) {
218                          $msg .= sprintf($args->{fmt}->{time_channel}, $t, $row->{channel});                          $msg .= sprintf($args->{fmt}->{time_channel}, $t, $row->{channel});
219                  } else {                  } else {
# Line 217  sub get_from_log { Line 223  sub get_from_log {
223                  my $append = 1;                  my $append = 1;
224    
225                  my $nick = $row->{nick};                  my $nick = $row->{nick};
226                  $nick =~ s/^_*(.*?)_*$/$1/;                  if ($nick =~ s/^_*(.*?)_*$/$1/) {
227                                    $row->{nick} = $nick;
228                    }
229    
230                  if ($last_row->{nick} ne $nick) {                  if ($last_row->{nick} ne $nick) {
231                          # 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
232                          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 282  POE::Session->create Line 290  POE::Session->create
290    
291                  print "$channel: <$nick> $msg\n";                  print "$channel: <$nick> $msg\n";
292                  $sth->execute($channel, 0, $nick, $msg);                  $sth->execute($channel, 0, $nick, $msg);
293                    add_tag( id => $dbh->last_insert_id(undef,undef,"log",undef),
294                            message => $msg);
295      },      },
296      irc_ctcp_action => sub {      irc_ctcp_action => sub {
297                  my $kernel = $_[KERNEL];                  my $kernel = $_[KERNEL];
# Line 293  POE::Session->create Line 303  POE::Session->create
303    
304                  print "$channel ***$nick $msg\n";                  print "$channel ***$nick $msg\n";
305                  $sth->execute($channel, 1, $nick, $msg);                  $sth->execute($channel, 1, $nick, $msg);
306                    add_tag( id => $dbh->last_insert_id(undef,undef,"log",undef),
307                            message => $msg);
308      },      },
309          irc_msg => sub {          irc_msg => sub {
310                  my $kernel = $_[KERNEL];                  my $kernel = $_[KERNEL];
# Line 471  POE::Session->create Line 483  POE::Session->create
483     },     },
484    );    );
485    
486    # tags support
487    
488    my $cloud = HTML::TagCloud->new;
489    
490    =head2 add_tag
491    
492     add_tag( id => 42, message => 'irc message' );
493    
494    =cut
495    
496    sub add_tag {
497            my $arg = {@_};
498    
499            return unless ($arg->{id} && $arg->{message});
500    
501            my $m = $arg->{message};
502            from_to('UTF-8', 'iso-8859-2', $m) if (is_utf8($m));
503    
504            while ($m =~ s#\b([\w-_]+)//##s) {
505                    my $tag = $1;
506                    next if (! $tag || $tag =~ m/https?:/i);
507                    push @{ $tags->{$tag} }, $arg->{id};
508            }
509    }
510    
511    =head2 seed_tags
512    
513    Read all tags from database and create in-memory cache for tags
514    
515    =cut
516    
517    sub seed_tags {
518            my $sth = $dbh->prepare(qq{ select id,message from log where message like '%//%' });
519            $sth->execute;
520            while (my $row = $sth->fetchrow_hashref) {
521                    add_tag( %$row );
522            }
523    
524            foreach my $tag (keys %$tags) {
525                    $cloud->add($tag, "?tag=$tag", scalar @{$tags->{$tag}} + 1);
526            }
527    }
528    
529    seed_tags;
530    
531  # http server  # http server
532    
533  my $httpd = POE::Component::Server::HTTP->new(  my $httpd = POE::Component::Server::HTTP->new(
# Line 485  my $escape_re  = join '|' => keys %escap Line 542  my $escape_re  = join '|' => keys %escap
542  my $style = <<'_END_OF_STYLE_';  my $style = <<'_END_OF_STYLE_';
543  p { margin: 0; padding: 0.1em; }  p { margin: 0; padding: 0.1em; }
544  .time, .channel { color: #808080; font-size: 60%; }  .time, .channel { color: #808080; font-size: 60%; }
545    .date { float: right; background: #e0e0e0; color: #404040; font-size: 120%; padding: 0.25em; border: 1px dashed #808080; }
546  .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 ; }
547  .message { color: #000000; font-size: 100%; }  .message { color: #000000; font-size: 100%; }
548  .search { float: right; }  .search { float: right; }
# Line 517  sub root_handler { Line 575  sub root_handler {
575          my $search = $q->param('search') || $q->param('grep') || '';          my $search = $q->param('search') || $q->param('grep') || '';
576    
577          $response->content(          $response->content(
578                  qq{<html><head><title>$NICK</title><style type="text/css">$style</style></head><body>                  qq{<html><head><title>$NICK</title><style type="text/css">$style} .
579                    $cloud->css .
580                    qq{</style></head><body>} .
581                    qq{
582                  <form method="post" class="search">                  <form method="post" class="search">
583                  <input type="text" name="search" value="$search" size="10">                  <input type="text" name="search" value="$search" size="10">
584                  <input type="submit" value="search">                  <input type="submit" value="search">
585                  </form>                  </form>
                 <p>  
586                  } .                  } .
587                    qq{<div>} . $cloud->html(500) . qq{</div>} .
588                    qq{<p>} .
589                  join("</p><p>",                  join("</p><p>",
590                          get_from_log(                          get_from_log(
591                                  limit => $q->param('last') || 100,                                  limit => $q->param('last') || 100,
592                                  search => $q->param('search') || $q->param('grep') || undef,                                  search => $search || undef,
593                                    tag => $q->param('tag') || undef,
594                                  fmt => {                                  fmt => {
595                                            date => '<hr size="1" style="clear: both;"/><div class="date">%s</div> ',
596                                          time => '<span class="time">%s</span> ',                                          time => '<span class="time">%s</span> ',
597                                          time_channel => '<span class="channel">%s %s</span> ',                                          time_channel => '<span class="channel">%s %s</span> ',
598                                          nick => '%s:&nbsp;',                                          nick => '%s:&nbsp;',

Legend:
Removed from v.23  
changed lines
  Added in v.30

  ViewVC Help
Powered by ViewVC 1.1.26