--- trunk/bin/irc-logger.pl 2008/03/09 22:12:06 111 +++ trunk/bin/irc-logger.pl 2008/03/12 18:14:57 117 @@ -61,9 +61,9 @@ my $CHANNEL = '#razmjenavjestina'; if ( $HOSTNAME =~ m/llin/ ) { - $irc_config->{nick} = 'irc-logger-dev'; + $irc_config->{nick} = 'irc-logger-llin'; # $irc_config = { -# nick => 'irc-logger-dev', +# nick => 'irc-logger-llin', # server => 'localhost', # port => 6668, # }; @@ -112,12 +112,12 @@ # confess "fatal error"; #}; -open(STDOUT, '>', $log_path) || warn "can't redirect log to $log_path: $!"; - sub _log { - print strftime($TIMESTAMP,localtime()) . ' ' . join(" ",@_) . $/; + print strftime($TIMESTAMP,localtime()) . ' ' . join(" ",map { ref($_) ? dump( $_ ) : $_ } @_) . $/; } +open(STDOUT, '>', $log_path) && warn "log to $log_path: $!\n"; + # HTML formatters my %escape = ('<'=>'<', '>'=>'>', '&'=>'&', '"'=>'"'); @@ -160,6 +160,13 @@ }, }; +# POE IRC +my $poe_irc = POE::Component::IRC->spawn( %$irc_config ) or + die "can't start ", dump( $irc_config ), ": $!"; + +my $irc = $poe_irc->session_id(); +_log "IRC session_id $irc"; + my $dbh = DBI->connect($DSN,"","", { RaiseError => 1, AutoCommit => 1 }) || die $DBI::errstr; $dbh->do( qq{ set client_encoding = 'UTF-8' } ); @@ -658,6 +665,8 @@ return; } + $_stat->{rss}->{url2link}->{ $args->{url} } = $feed->link; + my ( $total, $updates ) = ( 0, 0 ); for my $entry ($feed->entries) { $total++; @@ -689,6 +698,12 @@ $msg .= prefix( ' | ' , $entry->title ); $msg .= prefix( ' | ' , $link ); # $msg .= prefix( ' id ' , $entry->id ); + if ( my $tags = $entry->category ) { + $tags =~ s!^\s+!!; + $tags =~ s!\s*$! !; + $tags =~ s!\s+!// !g; + $msg .= prefix( ' ' , $tags ); + } if ( $args->{kernel} && $send_rss_msgs ) { $send_rss_msgs--; @@ -699,8 +714,8 @@ } my ( $type, $to ) = ( 'notice', $args->{channel} ); ( $type, $to ) = ( 'privmsg', $args->{nick} ) if $args->{private}; - _log(">> $type $to |", $msg); - $args->{kernel}->post( irc => $type => $to, $msg ); + _log(">> $type $to", $msg); + $args->{kernel}->post( $irc => $type => $to, $msg ); $updates++; } } @@ -750,16 +765,6 @@ # seed rss seen cache so we won't send out all items on startup _log rss_fetch_all; -# -# 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"; - POE::Session->create( inline_states => { _start => sub { $_[KERNEL]->post( $irc => register => 'all' ); @@ -774,7 +779,6 @@ }, irc_255 => sub { # server is done blabbing $_[KERNEL]->post( $irc => join => $CHANNEL); - $_[KERNEL]->post( $irc => privmsg => 'nickserv', "IDENTIFY $NICK" ); }, irc_public => sub { my $kernel = $_[KERNEL]; @@ -953,10 +957,6 @@ } } elsif ($msg =~ m/^rss-update/) { $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"; } elsif ($msg =~ m/^rss-list/) { my $sth = $dbh->prepare(qq{ select url,name,last_update,active,channel,nick,private from feeds }); $sth->execute; @@ -964,7 +964,7 @@ $_[KERNEL]->post( $irc => privmsg => $nick, join(' | ',@row) ); } $res = ''; - } 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*(.*)!) { my ( $command, $sub, $url, $arg ) = ( $1,$2,$3,$4 ); my $channel = $1 if ( $arg =~ s/\s*(#\S+)\s*// ); @@ -975,6 +975,7 @@ # remove => qq{ delete from feeds where url = ? and name = ? }, start => qq{ update feeds set active = true where url = ? }, stop => qq{ update feeds set active = false where url = ? }, + clean => qq{ update feeds set last_update = now() - delay where url = ? }, }; if ( $command eq 'add' && ! $channel ) { @@ -990,11 +991,28 @@ if ($@) { $res = "ERROR: $@"; } else { - $res = "OK, RSS [$command|$sub|$url|$arg]"; + $res = "OK, RSS executed $command " . ( $sub ? "-$sub" : '' ) ."on $channel url $url"; + if ( $command eq 'clean' ) { + my $seen = $_stat->{rss}->{seen} || die "no seen?"; + my $want_link = $_stat->{rss}->{url2link}->{$url} || warn "no url2link($url)"; + foreach my $c ( keys %$seen ) { + my $c_hash = $seen->{$c} || die "no seen->{$c}"; + die "not HASH with rss links but ", dump($c_hash) unless ref($c_hash) eq 'HASH'; + foreach my $link ( keys %$c_hash ) { + next unless $link eq $want_link; + _log "RSS removed seen $c $url $link"; + } + } + } } } else { $res = "ERROR: don't know what to do with: $msg"; } + } elsif ($msg =~ m/^rss-clean/) { + # this makes sense because we didn't catch rss-clean http://... before! + $_stat->{rss} = undef; + $dbh->do( qq{ update feeds set last_update = now() - delay } ); + $res = "OK, cleaned RSS cache"; } if ($res) { @@ -1013,13 +1031,22 @@ irc_376 => sub { _log "<< motd", $_[ARG0], "end"; }, +# irc_433 => sub { +# print "# irc_433: ",$_[ARG1], "\n"; +# warn "## indetify $NICK\n"; +# $_[KERNEL]->post( $irc => privmsg => 'nickserv', "IDENTIFY $NICK" ); +# }, +# irc_451 # please register irc_477 => sub { _log "<< irc_477: ",$_[ARG1]; - $_[KERNEL]->post( $irc => privmsg => 'nickserv', "register $NICK" ); + _log ">> IDENTIFY $NICK"; + $_[KERNEL]->post( $irc => privmsg => 'NickServ', "IDENTIFY $NICK" ); }, irc_505 => sub { _log "<< irc_505: ",$_[ARG1]; - $_[KERNEL]->post( $irc => privmsg => 'nickserv', "register $NICK" ); + _log ">> register $NICK"; + $_[KERNEL]->post( $irc => privmsg => 'NickServ', "register $NICK" ); +# $_[KERNEL]->post( $irc => privmsg => 'nickserv', "IDENTIFY $NICK" ); # $_[KERNEL]->post( $irc => privmsg => 'nickserv', "set hide email on" ); # $_[KERNEL]->post( $irc => privmsg => 'nickserv', "set email dpavlin\@rot13.org" ); }, @@ -1036,20 +1063,21 @@ sleep($sleep_on_error); $_[KERNEL]->post( $irc => connect => {} ); }, -# irc_433 => sub { -# print "# irc_433: ",$_[ARG1], "\n"; -# warn "## indetify $NICK\n"; -# $_[KERNEL]->post( $irc => privmsg => 'nickserv', "IDENTIFY $NICK" ); -# }, -# irc_451 # please register irc_notice => sub { - _log "<< notice",$_[ARG0]; - if ( $_[ARG0] =~ m!/msg\s+NickServ\s+IDENTIFY!i ) { + _log "<< notice from ", $_[ARG0], $_[ARG1], $_[ARG2]; + my $m = $_[ARG2]; + if ( $m =~ m!/msg.*(NickServ).*(IDENTIFY)!i ) { + _log ">> suggested to $1 $2"; + $_[KERNEL]->post( $irc => privmsg => $1, "$2 $NICK" ); + } elsif ( $m =~ m!\Q$NICK\E.*registered!i ) { + _log ">> registreted, so IDENTIFY"; $_[KERNEL]->post( $irc => privmsg => 'nickserv', "IDENTIFY $NICK" ); + } else { + warn "## ignore $m\n"; } }, irc_snotice => sub { - _log "<< snotice",$_[ARG0]; + _log "<< snotice", $_[ARG0]; #dump( $_[ARG0],$_[ARG1], $_[ARG2] ); if ( $_[ARG0] =~ m!/(QUOTE)\s+(PASS\s+\d+)!i ) { warn ">> $1 | $2\n"; $_[KERNEL]->post( $irc => lc($1) => $2); @@ -1069,6 +1097,8 @@ # http server +_log "WEB archive at $url"; + my $httpd = POE::Component::Server::HTTP->new( Port => $http_port, PreHandler => { @@ -1115,7 +1145,7 @@ $style .= ".col-${max_color} { background: $c }\n"; $max_color++; } -warn "defined $max_color colors for users...\n"; +_log "WEB defined $max_color colors for users..."; sub root_handler { my ($request, $response) = @_; @@ -1237,7 +1267,7 @@ $feed->add_entry( $feed_entry ); } else { - _log "unknown rss request $r_url"; + _log "WEB unknown rss request $r_url"; $feed->title( "unknown $r_url" ); foreach my $c ( @commands ) { my $feed_entry = XML::Feed::Entry->new($type);