18 |
|
|
19 |
## CONFIG |
## CONFIG |
20 |
|
|
21 |
my $NICK = 'irc-logger-dev'; |
my $HOSTNAME = `hostname`; |
22 |
|
|
23 |
|
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; |
use HTML::TagCloud; |
56 |
|
use POSIX qw/strftime/; |
57 |
|
use HTML::CalendarMonthSimple; |
58 |
|
|
59 |
my $dbh = DBI->connect($DSN,"","", { RaiseError => 1, AutoCommit => 1 }) || die $DBI::errstr; |
my $dbh = DBI->connect($DSN,"","", { RaiseError => 1, AutoCommit => 1 }) || die $DBI::errstr; |
60 |
|
|
90 |
}); |
}); |
91 |
|
|
92 |
my $tags; |
my $tags; |
93 |
|
my $tag_regex = '\b([\w-_]+)//'; |
94 |
|
|
95 |
=head2 get_from_log |
=head2 get_from_log |
96 |
|
|
123 |
sub get_from_log { |
sub get_from_log { |
124 |
my $args = {@_}; |
my $args = {@_}; |
125 |
|
|
|
$args->{limit} ||= 10; |
|
|
|
|
126 |
$args->{fmt} ||= { |
$args->{fmt} ||= { |
127 |
date => '[%s] ', |
date => '[%s] ', |
128 |
time => '{%s} ', |
time => '{%s} ', |
155 |
|
|
156 |
$sql .= " where message ilike ? or nick ilike ? " if ($args->{search}); |
$sql .= " where message ilike ? or nick ilike ? " if ($args->{search}); |
157 |
$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} }); |
158 |
|
$sql .= " where date(time) = ? " if ($args->{date}); |
159 |
$sql .= " order by log.time desc"; |
$sql .= " order by log.time desc"; |
160 |
$sql .= " limit " . $args->{limit}; |
$sql .= " limit " . $args->{limit} if ($args->{limit}); |
161 |
|
|
162 |
my $sth = $dbh->prepare( $sql ); |
my $sth = $dbh->prepare( $sql ); |
163 |
if (my $search = $args->{search}) { |
if (my $search = $args->{search}) { |
168 |
} elsif (my $tag = $args->{tag}) { |
} elsif (my $tag = $args->{tag}) { |
169 |
$sth->execute(); |
$sth->execute(); |
170 |
warn "tag '$tag' returned ", $sth->rows, " results ", $context || '', "\n"; |
warn "tag '$tag' returned ", $sth->rows, " results ", $context || '', "\n"; |
171 |
|
} elsif (my $date = $args->{date}) { |
172 |
|
$sth->execute($date); |
173 |
|
warn "found ", $sth->rows, " messages for date $date ", $context || '', "\n"; |
174 |
} else { |
} else { |
175 |
$sth->execute(); |
$sth->execute(); |
176 |
} |
} |
215 |
} |
} |
216 |
} |
} |
217 |
|
|
218 |
|
# 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 |
foreach my $row (@rows) { |
foreach my $row (@rows) { |
229 |
|
|
230 |
$row->{time} =~ s#\.\d+##; |
$row->{time} =~ s#\.\d+##; |
231 |
|
|
232 |
my $msg = ''; |
my $msg = ''; |
233 |
|
|
234 |
$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}); |
235 |
my $t = $row->{time}; |
my $t = $row->{time}; |
236 |
|
|
237 |
if ($last_row->{channel} ne $row->{channel}) { |
if ($last_row->{channel} ne $row->{channel}) { |
238 |
$msg .= sprintf($args->{fmt}->{time_channel}, $t, $row->{channel}); |
$msg .= cr_sprintf($args->{fmt}->{time_channel}, $t, $row->{channel}); |
239 |
} else { |
} else { |
240 |
$msg .= sprintf($args->{fmt}->{time}, $t); |
$msg .= cr_sprintf($args->{fmt}->{time}, $t); |
241 |
} |
} |
242 |
|
|
243 |
my $append = 1; |
my $append = 1; |
254 |
|
|
255 |
$nick = $args->{filter}->{nick}->($nick) if (ref($args->{filter}->{nick}) eq 'CODE'); |
$nick = $args->{filter}->{nick}->($nick) if (ref($args->{filter}->{nick}) eq 'CODE'); |
256 |
|
|
257 |
$msg .= sprintf( $fmt, $nick ); |
$msg .= cr_sprintf( $fmt, $nick ); |
258 |
$append = 0; |
$append = 0; |
259 |
} |
} |
260 |
|
|
261 |
$args->{fmt}->{message} ||= '%s'; |
$args->{fmt}->{message} ||= '%s'; |
262 |
if (ref($args->{filter}->{message}) eq 'CODE') { |
if (ref($args->{filter}->{message}) eq 'CODE') { |
263 |
$msg .= sprintf($args->{fmt}->{message}, |
$msg .= cr_sprintf($args->{fmt}->{message}, |
264 |
$args->{filter}->{message}->( |
$args->{filter}->{message}->( |
265 |
$row->{message} |
$row->{message} |
266 |
) |
) |
267 |
); |
); |
268 |
} else { |
} else { |
269 |
$msg .= sprintf($args->{fmt}->{message}, $row->{message}); |
$msg .= cr_sprintf($args->{fmt}->{message}, $row->{message}); |
270 |
} |
} |
271 |
|
|
272 |
if ($append && @msgs) { |
if ($append && @msgs) { |
287 |
|
|
288 |
POE::Component::IRC->new($IRC_ALIAS); |
POE::Component::IRC->new($IRC_ALIAS); |
289 |
|
|
290 |
POE::Session->create |
=head2 save_message |
291 |
(inline_states => |
|
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 |
|
POE::Session->create( inline_states => |
305 |
{_start => sub { |
{_start => sub { |
306 |
$_[KERNEL]->post($IRC_ALIAS => register => 'all'); |
$_[KERNEL]->post($IRC_ALIAS => register => 'all'); |
307 |
$_[KERNEL]->post($IRC_ALIAS => connect => $CONNECT); |
$_[KERNEL]->post($IRC_ALIAS => connect => $CONNECT); |
322 |
from_to($msg, 'UTF-8', $ENCODING); |
from_to($msg, 'UTF-8', $ENCODING); |
323 |
|
|
324 |
print "$channel: <$nick> $msg\n"; |
print "$channel: <$nick> $msg\n"; |
325 |
$sth->execute($channel, 0, $nick, $msg); |
save_message($channel, 0, $nick, $msg); |
326 |
}, |
}, |
327 |
irc_ctcp_action => sub { |
irc_ctcp_action => sub { |
328 |
my $kernel = $_[KERNEL]; |
my $kernel = $_[KERNEL]; |
333 |
from_to($msg, 'UTF-8', $ENCODING); |
from_to($msg, 'UTF-8', $ENCODING); |
334 |
|
|
335 |
print "$channel ***$nick $msg\n"; |
print "$channel ***$nick $msg\n"; |
336 |
$sth->execute($channel, 1, $nick, $msg); |
save_message($channel, 1, $nick, $msg); |
337 |
}, |
}, |
338 |
irc_msg => sub { |
irc_msg => sub { |
339 |
my $kernel = $_[KERNEL]; |
my $kernel = $_[KERNEL]; |
423 |
# warn "## indetify $NICK\n"; |
# warn "## indetify $NICK\n"; |
424 |
# $_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "IDENTIFY $NICK" ); |
# $_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "IDENTIFY $NICK" ); |
425 |
# }, |
# }, |
|
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 |
|
426 |
_child => sub {}, |
_child => sub {}, |
427 |
_default => sub { |
_default => sub { |
428 |
printf "%s: session %s caught an unhandled %s event.\n", |
printf "%s #%s %s %s\n", |
429 |
scalar localtime(), $_[SESSION]->ID, $_[ARG0]; |
strftime($TIMESTAMP,localtime()), $_[SESSION]->ID, $_[ARG0], |
430 |
print "The $_[ARG0] event was given these parameters: ", |
ref($_[ARG1]) eq "ARRAY" ? join(",", map { ref($_) eq "ARRAY" ? join(";", @{$_}) : $_ } @{ $_[ARG1] }) : |
431 |
join(" ", map({"ARRAY" eq ref $_ ? "[@$_]" : "$_"} @{$_[ARG1]})), "\n"; |
$_[ARG1] ? $_[ARG1] : |
432 |
|
""; |
433 |
0; # false for signals |
0; # false for signals |
434 |
}, |
}, |
435 |
my_add => sub { |
my_add => sub { |
510 |
|
|
511 |
return unless ($arg->{id} && $arg->{message}); |
return unless ($arg->{id} && $arg->{message}); |
512 |
|
|
513 |
while ($arg->{message} =~ s#\b(\S+)//##s) { |
my $m = $arg->{message}; |
514 |
|
from_to('UTF-8', 'iso-8859-2', $m) if (is_utf8($m)); |
515 |
|
|
516 |
|
while ($m =~ s#$tag_regex##s) { |
517 |
my $tag = $1; |
my $tag = $1; |
518 |
next if (! $tag || $tag =~ m/https?:/i); |
next if (! $tag || $tag =~ m/https?:/i); |
519 |
push @{ $tags->{$tag} }, $arg->{id}; |
push @{ $tags->{$tag} }, $arg->{id}; |
520 |
|
#warn "+tag $tag: $arg->{id}\n"; |
521 |
|
$cloud->add($tag, "?tag=$tag", scalar @{$tags->{$tag}} + 1); |
522 |
} |
} |
523 |
} |
} |
524 |
|
|
565 |
.col-2 { background: #99ff99 } |
.col-2 { background: #99ff99 } |
566 |
.col-3 { background: #ff9999 } |
.col-3 { background: #ff9999 } |
567 |
.col-4 { background: #ff66ff } |
.col-4 { background: #ff66ff } |
568 |
|
a:link.tag, a:visited.tag { border: 1px dashed #ccc; backgound: #ccc; text-decoration: none } |
569 |
|
a:hover.tag { border: 1px solid #eee } |
570 |
|
hr { border: 1px dashed #ccc; height: 1px; clear: both; } |
571 |
_END_OF_STYLE_ |
_END_OF_STYLE_ |
572 |
|
|
573 |
my $max_color = 4; |
my $max_color = 4; |
591 |
|
|
592 |
my $search = $q->param('search') || $q->param('grep') || ''; |
my $search = $q->param('search') || $q->param('grep') || ''; |
593 |
|
|
594 |
$response->content( |
my $html = |
595 |
qq{<html><head><title>$NICK</title><style type="text/css">$style} . |
qq{<html><head><title>$NICK</title><style type="text/css">$style} . |
596 |
$cloud->css . |
$cloud->css . |
597 |
qq{</style></head><body>} . |
qq{</style></head><body>} . |
598 |
qq{ |
qq{ |
599 |
<form method="post" class="search"> |
<form method="post" class="search" action="/"> |
600 |
<input type="text" name="search" value="$search" size="10"> |
<input type="text" name="search" value="$search" size="10"> |
601 |
<input type="submit" value="search"> |
<input type="submit" value="search"> |
602 |
</form> |
</form> |
603 |
} . |
} . |
604 |
qq{<div>} . $cloud->html(500) . qq{</div>} . |
$cloud->html(500) . |
605 |
qq{<p>} . |
qq{<p>}; |
606 |
join("</p><p>", |
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 |
get_from_log( |
get_from_log( |
634 |
limit => $q->param('last') || 100, |
limit => $q->param('last') || $q->param('date') ? undef : 100, |
635 |
search => $search || undef, |
search => $search || undef, |
636 |
tag => $q->param('tag'), |
tag => $q->param('tag') || undef, |
637 |
|
date => $q->param('date') || undef, |
638 |
fmt => { |
fmt => { |
639 |
date => '<hr size="1" style="clear: both;"/><div class="date">%s</div> ', |
date => sub { |
640 |
|
my $date = shift || return; |
641 |
|
qq{<hr/><div class="date"><a href="/?date=$date">$date</a></div> '}; |
642 |
|
}, |
643 |
time => '<span class="time">%s</span> ', |
time => '<span class="time">%s</span> ', |
644 |
time_channel => '<span class="channel">%s %s</span> ', |
time_channel => '<span class="channel">%s %s</span> ', |
645 |
nick => '%s: ', |
nick => '%s: ', |
651 |
my $m = shift || return; |
my $m = shift || return; |
652 |
$m =~ s/($escape_re)/$escape{$1}/gs; |
$m =~ s/($escape_re)/$escape{$1}/gs; |
653 |
$m =~ s#($RE{URI}{HTTP})#<a href="$1">$1</a>#gs; |
$m =~ s#($RE{URI}{HTTP})#<a href="$1">$1</a>#gs; |
654 |
|
$m =~ s#$tag_regex#<a href="?tag=$1" class="tag">$1</a>#g; |
655 |
return $m; |
return $m; |
656 |
}, |
}, |
657 |
nick => sub { |
nick => sub { |
666 |
}, |
}, |
667 |
}, |
}, |
668 |
) |
) |
669 |
) . |
); |
670 |
qq{</p></body></html>} |
} |
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 |
return RC_OK; |
return RC_OK; |
679 |
} |
} |
680 |
|
|