--- trunk/bin/irc-logger.pl 2008/03/06 22:57:16 86
+++ trunk/bin/irc-logger.pl 2008/03/08 00:14:41 100
@@ -20,12 +20,6 @@
=item --log=irc-logger.log
-Name of log file
-
-=item --follow=file.log
-
-Follows new messages in file
-
=back
=head1 DESCRIPTION
@@ -50,13 +44,8 @@
$CHANNEL = '#irc-logger' if ($HOSTNAME =~ m/llin/);
my $IRC_ALIAS = "log";
-# default log to follow and announce messages
-my $follows_path = 'follows.log';
-
my $DSN = 'DBI:Pg:dbname=' . $NICK;
-# log output encoding
-my $ENCODING = 'ISO-8859-2';
my $TIMESTAMP = '%Y-%m-%d %H:%M:%S';
my $sleep_on_error = 5;
@@ -66,7 +55,6 @@
# don't pull rss feeds more often than this
my $rss_min_delay = 60;
-$rss_min_delay = 15;
my $http_port = $NICK =~ m/-dev/ ? 8001 : 8000;
@@ -74,10 +62,9 @@
## END CONFIG
-use POE qw(Component::IRC Wheel::FollowTail Component::Server::HTTP);
+use POE qw(Component::IRC Component::Server::HTTP);
use HTTP::Status;
use DBI;
-use Encode qw/from_to is_utf8/;
use Regexp::Common qw /URI/;
use CGI::Simple;
use HTML::TagCloud;
@@ -100,40 +87,19 @@
my $log_path;
GetOptions(
'import-dircproxy:s' => \$import_dircproxy,
- 'follows:s' => \$follows_path,
'log:s' => \$log_path,
);
-$SIG{__DIE__} = sub {
- confess "fatal error";
-};
+#$SIG{__DIE__} = sub {
+# confess "fatal error";
+#};
open(STDOUT, '>', $log_path) || warn "can't redirect log to $log_path: $!";
sub _log {
- my $out = strftime($TIMESTAMP,localtime()) . ' ' . join(" ",@_) . $/;
- from_to( $out, 'UTF-8', $ENCODING );
- print $out;
-}
-
-# 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;
+ print strftime($TIMESTAMP,localtime()) . ' ' . join(" ",@_) . $/;
}
-add_follow_path( $follows_path ) if ( -e $follows_path );
-
# HTML formatters
my %escape = ('<'=>'<', '>'=>'>', '&'=>'&', '"'=>'"');
@@ -151,11 +117,11 @@
# protect HTML from wiki modifications
sub e {
my $t = shift;
- return 'uri_unescape{' . uri_escape($t) . '}';
+ return 'uri_unescape{' . uri_escape($t, '^a-zA-Z0-9') . '}';
}
$m =~ s/($escape_re)/$escape{$1}/gs;
- $m =~ s#($RE{URI}{HTTP})#e(qq{$1})#egs ||
+ $m =~ s#($RE{URI}{HTTP})#e(qq{$1})#egs;
$m =~ s#\/(\w+)\/#$1#gs;
$m =~ s#$tag_regex#e(qq{$1})#egs;
$m =~ s#\*(\w+)\*#$1#gs;
@@ -212,12 +178,15 @@
name text,
delay interval not null default '5 min',
active boolean default true,
+ channel text not null,
+ nick text not null,
+ private boolean default false,
last_update timestamp default 'now()',
polls int default 0,
updates int default 0
);
create unique index feeds_url on feeds(url);
-insert into feeds (url,name) values ('http://wiki.razmjenavjestina.org/feed/workspace/razmjenavjestina?category=Recent%20Changes','wiki');
+insert into feeds (url,name,channel,nick) values ('http://wiki.razmjenavjestina.org/feed/workspace/razmjenavjestina?category=Recent%20Changes','wiki','$CHANNEL','dpavlin');
},
};
@@ -261,9 +230,9 @@
if ( $@ || ! $sth->rows ) {
$sth = $dbh->prepare(qq{ insert into meta (value,nick,channel,name,changed) values (?,?,?,?,now()) });
$sth->execute( $value, $nick, $channel, $name );
- _log "created $nick/$channel/$name = $value";
+ warn "## created $nick/$channel/$name = $value\n";
} else {
- _log "updated $nick/$channel/$name = $value ";
+ warn "## updated $nick/$channel/$name = $value\n";
}
return $value;
@@ -273,7 +242,7 @@
my $sth = $dbh->prepare(qq{ select value,changed from meta where nick = ? and channel = ? and name = ? });
$sth->execute( $nick, $channel, $name );
my ($v,$c) = $sth->fetchrow_array;
- _log "fetched $nick/$channel/$name = $v [$c]";
+ warn "## fetched $nick/$channel/$name = $v [$c]\n";
return ($v,$c) if wantarray;
return $v;
@@ -282,7 +251,7 @@
-my $sth = $dbh->prepare(qq{
+my $sth_insert_log = $dbh->prepare(qq{
insert into log
(channel, me, nick, message, time)
values (?,?,?,?,?)
@@ -370,25 +339,26 @@
my @where;
my @args;
+ my $msg;
if (my $search = $args->{search}) {
$search =~ s/^\s+//;
$search =~ s/\s+$//;
push @where, 'message ilike ? or nick ilike ?';
push @args, ( ( '%' . $search . '%' ) x 2 );
- _log "search for '$search'";
+ $msg = "Search for '$search'";
}
if ($args->{tag} && $tags->{ $args->{tag} }) {
push @where, 'id in (' . join(',', @{ $tags->{ $args->{tag} } }) . ')';
- _log "search for tags $args->{tag}";
+ $msg = "Search for tags $args->{tag}";
}
if (my $date = $args->{date} ) {
$date = check_date( $date );
push @where, 'date(time) = ?';
push @args, $date;
- _log "search for date $date";
+ $msg = "search for date $date";
}
$sql .= " where " . join(" and ", @where) if @where;
@@ -402,6 +372,8 @@
eval { $sth->execute( @args ) };
return if $@;
+ my $nr_results = $sth->rows;
+
my $last_row = {
date => '',
time => '',
@@ -422,10 +394,14 @@
return @rows if ($args->{full_rows});
- my @msgs = (
- "Showing " . ($#rows + 1) . " messages..."
+ $msg .= ' produced ' . (
+ $nr_results == 0 ? 'no results' :
+ $nr_results == 0 ? 'one result' :
+ $nr_results . ' results'
);
+ my @msgs = ( $msg );
+
if ($context) {
my @ids = @rows;
@rows = ();
@@ -534,7 +510,6 @@
return unless ($arg->{id} && $arg->{message});
my $m = $arg->{message};
- from_to('UTF-8', 'iso-8859-2', $m) if (is_utf8($m));
my @tags;
@@ -603,7 +578,7 @@
$a->{me} ? "***" . $a->{nick} : "<" . $a->{nick} . ">",
" " . $a->{message};
- $sth->execute($a->{channel}, $a->{me}, $a->{nick}, $a->{message}, $a->{time});
+ $sth_insert_log->execute($a->{channel}, $a->{me}, $a->{nick}, $a->{message}, $a->{time});
add_tag( id => $dbh->last_insert_id(undef,undef,"log",undef), %$a );
}
@@ -655,36 +630,54 @@
# how many messages to send out when feed is seen for the first time?
my $send_rss_msgs = 1;
+ _log "RSS fetch", $args->{url};
+
my $feed = XML::Feed->parse(URI->new( $args->{url} ));
if ( ! $feed ) {
_log("can't fetch RSS ", $args->{url});
return;
}
- my $updates = 0;
+
+ my ( $total, $updates ) = ( 0, 0 );
for my $entry ($feed->entries) {
+ $total++;
# seen allready?
- return if $_rss->{$feed->link}->{seen}->{$entry->id}++ > 0;
+ next if $_rss->{$args->{channel}}->{$feed->link}->{$entry->id}++ > 0;
sub prefix {
my ($txt,$var) = @_;
+ $var =~ s/\s+/ /gs;
$var =~ s/^\s+//g;
+ $var =~ s/\s+$//g;
return $txt . $var if $var;
}
+ # fix absolute and relative links to feed entries
+ my $link = $entry->link;
+ if ( $link =~ m!^/! ) {
+ my $host = $args->{url};
+ $host =~ s!^(http://[^/]+).*$!$1!; #!vim
+ $link = "$host/$link";
+ } elsif ( $link !~ m!^http! ) {
+ $link = $args->{url} . $link;
+ }
+
my $msg;
- $msg .= prefix( 'From: ' , $feed->title );
+ $msg .= prefix( 'From: ' , $args->{name} || $feed->title );
$msg .= prefix( ' by ' , $entry->author );
- $msg .= prefix( ' -- ' , $entry->link );
+ $msg .= prefix( ' | ' , $entry->title );
+ $msg .= prefix( ' | ' , $link );
# $msg .= prefix( ' id ' , $entry->id );
if ( $args->{kernel} && $send_rss_msgs ) {
- warn "# sending to $CHANNEL\n";
$send_rss_msgs--;
- $args->{kernel}->post( $IRC_ALIAS => notice => $CHANNEL, $msg );
+ $sth_insert_log->execute( $args->{channel}, 1, $NICK, $msg, 'now()' );
+ my ( $type, $to ) = ( 'notice', $args->{channel} );
+ ( $type, $to ) = ( 'privmsg', $args->{nick} ) if $args->{private};
+ _log(">> $type $to |", $msg);
+ $args->{kernel}->post( $IRC_ALIAS => $type => $to, $msg );
$updates++;
- save_message( channel => $CHANNEL, me => 1, nick => $NICK, message => $msg );
- _log('RSS', $msg);
}
}
@@ -693,13 +686,15 @@
$sql .= qq{where id = } . $args->{id};
eval { $dbh->do( $sql ) };
+ _log "RSS got $total items of which $updates new";
+
return $updates;
}
sub rss_fetch_all {
my $kernel = shift;
my $sql = qq{
- select id, url, name
+ select id, url, name, channel, nick, private
from feeds
where active is true
};
@@ -710,20 +705,20 @@
warn "# ",$sth->rows," active RSS feeds\n";
my $count = 0;
while (my $row = $sth->fetchrow_hashref) {
- warn "+++ fetch RSS feed: ",dump( $row );
$row->{kernel} = $kernel if $kernel;
$count += rss_fetch( $row );
}
return "OK, fetched $count posts from " . $sth->rows . " feeds";
}
-my $rss_last_poll = time();
sub rss_check_updates {
my $kernel = shift;
- my $t = time();
- if ( $rss_last_poll - $t > $rss_min_delay ) {
- $rss_last_poll = $t;
+ $_rss->{last_poll} ||= time();
+ my $dt = time() - $_rss->{last_poll};
+ warn "## rss_check_updates $dt > $rss_min_delay\n";
+ if ( $dt > $rss_min_delay ) {
+ $_rss->{last_poll} = time();
_log rss_fetch_all( $kernel );
}
}
@@ -735,8 +730,6 @@
# POE handing part
#
-my $SKIPPING = 0; # if skipping, how many we've done
-my $SEND_QUEUE; # cache
my $ping; # ping stats
POE::Component::IRC->new($IRC_ALIAS);
@@ -748,9 +741,6 @@
},
irc_255 => sub { # server is done blabbing
$_[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]->post( $IRC_ALIAS => privmsg => 'nickserv', "IDENTIFY $NICK" );
},
irc_public => sub {
@@ -761,6 +751,7 @@
save_message( channel => $channel, me => 0, nick => $nick, message => $msg);
meta( $nick, $channel, 'last-msg', $msg );
+ rss_check_updates( $kernel );
},
irc_ctcp_action => sub {
my $kernel = $_[KERNEL];
@@ -931,22 +922,46 @@
$res = rss_fetch_all( $_[KERNEL] );
} elsif ($msg =~ m/^rss-clean/) {
$_rss = undef;
+ $dbh->do( qq{ update feeds set last_update = now() - delay } );
$res = "OK, cleaned RSS cache";
- } elsif ($msg =~ m!^rss-(add|remove|stop|start)\s+(http://\S+)\s*(.*)!) {
+ } elsif ($msg =~ m/^rss-list/) {
+ my $sth = $dbh->prepare(qq{ select url,name,last_update,active,channel,nick,private from feeds });
+ $sth->execute;
+ while (my @row = $sth->fetchrow_array) {
+ $_[KERNEL]->post( $IRC_ALIAS => privmsg => $nick, join(' | ',@row) );
+ }
+ $res = '';
+ } elsif ($msg =~ m!^rss-(add|remove|stop|start)(?:-(private))?\s+(http://\S+)\s*(.*)!) {
+ my ( $command, $sub, $url, $arg ) = ( $1,$2,$3,$4 );
+
+ my $channel = $1 if ( $arg =~ s/\s*(#\S+)\s*// );
+ $channel = $nick if $sub eq 'private';
+
my $sql = {
- add => qq{ insert into feeds (url,name) values (?,?) },
+ add => qq{ insert into feeds (url,name,channel,nick,private) values (?,?,?,?,?) },
# 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 = ? -- ? },
-
+ start => qq{ update feeds set active = true where url = ? },
+ stop => qq{ update feeds set active = false where url = ? },
};
- if (my $q = $sql->{$1} ) {
+
+ if ( $command eq 'add' && ! $channel ) {
+ $res = "ERROR: got '$msg' which doesn't have #channel in it, ignoring!";
+ } elsif (my $q = $sql->{$command} ) {
my $sth = $dbh->prepare( $q );
- warn "## SQL $q ( $2 | $3 )\n";
- eval { $sth->execute( $2, $3 ) };
+ my @data = ( $url );
+ if ( $command eq 'add' ) {
+ push @data, ( $arg, $channel, $nick, $sub eq 'private' ? 1 : 0 );
+ }
+ warn "## $command SQL $q with ",dump( @data ),"\n";
+ eval { $sth->execute( @data ) };
+ if ($@) {
+ $res = "ERROR: $@";
+ } else {
+ $res = "OK, RSS [$command|$sub|$url|$arg]";
+ }
+ } else {
+ $res = "ERROR: don't know what to do with: $msg";
}
-
- $res ||= "OK, RSS $1 : $2 - $3";
}
if ($res) {
@@ -993,67 +1008,6 @@
"";
0; # false for signals
},
- my_add => sub {
- my $trailing = $_[ARG0];
- my $session = $_[SESSION];
- POE::Session->create
- (inline_states =>
- {_start => sub {
- $_[HEAP]->{wheel} =
- POE::Wheel::FollowTail->new
- (
- Filename => $FOLLOWS{$trailing},
- InputEvent => 'got_line',
- );
- warn "+++ following $trailing at $FOLLOWS{$trailing}\n";
- },
- got_line => sub {
- warn "+++ $trailing : $_[ARG0]\n";
- $_[KERNEL]->post($session => my_tailed => time, $trailing, $_[ARG0]);
- },
- },
- );
-
- },
- my_tailed => sub {
- my ($time, $file, $line) = @_[ARG0..ARG2];
- ## $time will be undef on a probe, or a time value if a real line
-
- ## PoCo::IRC has throttling built in, but no external visibility
- ## so this is reaching "under the hood"
- $SEND_QUEUE ||=
- $_[KERNEL]->alias_resolve($IRC_ALIAS)->get_heap->{send_queue};
-
- ## handle "no need to keep skipping" transition
- if ($SKIPPING and @$SEND_QUEUE < 1) {
- $_[KERNEL]->post($IRC_ALIAS => privmsg => $CHANNEL =>
- "[discarded $SKIPPING messages]");
- $SKIPPING = 0;
- }
-
- ## handle potential message display
- if ($time) {
- if ($SKIPPING or @$SEND_QUEUE > 3) { # 3 msgs per 10 seconds
- $SKIPPING++;
- } else {
- my @time = localtime $time;
- $_[KERNEL]->post($IRC_ALIAS => privmsg => $CHANNEL =>
- sprintf "%02d:%02d:%02d: %s: %s",
- ($time[2] + 11) % 12 + 1, $time[1], $time[0],
- $file, $line);
- }
- }
-
- ## handle re-probe/flush if skipping
- if ($SKIPPING) {
- $_[KERNEL]->delay($_[STATE] => 0.5); # $time will be undef
- }
-
- },
- my_heartbeat => sub {
- $_[KERNEL]->yield(my_tailed => time, "heartbeat", "beep");
- $_[KERNEL]->delay($_[STATE] => 10);
- }
},
);
@@ -1211,6 +1165,13 @@
$feed->add_entry( $feed_entry );
}
+ my $feed_entry = XML::Feed::Entry->new($type);
+ $feed_entry->title( "Internal stats" );
+ $feed_entry->content(
+ '' . dump( $_rss ) . ']]>'
+ );
+ $feed->add_entry( $feed_entry );
+
} else {
_log "unknown rss request ",$request->url;
return RC_DENY;
@@ -1275,7 +1236,7 @@
}
$cal->setcontent($dd, qq[
$row->{nr}
$row->{len}
- ]);
+ ]) if $cal;
}
$html .= qq{