/[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 17 by dpavlin, Mon Mar 13 16:50:07 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 $style = <<'_END_OF_STYLE_';  my $style = <<'_END_OF_STYLE_';
406    p { margin: 0; padding: 0.1em; }
407  .time, .channel { color: #808080; font-size: 60%; }  .time, .channel { color: #808080; font-size: 60%; }
408  .nick { color: #0000ff; font-size: 80%; }  .nick { color: #0000ff; font-size: 80%; }
409  .message { color: #000000; font-size: 100%; }  .message { color: #000000; font-size: 100%; }
410    .search { float: right; }
411  _END_OF_STYLE_  _END_OF_STYLE_
412    
413  sub root_handler {  sub root_handler {
414          my ($request, $response) = @_;          my ($request, $response) = @_;
415          $response->code(RC_OK);          $response->code(RC_OK);
416          $response->content_type('text/html');          $response->content_type("text/html; charset=$ENCODING");
417    
418            my $q;
419    
420            if ( $request->method eq 'POST' ) {
421                    $q = new CGI::Simple( $request->content );
422            } elsif ( $request->uri =~ /\?(.+)$/ ) {
423                    $q = new CGI::Simple( $1 );
424            } else {
425                    $q = new CGI::Simple;
426            }
427    
428            my $search = $q->param('search') || $q->param('grep') || '';
429    
430          $response->content(          $response->content(
431                  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>
432                  "irc-logger url: " . $request->uri . '<br/>' .                  <form method="post" class="search">
433                  join("<br/>",                  <input type="text" name="search" value="$search" size="10">
434                    <input type="submit" value="search">
435                    </form>
436                    <p>
437                    } .
438                    join("</p><p>",
439                          get_from_log(                          get_from_log(
440                                  limit => 100,                                  limit => $q->param('limit') || 100,
441                                    search => $q->param('search') || $q->param('grep') || undef,
442                                  fmt => {                                  fmt => {
443                                          time => '<span class="time">%s</span> ',                                          time => '<span class="time">%s</span> ',
444                                          time_channel => '<span class="channel">%s %s</span> ',                                          time_channel => '<span class="channel">%s %s</span> ',
445                                          nick => '<span class="nick">%s:</span> ',                                          nick => '<span class="nick">%s:</span> ',
446                                          message => '<span class="message">%s</span>',                                          message => '<span class="message">%s</span>',
447                                  },                                  },
448                                    message_filter => sub {
449                                            my $m = shift || return;
450                                            $m =~ s#($RE{URI}{HTTP})#<a href="$1">$1</a>#gs;
451                                            return $m;
452                                    },
453                          )                          )
454                  ) .                  ) .
455                  qq{</body></html>}                  qq{</p></body></html>}
456          );          );
457          return RC_OK;          return RC_OK;
458  }  }

Legend:
Removed from v.13  
changed lines
  Added in v.17

  ViewVC Help
Powered by ViewVC 1.1.26