/[irc-logger]/trunk/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/irc-logger.pl

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 27 by dpavlin, Mon May 22 16:19:00 2006 UTC revision 28 by dpavlin, Fri Jun 16 20:51:32 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';  my $NICK = 'irc-logger-dev';
22  my $CONNECT =  my $CONNECT =
23    {Server => 'irc.freenode.net',    {Server => 'irc.freenode.net',
24     Nick => $NICK,     Nick => $NICK,
# Line 47  use DBI; Line 47  use DBI;
47  use Encode qw/from_to/;  use Encode qw/from_to/;
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 146  sub get_from_log { Line 148  sub get_from_log {
148          my $sql = $context ? $sql_context : $sql_message;          my $sql = $context ? $sql_context : $sql_message;
149    
150          $sql .= " where message ilike ? or nick 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 155  sub get_from_log { Line 158  sub get_from_log {
158                  $search =~ s/\s+$//;                  $search =~ s/\s+$//;
159                  $sth->execute( ( '%' . $search . '%' ) x 2 );                  $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 473  POE::Session->create Line 479  POE::Session->create
479     },     },
480    );    );
481    
482    # tags support
483    
484    my $cloud = HTML::TagCloud->new;
485    
486    =head2 add_tag
487    
488     add_tag( id => 42, message => 'irc message' );
489    
490    =cut
491    
492    sub add_tag {
493            my $arg = {@_};
494    
495            return unless ($arg->{id} && $arg->{message});
496    
497            while ($arg->{message} =~ s#\b(\S+)//##s) {
498                    my $tag = $1;
499                    next if (! $tag || $tag =~ m/https?:/i);
500                    push @{ $tags->{$tag} }, $arg->{id};
501            }
502    }
503    
504    =head2 seed_tags
505    
506    Read all tags from database and create in-memory cache for tags
507    
508    =cut
509    
510    sub seed_tags {
511            my $sth = $dbh->prepare(qq{ select id,message from log where message like '%//%' });
512            $sth->execute;
513            while (my $row = $sth->fetchrow_hashref) {
514                    add_tag( %$row );
515            }
516    
517            foreach my $tag (keys %$tags) {
518                    $cloud->add($tag, "?tag=$tag", scalar @{$tags->{$tag}} + 1);
519            }
520    }
521    
522    seed_tags;
523    
524  # http server  # http server
525    
526  my $httpd = POE::Component::Server::HTTP->new(  my $httpd = POE::Component::Server::HTTP->new(
# Line 487  my $escape_re  = join '|' => keys %escap Line 535  my $escape_re  = join '|' => keys %escap
535  my $style = <<'_END_OF_STYLE_';  my $style = <<'_END_OF_STYLE_';
536  p { margin: 0; padding: 0.1em; }  p { margin: 0; padding: 0.1em; }
537  .time, .channel { color: #808080; font-size: 60%; }  .time, .channel { color: #808080; font-size: 60%; }
538  .date { float: right; clear: right; background: #e0e0e0; color: #404040; font-size: 120%; padding: 0.25em; border: 1px dashed #808080; }  .date { float: right; background: #e0e0e0; color: #404040; font-size: 120%; padding: 0.25em; border: 1px dashed #808080; }
539  .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 ; }
540  .message { color: #000000; font-size: 100%; }  .message { color: #000000; font-size: 100%; }
541  .search { float: right; }  .search { float: right; }
# Line 520  sub root_handler { Line 568  sub root_handler {
568          my $search = $q->param('search') || $q->param('grep') || '';          my $search = $q->param('search') || $q->param('grep') || '';
569    
570          $response->content(          $response->content(
571                  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} .
572                    $cloud->css .
573                    qq{</style></head><body>} .
574                    qq{
575                  <form method="post" class="search">                  <form method="post" class="search">
576                  <input type="text" name="search" value="$search" size="10">                  <input type="text" name="search" value="$search" size="10">
577                  <input type="submit" value="search">                  <input type="submit" value="search">
578                  </form>                  </form>
                 <p>  
579                  } .                  } .
580                    qq{<div>} . $cloud->html(500) . qq{</div>} .
581                    qq{<p>} .
582                  join("</p><p>",                  join("</p><p>",
583                          get_from_log(                          get_from_log(
584                                  limit => $q->param('last') || 100,                                  limit => $q->param('last') || 100,
585                                  search => $q->param('search') || $q->param('grep') || undef,                                  search => $search || undef,
586                                    tag => $q->param('tag'),
587                                  fmt => {                                  fmt => {
588                                          date => '<hr size="1" style="clear: both;"/><div class="date">%s</div> ',                                          date => '<hr size="1" style="clear: both;"/><div class="date">%s</div> ',
589                                          time => '<span class="time">%s</span> ',                                          time => '<span class="time">%s</span> ',

Legend:
Removed from v.27  
changed lines
  Added in v.28

  ViewVC Help
Powered by ViewVC 1.1.26