20 |
|
|
21 |
=item --log=irc-logger.log |
=item --log=irc-logger.log |
22 |
|
|
|
Name of log file |
|
|
|
|
|
=item --follow=file.log |
|
|
|
|
|
Follows new messages in file |
|
|
|
|
23 |
=back |
=back |
24 |
|
|
25 |
=head1 DESCRIPTION |
=head1 DESCRIPTION |
44 |
$CHANNEL = '#irc-logger' if ($HOSTNAME =~ m/llin/); |
$CHANNEL = '#irc-logger' if ($HOSTNAME =~ m/llin/); |
45 |
my $IRC_ALIAS = "log"; |
my $IRC_ALIAS = "log"; |
46 |
|
|
|
# default log to follow and announce messages |
|
|
my $follows_path = 'follows.log'; |
|
|
|
|
47 |
my $DSN = 'DBI:Pg:dbname=' . $NICK; |
my $DSN = 'DBI:Pg:dbname=' . $NICK; |
48 |
|
|
|
my $ENCODING = 'ISO-8859-2'; |
|
49 |
my $TIMESTAMP = '%Y-%m-%d %H:%M:%S'; |
my $TIMESTAMP = '%Y-%m-%d %H:%M:%S'; |
50 |
|
|
51 |
my $sleep_on_error = 5; |
my $sleep_on_error = 5; |
63 |
|
|
64 |
## END CONFIG |
## END CONFIG |
65 |
|
|
66 |
use POE qw(Component::IRC Wheel::FollowTail Component::Server::HTTP); |
use POE qw(Component::IRC Component::Server::HTTP); |
67 |
use HTTP::Status; |
use HTTP::Status; |
68 |
use DBI; |
use DBI; |
|
use Encode qw/from_to is_utf8/; |
|
69 |
use Regexp::Common qw /URI/; |
use Regexp::Common qw /URI/; |
70 |
use CGI::Simple; |
use CGI::Simple; |
71 |
use HTML::TagCloud; |
use HTML::TagCloud; |
88 |
my $log_path; |
my $log_path; |
89 |
GetOptions( |
GetOptions( |
90 |
'import-dircproxy:s' => \$import_dircproxy, |
'import-dircproxy:s' => \$import_dircproxy, |
|
'follows:s' => \$follows_path, |
|
91 |
'log:s' => \$log_path, |
'log:s' => \$log_path, |
92 |
); |
); |
93 |
|
|
94 |
$SIG{__DIE__} = sub { |
#$SIG{__DIE__} = sub { |
95 |
confess "fatal error"; |
# confess "fatal error"; |
96 |
}; |
#}; |
97 |
|
|
98 |
open(STDOUT, '>', $log_path) || warn "can't redirect log to $log_path: $!"; |
open(STDOUT, '>', $log_path) || warn "can't redirect log to $log_path: $!"; |
99 |
|
|
100 |
sub _log { |
sub _log { |
101 |
print strftime($TIMESTAMP,localtime()), ' ', join(" ",@_), $/; |
print strftime($TIMESTAMP,localtime()) . ' ' . join(" ",@_) . $/; |
|
} |
|
|
|
|
|
# LOG following |
|
|
|
|
|
my %FOLLOWS = |
|
|
( |
|
|
# ACCESS => "/var/log/apache/access.log", |
|
|
# ERROR => "/var/log/apache/error.log", |
|
|
); |
|
|
|
|
|
sub add_follow_path { |
|
|
my $path = shift; |
|
|
my $name = $path; |
|
|
$name =~ s/\..*$//; |
|
|
warn "# using $path to announce messages from $name\n"; |
|
|
$FOLLOWS{$name} = $path; |
|
102 |
} |
} |
103 |
|
|
|
add_follow_path( $follows_path ) if ( -e $follows_path ); |
|
|
|
|
104 |
# HTML formatters |
# HTML formatters |
105 |
|
|
106 |
my %escape = ('<'=>'<', '>'=>'>', '&'=>'&', '"'=>'"'); |
my %escape = ('<'=>'<', '>'=>'>', '&'=>'&', '"'=>'"'); |
144 |
}; |
}; |
145 |
|
|
146 |
my $dbh = DBI->connect($DSN,"","", { RaiseError => 1, AutoCommit => 1 }) || die $DBI::errstr; |
my $dbh = DBI->connect($DSN,"","", { RaiseError => 1, AutoCommit => 1 }) || die $DBI::errstr; |
147 |
|
$dbh->do( qq{ set client_encoding = 'UTF-8' } ); |
148 |
|
|
149 |
my $sql_schema = { |
my $sql_schema = { |
150 |
log => qq{ |
log => qq{ |
177 |
id serial, |
id serial, |
178 |
url text not null, |
url text not null, |
179 |
name text, |
name text, |
180 |
delay interval not null default '30 sec', --'5 min', |
delay interval not null default '5 min', |
181 |
active boolean default true, |
active boolean default true, |
182 |
last_update timestamp default 'now()', |
last_update timestamp default 'now()', |
183 |
polls int default 0, |
polls int default 0, |
249 |
|
|
250 |
|
|
251 |
|
|
252 |
my $sth = $dbh->prepare(qq{ |
my $sth_insert_log = $dbh->prepare(qq{ |
253 |
insert into log |
insert into log |
254 |
(channel, me, nick, message, time) |
(channel, me, nick, message, time) |
255 |
values (?,?,?,?,?) |
values (?,?,?,?,?) |
501 |
return unless ($arg->{id} && $arg->{message}); |
return unless ($arg->{id} && $arg->{message}); |
502 |
|
|
503 |
my $m = $arg->{message}; |
my $m = $arg->{message}; |
|
from_to('UTF-8', 'iso-8859-2', $m) if (is_utf8($m)); |
|
504 |
|
|
505 |
my @tags; |
my @tags; |
506 |
|
|
569 |
$a->{me} ? "***" . $a->{nick} : "<" . $a->{nick} . ">", |
$a->{me} ? "***" . $a->{nick} : "<" . $a->{nick} . ">", |
570 |
" " . $a->{message}; |
" " . $a->{message}; |
571 |
|
|
572 |
from_to($a->{message}, 'UTF-8', $ENCODING); |
$sth_insert_log->execute($a->{channel}, $a->{me}, $a->{nick}, $a->{message}, $a->{time}); |
|
|
|
|
$sth->execute($a->{channel}, $a->{me}, $a->{nick}, $a->{message}, $a->{time}); |
|
573 |
add_tag( id => $dbh->last_insert_id(undef,undef,"log",undef), %$a ); |
add_tag( id => $dbh->last_insert_id(undef,undef,"log",undef), %$a ); |
574 |
} |
} |
575 |
|
|
621 |
# how many messages to send out when feed is seen for the first time? |
# how many messages to send out when feed is seen for the first time? |
622 |
my $send_rss_msgs = 1; |
my $send_rss_msgs = 1; |
623 |
|
|
624 |
|
_log "RSS fetch", $args->{url}; |
625 |
|
|
626 |
my $feed = XML::Feed->parse(URI->new( $args->{url} )); |
my $feed = XML::Feed->parse(URI->new( $args->{url} )); |
627 |
if ( ! $feed ) { |
if ( ! $feed ) { |
628 |
_log("can't fetch RSS ", $args->{url}); |
_log("can't fetch RSS ", $args->{url}); |
629 |
return; |
return; |
630 |
} |
} |
631 |
my $updates = 0; |
my ( $total, $updates ) = ( 0, 0 ); |
632 |
for my $entry ($feed->entries) { |
for my $entry ($feed->entries) { |
633 |
|
$total++; |
634 |
|
|
635 |
# seen allready? |
# seen allready? |
636 |
return if $_rss->{$feed->link}->{seen}->{$entry->id}++ > 0; |
return if $_rss->{$feed->link}->{seen}->{$entry->id}++ > 0; |
642 |
} |
} |
643 |
|
|
644 |
my $msg; |
my $msg; |
645 |
$msg .= prefix( 'From: ' , $feed->title ); |
$msg .= prefix( 'From: ' , $args->{name} || $feed->title ); |
646 |
$msg .= prefix( ' by ' , $entry->author ); |
$msg .= prefix( ' by ' , $entry->author ); |
647 |
$msg .= prefix( ' -- ' , $entry->link ); |
$msg .= prefix( ' -- ' , $entry->link ); |
648 |
# $msg .= prefix( ' id ' , $entry->id ); |
# $msg .= prefix( ' id ' , $entry->id ); |
649 |
|
|
|
_log('RSS', $msg); |
|
|
|
|
650 |
if ( $args->{kernel} && $send_rss_msgs ) { |
if ( $args->{kernel} && $send_rss_msgs ) { |
|
warn "# sending to $CHANNEL\n"; |
|
651 |
$send_rss_msgs--; |
$send_rss_msgs--; |
652 |
|
_log('RSS', $msg); |
653 |
|
$sth_insert_log->execute( $CHANNEL, 1, $NICK, $msg, undef ); |
654 |
$args->{kernel}->post( $IRC_ALIAS => notice => $CHANNEL, $msg ); |
$args->{kernel}->post( $IRC_ALIAS => notice => $CHANNEL, $msg ); |
655 |
$updates++; |
$updates++; |
656 |
} |
} |
659 |
my $sql = qq{ update feeds set last_update = now(), polls = polls + 1 }; |
my $sql = qq{ update feeds set last_update = now(), polls = polls + 1 }; |
660 |
$sql .= qq{, updates = updates + $updates } if $updates; |
$sql .= qq{, updates = updates + $updates } if $updates; |
661 |
$sql .= qq{where id = } . $args->{id}; |
$sql .= qq{where id = } . $args->{id}; |
662 |
$dbh->do( $sql ); |
eval { $dbh->do( $sql ) }; |
663 |
|
|
664 |
|
_log "RSS got $total items of which $updates new"; |
665 |
|
|
666 |
return $updates; |
return $updates; |
667 |
} |
} |
680 |
warn "# ",$sth->rows," active RSS feeds\n"; |
warn "# ",$sth->rows," active RSS feeds\n"; |
681 |
my $count = 0; |
my $count = 0; |
682 |
while (my $row = $sth->fetchrow_hashref) { |
while (my $row = $sth->fetchrow_hashref) { |
|
warn "+++ fetch RSS feed: ",dump( $row ); |
|
683 |
$row->{kernel} = $kernel if $kernel; |
$row->{kernel} = $kernel if $kernel; |
684 |
$count += rss_fetch( $row ); |
$count += rss_fetch( $row ); |
685 |
} |
} |
686 |
return "OK, fetched $count posts from " . $sth->rows . " feeds"; |
return "OK, fetched $count posts from " . $sth->rows . " feeds"; |
687 |
} |
} |
688 |
|
|
|
my $rss_last_poll = time(); |
|
689 |
|
|
690 |
sub rss_check_updates { |
sub rss_check_updates { |
691 |
my $kernel = shift; |
my $kernel = shift; |
692 |
|
my $last_t = $_rss->{last_poll} || time(); |
693 |
my $t = time(); |
my $t = time(); |
694 |
if ( $rss_last_poll - $t > $rss_min_delay ) { |
if ( $t - $last_t > $rss_min_delay ) { |
695 |
$rss_last_poll = $t; |
$_rss->{last_poll} = $t; |
696 |
_log rss_fetch_all( $kernel ); |
_log rss_fetch_all( $kernel ); |
697 |
} |
} |
698 |
} |
} |
704 |
# POE handing part |
# POE handing part |
705 |
# |
# |
706 |
|
|
|
my $SKIPPING = 0; # if skipping, how many we've done |
|
|
my $SEND_QUEUE; # cache |
|
707 |
my $ping; # ping stats |
my $ping; # ping stats |
708 |
|
|
709 |
POE::Component::IRC->new($IRC_ALIAS); |
POE::Component::IRC->new($IRC_ALIAS); |
715 |
}, |
}, |
716 |
irc_255 => sub { # server is done blabbing |
irc_255 => sub { # server is done blabbing |
717 |
$_[KERNEL]->post($IRC_ALIAS => join => $CHANNEL); |
$_[KERNEL]->post($IRC_ALIAS => join => $CHANNEL); |
|
$_[KERNEL]->post($IRC_ALIAS => join => '#logger'); |
|
|
$_[KERNEL]->yield("heartbeat"); # start heartbeat |
|
|
$_[KERNEL]->yield("my_add", $_) for keys %FOLLOWS; |
|
718 |
$_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "IDENTIFY $NICK" ); |
$_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "IDENTIFY $NICK" ); |
719 |
}, |
}, |
720 |
irc_public => sub { |
irc_public => sub { |
765 |
my $nick = (split /!/, $_[ARG0])[0]; |
my $nick = (split /!/, $_[ARG0])[0]; |
766 |
my $msg = $_[ARG2]; |
my $msg = $_[ARG2]; |
767 |
my $channel = $_[ARG1]->[0]; |
my $channel = $_[ARG1]->[0]; |
|
from_to($msg, 'UTF-8', $ENCODING); |
|
768 |
|
|
769 |
my $res = "unknown command '$msg', try /msg $NICK help!"; |
my $res = "unknown command '$msg', try /msg $NICK help!"; |
770 |
my @out; |
my @out; |
808 |
|
|
809 |
foreach my $res (get_from_log( limit => $limit )) { |
foreach my $res (get_from_log( limit => $limit )) { |
810 |
_log "last: $res"; |
_log "last: $res"; |
|
from_to($res, $ENCODING, 'UTF-8'); |
|
811 |
$_[KERNEL]->post( $IRC_ALIAS => privmsg => $nick, $res ); |
$_[KERNEL]->post( $IRC_ALIAS => privmsg => $nick, $res ); |
812 |
} |
} |
813 |
|
|
822 |
search => $what, |
search => $what, |
823 |
)) { |
)) { |
824 |
_log "search [$what]: $res"; |
_log "search [$what]: $res"; |
|
from_to($res, $ENCODING, 'UTF-8'); |
|
825 |
$_[KERNEL]->post( $IRC_ALIAS => privmsg => $nick, $res ); |
$_[KERNEL]->post( $IRC_ALIAS => privmsg => $nick, $res ); |
826 |
} |
} |
827 |
|
|
895 |
$res = rss_fetch_all( $_[KERNEL] ); |
$res = rss_fetch_all( $_[KERNEL] ); |
896 |
} elsif ($msg =~ m/^rss-clean/) { |
} elsif ($msg =~ m/^rss-clean/) { |
897 |
$_rss = undef; |
$_rss = undef; |
898 |
|
$dbh->do( qq{ update feeds set last_update = now() - delay } ); |
899 |
$res = "OK, cleaned RSS cache"; |
$res = "OK, cleaned RSS cache"; |
900 |
} elsif ($msg =~ m!^rss-(add|remove|stop|start)\s+(http://\S+)\s*(.*)!) { |
} elsif ($msg =~ m!^rss-(add|remove|stop|start)\s+(http://\S+)\s*(.*)!) { |
901 |
my $sql = { |
my $sql = { |
911 |
eval { $sth->execute( $2, $3 ) }; |
eval { $sth->execute( $2, $3 ) }; |
912 |
} |
} |
913 |
|
|
914 |
$res ||= "OK, RSS $1 : $2 - $3"; |
$res = "OK, RSS $1 : $2 - $3"; |
915 |
} |
} |
916 |
|
|
917 |
if ($res) { |
if ($res) { |
918 |
_log ">> [$nick] $res"; |
_log ">> [$nick] $res"; |
|
from_to($res, $ENCODING, 'UTF-8'); |
|
919 |
$_[KERNEL]->post( $IRC_ALIAS => privmsg => $nick, $res ); |
$_[KERNEL]->post( $IRC_ALIAS => privmsg => $nick, $res ); |
920 |
} |
} |
921 |
|
|
958 |
""; |
""; |
959 |
0; # false for signals |
0; # false for signals |
960 |
}, |
}, |
|
my_add => sub { |
|
|
my $trailing = $_[ARG0]; |
|
|
my $session = $_[SESSION]; |
|
|
POE::Session->create |
|
|
(inline_states => |
|
|
{_start => sub { |
|
|
$_[HEAP]->{wheel} = |
|
|
POE::Wheel::FollowTail->new |
|
|
( |
|
|
Filename => $FOLLOWS{$trailing}, |
|
|
InputEvent => 'got_line', |
|
|
); |
|
|
warn "+++ following $trailing at $FOLLOWS{$trailing}\n"; |
|
|
}, |
|
|
got_line => sub { |
|
|
warn "+++ $trailing : $_[ARG0]\n"; |
|
|
$_[KERNEL]->post($session => my_tailed => time, $trailing, $_[ARG0]); |
|
|
}, |
|
|
}, |
|
|
); |
|
|
|
|
|
}, |
|
|
my_tailed => sub { |
|
|
my ($time, $file, $line) = @_[ARG0..ARG2]; |
|
|
## $time will be undef on a probe, or a time value if a real line |
|
|
|
|
|
## PoCo::IRC has throttling built in, but no external visibility |
|
|
## so this is reaching "under the hood" |
|
|
$SEND_QUEUE ||= |
|
|
$_[KERNEL]->alias_resolve($IRC_ALIAS)->get_heap->{send_queue}; |
|
|
|
|
|
## handle "no need to keep skipping" transition |
|
|
if ($SKIPPING and @$SEND_QUEUE < 1) { |
|
|
$_[KERNEL]->post($IRC_ALIAS => privmsg => $CHANNEL => |
|
|
"[discarded $SKIPPING messages]"); |
|
|
$SKIPPING = 0; |
|
|
} |
|
|
|
|
|
## handle potential message display |
|
|
if ($time) { |
|
|
if ($SKIPPING or @$SEND_QUEUE > 3) { # 3 msgs per 10 seconds |
|
|
$SKIPPING++; |
|
|
} else { |
|
|
my @time = localtime $time; |
|
|
$_[KERNEL]->post($IRC_ALIAS => privmsg => $CHANNEL => |
|
|
sprintf "%02d:%02d:%02d: %s: %s", |
|
|
($time[2] + 11) % 12 + 1, $time[1], $time[0], |
|
|
$file, $line); |
|
|
} |
|
|
} |
|
|
|
|
|
## handle re-probe/flush if skipping |
|
|
if ($SKIPPING) { |
|
|
$_[KERNEL]->delay($_[STATE] => 0.5); # $time will be undef |
|
|
} |
|
|
|
|
|
}, |
|
|
my_heartbeat => sub { |
|
|
$_[KERNEL]->yield(my_tailed => time, "heartbeat", "beep"); |
|
|
$_[KERNEL]->delay($_[STATE] => 10); |
|
|
} |
|
961 |
}, |
}, |
962 |
); |
); |
963 |
|
|
1085 |
my $message = $filter->{message}->( $m->{message} ); |
my $message = $filter->{message}->( $m->{message} ); |
1086 |
$message .= "<br/>\n" unless $message =~ m!<(/p|br/?)>!; |
$message .= "<br/>\n" unless $message =~ m!<(/p|br/?)>!; |
1087 |
# warn "## message = $message\n"; |
# warn "## message = $message\n"; |
|
from_to( $message, $ENCODING, 'UTF-8' ); |
|
1088 |
|
|
1089 |
#$feed_entry->summary( |
#$feed_entry->summary( |
1090 |
$feed_entry->content( |
$feed_entry->content( |
1128 |
warn "$@"; |
warn "$@"; |
1129 |
} |
} |
1130 |
|
|
1131 |
$response->content_type("text/html; charset=$ENCODING"); |
$response->content_type("text/html; charset=UTF-8"); |
1132 |
|
|
1133 |
my $html = |
my $html = |
1134 |
qq{<html><head><title>$NICK</title><style type="text/css">$style} |
qq{<html><head><title>$NICK</title><style type="text/css">$style} |
1179 |
} |
} |
1180 |
$cal->setcontent($dd, qq[ |
$cal->setcontent($dd, qq[ |
1181 |
<a href="$url?date=$row->{date}">$row->{nr}</a><br/>$row->{len} |
<a href="$url?date=$row->{date}">$row->{nr}</a><br/>$row->{len} |
1182 |
]); |
]) if $cal; |
1183 |
|
|
1184 |
} |
} |
1185 |
$html .= qq{<td valign="top">} . $cal->as_HTML() . qq{</td></tr></table>}; |
$html .= qq{<td valign="top">} . $cal->as_HTML() . qq{</td></tr></table>}; |