--- trunk/bin/irc-logger.pl 2007/12/16 20:06:47 73 +++ trunk/bin/irc-logger.pl 2008/03/06 18:03:05 84 @@ -22,6 +22,10 @@ Name of log file +=item --follow=file.log + +Follows new messages in file + =back =head1 DESCRIPTION @@ -46,11 +50,8 @@ $CHANNEL = '#irc-logger' if ($HOSTNAME =~ m/llin/); my $IRC_ALIAS = "log"; -my %FOLLOWS = - ( - ACCESS => "/var/log/apache/access.log", - ERROR => "/var/log/apache/error.log", - ); +# default log to follow and announce messages +my $follows_path = 'follows.log'; my $DSN = 'DBI:Pg:dbname=' . $NICK; @@ -59,6 +60,9 @@ my $sleep_on_error = 5; +# number of last tags to keep in circular buffer +my $last_x_tags = 50; + my $http_port = $NICK =~ m/-dev/ ? 8001 : 8000; my $url = "http://$HOSTNAME:$http_port"; @@ -93,6 +97,7 @@ my $log_path; GetOptions( 'import-dircproxy:s' => \$import_dircproxy, + 'follows:s' => \$follows_path, 'log:s' => \$log_path, ); @@ -106,6 +111,24 @@ 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; +} + +add_follow_path( $follows_path ) if ( -e $follows_path ); + # HTML formatters my %escape = ('<'=>'<', '>'=>'>', '&'=>'&', '"'=>'"'); @@ -483,7 +506,6 @@ =cut -my $last_x_tags = 5; my @last_tags; sub add_tag { @@ -507,8 +529,8 @@ } if ( @tags ) { - shift @last_tags if $#last_tags == $last_x_tags; - push @last_tags, { tags => [ @tags ], %$arg }; + pop @last_tags if $#last_tags == $last_x_tags; + unshift @last_tags, { tags => [ @tags ], %$arg }; } } @@ -520,7 +542,7 @@ =cut sub seed_tags { - 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 }); $sth->execute; while (my $row = $sth->fetchrow_hashref) { add_tag( %$row ); @@ -622,7 +644,7 @@ $_[KERNEL]->post($IRC_ALIAS => join => $CHANNEL); $_[KERNEL]->post($IRC_ALIAS => join => '#logger'); $_[KERNEL]->yield("heartbeat"); # start heartbeat -# $_[KERNEL]->yield("my_add", $_) for keys %FOLLOWS; + $_[KERNEL]->yield("my_add", $_) for keys %FOLLOWS; $_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "IDENTIFY $NICK" ); }, irc_public => sub { @@ -653,7 +675,7 @@ }, irc_ping => sub { - warn "pong ", $_[ARG0], $/; + _log( "pong ", $_[ARG0] ); $ping->{ $_[ARG0] }++; }, irc_invite => sub { @@ -859,10 +881,11 @@ Filename => $FOLLOWS{$trailing}, InputEvent => 'got_line', ); + warn "+++ following $trailing at $FOLLOWS{$trailing}\n"; }, got_line => sub { - $_[KERNEL]->post($session => my_tailed => - time, $trailing, $_[ARG0]); + warn "+++ $trailing : $_[ARG0]\n"; + $_[KERNEL]->post($session => my_tailed => time, $trailing, $_[ARG0]); }, }, ); @@ -914,6 +937,11 @@ my $httpd = POE::Component::Server::HTTP->new( Port => $http_port, + PreHandler => { + '/' => sub { + $_[0]->header(Connection => 'close') + } + }, ContentHandler => { '/' => \&root_handler }, Headers => { Server => 'irc-logger' }, ); @@ -959,6 +987,9 @@ my ($request, $response) = @_; $response->code(RC_OK); + # this doesn't seem to work, so moved to PreHandler + #$response->header(Connection => 'close'); + return RC_OK if $request->uri =~ m/favicon.ico$/; my $q; @@ -973,38 +1004,76 @@ my $search = $q->param('search') || $q->param('grep') || ''; - if ($request->url =~ m#/rss#i) { + if ($request->url =~ m#/rss(?:/(tags|last-tag)\w*(?:=(\d+))?)?#i) { + my $show = lc($1); + my $nr = $2; + my $type = 'RSS'; # Atom $response->content_type( 'application/' . lc($type) . '+xml' ); my $html = ''; - warn "create $type feed from ",dump( @last_tags ); + #warn "create $type feed from ",dump( @last_tags ); my $feed = XML::Feed->new( $type ); - $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}} ); + if ( $show eq 'tags' ) { + $nr ||= 50; + $feed->title( "tags from $CHANNEL" ); + $feed->link( "$url/tags" ); + $feed->description( "tag cloud created from messages on channel $CHANNEL which have tags// in them" ); my $feed_entry = XML::Feed::Entry->new($type); - $feed_entry->title( $m->{nick} . '@' . $m->{time} ); - $feed_entry->author( $m->{nick} ); - $feed_entry->link( '/#' . $m->{id} ); - $feed_entry->issued( DateTime::Format::Flexible->build( $m->{time} ) ); - #$feed_entry->summary( + $feed_entry->title( "$nr tags from $CHANNEL" ); + $feed_entry->author( $NICK ); + $feed_entry->link( '/#tags' ); + $feed_entry->content( - '{nick}->( $m->{nick} ) . -# '' . $m->{nick} . ' ' . - $filter->{message}->( $m->{message} ) . - "
\n]]>" + qq{} + . $cloud->css + . qq{} + . $cloud->html( $nr ) + . qq{]]>} ); - $feed_entry->category( join(', ', @{$m->{tags}}) ); $feed->add_entry( $feed_entry ); + + } elsif ( $show eq 'last-tag' ) { + + $nr ||= $last_x_tags; + $nr = $last_x_tags if $nr > $last_x_tags; + + $feed->title( "last $nr tagged messages from $CHANNEL" ); + $feed->link( $url ); + $feed->description( "collects messages which have tags// in them" ); + + foreach my $m ( @last_tags ) { +# warn dump( $m ); + #my $tags = join(' ', @{$m->{tags}} ); + my $feed_entry = XML::Feed::Entry->new($type); + $feed_entry->title( $m->{nick} . '@' . $m->{time} ); + $feed_entry->author( $m->{nick} ); + $feed_entry->link( '/#' . $m->{id} ); + $feed_entry->issued( DateTime::Format::Flexible->build( $m->{time} ) ); + + my $message = $filter->{message}->( $m->{message} ); + $message .= "
\n" unless $message =~ m!<(/p|br/?)>!; +# warn "## message = $message\n"; + from_to( $message, $ENCODING, 'UTF-8' ); + + #$feed_entry->summary( + $feed_entry->content( + "" + ); + $feed_entry->category( join(', ', @{$m->{tags}}) ); + $feed->add_entry( $feed_entry ); + + $nr--; + last if $nr <= 0; + + } + + } else { + warn "!! unknown rss request for $show\n"; + return RC_DENY; } $response->content( $feed->as_xml ); @@ -1018,18 +1087,21 @@ $response->content_type("text/html; charset=$ENCODING"); my $html = - qq{$NICK} . - qq{ + qq{$NICK} + . qq{ - } . - $cloud->html(500) . - qq{

}; - if ($request->url =~ m#/history#) { + } + . $cloud->html(500) + . qq{

}; + + if ($request->url =~ m#/tags?#) { + # nop + } elsif ($request->url =~ m#/history#) { my $sth = $dbh->prepare(qq{ select date(time) as date,count(*) as nr,sum(length(message)) as len from log @@ -1061,9 +1133,9 @@ $cal->weekdays('MON','TUE','WED','THU','FRI'); ($l_yyyy,$l_mm) = ($yyyy,$mm); } - $cal->setcontent($dd, qq{ + $cal->setcontent($dd, qq[ $row->{nr}
$row->{len} - }); + ]); } $html .= qq{} . $cal->as_HTML() . qq{}; @@ -1097,7 +1169,7 @@ }; $response->content( $html ); - warn "<< ", $request->method, $request->uri, " created ", length($html), " bytes\n"; + warn "<< ", $request->method, " ", $request->uri, " created ", length($html), " bytes\n"; return RC_OK; }