61 |
my $CHANNEL = '#razmjenavjestina'; |
my $CHANNEL = '#razmjenavjestina'; |
62 |
|
|
63 |
if ( $HOSTNAME =~ m/llin/ ) { |
if ( $HOSTNAME =~ m/llin/ ) { |
64 |
$irc_config->{nick} = 'irc-logger-dev'; |
$irc_config->{nick} = 'irc-logger-llin'; |
65 |
# $irc_config = { |
# $irc_config = { |
66 |
# nick => 'irc-logger-dev', |
# nick => 'irc-logger-llin', |
67 |
# server => 'localhost', |
# server => 'localhost', |
68 |
# port => 6668, |
# port => 6668, |
69 |
# }; |
# }; |
112 |
# confess "fatal error"; |
# confess "fatal error"; |
113 |
#}; |
#}; |
114 |
|
|
|
open(STDOUT, '>', $log_path) || warn "can't redirect log to $log_path: $!"; |
|
|
|
|
115 |
sub _log { |
sub _log { |
116 |
print strftime($TIMESTAMP,localtime()) . ' ' . join(" ",@_) . $/; |
print strftime($TIMESTAMP,localtime()) . ' ' . join(" ",map { ref($_) ? dump( $_ ) : $_ } @_) . $/; |
117 |
} |
} |
118 |
|
|
119 |
|
open(STDOUT, '>', $log_path) && warn "log to $log_path: $!\n"; |
120 |
|
|
121 |
# HTML formatters |
# HTML formatters |
122 |
|
|
123 |
my %escape = ('<'=>'<', '>'=>'>', '&'=>'&', '"'=>'"'); |
my %escape = ('<'=>'<', '>'=>'>', '&'=>'&', '"'=>'"'); |
160 |
}, |
}, |
161 |
}; |
}; |
162 |
|
|
163 |
|
# POE IRC |
164 |
|
my $poe_irc = POE::Component::IRC->spawn( %$irc_config ) or |
165 |
|
die "can't start ", dump( $irc_config ), ": $!"; |
166 |
|
|
167 |
|
my $irc = $poe_irc->session_id(); |
168 |
|
_log "IRC session_id $irc"; |
169 |
|
|
170 |
my $dbh = DBI->connect($DSN,"","", { RaiseError => 1, AutoCommit => 1 }) || die $DBI::errstr; |
my $dbh = DBI->connect($DSN,"","", { RaiseError => 1, AutoCommit => 1 }) || die $DBI::errstr; |
171 |
$dbh->do( qq{ set client_encoding = 'UTF-8' } ); |
$dbh->do( qq{ set client_encoding = 'UTF-8' } ); |
172 |
|
|
665 |
return; |
return; |
666 |
} |
} |
667 |
|
|
668 |
|
$_stat->{rss}->{url2link}->{ $args->{url} } = $feed->link; |
669 |
|
|
670 |
my ( $total, $updates ) = ( 0, 0 ); |
my ( $total, $updates ) = ( 0, 0 ); |
671 |
for my $entry ($feed->entries) { |
for my $entry ($feed->entries) { |
672 |
$total++; |
$total++; |
698 |
$msg .= prefix( ' | ' , $entry->title ); |
$msg .= prefix( ' | ' , $entry->title ); |
699 |
$msg .= prefix( ' | ' , $link ); |
$msg .= prefix( ' | ' , $link ); |
700 |
# $msg .= prefix( ' id ' , $entry->id ); |
# $msg .= prefix( ' id ' , $entry->id ); |
701 |
|
if ( my $tags = $entry->category ) { |
702 |
|
$tags =~ s!^\s+!!; |
703 |
|
$tags =~ s!\s*$! !; |
704 |
|
$tags =~ s!,?\s+!// !g; |
705 |
|
$msg .= prefix( ' ' , $tags ); |
706 |
|
} |
707 |
|
|
708 |
if ( $args->{kernel} && $send_rss_msgs ) { |
if ( $args->{kernel} && $send_rss_msgs ) { |
709 |
$send_rss_msgs--; |
$send_rss_msgs--; |
714 |
} |
} |
715 |
my ( $type, $to ) = ( 'notice', $args->{channel} ); |
my ( $type, $to ) = ( 'notice', $args->{channel} ); |
716 |
( $type, $to ) = ( 'privmsg', $args->{nick} ) if $args->{private}; |
( $type, $to ) = ( 'privmsg', $args->{nick} ) if $args->{private}; |
717 |
_log(">> $type $to |", $msg); |
_log(">> $type $to", $msg); |
718 |
$args->{kernel}->post( irc => $type => $to, $msg ); |
$args->{kernel}->post( $irc => $type => $to, $msg ); |
719 |
$updates++; |
$updates++; |
720 |
} |
} |
721 |
} |
} |
765 |
# seed rss seen cache so we won't send out all items on startup |
# seed rss seen cache so we won't send out all items on startup |
766 |
_log rss_fetch_all; |
_log rss_fetch_all; |
767 |
|
|
|
# |
|
|
# POE handing part |
|
|
# |
|
|
|
|
|
my $poe_irc = POE::Component::IRC->spawn( %$irc_config ) or |
|
|
die "can't start ", dump( $irc_config ), ": $!"; |
|
|
|
|
|
my $irc = $poe_irc->session_id(); |
|
|
_log "session_id $irc"; |
|
|
|
|
768 |
POE::Session->create( inline_states => { |
POE::Session->create( inline_states => { |
769 |
_start => sub { |
_start => sub { |
770 |
$_[KERNEL]->post( $irc => register => 'all' ); |
$_[KERNEL]->post( $irc => register => 'all' ); |
779 |
}, |
}, |
780 |
irc_255 => sub { # server is done blabbing |
irc_255 => sub { # server is done blabbing |
781 |
$_[KERNEL]->post( $irc => join => $CHANNEL); |
$_[KERNEL]->post( $irc => join => $CHANNEL); |
|
$_[KERNEL]->post( $irc => privmsg => 'nickserv', "IDENTIFY $NICK" ); |
|
782 |
}, |
}, |
783 |
irc_public => sub { |
irc_public => sub { |
784 |
my $kernel = $_[KERNEL]; |
my $kernel = $_[KERNEL]; |
839 |
|
|
840 |
$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"; |
841 |
|
|
842 |
} elsif ($msg =~ m/^msg\s+(\S+)\s+(.*)$/i) { |
} elsif ($msg =~ m/^(privmsg|notice)\s+(\S+)\s+(.*)$/i) { |
843 |
|
|
844 |
_log ">> /msg $1 $2"; |
_log ">> /$1 $2 $3"; |
845 |
$_[KERNEL]->post( $irc => privmsg => $1, $2 ); |
$_[KERNEL]->post( $irc => $1 => $2, $3 ); |
846 |
$res = ''; |
$res = ''; |
847 |
|
|
848 |
} elsif ($msg =~ m/^stat.*?\s*(\d*)/i) { |
} elsif ($msg =~ m/^stat.*?\s*(\d*)/i) { |
957 |
} |
} |
958 |
} elsif ($msg =~ m/^rss-update/) { |
} elsif ($msg =~ m/^rss-update/) { |
959 |
$res = rss_fetch_all( $_[KERNEL] ); |
$res = rss_fetch_all( $_[KERNEL] ); |
|
} elsif ($msg =~ m/^rss-clean/) { |
|
|
$_stat->{rss} = undef; |
|
|
$dbh->do( qq{ update feeds set last_update = now() - delay } ); |
|
|
$res = "OK, cleaned RSS cache"; |
|
960 |
} elsif ($msg =~ m/^rss-list/) { |
} elsif ($msg =~ m/^rss-list/) { |
961 |
my $sth = $dbh->prepare(qq{ select url,name,last_update,active,channel,nick,private from feeds }); |
my $sth = $dbh->prepare(qq{ select url,name,last_update,active,channel,nick,private from feeds }); |
962 |
$sth->execute; |
$sth->execute; |
964 |
$_[KERNEL]->post( $irc => privmsg => $nick, join(' | ',@row) ); |
$_[KERNEL]->post( $irc => privmsg => $nick, join(' | ',@row) ); |
965 |
} |
} |
966 |
$res = ''; |
$res = ''; |
967 |
} elsif ($msg =~ m!^rss-(add|remove|stop|start)(?:-(private))?\s+(http://\S+)\s*(.*)!) { |
} elsif ($msg =~ m!^rss-(add|remove|stop|start|clean)(?:-(private))?\s+(http://\S+)\s*(.*)!) { |
968 |
my ( $command, $sub, $url, $arg ) = ( $1,$2,$3,$4 ); |
my ( $command, $sub, $url, $arg ) = ( $1,$2,$3,$4 ); |
969 |
|
|
970 |
my $channel = $1 if ( $arg =~ s/\s*(#\S+)\s*// ); |
my $channel = $1 if ( $arg =~ s/\s*(#\S+)\s*// ); |
975 |
# remove => qq{ delete from feeds where url = ? and name = ? }, |
# remove => qq{ delete from feeds where url = ? and name = ? }, |
976 |
start => qq{ update feeds set active = true where url = ? }, |
start => qq{ update feeds set active = true where url = ? }, |
977 |
stop => qq{ update feeds set active = false where url = ? }, |
stop => qq{ update feeds set active = false where url = ? }, |
978 |
|
clean => qq{ update feeds set last_update = now() - delay where url = ? }, |
979 |
}; |
}; |
980 |
|
|
981 |
if ( $command eq 'add' && ! $channel ) { |
if ( $command eq 'add' && ! $channel ) { |
991 |
if ($@) { |
if ($@) { |
992 |
$res = "ERROR: $@"; |
$res = "ERROR: $@"; |
993 |
} else { |
} else { |
994 |
$res = "OK, RSS [$command|$sub|$url|$arg]"; |
$res = "OK, RSS executed $command " . ( $sub ? "-$sub" : '' ) ."on $channel url $url"; |
995 |
|
if ( $command eq 'clean' ) { |
996 |
|
my $seen = $_stat->{rss}->{seen} || die "no seen?"; |
997 |
|
my $want_link = $_stat->{rss}->{url2link}->{$url} || warn "no url2link($url)"; |
998 |
|
foreach my $c ( keys %$seen ) { |
999 |
|
my $c_hash = $seen->{$c} || die "no seen->{$c}"; |
1000 |
|
die "not HASH with rss links but ", dump($c_hash) unless ref($c_hash) eq 'HASH'; |
1001 |
|
foreach my $link ( keys %$c_hash ) { |
1002 |
|
next unless $link eq $want_link; |
1003 |
|
_log "RSS removed seen $c $url $link"; |
1004 |
|
} |
1005 |
|
} |
1006 |
|
} |
1007 |
} |
} |
1008 |
} else { |
} else { |
1009 |
$res = "ERROR: don't know what to do with: $msg"; |
$res = "ERROR: don't know what to do with: $msg"; |
1010 |
} |
} |
1011 |
|
} elsif ($msg =~ m/^rss-clean/) { |
1012 |
|
# this makes sense because we didn't catch rss-clean http://... before! |
1013 |
|
$_stat->{rss} = undef; |
1014 |
|
$dbh->do( qq{ update feeds set last_update = now() - delay } ); |
1015 |
|
$res = "OK, cleaned RSS cache"; |
1016 |
} |
} |
1017 |
|
|
1018 |
if ($res) { |
if ($res) { |
1031 |
irc_376 => sub { |
irc_376 => sub { |
1032 |
_log "<< motd", $_[ARG0], "end"; |
_log "<< motd", $_[ARG0], "end"; |
1033 |
}, |
}, |
1034 |
|
# irc_433 => sub { |
1035 |
|
# print "# irc_433: ",$_[ARG1], "\n"; |
1036 |
|
# warn "## indetify $NICK\n"; |
1037 |
|
# $_[KERNEL]->post( $irc => privmsg => 'nickserv', "IDENTIFY $NICK" ); |
1038 |
|
# }, |
1039 |
|
# irc_451 # please register |
1040 |
irc_477 => sub { |
irc_477 => sub { |
1041 |
_log "<< irc_477: ",$_[ARG1]; |
_log "<< irc_477: ",$_[ARG1]; |
1042 |
$_[KERNEL]->post( $irc => privmsg => 'nickserv', "register $NICK" ); |
_log ">> IDENTIFY $NICK"; |
1043 |
|
$_[KERNEL]->post( $irc => privmsg => 'NickServ', "IDENTIFY $NICK" ); |
1044 |
}, |
}, |
1045 |
irc_505 => sub { |
irc_505 => sub { |
1046 |
_log "<< irc_505: ",$_[ARG1]; |
_log "<< irc_505: ",$_[ARG1]; |
1047 |
$_[KERNEL]->post( $irc => privmsg => 'nickserv', "register $NICK" ); |
_log ">> register $NICK"; |
1048 |
|
$_[KERNEL]->post( $irc => privmsg => 'NickServ', "register $NICK" ); |
1049 |
|
# $_[KERNEL]->post( $irc => privmsg => 'nickserv', "IDENTIFY $NICK" ); |
1050 |
# $_[KERNEL]->post( $irc => privmsg => 'nickserv', "set hide email on" ); |
# $_[KERNEL]->post( $irc => privmsg => 'nickserv', "set hide email on" ); |
1051 |
# $_[KERNEL]->post( $irc => privmsg => 'nickserv', "set email dpavlin\@rot13.org" ); |
# $_[KERNEL]->post( $irc => privmsg => 'nickserv', "set email dpavlin\@rot13.org" ); |
1052 |
}, |
}, |
1063 |
sleep($sleep_on_error); |
sleep($sleep_on_error); |
1064 |
$_[KERNEL]->post( $irc => connect => {} ); |
$_[KERNEL]->post( $irc => connect => {} ); |
1065 |
}, |
}, |
|
# irc_433 => sub { |
|
|
# print "# irc_433: ",$_[ARG1], "\n"; |
|
|
# warn "## indetify $NICK\n"; |
|
|
# $_[KERNEL]->post( $irc => privmsg => 'nickserv', "IDENTIFY $NICK" ); |
|
|
# }, |
|
|
# irc_451 # please register |
|
1066 |
irc_notice => sub { |
irc_notice => sub { |
1067 |
_log "<< notice",$_[ARG0]; |
_log "<< notice from ", $_[ARG0], $_[ARG1], $_[ARG2]; |
1068 |
if ( $_[ARG0] =~ m!/msg\s+NickServ\s+IDENTIFY!i ) { |
my $m = $_[ARG2]; |
1069 |
|
if ( $m =~ m!/msg.*(NickServ).*(IDENTIFY)!i ) { |
1070 |
|
_log ">> suggested to $1 $2"; |
1071 |
|
$_[KERNEL]->post( $irc => privmsg => $1, "$2 $NICK" ); |
1072 |
|
} elsif ( $m =~ m!\Q$NICK\E.*registered!i ) { |
1073 |
|
_log ">> registreted, so IDENTIFY"; |
1074 |
$_[KERNEL]->post( $irc => privmsg => 'nickserv', "IDENTIFY $NICK" ); |
$_[KERNEL]->post( $irc => privmsg => 'nickserv', "IDENTIFY $NICK" ); |
1075 |
|
} else { |
1076 |
|
warn "## ignore $m\n"; |
1077 |
} |
} |
1078 |
}, |
}, |
1079 |
irc_snotice => sub { |
irc_snotice => sub { |
1080 |
_log "<< snotice",$_[ARG0]; |
_log "<< snotice", $_[ARG0]; #dump( $_[ARG0],$_[ARG1], $_[ARG2] ); |
1081 |
if ( $_[ARG0] =~ m!/(QUOTE)\s+(PASS\s+\d+)!i ) { |
if ( $_[ARG0] =~ m!/(QUOTE)\s+(PASS\s+\d+)!i ) { |
1082 |
warn ">> $1 | $2\n"; |
warn ">> $1 | $2\n"; |
1083 |
$_[KERNEL]->post( $irc => lc($1) => $2); |
$_[KERNEL]->post( $irc => lc($1) => $2); |
1097 |
|
|
1098 |
# http server |
# http server |
1099 |
|
|
1100 |
|
_log "WEB archive at $url"; |
1101 |
|
|
1102 |
my $httpd = POE::Component::Server::HTTP->new( |
my $httpd = POE::Component::Server::HTTP->new( |
1103 |
Port => $http_port, |
Port => $http_port, |
1104 |
PreHandler => { |
PreHandler => { |
1145 |
$style .= ".col-${max_color} { background: $c }\n"; |
$style .= ".col-${max_color} { background: $c }\n"; |
1146 |
$max_color++; |
$max_color++; |
1147 |
} |
} |
1148 |
warn "defined $max_color colors for users...\n"; |
_log "WEB defined $max_color colors for users..."; |
1149 |
|
|
1150 |
sub root_handler { |
sub root_handler { |
1151 |
my ($request, $response) = @_; |
my ($request, $response) = @_; |
1267 |
$feed->add_entry( $feed_entry ); |
$feed->add_entry( $feed_entry ); |
1268 |
|
|
1269 |
} else { |
} else { |
1270 |
_log "unknown rss request $r_url"; |
_log "WEB unknown rss request $r_url"; |
1271 |
$feed->title( "unknown $r_url" ); |
$feed->title( "unknown $r_url" ); |
1272 |
foreach my $c ( @commands ) { |
foreach my $c ( @commands ) { |
1273 |
my $feed_entry = XML::Feed::Entry->new($type); |
my $feed_entry = XML::Feed::Entry->new($type); |