18 |
|
|
19 |
## CONFIG |
## CONFIG |
20 |
|
|
21 |
|
my $HOSTNAME = `hostname`; |
22 |
|
|
23 |
my $NICK = 'irc-logger'; |
my $NICK = 'irc-logger'; |
24 |
|
$NICK .= '-dev' if ($HOSTNAME =~ m/llin/); |
25 |
my $CONNECT = |
my $CONNECT = |
26 |
{Server => 'irc.freenode.net', |
{Server => 'irc.freenode.net', |
27 |
Nick => $NICK, |
Nick => $NICK, |
28 |
Ircname => "try /msg $NICK help", |
Ircname => "try /msg $NICK help", |
29 |
}; |
}; |
30 |
my $CHANNEL = '#razmjenavjestina'; |
my $CHANNEL = '#razmjenavjestina'; |
31 |
|
$CHANNEL = '#irc-logger' if ($HOSTNAME =~ m/llin/); |
32 |
my $IRC_ALIAS = "log"; |
my $IRC_ALIAS = "log"; |
33 |
|
|
34 |
my %FOLLOWS = |
my %FOLLOWS = |
40 |
my $DSN = 'DBI:Pg:dbname=' . $NICK; |
my $DSN = 'DBI:Pg:dbname=' . $NICK; |
41 |
|
|
42 |
my $ENCODING = 'ISO-8859-2'; |
my $ENCODING = 'ISO-8859-2'; |
43 |
|
my $TIMESTAMP = '%Y-%m-%d %H:%M:%S'; |
44 |
|
|
45 |
## END CONFIG |
## END CONFIG |
46 |
|
|
49 |
use POE qw(Component::IRC Wheel::FollowTail Component::Server::HTTP); |
use POE qw(Component::IRC Wheel::FollowTail Component::Server::HTTP); |
50 |
use HTTP::Status; |
use HTTP::Status; |
51 |
use DBI; |
use DBI; |
52 |
use Encode qw/from_to/; |
use Encode qw/from_to is_utf8/; |
53 |
use Regexp::Common qw /URI/; |
use Regexp::Common qw /URI/; |
54 |
use CGI::Simple; |
use CGI::Simple; |
55 |
|
use HTML::TagCloud; |
56 |
|
use POSIX qw/strftime/; |
57 |
|
|
58 |
my $dbh = DBI->connect($DSN,"","", { RaiseError => 1, AutoCommit => 1 }) || die $DBI::errstr; |
my $dbh = DBI->connect($DSN,"","", { RaiseError => 1, AutoCommit => 1 }) || die $DBI::errstr; |
59 |
|
|
88 |
values (?,?,?,?) |
values (?,?,?,?) |
89 |
}); |
}); |
90 |
|
|
91 |
|
my $tags; |
92 |
|
my $tag_regex = '\b([\w-_]+)//'; |
93 |
|
|
94 |
=head2 get_from_log |
=head2 get_from_log |
95 |
|
|
96 |
my @messages = get_from_log( |
my @messages = get_from_log( |
155 |
my $sql = $context ? $sql_context : $sql_message; |
my $sql = $context ? $sql_context : $sql_message; |
156 |
|
|
157 |
$sql .= " where message ilike ? or nick ilike ? " if ($args->{search}); |
$sql .= " where message ilike ? or nick ilike ? " if ($args->{search}); |
158 |
|
$sql .= " where id in (" . join(",", @{ $tags->{ $args->{tag} } }) . ") " if ($args->{tag} && $tags->{ $args->{tag} }); |
159 |
$sql .= " order by log.time desc"; |
$sql .= " order by log.time desc"; |
160 |
$sql .= " limit " . $args->{limit}; |
$sql .= " limit " . $args->{limit}; |
161 |
|
|
165 |
$search =~ s/\s+$//; |
$search =~ s/\s+$//; |
166 |
$sth->execute( ( '%' . $search . '%' ) x 2 ); |
$sth->execute( ( '%' . $search . '%' ) x 2 ); |
167 |
warn "search for '$search' returned ", $sth->rows, " results ", $context || '', "\n"; |
warn "search for '$search' returned ", $sth->rows, " results ", $context || '', "\n"; |
168 |
|
} elsif (my $tag = $args->{tag}) { |
169 |
|
$sth->execute(); |
170 |
|
warn "tag '$tag' returned ", $sth->rows, " results ", $context || '', "\n"; |
171 |
} else { |
} else { |
172 |
$sth->execute(); |
$sth->execute(); |
173 |
} |
} |
274 |
|
|
275 |
POE::Component::IRC->new($IRC_ALIAS); |
POE::Component::IRC->new($IRC_ALIAS); |
276 |
|
|
277 |
POE::Session->create |
POE::Session->create( inline_states => |
|
(inline_states => |
|
278 |
{_start => sub { |
{_start => sub { |
279 |
$_[KERNEL]->post($IRC_ALIAS => register => 'all'); |
$_[KERNEL]->post($IRC_ALIAS => register => 'all'); |
280 |
$_[KERNEL]->post($IRC_ALIAS => connect => $CONNECT); |
$_[KERNEL]->post($IRC_ALIAS => connect => $CONNECT); |
296 |
|
|
297 |
print "$channel: <$nick> $msg\n"; |
print "$channel: <$nick> $msg\n"; |
298 |
$sth->execute($channel, 0, $nick, $msg); |
$sth->execute($channel, 0, $nick, $msg); |
299 |
|
add_tag( id => $dbh->last_insert_id(undef,undef,"log",undef), |
300 |
|
message => $msg); |
301 |
}, |
}, |
302 |
irc_ctcp_action => sub { |
irc_ctcp_action => sub { |
303 |
my $kernel = $_[KERNEL]; |
my $kernel = $_[KERNEL]; |
309 |
|
|
310 |
print "$channel ***$nick $msg\n"; |
print "$channel ***$nick $msg\n"; |
311 |
$sth->execute($channel, 1, $nick, $msg); |
$sth->execute($channel, 1, $nick, $msg); |
312 |
|
add_tag( id => $dbh->last_insert_id(undef,undef,"log",undef), |
313 |
|
message => $msg); |
314 |
}, |
}, |
315 |
irc_msg => sub { |
irc_msg => sub { |
316 |
my $kernel = $_[KERNEL]; |
my $kernel = $_[KERNEL]; |
400 |
# warn "## indetify $NICK\n"; |
# warn "## indetify $NICK\n"; |
401 |
# $_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "IDENTIFY $NICK" ); |
# $_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "IDENTIFY $NICK" ); |
402 |
# }, |
# }, |
|
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 |
|
403 |
_child => sub {}, |
_child => sub {}, |
404 |
_default => sub { |
_default => sub { |
405 |
printf "%s: session %s caught an unhandled %s event.\n", |
printf "%s #%s %s %s\n", |
406 |
scalar localtime(), $_[SESSION]->ID, $_[ARG0]; |
strftime($TIMESTAMP,localtime()), $_[SESSION]->ID, $_[ARG0], |
407 |
print "The $_[ARG0] event was given these parameters: ", |
ref($_[ARG1]) eq "ARRAY" ? join(",", map { ref($_) eq "ARRAY" ? join(";", @{$_}) : $_ } @{ $_[ARG1] }) : |
408 |
join(" ", map({"ARRAY" eq ref $_ ? "[@$_]" : "$_"} @{$_[ARG1]})), "\n"; |
$_[ARG1] ? $_[ARG1] : |
409 |
|
""; |
410 |
0; # false for signals |
0; # false for signals |
411 |
}, |
}, |
412 |
my_add => sub { |
my_add => sub { |
472 |
}, |
}, |
473 |
); |
); |
474 |
|
|
475 |
|
# tags support |
476 |
|
|
477 |
|
my $cloud = HTML::TagCloud->new; |
478 |
|
|
479 |
|
=head2 add_tag |
480 |
|
|
481 |
|
add_tag( id => 42, message => 'irc message' ); |
482 |
|
|
483 |
|
=cut |
484 |
|
|
485 |
|
sub add_tag { |
486 |
|
my $arg = {@_}; |
487 |
|
|
488 |
|
return unless ($arg->{id} && $arg->{message}); |
489 |
|
|
490 |
|
my $m = $arg->{message}; |
491 |
|
from_to('UTF-8', 'iso-8859-2', $m) if (is_utf8($m)); |
492 |
|
|
493 |
|
while ($m =~ s#$tag_regex##s) { |
494 |
|
my $tag = $1; |
495 |
|
next if (! $tag || $tag =~ m/https?:/i); |
496 |
|
push @{ $tags->{$tag} }, $arg->{id}; |
497 |
|
#warn "+tag $tag: $arg->{id}\n"; |
498 |
|
$cloud->add($tag, "?tag=$tag", scalar @{$tags->{$tag}} + 1); |
499 |
|
} |
500 |
|
} |
501 |
|
|
502 |
|
=head2 seed_tags |
503 |
|
|
504 |
|
Read all tags from database and create in-memory cache for tags |
505 |
|
|
506 |
|
=cut |
507 |
|
|
508 |
|
sub seed_tags { |
509 |
|
my $sth = $dbh->prepare(qq{ select id,message from log where message like '%//%' }); |
510 |
|
$sth->execute; |
511 |
|
while (my $row = $sth->fetchrow_hashref) { |
512 |
|
add_tag( %$row ); |
513 |
|
} |
514 |
|
|
515 |
|
foreach my $tag (keys %$tags) { |
516 |
|
$cloud->add($tag, "?tag=$tag", scalar @{$tags->{$tag}} + 1); |
517 |
|
} |
518 |
|
} |
519 |
|
|
520 |
|
seed_tags; |
521 |
|
|
522 |
# http server |
# http server |
523 |
|
|
524 |
my $httpd = POE::Component::Server::HTTP->new( |
my $httpd = POE::Component::Server::HTTP->new( |
533 |
my $style = <<'_END_OF_STYLE_'; |
my $style = <<'_END_OF_STYLE_'; |
534 |
p { margin: 0; padding: 0.1em; } |
p { margin: 0; padding: 0.1em; } |
535 |
.time, .channel { color: #808080; font-size: 60%; } |
.time, .channel { color: #808080; font-size: 60%; } |
536 |
.date { float: right; clear: right; background: #e0e0e0; color: #404040; font-size: 120%; padding: 0.25em; border: 1px dashed #808080; } |
.date { float: right; background: #e0e0e0; color: #404040; font-size: 120%; padding: 0.25em; border: 1px dashed #808080; } |
537 |
.nick { color: #000000; font-size: 80%; padding: 2px; font-family: courier, courier new, monospace ; } |
.nick { color: #000000; font-size: 80%; padding: 2px; font-family: courier, courier new, monospace ; } |
538 |
.message { color: #000000; font-size: 100%; } |
.message { color: #000000; font-size: 100%; } |
539 |
.search { float: right; } |
.search { float: right; } |
542 |
.col-2 { background: #99ff99 } |
.col-2 { background: #99ff99 } |
543 |
.col-3 { background: #ff9999 } |
.col-3 { background: #ff9999 } |
544 |
.col-4 { background: #ff66ff } |
.col-4 { background: #ff66ff } |
545 |
|
a:link.tag, a:visited.tag { border: 1px dashed #ccc; backgound: #ccc; text-decoration: none } |
546 |
|
a:hover.tag { border: 1px solid #eee } |
547 |
_END_OF_STYLE_ |
_END_OF_STYLE_ |
548 |
|
|
549 |
my $max_color = 4; |
my $max_color = 4; |
568 |
my $search = $q->param('search') || $q->param('grep') || ''; |
my $search = $q->param('search') || $q->param('grep') || ''; |
569 |
|
|
570 |
$response->content( |
$response->content( |
571 |
qq{<html><head><title>$NICK</title><style type="text/css">$style</style></head><body> |
qq{<html><head><title>$NICK</title><style type="text/css">$style} . |
572 |
<form method="post" class="search"> |
$cloud->css . |
573 |
|
qq{</style></head><body>} . |
574 |
|
qq{ |
575 |
|
<form method="post" class="search" action="/"> |
576 |
<input type="text" name="search" value="$search" size="10"> |
<input type="text" name="search" value="$search" size="10"> |
577 |
<input type="submit" value="search"> |
<input type="submit" value="search"> |
578 |
</form> |
</form> |
|
<p> |
|
579 |
} . |
} . |
580 |
|
$cloud->html(500) . |
581 |
|
qq{<p>} . |
582 |
join("</p><p>", |
join("</p><p>", |
583 |
get_from_log( |
get_from_log( |
584 |
limit => $q->param('last') || 100, |
limit => $q->param('last') || 100, |
585 |
search => $q->param('search') || $q->param('grep') || undef, |
search => $search || undef, |
586 |
|
tag => $q->param('tag') || undef, |
587 |
fmt => { |
fmt => { |
588 |
date => '<hr size="1" style="clear: both;"/><div class="date">%s</div> ', |
date => '<hr size="1" style="clear: both;"/><div class="date">%s</div> ', |
589 |
time => '<span class="time">%s</span> ', |
time => '<span class="time">%s</span> ', |
597 |
my $m = shift || return; |
my $m = shift || return; |
598 |
$m =~ s/($escape_re)/$escape{$1}/gs; |
$m =~ s/($escape_re)/$escape{$1}/gs; |
599 |
$m =~ s#($RE{URI}{HTTP})#<a href="$1">$1</a>#gs; |
$m =~ s#($RE{URI}{HTTP})#<a href="$1">$1</a>#gs; |
600 |
|
$m =~ s#$tag_regex#<a href="?tag=$1" class="tag">$1</a>#g; |
601 |
return $m; |
return $m; |
602 |
}, |
}, |
603 |
nick => sub { |
nick => sub { |