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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 42 - (hide annotations)
Fri Feb 2 21:37:52 2007 UTC (14 years, 4 months ago) by dpavlin
Original Path: trunk/irc-logger.pl
File MIME type: text/plain
File size: 19861 byte(s)
implement count|poll <something> <scan nr last messages> which will count all
<something>+ or <something>- so you can create quick polls about topics.

1 dpavlin 4 #!/usr/bin/perl -w
2     use strict;
3     $|++;
4    
5 dpavlin 5 =head1 NAME
6    
7     irc-logger.pl
8    
9     =head1 SYNOPSIS
10    
11     ./irc-logger.pl
12    
13 dpavlin 37 =head2 Options
14    
15     =over 4
16    
17     =item --import-dircproxy=filename
18    
19     Import log from C<dircproxy> to C<irc-logger> database
20    
21 dpavlin 5 =head1 DESCRIPTION
22    
23     log all conversation on irc channel
24    
25     =cut
26    
27 dpavlin 4 ## CONFIG
28    
29 dpavlin 33 my $HOSTNAME = `hostname`;
30    
31 dpavlin 29 my $NICK = 'irc-logger';
32 dpavlin 33 $NICK .= '-dev' if ($HOSTNAME =~ m/llin/);
33 dpavlin 4 my $CONNECT =
34     {Server => 'irc.freenode.net',
35     Nick => $NICK,
36 dpavlin 8 Ircname => "try /msg $NICK help",
37 dpavlin 4 };
38     my $CHANNEL = '#razmjenavjestina';
39 dpavlin 33 $CHANNEL = '#irc-logger' if ($HOSTNAME =~ m/llin/);
40 dpavlin 4 my $IRC_ALIAS = "log";
41    
42     my %FOLLOWS =
43     (
44     ACCESS => "/var/log/apache/access.log",
45     ERROR => "/var/log/apache/error.log",
46     );
47    
48 dpavlin 19 my $DSN = 'DBI:Pg:dbname=' . $NICK;
49 dpavlin 5
50 dpavlin 14 my $ENCODING = 'ISO-8859-2';
51 dpavlin 34 my $TIMESTAMP = '%Y-%m-%d %H:%M:%S';
52 dpavlin 14
53 dpavlin 41 my $sleep_on_error = 5;
54    
55 dpavlin 4 ## END CONFIG
56    
57 dpavlin 5
58    
59 dpavlin 13 use POE qw(Component::IRC Wheel::FollowTail Component::Server::HTTP);
60     use HTTP::Status;
61 dpavlin 5 use DBI;
62 dpavlin 30 use Encode qw/from_to is_utf8/;
63 dpavlin 15 use Regexp::Common qw /URI/;
64 dpavlin 16 use CGI::Simple;
65 dpavlin 28 use HTML::TagCloud;
66 dpavlin 34 use POSIX qw/strftime/;
67 dpavlin 35 use HTML::CalendarMonthSimple;
68 dpavlin 37 use Getopt::Long;
69     use DateTime;
70 dpavlin 5
71 dpavlin 37 my $import_dircproxy;
72     GetOptions(
73     'import-dircproxy:s' => \$import_dircproxy,
74     );
75    
76 dpavlin 5 my $dbh = DBI->connect($DSN,"","", { RaiseError => 1, AutoCommit => 1 }) || die $DBI::errstr;
77    
78 dpavlin 19 eval {
79     $dbh->do(qq{ select count(*) from log });
80     };
81 dpavlin 5
82 dpavlin 19 if ($@) {
83     warn "creating database table in $DSN\n";
84     $dbh->do(<<'_SQL_SCHEMA_');
85    
86 dpavlin 5 create table log (
87     id serial,
88     time timestamp default now(),
89     channel text not null,
90 dpavlin 19 me boolean default false,
91 dpavlin 5 nick text not null,
92     message text not null,
93     primary key(id)
94     );
95    
96     create index log_time on log(time);
97     create index log_channel on log(channel);
98     create index log_nick on log(nick);
99    
100 dpavlin 19 _SQL_SCHEMA_
101     }
102 dpavlin 5
103     my $sth = $dbh->prepare(qq{
104     insert into log
105 dpavlin 37 (channel, me, nick, message, time)
106     values (?,?,?,?,?)
107 dpavlin 5 });
108    
109 dpavlin 28 my $tags;
110 dpavlin 31 my $tag_regex = '\b([\w-_]+)//';
111 dpavlin 28
112 dpavlin 11 =head2 get_from_log
113 dpavlin 5
114 dpavlin 11 my @messages = get_from_log(
115     limit => 42,
116     search => '%what to stuff in ilike%',
117 dpavlin 13 fmt => {
118     time => '{%s} ',
119     time_channel => '{%s %s} ',
120     nick => '%s: ',
121 dpavlin 20 me_nick => '***%s ',
122 dpavlin 13 message => '%s',
123     },
124 dpavlin 20 filter => {
125     message => sub {
126     # modify message content
127     return shift;
128     }
129 dpavlin 21 },
130     context => 5,
131 dpavlin 42 full_rows => 1,
132 dpavlin 11 );
133    
134 dpavlin 20 Order is important. Fields are first passed through C<filter> (if available) and
135     then throgh C<< sprintf($fmt->{message}, $message >> if available.
136    
137 dpavlin 21 C<context> defines number of messages around each search hit for display.
138    
139 dpavlin 42 C<full_rows> will return database rows for each result with C<date>, C<time>, C<channel>,
140     C<me>, C<nick> and C<message> keys.
141    
142 dpavlin 11 =cut
143    
144     sub get_from_log {
145     my $args = {@_};
146    
147 dpavlin 13 $args->{fmt} ||= {
148 dpavlin 26 date => '[%s] ',
149 dpavlin 13 time => '{%s} ',
150     time_channel => '{%s %s} ',
151     nick => '%s: ',
152 dpavlin 20 me_nick => '***%s ',
153 dpavlin 13 message => '%s',
154     };
155    
156 dpavlin 21 my $sql_message = qq{
157 dpavlin 11 select
158     time::date as date,
159     time::time as time,
160     channel,
161 dpavlin 20 me,
162 dpavlin 11 nick,
163     message
164     from log
165     };
166 dpavlin 21
167     my $sql_context = qq{
168     select
169     id
170     from log
171     };
172    
173     my $context = $1 if ($args->{search} && $args->{search} =~ s/\s*\+(\d+)\s*/ /);
174    
175     my $sql = $context ? $sql_context : $sql_message;
176    
177 dpavlin 25 $sql .= " where message ilike ? or nick ilike ? " if ($args->{search});
178 dpavlin 28 $sql .= " where id in (" . join(",", @{ $tags->{ $args->{tag} } }) . ") " if ($args->{tag} && $tags->{ $args->{tag} });
179 dpavlin 35 $sql .= " where date(time) = ? " if ($args->{date});
180 dpavlin 11 $sql .= " order by log.time desc";
181 dpavlin 35 $sql .= " limit " . $args->{limit} if ($args->{limit});
182 dpavlin 11
183     my $sth = $dbh->prepare( $sql );
184 dpavlin 21 if (my $search = $args->{search}) {
185     $search =~ s/^\s+//;
186     $search =~ s/\s+$//;
187 dpavlin 25 $sth->execute( ( '%' . $search . '%' ) x 2 );
188 dpavlin 21 warn "search for '$search' returned ", $sth->rows, " results ", $context || '', "\n";
189 dpavlin 28 } elsif (my $tag = $args->{tag}) {
190     $sth->execute();
191     warn "tag '$tag' returned ", $sth->rows, " results ", $context || '', "\n";
192 dpavlin 35 } elsif (my $date = $args->{date}) {
193     $sth->execute($date);
194     warn "found ", $sth->rows, " messages for date $date ", $context || '', "\n";
195 dpavlin 11 } else {
196     $sth->execute();
197     }
198     my $last_row = {
199     date => '',
200     time => '',
201     channel => '',
202     nick => '',
203     };
204    
205     my @rows;
206    
207     while (my $row = $sth->fetchrow_hashref) {
208     unshift @rows, $row;
209     }
210    
211 dpavlin 42 # normalize nick names
212     map {
213     $_->{nick} =~ s/^_*(.*?)_*$/$1/
214     } @rows;
215    
216     return @rows if ($args->{full_rows});
217    
218 dpavlin 16 my @msgs = (
219     "Showing " . ($#rows + 1) . " messages..."
220     );
221 dpavlin 11
222 dpavlin 21 if ($context) {
223     my @ids = @rows;
224     @rows = ();
225    
226     my $last_to = 0;
227    
228     my $sth = $dbh->prepare( $sql_message . qq{ where id >= ? and id < ? } );
229     foreach my $row_id (sort { $a->{id} <=> $b->{id} } @ids) {
230     my $id = $row_id->{id} || die "can't find id in row";
231    
232     my ($from, $to) = ($id - $context, $id + $context);
233     $from = $last_to if ($from < $last_to);
234     $last_to = $to;
235     $sth->execute( $from, $to );
236    
237     #warn "## id: $id from: $from to: $to returned: ", $sth->rows, "\n";
238    
239     while (my $row = $sth->fetchrow_hashref) {
240     push @rows, $row;
241     }
242    
243     }
244     }
245    
246 dpavlin 35 # sprintf which can take coderef as first parametar
247     sub cr_sprintf {
248     my $fmt = shift || return;
249     if (ref($fmt) eq 'CODE') {
250     $fmt->(@_);
251     } else {
252     sprintf($fmt, @_);
253     }
254     }
255    
256 dpavlin 11 foreach my $row (@rows) {
257    
258     $row->{time} =~ s#\.\d+##;
259    
260     my $msg = '';
261    
262 dpavlin 35 $msg = cr_sprintf($args->{fmt}->{date}, $row->{date}) . ' ' if ($last_row->{date} ne $row->{date});
263 dpavlin 26 my $t = $row->{time};
264    
265 dpavlin 13 if ($last_row->{channel} ne $row->{channel}) {
266 dpavlin 35 $msg .= cr_sprintf($args->{fmt}->{time_channel}, $t, $row->{channel});
267 dpavlin 13 } else {
268 dpavlin 35 $msg .= cr_sprintf($args->{fmt}->{time}, $t);
269 dpavlin 13 }
270 dpavlin 11
271 dpavlin 12 my $append = 1;
272 dpavlin 11
273 dpavlin 22 my $nick = $row->{nick};
274 dpavlin 42 # if ($nick =~ s/^_*(.*?)_*$/$1/) {
275     # $row->{nick} = $nick;
276     # }
277 dpavlin 24
278 dpavlin 22 if ($last_row->{nick} ne $nick) {
279 dpavlin 20 # obfu way to find format for me_nick if needed or fallback to default
280     my $fmt = $row->{me} ? ( $args->{fmt}->{me_nick} || $args->{fmt}->{nick} ) : $args->{fmt}->{nick};
281     $fmt ||= '%s';
282    
283     $nick = $args->{filter}->{nick}->($nick) if (ref($args->{filter}->{nick}) eq 'CODE');
284    
285 dpavlin 35 $msg .= cr_sprintf( $fmt, $nick );
286 dpavlin 12 $append = 0;
287     }
288    
289 dpavlin 20 $args->{fmt}->{message} ||= '%s';
290     if (ref($args->{filter}->{message}) eq 'CODE') {
291 dpavlin 35 $msg .= cr_sprintf($args->{fmt}->{message},
292 dpavlin 20 $args->{filter}->{message}->(
293 dpavlin 15 $row->{message}
294     )
295     );
296     } else {
297 dpavlin 35 $msg .= cr_sprintf($args->{fmt}->{message}, $row->{message});
298 dpavlin 15 }
299 dpavlin 11
300 dpavlin 12 if ($append && @msgs) {
301     $msgs[$#msgs] .= " " . $msg;
302     } else {
303     push @msgs, $msg;
304     }
305 dpavlin 11
306     $last_row = $row;
307     }
308    
309     return @msgs;
310     }
311    
312 dpavlin 37 # tags support
313 dpavlin 11
314 dpavlin 37 my $cloud = HTML::TagCloud->new;
315 dpavlin 4
316 dpavlin 37 =head2 add_tag
317 dpavlin 4
318 dpavlin 37 add_tag( id => 42, message => 'irc message' );
319    
320     =cut
321    
322     sub add_tag {
323     my $arg = {@_};
324    
325     return unless ($arg->{id} && $arg->{message});
326    
327     my $m = $arg->{message};
328     from_to('UTF-8', 'iso-8859-2', $m) if (is_utf8($m));
329    
330     while ($m =~ s#$tag_regex##s) {
331     my $tag = $1;
332     next if (! $tag || $tag =~ m/https?:/i);
333     push @{ $tags->{$tag} }, $arg->{id};
334     #warn "+tag $tag: $arg->{id}\n";
335     $cloud->add($tag, "?tag=$tag", scalar @{$tags->{$tag}} + 1);
336     }
337     }
338    
339     =head2 seed_tags
340    
341     Read all tags from database and create in-memory cache for tags
342    
343     =cut
344    
345     sub seed_tags {
346     my $sth = $dbh->prepare(qq{ select id,message from log where message like '%//%' });
347     $sth->execute;
348     while (my $row = $sth->fetchrow_hashref) {
349     add_tag( %$row );
350     }
351    
352     foreach my $tag (keys %$tags) {
353     $cloud->add($tag, "?tag=$tag", scalar @{$tags->{$tag}} + 1);
354     }
355     }
356    
357     seed_tags;
358    
359    
360 dpavlin 36 =head2 save_message
361    
362 dpavlin 37 save_message(
363     channel => '#foobar',
364     me => 0,
365     nick => 'dpavlin',
366     msg => 'test message',
367     time => '2006-06-25 18:57:18',
368     );
369 dpavlin 36
370 dpavlin 37 C<time> is optional, it will use C<< now() >> if it's not available.
371    
372     C<me> if not specified will be C<0> (not C</me> message)
373    
374 dpavlin 36 =cut
375    
376     sub save_message {
377 dpavlin 37 my $a = {@_};
378     $a->{me} ||= 0;
379 dpavlin 38 $a->{time} ||= strftime($TIMESTAMP,localtime());
380 dpavlin 37
381     print
382 dpavlin 38 $a->{time}, " ",
383 dpavlin 37 $a->{channel}, " ",
384     $a->{me} ? "***" . $a->{nick} : "<" . $a->{nick} . ">",
385     " " . $a->{msg} . "\n";
386    
387     from_to($a->{msg}, 'UTF-8', $ENCODING);
388    
389     $sth->execute($a->{channel}, $a->{me}, $a->{nick}, $a->{msg}, $a->{time});
390 dpavlin 36 add_tag( id => $dbh->last_insert_id(undef,undef,"log",undef),
391 dpavlin 37 message => $a->{msg});
392 dpavlin 36 }
393    
394 dpavlin 37 if ($import_dircproxy) {
395     open(my $l, $import_dircproxy) || die "can't open $import_dircproxy: $!";
396     warn "importing $import_dircproxy...\n";
397     my $tz_offset = 2 * 60 * 60; # TZ GMT+2
398     while(<$l>) {
399     chomp;
400     if (/^@(\d+)\s(\S+)\s(.+)$/) {
401     my ($time, $nick, $msg) = ($1,$2,$3);
402    
403     my $dt = DateTime->from_epoch( epoch => $time + $tz_offset );
404    
405     my $me = 0;
406     $me = 1 if ($nick =~ m/^\[\S+]/);
407     $nick =~ s/^[\[<]([^!]+).*$/$1/;
408    
409     $msg =~ s/^ACTION\s+// if ($me);
410    
411     save_message(
412     channel => $CHANNEL,
413     me => $me,
414     nick => $nick,
415     msg => $msg,
416     time => $dt->ymd . " " . $dt->hms,
417     ) if ($nick !~ m/^-/);
418    
419     } else {
420     warn "can't parse: $_\n";
421     }
422     }
423     close($l);
424     warn "import over\n";
425     exit;
426     }
427    
428    
429     #
430     # POE handing part
431     #
432    
433     my $SKIPPING = 0; # if skipping, how many we've done
434     my $SEND_QUEUE; # cache
435    
436     POE::Component::IRC->new($IRC_ALIAS);
437    
438 dpavlin 34 POE::Session->create( inline_states =>
439 dpavlin 4 {_start => sub {
440 dpavlin 7 $_[KERNEL]->post($IRC_ALIAS => register => 'all');
441     $_[KERNEL]->post($IRC_ALIAS => connect => $CONNECT);
442 dpavlin 4 },
443 dpavlin 9 irc_255 => sub { # server is done blabbing
444 dpavlin 7 $_[KERNEL]->post($IRC_ALIAS => join => $CHANNEL);
445     $_[KERNEL]->post($IRC_ALIAS => join => '#logger');
446     $_[KERNEL]->yield("heartbeat"); # start heartbeat
447     # $_[KERNEL]->yield("my_add", $_) for keys %FOLLOWS;
448 dpavlin 11 $_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "IDENTIFY $NICK" );
449 dpavlin 4 },
450     irc_public => sub {
451 dpavlin 7 my $kernel = $_[KERNEL];
452     my $nick = (split /!/, $_[ARG0])[0];
453     my $channel = $_[ARG1]->[0];
454     my $msg = $_[ARG2];
455 dpavlin 4
456 dpavlin 37 save_message( channel => $channel, me => 0, nick => $nick, msg => $msg);
457 dpavlin 4 },
458 dpavlin 19 irc_ctcp_action => sub {
459     my $kernel = $_[KERNEL];
460     my $nick = (split /!/, $_[ARG0])[0];
461     my $channel = $_[ARG1]->[0];
462     my $msg = $_[ARG2];
463    
464 dpavlin 37 save_message( channel => $channel, me => 1, nick => $nick, msg => $msg);
465 dpavlin 19 },
466 dpavlin 7 irc_msg => sub {
467     my $kernel = $_[KERNEL];
468     my $nick = (split /!/, $_[ARG0])[0];
469     my $msg = $_[ARG2];
470 dpavlin 14 from_to($msg, 'UTF-8', $ENCODING);
471 dpavlin 7
472 dpavlin 8 my $res = "unknown command '$msg', try /msg $NICK help!";
473 dpavlin 11 my @out;
474 dpavlin 7
475     print "<< $msg\n";
476    
477 dpavlin 8 if ($msg =~ m/^help/i) {
478 dpavlin 7
479 dpavlin 11 $res = "usage: /msg $NICK comand | commands: stat - user/message stat | last - show backtrace | grep foobar - find foobar";
480 dpavlin 8
481 dpavlin 10 } elsif ($msg =~ m/^msg\s+(\S+)\s+(.*)$/i) {
482    
483     print ">> /msg $1 $2\n";
484     $_[KERNEL]->post( $IRC_ALIAS => privmsg => $1, $2 );
485     $res = '';
486    
487 dpavlin 8 } elsif ($msg =~ m/^stat.*?\s*(\d*)/i) {
488    
489 dpavlin 7 my $nr = $1 || 10;
490    
491     my $sth = $dbh->prepare(qq{
492 dpavlin 40 select
493     nick,
494     count(*) as count,
495     sum(length(message)) as len
496     from log
497     group by nick
498     order by len desc,count desc
499     limit $nr
500 dpavlin 7 });
501     $sth->execute();
502     $res = "Top $nr users: ";
503 dpavlin 8 my @users;
504 dpavlin 7 while (my $row = $sth->fetchrow_hashref) {
505 dpavlin 40 push @users,$row->{nick} . ': ' . $row->{count} . '/' . $row->{len} . '=' . sprintf("%.2f", $row->{len}/$row->{count});
506 dpavlin 7 }
507 dpavlin 8 $res .= join(" | ", @users);
508     } elsif ($msg =~ m/^last.*?\s*(\d*)/i) {
509    
510 dpavlin 39 foreach my $res (get_from_log( limit => ($1 || 100) )) {
511 dpavlin 11 print "last: $res\n";
512 dpavlin 14 from_to($res, $ENCODING, 'UTF-8');
513 dpavlin 11 $_[KERNEL]->post( $IRC_ALIAS => privmsg => $nick, $res );
514 dpavlin 8 }
515    
516 dpavlin 11 $res = '';
517 dpavlin 8
518 dpavlin 21 } elsif ($msg =~ m/^(search|grep)\s+(.*)\s*$/i) {
519 dpavlin 8
520 dpavlin 11 my $what = $2;
521 dpavlin 8
522 dpavlin 21 foreach my $res (get_from_log(
523     limit => 20,
524     search => $what,
525     )) {
526 dpavlin 11 print "search [$what]: $res\n";
527 dpavlin 14 from_to($res, $ENCODING, 'UTF-8');
528 dpavlin 8 $_[KERNEL]->post( $IRC_ALIAS => privmsg => $nick, $res );
529     }
530    
531     $res = '';
532 dpavlin 11
533 dpavlin 42 } elsif ($msg =~ m/^(?:count|poll)\s+(.*)(?:\s+(\d+))?\s*$/i) {
534    
535     my ($what,$limit) = ($1,$2);
536     $limit ||= 100;
537    
538     my $stat;
539    
540     foreach my $res (get_from_log(
541     limit => $limit,
542     search => $what,
543     full_rows => 1,
544     )) {
545     while ($res->{message} =~ s/\Q$what\E(\+|\-)//) {
546     $stat->{vote}->{$1}++;
547     $stat->{from}->{ $res->{nick} }++;
548     }
549     }
550    
551     my @nicks;
552     foreach my $nick (sort { $stat->{from}->{$a} cmp $stat->{from}->{$b} } keys %{ $stat->{from} }) {
553     push @nicks, $nick . $stat->{from}->{$nick} == 1 ? '' :
554     "(" . $stat->{from}->{$nick} . ")";
555     }
556    
557     $res =
558     "+ " . ( $stat->{vote}->{'+'} || 0 ) . " : " .
559     "- " . ( $stat->{vote}->{'-'} || 0 ) .
560     " from " . ( join(", ", @nicks) || 'nobody' );
561    
562 dpavlin 7 }
563    
564 dpavlin 8 if ($res) {
565     print ">> [$nick] $res\n";
566 dpavlin 14 from_to($res, $ENCODING, 'UTF-8');
567 dpavlin 8 $_[KERNEL]->post( $IRC_ALIAS => privmsg => $nick, $res );
568     }
569 dpavlin 7
570     },
571 dpavlin 10 irc_477 => sub {
572     print "# irc_477: ",$_[ARG1], "\n";
573     $_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "register $NICK" );
574     },
575 dpavlin 7 irc_505 => sub {
576 dpavlin 10 print "# irc_505: ",$_[ARG1], "\n";
577 dpavlin 7 $_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "register $NICK" );
578 dpavlin 10 # $_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "set hide email on" );
579     # $_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "set email dpavlin\@rot13.org" );
580 dpavlin 8 },
581     irc_registered => sub {
582 dpavlin 10 warn "## indetify $NICK\n";
583 dpavlin 7 $_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "IDENTIFY $NICK" );
584 dpavlin 10 },
585 dpavlin 41 irc_disconnected => sub {
586     warn "## disconnected, reconnecting again\n";
587     $_[KERNEL]->post($IRC_ALIAS => connect => $CONNECT);
588     },
589     irc_socketerr => sub {
590     warn "## socket error... sleeping for $sleep_on_error seconds and retry";
591     sleep($sleep_on_error);
592     $_[KERNEL]->post($IRC_ALIAS => connect => $CONNECT);
593     },
594 dpavlin 11 # irc_433 => sub {
595     # print "# irc_433: ",$_[ARG1], "\n";
596     # warn "## indetify $NICK\n";
597     # $_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "IDENTIFY $NICK" );
598     # },
599 dpavlin 4 _child => sub {},
600     _default => sub {
601 dpavlin 34 printf "%s #%s %s %s\n",
602     strftime($TIMESTAMP,localtime()), $_[SESSION]->ID, $_[ARG0],
603     ref($_[ARG1]) eq "ARRAY" ? join(",", map { ref($_) eq "ARRAY" ? join(";", @{$_}) : $_ } @{ $_[ARG1] }) :
604     $_[ARG1] ? $_[ARG1] :
605     "";
606 dpavlin 4 0; # false for signals
607     },
608     my_add => sub {
609     my $trailing = $_[ARG0];
610     my $session = $_[SESSION];
611     POE::Session->create
612     (inline_states =>
613     {_start => sub {
614     $_[HEAP]->{wheel} =
615     POE::Wheel::FollowTail->new
616     (
617     Filename => $FOLLOWS{$trailing},
618     InputEvent => 'got_line',
619     );
620     },
621     got_line => sub {
622     $_[KERNEL]->post($session => my_tailed =>
623     time, $trailing, $_[ARG0]);
624     },
625     },
626     );
627    
628     },
629     my_tailed => sub {
630     my ($time, $file, $line) = @_[ARG0..ARG2];
631     ## $time will be undef on a probe, or a time value if a real line
632    
633     ## PoCo::IRC has throttling built in, but no external visibility
634     ## so this is reaching "under the hood"
635     $SEND_QUEUE ||=
636     $_[KERNEL]->alias_resolve($IRC_ALIAS)->get_heap->{send_queue};
637    
638     ## handle "no need to keep skipping" transition
639     if ($SKIPPING and @$SEND_QUEUE < 1) {
640     $_[KERNEL]->post($IRC_ALIAS => privmsg => $CHANNEL =>
641     "[discarded $SKIPPING messages]");
642     $SKIPPING = 0;
643     }
644    
645     ## handle potential message display
646     if ($time) {
647     if ($SKIPPING or @$SEND_QUEUE > 3) { # 3 msgs per 10 seconds
648     $SKIPPING++;
649     } else {
650     my @time = localtime $time;
651     $_[KERNEL]->post($IRC_ALIAS => privmsg => $CHANNEL =>
652     sprintf "%02d:%02d:%02d: %s: %s",
653     ($time[2] + 11) % 12 + 1, $time[1], $time[0],
654     $file, $line);
655     }
656     }
657    
658     ## handle re-probe/flush if skipping
659     if ($SKIPPING) {
660     $_[KERNEL]->delay($_[STATE] => 0.5); # $time will be undef
661     }
662    
663     },
664     my_heartbeat => sub {
665     $_[KERNEL]->yield(my_tailed => time, "heartbeat", "beep");
666     $_[KERNEL]->delay($_[STATE] => 10);
667     }
668     },
669     );
670    
671 dpavlin 13 # http server
672    
673     my $httpd = POE::Component::Server::HTTP->new(
674 dpavlin 14 Port => $NICK =~ m/-dev/ ? 8001 : 8000,
675 dpavlin 13 ContentHandler => { '/' => \&root_handler },
676     Headers => { Server => 'irc-logger' },
677     );
678    
679 dpavlin 18 my %escape = ('<'=>'&lt;', '>'=>'&gt;', '&'=>'&amp;', '"'=>'&quot;');
680     my $escape_re = join '|' => keys %escape;
681    
682 dpavlin 13 my $style = <<'_END_OF_STYLE_';
683 dpavlin 16 p { margin: 0; padding: 0.1em; }
684 dpavlin 13 .time, .channel { color: #808080; font-size: 60%; }
685 dpavlin 28 .date { float: right; background: #e0e0e0; color: #404040; font-size: 120%; padding: 0.25em; border: 1px dashed #808080; }
686 dpavlin 20 .nick { color: #000000; font-size: 80%; padding: 2px; font-family: courier, courier new, monospace ; }
687 dpavlin 13 .message { color: #000000; font-size: 100%; }
688 dpavlin 16 .search { float: right; }
689 dpavlin 20 .col-0 { background: #ffff66 }
690     .col-1 { background: #a0ffff }
691     .col-2 { background: #99ff99 }
692     .col-3 { background: #ff9999 }
693     .col-4 { background: #ff66ff }
694 dpavlin 31 a:link.tag, a:visited.tag { border: 1px dashed #ccc; backgound: #ccc; text-decoration: none }
695     a:hover.tag { border: 1px solid #eee }
696 dpavlin 35 hr { border: 1px dashed #ccc; height: 1px; clear: both; }
697 dpavlin 13 _END_OF_STYLE_
698    
699 dpavlin 20 my $max_color = 4;
700    
701     my %nick_enumerator;
702    
703 dpavlin 13 sub root_handler {
704     my ($request, $response) = @_;
705     $response->code(RC_OK);
706 dpavlin 14 $response->content_type("text/html; charset=$ENCODING");
707 dpavlin 16
708     my $q;
709    
710     if ( $request->method eq 'POST' ) {
711     $q = new CGI::Simple( $request->content );
712     } elsif ( $request->uri =~ /\?(.+)$/ ) {
713     $q = new CGI::Simple( $1 );
714     } else {
715     $q = new CGI::Simple;
716     }
717    
718     my $search = $q->param('search') || $q->param('grep') || '';
719    
720 dpavlin 35 my $html =
721 dpavlin 28 qq{<html><head><title>$NICK</title><style type="text/css">$style} .
722     $cloud->css .
723     qq{</style></head><body>} .
724     qq{
725 dpavlin 32 <form method="post" class="search" action="/">
726 dpavlin 16 <input type="text" name="search" value="$search" size="10">
727     <input type="submit" value="search">
728     </form>
729     } .
730 dpavlin 31 $cloud->html(500) .
731 dpavlin 35 qq{<p>};
732     if ($request->url =~ m#/history#) {
733     my $sth = $dbh->prepare(qq{
734     select date(time) as date,count(*) as nr
735     from log
736     group by date(time)
737     order by date(time) desc
738     });
739     $sth->execute();
740     my ($l_yyyy,$l_mm) = (0,0);
741     my $cal;
742     while (my $row = $sth->fetchrow_hashref) {
743     # this is probably PostgreSQL specific, expects ISO date
744     my ($yyyy,$mm,$dd) = split(/-/, $row->{date});
745     if ($yyyy != $l_yyyy || $mm != $l_mm) {
746     $html .= $cal->as_HTML() if ($cal);
747     $cal = new HTML::CalendarMonthSimple('month'=>$mm,'year'=>$yyyy);
748     $cal->border(2);
749     ($l_yyyy,$l_mm) = ($yyyy,$mm);
750     }
751     $cal->setcontent($dd, qq{
752     <a href="/?date=$row->{date}">$row->{nr}</a>
753     });
754     }
755     $html .= $cal->as_HTML() if ($cal);
756    
757     } else {
758     $html .= join("</p><p>",
759 dpavlin 13 get_from_log(
760 dpavlin 35 limit => $q->param('last') || $q->param('date') ? undef : 100,
761 dpavlin 28 search => $search || undef,
762 dpavlin 29 tag => $q->param('tag') || undef,
763 dpavlin 35 date => $q->param('date') || undef,
764 dpavlin 13 fmt => {
765 dpavlin 35 date => sub {
766     my $date = shift || return;
767 dpavlin 37 qq{<hr/><div class="date"><a href="/?date=$date">$date</a></div>};
768 dpavlin 35 },
769 dpavlin 13 time => '<span class="time">%s</span> ',
770     time_channel => '<span class="channel">%s %s</span> ',
771 dpavlin 20 nick => '%s:&nbsp;',
772     me_nick => '***%s&nbsp;',
773 dpavlin 13 message => '<span class="message">%s</span>',
774     },
775 dpavlin 20 filter => {
776     message => sub {
777     my $m = shift || return;
778     $m =~ s/($escape_re)/$escape{$1}/gs;
779     $m =~ s#($RE{URI}{HTTP})#<a href="$1">$1</a>#gs;
780 dpavlin 31 $m =~ s#$tag_regex#<a href="?tag=$1" class="tag">$1</a>#g;
781 dpavlin 20 return $m;
782     },
783     nick => sub {
784     my $n = shift || return;
785     if (! $nick_enumerator{$n}) {
786     my $max = scalar keys %nick_enumerator;
787     $nick_enumerator{$n} = $max + 1;
788     }
789     return '<span class="nick col-' .
790     ( $nick_enumerator{$n} % $max_color ) .
791     '">' . $n . '</span>';
792     },
793 dpavlin 15 },
794 dpavlin 13 )
795 dpavlin 35 );
796     }
797    
798     $html .= qq{</p>
799     <hr/>
800     <p>See <a href="/history">history</a> of all messages.</p>
801     </body></html>};
802    
803     $response->content( $html );
804 dpavlin 13 return RC_OK;
805     }
806    
807 dpavlin 4 POE::Kernel->run;

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26