44 |
use POE qw(Component::IRC Wheel::FollowTail Component::Server::HTTP); |
use POE qw(Component::IRC Wheel::FollowTail Component::Server::HTTP); |
45 |
use HTTP::Status; |
use HTTP::Status; |
46 |
use DBI; |
use DBI; |
47 |
use Encode qw/from_to/; |
use Encode qw/from_to is_utf8/; |
48 |
use Regexp::Common qw /URI/; |
use Regexp::Common qw /URI/; |
49 |
use CGI::Simple; |
use CGI::Simple; |
50 |
|
use HTML::TagCloud; |
51 |
|
|
52 |
my $dbh = DBI->connect($DSN,"","", { RaiseError => 1, AutoCommit => 1 }) || die $DBI::errstr; |
my $dbh = DBI->connect($DSN,"","", { RaiseError => 1, AutoCommit => 1 }) || die $DBI::errstr; |
53 |
|
|
82 |
values (?,?,?,?) |
values (?,?,?,?) |
83 |
}); |
}); |
84 |
|
|
85 |
|
my $tags; |
86 |
|
my $tag_regex = '\b([\w-_]+)//'; |
87 |
|
|
88 |
=head2 get_from_log |
=head2 get_from_log |
89 |
|
|
90 |
my @messages = get_from_log( |
my @messages = get_from_log( |
149 |
my $sql = $context ? $sql_context : $sql_message; |
my $sql = $context ? $sql_context : $sql_message; |
150 |
|
|
151 |
$sql .= " where message ilike ? or nick ilike ? " if ($args->{search}); |
$sql .= " where message ilike ? or nick ilike ? " if ($args->{search}); |
152 |
|
$sql .= " where id in (" . join(",", @{ $tags->{ $args->{tag} } }) . ") " if ($args->{tag} && $tags->{ $args->{tag} }); |
153 |
$sql .= " order by log.time desc"; |
$sql .= " order by log.time desc"; |
154 |
$sql .= " limit " . $args->{limit}; |
$sql .= " limit " . $args->{limit}; |
155 |
|
|
159 |
$search =~ s/\s+$//; |
$search =~ s/\s+$//; |
160 |
$sth->execute( ( '%' . $search . '%' ) x 2 ); |
$sth->execute( ( '%' . $search . '%' ) x 2 ); |
161 |
warn "search for '$search' returned ", $sth->rows, " results ", $context || '', "\n"; |
warn "search for '$search' returned ", $sth->rows, " results ", $context || '', "\n"; |
162 |
|
} elsif (my $tag = $args->{tag}) { |
163 |
|
$sth->execute(); |
164 |
|
warn "tag '$tag' returned ", $sth->rows, " results ", $context || '', "\n"; |
165 |
} else { |
} else { |
166 |
$sth->execute(); |
$sth->execute(); |
167 |
} |
} |
291 |
|
|
292 |
print "$channel: <$nick> $msg\n"; |
print "$channel: <$nick> $msg\n"; |
293 |
$sth->execute($channel, 0, $nick, $msg); |
$sth->execute($channel, 0, $nick, $msg); |
294 |
|
add_tag( id => $dbh->last_insert_id(undef,undef,"log",undef), |
295 |
|
message => $msg); |
296 |
}, |
}, |
297 |
irc_ctcp_action => sub { |
irc_ctcp_action => sub { |
298 |
my $kernel = $_[KERNEL]; |
my $kernel = $_[KERNEL]; |
304 |
|
|
305 |
print "$channel ***$nick $msg\n"; |
print "$channel ***$nick $msg\n"; |
306 |
$sth->execute($channel, 1, $nick, $msg); |
$sth->execute($channel, 1, $nick, $msg); |
307 |
|
add_tag( id => $dbh->last_insert_id(undef,undef,"log",undef), |
308 |
|
message => $msg); |
309 |
}, |
}, |
310 |
irc_msg => sub { |
irc_msg => sub { |
311 |
my $kernel = $_[KERNEL]; |
my $kernel = $_[KERNEL]; |
484 |
}, |
}, |
485 |
); |
); |
486 |
|
|
487 |
|
# tags support |
488 |
|
|
489 |
|
my $cloud = HTML::TagCloud->new; |
490 |
|
|
491 |
|
=head2 add_tag |
492 |
|
|
493 |
|
add_tag( id => 42, message => 'irc message' ); |
494 |
|
|
495 |
|
=cut |
496 |
|
|
497 |
|
sub add_tag { |
498 |
|
my $arg = {@_}; |
499 |
|
|
500 |
|
return unless ($arg->{id} && $arg->{message}); |
501 |
|
|
502 |
|
my $m = $arg->{message}; |
503 |
|
from_to('UTF-8', 'iso-8859-2', $m) if (is_utf8($m)); |
504 |
|
|
505 |
|
while ($m =~ s#$tag_regex##s) { |
506 |
|
my $tag = $1; |
507 |
|
next if (! $tag || $tag =~ m/https?:/i); |
508 |
|
push @{ $tags->{$tag} }, $arg->{id}; |
509 |
|
warn "+tag $tag: $arg->{id}\n"; |
510 |
|
$cloud->add($tag, "?tag=$tag", scalar @{$tags->{$tag}} + 1); |
511 |
|
} |
512 |
|
} |
513 |
|
|
514 |
|
=head2 seed_tags |
515 |
|
|
516 |
|
Read all tags from database and create in-memory cache for tags |
517 |
|
|
518 |
|
=cut |
519 |
|
|
520 |
|
sub seed_tags { |
521 |
|
my $sth = $dbh->prepare(qq{ select id,message from log where message like '%//%' }); |
522 |
|
$sth->execute; |
523 |
|
while (my $row = $sth->fetchrow_hashref) { |
524 |
|
add_tag( %$row ); |
525 |
|
} |
526 |
|
|
527 |
|
foreach my $tag (keys %$tags) { |
528 |
|
$cloud->add($tag, "?tag=$tag", scalar @{$tags->{$tag}} + 1); |
529 |
|
} |
530 |
|
} |
531 |
|
|
532 |
|
seed_tags; |
533 |
|
|
534 |
# http server |
# http server |
535 |
|
|
536 |
my $httpd = POE::Component::Server::HTTP->new( |
my $httpd = POE::Component::Server::HTTP->new( |
545 |
my $style = <<'_END_OF_STYLE_'; |
my $style = <<'_END_OF_STYLE_'; |
546 |
p { margin: 0; padding: 0.1em; } |
p { margin: 0; padding: 0.1em; } |
547 |
.time, .channel { color: #808080; font-size: 60%; } |
.time, .channel { color: #808080; font-size: 60%; } |
548 |
.date { float: right; background: #404040; color: #e0e0e0; font-size: 120%; padding: 0.5em; border-top: 1px dashed #e0e0e0; } |
.date { float: right; background: #e0e0e0; color: #404040; font-size: 120%; padding: 0.25em; border: 1px dashed #808080; } |
549 |
.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 ; } |
550 |
.message { color: #000000; font-size: 100%; } |
.message { color: #000000; font-size: 100%; } |
551 |
.search { float: right; } |
.search { float: right; } |
554 |
.col-2 { background: #99ff99 } |
.col-2 { background: #99ff99 } |
555 |
.col-3 { background: #ff9999 } |
.col-3 { background: #ff9999 } |
556 |
.col-4 { background: #ff66ff } |
.col-4 { background: #ff66ff } |
557 |
|
a:link.tag, a:visited.tag { border: 1px dashed #ccc; backgound: #ccc; text-decoration: none } |
558 |
|
a:hover.tag { border: 1px solid #eee } |
559 |
_END_OF_STYLE_ |
_END_OF_STYLE_ |
560 |
|
|
561 |
my $max_color = 4; |
my $max_color = 4; |
580 |
my $search = $q->param('search') || $q->param('grep') || ''; |
my $search = $q->param('search') || $q->param('grep') || ''; |
581 |
|
|
582 |
$response->content( |
$response->content( |
583 |
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} . |
584 |
<form method="post" class="search"> |
$cloud->css . |
585 |
|
qq{</style></head><body>} . |
586 |
|
qq{ |
587 |
|
<form method="post" class="search" action="/"> |
588 |
<input type="text" name="search" value="$search" size="10"> |
<input type="text" name="search" value="$search" size="10"> |
589 |
<input type="submit" value="search"> |
<input type="submit" value="search"> |
590 |
</form> |
</form> |
|
<p> |
|
591 |
} . |
} . |
592 |
|
$cloud->html(500) . |
593 |
|
qq{<p>} . |
594 |
join("</p><p>", |
join("</p><p>", |
595 |
get_from_log( |
get_from_log( |
596 |
limit => $q->param('last') || 100, |
limit => $q->param('last') || 100, |
597 |
search => $q->param('search') || $q->param('grep') || undef, |
search => $search || undef, |
598 |
|
tag => $q->param('tag') || undef, |
599 |
fmt => { |
fmt => { |
600 |
date => '<hr size="1"/><div class="date">%s</div> ', |
date => '<hr size="1" style="clear: both;"/><div class="date">%s</div> ', |
601 |
time => '<span class="time">%s</span> ', |
time => '<span class="time">%s</span> ', |
602 |
time_channel => '<span class="channel">%s %s</span> ', |
time_channel => '<span class="channel">%s %s</span> ', |
603 |
nick => '%s: ', |
nick => '%s: ', |
609 |
my $m = shift || return; |
my $m = shift || return; |
610 |
$m =~ s/($escape_re)/$escape{$1}/gs; |
$m =~ s/($escape_re)/$escape{$1}/gs; |
611 |
$m =~ s#($RE{URI}{HTTP})#<a href="$1">$1</a>#gs; |
$m =~ s#($RE{URI}{HTTP})#<a href="$1">$1</a>#gs; |
612 |
|
$m =~ s#$tag_regex#<a href="?tag=$1" class="tag">$1</a>#g; |
613 |
return $m; |
return $m; |
614 |
}, |
}, |
615 |
nick => sub { |
nick => sub { |