2 |
use strict; |
use strict; |
3 |
$|++; |
$|++; |
4 |
|
|
5 |
use POE qw(Component::IRC Component::Server::HTTP); |
use POE qw(Component::IRC Component::Server::HTTP Component::Client::HTTP); |
6 |
use HTTP::Status; |
use HTTP::Status; |
7 |
use DBI; |
use DBI; |
8 |
use Regexp::Common qw /URI/; |
use Regexp::Common qw /URI/; |
18 |
use Carp qw/confess/; |
use Carp qw/confess/; |
19 |
use XML::Feed; |
use XML::Feed; |
20 |
use DateTime::Format::Flexible; |
use DateTime::Format::Flexible; |
21 |
|
use Encode; |
22 |
|
|
23 |
=head1 NAME |
=head1 NAME |
24 |
|
|
48 |
|
|
49 |
## CONFIG |
## CONFIG |
50 |
|
|
51 |
|
my $debug = 0; |
52 |
|
|
53 |
my $irc_config = { |
my $irc_config = { |
54 |
nick => 'irc-logger', |
nick => 'irc-logger', |
55 |
server => 'irc.freenode.net', |
server => 'irc.freenode.net', |
78 |
|
|
79 |
my @channels = ( $CHANNEL ); |
my @channels = ( $CHANNEL ); |
80 |
|
|
81 |
warn "# config = ", dump( $irc_config ), $/; |
warn "## config = ", dump( $irc_config ) if $debug; |
82 |
|
|
83 |
my $NICK = $irc_config->{nick} or die "no nick?"; |
my $NICK = $irc_config->{nick} or die "no nick?"; |
84 |
|
|
109 |
GetOptions( |
GetOptions( |
110 |
'import-dircproxy:s' => \$import_dircproxy, |
'import-dircproxy:s' => \$import_dircproxy, |
111 |
'log:s' => \$log_path, |
'log:s' => \$log_path, |
112 |
|
'debug!' => \$debug, |
113 |
); |
); |
114 |
|
|
115 |
#$SIG{__DIE__} = sub { |
#$SIG{__DIE__} = sub { |
122 |
|
|
123 |
open(STDOUT, '>', $log_path) && warn "log to $log_path: $!\n"; |
open(STDOUT, '>', $log_path) && warn "log to $log_path: $!\n"; |
124 |
|
|
125 |
|
|
126 |
# HTML formatters |
# HTML formatters |
127 |
|
|
128 |
my %escape = ('<'=>'<', '>'=>'>', '&'=>'&', '"'=>'"'); |
my %escape = ('<'=>'<', '>'=>'>', '&'=>'&', '"'=>'"'); |
605 |
$a->{me} ||= 0; |
$a->{me} ||= 0; |
606 |
$a->{time} ||= strftime($TIMESTAMP,localtime()); |
$a->{time} ||= strftime($TIMESTAMP,localtime()); |
607 |
|
|
608 |
_log |
_log "ARCHIVE", |
609 |
$a->{channel}, " ", |
$a->{channel}, " ", |
610 |
$a->{me} ? "***" . $a->{nick} : "<" . $a->{nick} . ">", |
$a->{me} ? "***" . $a->{nick} : "<" . $a->{nick} . ">", |
611 |
" " . $a->{message}; |
" " . $a->{message}; |
655 |
|
|
656 |
my $_stat; |
my $_stat; |
657 |
|
|
658 |
|
POE::Component::Client::HTTP->spawn( |
659 |
|
Alias => 'rss-fetch', |
660 |
|
Timeout => 30, |
661 |
|
); |
662 |
|
|
663 |
|
=head2 rss_parse_xml |
664 |
|
|
665 |
|
rss_parse_xml({ |
666 |
|
url => 'http://www.example.com/rss', |
667 |
|
send_rss_msgs => 42, |
668 |
|
}); |
669 |
|
|
670 |
sub rss_fetch { |
=cut |
671 |
my ($args) = @_; |
|
672 |
|
sub rss_parse_xml { |
673 |
|
my ($kernel,$args) = @_; |
674 |
|
|
675 |
|
warn "## rss_parse_xml ",dump( @_ ) if $debug; |
676 |
|
|
677 |
# 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? |
678 |
my $send_rss_msgs = 1; |
my $send_rss_msgs = $args->{send_rss_msgs}; |
679 |
|
$send_rss_msgs = 1 if ! defined $send_rss_msgs; |
680 |
|
|
681 |
_log "RSS fetch", $args->{url}; |
warn "## RSS fetch first $send_rss_msgs items from", $args->{url} if $debug; |
682 |
|
|
683 |
my $feed = XML::Feed->parse(URI->new( $args->{url} )); |
my $feed = XML::Feed->parse( \$args->{xml} ); |
684 |
if ( ! $feed ) { |
if ( ! $feed ) { |
685 |
_log("can't fetch RSS ", $args->{url}); |
_log "can't fetch RSS ", $args->{url}, XML::Feed->errstr; |
686 |
return; |
return; |
687 |
} |
} |
688 |
|
|
692 |
for my $entry ($feed->entries) { |
for my $entry ($feed->entries) { |
693 |
$total++; |
$total++; |
694 |
|
|
695 |
|
my $seen_times = $_stat->{rss}->{seen}->{$args->{channel}}->{$feed->link}->{$entry->id}++; |
696 |
# seen allready? |
# seen allready? |
697 |
next if $_stat->{rss}->{seen}->{$args->{channel}}->{$feed->link}->{$entry->id}++ > 0; |
warn "## $seen_times ",$entry->id if $debug; |
698 |
|
next if $seen_times > 0; |
699 |
|
|
700 |
sub prefix { |
sub prefix { |
701 |
my ($txt,$var) = @_; |
my ($txt,$var) = @_; |
728 |
$msg .= prefix( ' ' , $tags ); |
$msg .= prefix( ' ' , $tags ); |
729 |
} |
} |
730 |
|
|
731 |
if ( $args->{kernel} && $send_rss_msgs ) { |
if ( $seen_times == 0 && $send_rss_msgs ) { |
732 |
$send_rss_msgs--; |
$send_rss_msgs--; |
733 |
if ( ! $args->{private} ) { |
if ( ! $args->{private} ) { |
734 |
# FIXME bug! should be save_message |
# FIXME bug! should be save_message |
735 |
# save_message( channel => $args->{channel}, me => 1, nick => $NICK, message => $msg ); |
save_message( channel => $args->{channel}, me => 1, nick => $NICK, message => $msg ); |
736 |
$sth_insert_log->execute( $args->{channel}, 1, $NICK, $msg, 'now()' ); |
# $sth_insert_log->execute( $args->{channel}, 1, $NICK, $msg, 'now()' ); |
737 |
} |
} |
738 |
my ( $type, $to ) = ( 'notice', $args->{channel} ); |
my ( $type, $to ) = ( 'notice', $args->{channel} ); |
739 |
( $type, $to ) = ( 'privmsg', $args->{nick} ) if $args->{private}; |
( $type, $to ) = ( 'privmsg', $args->{nick} ) if $args->{private}; |
740 |
_log(">> $type $to", $msg); |
|
741 |
$args->{kernel}->post( $irc => $type => $to, $msg ); |
_log(">> RSS $type to $to:", $msg); |
742 |
|
$kernel->post( $irc => $type => $to => $msg ); |
743 |
|
|
744 |
$updates++; |
$updates++; |
745 |
} |
} |
746 |
} |
} |
750 |
$sql .= qq{where id = } . $args->{id}; |
$sql .= qq{where id = } . $args->{id}; |
751 |
eval { $dbh->do( $sql ) }; |
eval { $dbh->do( $sql ) }; |
752 |
|
|
753 |
_log "RSS got $total items of which $updates new"; |
_log "RSS $updates/$total new items from", $args->{url}; |
754 |
|
|
755 |
return $updates; |
return $updates; |
756 |
} |
} |
757 |
|
|
758 |
sub rss_fetch_all { |
sub rss_fetch_all { |
759 |
my $kernel = shift; |
my ( $kernel, $send_rss_msgs ) = @_; |
760 |
|
warn "## rss_fetch_all -- send_rss_msgs: $send_rss_msgs\n" if $debug; |
761 |
my $sql = qq{ |
my $sql = qq{ |
762 |
select id, url, name, channel, nick, private |
select id, url, name, channel, nick, private |
763 |
from feeds |
from feeds |
764 |
where active is true |
where active is true |
765 |
}; |
}; |
766 |
# limit to newer feeds only if we are not sending messages out |
# limit to newer feeds only if we are not sending messages out |
767 |
$sql .= qq{ and last_update + delay < now() } if $kernel; |
$sql .= qq{ and last_update + delay < now() } if defined ( $_stat->{rss}->{fetch} ); |
768 |
my $sth = $dbh->prepare( $sql ); |
my $sth = $dbh->prepare( $sql ); |
769 |
$sth->execute(); |
$sth->execute(); |
770 |
warn "# ",$sth->rows," active RSS feeds\n"; |
warn "# ",$sth->rows," active RSS feeds\n"; |
771 |
my $count = 0; |
my $count = 0; |
772 |
while (my $row = $sth->fetchrow_hashref) { |
while (my $row = $sth->fetchrow_hashref) { |
773 |
$row->{kernel} = $kernel if $kernel; |
$row->{send_rss_msgs} = $send_rss_msgs if defined $send_rss_msgs; |
774 |
$count += rss_fetch( $row ); |
$_stat->{rss}->{fetch}->{ $row->{url} } = $row; |
775 |
|
$kernel->post( |
776 |
|
'rss-fetch', |
777 |
|
'request', |
778 |
|
'rss_response', |
779 |
|
HTTP::Request->new( GET => $row->{url} ), |
780 |
|
); |
781 |
|
warn "## queued rss-fetch ", dump( $row ) if $debug; |
782 |
} |
} |
783 |
return "OK, fetched $count posts from " . $sth->rows . " feeds"; |
return "OK, scheduled " . $sth->rows . " feeds for refresh"; |
784 |
} |
} |
785 |
|
|
786 |
|
|
788 |
my $kernel = shift; |
my $kernel = shift; |
789 |
$_stat->{rss}->{last_poll} ||= time(); |
$_stat->{rss}->{last_poll} ||= time(); |
790 |
my $dt = time() - $_stat->{rss}->{last_poll}; |
my $dt = time() - $_stat->{rss}->{last_poll}; |
|
warn "## rss_check_updates $dt > $rss_min_delay\n"; |
|
791 |
if ( $dt > $rss_min_delay ) { |
if ( $dt > $rss_min_delay ) { |
792 |
|
warn "## rss_check_updates $dt > $rss_min_delay\n"; |
793 |
$_stat->{rss}->{last_poll} = time(); |
$_stat->{rss}->{last_poll} = time(); |
794 |
_log rss_fetch_all( $kernel ); |
_log rss_fetch_all( $kernel ); |
795 |
} |
} |
796 |
} |
} |
797 |
|
|
|
# seed rss seen cache so we won't send out all items on startup |
|
|
_log rss_fetch_all; |
|
|
|
|
798 |
POE::Session->create( inline_states => { |
POE::Session->create( inline_states => { |
799 |
_start => sub { |
_start => sub { |
800 |
$_[KERNEL]->post( $irc => register => 'all' ); |
$_[KERNEL]->post( $irc => register => 'all' ); |
805 |
my $poco_object = $sender->get_heap(); |
my $poco_object = $sender->get_heap(); |
806 |
_log "connected to",$poco_object->server_name(); |
_log "connected to",$poco_object->server_name(); |
807 |
$kernel->post( $sender => join => $_ ) for @channels; |
$kernel->post( $sender => join => $_ ) for @channels; |
808 |
|
# seen RSS cache, so don't send out messages |
809 |
|
_log rss_fetch_all( $kernel, 0 ); |
810 |
undef; |
undef; |
811 |
}, |
}, |
812 |
irc_255 => sub { # server is done blabbing |
# irc_255 => sub { # server is done blabbing |
813 |
$_[KERNEL]->post( $irc => join => $CHANNEL); |
# $_[KERNEL]->post( $irc => join => $CHANNEL); |
814 |
}, |
# }, |
815 |
irc_public => sub { |
irc_public => sub { |
816 |
my $kernel = $_[KERNEL]; |
my $kernel = $_[KERNEL]; |
817 |
my $nick = (split /!/, $_[ARG0])[0]; |
my $nick = (split /!/, $_[ARG0])[0]; |
861 |
my $nick = (split /!/, $_[ARG0])[0]; |
my $nick = (split /!/, $_[ARG0])[0]; |
862 |
my $msg = $_[ARG2]; |
my $msg = $_[ARG2]; |
863 |
my $channel = $_[ARG1]->[0]; |
my $channel = $_[ARG1]->[0]; |
864 |
|
warn "# ARG = ",dump( @_[ARG0,ARG1,ARG2] ) if $debug; |
865 |
|
|
866 |
my $res = "unknown command '$msg', try /msg $NICK help!"; |
my $res = "unknown command '$msg', try /msg $NICK help!"; |
867 |
my @out; |
my @out; |
1005 |
|
|
1006 |
my $sql = { |
my $sql = { |
1007 |
add => qq{ insert into feeds (url,name,channel,nick,private) values (?,?,?,?,?) }, |
add => qq{ insert into feeds (url,name,channel,nick,private) values (?,?,?,?,?) }, |
1008 |
# remove => qq{ delete from feeds where url = ? and name = ? }, |
remove => qq{ delete from feeds where url = ? and nick = ? }, |
1009 |
start => qq{ update feeds set active = true where url = ? }, |
start => qq{ update feeds set active = true where url = ? }, |
1010 |
stop => qq{ update feeds set active = false where url = ? }, |
stop => qq{ update feeds set active = false where url = ? }, |
1011 |
clean => qq{ update feeds set last_update = now() - delay where url = ? }, |
clean => qq{ update feeds set last_update = now() - delay where url = ? }, |
1018 |
my @data = ( $url ); |
my @data = ( $url ); |
1019 |
if ( $command eq 'add' ) { |
if ( $command eq 'add' ) { |
1020 |
push @data, ( $arg, $channel, $nick, $sub eq 'private' ? 1 : 0 ); |
push @data, ( $arg, $channel, $nick, $sub eq 'private' ? 1 : 0 ); |
1021 |
|
} elsif ( $command eq 'remove' ) { |
1022 |
|
push @data, $nick; |
1023 |
} |
} |
1024 |
warn "## $command SQL $q with ",dump( @data ),"\n"; |
warn "## $command SQL $q with ",dump( @data ),"\n"; |
1025 |
eval { $sth->execute( @data ) }; |
eval { $sth->execute( @data ) }; |
1026 |
if ($@) { |
if ($@) { |
1027 |
$res = "ERROR: $@"; |
$res = "ERROR: $@"; |
1028 |
} else { |
} else { |
1029 |
$res = "OK, RSS executed $command " . ( $sub ? "-$sub" : '' ) ."on $channel url $url"; |
$res = "OK, RSS executed $command" . |
1030 |
|
( $sub ? "-$sub " : ' ' ) . |
1031 |
|
( $channel ? "on $channel " : '' ) . |
1032 |
|
"url $url"; |
1033 |
if ( $command eq 'clean' ) { |
if ( $command eq 'clean' ) { |
1034 |
my $seen = $_stat->{rss}->{seen} || die "no seen?"; |
my $seen = $_stat->{rss}->{seen} || die "no seen?"; |
1035 |
my $want_link = $_stat->{rss}->{url2link}->{$url} || warn "no url2link($url)"; |
my $want_link = $_stat->{rss}->{url2link}->{$url} || warn "no url2link($url)"; |
1041 |
_log "RSS removed seen $c $url $link"; |
_log "RSS removed seen $c $url $link"; |
1042 |
} |
} |
1043 |
} |
} |
1044 |
|
} elsif ( $command eq 'add' ) { |
1045 |
|
rss_fetch_all( $_[KERNEL] ); |
1046 |
} |
} |
1047 |
} |
} |
1048 |
} else { |
} else { |
1052 |
# this makes sense because we didn't catch rss-clean http://... before! |
# this makes sense because we didn't catch rss-clean http://... before! |
1053 |
$_stat->{rss} = undef; |
$_stat->{rss} = undef; |
1054 |
$dbh->do( qq{ update feeds set last_update = now() - delay } ); |
$dbh->do( qq{ update feeds set last_update = now() - delay } ); |
1055 |
$res = "OK, cleaned RSS cache"; |
$res = rss_fetch_all( $_[KERNEL] ); |
1056 |
} |
} |
1057 |
|
|
1058 |
if ($res) { |
if ($res) { |
1113 |
_log ">> registreted, so IDENTIFY"; |
_log ">> registreted, so IDENTIFY"; |
1114 |
$_[KERNEL]->post( $irc => privmsg => 'nickserv', "IDENTIFY $NICK" ); |
$_[KERNEL]->post( $irc => privmsg => 'nickserv', "IDENTIFY $NICK" ); |
1115 |
} else { |
} else { |
1116 |
warn "## ignore $m\n"; |
warn "## ignore $m\n" if $debug; |
1117 |
} |
} |
1118 |
}, |
}, |
1119 |
irc_snotice => sub { |
irc_snotice => sub { |
1132 |
""; |
""; |
1133 |
0; # false for signals |
0; # false for signals |
1134 |
}, |
}, |
1135 |
|
rss_response => sub { |
1136 |
|
my ($request_packet, $response_packet) = @_[ARG0, ARG1]; |
1137 |
|
my $request_object = $request_packet->[0]; |
1138 |
|
my $response_object = $response_packet->[0]; |
1139 |
|
|
1140 |
|
my $row = delete( $_stat->{rss}->{fetch}->{ $request_object->uri } ); |
1141 |
|
if ( $row ) { |
1142 |
|
$row->{xml} = $response_object->content; |
1143 |
|
rss_parse_xml( $_[KERNEL], $row ); |
1144 |
|
} else { |
1145 |
|
warn "## can't find rss->fetch for ", $request_object->uri; |
1146 |
|
} |
1147 |
|
}, |
1148 |
}, |
}, |
1149 |
); |
); |
1150 |
|
|
1423 |
<p>See <a href="/history">history</a> of all messages.</p> |
<p>See <a href="/history">history</a> of all messages.</p> |
1424 |
</body></html>}; |
</body></html>}; |
1425 |
|
|
1426 |
$response->content( $html ); |
$response->content( decode('utf-8',$html) ); |
1427 |
warn "<< ", $request->method, " ", $request->uri, " created ", length($html), " bytes\n"; |
warn "<< ", $request->method, " ", $request->uri, " created ", length($html), " bytes\n"; |
1428 |
return RC_OK; |
return RC_OK; |
1429 |
} |
} |