/[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 9 by dpavlin, Wed Mar 1 23:35:56 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 72  insert into log Line 77  insert into log
77  values (?,?,?)  values (?,?,?)
78  });  });
79    
80    =head2 get_from_log
81    
82     my @messages = get_from_log(
83            limit => 42,
84            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
98    
99    sub get_from_log {
100            my $args = {@_};
101    
102            $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{
112                    select
113                            time::date as date,
114                            time::time as time,
115                            channel,
116                            nick,
117                            message
118                    from log
119            };
120            $sql .= " where message ilike ? " if ($args->{search});
121            $sql .= " order by log.time desc";
122            $sql .= " limit " . $args->{limit};
123    
124            my $sth = $dbh->prepare( $sql );
125            if ($args->{search}) {
126                    warn "search for '$args->{search}' returned ", $sth->rows, " results\n";
127                    $sth->execute( '%' . $args->{search} . '%' );
128            } else {
129                    $sth->execute();
130            }
131            my $last_row = {
132                    date => '',
133                    time => '',
134                    channel => '',
135                    nick => '',
136            };
137    
138            my @rows;
139    
140            while (my $row = $sth->fetchrow_hashref) {
141                    unshift @rows, $row;
142            }
143    
144            my @msgs = (
145                    "Showing " . ($#rows + 1) . " messages..."
146            );
147    
148            foreach my $row (@rows) {
149    
150                    $row->{time} =~ s#\.\d+##;
151    
152                    my $t;
153                    $t = $row->{date} . ' ' if ($last_row->{date} ne $row->{date});
154                    $t .= $row->{time};
155    
156                    my $msg = '';
157    
158                    if ($last_row->{channel} ne $row->{channel}) {
159                            $msg .= sprintf($args->{fmt}->{time_channel}, $t, $row->{channel});
160                    } else {
161                            $msg .= sprintf($args->{fmt}->{time}, $t);
162                    }
163    
164                    my $append = 1;
165    
166                    if ($last_row->{nick} ne $row->{nick}) {
167                            $msg .= sprintf($args->{fmt}->{nick}, $row->{nick});
168                            $append = 0;
169                    }
170    
171                    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) {
182                            $msgs[$#msgs] .= " " . $msg;
183                    } else {
184                            push @msgs, $msg;
185                    }
186    
187                    $last_row = $row;
188            }
189    
190            return @msgs;
191    }
192    
193    
194  my $SKIPPING = 0;               # if skipping, how many we've done  my $SKIPPING = 0;               # if skipping, how many we've done
195  my $SEND_QUEUE;                 # cache  my $SEND_QUEUE;                 # cache
# Line 89  POE::Session->create Line 207  POE::Session->create
207                  $_[KERNEL]->post($IRC_ALIAS => join => '#logger');                  $_[KERNEL]->post($IRC_ALIAS => join => '#logger');
208                  $_[KERNEL]->yield("heartbeat"); # start heartbeat                  $_[KERNEL]->yield("heartbeat"); # start heartbeat
209  #               $_[KERNEL]->yield("my_add", $_) for keys %FOLLOWS;  #               $_[KERNEL]->yield("my_add", $_) for keys %FOLLOWS;
210                    $_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "IDENTIFY $NICK" );
211      },      },
212      irc_public => sub {      irc_public => sub {
213                  my $kernel = $_[KERNEL];                  my $kernel = $_[KERNEL];
# Line 96  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 105  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;
231    
232                  print "<< $msg\n";                  print "<< $msg\n";
233    
234                  if ($msg =~ m/^help/i) {                  if ($msg =~ m/^help/i) {
235    
236                          $res = "usage: /msg $NICK stat - shows user statistics | /msg $NICK last - show backtrace of conversations";                          $res = "usage: /msg $NICK comand | commands: stat - user/message stat | last - show backtrace | grep foobar - find foobar";
237    
238                    } elsif ($msg =~ m/^msg\s+(\S+)\s+(.*)$/i) {
239    
240                            print ">> /msg $1 $2\n";
241                            $_[KERNEL]->post( $IRC_ALIAS => privmsg => $1, $2 );
242                            $res = '';
243    
244                  } elsif ($msg =~ m/^stat.*?\s*(\d*)/i) {                  } elsif ($msg =~ m/^stat.*?\s*(\d*)/i) {
245    
# Line 131  POE::Session->create Line 257  POE::Session->create
257                          $res .= join(" | ", @users);                          $res .= join(" | ", @users);
258                  } elsif ($msg =~ m/^last.*?\s*(\d*)/i) {                  } elsif ($msg =~ m/^last.*?\s*(\d*)/i) {
259    
260                          my $nr = $1 || 10;                          foreach my $res (get_from_log( limit => $1 )) {
261                                    print "last: $res\n";
262                          my $sth = $dbh->prepare(qq{                                  from_to($res, $ENCODING, 'UTF-8');
263                                  select                                  $_[KERNEL]->post( $IRC_ALIAS => privmsg => $nick, $res );
                                         time::date as date,  
                                         time::time as time,  
                                         channel,  
                                         nick,  
                                         message  
                                 from log order by log.time desc limit $nr  
                         });  
                         $sth->execute();  
                         $res = "Last $nr messages: ";  
                         my $last_row = {  
                                 date => '',  
                                 time => '',  
                                 channel => '',  
                                 nick => '',  
                         };  
   
                         my @rows;  
   
                         while (my $row = $sth->fetchrow_hashref) {  
                                 unshift @rows, $row;  
264                          }                          }
265    
266                          my @msgs;                          $res = '';
   
                         foreach my $row (@rows) {  
   
                                 $row->{time} =~ s#\.\d+##;  
   
                                 my $t;  
                                 $t = $row->{date} . ' ' if ($last_row->{date} ne $row->{date});  
                                 $t .= $row->{time};  
   
                                 my $msg = '';  
   
                                 $msg .= "($t";  
                                 $msg .= ' ' . $row->{channel} if ($last_row->{channel} ne $row->{channel});  
                                 $msg .= ") ";  
   
                                 $msg .= $row->{nick} . ': '  if ($last_row->{nick} ne $row->{nick});  
267    
268                                  $msg .= $row->{message};                  } elsif ($msg =~ m/^(search|grep)\s+(.*)$/i) {
269    
270                                  push @msgs, $msg;                          my $what = $2;
271    
272                                  $last_row = $row;                          foreach my $res (get_from_log( limit => 20, search => $what )) {
273                          }                                  print "search [$what]: $res\n";
274                                    from_to($res, $ENCODING, 'UTF-8');
                         foreach my $res (@msgs) {  
                                 print "last: $res\n";  
                                 from_to($res, 'ISO-8859-2', 'UTF-8');  
275                                  $_[KERNEL]->post( $IRC_ALIAS => privmsg => $nick, $res );                                  $_[KERNEL]->post( $IRC_ALIAS => privmsg => $nick, $res );
276                          }                          }
277    
278                          $res = '';                          $res = '';
279    
280                  }                  }
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    
288          },          },
289            irc_477 => sub {
290                    print "# irc_477: ",$_[ARG1], "\n";
291                    $_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "register $NICK" );
292            },
293          irc_505 => sub {          irc_505 => sub {
294          print "# irc_505: ",$_[ARG1], "\n";                  print "# irc_505: ",$_[ARG1], "\n";
295                  $_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "register $NICK" );                  $_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "register $NICK" );
296                  warn "## register $NICK\n";  #               $_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "set hide email on" );
297    #               $_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "set email dpavlin\@rot13.org" );
298          },          },
299          irc_registered => sub {          irc_registered => sub {
                 $_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "IDENTIFY $NICK" );  
300                  warn "## indetify $NICK\n";                  warn "## indetify $NICK\n";
301                    $_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "IDENTIFY $NICK" );
302            },
303    #       irc_433 => sub {
304    #               print "# irc_433: ",$_[ARG1], "\n";
305    #               warn "## indetify $NICK\n";
306    #               $_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "IDENTIFY $NICK" );
307    #       },
308            irc_372 => sub {
309                    print "MOTD: ", $_[ARG1], "\n";
310            },
311            irc_snotice => sub {
312                    print "(server notice): ", $_[ARG0], "\n";
313          },          },
314      (map      (map
315       {       {
316         ;"irc_$_" => sub { }}         ;"irc_$_" => sub { }}
317       qw(join       qw(
         ctcp_version  
         connected snotice ctcp_action ping notice mode part quit  
         001 002 003 004 005  
         250 251 252 253 254 265 266  
         332 333 353 366 372 375 376  
                 477  
318                  )),                  )),
319    #       join
320    #       ctcp_version
321    #       connected snotice ctcp_action ping notice mode part quit
322    #       001 002 003 004 005
323    #       250 251 252 253 254 265 266
324    #       332 333 353 366 372 375 376
325    #       477
326      _child => sub {},      _child => sub {},
327      _default => sub {      _default => sub {
328        printf "%s: session %s caught an unhandled %s event.\n",        printf "%s: session %s caught an unhandled %s event.\n",
# Line 289  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.9  
changed lines
  Added in v.16

  ViewVC Help
Powered by ViewVC 1.1.26