/[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 37 - (hide annotations)
Sun Jun 25 17:40:59 2006 UTC (14 years, 7 months ago) by dpavlin
Original Path: trunk/irc-logger.pl
File MIME type: text/plain
File size: 18341 byte(s)
re-structured code and wrote --import-dircproxy to import logfiles from dircproxy

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

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26