--- trunk/bin/irc-logger.pl 2007/12/16 18:51:05 71
+++ 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
@@ -32,7 +36,8 @@
## CONFIG
-my $HOSTNAME = `hostname`;
+my $HOSTNAME = `hostname -f`;
+chomp($HOSTNAME);
my $NICK = 'irc-logger';
$NICK .= '-dev' if ($HOSTNAME =~ m/llin/);
@@ -45,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;
@@ -58,9 +60,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
@@ -92,6 +97,7 @@
my $log_path;
GetOptions(
'import-dircproxy:s' => \$import_dircproxy,
+ 'follows:s' => \$follows_path,
'log:s' => \$log_path,
);
@@ -105,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 = ('<'=>'<', '>'=>'>', '&'=>'&', '"'=>'"');
@@ -128,7 +152,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 +506,6 @@
=cut
-my $last_x_tags = 5;
my @last_tags;
sub add_tag {
@@ -500,14 +523,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 +542,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);
}
}
@@ -621,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 {
@@ -652,7 +675,7 @@
},
irc_ping => sub {
- warn "pong ", $_[ARG0], $/;
+ _log( "pong ", $_[ARG0] );
$ping->{ $_[ARG0] }++;
},
irc_invite => sub {
@@ -858,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]);
},
},
);
@@ -913,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' },
);
@@ -958,6 +987,11 @@
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;
if ( $request->method eq 'POST' ) {
@@ -970,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
- my $url = "http://$http_hostname:$http_port";
- $response->content_type("application/$type+xml");
+ $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;
+ $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 );
@@ -1015,18 +1087,21 @@
$response->content_type("text/html; charset=$ENCODING");
my $html =
- 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
@@ -1058,9 +1133,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{