2 |
use strict; |
use strict; |
3 |
$|++; |
$|++; |
4 |
|
|
5 |
|
use POE qw(Component::IRC Component::Server::HTTP Component::Client::HTTP); |
6 |
|
use HTTP::Status; |
7 |
|
use DBI; |
8 |
|
use Regexp::Common qw /URI/; |
9 |
|
use CGI::Simple; |
10 |
|
use HTML::TagCloud; |
11 |
|
use POSIX qw/strftime/; |
12 |
|
use HTML::CalendarMonthSimple; |
13 |
|
use Getopt::Long; |
14 |
|
use DateTime; |
15 |
|
use URI::Escape; |
16 |
|
use Data::Dump qw/dump/; |
17 |
|
use DateTime::Format::ISO8601; |
18 |
|
use Carp qw/confess/; |
19 |
|
use XML::Feed; |
20 |
|
use DateTime::Format::Flexible; |
21 |
|
use IPC::DirQueue; |
22 |
|
use File::Slurp; |
23 |
|
use Encode; |
24 |
|
|
25 |
=head1 NAME |
=head1 NAME |
26 |
|
|
27 |
irc-logger.pl |
irc-logger.pl |
40 |
|
|
41 |
=item --log=irc-logger.log |
=item --log=irc-logger.log |
42 |
|
|
|
Name of log file |
|
|
|
|
43 |
=back |
=back |
44 |
|
|
45 |
=head1 DESCRIPTION |
=head1 DESCRIPTION |
50 |
|
|
51 |
## CONFIG |
## CONFIG |
52 |
|
|
53 |
|
my $debug = 0; |
54 |
|
|
55 |
|
my $irc_config = { |
56 |
|
nick => 'irc-logger', |
57 |
|
server => 'irc.freenode.net', |
58 |
|
port => 6667, |
59 |
|
ircname => 'Anna the bot: try /msg irc-logger help', |
60 |
|
}; |
61 |
|
|
62 |
|
my $queue_dir = './queue'; |
63 |
|
|
64 |
my $HOSTNAME = `hostname -f`; |
my $HOSTNAME = `hostname -f`; |
65 |
chomp($HOSTNAME); |
chomp($HOSTNAME); |
66 |
|
|
67 |
my $NICK = 'irc-logger'; |
|
|
$NICK .= '-dev' if ($HOSTNAME =~ m/llin/); |
|
|
my $CONNECT = |
|
|
{Server => 'irc.freenode.net', |
|
|
Nick => $NICK, |
|
|
Ircname => "try /msg $NICK help", |
|
|
}; |
|
68 |
my $CHANNEL = '#razmjenavjestina'; |
my $CHANNEL = '#razmjenavjestina'; |
|
$CHANNEL = '#irc-logger' if ($HOSTNAME =~ m/llin/); |
|
|
my $IRC_ALIAS = "log"; |
|
69 |
|
|
70 |
my %FOLLOWS = |
if ( $HOSTNAME =~ m/llin/ ) { |
71 |
( |
$irc_config->{nick} = 'irc-logger-llin'; |
72 |
ACCESS => "/var/log/apache/access.log", |
# $irc_config = { |
73 |
ERROR => "/var/log/apache/error.log", |
# nick => 'irc-logger-llin', |
74 |
); |
# server => 'localhost', |
75 |
|
# port => 6668, |
76 |
|
# }; |
77 |
|
$CHANNEL = '#irc-logger'; |
78 |
|
} elsif ( $HOSTNAME =~ m/lugarin/ ) { |
79 |
|
$irc_config->{server} = 'irc.carnet.hr'; |
80 |
|
$CHANNEL = '#riss'; |
81 |
|
} |
82 |
|
|
83 |
|
my @channels = ( $CHANNEL ); |
84 |
|
|
85 |
|
warn "## config = ", dump( $irc_config ) if $debug; |
86 |
|
|
87 |
|
my $NICK = $irc_config->{nick} or die "no nick?"; |
88 |
|
|
89 |
my $DSN = 'DBI:Pg:dbname=' . $NICK; |
my $DSN = 'DBI:Pg:dbname=' . $NICK; |
90 |
|
|
|
my $ENCODING = 'ISO-8859-2'; |
|
91 |
my $TIMESTAMP = '%Y-%m-%d %H:%M:%S'; |
my $TIMESTAMP = '%Y-%m-%d %H:%M:%S'; |
92 |
|
|
93 |
my $sleep_on_error = 5; |
my $sleep_on_error = 5; |
94 |
|
|
95 |
|
# number of last tags to keep in circular buffer |
96 |
|
my $last_x_tags = 50; |
97 |
|
|
98 |
|
# don't pull rss feeds more often than this |
99 |
|
my $rss_min_delay = 60; |
100 |
|
|
101 |
my $http_port = $NICK =~ m/-dev/ ? 8001 : 8000; |
my $http_port = $NICK =~ m/-dev/ ? 8001 : 8000; |
102 |
|
|
103 |
my $url = "http://$HOSTNAME:$http_port"; |
my $url = "http://$HOSTNAME:$http_port"; |
104 |
|
|
105 |
## END CONFIG |
## END CONFIG |
106 |
|
|
|
|
|
|
|
|
|
use POE qw(Component::IRC Wheel::FollowTail Component::Server::HTTP); |
|
|
use HTTP::Status; |
|
|
use DBI; |
|
|
use Encode qw/from_to is_utf8/; |
|
|
use Regexp::Common qw /URI/; |
|
|
use CGI::Simple; |
|
|
use HTML::TagCloud; |
|
|
use POSIX qw/strftime/; |
|
|
use HTML::CalendarMonthSimple; |
|
|
use Getopt::Long; |
|
|
use DateTime; |
|
|
use URI::Escape; |
|
|
use Data::Dump qw/dump/; |
|
|
use DateTime::Format::ISO8601; |
|
|
use Carp qw/confess/; |
|
|
use XML::Feed; |
|
|
use DateTime::Format::Flexible; |
|
|
|
|
107 |
my $use_twitter = 1; |
my $use_twitter = 1; |
108 |
eval { require Net::Twitter; }; |
eval { require Net::Twitter; }; |
109 |
$use_twitter = 0 if ($@); |
$use_twitter = 0 if ($@); |
113 |
GetOptions( |
GetOptions( |
114 |
'import-dircproxy:s' => \$import_dircproxy, |
'import-dircproxy:s' => \$import_dircproxy, |
115 |
'log:s' => \$log_path, |
'log:s' => \$log_path, |
116 |
|
'queue:s' => \$queue_dir, |
117 |
|
'debug!' => \$debug, |
118 |
); |
); |
119 |
|
|
120 |
$SIG{__DIE__} = sub { |
#$SIG{__DIE__} = sub { |
121 |
confess "fatal error"; |
# confess "fatal error"; |
122 |
}; |
#}; |
|
|
|
|
open(STDOUT, '>', $log_path) || warn "can't redirect log to $log_path: $!"; |
|
123 |
|
|
124 |
sub _log { |
sub _log { |
125 |
print strftime($TIMESTAMP,localtime()), ' ', join(" ",@_), $/; |
print strftime($TIMESTAMP,localtime()) . ' ' . join(" ",map { ref($_) ? dump( $_ ) : $_ } @_) . $/; |
126 |
} |
} |
127 |
|
|
128 |
|
open(STDOUT, '>', $log_path) && warn "log to $log_path: $!\n"; |
129 |
|
|
130 |
|
# queue |
131 |
|
|
132 |
|
if ( ! -d $queue_dir ) { |
133 |
|
warn "## creating queue directory $queue_dir"; |
134 |
|
mkdir $queue_dir or die "can't create queue directory $queue_dir: $!"; |
135 |
|
} |
136 |
|
|
137 |
|
my $dq = IPC::DirQueue->new({ dir => $queue_dir }); |
138 |
|
|
139 |
# HTML formatters |
# HTML formatters |
140 |
|
|
141 |
my %escape = ('<'=>'<', '>'=>'>', '&'=>'&', '"'=>'"'); |
my %escape = ('<'=>'<', '>'=>'>', '&'=>'&', '"'=>'"'); |
153 |
# protect HTML from wiki modifications |
# protect HTML from wiki modifications |
154 |
sub e { |
sub e { |
155 |
my $t = shift; |
my $t = shift; |
156 |
return 'uri_unescape{' . uri_escape($t) . '}'; |
return 'uri_unescape{' . uri_escape($t, '^a-zA-Z0-9') . '}'; |
157 |
} |
} |
158 |
|
|
159 |
$m =~ s/($escape_re)/$escape{$1}/gs; |
$m =~ s/($escape_re)/$escape{$1}/gs; |
160 |
$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; |
161 |
$m =~ s#\/(\w+)\/#<i>$1</i>#gs; |
$m =~ s#\/(\w+)\/#<i>$1</i>#gs; |
162 |
$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; |
163 |
$m =~ s#\*(\w+)\*#<b>$1</b>#gs; |
$m =~ s#\*(\w+)\*#<b>$1</b>#gs; |
178 |
}, |
}, |
179 |
}; |
}; |
180 |
|
|
181 |
|
# POE IRC |
182 |
|
my $poe_irc = POE::Component::IRC->spawn( %$irc_config ) or |
183 |
|
die "can't start ", dump( $irc_config ), ": $!"; |
184 |
|
|
185 |
|
my $irc = $poe_irc->session_id(); |
186 |
|
_log "IRC session_id $irc"; |
187 |
|
|
188 |
my $dbh = DBI->connect($DSN,"","", { RaiseError => 1, AutoCommit => 1 }) || die $DBI::errstr; |
my $dbh = DBI->connect($DSN,"","", { RaiseError => 1, AutoCommit => 1 }) || die $DBI::errstr; |
189 |
|
$dbh->do( qq{ set client_encoding = 'UTF-8' } ); |
190 |
|
|
191 |
my $sql_schema = { |
my $sql_schema = { |
192 |
log => ' |
log => qq{ |
193 |
create table log ( |
create table log ( |
194 |
id serial, |
id serial, |
195 |
time timestamp default now(), |
time timestamp default now(), |
203 |
create index log_time on log(time); |
create index log_time on log(time); |
204 |
create index log_channel on log(channel); |
create index log_channel on log(channel); |
205 |
create index log_nick on log(nick); |
create index log_nick on log(nick); |
206 |
', |
}, |
207 |
meta => ' |
meta => q{ |
208 |
create table meta ( |
create table meta ( |
209 |
nick text not null, |
nick text not null, |
210 |
channel text not null, |
channel text not null, |
211 |
name text not null, |
name text not null, |
212 |
value text, |
value text, |
213 |
changed timestamp default now(), |
changed timestamp default 'now()', |
214 |
primary key(nick,channel,name) |
primary key(nick,channel,name) |
215 |
); |
); |
216 |
', |
}, |
217 |
|
feeds => qq{ |
218 |
|
create table feeds ( |
219 |
|
id serial, |
220 |
|
url text not null, |
221 |
|
name text, |
222 |
|
delay interval not null default '5 min', |
223 |
|
active boolean default true, |
224 |
|
channel text not null, |
225 |
|
nick text not null, |
226 |
|
private boolean default false, |
227 |
|
last_update timestamp default 'now()', |
228 |
|
polls int default 0, |
229 |
|
updates int default 0 |
230 |
|
); |
231 |
|
create unique index feeds_url on feeds(url); |
232 |
|
insert into feeds (url,name,channel,nick) values ('http://wiki.razmjenavjestina.org/feed/workspace/razmjenavjestina?category=Recent%20Changes','wiki','$CHANNEL','dpavlin'); |
233 |
|
}, |
234 |
}; |
}; |
235 |
|
|
236 |
foreach my $table ( keys %$sql_schema ) { |
foreach my $table ( keys %$sql_schema ) { |
273 |
if ( $@ || ! $sth->rows ) { |
if ( $@ || ! $sth->rows ) { |
274 |
$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()) }); |
275 |
$sth->execute( $value, $nick, $channel, $name ); |
$sth->execute( $value, $nick, $channel, $name ); |
276 |
_log "created $nick/$channel/$name = $value"; |
warn "## created $nick/$channel/$name = $value\n"; |
277 |
} else { |
} else { |
278 |
_log "updated $nick/$channel/$name = $value "; |
warn "## updated $nick/$channel/$name = $value\n"; |
279 |
} |
} |
280 |
|
|
281 |
return $value; |
return $value; |
285 |
my $sth = $dbh->prepare(qq{ select value,changed from meta where nick = ? and channel = ? and name = ? }); |
my $sth = $dbh->prepare(qq{ select value,changed from meta where nick = ? and channel = ? and name = ? }); |
286 |
$sth->execute( $nick, $channel, $name ); |
$sth->execute( $nick, $channel, $name ); |
287 |
my ($v,$c) = $sth->fetchrow_array; |
my ($v,$c) = $sth->fetchrow_array; |
288 |
_log "fetched $nick/$channel/$name = $v [$c]"; |
warn "## fetched $nick/$channel/$name = $v [$c]\n"; |
289 |
return ($v,$c) if wantarray; |
return ($v,$c) if wantarray; |
290 |
return $v; |
return $v; |
291 |
|
|
294 |
|
|
295 |
|
|
296 |
|
|
297 |
my $sth = $dbh->prepare(qq{ |
my $sth_insert_log = $dbh->prepare(qq{ |
298 |
insert into log |
insert into log |
299 |
(channel, me, nick, message, time) |
(channel, me, nick, message, time) |
300 |
values (?,?,?,?,?) |
values (?,?,?,?,?) |
382 |
|
|
383 |
my @where; |
my @where; |
384 |
my @args; |
my @args; |
385 |
|
my $msg; |
386 |
|
|
387 |
if (my $search = $args->{search}) { |
if (my $search = $args->{search}) { |
388 |
$search =~ s/^\s+//; |
$search =~ s/^\s+//; |
389 |
$search =~ s/\s+$//; |
$search =~ s/\s+$//; |
390 |
push @where, 'message ilike ? or nick ilike ?'; |
push @where, 'message ilike ? or nick ilike ?'; |
391 |
push @args, ( ( '%' . $search . '%' ) x 2 ); |
push @args, ( ( '%' . $search . '%' ) x 2 ); |
392 |
_log "search for '$search'"; |
$msg = "Search for '$search'"; |
393 |
} |
} |
394 |
|
|
395 |
if ($args->{tag} && $tags->{ $args->{tag} }) { |
if ($args->{tag} && $tags->{ $args->{tag} }) { |
396 |
push @where, 'id in (' . join(',', @{ $tags->{ $args->{tag} } }) . ')'; |
push @where, 'id in (' . join(',', @{ $tags->{ $args->{tag} } }) . ')'; |
397 |
_log "search for tags $args->{tag}"; |
$msg = "Search for tags $args->{tag}"; |
398 |
} |
} |
399 |
|
|
400 |
if (my $date = $args->{date} ) { |
if (my $date = $args->{date} ) { |
401 |
$date = check_date( $date ); |
$date = check_date( $date ); |
402 |
push @where, 'date(time) = ?'; |
push @where, 'date(time) = ?'; |
403 |
push @args, $date; |
push @args, $date; |
404 |
_log "search for date $date"; |
$msg = "search for date $date"; |
405 |
} |
} |
406 |
|
|
407 |
$sql .= " where " . join(" and ", @where) if @where; |
$sql .= " where " . join(" and ", @where) if @where; |
415 |
eval { $sth->execute( @args ) }; |
eval { $sth->execute( @args ) }; |
416 |
return if $@; |
return if $@; |
417 |
|
|
418 |
|
my $nr_results = $sth->rows; |
419 |
|
|
420 |
my $last_row = { |
my $last_row = { |
421 |
date => '', |
date => '', |
422 |
time => '', |
time => '', |
437 |
|
|
438 |
return @rows if ($args->{full_rows}); |
return @rows if ($args->{full_rows}); |
439 |
|
|
440 |
my @msgs = ( |
$msg .= ' produced ' . ( |
441 |
"Showing " . ($#rows + 1) . " messages..." |
$nr_results == 0 ? 'no results' : |
442 |
|
$nr_results == 0 ? 'one result' : |
443 |
|
$nr_results . ' results' |
444 |
); |
); |
445 |
|
|
446 |
|
my @msgs = ( $msg ); |
447 |
|
|
448 |
if ($context) { |
if ($context) { |
449 |
my @ids = @rows; |
my @ids = @rows; |
450 |
@rows = (); |
@rows = (); |
501 |
# $row->{nick} = $nick; |
# $row->{nick} = $nick; |
502 |
# } |
# } |
503 |
|
|
504 |
|
$append = 0 if $row->{me}; |
505 |
|
|
506 |
if ($last_row->{nick} ne $nick) { |
if ($last_row->{nick} ne $nick) { |
507 |
# obfu way to find format for me_nick if needed or fallback to default |
# obfu way to find format for me_nick if needed or fallback to default |
508 |
my $fmt = $row->{me} ? ( $args->{fmt}->{me_nick} || $args->{fmt}->{nick} ) : $args->{fmt}->{nick}; |
my $fmt = $row->{me} ? ( $args->{fmt}->{me_nick} || $args->{fmt}->{nick} ) : $args->{fmt}->{nick}; |
547 |
|
|
548 |
=cut |
=cut |
549 |
|
|
|
my $last_x_tags = 5; |
|
550 |
my @last_tags; |
my @last_tags; |
551 |
|
|
552 |
sub add_tag { |
sub add_tag { |
555 |
return unless ($arg->{id} && $arg->{message}); |
return unless ($arg->{id} && $arg->{message}); |
556 |
|
|
557 |
my $m = $arg->{message}; |
my $m = $arg->{message}; |
|
from_to('UTF-8', 'iso-8859-2', $m) if (is_utf8($m)); |
|
558 |
|
|
559 |
my @tags; |
my @tags; |
560 |
|
|
569 |
} |
} |
570 |
|
|
571 |
if ( @tags ) { |
if ( @tags ) { |
572 |
shift @last_tags if $#last_tags == $last_x_tags; |
pop @last_tags if $#last_tags == $last_x_tags; |
573 |
push @last_tags, { tags => [ @tags ], %$arg }; |
unshift @last_tags, { tags => [ @tags ], %$arg }; |
574 |
} |
} |
575 |
|
|
576 |
} |
} |
582 |
=cut |
=cut |
583 |
|
|
584 |
sub seed_tags { |
sub seed_tags { |
585 |
my $sth = $dbh->prepare(qq{ select id,message,nick,me,time from log where message like '%//%' }); |
my $sth = $dbh->prepare(qq{ select id,message,nick,me,time from log where message like '%//%' order by time asc }); |
586 |
$sth->execute; |
$sth->execute; |
587 |
while (my $row = $sth->fetchrow_hashref) { |
while (my $row = $sth->fetchrow_hashref) { |
588 |
add_tag( %$row ); |
add_tag( %$row ); |
623 |
$a->{me} ? "***" . $a->{nick} : "<" . $a->{nick} . ">", |
$a->{me} ? "***" . $a->{nick} : "<" . $a->{nick} . ">", |
624 |
" " . $a->{message}; |
" " . $a->{message}; |
625 |
|
|
626 |
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}); |
|
627 |
add_tag( id => $dbh->last_insert_id(undef,undef,"log",undef), %$a ); |
add_tag( id => $dbh->last_insert_id(undef,undef,"log",undef), %$a ); |
628 |
} |
} |
629 |
|
|
662 |
exit; |
exit; |
663 |
} |
} |
664 |
|
|
|
|
|
665 |
# |
# |
666 |
# POE handing part |
# RSS follow |
667 |
# |
# |
668 |
|
|
669 |
my $SKIPPING = 0; # if skipping, how many we've done |
my $_stat; |
670 |
my $SEND_QUEUE; # cache |
|
671 |
my $ping; # ping stats |
POE::Component::Client::HTTP->spawn( |
672 |
|
Alias => 'rss-fetch', |
673 |
POE::Component::IRC->new($IRC_ALIAS); |
Timeout => 30, |
674 |
|
); |
675 |
POE::Session->create( inline_states => |
|
676 |
{_start => sub { |
sub rss_parse_xml { |
677 |
$_[KERNEL]->post($IRC_ALIAS => register => 'all'); |
my ($args) = @_; |
678 |
$_[KERNEL]->post($IRC_ALIAS => connect => $CONNECT); |
|
679 |
}, |
warn "## rss_parse_xml ",dump( @_ ) if $debug; |
680 |
irc_255 => sub { # server is done blabbing |
|
681 |
$_[KERNEL]->post($IRC_ALIAS => join => $CHANNEL); |
# how many messages to send out when feed is seen for the first time? |
682 |
$_[KERNEL]->post($IRC_ALIAS => join => '#logger'); |
my $send_rss_msgs = 1; |
683 |
$_[KERNEL]->yield("heartbeat"); # start heartbeat |
|
684 |
# $_[KERNEL]->yield("my_add", $_) for keys %FOLLOWS; |
_log "RSS fetch", $args->{url}; |
685 |
$_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "IDENTIFY $NICK" ); |
|
686 |
|
my $feed = XML::Feed->parse( \$args->{xml} ); |
687 |
|
if ( ! $feed ) { |
688 |
|
_log "can't fetch RSS ", $args->{url}, XML::Feed->errstr; |
689 |
|
return; |
690 |
|
} |
691 |
|
|
692 |
|
$_stat->{rss}->{url2link}->{ $args->{url} } = $feed->link; |
693 |
|
|
694 |
|
my ( $total, $updates ) = ( 0, 0 ); |
695 |
|
for my $entry ($feed->entries) { |
696 |
|
$total++; |
697 |
|
|
698 |
|
my $seen_times = $_stat->{rss}->{seen}->{$args->{channel}}->{$feed->link}->{$entry->id}++; |
699 |
|
# seen allready? |
700 |
|
warn "## $seen_times ",$entry->id if $debug; |
701 |
|
next if $seen_times > 0; |
702 |
|
|
703 |
|
sub prefix { |
704 |
|
my ($txt,$var) = @_; |
705 |
|
$var =~ s/\s+/ /gs; |
706 |
|
$var =~ s/^\s+//g; |
707 |
|
$var =~ s/\s+$//g; |
708 |
|
return $txt . $var if $var; |
709 |
|
} |
710 |
|
|
711 |
|
# fix absolute and relative links to feed entries |
712 |
|
my $link = $entry->link; |
713 |
|
if ( $link =~ m!^/! ) { |
714 |
|
my $host = $args->{url}; |
715 |
|
$host =~ s!^(http://[^/]+).*$!$1!; #!vim |
716 |
|
$link = "$host/$link"; |
717 |
|
} elsif ( $link !~ m!^http! ) { |
718 |
|
$link = $args->{url} . $link; |
719 |
|
} |
720 |
|
|
721 |
|
my $msg; |
722 |
|
$msg .= prefix( 'From: ' , $args->{name} || $feed->title ); |
723 |
|
$msg .= prefix( ' by ' , $entry->author ); |
724 |
|
$msg .= prefix( ' | ' , $entry->title ); |
725 |
|
$msg .= prefix( ' | ' , $link ); |
726 |
|
# $msg .= prefix( ' id ' , $entry->id ); |
727 |
|
if ( my $tags = $entry->category ) { |
728 |
|
$tags =~ s!^\s+!!; |
729 |
|
$tags =~ s!\s*$! !; |
730 |
|
$tags =~ s!,?\s+!// !g; |
731 |
|
$msg .= prefix( ' ' , $tags ); |
732 |
|
} |
733 |
|
|
734 |
|
if ( $seen_times == 0 && $send_rss_msgs ) { |
735 |
|
$send_rss_msgs--; |
736 |
|
if ( ! $args->{private} ) { |
737 |
|
# FIXME bug! should be save_message |
738 |
|
save_message( channel => $args->{channel}, me => 1, nick => $NICK, message => $msg ); |
739 |
|
# $sth_insert_log->execute( $args->{channel}, 1, $NICK, $msg, 'now()' ); |
740 |
|
} |
741 |
|
my ( $type, $to ) = ( 'notice', $args->{channel} ); |
742 |
|
( $type, $to ) = ( 'privmsg', $args->{nick} ) if $args->{private}; |
743 |
|
|
744 |
|
_log("RSS generated $type to $to:", $msg); |
745 |
|
# XXX enqueue message to send later |
746 |
|
sub enqueue_post { |
747 |
|
my $post = dump( @_ ); |
748 |
|
warn "## queue_post $post\n" if $debug; |
749 |
|
$dq->enqueue_string( $post ); |
750 |
|
} |
751 |
|
enqueue_post( $type => $to => $msg ); |
752 |
|
|
753 |
|
$updates++; |
754 |
|
} |
755 |
|
} |
756 |
|
|
757 |
|
my $sql = qq{ update feeds set last_update = now(), polls = polls + 1 }; |
758 |
|
$sql .= qq{, updates = updates + $updates } if $updates; |
759 |
|
$sql .= qq{where id = } . $args->{id}; |
760 |
|
eval { $dbh->do( $sql ) }; |
761 |
|
|
762 |
|
_log "RSS got $total items of which $updates new"; |
763 |
|
|
764 |
|
return $updates; |
765 |
|
} |
766 |
|
|
767 |
|
sub rss_fetch_all { |
768 |
|
my $kernel = shift; |
769 |
|
my $sql = qq{ |
770 |
|
select id, url, name, channel, nick, private |
771 |
|
from feeds |
772 |
|
where active is true |
773 |
|
}; |
774 |
|
# limit to newer feeds only if we are not sending messages out |
775 |
|
$sql .= qq{ and last_update + delay < now() } if defined ( $_stat->{rss}->{fetch} ); |
776 |
|
my $sth = $dbh->prepare( $sql ); |
777 |
|
$sth->execute(); |
778 |
|
warn "# ",$sth->rows," active RSS feeds\n"; |
779 |
|
my $count = 0; |
780 |
|
while (my $row = $sth->fetchrow_hashref) { |
781 |
|
warn "## queued rss-fetch for ", $row->{url} if $debug; |
782 |
|
$_stat->{rss}->{fetch}->{ $row->{url} } = $row; |
783 |
|
$kernel->post( |
784 |
|
'rss-fetch', |
785 |
|
'request', |
786 |
|
'rss_response', |
787 |
|
HTTP::Request->new( GET => $row->{url} ), |
788 |
|
); |
789 |
|
} |
790 |
|
return "OK, scheduled " . $sth->rows . " feeds for refresh"; |
791 |
|
} |
792 |
|
|
793 |
|
|
794 |
|
sub rss_check_updates { |
795 |
|
my $kernel = shift; |
796 |
|
$_stat->{rss}->{last_poll} ||= time(); |
797 |
|
my $dt = time() - $_stat->{rss}->{last_poll}; |
798 |
|
if ( $dt > $rss_min_delay ) { |
799 |
|
warn "## rss_check_updates $dt > $rss_min_delay\n"; |
800 |
|
$_stat->{rss}->{last_poll} = time(); |
801 |
|
_log rss_fetch_all( $kernel ); |
802 |
|
} |
803 |
|
# XXX send queue messages |
804 |
|
while ( my $job = $dq->pickup_queued_job() ) { |
805 |
|
my $data = read_file( $job->get_data_path ) || die "can't load ", $job->get_data_path, ": $!"; |
806 |
|
# $kernel->post( $irc => $type => $to, $msg ); |
807 |
|
my @data = eval $data; |
808 |
|
_log "IRC post from queue:", @data; |
809 |
|
$kernel->post( $irc => @data ); |
810 |
|
$job->finish; |
811 |
|
warn "## done queued job: ",dump( @data ) if $debug; |
812 |
|
} |
813 |
|
} |
814 |
|
|
815 |
|
POE::Session->create( inline_states => { |
816 |
|
_start => sub { |
817 |
|
$_[KERNEL]->post( $irc => register => 'all' ); |
818 |
|
$_[KERNEL]->post( $irc => connect => {} ); |
819 |
}, |
}, |
820 |
|
irc_001 => sub { |
821 |
|
my ($kernel,$sender) = @_[KERNEL,SENDER]; |
822 |
|
my $poco_object = $sender->get_heap(); |
823 |
|
_log "connected to",$poco_object->server_name(); |
824 |
|
$kernel->post( $sender => join => $_ ) for @channels; |
825 |
|
# seen RSS cache |
826 |
|
_log rss_fetch_all( $kernel ); |
827 |
|
undef; |
828 |
|
}, |
829 |
|
# irc_255 => sub { # server is done blabbing |
830 |
|
# $_[KERNEL]->post( $irc => join => $CHANNEL); |
831 |
|
# }, |
832 |
irc_public => sub { |
irc_public => sub { |
833 |
my $kernel = $_[KERNEL]; |
my $kernel = $_[KERNEL]; |
834 |
my $nick = (split /!/, $_[ARG0])[0]; |
my $nick = (split /!/, $_[ARG0])[0]; |
837 |
|
|
838 |
save_message( channel => $channel, me => 0, nick => $nick, message => $msg); |
save_message( channel => $channel, me => 0, nick => $nick, message => $msg); |
839 |
meta( $nick, $channel, 'last-msg', $msg ); |
meta( $nick, $channel, 'last-msg', $msg ); |
840 |
|
rss_check_updates( $kernel ); |
841 |
}, |
}, |
842 |
irc_ctcp_action => sub { |
irc_ctcp_action => sub { |
843 |
my $kernel = $_[KERNEL]; |
my $kernel = $_[KERNEL]; |
858 |
|
|
859 |
}, |
}, |
860 |
irc_ping => sub { |
irc_ping => sub { |
861 |
warn "pong ", $_[ARG0], $/; |
_log( "pong ", $_[ARG0] ); |
862 |
$ping->{ $_[ARG0] }++; |
$_stat->{ping}->{ $_[ARG0] }++; |
863 |
|
rss_check_updates( $_[KERNEL] ); |
864 |
}, |
}, |
865 |
irc_invite => sub { |
irc_invite => sub { |
866 |
my $kernel = $_[KERNEL]; |
my $kernel = $_[KERNEL]; |
867 |
my $nick = (split /!/, $_[ARG0])[0]; |
my $nick = (split /!/, $_[ARG0])[0]; |
868 |
my $channel = $_[ARG1]; |
my $channel = $_[ARG1]; |
869 |
|
|
870 |
warn "invited to $channel by $nick"; |
_log "invited to $channel by $nick"; |
871 |
|
|
872 |
$_[KERNEL]->post( $IRC_ALIAS => privmsg => $nick, "how nice of you to invite me to $channel, I'll be right there..." ); |
$_[KERNEL]->post( $irc => privmsg => $nick, "how nice of you to invite me to $channel, I'll be right there..." ); |
873 |
$_[KERNEL]->post($IRC_ALIAS => join => $channel); |
$_[KERNEL]->post( $irc => 'join' => $channel ); |
874 |
|
|
875 |
}, |
}, |
876 |
irc_msg => sub { |
irc_msg => sub { |
878 |
my $nick = (split /!/, $_[ARG0])[0]; |
my $nick = (split /!/, $_[ARG0])[0]; |
879 |
my $msg = $_[ARG2]; |
my $msg = $_[ARG2]; |
880 |
my $channel = $_[ARG1]->[0]; |
my $channel = $_[ARG1]->[0]; |
881 |
from_to($msg, 'UTF-8', $ENCODING); |
warn "# ARG = ",dump( @_[ARG0,ARG1,ARG2] ) if $debug; |
882 |
|
|
883 |
my $res = "unknown command '$msg', try /msg $NICK help!"; |
my $res = "unknown command '$msg', try /msg $NICK help!"; |
884 |
my @out; |
my @out; |
889 |
|
|
890 |
$res = "usage: /msg $NICK comand | commands: stat - user/message stat | last - show backtrace | grep foobar - find foobar"; |
$res = "usage: /msg $NICK comand | commands: stat - user/message stat | last - show backtrace | grep foobar - find foobar"; |
891 |
|
|
892 |
} elsif ($msg =~ m/^msg\s+(\S+)\s+(.*)$/i) { |
} elsif ($msg =~ m/^(privmsg|notice)\s+(\S+)\s+(.*)$/i) { |
893 |
|
|
894 |
_log ">> /msg $1 $2"; |
_log ">> /$1 $2 $3"; |
895 |
$_[KERNEL]->post( $IRC_ALIAS => privmsg => $1, $2 ); |
$_[KERNEL]->post( $irc => $1 => $2, $3 ); |
896 |
$res = ''; |
$res = ''; |
897 |
|
|
898 |
} elsif ($msg =~ m/^stat.*?\s*(\d*)/i) { |
} elsif ($msg =~ m/^stat.*?\s*(\d*)/i) { |
922 |
|
|
923 |
foreach my $res (get_from_log( limit => $limit )) { |
foreach my $res (get_from_log( limit => $limit )) { |
924 |
_log "last: $res"; |
_log "last: $res"; |
925 |
from_to($res, $ENCODING, 'UTF-8'); |
$_[KERNEL]->post( $irc => privmsg => $nick, $res ); |
|
$_[KERNEL]->post( $IRC_ALIAS => privmsg => $nick, $res ); |
|
926 |
} |
} |
927 |
|
|
928 |
$res = ''; |
$res = ''; |
936 |
search => $what, |
search => $what, |
937 |
)) { |
)) { |
938 |
_log "search [$what]: $res"; |
_log "search [$what]: $res"; |
939 |
from_to($res, $ENCODING, 'UTF-8'); |
$_[KERNEL]->post( $irc => privmsg => $nick, $res ); |
|
$_[KERNEL]->post( $IRC_ALIAS => privmsg => $nick, $res ); |
|
940 |
} |
} |
941 |
|
|
942 |
$res = ''; |
$res = ''; |
971 |
" : " . ( $stat->{vote}->{'-'} || 0 ) . " --" . |
" : " . ( $stat->{vote}->{'-'} || 0 ) . " --" . |
972 |
" from " . ( join(", ", @nicks) || 'nobody' ); |
" from " . ( join(", ", @nicks) || 'nobody' ); |
973 |
|
|
974 |
$_[KERNEL]->post( $IRC_ALIAS => notice => $nick, $res ); |
$_[KERNEL]->post( $irc => notice => $nick, $res ); |
975 |
|
|
976 |
} elsif ($msg =~ m/^ping/) { |
} elsif ($msg =~ m/^ping/) { |
977 |
$res = "ping = " . dump( $ping ); |
$res = "ping = " . dump( $_stat->{ping} ); |
978 |
} elsif ($msg =~ m/^conf(?:ig)*\s*(last-size|twitter)*\s*(.*)/) { |
} elsif ($msg =~ m/^conf(?:ig)*\s*(last-size|twitter)*\s*(.*)/) { |
979 |
if ( ! defined( $1 ) ) { |
if ( ! defined( $1 ) ) { |
980 |
my $sth = $dbh->prepare(qq{ select name,value,changed from meta where nick = ? and channel = ? }); |
my $sth = $dbh->prepare(qq{ select name,value,changed from meta where nick = ? and channel = ? }); |
1005 |
$res = "config option $op doesn't exist"; |
$res = "config option $op doesn't exist"; |
1006 |
} |
} |
1007 |
} |
} |
1008 |
|
} elsif ($msg =~ m/^rss-update/) { |
1009 |
|
$res = rss_fetch_all( $_[KERNEL] ); |
1010 |
|
} elsif ($msg =~ m/^rss-list/) { |
1011 |
|
my $sth = $dbh->prepare(qq{ select url,name,last_update,active,channel,nick,private from feeds }); |
1012 |
|
$sth->execute; |
1013 |
|
while (my @row = $sth->fetchrow_array) { |
1014 |
|
$_[KERNEL]->post( $irc => privmsg => $nick, join(' | ',@row) ); |
1015 |
|
} |
1016 |
|
$res = ''; |
1017 |
|
} elsif ($msg =~ m!^rss-(add|remove|stop|start|clean)(?:-(private))?\s+(http://\S+)\s*(.*)!) { |
1018 |
|
my ( $command, $sub, $url, $arg ) = ( $1,$2,$3,$4 ); |
1019 |
|
|
1020 |
|
my $channel = $1 if ( $arg =~ s/\s*(#\S+)\s*// ); |
1021 |
|
$channel = $nick if $sub eq 'private'; |
1022 |
|
|
1023 |
|
my $sql = { |
1024 |
|
add => qq{ insert into feeds (url,name,channel,nick,private) values (?,?,?,?,?) }, |
1025 |
|
# remove => qq{ delete from feeds where url = ? and name = ? }, |
1026 |
|
start => qq{ update feeds set active = true where url = ? }, |
1027 |
|
stop => qq{ update feeds set active = false where url = ? }, |
1028 |
|
clean => qq{ update feeds set last_update = now() - delay where url = ? }, |
1029 |
|
}; |
1030 |
|
|
1031 |
|
if ( $command eq 'add' && ! $channel ) { |
1032 |
|
$res = "ERROR: got '$msg' which doesn't have #channel in it, ignoring!"; |
1033 |
|
} elsif (my $q = $sql->{$command} ) { |
1034 |
|
my $sth = $dbh->prepare( $q ); |
1035 |
|
my @data = ( $url ); |
1036 |
|
if ( $command eq 'add' ) { |
1037 |
|
push @data, ( $arg, $channel, $nick, $sub eq 'private' ? 1 : 0 ); |
1038 |
|
} |
1039 |
|
warn "## $command SQL $q with ",dump( @data ),"\n"; |
1040 |
|
eval { $sth->execute( @data ) }; |
1041 |
|
if ($@) { |
1042 |
|
$res = "ERROR: $@"; |
1043 |
|
} else { |
1044 |
|
$res = "OK, RSS executed $command " . ( $sub ? "-$sub" : '' ) ."on $channel url $url"; |
1045 |
|
if ( $command eq 'clean' ) { |
1046 |
|
my $seen = $_stat->{rss}->{seen} || die "no seen?"; |
1047 |
|
my $want_link = $_stat->{rss}->{url2link}->{$url} || warn "no url2link($url)"; |
1048 |
|
foreach my $c ( keys %$seen ) { |
1049 |
|
my $c_hash = $seen->{$c} || die "no seen->{$c}"; |
1050 |
|
die "not HASH with rss links but ", dump($c_hash) unless ref($c_hash) eq 'HASH'; |
1051 |
|
foreach my $link ( keys %$c_hash ) { |
1052 |
|
next unless $link eq $want_link; |
1053 |
|
_log "RSS removed seen $c $url $link"; |
1054 |
|
} |
1055 |
|
} |
1056 |
|
} |
1057 |
|
} |
1058 |
|
} else { |
1059 |
|
$res = "ERROR: don't know what to do with: $msg"; |
1060 |
|
} |
1061 |
|
} elsif ($msg =~ m/^rss-clean/) { |
1062 |
|
# this makes sense because we didn't catch rss-clean http://... before! |
1063 |
|
$_stat->{rss} = undef; |
1064 |
|
$dbh->do( qq{ update feeds set last_update = now() - delay } ); |
1065 |
|
$res = "OK, cleaned RSS cache"; |
1066 |
} |
} |
1067 |
|
|
1068 |
if ($res) { |
if ($res) { |
1069 |
_log ">> [$nick] $res"; |
_log ">> [$nick] $res"; |
1070 |
from_to($res, $ENCODING, 'UTF-8'); |
$_[KERNEL]->post( $irc => privmsg => $nick, $res ); |
|
$_[KERNEL]->post( $IRC_ALIAS => privmsg => $nick, $res ); |
|
1071 |
} |
} |
1072 |
|
|
1073 |
|
rss_check_updates( $_[KERNEL] ); |
1074 |
|
}, |
1075 |
|
irc_372 => sub { |
1076 |
|
_log "<< motd",$_[ARG0],$_[ARG1]; |
1077 |
|
}, |
1078 |
|
irc_375 => sub { |
1079 |
|
_log "<< motd", $_[ARG0], "start"; |
1080 |
}, |
}, |
1081 |
|
irc_376 => sub { |
1082 |
|
_log "<< motd", $_[ARG0], "end"; |
1083 |
|
}, |
1084 |
|
# irc_433 => sub { |
1085 |
|
# print "# irc_433: ",$_[ARG1], "\n"; |
1086 |
|
# warn "## indetify $NICK\n"; |
1087 |
|
# $_[KERNEL]->post( $irc => privmsg => 'nickserv', "IDENTIFY $NICK" ); |
1088 |
|
# }, |
1089 |
|
# irc_451 # please register |
1090 |
irc_477 => sub { |
irc_477 => sub { |
1091 |
_log "# irc_477: ",$_[ARG1]; |
_log "<< irc_477: ",$_[ARG1]; |
1092 |
$_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "register $NICK" ); |
_log ">> IDENTIFY $NICK"; |
1093 |
|
$_[KERNEL]->post( $irc => privmsg => 'NickServ', "IDENTIFY $NICK" ); |
1094 |
}, |
}, |
1095 |
irc_505 => sub { |
irc_505 => sub { |
1096 |
_log "# irc_505: ",$_[ARG1]; |
_log "<< irc_505: ",$_[ARG1]; |
1097 |
$_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "register $NICK" ); |
_log ">> register $NICK"; |
1098 |
# $_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "set hide email on" ); |
$_[KERNEL]->post( $irc => privmsg => 'NickServ', "register $NICK" ); |
1099 |
# $_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "set email dpavlin\@rot13.org" ); |
# $_[KERNEL]->post( $irc => privmsg => 'nickserv', "IDENTIFY $NICK" ); |
1100 |
|
# $_[KERNEL]->post( $irc => privmsg => 'nickserv', "set hide email on" ); |
1101 |
|
# $_[KERNEL]->post( $irc => privmsg => 'nickserv', "set email dpavlin\@rot13.org" ); |
1102 |
}, |
}, |
1103 |
irc_registered => sub { |
irc_registered => sub { |
1104 |
_log "## registrated $NICK"; |
_log "<< registered $NICK"; |
|
$_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "IDENTIFY $NICK" ); |
|
1105 |
}, |
}, |
1106 |
irc_disconnected => sub { |
irc_disconnected => sub { |
1107 |
_log "## disconnected, reconnecting again"; |
_log "## disconnected.. sleeping for $sleep_on_error seconds and reconnecting again"; |
1108 |
$_[KERNEL]->post($IRC_ALIAS => connect => $CONNECT); |
sleep($sleep_on_error); |
1109 |
|
$_[KERNEL]->post( $irc => connect => {} ); |
1110 |
}, |
}, |
1111 |
irc_socketerr => sub { |
irc_socketerr => sub { |
1112 |
_log "## socket error... sleeping for $sleep_on_error seconds and retry"; |
_log "## socket error... sleeping for $sleep_on_error seconds and retry"; |
1113 |
sleep($sleep_on_error); |
sleep($sleep_on_error); |
1114 |
$_[KERNEL]->post($IRC_ALIAS => connect => $CONNECT); |
$_[KERNEL]->post( $irc => connect => {} ); |
1115 |
|
}, |
1116 |
|
irc_notice => sub { |
1117 |
|
_log "<< notice from ", $_[ARG0], $_[ARG1], $_[ARG2]; |
1118 |
|
my $m = $_[ARG2]; |
1119 |
|
if ( $m =~ m!/msg.*(NickServ).*(IDENTIFY)!i ) { |
1120 |
|
_log ">> suggested to $1 $2"; |
1121 |
|
$_[KERNEL]->post( $irc => privmsg => $1, "$2 $NICK" ); |
1122 |
|
} elsif ( $m =~ m!\Q$NICK\E.*registered!i ) { |
1123 |
|
_log ">> registreted, so IDENTIFY"; |
1124 |
|
$_[KERNEL]->post( $irc => privmsg => 'nickserv', "IDENTIFY $NICK" ); |
1125 |
|
} else { |
1126 |
|
warn "## ignore $m\n" if $debug; |
1127 |
|
} |
1128 |
|
}, |
1129 |
|
irc_snotice => sub { |
1130 |
|
_log "<< snotice", $_[ARG0]; #dump( $_[ARG0],$_[ARG1], $_[ARG2] ); |
1131 |
|
if ( $_[ARG0] =~ m!/(QUOTE)\s+(PASS\s+\d+)!i ) { |
1132 |
|
warn ">> $1 | $2\n"; |
1133 |
|
$_[KERNEL]->post( $irc => lc($1) => $2); |
1134 |
|
} |
1135 |
}, |
}, |
|
# irc_433 => sub { |
|
|
# print "# irc_433: ",$_[ARG1], "\n"; |
|
|
# warn "## indetify $NICK\n"; |
|
|
# $_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "IDENTIFY $NICK" ); |
|
|
# }, |
|
1136 |
_child => sub {}, |
_child => sub {}, |
1137 |
_default => sub { |
_default => sub { |
1138 |
_log sprintf "sID:%s %s %s", |
_log sprintf "sID:%s %s %s", |
1142 |
""; |
""; |
1143 |
0; # false for signals |
0; # false for signals |
1144 |
}, |
}, |
1145 |
my_add => sub { |
rss_response => sub { |
1146 |
my $trailing = $_[ARG0]; |
my ($request_packet, $response_packet) = @_[ARG0, ARG1]; |
1147 |
my $session = $_[SESSION]; |
my $request_object = $request_packet->[0]; |
1148 |
POE::Session->create |
my $response_object = $response_packet->[0]; |
1149 |
(inline_states => |
|
1150 |
{_start => sub { |
my $row = delete( $_stat->{rss}->{fetch}->{ $request_object->uri } ); |
1151 |
$_[HEAP]->{wheel} = |
if ( $row ) { |
1152 |
POE::Wheel::FollowTail->new |
$row->{xml} = $response_object->content; |
1153 |
( |
rss_parse_xml( $row ); |
1154 |
Filename => $FOLLOWS{$trailing}, |
} else { |
1155 |
InputEvent => 'got_line', |
warn "## can't find rss->fetch for ", $request_object->uri; |
1156 |
); |
} |
1157 |
}, |
}, |
|
got_line => sub { |
|
|
$_[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); |
|
|
} |
|
1158 |
}, |
}, |
1159 |
); |
); |
1160 |
|
|
1161 |
# http server |
# http server |
1162 |
|
|
1163 |
|
_log "WEB archive at $url"; |
1164 |
|
|
1165 |
my $httpd = POE::Component::Server::HTTP->new( |
my $httpd = POE::Component::Server::HTTP->new( |
1166 |
Port => $http_port, |
Port => $http_port, |
1167 |
|
PreHandler => { |
1168 |
|
'/' => sub { |
1169 |
|
$_[0]->header(Connection => 'close') |
1170 |
|
} |
1171 |
|
}, |
1172 |
ContentHandler => { '/' => \&root_handler }, |
ContentHandler => { '/' => \&root_handler }, |
1173 |
Headers => { Server => 'irc-logger' }, |
Headers => { Server => 'irc-logger' }, |
1174 |
); |
); |
1208 |
$style .= ".col-${max_color} { background: $c }\n"; |
$style .= ".col-${max_color} { background: $c }\n"; |
1209 |
$max_color++; |
$max_color++; |
1210 |
} |
} |
1211 |
warn "defined $max_color colors for users...\n"; |
_log "WEB defined $max_color colors for users..."; |
1212 |
|
|
1213 |
sub root_handler { |
sub root_handler { |
1214 |
my ($request, $response) = @_; |
my ($request, $response) = @_; |
1215 |
$response->code(RC_OK); |
$response->code(RC_OK); |
1216 |
|
|
1217 |
|
# this doesn't seem to work, so moved to PreHandler |
1218 |
|
#$response->header(Connection => 'close'); |
1219 |
|
|
1220 |
return RC_OK if $request->uri =~ m/favicon.ico$/; |
return RC_OK if $request->uri =~ m/favicon.ico$/; |
1221 |
|
|
1222 |
my $q; |
my $q; |
1230 |
} |
} |
1231 |
|
|
1232 |
my $search = $q->param('search') || $q->param('grep') || ''; |
my $search = $q->param('search') || $q->param('grep') || ''; |
1233 |
|
my $r_url = $request->url; |
1234 |
|
|
1235 |
|
my @commands = qw( tags last-tag follow stat ); |
1236 |
|
my $commands_re = join('|',@commands); |
1237 |
|
|
1238 |
|
if ($r_url =~ m#/rss(?:/($commands_re.*)\w*(?:=(\d+))?)?#i) { |
1239 |
|
my $show = lc($1); |
1240 |
|
my $nr = $2; |
1241 |
|
|
|
if ($request->url =~ m#/rss#i) { |
|
1242 |
my $type = 'RSS'; # Atom |
my $type = 'RSS'; # Atom |
1243 |
|
|
1244 |
$response->content_type( 'application/' . lc($type) . '+xml' ); |
$response->content_type( 'application/' . lc($type) . '+xml' ); |
1245 |
|
|
1246 |
my $html = '<!-- error -->'; |
my $html = '<!-- error -->'; |
1247 |
warn "create $type feed from ",dump( @last_tags ); |
#warn "create $type feed from ",dump( @last_tags ); |
1248 |
|
|
1249 |
my $feed = XML::Feed->new( $type ); |
my $feed = XML::Feed->new( $type ); |
1250 |
|
$feed->link( $url ); |
1251 |
|
|
1252 |
|
my $rc = RC_OK; |
1253 |
|
|
1254 |
|
if ( $show eq 'tags' ) { |
1255 |
|
$nr ||= 50; |
1256 |
|
$feed->title( "tags from $CHANNEL" ); |
1257 |
|
$feed->link( "$url/tags" ); |
1258 |
|
$feed->description( "tag cloud created from messages on channel $CHANNEL which have tags// in them" ); |
1259 |
|
my $feed_entry = XML::Feed::Entry->new($type); |
1260 |
|
$feed_entry->title( "$nr tags from $CHANNEL" ); |
1261 |
|
$feed_entry->author( $NICK ); |
1262 |
|
$feed_entry->link( '/#tags' ); |
1263 |
|
|
1264 |
|
$feed_entry->content( |
1265 |
|
qq{<![CDATA[<style type="text/css">} |
1266 |
|
. $cloud->css |
1267 |
|
. qq{</style>} |
1268 |
|
. $cloud->html( $nr ) |
1269 |
|
. qq{]]>} |
1270 |
|
); |
1271 |
|
$feed->add_entry( $feed_entry ); |
1272 |
|
|
1273 |
|
} elsif ( $show eq 'last-tag' ) { |
1274 |
|
|
1275 |
|
$nr ||= $last_x_tags; |
1276 |
|
$nr = $last_x_tags if $nr > $last_x_tags; |
1277 |
|
|
1278 |
|
$feed->title( "last $nr tagged messages from $CHANNEL" ); |
1279 |
|
$feed->description( "collects messages which have tags// in them" ); |
1280 |
|
|
1281 |
|
foreach my $m ( @last_tags ) { |
1282 |
|
# warn dump( $m ); |
1283 |
|
#my $tags = join(' ', @{$m->{tags}} ); |
1284 |
|
my $feed_entry = XML::Feed::Entry->new($type); |
1285 |
|
$feed_entry->title( $m->{nick} . '@' . $m->{time} ); |
1286 |
|
$feed_entry->author( $m->{nick} ); |
1287 |
|
$feed_entry->link( '/#' . $m->{id} ); |
1288 |
|
$feed_entry->issued( DateTime::Format::Flexible->build( $m->{time} ) ); |
1289 |
|
|
1290 |
|
my $message = $filter->{message}->( $m->{message} ); |
1291 |
|
$message .= "<br/>\n" unless $message =~ m!<(/p|br/?)>!; |
1292 |
|
# warn "## message = $message\n"; |
1293 |
|
|
1294 |
|
#$feed_entry->summary( |
1295 |
|
$feed_entry->content( |
1296 |
|
"<![CDATA[$message]]>" |
1297 |
|
); |
1298 |
|
$feed_entry->category( join(', ', @{$m->{tags}}) ); |
1299 |
|
$feed->add_entry( $feed_entry ); |
1300 |
|
|
1301 |
|
$nr--; |
1302 |
|
last if $nr <= 0; |
1303 |
|
|
1304 |
|
} |
1305 |
|
|
1306 |
|
} elsif ( $show =~ m/^follow/ ) { |
1307 |
|
|
1308 |
|
$feed->title( "Feeds which this bot follows" ); |
1309 |
|
|
1310 |
|
my $sth = $dbh->prepare( qq{ select * from feeds order by last_update desc } ); |
1311 |
|
$sth->execute; |
1312 |
|
while (my $row = $sth->fetchrow_hashref) { |
1313 |
|
my $feed_entry = XML::Feed::Entry->new($type); |
1314 |
|
$feed_entry->title( $row->{name} ); |
1315 |
|
$feed_entry->link( $row->{url} ); |
1316 |
|
$feed_entry->issued( DateTime::Format::Flexible->build( $row->{last_update} ) ); |
1317 |
|
$feed_entry->content( |
1318 |
|
'<![CDATA[<pre>' . dump( $row ) . '</pre>]]>' |
1319 |
|
); |
1320 |
|
$feed->add_entry( $feed_entry ); |
1321 |
|
} |
1322 |
|
|
1323 |
|
} elsif ( $show =~ m/^stat/ ) { |
1324 |
|
|
|
$feed->title( "last $last_x_tags from $CHANNEL" ); |
|
|
# $feed->link( "http://$http_hostname:$http_port" ); |
|
|
$feed->description( "collects messages which have tags// in them" ); |
|
|
|
|
|
foreach my $m ( @last_tags ) { |
|
|
# warn dump( $m ); |
|
|
#my $tags = join(' ', @{$m->{tags}} ); |
|
1325 |
my $feed_entry = XML::Feed::Entry->new($type); |
my $feed_entry = XML::Feed::Entry->new($type); |
1326 |
$feed_entry->title( $m->{nick} . '@' . $m->{time} ); |
$feed_entry->title( "Internal stats" ); |
|
$feed_entry->author( $m->{nick} ); |
|
|
$feed_entry->link( '/#' . $m->{id} ); |
|
|
$feed_entry->issued( DateTime::Format::Flexible->build( $m->{time} ) ); |
|
|
#$feed_entry->summary( |
|
1327 |
$feed_entry->content( |
$feed_entry->content( |
1328 |
'<![CDATA[' . |
'<![CDATA[<pre>' . dump( $_stat ) . '</pre>]]>' |
|
# $filter->{nick}->( $m->{nick} ) . |
|
|
# '<tt>' . $m->{nick} . '</tt> ' . |
|
|
$filter->{message}->( $m->{message} ) . |
|
|
"<br/>\n]]>" |
|
1329 |
); |
); |
|
$feed_entry->category( join(', ', @{$m->{tags}}) ); |
|
1330 |
$feed->add_entry( $feed_entry ); |
$feed->add_entry( $feed_entry ); |
1331 |
|
|
1332 |
|
} else { |
1333 |
|
_log "WEB unknown rss request $r_url"; |
1334 |
|
$feed->title( "unknown $r_url" ); |
1335 |
|
foreach my $c ( @commands ) { |
1336 |
|
my $feed_entry = XML::Feed::Entry->new($type); |
1337 |
|
$feed_entry->title( "rss/$c" ); |
1338 |
|
$feed_entry->link( "$url/rss/$c" ); |
1339 |
|
$feed->add_entry( $feed_entry ); |
1340 |
|
} |
1341 |
|
$rc = RC_DENY; |
1342 |
} |
} |
1343 |
|
|
1344 |
$response->content( $feed->as_xml ); |
$response->content( $feed->as_xml ); |
1345 |
return RC_OK; |
return $rc; |
1346 |
} |
} |
1347 |
|
|
1348 |
if ( $@ ) { |
if ( $@ ) { |
1349 |
warn "$@"; |
warn "$@"; |
1350 |
} |
} |
1351 |
|
|
1352 |
$response->content_type("text/html; charset=$ENCODING"); |
$response->content_type("text/html; charset=UTF-8"); |
1353 |
|
|
1354 |
my $html = |
my $html = |
1355 |
qq{<html><head><title>$NICK</title><style type="text/css">$style} . |
qq{<html><head><title>$NICK</title><style type="text/css">$style} |
1356 |
$cloud->css . |
. $cloud->css |
1357 |
qq{</style></head><body>} . |
. qq{</style></head><body>} |
1358 |
qq{ |
. qq{ |
1359 |
<form method="post" class="search" action="/"> |
<form method="post" class="search" action="/"> |
1360 |
<input type="text" name="search" value="$search" size="10"> |
<input type="text" name="search" value="$search" size="10"> |
1361 |
<input type="submit" value="search"> |
<input type="submit" value="search"> |
1362 |
</form> |
</form> |
1363 |
} . |
} |
1364 |
$cloud->html(500) . |
. $cloud->html(500) |
1365 |
qq{<p>}; |
. qq{<p>}; |
1366 |
if ($request->url =~ m#/history#) { |
|
1367 |
|
if ($request->url =~ m#/tags?#) { |
1368 |
|
# nop |
1369 |
|
} elsif ($request->url =~ m#/history#) { |
1370 |
my $sth = $dbh->prepare(qq{ |
my $sth = $dbh->prepare(qq{ |
1371 |
select date(time) as date,count(*) as nr,sum(length(message)) as len |
select date(time) as date,count(*) as nr,sum(length(message)) as len |
1372 |
from log |
from log |
1398 |
$cal->weekdays('MON','TUE','WED','THU','FRI'); |
$cal->weekdays('MON','TUE','WED','THU','FRI'); |
1399 |
($l_yyyy,$l_mm) = ($yyyy,$mm); |
($l_yyyy,$l_mm) = ($yyyy,$mm); |
1400 |
} |
} |
1401 |
$cal->setcontent($dd, qq{ |
$cal->setcontent($dd, qq[ |
1402 |
<a href="$url?date=$row->{date}">$row->{nr}</a><br/>$row->{len} |
<a href="$url?date=$row->{date}">$row->{nr}</a><br/>$row->{len} |
1403 |
}); |
]) if $cal; |
1404 |
|
|
1405 |
} |
} |
1406 |
$html .= qq{<td valign="top">} . $cal->as_HTML() . qq{</td></tr></table>}; |
$html .= qq{<td valign="top">} . $cal->as_HTML() . qq{</td></tr></table>}; |
1433 |
<p>See <a href="/history">history</a> of all messages.</p> |
<p>See <a href="/history">history</a> of all messages.</p> |
1434 |
</body></html>}; |
</body></html>}; |
1435 |
|
|
1436 |
$response->content( $html ); |
$response->content( decode('utf-8',$html) ); |
1437 |
warn "<< ", $request->method, $request->uri, " created ", length($html), " bytes\n"; |
warn "<< ", $request->method, " ", $request->uri, " created ", length($html), " bytes\n"; |
1438 |
return RC_OK; |
return RC_OK; |
1439 |
} |
} |
1440 |
|
|