/[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

Contents of /trunk/bin/irc-logger.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 22 - (show annotations)
Fri Mar 24 23:19:57 2006 UTC (18 years ago) by dpavlin
Original Path: trunk/irc-logger.pl
File MIME type: text/plain
File size: 13635 byte(s)
strip _ before and after nick

1 #!/usr/bin/perl -w
2 use strict;
3 $|++;
4
5 =head1 NAME
6
7 irc-logger.pl
8
9 =head1 SYNOPSIS
10
11 ./irc-logger.pl
12
13 =head1 DESCRIPTION
14
15 log all conversation on irc channel
16
17 =cut
18
19 ## CONFIG
20
21 my $NICK = 'irc-logger-dev';
22 my $CONNECT =
23 {Server => 'irc.freenode.net',
24 Nick => $NICK,
25 Ircname => "try /msg $NICK help",
26 };
27 my $CHANNEL = '#razmjenavjestina';
28 my $IRC_ALIAS = "log";
29
30 my %FOLLOWS =
31 (
32 ACCESS => "/var/log/apache/access.log",
33 ERROR => "/var/log/apache/error.log",
34 );
35
36 my $DSN = 'DBI:Pg:dbname=' . $NICK;
37
38 my $ENCODING = 'ISO-8859-2';
39
40 ## END CONFIG
41
42
43
44 use POE qw(Component::IRC Wheel::FollowTail Component::Server::HTTP);
45 use HTTP::Status;
46 use DBI;
47 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;
53
54 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
62 create table log (
63 id serial,
64 time timestamp default now(),
65 channel text not null,
66 me boolean default false,
67 nick text not null,
68 message text not null,
69 primary key(id)
70 );
71
72 create index log_time on log(time);
73 create index log_channel on log(channel);
74 create index log_nick on log(nick);
75
76 _SQL_SCHEMA_
77 }
78
79 my $sth = $dbh->prepare(qq{
80 insert into log
81 (channel, me, nick, message)
82 values (?,?,?,?)
83 });
84
85 =head2 get_from_log
86
87 my @messages = get_from_log(
88 limit => 42,
89 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
112
113 sub get_from_log {
114 my $args = {@_};
115
116 $args->{limit} ||= 10;
117
118 $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
128 time::date as date,
129 time::time as time,
130 channel,
131 me,
132 nick,
133 message
134 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});
148 $sql .= " order by log.time desc";
149 $sql .= " limit " . $args->{limit};
150
151 my $sth = $dbh->prepare( $sql );
152 if (my $search = $args->{search}) {
153 $search =~ s/^\s+//;
154 $search =~ s/\s+$//;
155 $sth->execute( '%' . $search . '%' );
156 warn "search for '$search' returned ", $sth->rows, " results ", $context || '', "\n";
157 } else {
158 $sth->execute();
159 }
160 my $last_row = {
161 date => '',
162 time => '',
163 channel => '',
164 nick => '',
165 };
166
167 my @rows;
168
169 while (my $row = $sth->fetchrow_hashref) {
170 unshift @rows, $row;
171 }
172
173 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) {
202
203 $row->{time} =~ s#\.\d+##;
204
205 my $t;
206 $t = $row->{date} . ' ' if ($last_row->{date} ne $row->{date});
207 $t .= $row->{time};
208
209 my $msg = '';
210
211 if ($last_row->{channel} ne $row->{channel}) {
212 $msg .= sprintf($args->{fmt}->{time_channel}, $t, $row->{channel});
213 } else {
214 $msg .= sprintf($args->{fmt}->{time}, $t);
215 }
216
217 my $append = 1;
218
219 my $nick = $row->{nick};
220 $nick =~ s/^_*(.*?)_*$/$1/;
221
222 if ($last_row->{nick} ne $nick) {
223 # obfu way to find format for me_nick if needed or fallback to default
224 my $fmt = $row->{me} ? ( $args->{fmt}->{me_nick} || $args->{fmt}->{nick} ) : $args->{fmt}->{nick};
225 $fmt ||= '%s';
226
227 $nick = $args->{filter}->{nick}->($nick) if (ref($args->{filter}->{nick}) eq 'CODE');
228
229 $msg .= sprintf( $fmt, $nick );
230 $append = 0;
231 }
232
233 $args->{fmt}->{message} ||= '%s';
234 if (ref($args->{filter}->{message}) eq 'CODE') {
235 $msg .= sprintf($args->{fmt}->{message},
236 $args->{filter}->{message}->(
237 $row->{message}
238 )
239 );
240 } else {
241 $msg .= sprintf($args->{fmt}->{message}, $row->{message});
242 }
243
244 if ($append && @msgs) {
245 $msgs[$#msgs] .= " " . $msg;
246 } else {
247 push @msgs, $msg;
248 }
249
250 $last_row = $row;
251 }
252
253 return @msgs;
254 }
255
256
257 my $SKIPPING = 0; # if skipping, how many we've done
258 my $SEND_QUEUE; # cache
259
260 POE::Component::IRC->new($IRC_ALIAS);
261
262 POE::Session->create
263 (inline_states =>
264 {_start => sub {
265 $_[KERNEL]->post($IRC_ALIAS => register => 'all');
266 $_[KERNEL]->post($IRC_ALIAS => connect => $CONNECT);
267 },
268 irc_255 => sub { # server is done blabbing
269 $_[KERNEL]->post($IRC_ALIAS => join => $CHANNEL);
270 $_[KERNEL]->post($IRC_ALIAS => join => '#logger');
271 $_[KERNEL]->yield("heartbeat"); # start heartbeat
272 # $_[KERNEL]->yield("my_add", $_) for keys %FOLLOWS;
273 $_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "IDENTIFY $NICK" );
274 },
275 irc_public => sub {
276 my $kernel = $_[KERNEL];
277 my $nick = (split /!/, $_[ARG0])[0];
278 my $channel = $_[ARG1]->[0];
279 my $msg = $_[ARG2];
280
281 from_to($msg, 'UTF-8', $ENCODING);
282
283 print "$channel: <$nick> $msg\n";
284 $sth->execute($channel, 0, $nick, $msg);
285 },
286 irc_ctcp_action => sub {
287 my $kernel = $_[KERNEL];
288 my $nick = (split /!/, $_[ARG0])[0];
289 my $channel = $_[ARG1]->[0];
290 my $msg = $_[ARG2];
291
292 from_to($msg, 'UTF-8', $ENCODING);
293
294 print "$channel ***$nick $msg\n";
295 $sth->execute($channel, 1, $nick, $msg);
296 },
297 irc_msg => sub {
298 my $kernel = $_[KERNEL];
299 my $nick = (split /!/, $_[ARG0])[0];
300 my $msg = $_[ARG2];
301 from_to($msg, 'UTF-8', $ENCODING);
302
303 my $res = "unknown command '$msg', try /msg $NICK help!";
304 my @out;
305
306 print "<< $msg\n";
307
308 if ($msg =~ m/^help/i) {
309
310 $res = "usage: /msg $NICK comand | commands: stat - user/message stat | last - show backtrace | grep foobar - find foobar";
311
312 } elsif ($msg =~ m/^msg\s+(\S+)\s+(.*)$/i) {
313
314 print ">> /msg $1 $2\n";
315 $_[KERNEL]->post( $IRC_ALIAS => privmsg => $1, $2 );
316 $res = '';
317
318 } elsif ($msg =~ m/^stat.*?\s*(\d*)/i) {
319
320 my $nr = $1 || 10;
321
322 my $sth = $dbh->prepare(qq{
323 select nick,count(*) from log group by nick order by count desc limit $nr
324 });
325 $sth->execute();
326 $res = "Top $nr users: ";
327 my @users;
328 while (my $row = $sth->fetchrow_hashref) {
329 push @users,$row->{nick} . ': ' . $row->{count};
330 }
331 $res .= join(" | ", @users);
332 } elsif ($msg =~ m/^last.*?\s*(\d*)/i) {
333
334 foreach my $res (get_from_log( limit => $1 )) {
335 print "last: $res\n";
336 from_to($res, $ENCODING, 'UTF-8');
337 $_[KERNEL]->post( $IRC_ALIAS => privmsg => $nick, $res );
338 }
339
340 $res = '';
341
342 } elsif ($msg =~ m/^(search|grep)\s+(.*)\s*$/i) {
343
344 my $what = $2;
345
346 foreach my $res (get_from_log(
347 limit => 20,
348 search => $what,
349 )) {
350 print "search [$what]: $res\n";
351 from_to($res, $ENCODING, 'UTF-8');
352 $_[KERNEL]->post( $IRC_ALIAS => privmsg => $nick, $res );
353 }
354
355 $res = '';
356
357 }
358
359 if ($res) {
360 print ">> [$nick] $res\n";
361 from_to($res, $ENCODING, 'UTF-8');
362 $_[KERNEL]->post( $IRC_ALIAS => privmsg => $nick, $res );
363 }
364
365 },
366 irc_477 => sub {
367 print "# irc_477: ",$_[ARG1], "\n";
368 $_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "register $NICK" );
369 },
370 irc_505 => sub {
371 print "# irc_505: ",$_[ARG1], "\n";
372 $_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "register $NICK" );
373 # $_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "set hide email on" );
374 # $_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "set email dpavlin\@rot13.org" );
375 },
376 irc_registered => sub {
377 warn "## indetify $NICK\n";
378 $_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "IDENTIFY $NICK" );
379 },
380 # irc_433 => sub {
381 # print "# irc_433: ",$_[ARG1], "\n";
382 # warn "## indetify $NICK\n";
383 # $_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "IDENTIFY $NICK" );
384 # },
385 irc_372 => sub {
386 print "MOTD: ", $_[ARG1], "\n";
387 },
388 irc_snotice => sub {
389 print "(server notice): ", $_[ARG0], "\n";
390 },
391 (map
392 {
393 ;"irc_$_" => sub { }}
394 qw(
395 )),
396 # join
397 # ctcp_version
398 # connected snotice ctcp_action ping notice mode part quit
399 # 001 002 003 004 005
400 # 250 251 252 253 254 265 266
401 # 332 333 353 366 372 375 376
402 # 477
403 _child => sub {},
404 _default => sub {
405 printf "%s: session %s caught an unhandled %s event.\n",
406 scalar localtime(), $_[SESSION]->ID, $_[ARG0];
407 print "The $_[ARG0] event was given these parameters: ",
408 join(" ", map({"ARRAY" eq ref $_ ? "[@$_]" : "$_"} @{$_[ARG1]})), "\n";
409 0; # false for signals
410 },
411 my_add => sub {
412 my $trailing = $_[ARG0];
413 my $session = $_[SESSION];
414 POE::Session->create
415 (inline_states =>
416 {_start => sub {
417 $_[HEAP]->{wheel} =
418 POE::Wheel::FollowTail->new
419 (
420 Filename => $FOLLOWS{$trailing},
421 InputEvent => 'got_line',
422 );
423 },
424 got_line => sub {
425 $_[KERNEL]->post($session => my_tailed =>
426 time, $trailing, $_[ARG0]);
427 },
428 },
429 );
430
431 },
432 my_tailed => sub {
433 my ($time, $file, $line) = @_[ARG0..ARG2];
434 ## $time will be undef on a probe, or a time value if a real line
435
436 ## PoCo::IRC has throttling built in, but no external visibility
437 ## so this is reaching "under the hood"
438 $SEND_QUEUE ||=
439 $_[KERNEL]->alias_resolve($IRC_ALIAS)->get_heap->{send_queue};
440
441 ## handle "no need to keep skipping" transition
442 if ($SKIPPING and @$SEND_QUEUE < 1) {
443 $_[KERNEL]->post($IRC_ALIAS => privmsg => $CHANNEL =>
444 "[discarded $SKIPPING messages]");
445 $SKIPPING = 0;
446 }
447
448 ## handle potential message display
449 if ($time) {
450 if ($SKIPPING or @$SEND_QUEUE > 3) { # 3 msgs per 10 seconds
451 $SKIPPING++;
452 } else {
453 my @time = localtime $time;
454 $_[KERNEL]->post($IRC_ALIAS => privmsg => $CHANNEL =>
455 sprintf "%02d:%02d:%02d: %s: %s",
456 ($time[2] + 11) % 12 + 1, $time[1], $time[0],
457 $file, $line);
458 }
459 }
460
461 ## handle re-probe/flush if skipping
462 if ($SKIPPING) {
463 $_[KERNEL]->delay($_[STATE] => 0.5); # $time will be undef
464 }
465
466 },
467 my_heartbeat => sub {
468 $_[KERNEL]->yield(my_tailed => time, "heartbeat", "beep");
469 $_[KERNEL]->delay($_[STATE] => 10);
470 }
471 },
472 );
473
474 # http server
475
476 my $httpd = POE::Component::Server::HTTP->new(
477 Port => $NICK =~ m/-dev/ ? 8001 : 8000,
478 ContentHandler => { '/' => \&root_handler },
479 Headers => { Server => 'irc-logger' },
480 );
481
482 my %escape = ('<'=>'&lt;', '>'=>'&gt;', '&'=>'&amp;', '"'=>'&quot;');
483 my $escape_re = join '|' => keys %escape;
484
485 my $style = <<'_END_OF_STYLE_';
486 p { margin: 0; padding: 0.1em; }
487 .time, .channel { color: #808080; font-size: 60%; }
488 .nick { color: #000000; font-size: 80%; padding: 2px; font-family: courier, courier new, monospace ; }
489 .message { color: #000000; font-size: 100%; }
490 .search { float: right; }
491 .col-0 { background: #ffff66 }
492 .col-1 { background: #a0ffff }
493 .col-2 { background: #99ff99 }
494 .col-3 { background: #ff9999 }
495 .col-4 { background: #ff66ff }
496 _END_OF_STYLE_
497
498 my $max_color = 4;
499
500 my %nick_enumerator;
501
502 sub root_handler {
503 my ($request, $response) = @_;
504 $response->code(RC_OK);
505 $response->content_type("text/html; charset=$ENCODING");
506
507 my $q;
508
509 if ( $request->method eq 'POST' ) {
510 $q = new CGI::Simple( $request->content );
511 } elsif ( $request->uri =~ /\?(.+)$/ ) {
512 $q = new CGI::Simple( $1 );
513 } else {
514 $q = new CGI::Simple;
515 }
516
517 my $search = $q->param('search') || $q->param('grep') || '';
518
519 $response->content(
520 qq{<html><head><title>$NICK</title><style type="text/css">$style</style></head><body>
521 <form method="post" class="search">
522 <input type="text" name="search" value="$search" size="10">
523 <input type="submit" value="search">
524 </form>
525 <p>
526 } .
527 join("</p><p>",
528 get_from_log(
529 limit => $q->param('limit') || 100,
530 search => $q->param('search') || $q->param('grep') || undef,
531 fmt => {
532 time => '<span class="time">%s</span> ',
533 time_channel => '<span class="channel">%s %s</span> ',
534 nick => '%s:&nbsp;',
535 me_nick => '***%s&nbsp;',
536 message => '<span class="message">%s</span>',
537 },
538 filter => {
539 message => sub {
540 my $m = shift || return;
541 $m =~ s/($escape_re)/$escape{$1}/gs;
542 $m =~ s#($RE{URI}{HTTP})#<a href="$1">$1</a>#gs;
543 return $m;
544 },
545 nick => sub {
546 my $n = shift || return;
547 if (! $nick_enumerator{$n}) {
548 my $max = scalar keys %nick_enumerator;
549 $nick_enumerator{$n} = $max + 1;
550 }
551 return '<span class="nick col-' .
552 ( $nick_enumerator{$n} % $max_color ) .
553 '">' . $n . '</span>';
554 },
555 },
556 )
557 ) .
558 qq{</p></body></html>}
559 );
560 return RC_OK;
561 }
562
563 POE::Kernel->run;

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26