/[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 12 by dpavlin, Sun Mar 12 13:33:20 2006 UTC revision 16 by dpavlin, Mon Mar 13 16:43:18 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    
43    
44  use POE qw(Component::IRC Wheel::FollowTail);  use POE qw(Component::IRC Wheel::FollowTail Component::Server::HTTP);
45    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 77  values (?,?,?) Line 82  values (?,?,?)
82   my @messages = get_from_log(   my @messages = get_from_log(
83          limit => 42,          limit => 42,
84          search => '%what to stuff in ilike%',          search => '%what to stuff in ilike%',
85            fmt => {
86                    time => '{%s} ',
87                    time_channel => '{%s %s} ',
88                    nick => '%s: ',
89                    message => '%s',
90            },
91            message_filter => sub {
92                    # modify message content
93                    return shift;
94            }
95   );   );
96    
97  =cut  =cut
# Line 86  sub get_from_log { Line 101  sub get_from_log {
101    
102          $args->{limit} ||= 10;          $args->{limit} ||= 10;
103    
104            $args->{fmt} ||= {
105                    time => '{%s} ',
106                    time_channel => '{%s %s} ',
107                    nick => '%s: ',
108                    message => '%s',
109            };
110    
111          my $sql = qq{          my $sql = qq{
112                  select                  select
113                          time::date as date,                          time::date as date,
# Line 101  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} );                  warn "search for '$args->{search}' returned ", $sth->rows, " results\n";
127                    $sth->execute( '%' . $args->{search} . '%' );
128          } else {          } else {
129                  $sth->execute();                  $sth->execute();
130          }          }
# Line 118  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 130  sub get_from_log { Line 155  sub get_from_log {
155    
156                  my $msg = '';                  my $msg = '';
157    
158                  $msg .= "{$t";                  if ($last_row->{channel} ne $row->{channel}) {
159                  $msg .= ' ' . $row->{channel} if ($last_row->{channel} ne $row->{channel});                          $msg .= sprintf($args->{fmt}->{time_channel}, $t, $row->{channel});
160                  $msg .= "} ";                  } else {
161                            $msg .= sprintf($args->{fmt}->{time}, $t);
162                    }
163    
164                  my $append = 1;                  my $append = 1;
165    
166                  if ($last_row->{nick} ne $row->{nick}) {                  if ($last_row->{nick} ne $row->{nick}) {
167                          $msg .= $row->{nick} . ': ';                          $msg .= sprintf($args->{fmt}->{nick}, $row->{nick});
168                          $append = 0;                          $append = 0;
169                  }                  }
170    
171                  $msg .= $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 180  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 189  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 224  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 234  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 246  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 359  POE::Session->create Line 394  POE::Session->create
394     },     },
395    );    );
396    
397    # http server
398    
399    my $httpd = POE::Component::Server::HTTP->new(
400            Port => $NICK =~ m/-dev/ ? 8001 : 8000,
401            ContentHandler => { '/' => \&root_handler },
402            Headers        => { Server => 'irc-logger' },
403    );
404    
405    my $style = <<'_END_OF_STYLE_';
406    p { margin: 0; padding: 0.1em; }
407    .time, .channel { color: #808080; font-size: 60%; }
408    .nick { color: #0000ff; font-size: 80%; }
409    .message { color: #000000; font-size: 100%; }
410    .search { float: right; }
411    _END_OF_STYLE_
412    
413    sub root_handler {
414            my ($request, $response) = @_;
415            $response->code(RC_OK);
416            $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(
431                    qq{<html><head><title>$NICK</title><style type="text/css">$style</style></head><body>
432                    <form method="post" class="search">
433                    <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(
440                                    limit => $q->param('limit') || 100,
441                                    search => $q->param('search') || $q->param('grep') || undef,
442                                    fmt => {
443                                            time => '<span class="time">%s</span> ',
444                                            time_channel => '<span class="channel">%s %s</span> ',
445                                            nick => '<span class="nick">%s:</span> ',
446                                            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{</p></body></html>}
456            );
457            return RC_OK;
458    }
459    
460  POE::Kernel->run;  POE::Kernel->run;

Legend:
Removed from v.12  
changed lines
  Added in v.16

  ViewVC Help
Powered by ViewVC 1.1.26