--- trunk/irc-logger.pl 2006/03/26 17:49:54 24
+++ trunk/bin/irc-logger.pl 2008/03/06 22:16:27 85
@@ -10,6 +10,24 @@
./irc-logger.pl
+=head2 Options
+
+=over 4
+
+=item --import-dircproxy=filename
+
+Import log from C to C database
+
+=item --log=irc-logger.log
+
+Name of log file
+
+=item --follow=file.log
+
+Follows new messages in file
+
+=back
+
=head1 DESCRIPTION
log all conversation on irc channel
@@ -18,47 +36,147 @@
## CONFIG
+my $HOSTNAME = `hostname -f`;
+chomp($HOSTNAME);
+
my $NICK = 'irc-logger';
+$NICK .= '-dev' if ($HOSTNAME =~ m/llin/);
my $CONNECT =
{Server => 'irc.freenode.net',
Nick => $NICK,
Ircname => "try /msg $NICK help",
};
my $CHANNEL = '#razmjenavjestina';
+$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;
my $ENCODING = 'ISO-8859-2';
+my $TIMESTAMP = '%Y-%m-%d %H:%M:%S';
-## END CONFIG
+my $sleep_on_error = 5;
+# number of last tags to keep in circular buffer
+my $last_x_tags = 50;
+# 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;
+
+my $url = "http://$HOSTNAME:$http_port";
+
+## END CONFIG
use POE qw(Component::IRC Wheel::FollowTail Component::Server::HTTP);
use HTTP::Status;
use DBI;
-use Encode qw/from_to/;
+use Encode qw/from_to is_utf8/;
use Regexp::Common qw /URI/;
use CGI::Simple;
+use HTML::TagCloud;
+use POSIX qw/strftime/;
+use HTML::CalendarMonthSimple;
+use Getopt::Long;
+use DateTime;
+use URI::Escape;
+use Data::Dump qw/dump/;
+use DateTime::Format::ISO8601;
+use Carp qw/confess/;
+use XML::Feed;
+use DateTime::Format::Flexible;
+
+my $use_twitter = 1;
+eval { require Net::Twitter; };
+$use_twitter = 0 if ($@);
+
+my $import_dircproxy;
+my $log_path;
+GetOptions(
+ 'import-dircproxy:s' => \$import_dircproxy,
+ 'follows:s' => \$follows_path,
+ 'log:s' => \$log_path,
+);
+$SIG{__DIE__} = sub {
+ confess "fatal error";
+};
-my $dbh = DBI->connect($DSN,"","", { RaiseError => 1, AutoCommit => 1 }) || die $DBI::errstr;
+open(STDOUT, '>', $log_path) || warn "can't redirect log to $log_path: $!";
+
+sub _log {
+ 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 = ('<'=>'<', '>'=>'>', '&'=>'&', '"'=>'"');
+my $escape_re = join '|' => keys %escape;
-eval {
- $dbh->do(qq{ select count(*) from log });
+my $tag_regex = '\b([\w-_]+)//';
+
+my %nick_enumerator;
+my $max_color = 0;
+
+my $filter = {
+ message => sub {
+ my $m = shift || return;
+
+ # protect HTML from wiki modifications
+ sub e {
+ my $t = shift;
+ return 'uri_unescape{' . uri_escape($t) . '}';
+ }
+
+ $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#\*(\w+)\*#$1#gs;
+ $m =~ s#_(\w+)_#$1#gs;
+
+ $m =~ s#uri_unescape{([^}]+)}#uri_unescape($1)#egs;
+ return $m;
+ },
+ nick => sub {
+ my $n = shift || return;
+ if (! $nick_enumerator{$n}) {
+ my $max = scalar keys %nick_enumerator;
+ $nick_enumerator{$n} = $max + 1;
+ }
+ return '' . $n . '';
+ },
};
-if ($@) {
- warn "creating database table in $DSN\n";
- $dbh->do(<<'_SQL_SCHEMA_');
+my $dbh = DBI->connect($DSN,"","", { RaiseError => 1, AutoCommit => 1 }) || die $DBI::errstr;
+my $sql_schema = {
+ log => qq{
create table log (
id serial,
time timestamp default now(),
@@ -72,16 +190,103 @@
create index log_time on log(time);
create index log_channel on log(channel);
create index log_nick on log(nick);
+ },
+ meta => q{
+create table meta (
+ nick text not null,
+ channel text not null,
+ name text not null,
+ value text,
+ changed timestamp default 'now()',
+ primary key(nick,channel,name)
+);
+ },
+ feeds => qq{
+create table feeds (
+ id serial,
+ url text not null,
+ name text,
+ delay interval not null default '30 sec', --'5 min',
+ active boolean default true,
+ 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');
+ },
+};
+
+foreach my $table ( keys %$sql_schema ) {
-_SQL_SCHEMA_
+ eval {
+ $dbh->do(qq{ select count(*) from $table });
+ };
+
+ if ($@) {
+ warn "creating database table $table in $DSN\n";
+ $dbh->do( $sql_schema->{ $table } );
+ }
}
+
+=head2 meta
+
+Set or get some meta data into database
+
+ meta('nick','channel','var_name', $var_value );
+
+ $var_value = meta('nick','channel','var_name');
+ ( $var_value, $changed ) = meta('nick','channel','var_name');
+
+=cut
+
+sub meta {
+ my ($nick,$channel,$name,$value) = @_;
+
+ # normalize channel name
+ $channel =~ s/^#//;
+
+ if (defined($value)) {
+
+ my $sth = $dbh->prepare(qq{ update meta set value = ?, changed = now() where nick = ? and channel = ? and name = ? });
+
+ eval { $sth->execute( $value, $nick, $channel, $name ) };
+
+ # error or no result
+ 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";
+ } else {
+ _log "updated $nick/$channel/$name = $value ";
+ }
+
+ return $value;
+
+ } else {
+
+ 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]";
+ return ($v,$c) if wantarray;
+ return $v;
+
+ }
+}
+
+
+
my $sth = $dbh->prepare(qq{
insert into log
- (channel, me, nick, message)
-values (?,?,?,?)
+ (channel, me, nick, message, time)
+values (?,?,?,?,?)
});
+
+my $tags;
+
=head2 get_from_log
my @messages = get_from_log(
@@ -101,6 +306,7 @@
}
},
context => 5,
+ full_rows => 1,
);
Order is important. Fields are first passed through C (if available) and
@@ -108,20 +314,24 @@
C defines number of messages around each search hit for display.
+C will return database rows for each result with C, C
",
get_from_log(
- limit => $q->param('last') || 100,
- search => $q->param('search') || $q->param('grep') || undef,
+ limit => ( $q->param('last') || $q->param('date') ) ? undef : 100,
+ search => $search || undef,
+ tag => $q->param('tag') || undef,
+ date => $q->param('date') || undef,
fmt => {
+ date => sub {
+ my $date = shift || return;
+ qq{
};
+ },
time => '%s ',
time_channel => '%s %s ',
nick => '%s: ',
me_nick => '***%s ',
message => '%s',
},
- filter => {
- message => sub {
- my $m = shift || return;
- $m =~ s/($escape_re)/$escape{$1}/gs;
- $m =~ s#($RE{URI}{HTTP})#$1#gs;
- return $m;
- },
- nick => sub {
- my $n = shift || return;
- if (! $nick_enumerator{$n}) {
- my $max = scalar keys %nick_enumerator;
- $nick_enumerator{$n} = $max + 1;
- }
- return '' . $n . '';
- },
- },
+ filter => $filter,
)
- ) .
- qq{}
- );
+ );
+ }
+
+ $html .= qq{
+
+ See history of all messages.
+ };
+
+ $response->content( $html );
+ warn "<< ", $request->method, " ", $request->uri, " created ", length($html), " bytes\n";
return RC_OK;
}