/[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 36 - (hide annotations)
Sun Jun 25 16:37:39 2006 UTC (14 years, 7 months ago) by dpavlin
Original Path: trunk/irc-logger.pl
File MIME type: text/plain
File size: 16971 byte(s)
added save_message which saves to db and adds tags

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

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26