/[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 21 by dpavlin, Sat Mar 18 16:02:32 2006 UTC
# Line 33  my %FOLLOWS = Line 33  my %FOLLOWS =
33     ERROR => "/var/log/apache/error.log",     ERROR => "/var/log/apache/error.log",
34    );    );
35    
36  my $DSN = 'DBI:Pg:dbname=irc-logger';  my $DSN = 'DBI:Pg:dbname=' . $NICK;
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;
53    
54  =for SQL schema  eval {
55            $dbh->do(qq{ select count(*) from log });
56    };
57    
58    if ($@) {
59            warn "creating database table in $DSN\n";
60            $dbh->do(<<'_SQL_SCHEMA_');
61    
 $dbh->do(qq{  
62  create table log (  create table log (
63          id serial,          id serial,
64          time timestamp default now(),          time timestamp default now(),
65          channel text not null,          channel text not null,
66            me boolean default false,
67          nick text not null,          nick text not null,
68          message text not null,          message text not null,
69          primary key(id)          primary key(id)
# Line 62  create index log_time on log(time); Line 73  create index log_time on log(time);
73  create index log_channel on log(channel);  create index log_channel on log(channel);
74  create index log_nick on log(nick);  create index log_nick on log(nick);
75    
76  });  _SQL_SCHEMA_
77    }
 =cut  
78    
79  my $sth = $dbh->prepare(qq{  my $sth = $dbh->prepare(qq{
80  insert into log  insert into log
81          (channel, nick, message)          (channel, me, nick, message)
82  values (?,?,?)  values (?,?,?,?)
83  });  });
84    
85  =head2 get_from_log  =head2 get_from_log
# Line 77  values (?,?,?) Line 87  values (?,?,?)
87   my @messages = get_from_log(   my @messages = get_from_log(
88          limit => 42,          limit => 42,
89          search => '%what to stuff in ilike%',          search => '%what to stuff in ilike%',
90            fmt => {
91                    time => '{%s} ',
92                    time_channel => '{%s %s} ',
93                    nick => '%s: ',
94                    me_nick => '***%s ',
95                    message => '%s',
96            },
97            filter => {
98                    message => sub {
99                            # modify message content
100                            return shift;
101                    }
102            },
103            context => 5,
104   );   );
105    
106    Order is important. Fields are first passed through C<filter> (if available) and
107    then throgh C<< sprintf($fmt->{message}, $message >> if available.
108    
109    C<context> defines number of messages around each search hit for display.
110    
111  =cut  =cut
112    
113  sub get_from_log {  sub get_from_log {
# Line 86  sub get_from_log { Line 115  sub get_from_log {
115    
116          $args->{limit} ||= 10;          $args->{limit} ||= 10;
117    
118          my $sql = qq{          $args->{fmt} ||= {
119                    time => '{%s} ',
120                    time_channel => '{%s %s} ',
121                    nick => '%s: ',
122                    me_nick => '***%s ',
123                    message => '%s',
124            };
125    
126            my $sql_message = qq{
127                  select                  select
128                          time::date as date,                          time::date as date,
129                          time::time as time,                          time::time as time,
130                          channel,                          channel,
131                            me,
132                          nick,                          nick,
133                          message                          message
134                  from log                  from log
135          };          };
136    
137            my $sql_context = qq{
138                    select
139                            id
140                    from log
141            };
142    
143            my $context = $1 if ($args->{search} && $args->{search} =~ s/\s*\+(\d+)\s*/ /);
144    
145            my $sql = $context ? $sql_context : $sql_message;
146    
147          $sql .= " where message ilike ? " if ($args->{search});          $sql .= " where message ilike ? " if ($args->{search});
148          $sql .= " order by log.time desc";          $sql .= " order by log.time desc";
149          $sql .= " limit " . $args->{limit};          $sql .= " limit " . $args->{limit};
150    
151          my $sth = $dbh->prepare( $sql );          my $sth = $dbh->prepare( $sql );
152          if ($args->{search}) {          if (my $search = $args->{search}) {
153                  $sth->execute( $args->{search} );                  $search =~ s/^\s+//;
154                    $search =~ s/\s+$//;
155                    $sth->execute( '%' . $search . '%' );
156                    warn "search for '$search' returned ", $sth->rows, " results ", $context || '', "\n";
157          } else {          } else {
158                  $sth->execute();                  $sth->execute();
159          }          }
# Line 118  sub get_from_log { Line 170  sub get_from_log {
170                  unshift @rows, $row;                  unshift @rows, $row;
171          }          }
172    
173          my @msgs;          my @msgs = (
174                    "Showing " . ($#rows + 1) . " messages..."
175            );
176    
177            if ($context) {
178                    my @ids = @rows;
179                    @rows = ();
180    
181                    my $last_to = 0;
182    
183                    my $sth = $dbh->prepare( $sql_message . qq{ where id >= ? and id < ? } );
184                    foreach my $row_id (sort { $a->{id} <=> $b->{id} } @ids) {
185                            my $id = $row_id->{id} || die "can't find id in row";
186            
187                            my ($from, $to) = ($id - $context, $id + $context);
188                            $from = $last_to if ($from < $last_to);
189                            $last_to = $to;
190                            $sth->execute( $from, $to );
191    
192                            #warn "## id: $id from: $from to: $to returned: ", $sth->rows, "\n";
193    
194                            while (my $row = $sth->fetchrow_hashref) {
195                                    push @rows, $row;
196                            }
197    
198                    }
199            }
200    
201          foreach my $row (@rows) {          foreach my $row (@rows) {
202    
# Line 130  sub get_from_log { Line 208  sub get_from_log {
208    
209                  my $msg = '';                  my $msg = '';
210    
211                  $msg .= "{$t";                  if ($last_row->{channel} ne $row->{channel}) {
212                  $msg .= ' ' . $row->{channel} if ($last_row->{channel} ne $row->{channel});                          $msg .= sprintf($args->{fmt}->{time_channel}, $t, $row->{channel});
213                  $msg .= "} ";                  } else {
214                            $msg .= sprintf($args->{fmt}->{time}, $t);
215                    }
216    
217                  my $append = 1;                  my $append = 1;
218    
219                  if ($last_row->{nick} ne $row->{nick}) {                  if ($last_row->{nick} ne $row->{nick}) {
220                          $msg .= $row->{nick} . ': ';                          # obfu way to find format for me_nick if needed or fallback to default
221                            my $fmt = $row->{me} ? ( $args->{fmt}->{me_nick} || $args->{fmt}->{nick} ) : $args->{fmt}->{nick};
222                            $fmt ||= '%s';
223    
224                            my $nick = $row->{nick};
225                            $nick = $args->{filter}->{nick}->($nick) if (ref($args->{filter}->{nick}) eq 'CODE');
226    
227                            $msg .= sprintf( $fmt, $nick );
228                          $append = 0;                          $append = 0;
229                  }                  }
230    
231                  $msg .= $row->{message};                  $args->{fmt}->{message} ||= '%s';
232                    if (ref($args->{filter}->{message}) eq 'CODE') {
233                            $msg .= sprintf($args->{fmt}->{message},
234                                    $args->{filter}->{message}->(
235                                            $row->{message}
236                                    )
237                            );
238                    } else {
239                            $msg .= sprintf($args->{fmt}->{message}, $row->{message});
240                    }
241    
242                  if ($append && @msgs) {                  if ($append && @msgs) {
243                          $msgs[$#msgs] .= " " . $msg;                          $msgs[$#msgs] .= " " . $msg;
# Line 180  POE::Session->create Line 276  POE::Session->create
276                  my $channel = $_[ARG1]->[0];                  my $channel = $_[ARG1]->[0];
277                  my $msg = $_[ARG2];                  my $msg = $_[ARG2];
278    
279                  from_to($msg, 'UTF-8', 'ISO-8859-2');                  from_to($msg, 'UTF-8', $ENCODING);
280    
281                  print "$channel: <$nick> $msg\n";                  print "$channel: <$nick> $msg\n";
282                  $sth->execute($channel, $nick, $msg);                  $sth->execute($channel, 0, $nick, $msg);
283        },
284        irc_ctcp_action => sub {
285                    my $kernel = $_[KERNEL];
286                    my $nick = (split /!/, $_[ARG0])[0];
287                    my $channel = $_[ARG1]->[0];
288                    my $msg = $_[ARG2];
289    
290                    from_to($msg, 'UTF-8', $ENCODING);
291    
292                    print "$channel ***$nick $msg\n";
293                    $sth->execute($channel, 1, $nick, $msg);
294      },      },
295          irc_msg => sub {          irc_msg => sub {
296                  my $kernel = $_[KERNEL];                  my $kernel = $_[KERNEL];
297                  my $nick = (split /!/, $_[ARG0])[0];                  my $nick = (split /!/, $_[ARG0])[0];
298                  my $msg = $_[ARG2];                  my $msg = $_[ARG2];
299                  from_to($msg, 'UTF-8', 'ISO-8859-2');                  from_to($msg, 'UTF-8', $ENCODING);
300    
301                  my $res = "unknown command '$msg', try /msg $NICK help!";                  my $res = "unknown command '$msg', try /msg $NICK help!";
302                  my @out;                  my @out;
# Line 224  POE::Session->create Line 331  POE::Session->create
331    
332                          foreach my $res (get_from_log( limit => $1 )) {                          foreach my $res (get_from_log( limit => $1 )) {
333                                  print "last: $res\n";                                  print "last: $res\n";
334                                  from_to($res, 'ISO-8859-2', 'UTF-8');                                  from_to($res, $ENCODING, 'UTF-8');
335                                  $_[KERNEL]->post( $IRC_ALIAS => privmsg => $nick, $res );                                  $_[KERNEL]->post( $IRC_ALIAS => privmsg => $nick, $res );
336                          }                          }
337    
338                          $res = '';                          $res = '';
339    
340                  } elsif ($msg =~ m/^(search|grep)\s+(.*)$/i) {                  } elsif ($msg =~ m/^(search|grep)\s+(.*)\s*$/i) {
341    
342                          my $what = $2;                          my $what = $2;
343    
344                          foreach my $res (get_from_log( limit => 20, search => "%${what}%" )) {                          foreach my $res (get_from_log(
345                                            limit => 20,
346                                            search => $what,
347                                    )) {
348                                  print "search [$what]: $res\n";                                  print "search [$what]: $res\n";
349                                  from_to($res, 'ISO-8859-2', 'UTF-8');                                  from_to($res, $ENCODING, 'UTF-8');
350                                  $_[KERNEL]->post( $IRC_ALIAS => privmsg => $nick, $res );                                  $_[KERNEL]->post( $IRC_ALIAS => privmsg => $nick, $res );
351                          }                          }
352    
# Line 246  POE::Session->create Line 356  POE::Session->create
356    
357                  if ($res) {                  if ($res) {
358                          print ">> [$nick] $res\n";                          print ">> [$nick] $res\n";
359                          from_to($res, 'ISO-8859-2', 'UTF-8');                          from_to($res, $ENCODING, 'UTF-8');
360                          $_[KERNEL]->post( $IRC_ALIAS => privmsg => $nick, $res );                          $_[KERNEL]->post( $IRC_ALIAS => privmsg => $nick, $res );
361                  }                  }
362    
# Line 359  POE::Session->create Line 469  POE::Session->create
469     },     },
470    );    );
471    
472    # http server
473    
474    my $httpd = POE::Component::Server::HTTP->new(
475            Port => $NICK =~ m/-dev/ ? 8001 : 8000,
476            ContentHandler => { '/' => \&root_handler },
477            Headers        => { Server => 'irc-logger' },
478    );
479    
480    my %escape = ('<'=>'&lt;', '>'=>'&gt;', '&'=>'&amp;', '"'=>'&quot;');
481    my $escape_re  = join '|' => keys %escape;
482    
483    my $style = <<'_END_OF_STYLE_';
484    p { margin: 0; padding: 0.1em; }
485    .time, .channel { color: #808080; font-size: 60%; }
486    .nick { color: #000000; font-size: 80%; padding: 2px; font-family: courier, courier new, monospace ; }
487    .message { color: #000000; font-size: 100%; }
488    .search { float: right; }
489    .col-0 { background: #ffff66 }
490    .col-1 { background: #a0ffff }
491    .col-2 { background: #99ff99 }
492    .col-3 { background: #ff9999 }
493    .col-4 { background: #ff66ff }
494    _END_OF_STYLE_
495    
496    my $max_color = 4;
497    
498    my %nick_enumerator;
499    
500    sub root_handler {
501            my ($request, $response) = @_;
502            $response->code(RC_OK);
503            $response->content_type("text/html; charset=$ENCODING");
504    
505            my $q;
506    
507            if ( $request->method eq 'POST' ) {
508                    $q = new CGI::Simple( $request->content );
509            } elsif ( $request->uri =~ /\?(.+)$/ ) {
510                    $q = new CGI::Simple( $1 );
511            } else {
512                    $q = new CGI::Simple;
513            }
514    
515            my $search = $q->param('search') || $q->param('grep') || '';
516    
517            $response->content(
518                    qq{<html><head><title>$NICK</title><style type="text/css">$style</style></head><body>
519                    <form method="post" class="search">
520                    <input type="text" name="search" value="$search" size="10">
521                    <input type="submit" value="search">
522                    </form>
523                    <p>
524                    } .
525                    join("</p><p>",
526                            get_from_log(
527                                    limit => $q->param('limit') || 100,
528                                    search => $q->param('search') || $q->param('grep') || undef,
529                                    fmt => {
530                                            time => '<span class="time">%s</span> ',
531                                            time_channel => '<span class="channel">%s %s</span> ',
532                                            nick => '%s:&nbsp;',
533                                            me_nick => '***%s&nbsp;',
534                                            message => '<span class="message">%s</span>',
535                                    },
536                                    filter => {
537                                            message => sub {
538                                                    my $m = shift || return;
539                                                    $m =~ s/($escape_re)/$escape{$1}/gs;
540                                                    $m =~ s#($RE{URI}{HTTP})#<a href="$1">$1</a>#gs;
541                                                    return $m;
542                                            },
543                                            nick => sub {
544                                                    my $n = shift || return;
545                                                    if (! $nick_enumerator{$n})  {
546                                                            my $max = scalar keys %nick_enumerator;
547                                                            $nick_enumerator{$n} = $max + 1;
548                                                    }
549                                                    return '<span class="nick col-' .
550                                                            ( $nick_enumerator{$n} % $max_color ) .
551                                                            '">' . $n . '</span>';
552                                            },
553                                    },
554                            )
555                    ) .
556                    qq{</p></body></html>}
557            );
558            return RC_OK;
559    }
560    
561  POE::Kernel->run;  POE::Kernel->run;

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

  ViewVC Help
Powered by ViewVC 1.1.26