/[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 15 by dpavlin, Mon Mar 13 12:56:26 2006 UTC revision 18 by dpavlin, Mon Mar 13 17:07:40 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 46  use HTTP::Status; Line 46  use HTTP::Status;
46  use DBI;  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;
50    
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;
# Line 122  sub get_from_log { Line 123  sub get_from_log {
123    
124          my $sth = $dbh->prepare( $sql );          my $sth = $dbh->prepare( $sql );
125          if ($args->{search}) {          if ($args->{search}) {
126                  $sth->execute( $args->{search} );                  $sth->execute( '%' . $args->{search} . '%' );
127                    warn "search for '$args->{search}' returned ", $sth->rows, " results\n";
128          } else {          } else {
129                  $sth->execute();                  $sth->execute();
130          }          }
# Line 139  sub get_from_log { Line 141  sub get_from_log {
141                  unshift @rows, $row;                  unshift @rows, $row;
142          }          }
143    
144          my @msgs;          my @msgs = (
145                    "Showing " . ($#rows + 1) . " messages..."
146            );
147    
148          foreach my $row (@rows) {          foreach my $row (@rows) {
149    
# Line 265  POE::Session->create Line 269  POE::Session->create
269    
270                          my $what = $2;                          my $what = $2;
271    
272                          foreach my $res (get_from_log( limit => 20, search => "%${what}%" )) {                          foreach my $res (get_from_log( limit => 20, search => $what )) {
273                                  print "search [$what]: $res\n";                                  print "search [$what]: $res\n";
274                                  from_to($res, $ENCODING, 'UTF-8');                                  from_to($res, $ENCODING, 'UTF-8');
275                                  $_[KERNEL]->post( $IRC_ALIAS => privmsg => $nick, $res );                                  $_[KERNEL]->post( $IRC_ALIAS => privmsg => $nick, $res );
# Line 398  my $httpd = POE::Component::Server::HTTP Line 402  my $httpd = POE::Component::Server::HTTP
402          Headers        => { Server => 'irc-logger' },          Headers        => { Server => 'irc-logger' },
403  );  );
404    
405    my %escape = ('<'=>'&lt;', '>'=>'&gt;', '&'=>'&amp;', '"'=>'&quot;');
406    my $escape_re  = join '|' => keys %escape;
407    
408  my $style = <<'_END_OF_STYLE_';  my $style = <<'_END_OF_STYLE_';
409    p { margin: 0; padding: 0.1em; }
410  .time, .channel { color: #808080; font-size: 60%; }  .time, .channel { color: #808080; font-size: 60%; }
411  .nick { color: #0000ff; font-size: 80%; }  .nick { color: #0000ff; font-size: 80%; }
412  .message { color: #000000; font-size: 100%; }  .message { color: #000000; font-size: 100%; }
413    .search { float: right; }
414  _END_OF_STYLE_  _END_OF_STYLE_
415    
416  sub root_handler {  sub root_handler {
417          my ($request, $response) = @_;          my ($request, $response) = @_;
418          $response->code(RC_OK);          $response->code(RC_OK);
419          $response->content_type("text/html; charset=$ENCODING");          $response->content_type("text/html; charset=$ENCODING");
420    
421            my $q;
422    
423            if ( $request->method eq 'POST' ) {
424                    $q = new CGI::Simple( $request->content );
425            } elsif ( $request->uri =~ /\?(.+)$/ ) {
426                    $q = new CGI::Simple( $1 );
427            } else {
428                    $q = new CGI::Simple;
429            }
430    
431            my $search = $q->param('search') || $q->param('grep') || '';
432    
433          $response->content(          $response->content(
434                  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</style></head><body>
435                  "irc-logger url: " . $request->uri . '<br/>' .                  <form method="post" class="search">
436                  join("<br/>",                  <input type="text" name="search" value="$search" size="10">
437                    <input type="submit" value="search">
438                    </form>
439                    <p>
440                    } .
441                    join("</p><p>",
442                          get_from_log(                          get_from_log(
443                                  limit => 100,                                  limit => $q->param('limit') || 100,
444                                    search => $q->param('search') || $q->param('grep') || undef,
445                                  fmt => {                                  fmt => {
446                                          time => '<span class="time">%s</span> ',                                          time => '<span class="time">%s</span> ',
447                                          time_channel => '<span class="channel">%s %s</span> ',                                          time_channel => '<span class="channel">%s %s</span> ',
# Line 422  sub root_handler { Line 450  sub root_handler {
450                                  },                                  },
451                                  message_filter => sub {                                  message_filter => sub {
452                                          my $m = shift || return;                                          my $m = shift || return;
453                                            $m =~ s/($escape_re)/$escape{$1}/gs;
454                                          $m =~ s#($RE{URI}{HTTP})#<a href="$1">$1</a>#gs;                                          $m =~ s#($RE{URI}{HTTP})#<a href="$1">$1</a>#gs;
455                                          return $m;                                          return $m;
456                                  },                                  },
457                          )                          )
458                  ) .                  ) .
459                  qq{</body></html>}                  qq{</p></body></html>}
460          );          );
461          return RC_OK;          return RC_OK;
462  }  }

Legend:
Removed from v.15  
changed lines
  Added in v.18

  ViewVC Help
Powered by ViewVC 1.1.26