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 |
50 |
my $ENCODING = 'ISO-8859-2'; |
my $ENCODING = 'ISO-8859-2'; |
51 |
my $TIMESTAMP = '%Y-%m-%d %H:%M:%S'; |
my $TIMESTAMP = '%Y-%m-%d %H:%M:%S'; |
52 |
|
|
53 |
|
my $sleep_on_error = 5; |
54 |
|
|
55 |
## END CONFIG |
## END CONFIG |
56 |
|
|
57 |
|
|
65 |
use HTML::TagCloud; |
use HTML::TagCloud; |
66 |
use POSIX qw/strftime/; |
use POSIX qw/strftime/; |
67 |
use HTML::CalendarMonthSimple; |
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 { |
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 |
); |
); |
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 |
309 |
return @msgs; |
return @msgs; |
310 |
} |
} |
311 |
|
|
312 |
|
# tags support |
313 |
|
|
314 |
my $SKIPPING = 0; # if skipping, how many we've done |
my $cloud = HTML::TagCloud->new; |
315 |
my $SEND_QUEUE; # cache |
|
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 |
|
|
|
POE::Component::IRC->new($IRC_ALIAS); |
|
359 |
|
|
360 |
=head2 save_message |
=head2 save_message |
361 |
|
|
362 |
save_message($channel,$me,$nick,$msg); |
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 |
=cut |
375 |
|
|
376 |
sub save_message { |
sub save_message { |
377 |
my ($channel,$me,$nick,$msg) = @_; |
my $a = {@_}; |
378 |
$me ||= 0; |
$a->{me} ||= 0; |
379 |
$sth->execute($channel, $me, $nick, $msg); |
$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), |
add_tag( id => $dbh->last_insert_id(undef,undef,"log",undef), |
391 |
message => $msg); |
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 |
434 |
|
my $SEND_QUEUE; # cache |
435 |
|
|
436 |
|
POE::Component::IRC->new($IRC_ALIAS); |
437 |
|
|
438 |
POE::Session->create( inline_states => |
POE::Session->create( inline_states => |
439 |
{_start => sub { |
{_start => sub { |
440 |
$_[KERNEL]->post($IRC_ALIAS => register => 'all'); |
$_[KERNEL]->post($IRC_ALIAS => register => 'all'); |
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"; |
|
|
save_message($channel, 0, $nick, $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"; |
|
|
save_message($channel, 1, $nick, $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"; |
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( |
764 |
fmt => { |
fmt => { |
765 |
date => sub { |
date => sub { |
766 |
my $date = shift || return; |
my $date = shift || return; |
767 |
qq{<hr/><div class="date"><a href="/?date=$date">$date</a></div> '}; |
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> ', |