/[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 24 by dpavlin, Sun Mar 26 17:49:54 2006 UTC revision 33 by dpavlin, Sat Jun 24 22:15:47 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 $HOSTNAME = `hostname`;
22    
23  my $NICK = 'irc-logger';  my $NICK = 'irc-logger';
24    $NICK .= '-dev' if ($HOSTNAME =~ m/llin/);
25  my $CONNECT =  my $CONNECT =
26    {Server => 'irc.freenode.net',    {Server => 'irc.freenode.net',
27     Nick => $NICK,     Nick => $NICK,
28     Ircname => "try /msg $NICK help",     Ircname => "try /msg $NICK help",
29    };    };
30  my $CHANNEL = '#razmjenavjestina';  my $CHANNEL = '#razmjenavjestina';
31    $CHANNEL = '#irc-logger' if ($HOSTNAME =~ m/llin/);
32  my $IRC_ALIAS = "log";  my $IRC_ALIAS = "log";
33    
34  my %FOLLOWS =  my %FOLLOWS =
# Line 44  my $ENCODING = 'ISO-8859-2'; Line 48  my $ENCODING = 'ISO-8859-2';
48  use POE qw(Component::IRC Wheel::FollowTail Component::Server::HTTP);  use POE qw(Component::IRC Wheel::FollowTail Component::Server::HTTP);
49  use HTTP::Status;  use HTTP::Status;
50  use DBI;  use DBI;
51  use Encode qw/from_to/;  use Encode qw/from_to is_utf8/;
52  use Regexp::Common qw /URI/;  use Regexp::Common qw /URI/;
53  use CGI::Simple;  use CGI::Simple;
54    use HTML::TagCloud;
55    
56  my $dbh = DBI->connect($DSN,"","", { RaiseError => 1, AutoCommit => 1 }) || die $DBI::errstr;  my $dbh = DBI->connect($DSN,"","", { RaiseError => 1, AutoCommit => 1 }) || die $DBI::errstr;
57    
# Line 82  insert into log Line 86  insert into log
86  values (?,?,?,?)  values (?,?,?,?)
87  });  });
88    
89    my $tags;
90    my $tag_regex = '\b([\w-_]+)//';
91    
92  =head2 get_from_log  =head2 get_from_log
93    
94   my @messages = get_from_log(   my @messages = get_from_log(
# Line 116  sub get_from_log { Line 123  sub get_from_log {
123          $args->{limit} ||= 10;          $args->{limit} ||= 10;
124    
125          $args->{fmt} ||= {          $args->{fmt} ||= {
126                    date => '[%s] ',
127                  time => '{%s} ',                  time => '{%s} ',
128                  time_channel => '{%s %s} ',                  time_channel => '{%s %s} ',
129                  nick => '%s: ',                  nick => '%s: ',
# Line 144  sub get_from_log { Line 152  sub get_from_log {
152    
153          my $sql = $context ? $sql_context : $sql_message;          my $sql = $context ? $sql_context : $sql_message;
154    
155          $sql .= " where message ilike ? " if ($args->{search});          $sql .= " where message ilike ? or nick ilike ? " if ($args->{search});
156            $sql .= " where id in (" . join(",", @{ $tags->{ $args->{tag} } }) . ") " if ($args->{tag} && $tags->{ $args->{tag} });
157          $sql .= " order by log.time desc";          $sql .= " order by log.time desc";
158          $sql .= " limit " . $args->{limit};          $sql .= " limit " . $args->{limit};
159    
# Line 152  sub get_from_log { Line 161  sub get_from_log {
161          if (my $search = $args->{search}) {          if (my $search = $args->{search}) {
162                  $search =~ s/^\s+//;                  $search =~ s/^\s+//;
163                  $search =~ s/\s+$//;                  $search =~ s/\s+$//;
164                  $sth->execute( '%' . $search . '%' );                  $sth->execute( ( '%' . $search . '%' ) x 2 );
165                  warn "search for '$search' returned ", $sth->rows, " results ", $context || '', "\n";                  warn "search for '$search' returned ", $sth->rows, " results ", $context || '', "\n";
166            } elsif (my $tag = $args->{tag}) {
167                    $sth->execute();
168                    warn "tag '$tag' returned ", $sth->rows, " results ", $context || '', "\n";
169          } else {          } else {
170                  $sth->execute();                  $sth->execute();
171          }          }
# Line 202  sub get_from_log { Line 214  sub get_from_log {
214    
215                  $row->{time} =~ s#\.\d+##;                  $row->{time} =~ s#\.\d+##;
216    
                 my $t;  
                 $t = $row->{date} . ' ' if ($last_row->{date} ne $row->{date});  
                 $t .= $row->{time};  
   
217                  my $msg = '';                  my $msg = '';
218    
219                    $msg = sprintf($args->{fmt}->{date}, $row->{date}) . ' ' if ($last_row->{date} ne $row->{date});
220                    my $t = $row->{time};
221    
222                  if ($last_row->{channel} ne $row->{channel}) {                  if ($last_row->{channel} ne $row->{channel}) {
223                          $msg .= sprintf($args->{fmt}->{time_channel}, $t, $row->{channel});                          $msg .= sprintf($args->{fmt}->{time_channel}, $t, $row->{channel});
224                  } else {                  } else {
# Line 284  POE::Session->create Line 295  POE::Session->create
295    
296                  print "$channel: <$nick> $msg\n";                  print "$channel: <$nick> $msg\n";
297                  $sth->execute($channel, 0, $nick, $msg);                  $sth->execute($channel, 0, $nick, $msg);
298                    add_tag( id => $dbh->last_insert_id(undef,undef,"log",undef),
299                            message => $msg);
300      },      },
301      irc_ctcp_action => sub {      irc_ctcp_action => sub {
302                  my $kernel = $_[KERNEL];                  my $kernel = $_[KERNEL];
# Line 295  POE::Session->create Line 308  POE::Session->create
308    
309                  print "$channel ***$nick $msg\n";                  print "$channel ***$nick $msg\n";
310                  $sth->execute($channel, 1, $nick, $msg);                  $sth->execute($channel, 1, $nick, $msg);
311                    add_tag( id => $dbh->last_insert_id(undef,undef,"log",undef),
312                            message => $msg);
313      },      },
314          irc_msg => sub {          irc_msg => sub {
315                  my $kernel = $_[KERNEL];                  my $kernel = $_[KERNEL];
# Line 473  POE::Session->create Line 488  POE::Session->create
488     },     },
489    );    );
490    
491    # tags support
492    
493    my $cloud = HTML::TagCloud->new;
494    
495    =head2 add_tag
496    
497     add_tag( id => 42, message => 'irc message' );
498    
499    =cut
500    
501    sub add_tag {
502            my $arg = {@_};
503    
504            return unless ($arg->{id} && $arg->{message});
505    
506            my $m = $arg->{message};
507            from_to('UTF-8', 'iso-8859-2', $m) if (is_utf8($m));
508    
509            while ($m =~ s#$tag_regex##s) {
510                    my $tag = $1;
511                    next if (! $tag || $tag =~ m/https?:/i);
512                    push @{ $tags->{$tag} }, $arg->{id};
513                    #warn "+tag $tag: $arg->{id}\n";
514                    $cloud->add($tag, "?tag=$tag", scalar @{$tags->{$tag}} + 1);
515            }
516    }
517    
518    =head2 seed_tags
519    
520    Read all tags from database and create in-memory cache for tags
521    
522    =cut
523    
524    sub seed_tags {
525            my $sth = $dbh->prepare(qq{ select id,message from log where message like '%//%' });
526            $sth->execute;
527            while (my $row = $sth->fetchrow_hashref) {
528                    add_tag( %$row );
529            }
530    
531            foreach my $tag (keys %$tags) {
532                    $cloud->add($tag, "?tag=$tag", scalar @{$tags->{$tag}} + 1);
533            }
534    }
535    
536    seed_tags;
537    
538  # http server  # http server
539    
540  my $httpd = POE::Component::Server::HTTP->new(  my $httpd = POE::Component::Server::HTTP->new(
# Line 487  my $escape_re  = join '|' => keys %escap Line 549  my $escape_re  = join '|' => keys %escap
549  my $style = <<'_END_OF_STYLE_';  my $style = <<'_END_OF_STYLE_';
550  p { margin: 0; padding: 0.1em; }  p { margin: 0; padding: 0.1em; }
551  .time, .channel { color: #808080; font-size: 60%; }  .time, .channel { color: #808080; font-size: 60%; }
552    .date { float: right; background: #e0e0e0; color: #404040; font-size: 120%; padding: 0.25em; border: 1px dashed #808080; }
553  .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 ; }
554  .message { color: #000000; font-size: 100%; }  .message { color: #000000; font-size: 100%; }
555  .search { float: right; }  .search { float: right; }
# Line 495  p { margin: 0; padding: 0.1em; } Line 558  p { margin: 0; padding: 0.1em; }
558  .col-2 { background: #99ff99 }  .col-2 { background: #99ff99 }
559  .col-3 { background: #ff9999 }  .col-3 { background: #ff9999 }
560  .col-4 { background: #ff66ff }  .col-4 { background: #ff66ff }
561    a:link.tag, a:visited.tag { border: 1px dashed #ccc; backgound: #ccc; text-decoration: none }
562    a:hover.tag { border: 1px solid #eee }
563  _END_OF_STYLE_  _END_OF_STYLE_
564    
565  my $max_color = 4;  my $max_color = 4;
# Line 519  sub root_handler { Line 584  sub root_handler {
584          my $search = $q->param('search') || $q->param('grep') || '';          my $search = $q->param('search') || $q->param('grep') || '';
585    
586          $response->content(          $response->content(
587                  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} .
588                  <form method="post" class="search">                  $cloud->css .
589                    qq{</style></head><body>} .
590                    qq{
591                    <form method="post" class="search" action="/">
592                  <input type="text" name="search" value="$search" size="10">                  <input type="text" name="search" value="$search" size="10">
593                  <input type="submit" value="search">                  <input type="submit" value="search">
594                  </form>                  </form>
                 <p>  
595                  } .                  } .
596                    $cloud->html(500) .
597                    qq{<p>} .
598                  join("</p><p>",                  join("</p><p>",
599                          get_from_log(                          get_from_log(
600                                  limit => $q->param('last') || 100,                                  limit => $q->param('last') || 100,
601                                  search => $q->param('search') || $q->param('grep') || undef,                                  search => $search || undef,
602                                    tag => $q->param('tag') || undef,
603                                  fmt => {                                  fmt => {
604                                            date => '<hr size="1" style="clear: both;"/><div class="date">%s</div> ',
605                                          time => '<span class="time">%s</span> ',                                          time => '<span class="time">%s</span> ',
606                                          time_channel => '<span class="channel">%s %s</span> ',                                          time_channel => '<span class="channel">%s %s</span> ',
607                                          nick => '%s:&nbsp;',                                          nick => '%s:&nbsp;',
# Line 542  sub root_handler { Line 613  sub root_handler {
613                                                  my $m = shift || return;                                                  my $m = shift || return;
614                                                  $m =~ s/($escape_re)/$escape{$1}/gs;                                                  $m =~ s/($escape_re)/$escape{$1}/gs;
615                                                  $m =~ s#($RE{URI}{HTTP})#<a href="$1">$1</a>#gs;                                                  $m =~ s#($RE{URI}{HTTP})#<a href="$1">$1</a>#gs;
616                                                    $m =~ s#$tag_regex#<a href="?tag=$1" class="tag">$1</a>#g;
617                                                  return $m;                                                  return $m;
618                                          },                                          },
619                                          nick => sub {                                          nick => sub {

Legend:
Removed from v.24  
changed lines
  Added in v.33

  ViewVC Help
Powered by ViewVC 1.1.26