10 |
|
|
11 |
./irc-logger.pl |
./irc-logger.pl |
12 |
|
|
13 |
|
=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 |
=head1 DESCRIPTION |
=head1 DESCRIPTION |
22 |
|
|
23 |
log all conversation on irc channel |
log all conversation on irc channel |
26 |
|
|
27 |
## CONFIG |
## CONFIG |
28 |
|
|
29 |
|
my $HOSTNAME = `hostname`; |
30 |
|
|
31 |
my $NICK = 'irc-logger'; |
my $NICK = 'irc-logger'; |
32 |
|
$NICK .= '-dev' if ($HOSTNAME =~ m/llin/); |
33 |
my $CONNECT = |
my $CONNECT = |
34 |
{Server => 'irc.freenode.net', |
{Server => 'irc.freenode.net', |
35 |
Nick => $NICK, |
Nick => $NICK, |
36 |
Ircname => "try /msg $NICK help", |
Ircname => "try /msg $NICK help", |
37 |
}; |
}; |
38 |
my $CHANNEL = '#razmjenavjestina'; |
my $CHANNEL = '#razmjenavjestina'; |
39 |
|
$CHANNEL = '#irc-logger' if ($HOSTNAME =~ m/llin/); |
40 |
my $IRC_ALIAS = "log"; |
my $IRC_ALIAS = "log"; |
41 |
|
|
42 |
my %FOLLOWS = |
my %FOLLOWS = |
48 |
my $DSN = 'DBI:Pg:dbname=' . $NICK; |
my $DSN = 'DBI:Pg:dbname=' . $NICK; |
49 |
|
|
50 |
my $ENCODING = 'ISO-8859-2'; |
my $ENCODING = 'ISO-8859-2'; |
51 |
|
my $TIMESTAMP = '%Y-%m-%d %H:%M:%S'; |
52 |
|
|
53 |
|
my $sleep_on_error = 5; |
54 |
|
|
55 |
## END CONFIG |
## END CONFIG |
56 |
|
|
63 |
use Regexp::Common qw /URI/; |
use Regexp::Common qw /URI/; |
64 |
use CGI::Simple; |
use CGI::Simple; |
65 |
use HTML::TagCloud; |
use HTML::TagCloud; |
66 |
|
use POSIX qw/strftime/; |
67 |
|
use HTML::CalendarMonthSimple; |
68 |
|
use Getopt::Long; |
69 |
|
use DateTime; |
70 |
|
|
71 |
|
my $import_dircproxy; |
72 |
|
GetOptions( |
73 |
|
'import-dircproxy:s' => \$import_dircproxy, |
74 |
|
); |
75 |
|
|
76 |
my $dbh = DBI->connect($DSN,"","", { RaiseError => 1, AutoCommit => 1 }) || die $DBI::errstr; |
my $dbh = DBI->connect($DSN,"","", { RaiseError => 1, AutoCommit => 1 }) || die $DBI::errstr; |
77 |
|
|
102 |
|
|
103 |
my $sth = $dbh->prepare(qq{ |
my $sth = $dbh->prepare(qq{ |
104 |
insert into log |
insert into log |
105 |
(channel, me, nick, message) |
(channel, me, nick, message, time) |
106 |
values (?,?,?,?) |
values (?,?,?,?,?) |
107 |
}); |
}); |
108 |
|
|
109 |
my $tags; |
my $tags; |
128 |
} |
} |
129 |
}, |
}, |
130 |
context => 5, |
context => 5, |
131 |
|
full_rows => 1, |
132 |
); |
); |
133 |
|
|
134 |
Order is important. Fields are first passed through C<filter> (if available) and |
Order is important. Fields are first passed through C<filter> (if available) and |
136 |
|
|
137 |
C<context> defines number of messages around each search hit for display. |
C<context> defines number of messages around each search hit for display. |
138 |
|
|
139 |
|
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 |
=cut |
=cut |
143 |
|
|
144 |
sub get_from_log { |
sub get_from_log { |
145 |
my $args = {@_}; |
my $args = {@_}; |
146 |
|
|
|
$args->{limit} ||= 10; |
|
|
|
|
147 |
$args->{fmt} ||= { |
$args->{fmt} ||= { |
148 |
date => '[%s] ', |
date => '[%s] ', |
149 |
time => '{%s} ', |
time => '{%s} ', |
176 |
|
|
177 |
$sql .= " where message ilike ? or nick ilike ? " if ($args->{search}); |
$sql .= " where message ilike ? or nick ilike ? " if ($args->{search}); |
178 |
$sql .= " where id in (" . join(",", @{ $tags->{ $args->{tag} } }) . ") " if ($args->{tag} && $tags->{ $args->{tag} }); |
$sql .= " where id in (" . join(",", @{ $tags->{ $args->{tag} } }) . ") " if ($args->{tag} && $tags->{ $args->{tag} }); |
179 |
|
$sql .= " where date(time) = ? " if ($args->{date}); |
180 |
$sql .= " order by log.time desc"; |
$sql .= " order by log.time desc"; |
181 |
$sql .= " limit " . $args->{limit}; |
$sql .= " limit " . $args->{limit} if ($args->{limit}); |
182 |
|
|
183 |
my $sth = $dbh->prepare( $sql ); |
my $sth = $dbh->prepare( $sql ); |
184 |
if (my $search = $args->{search}) { |
if (my $search = $args->{search}) { |
189 |
} elsif (my $tag = $args->{tag}) { |
} elsif (my $tag = $args->{tag}) { |
190 |
$sth->execute(); |
$sth->execute(); |
191 |
warn "tag '$tag' returned ", $sth->rows, " results ", $context || '', "\n"; |
warn "tag '$tag' returned ", $sth->rows, " results ", $context || '', "\n"; |
192 |
|
} elsif (my $date = $args->{date}) { |
193 |
|
$sth->execute($date); |
194 |
|
warn "found ", $sth->rows, " messages for date $date ", $context || '', "\n"; |
195 |
} else { |
} else { |
196 |
$sth->execute(); |
$sth->execute(); |
197 |
} |
} |
208 |
unshift @rows, $row; |
unshift @rows, $row; |
209 |
} |
} |
210 |
|
|
211 |
|
# normalize nick names |
212 |
|
map { |
213 |
|
$_->{nick} =~ s/^_*(.*?)_*$/$1/ |
214 |
|
} @rows; |
215 |
|
|
216 |
|
return @rows if ($args->{full_rows}); |
217 |
|
|
218 |
my @msgs = ( |
my @msgs = ( |
219 |
"Showing " . ($#rows + 1) . " messages..." |
"Showing " . ($#rows + 1) . " messages..." |
220 |
); |
); |
243 |
} |
} |
244 |
} |
} |
245 |
|
|
246 |
|
# 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 |
foreach my $row (@rows) { |
foreach my $row (@rows) { |
257 |
|
|
258 |
$row->{time} =~ s#\.\d+##; |
$row->{time} =~ s#\.\d+##; |
259 |
|
|
260 |
my $msg = ''; |
my $msg = ''; |
261 |
|
|
262 |
$msg = sprintf($args->{fmt}->{date}, $row->{date}) . ' ' if ($last_row->{date} ne $row->{date}); |
$msg = cr_sprintf($args->{fmt}->{date}, $row->{date}) . ' ' if ($last_row->{date} ne $row->{date}); |
263 |
my $t = $row->{time}; |
my $t = $row->{time}; |
264 |
|
|
265 |
if ($last_row->{channel} ne $row->{channel}) { |
if ($last_row->{channel} ne $row->{channel}) { |
266 |
$msg .= sprintf($args->{fmt}->{time_channel}, $t, $row->{channel}); |
$msg .= cr_sprintf($args->{fmt}->{time_channel}, $t, $row->{channel}); |
267 |
} else { |
} else { |
268 |
$msg .= sprintf($args->{fmt}->{time}, $t); |
$msg .= cr_sprintf($args->{fmt}->{time}, $t); |
269 |
} |
} |
270 |
|
|
271 |
my $append = 1; |
my $append = 1; |
272 |
|
|
273 |
my $nick = $row->{nick}; |
my $nick = $row->{nick}; |
274 |
if ($nick =~ s/^_*(.*?)_*$/$1/) { |
# if ($nick =~ s/^_*(.*?)_*$/$1/) { |
275 |
$row->{nick} = $nick; |
# $row->{nick} = $nick; |
276 |
} |
# } |
277 |
|
|
278 |
if ($last_row->{nick} ne $nick) { |
if ($last_row->{nick} ne $nick) { |
279 |
# obfu way to find format for me_nick if needed or fallback to default |
# obfu way to find format for me_nick if needed or fallback to default |
282 |
|
|
283 |
$nick = $args->{filter}->{nick}->($nick) if (ref($args->{filter}->{nick}) eq 'CODE'); |
$nick = $args->{filter}->{nick}->($nick) if (ref($args->{filter}->{nick}) eq 'CODE'); |
284 |
|
|
285 |
$msg .= sprintf( $fmt, $nick ); |
$msg .= cr_sprintf( $fmt, $nick ); |
286 |
$append = 0; |
$append = 0; |
287 |
} |
} |
288 |
|
|
289 |
$args->{fmt}->{message} ||= '%s'; |
$args->{fmt}->{message} ||= '%s'; |
290 |
if (ref($args->{filter}->{message}) eq 'CODE') { |
if (ref($args->{filter}->{message}) eq 'CODE') { |
291 |
$msg .= sprintf($args->{fmt}->{message}, |
$msg .= cr_sprintf($args->{fmt}->{message}, |
292 |
$args->{filter}->{message}->( |
$args->{filter}->{message}->( |
293 |
$row->{message} |
$row->{message} |
294 |
) |
) |
295 |
); |
); |
296 |
} else { |
} else { |
297 |
$msg .= sprintf($args->{fmt}->{message}, $row->{message}); |
$msg .= cr_sprintf($args->{fmt}->{message}, $row->{message}); |
298 |
} |
} |
299 |
|
|
300 |
if ($append && @msgs) { |
if ($append && @msgs) { |
309 |
return @msgs; |
return @msgs; |
310 |
} |
} |
311 |
|
|
312 |
|
# tags support |
313 |
|
|
314 |
|
my $cloud = HTML::TagCloud->new; |
315 |
|
|
316 |
|
=head2 add_tag |
317 |
|
|
318 |
|
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 |
|
=head2 save_message |
361 |
|
|
362 |
|
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 |
|
|
370 |
|
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 |
|
=cut |
375 |
|
|
376 |
|
sub save_message { |
377 |
|
my $a = {@_}; |
378 |
|
$a->{me} ||= 0; |
379 |
|
$a->{time} ||= strftime($TIMESTAMP,localtime()); |
380 |
|
|
381 |
|
print |
382 |
|
$a->{time}, " ", |
383 |
|
$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 |
|
add_tag( id => $dbh->last_insert_id(undef,undef,"log",undef), |
391 |
|
message => $a->{msg}); |
392 |
|
} |
393 |
|
|
394 |
|
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 |
my $SKIPPING = 0; # if skipping, how many we've done |
434 |
my $SEND_QUEUE; # cache |
my $SEND_QUEUE; # cache |
435 |
|
|
436 |
POE::Component::IRC->new($IRC_ALIAS); |
POE::Component::IRC->new($IRC_ALIAS); |
437 |
|
|
438 |
POE::Session->create |
POE::Session->create( inline_states => |
|
(inline_states => |
|
439 |
{_start => sub { |
{_start => sub { |
440 |
$_[KERNEL]->post($IRC_ALIAS => register => 'all'); |
$_[KERNEL]->post($IRC_ALIAS => register => 'all'); |
441 |
$_[KERNEL]->post($IRC_ALIAS => connect => $CONNECT); |
$_[KERNEL]->post($IRC_ALIAS => connect => $CONNECT); |
453 |
my $channel = $_[ARG1]->[0]; |
my $channel = $_[ARG1]->[0]; |
454 |
my $msg = $_[ARG2]; |
my $msg = $_[ARG2]; |
455 |
|
|
456 |
from_to($msg, 'UTF-8', $ENCODING); |
save_message( channel => $channel, me => 0, nick => $nick, msg => $msg); |
|
|
|
|
print "$channel: <$nick> $msg\n"; |
|
|
$sth->execute($channel, 0, $nick, $msg); |
|
|
add_tag( id => $dbh->last_insert_id(undef,undef,"log",undef), |
|
|
message => $msg); |
|
457 |
}, |
}, |
458 |
irc_ctcp_action => sub { |
irc_ctcp_action => sub { |
459 |
my $kernel = $_[KERNEL]; |
my $kernel = $_[KERNEL]; |
461 |
my $channel = $_[ARG1]->[0]; |
my $channel = $_[ARG1]->[0]; |
462 |
my $msg = $_[ARG2]; |
my $msg = $_[ARG2]; |
463 |
|
|
464 |
from_to($msg, 'UTF-8', $ENCODING); |
save_message( channel => $channel, me => 1, nick => $nick, msg => $msg); |
|
|
|
|
print "$channel ***$nick $msg\n"; |
|
|
$sth->execute($channel, 1, $nick, $msg); |
|
|
add_tag( id => $dbh->last_insert_id(undef,undef,"log",undef), |
|
|
message => $msg); |
|
465 |
}, |
}, |
466 |
irc_msg => sub { |
irc_msg => sub { |
467 |
my $kernel = $_[KERNEL]; |
my $kernel = $_[KERNEL]; |
489 |
my $nr = $1 || 10; |
my $nr = $1 || 10; |
490 |
|
|
491 |
my $sth = $dbh->prepare(qq{ |
my $sth = $dbh->prepare(qq{ |
492 |
select nick,count(*) from log group by nick order by count desc limit $nr |
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 |
}); |
}); |
501 |
$sth->execute(); |
$sth->execute(); |
502 |
$res = "Top $nr users: "; |
$res = "Top $nr users: "; |
503 |
my @users; |
my @users; |
504 |
while (my $row = $sth->fetchrow_hashref) { |
while (my $row = $sth->fetchrow_hashref) { |
505 |
push @users,$row->{nick} . ': ' . $row->{count}; |
push @users,$row->{nick} . ': ' . $row->{count} . '/' . $row->{len} . '=' . sprintf("%.2f", $row->{len}/$row->{count}); |
506 |
} |
} |
507 |
$res .= join(" | ", @users); |
$res .= join(" | ", @users); |
508 |
} elsif ($msg =~ m/^last.*?\s*(\d*)/i) { |
} elsif ($msg =~ m/^last.*?\s*(\d*)/i) { |
509 |
|
|
510 |
foreach my $res (get_from_log( limit => $1 )) { |
foreach my $res (get_from_log( limit => ($1 || 100) )) { |
511 |
print "last: $res\n"; |
print "last: $res\n"; |
512 |
from_to($res, $ENCODING, 'UTF-8'); |
from_to($res, $ENCODING, 'UTF-8'); |
513 |
$_[KERNEL]->post( $IRC_ALIAS => privmsg => $nick, $res ); |
$_[KERNEL]->post( $IRC_ALIAS => privmsg => $nick, $res ); |
530 |
|
|
531 |
$res = ''; |
$res = ''; |
532 |
|
|
533 |
|
} 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 |
} |
} |
563 |
|
|
564 |
if ($res) { |
if ($res) { |
582 |
warn "## indetify $NICK\n"; |
warn "## indetify $NICK\n"; |
583 |
$_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "IDENTIFY $NICK" ); |
$_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "IDENTIFY $NICK" ); |
584 |
}, |
}, |
585 |
|
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 |
# irc_433 => sub { |
# irc_433 => sub { |
595 |
# print "# irc_433: ",$_[ARG1], "\n"; |
# print "# irc_433: ",$_[ARG1], "\n"; |
596 |
# warn "## indetify $NICK\n"; |
# warn "## indetify $NICK\n"; |
597 |
# $_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "IDENTIFY $NICK" ); |
# $_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "IDENTIFY $NICK" ); |
598 |
# }, |
# }, |
|
irc_372 => sub { |
|
|
print "MOTD: ", $_[ARG1], "\n"; |
|
|
}, |
|
|
irc_snotice => sub { |
|
|
print "(server notice): ", $_[ARG0], "\n"; |
|
|
}, |
|
|
(map |
|
|
{ |
|
|
;"irc_$_" => sub { }} |
|
|
qw( |
|
|
)), |
|
|
# join |
|
|
# 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 |
|
599 |
_child => sub {}, |
_child => sub {}, |
600 |
_default => sub { |
_default => sub { |
601 |
printf "%s: session %s caught an unhandled %s event.\n", |
printf "%s #%s %s %s\n", |
602 |
scalar localtime(), $_[SESSION]->ID, $_[ARG0]; |
strftime($TIMESTAMP,localtime()), $_[SESSION]->ID, $_[ARG0], |
603 |
print "The $_[ARG0] event was given these parameters: ", |
ref($_[ARG1]) eq "ARRAY" ? join(",", map { ref($_) eq "ARRAY" ? join(";", @{$_}) : $_ } @{ $_[ARG1] }) : |
604 |
join(" ", map({"ARRAY" eq ref $_ ? "[@$_]" : "$_"} @{$_[ARG1]})), "\n"; |
$_[ARG1] ? $_[ARG1] : |
605 |
|
""; |
606 |
0; # false for signals |
0; # false for signals |
607 |
}, |
}, |
608 |
my_add => sub { |
my_add => sub { |
668 |
}, |
}, |
669 |
); |
); |
670 |
|
|
|
# tags support |
|
|
|
|
|
my $cloud = HTML::TagCloud->new; |
|
|
|
|
|
=head2 add_tag |
|
|
|
|
|
add_tag( id => 42, message => 'irc message' ); |
|
|
|
|
|
=cut |
|
|
|
|
|
sub add_tag { |
|
|
my $arg = {@_}; |
|
|
|
|
|
return unless ($arg->{id} && $arg->{message}); |
|
|
|
|
|
my $m = $arg->{message}; |
|
|
from_to('UTF-8', 'iso-8859-2', $m) if (is_utf8($m)); |
|
|
|
|
|
while ($m =~ s#$tag_regex##s) { |
|
|
my $tag = $1; |
|
|
next if (! $tag || $tag =~ m/https?:/i); |
|
|
push @{ $tags->{$tag} }, $arg->{id}; |
|
|
warn "+tag $tag: $arg->{id}\n"; |
|
|
$cloud->add($tag, "?tag=$tag", scalar @{$tags->{$tag}} + 1); |
|
|
} |
|
|
} |
|
|
|
|
|
=head2 seed_tags |
|
|
|
|
|
Read all tags from database and create in-memory cache for tags |
|
|
|
|
|
=cut |
|
|
|
|
|
sub seed_tags { |
|
|
my $sth = $dbh->prepare(qq{ select id,message from log where message like '%//%' }); |
|
|
$sth->execute; |
|
|
while (my $row = $sth->fetchrow_hashref) { |
|
|
add_tag( %$row ); |
|
|
} |
|
|
|
|
|
foreach my $tag (keys %$tags) { |
|
|
$cloud->add($tag, "?tag=$tag", scalar @{$tags->{$tag}} + 1); |
|
|
} |
|
|
} |
|
|
|
|
|
seed_tags; |
|
|
|
|
671 |
# http server |
# http server |
672 |
|
|
673 |
my $httpd = POE::Component::Server::HTTP->new( |
my $httpd = POE::Component::Server::HTTP->new( |
693 |
.col-4 { background: #ff66ff } |
.col-4 { background: #ff66ff } |
694 |
a:link.tag, a:visited.tag { border: 1px dashed #ccc; backgound: #ccc; text-decoration: none } |
a:link.tag, a:visited.tag { border: 1px dashed #ccc; backgound: #ccc; text-decoration: none } |
695 |
a:hover.tag { border: 1px solid #eee } |
a:hover.tag { border: 1px solid #eee } |
696 |
|
hr { border: 1px dashed #ccc; height: 1px; clear: both; } |
697 |
_END_OF_STYLE_ |
_END_OF_STYLE_ |
698 |
|
|
699 |
my $max_color = 4; |
my $max_color = 4; |
717 |
|
|
718 |
my $search = $q->param('search') || $q->param('grep') || ''; |
my $search = $q->param('search') || $q->param('grep') || ''; |
719 |
|
|
720 |
$response->content( |
my $html = |
721 |
qq{<html><head><title>$NICK</title><style type="text/css">$style} . |
qq{<html><head><title>$NICK</title><style type="text/css">$style} . |
722 |
$cloud->css . |
$cloud->css . |
723 |
qq{</style></head><body>} . |
qq{</style></head><body>} . |
728 |
</form> |
</form> |
729 |
} . |
} . |
730 |
$cloud->html(500) . |
$cloud->html(500) . |
731 |
qq{<p>} . |
qq{<p>}; |
732 |
join("</p><p>", |
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 |
get_from_log( |
get_from_log( |
760 |
limit => $q->param('last') || 100, |
limit => $q->param('last') || $q->param('date') ? undef : 100, |
761 |
search => $search || undef, |
search => $search || undef, |
762 |
tag => $q->param('tag') || undef, |
tag => $q->param('tag') || undef, |
763 |
|
date => $q->param('date') || undef, |
764 |
fmt => { |
fmt => { |
765 |
date => '<hr size="1" style="clear: both;"/><div class="date">%s</div> ', |
date => sub { |
766 |
|
my $date = shift || return; |
767 |
|
qq{<hr/><div class="date"><a href="/?date=$date">$date</a></div>}; |
768 |
|
}, |
769 |
time => '<span class="time">%s</span> ', |
time => '<span class="time">%s</span> ', |
770 |
time_channel => '<span class="channel">%s %s</span> ', |
time_channel => '<span class="channel">%s %s</span> ', |
771 |
nick => '%s: ', |
nick => '%s: ', |
792 |
}, |
}, |
793 |
}, |
}, |
794 |
) |
) |
795 |
) . |
); |
796 |
qq{</p></body></html>} |
} |
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 |
return RC_OK; |
return RC_OK; |
805 |
} |
} |
806 |
|
|