--- trunk/irc-logger.pl 2006/02/27 11:54:38 4 +++ trunk/irc-logger.pl 2007/02/03 12:18:04 45 @@ -2,15 +2,47 @@ use strict; $|++; +=head1 NAME + +irc-logger.pl + +=head1 SYNOPSIS + +./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 + +=back + +=head1 DESCRIPTION + +log all conversation on irc channel + +=cut + ## CONFIG +my $HOSTNAME = `hostname`; + my $NICK = 'irc-logger'; +$NICK .= '-dev' if ($HOSTNAME =~ m/llin/); my $CONNECT = {Server => 'irc.freenode.net', Nick => $NICK, - Ircname => 'logger: ask dpavlin@rot13.org' + Ircname => "try /msg $NICK help", }; my $CHANNEL = '#razmjenavjestina'; +$CHANNEL = '#irc-logger' if ($HOSTNAME =~ m/llin/); my $IRC_ALIAS = "log"; my %FOLLOWS = @@ -19,50 +51,594 @@ ERROR => "/var/log/apache/error.log", ); +my $DSN = 'DBI:Pg:dbname=' . $NICK; + +my $ENCODING = 'ISO-8859-2'; +my $TIMESTAMP = '%Y-%m-%d %H:%M:%S'; + +my $sleep_on_error = 5; + ## END CONFIG + + +use POE qw(Component::IRC Wheel::FollowTail 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; +use POSIX qw/strftime/; +use HTML::CalendarMonthSimple; +use Getopt::Long; +use DateTime; +use Data::Dump qw/dump/; + +my $import_dircproxy; +my $log_path; +GetOptions( + 'import-dircproxy:s' => \$import_dircproxy, + 'log:s' => \$log_path, +); + +open(STDOUT, '>', $log_path) || warn "can't redirect log to $log_path: $!"; + +sub _log { + print strftime($TIMESTAMP,localtime()), ' ', join(" ",@_), $/; +} + +my $dbh = DBI->connect($DSN,"","", { RaiseError => 1, AutoCommit => 1 }) || die $DBI::errstr; + +eval { + $dbh->do(qq{ select count(*) from log }); +}; + +if ($@) { + warn "creating database table in $DSN\n"; + $dbh->do(<<'_SQL_SCHEMA_'); + +create table log ( + id serial, + time timestamp default now(), + channel text not null, + me boolean default false, + nick text not null, + message text not null, + primary key(id) +); + +create index log_time on log(time); +create index log_channel on log(channel); +create index log_nick on log(nick); + +_SQL_SCHEMA_ +} + +my $sth = $dbh->prepare(qq{ +insert into log + (channel, me, nick, message, time) +values (?,?,?,?,?) +}); + +my $tags; +my $tag_regex = '\b([\w-_]+)//'; + +=head2 get_from_log + + my @messages = get_from_log( + limit => 42, + search => '%what to stuff in ilike%', + fmt => { + time => '{%s} ', + time_channel => '{%s %s} ', + nick => '%s: ', + me_nick => '***%s ', + message => '%s', + }, + filter => { + message => sub { + # modify message content + return shift; + } + }, + context => 5, + full_rows => 1, + ); + +Order is important. Fields are first passed through C (if available) and +then throgh C<< sprintf($fmt->{message}, $message >> if available. + +C defines number of messages around each search hit for display. + +C will return database rows for each result with C, C