--- trunk/bin/irc-logger.pl 2007/03/02 22:00:06 49 +++ trunk/bin/irc-logger.pl 2007/03/18 15:37:05 50 @@ -90,14 +90,8 @@ 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_'); - +my $sql_schema = { + log => ' create table log ( id serial, time timestamp default now(), @@ -111,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-_]+)//'; @@ -405,6 +470,7 @@ message => $a->{msg}); } + if ($import_dircproxy) { open(my $l, $import_dircproxy) || die "can't open $import_dircproxy: $!"; warn "importing $import_dircproxy...\n"; @@ -469,6 +535,7 @@ my $msg = $_[ARG2]; save_message( channel => $channel, me => 0, nick => $nick, msg => $msg); + meta( $nick, $channel, 'last-msg', $msg ); }, irc_ctcp_action => sub { my $kernel = $_[KERNEL]; @@ -477,6 +544,11 @@ my $msg = $_[ARG2]; save_message( channel => $channel, me => 1, nick => $nick, msg => $msg); + + if ( my $twitter = ( $nick, $channel, 'twitter' ) ) { + _log("FIXME: send twitter for $nick on $channel [$twitter]"); + } + }, irc_ping => sub { warn "pong ", $_[ARG0], $/; @@ -486,7 +558,6 @@ my $kernel = $_[KERNEL]; my $nick = (split /!/, $_[ARG0])[0]; my $channel = $_[ARG1]; - warn "invited to $channel by $nick"; @@ -498,6 +569,7 @@ my $kernel = $_[KERNEL]; my $nick = (split /!/, $_[ARG0])[0]; my $msg = $_[ARG2]; + my $channel = $_[ARG1]->[0]; from_to($msg, 'UTF-8', $ENCODING); my $res = "unknown command '$msg', try /msg $NICK help!"; @@ -538,7 +610,9 @@ $res .= join(" | ", @users); } elsif ($msg =~ m/^last.*?\s*(\d*)/i) { - foreach my $res (get_from_log( limit => ($1 || 100) )) { + my $limit = $1 || meta( $nick, $channel, 'last-size' ) || 10; + + foreach my $res (get_from_log( limit => $limit )) { _log "last: $res"; from_to($res, $ENCODING, 'UTF-8'); $_[KERNEL]->post( $IRC_ALIAS => privmsg => $nick, $res ); @@ -595,6 +669,29 @@ } elsif ($msg =~ m/^ping/) { $res = "ping = " . dump( $ping ); + } elsif ($msg =~ m/^(?:twitter)\s+(\S+)\s+(.*?)/) { + if ( defined( $2 ) ) { + meta($nick, $channel, 'twitter', "$1\t$2"); + $res = "saved twitter auth for $1 -- /me on $channel will auto-update twitter status"; + } else { + meta($nick, $channel, 'twitter', '' ); + $res = "removed twitter status update for /me on $channel"; + } + } elsif ($msg =~ m/^conf(?:ig)*\s*(last-size)*\s*(\d*)/) { + if ( ! defined( $1 ) ) { + my $sth = $dbh->prepare(qq{ select name,value,changed from meta where nick = ? and channel = ? }); + $sth->execute( $nick, $channel ); + $res = "config for $nick "; + while ( my ($n,$v) = $sth->fetchrow_array ) { + $res .= "| $n = $v"; + } + } elsif ( defined( $2 ) ) { + meta( $nick, $channel, $1, $2 ); + $res = "saved $1 = $2"; + } else { + my $val = meta( $nick, $channel, $1 ); + $res = "current $1 = " . ( $val ? $val : 'undefined' ); + } } if ($res) {