--- trunk/irc-logger.pl 2006/06/25 17:40:59 37 +++ trunk/bin/irc-logger.pl 2007/03/18 16:45:18 52 @@ -18,6 +18,12 @@ 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 @@ -50,6 +56,8 @@ my $ENCODING = 'ISO-8859-2'; my $TIMESTAMP = '%Y-%m-%d %H:%M:%S'; +my $sleep_on_error = 5; + ## END CONFIG @@ -65,22 +73,25 @@ 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, ); -my $dbh = DBI->connect($DSN,"","", { RaiseError => 1, AutoCommit => 1 }) || die $DBI::errstr; +open(STDOUT, '>', $log_path) || warn "can't redirect log to $log_path: $!"; -eval { - $dbh->do(qq{ select count(*) from log }); -}; +sub _log { + print strftime($TIMESTAMP,localtime()), ' ', join(" ",@_), $/; +} -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 => ' create table log ( id serial, time timestamp default now(), @@ -94,16 +105,87 @@ create index log_time on log(time); create index log_channel on log(channel); create index log_nick on log(nick); + ', + meta => ' +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) +); + ', +}; + +foreach my $table ( keys %$sql_schema ) { + + eval { + $dbh->do(qq{ select count(*) from $table }); + }; -_SQL_SCHEMA_ + 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, time) values (?,?,?,?,?) }); + my $tags; my $tag_regex = '\b([\w-_]+)//'; @@ -126,6 +208,7 @@ } }, context => 5, + full_rows => 1, ); Order is important. Fields are first passed through C (if available) and @@ -133,6 +216,9 @@ C defines number of messages around each search hit for display. +C will return database rows for each result with C, C