/[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 13 by dpavlin, Sun Mar 12 14:19:00 2006 UTC revision 18 by dpavlin, Mon Mar 13 17:07:40 2006 UTC
# Line 35  my %FOLLOWS = Line 35  my %FOLLOWS =
35    
36  my $DSN = 'DBI:Pg:dbname=irc-logger';  my $DSN = 'DBI:Pg:dbname=irc-logger';
37    
38    my $ENCODING = 'ISO-8859-2';
39    
40  ## END CONFIG  ## END CONFIG
41    
42    
# Line 43  use POE qw(Component::IRC Wheel::FollowT Line 45  use POE qw(Component::IRC Wheel::FollowT
45  use HTTP::Status;  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/;
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 84  values (?,?,?) Line 88  values (?,?,?)
88                  nick => '%s: ',                  nick => '%s: ',
89                  message => '%s',                  message => '%s',
90          },          },
91            message_filter => sub {
92                    # modify message content
93                    return shift;
94            }
95   );   );
96    
97  =cut  =cut
# Line 115  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 132  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 157  sub get_from_log { Line 168  sub get_from_log {
168                          $append = 0;                          $append = 0;
169                  }                  }
170    
171                  $msg .= sprintf($args->{fmt}->{message}, $row->{message});                  if (ref($args->{message_filter}) eq 'CODE') {
172                            $msg .= sprintf($args->{fmt}->{message},
173                                    $args->{message_filter}->(
174                                            $row->{message}
175                                    )
176                            );
177                    } else {
178                            $msg .= sprintf($args->{fmt}->{message}, $row->{message});
179                    }
180    
181                  if ($append && @msgs) {                  if ($append && @msgs) {
182                          $msgs[$#msgs] .= " " . $msg;                          $msgs[$#msgs] .= " " . $msg;
# Line 196  POE::Session->create Line 215  POE::Session->create
215                  my $channel = $_[ARG1]->[0];                  my $channel = $_[ARG1]->[0];
216                  my $msg = $_[ARG2];                  my $msg = $_[ARG2];
217    
218                  from_to($msg, 'UTF-8', 'ISO-8859-2');                  from_to($msg, 'UTF-8', $ENCODING);
219    
220                  print "$channel: <$nick> $msg\n";                  print "$channel: <$nick> $msg\n";
221                  $sth->execute($channel, $nick, $msg);                  $sth->execute($channel, $nick, $msg);
# Line 205  POE::Session->create Line 224  POE::Session->create
224                  my $kernel = $_[KERNEL];                  my $kernel = $_[KERNEL];
225                  my $nick = (split /!/, $_[ARG0])[0];                  my $nick = (split /!/, $_[ARG0])[0];
226                  my $msg = $_[ARG2];                  my $msg = $_[ARG2];
227                  from_to($msg, 'UTF-8', 'ISO-8859-2');                  from_to($msg, 'UTF-8', $ENCODING);
228    
229                  my $res = "unknown command '$msg', try /msg $NICK help!";                  my $res = "unknown command '$msg', try /msg $NICK help!";
230                  my @out;                  my @out;
# Line 240  POE::Session->create Line 259  POE::Session->create
259    
260                          foreach my $res (get_from_log( limit => $1 )) {                          foreach my $res (get_from_log( limit => $1 )) {
261                                  print "last: $res\n";                                  print "last: $res\n";
262                                  from_to($res, 'ISO-8859-2', 'UTF-8');                                  from_to($res, $ENCODING, 'UTF-8');
263                                  $_[KERNEL]->post( $IRC_ALIAS => privmsg => $nick, $res );                                  $_[KERNEL]->post( $IRC_ALIAS => privmsg => $nick, $res );
264                          }                          }
265    
# Line 250  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, 'ISO-8859-2', 'UTF-8');                                  from_to($res, $ENCODING, 'UTF-8');
275                                  $_[KERNEL]->post( $IRC_ALIAS => privmsg => $nick, $res );                                  $_[KERNEL]->post( $IRC_ALIAS => privmsg => $nick, $res );
276                          }                          }
277    
# Line 262  POE::Session->create Line 281  POE::Session->create
281    
282                  if ($res) {                  if ($res) {
283                          print ">> [$nick] $res\n";                          print ">> [$nick] $res\n";
284                          from_to($res, 'ISO-8859-2', 'UTF-8');                          from_to($res, $ENCODING, 'UTF-8');
285                          $_[KERNEL]->post( $IRC_ALIAS => privmsg => $nick, $res );                          $_[KERNEL]->post( $IRC_ALIAS => privmsg => $nick, $res );
286                  }                  }
287    
# Line 378  POE::Session->create Line 397  POE::Session->create
397  # http server  # http server
398    
399  my $httpd = POE::Component::Server::HTTP->new(  my $httpd = POE::Component::Server::HTTP->new(
400          Port => 8000,          Port => $NICK =~ m/-dev/ ? 8001 : 8000,
401          ContentHandler => { '/' => \&root_handler },          ContentHandler => { '/' => \&root_handler },
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');          $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> ',
448                                          nick => '<span class="nick">%s:</span> ',                                          nick => '<span class="nick">%s:</span> ',
449                                          message => '<span class="message">%s</span>',                                          message => '<span class="message">%s</span>',
450                                  },                                  },
451                                    message_filter => sub {
452                                            my $m = shift || return;
453                                            $m =~ s/($escape_re)/$escape{$1}/gs;
454                                            $m =~ s#($RE{URI}{HTTP})#<a href="$1">$1</a>#gs;
455                                            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.13  
changed lines
  Added in v.18

  ViewVC Help
Powered by ViewVC 1.1.26