--- trunk/bin/irc-logger.pl 2007/12/16 19:03:35 72 +++ trunk/bin/irc-logger.pl 2008/02/20 20:26:45 79 @@ -32,7 +32,8 @@ ## CONFIG -my $HOSTNAME = `hostname`; +my $HOSTNAME = `hostname -f`; +chomp($HOSTNAME); my $NICK = 'irc-logger'; $NICK .= '-dev' if ($HOSTNAME =~ m/llin/); @@ -58,9 +59,12 @@ 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 $http_hostname = `hostname`; -chomp( $http_hostname ); + +my $url = "http://$HOSTNAME:$http_port"; ## END CONFIG @@ -128,7 +132,7 @@ $m =~ s/($escape_re)/$escape{$1}/gs; $m =~ s#($RE{URI}{HTTP})#e(qq{$1})#egs || $m =~ s#\/(\w+)\/#$1#gs; - $m =~ s#$tag_regex#e(qq{$1})#egs; + $m =~ s#$tag_regex#e(qq{$1})#egs; $m =~ s#\*(\w+)\*#$1#gs; $m =~ s#_(\w+)_#$1#gs; @@ -482,7 +486,6 @@ =cut -my $last_x_tags = 5; my @last_tags; sub add_tag { @@ -500,14 +503,14 @@ next if (! $tag || $tag =~ m/https?:/i); push @{ $tags->{$tag} }, $arg->{id}; #warn "+tag $tag: $arg->{id}\n"; - $cloud->add($tag, "?tag=$tag", scalar @{$tags->{$tag}} + 1); + $cloud->add($tag, "$url?tag=$tag", scalar @{$tags->{$tag}} + 1); push @tags, $tag; } 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 }; } } @@ -519,14 +522,14 @@ =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 ); } foreach my $tag (keys %$tags) { - $cloud->add($tag, "?tag=$tag", scalar @{$tags->{$tag}} + 1); + $cloud->add($tag, "$url?tag=$tag", scalar @{$tags->{$tag}} + 1); } } @@ -958,6 +961,8 @@ my ($request, $response) = @_; $response->code(RC_OK); + return RC_OK if $request->uri =~ m/favicon.ico$/; + my $q; if ( $request->method eq 'POST' ) { @@ -970,37 +975,75 @@ 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( ); - $feed_entry->issued( DateTime::Format::Flexible->build( $m->{time} ) ); - $feed_entry->summary( - '{nick}->( $m->{nick} ) . -# '' . $m->{nick} . ' ' . - $filter->{message}->( $m->{message} ) . - ']]>' + $feed_entry->title( "$nr tags from $CHANNEL" ); + $feed_entry->author( $NICK ); + $feed_entry->link( '/#tags' ); + + $feed_entry->content( + 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; + + $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 ); @@ -1014,18 +1057,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 @@ -1057,9 +1103,9 @@ $cal->weekdays('MON','TUE','WED','THU','FRI'); ($l_yyyy,$l_mm) = ($yyyy,$mm); } - $cal->setcontent($dd, qq{ - $row->{nr}
$row->{len} - }); + $cal->setcontent($dd, qq[ + $row->{nr}
$row->{len} + ]); } $html .= qq{} . $cal->as_HTML() . qq{}; @@ -1074,7 +1120,7 @@ fmt => { date => sub { my $date = shift || return; - qq{


$date
}; + qq{
$date
}; }, time => '%s ', time_channel => '%s %s ', @@ -1093,6 +1139,7 @@ }; $response->content( $html ); + warn "<< ", $request->method, " ", $request->uri, " created ", length($html), " bytes\n"; return RC_OK; }