7 |
use DBI; |
use DBI; |
8 |
use Regexp::Common qw /URI/; |
use Regexp::Common qw /URI/; |
9 |
use CGI::Simple; |
use CGI::Simple; |
|
use HTML::TagCloud; |
|
10 |
use POSIX qw/strftime/; |
use POSIX qw/strftime/; |
11 |
use HTML::CalendarMonthSimple; |
use HTML::CalendarMonthSimple; |
12 |
use Getopt::Long; |
use Getopt::Long; |
18 |
use XML::Feed; |
use XML::Feed; |
19 |
use DateTime::Format::Flexible; |
use DateTime::Format::Flexible; |
20 |
use Encode; |
use Encode; |
21 |
|
#use Redis 2.0; |
22 |
|
|
23 |
=head1 NAME |
=head1 NAME |
24 |
|
|
128 |
my %escape = ('<'=>'<', '>'=>'>', '&'=>'&', '"'=>'"'); |
my %escape = ('<'=>'<', '>'=>'>', '&'=>'&', '"'=>'"'); |
129 |
my $escape_re = join '|' => keys %escape; |
my $escape_re = join '|' => keys %escape; |
130 |
|
|
131 |
my $tag_regex = '\b([\w-_]+)//'; |
my $tag_regex = '\b([\w\-_]+)//'; |
132 |
|
|
133 |
my %nick_enumerator; |
my %nick_enumerator; |
134 |
my $max_color = 0; |
my $max_color = 0; |
140 |
# protect HTML from wiki modifications |
# protect HTML from wiki modifications |
141 |
sub e { |
sub e { |
142 |
my $t = shift; |
my $t = shift; |
143 |
return 'uri_unescape{' . uri_escape($t, '^a-zA-Z0-9') . '}'; |
eval { $t = 'uri_unescape{' . uri_escape($t, '^a-zA-Z0-9') . '}'; }; |
144 |
|
return $t; |
145 |
} |
} |
146 |
|
|
147 |
$m =~ s/($escape_re)/$escape{$1}/gs; |
$m =~ s/($escape_re)/$escape{$1}/gs; |
148 |
$m =~ s#($RE{URI}{HTTP})#e(qq{<a href="$1">$1</a>})#egs; |
$m =~ s#($RE{URI}{HTTP})#e(qq{<a href="$1">$1</a>})#egs; |
149 |
$m =~ s#\/(\w+)\/#<i>$1</i>#gs; |
# $m =~ s#\/(\w+)\/#<i>$1</i>#gs; |
150 |
$m =~ s#$tag_regex#e(qq{<a href="$url?tag=$1" class="tag">$1</a>})#egs; |
$m =~ s#$tag_regex#e(qq{<a href="$url?tag=$1" class="tag">$1</a>})#egs; |
151 |
$m =~ s#\*(\w+)\*#<b>$1</b>#gs; |
$m =~ s#\*(\w+)\*#<b>$1</b>#gs; |
152 |
$m =~ s#_(\w+)_#<u>$1</u>#gs; |
$m =~ s#_(\w+)_#<u>$1</u>#gs; |
257 |
|
|
258 |
eval { $sth->execute( $value, $nick, $channel, $name ) }; |
eval { $sth->execute( $value, $nick, $channel, $name ) }; |
259 |
|
|
260 |
# error or no result |
if ( $@ ) { |
261 |
if ( $@ || ! $sth->rows ) { |
# error |
262 |
|
_log("META ERROR: $@"); |
263 |
|
} elsif ( ! $sth->rows ) { |
264 |
|
# no result -> add new |
265 |
$sth = $dbh->prepare(qq{ insert into meta (value,nick,channel,name,changed) values (?,?,?,?,now()) }); |
$sth = $dbh->prepare(qq{ insert into meta (value,nick,channel,name,changed) values (?,?,?,?,now()) }); |
266 |
$sth->execute( $value, $nick, $channel, $name ); |
eval { $sth->execute( $value, $nick, $channel, $name ); }; |
267 |
warn "## created $nick/$channel/$name = $value\n"; |
if ( $@ ) { |
268 |
|
_log "META ERROR: $@"; |
269 |
|
} else { |
270 |
|
_log "META: created $nick/$channel/$name = $value\n"; |
271 |
|
} |
272 |
} else { |
} else { |
273 |
warn "## updated $nick/$channel/$name = $value\n"; |
_log "META: updated $nick/$channel/$name = $value\n"; |
274 |
} |
} |
275 |
|
|
276 |
return $value; |
return $value; |
534 |
|
|
535 |
# tags support |
# tags support |
536 |
|
|
537 |
my $cloud = HTML::TagCloud->new; |
my $cloud = TagCloud->new; |
538 |
|
$cloud->seed_tags; |
|
=head2 add_tag |
|
|
|
|
|
add_tag( id => 42, message => 'irc message', nick => 'foobar' [, me => 1 ] ); |
|
|
|
|
|
=cut |
|
|
|
|
|
my @last_tags; |
|
|
|
|
|
sub add_tag { |
|
|
my $arg = {@_}; |
|
|
|
|
|
return unless ($arg->{id} && $arg->{message}); |
|
|
|
|
|
my $m = $arg->{message}; |
|
|
|
|
|
my @tags; |
|
|
|
|
|
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, "$url?tag=$tag", scalar @{$tags->{$tag}} + 1); |
|
|
push @tags, $tag; |
|
|
|
|
|
} |
|
|
|
|
|
if ( @tags ) { |
|
|
pop @last_tags if $#last_tags == $last_x_tags; |
|
|
unshift @last_tags, { tags => [ @tags ], %$arg }; |
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
=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,nick,me,time from log where message like '%//%' order by time asc }); |
|
|
$sth->execute; |
|
|
while (my $row = $sth->fetchrow_hashref) { |
|
|
add_tag( %$row ); |
|
|
} |
|
|
|
|
|
foreach my $tag (keys %$tags) { |
|
|
$cloud->add($tag, "$url?tag=$tag", scalar @{$tags->{$tag}} + 1); |
|
|
} |
|
|
} |
|
|
|
|
|
seed_tags; |
|
|
|
|
539 |
|
|
540 |
=head2 save_message |
=head2 save_message |
541 |
|
|
565 |
" " . $a->{message}; |
" " . $a->{message}; |
566 |
|
|
567 |
eval { $sth_insert_log->execute($a->{channel}, $a->{me}, $a->{nick}, $a->{message}, $a->{time}); }; |
eval { $sth_insert_log->execute($a->{channel}, $a->{me}, $a->{nick}, $a->{message}, $a->{time}); }; |
568 |
_log "ERROR: can't archive ", $a->{message} if $@; |
|
569 |
add_tag( id => $dbh->last_insert_id(undef,undef,"log",undef), %$a ); |
eval { |
570 |
|
my @channel = ( 'channel' , $a->{channel}, $a->{nick} ); |
571 |
|
push @channel, 'me' if $a->{me}; |
572 |
|
# my $redis = Redis->new( server => '192.168.1.61:6379' ); |
573 |
|
# $redis->publish( join(' ',@channel), $a->{message} ); |
574 |
|
}; |
575 |
|
|
576 |
|
if ( $@ ) { |
577 |
|
_log "ERROR: can't archive ", $a->{message}; |
578 |
|
} else { |
579 |
|
$cloud->add_tag( id => $dbh->last_insert_id(undef,undef,"log",undef), %$a ); |
580 |
|
} |
581 |
} |
} |
582 |
|
|
583 |
|
|
584 |
if ($import_dircproxy) { |
if ($import_dircproxy) { |
585 |
|
my ( $from_time, $date_time ) = $dbh->selectrow_array(qq{ |
586 |
|
select date_part('epoch',max(time)),max(time) from log |
587 |
|
}); |
588 |
|
|
589 |
|
warn "IMPORT $date_time [$from_time]\n"; |
590 |
|
|
591 |
open(my $l, $import_dircproxy) || die "can't open $import_dircproxy: $!"; |
open(my $l, $import_dircproxy) || die "can't open $import_dircproxy: $!"; |
592 |
warn "importing $import_dircproxy...\n"; |
warn "importing $import_dircproxy...\n"; |
593 |
my $tz_offset = 1 * 60 * 60; # TZ GMT+2 |
my $tz_offset = 1 * 60 * 60; # TZ GMT+2 |
596 |
if (/^@(\d+)\s(\S+)\s(.+)$/) { |
if (/^@(\d+)\s(\S+)\s(.+)$/) { |
597 |
my ($time, $nick, $msg) = ($1,$2,$3); |
my ($time, $nick, $msg) = ($1,$2,$3); |
598 |
|
|
599 |
|
next if $time <= $from_time; |
600 |
|
|
601 |
my $dt = DateTime->from_epoch( epoch => $time + $tz_offset ); |
my $dt = DateTime->from_epoch( epoch => $time + $tz_offset ); |
602 |
|
|
603 |
my $me = 0; |
my $me = 0; |
654 |
|
|
655 |
warn "## RSS fetch first $send_rss_msgs items from", $args->{url} if $debug; |
warn "## RSS fetch first $send_rss_msgs items from", $args->{url} if $debug; |
656 |
|
|
657 |
my $feed = XML::Feed->parse( \$args->{xml} ); |
my $feed; |
658 |
|
eval { $feed = XML::Feed->parse( \$args->{xml} ) }; |
659 |
if ( ! $feed ) { |
if ( ! $feed ) { |
660 |
_log "can't fetch RSS ", $args->{url}, XML::Feed->errstr; |
_log "can't fetch RSS ", $args->{url}, XML::Feed->errstr; |
661 |
return; |
return; |
1190 |
|
|
1191 |
return RC_OK if $request->uri =~ m/favicon.ico$/; |
return RC_OK if $request->uri =~ m/favicon.ico$/; |
1192 |
|
|
1193 |
|
if ( $request->uri =~ m/robots.txt$/ ) { |
1194 |
|
$response->content_type( 'text/plain' ); |
1195 |
|
$response->content( qq{ |
1196 |
|
|
1197 |
|
User-Agent: * |
1198 |
|
Disallow: / |
1199 |
|
|
1200 |
|
}); |
1201 |
|
return RC_OK; |
1202 |
|
} |
1203 |
|
|
1204 |
my $q; |
my $q; |
1205 |
|
|
1206 |
if ( $request->method eq 'POST' ) { |
if ( $request->method eq 'POST' ) { |
1226 |
$response->content_type( 'application/' . lc($type) . '+xml' ); |
$response->content_type( 'application/' . lc($type) . '+xml' ); |
1227 |
|
|
1228 |
my $html = '<!-- error -->'; |
my $html = '<!-- error -->'; |
1229 |
#warn "create $type feed from ",dump( @last_tags ); |
#warn "create $type feed from ",dump( $cloud->last_tags ); |
1230 |
|
|
1231 |
my $feed = XML::Feed->new( $type ); |
my $feed = XML::Feed->new( $type ); |
1232 |
$feed->link( $url ); |
$feed->link( $url ); |
1260 |
$feed->title( "last $nr tagged messages from $CHANNEL" ); |
$feed->title( "last $nr tagged messages from $CHANNEL" ); |
1261 |
$feed->description( "collects messages which have tags// in them" ); |
$feed->description( "collects messages which have tags// in them" ); |
1262 |
|
|
1263 |
foreach my $m ( @last_tags ) { |
foreach my $m ( $cloud->last_tags ) { |
1264 |
# warn dump( $m ); |
# warn dump( $m ); |
1265 |
#my $tags = join(' ', @{$m->{tags}} ); |
#my $tags = join(' ', @{$m->{tags}} ); |
1266 |
my $feed_entry = XML::Feed::Entry->new($type); |
my $feed_entry = XML::Feed::Entry->new($type); |
1323 |
$rc = RC_DENY; |
$rc = RC_DENY; |
1324 |
} |
} |
1325 |
|
|
1326 |
$response->content( $feed->as_xml ); |
eval { $response->content( $feed->as_xml ); }; |
1327 |
|
$rc = RC_INTERNAL_SERVER_ERROR if $@; |
1328 |
return $rc; |
return $rc; |
1329 |
} |
} |
1330 |
|
|
1337 |
my $html = |
my $html = |
1338 |
qq{<html><head><title>$NICK</title><style type="text/css">$style} |
qq{<html><head><title>$NICK</title><style type="text/css">$style} |
1339 |
. $cloud->css |
. $cloud->css |
1340 |
. qq{</style></head><body>} |
. qq{</style> |
1341 |
|
<meta name="google-site-verification" content="oe-LvUiNiQRPpc_uB-3rY4MWvFifkmLf276WzAvTL5U" /> |
1342 |
|
</head><body>} |
1343 |
. qq{ |
. qq{ |
1344 |
<form method="post" class="search" action="/"> |
<form method="post" class="search" action="/"> |
1345 |
<input type="text" name="search" value="$search" size="10"> |
<input type="text" name="search" value="$search" size="10"> |
1393 |
} else { |
} else { |
1394 |
$html .= join("</p><p>", |
$html .= join("</p><p>", |
1395 |
get_from_log( |
get_from_log( |
1396 |
limit => ( $q->param('last') || $q->param('date') ) ? undef : 100, |
limit => ( $q->param('date') ? undef : $q->param('last') || 100 ), |
1397 |
search => $search || undef, |
search => $search || undef, |
1398 |
tag => $q->param('tag') || undef, |
tag => $q->param('tag') || undef, |
1399 |
date => $q->param('date') || undef, |
date => $q->param('date') || undef, |
1418 |
<p>See <a href="/history">history</a> of all messages.</p> |
<p>See <a href="/history">history</a> of all messages.</p> |
1419 |
</body></html>}; |
</body></html>}; |
1420 |
|
|
1421 |
$response->content( decode('utf-8',$html) ); |
$response->content( $html ); |
1422 |
warn "<< ", $request->method, " ", $request->uri, " created ", length($html), " bytes\n"; |
warn "<< ", $request->method, " ", $request->uri, " created ", length($html), " bytes\n"; |
1423 |
return RC_OK; |
return RC_OK; |
1424 |
} |
} |
1425 |
|
|
1426 |
POE::Kernel->run; |
POE::Kernel->run; |
1427 |
|
|
1428 |
|
=head1 TagCloud |
1429 |
|
|
1430 |
|
Extended L<HTML::TagCloud> |
1431 |
|
|
1432 |
|
=cut |
1433 |
|
|
1434 |
|
package TagCloud; |
1435 |
|
use warnings; |
1436 |
|
use strict; |
1437 |
|
use HTML::TagCloud; |
1438 |
|
use base 'HTML::TagCloud'; |
1439 |
|
use Data::Dump qw/dump/; |
1440 |
|
|
1441 |
|
=head2 html |
1442 |
|
|
1443 |
|
Generate html with number of tags in title of link |
1444 |
|
|
1445 |
|
=cut |
1446 |
|
|
1447 |
|
sub html { |
1448 |
|
my($self, $limit) = @_; |
1449 |
|
my @tags=$self->tags($limit); |
1450 |
|
|
1451 |
|
my $ntags = scalar(@tags); |
1452 |
|
if ($ntags == 0) { |
1453 |
|
return ""; |
1454 |
|
# } elsif ($ntags == 1) { |
1455 |
|
# my $tag = $tags[0]; |
1456 |
|
# return qq{<div id="htmltagcloud"><span class="tagcloud1"><a href="}. |
1457 |
|
# $tag->{url}.qq{">}.$tag->{name}.qq{</a></span></div>\n}; |
1458 |
|
} |
1459 |
|
|
1460 |
|
my $html = qq{<div id="htmltagcloud">}; |
1461 |
|
foreach my $tag ( sort { lc($a->{name}) cmp lc($b->{name}) } @tags) { |
1462 |
|
$html .= sprintf(qq{<span class="tag tagcloud%d"><a href="%s" title="%s">%s</a></span>\n}, |
1463 |
|
$tag->{level}, $tag->{url}, $tag->{count}, $tag->{name} |
1464 |
|
); |
1465 |
|
} |
1466 |
|
$html .= qq{</div>}; |
1467 |
|
return $html; |
1468 |
|
} |
1469 |
|
|
1470 |
|
=head2 last_tags |
1471 |
|
|
1472 |
|
my @tags = $cloud->last_tags; |
1473 |
|
|
1474 |
|
=cut |
1475 |
|
|
1476 |
|
my @last_tags; |
1477 |
|
sub last_tags { |
1478 |
|
return @last_tags; |
1479 |
|
} |
1480 |
|
|
1481 |
|
=head2 add_tag |
1482 |
|
|
1483 |
|
$cloud->add_tag( id => 42, message => 'irc message', nick => 'foobar' [, me => 1 ] ); |
1484 |
|
|
1485 |
|
=cut |
1486 |
|
|
1487 |
|
|
1488 |
|
sub add_tag { |
1489 |
|
my $self = shift; |
1490 |
|
my $arg = {@_}; |
1491 |
|
|
1492 |
|
return unless ($arg->{id} && $arg->{message}); |
1493 |
|
|
1494 |
|
my $m = $arg->{message}; |
1495 |
|
|
1496 |
|
my @tags; |
1497 |
|
|
1498 |
|
while ($m =~ s#$tag_regex##s) { |
1499 |
|
my $tag = $1; |
1500 |
|
next if (! $tag || $tag =~ m/https?:/i); |
1501 |
|
push @{ $tags->{$tag} }, $arg->{id}; |
1502 |
|
#warn "+tag $tag: $arg->{id}\n"; |
1503 |
|
$self->add($tag, "$url?tag=$tag", scalar @{$tags->{$tag}}); |
1504 |
|
push @tags, $tag; |
1505 |
|
|
1506 |
|
} |
1507 |
|
|
1508 |
|
if ( @tags ) { |
1509 |
|
pop @last_tags if $#last_tags == $last_x_tags; |
1510 |
|
unshift @last_tags, { tags => [ @tags ], %$arg }; |
1511 |
|
} |
1512 |
|
|
1513 |
|
} |
1514 |
|
|
1515 |
|
=head2 seed_tags |
1516 |
|
|
1517 |
|
Read all tags from database and create in-memory cache for tags |
1518 |
|
|
1519 |
|
=cut |
1520 |
|
|
1521 |
|
sub seed_tags { |
1522 |
|
my $self = shift; |
1523 |
|
my $sth = $dbh->prepare(qq{ select id,message,nick,me,time from log where message like '%//%' order by time asc }); |
1524 |
|
$sth->execute; |
1525 |
|
while (my $row = $sth->fetchrow_hashref) { |
1526 |
|
$self->add_tag( %$row ); |
1527 |
|
} |
1528 |
|
|
1529 |
|
foreach my $tag (keys %$tags) { |
1530 |
|
$self->add($tag, "$url?tag=$tag", scalar @{$tags->{$tag}}); |
1531 |
|
} |
1532 |
|
} |
1533 |
|
|