--- trunk/irc-logger.pl 2006/03/02 00:19:12 10 +++ trunk/irc-logger.pl 2006/06/16 20:55:19 29 @@ -33,26 +33,37 @@ ERROR => "/var/log/apache/error.log", ); -my $DSN = 'DBI:Pg:dbname=irc-logger'; +my $DSN = 'DBI:Pg:dbname=' . $NICK; + +my $ENCODING = 'ISO-8859-2'; ## END CONFIG -use POE qw(Component::IRC Wheel::FollowTail); +use POE qw(Component::IRC Wheel::FollowTail Component::Server::HTTP); +use HTTP::Status; use DBI; use Encode qw/from_to/; - +use Regexp::Common qw /URI/; +use CGI::Simple; +use HTML::TagCloud; my $dbh = DBI->connect($DSN,"","", { RaiseError => 1, AutoCommit => 1 }) || die $DBI::errstr; -=for SQL schema +eval { + $dbh->do(qq{ select count(*) from log }); +}; + +if ($@) { + warn "creating database table in $DSN\n"; + $dbh->do(<<'_SQL_SCHEMA_'); -$dbh->do(qq{ 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) @@ -62,16 +73,194 @@ create index log_channel on log(channel); create index log_nick on log(nick); -}); - -=cut +_SQL_SCHEMA_ +} my $sth = $dbh->prepare(qq{ insert into log - (channel, nick, message) -values (?,?,?) + (channel, me, nick, message) +values (?,?,?,?) }); +my $tags; + +=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, + ); + +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. + +=cut + +sub get_from_log { + my $args = {@_}; + + $args->{limit} ||= 10; + + $args->{fmt} ||= { + date => '[%s] ', + time => '{%s} ', + time_channel => '{%s %s} ', + nick => '%s: ', + me_nick => '***%s ', + message => '%s', + }; + + my $sql_message = qq{ + select + time::date as date, + time::time as time, + channel, + me, + nick, + message + from log + }; + + my $sql_context = qq{ + select + id + from log + }; + + my $context = $1 if ($args->{search} && $args->{search} =~ s/\s*\+(\d+)\s*/ /); + + my $sql = $context ? $sql_context : $sql_message; + + $sql .= " where message ilike ? or nick ilike ? " if ($args->{search}); + $sql .= " where id in (" . join(",", @{ $tags->{ $args->{tag} } }) . ") " if ($args->{tag} && $tags->{ $args->{tag} }); + $sql .= " order by log.time desc"; + $sql .= " limit " . $args->{limit}; + + my $sth = $dbh->prepare( $sql ); + if (my $search = $args->{search}) { + $search =~ s/^\s+//; + $search =~ s/\s+$//; + $sth->execute( ( '%' . $search . '%' ) x 2 ); + warn "search for '$search' returned ", $sth->rows, " results ", $context || '', "\n"; + } elsif (my $tag = $args->{tag}) { + $sth->execute(); + warn "tag '$tag' returned ", $sth->rows, " results ", $context || '', "\n"; + } else { + $sth->execute(); + } + my $last_row = { + date => '', + time => '', + channel => '', + nick => '', + }; + + my @rows; + + while (my $row = $sth->fetchrow_hashref) { + unshift @rows, $row; + } + + my @msgs = ( + "Showing " . ($#rows + 1) . " messages..." + ); + + if ($context) { + my @ids = @rows; + @rows = (); + + my $last_to = 0; + + my $sth = $dbh->prepare( $sql_message . qq{ where id >= ? and id < ? } ); + foreach my $row_id (sort { $a->{id} <=> $b->{id} } @ids) { + my $id = $row_id->{id} || die "can't find id in row"; + + my ($from, $to) = ($id - $context, $id + $context); + $from = $last_to if ($from < $last_to); + $last_to = $to; + $sth->execute( $from, $to ); + + #warn "## id: $id from: $from to: $to returned: ", $sth->rows, "\n"; + + while (my $row = $sth->fetchrow_hashref) { + push @rows, $row; + } + + } + } + + foreach my $row (@rows) { + + $row->{time} =~ s#\.\d+##; + + my $msg = ''; + + $msg = sprintf($args->{fmt}->{date}, $row->{date}) . ' ' if ($last_row->{date} ne $row->{date}); + my $t = $row->{time}; + + if ($last_row->{channel} ne $row->{channel}) { + $msg .= sprintf($args->{fmt}->{time_channel}, $t, $row->{channel}); + } else { + $msg .= sprintf($args->{fmt}->{time}, $t); + } + + my $append = 1; + + my $nick = $row->{nick}; + if ($nick =~ s/^_*(.*?)_*$/$1/) { + $row->{nick} = $nick; + } + + if ($last_row->{nick} ne $nick) { + # obfu way to find format for me_nick if needed or fallback to default + my $fmt = $row->{me} ? ( $args->{fmt}->{me_nick} || $args->{fmt}->{nick} ) : $args->{fmt}->{nick}; + $fmt ||= '%s'; + + $nick = $args->{filter}->{nick}->($nick) if (ref($args->{filter}->{nick}) eq 'CODE'); + + $msg .= sprintf( $fmt, $nick ); + $append = 0; + } + + $args->{fmt}->{message} ||= '%s'; + if (ref($args->{filter}->{message}) eq 'CODE') { + $msg .= sprintf($args->{fmt}->{message}, + $args->{filter}->{message}->( + $row->{message} + ) + ); + } else { + $msg .= sprintf($args->{fmt}->{message}, $row->{message}); + } + + if ($append && @msgs) { + $msgs[$#msgs] .= " " . $msg; + } else { + push @msgs, $msg; + } + + $last_row = $row; + } + + return @msgs; +} + my $SKIPPING = 0; # if skipping, how many we've done my $SEND_QUEUE; # cache @@ -89,6 +278,7 @@ $_[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 { my $kernel = $_[KERNEL]; @@ -96,24 +286,36 @@ my $channel = $_[ARG1]->[0]; my $msg = $_[ARG2]; - from_to($msg, 'UTF-8', 'ISO-8859-2'); + from_to($msg, 'UTF-8', $ENCODING); print "$channel: <$nick> $msg\n"; - $sth->execute($channel, $nick, $msg); + $sth->execute($channel, 0, $nick, $msg); + }, + irc_ctcp_action => sub { + my $kernel = $_[KERNEL]; + my $nick = (split /!/, $_[ARG0])[0]; + my $channel = $_[ARG1]->[0]; + my $msg = $_[ARG2]; + + from_to($msg, 'UTF-8', $ENCODING); + + print "$channel ***$nick $msg\n"; + $sth->execute($channel, 1, $nick, $msg); }, irc_msg => sub { my $kernel = $_[KERNEL]; my $nick = (split /!/, $_[ARG0])[0]; my $msg = $_[ARG2]; - from_to($msg, 'UTF-8', 'ISO-8859-2'); + from_to($msg, 'UTF-8', $ENCODING); my $res = "unknown command '$msg', try /msg $NICK help!"; + my @out; print "<< $msg\n"; if ($msg =~ m/^help/i) { - $res = "usage: /msg $NICK stat - shows user statistics | /msg $NICK last - show backtrace of conversations"; + $res = "usage: /msg $NICK comand | commands: stat - user/message stat | last - show backtrace | grep foobar - find foobar"; } elsif ($msg =~ m/^msg\s+(\S+)\s+(.*)$/i) { @@ -137,69 +339,34 @@ $res .= join(" | ", @users); } elsif ($msg =~ m/^last.*?\s*(\d*)/i) { - my $nr = $1 || 10; - - my $sth = $dbh->prepare(qq{ - select - time::date as date, - time::time as time, - channel, - nick, - message - from log order by log.time desc limit $nr - }); - $sth->execute(); - $res = "Last $nr messages: "; - my $last_row = { - date => '', - time => '', - channel => '', - nick => '', - }; - - my @rows; - - while (my $row = $sth->fetchrow_hashref) { - unshift @rows, $row; + foreach my $res (get_from_log( limit => $1 )) { + print "last: $res\n"; + from_to($res, $ENCODING, 'UTF-8'); + $_[KERNEL]->post( $IRC_ALIAS => privmsg => $nick, $res ); } - my @msgs; - - foreach my $row (@rows) { - - $row->{time} =~ s#\.\d+##; - - my $t; - $t = $row->{date} . ' ' if ($last_row->{date} ne $row->{date}); - $t .= $row->{time}; - - my $msg = ''; - - $msg .= "($t"; - $msg .= ' ' . $row->{channel} if ($last_row->{channel} ne $row->{channel}); - $msg .= ") "; - - $msg .= $row->{nick} . ': ' if ($last_row->{nick} ne $row->{nick}); - - $msg .= $row->{message}; + $res = ''; - push @msgs, $msg; + } elsif ($msg =~ m/^(search|grep)\s+(.*)\s*$/i) { - $last_row = $row; - } + my $what = $2; - foreach my $res (@msgs) { - print "last: $res\n"; - from_to($res, 'ISO-8859-2', 'UTF-8'); + foreach my $res (get_from_log( + limit => 20, + search => $what, + )) { + print "search [$what]: $res\n"; + from_to($res, $ENCODING, 'UTF-8'); $_[KERNEL]->post( $IRC_ALIAS => privmsg => $nick, $res ); } $res = ''; + } if ($res) { print ">> [$nick] $res\n"; - from_to($res, 'ISO-8859-2', 'UTF-8'); + from_to($res, $ENCODING, 'UTF-8'); $_[KERNEL]->post( $IRC_ALIAS => privmsg => $nick, $res ); } @@ -218,11 +385,11 @@ warn "## indetify $NICK\n"; $_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "IDENTIFY $NICK" ); }, - irc_433 => sub { - print "# irc_433: ",$_[ARG1], "\n"; - warn "## indetify $NICK\n"; - $_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "IDENTIFY $NICK" ); - }, +# irc_433 => sub { +# print "# irc_433: ",$_[ARG1], "\n"; +# warn "## indetify $NICK\n"; +# $_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "IDENTIFY $NICK" ); +# }, irc_372 => sub { print "MOTD: ", $_[ARG1], "\n"; }, @@ -312,4 +479,142 @@ }, ); +# tags support + +my $cloud = HTML::TagCloud->new; + +=head2 add_tag + + add_tag( id => 42, message => 'irc message' ); + +=cut + +sub add_tag { + my $arg = {@_}; + + return unless ($arg->{id} && $arg->{message}); + + while ($arg->{message} =~ s#\b(\S+)//##s) { + my $tag = $1; + next if (! $tag || $tag =~ m/https?:/i); + push @{ $tags->{$tag} }, $arg->{id}; + } +} + +=head2 seed_tags + +Read all tags from database and create in-memory cache for tags + +=cut + +sub seed_tags { + my $sth = $dbh->prepare(qq{ select id,message from log where message like '%//%' }); + $sth->execute; + while (my $row = $sth->fetchrow_hashref) { + add_tag( %$row ); + } + + foreach my $tag (keys %$tags) { + $cloud->add($tag, "?tag=$tag", scalar @{$tags->{$tag}} + 1); + } +} + +seed_tags; + +# http server + +my $httpd = POE::Component::Server::HTTP->new( + Port => $NICK =~ m/-dev/ ? 8001 : 8000, + ContentHandler => { '/' => \&root_handler }, + Headers => { Server => 'irc-logger' }, +); + +my %escape = ('<'=>'<', '>'=>'>', '&'=>'&', '"'=>'"'); +my $escape_re = join '|' => keys %escape; + +my $style = <<'_END_OF_STYLE_'; +p { margin: 0; padding: 0.1em; } +.time, .channel { color: #808080; font-size: 60%; } +.date { float: right; background: #e0e0e0; color: #404040; font-size: 120%; padding: 0.25em; border: 1px dashed #808080; } +.nick { color: #000000; font-size: 80%; padding: 2px; font-family: courier, courier new, monospace ; } +.message { color: #000000; font-size: 100%; } +.search { float: right; } +.col-0 { background: #ffff66 } +.col-1 { background: #a0ffff } +.col-2 { background: #99ff99 } +.col-3 { background: #ff9999 } +.col-4 { background: #ff66ff } +_END_OF_STYLE_ + +my $max_color = 4; + +my %nick_enumerator; + +sub root_handler { + my ($request, $response) = @_; + $response->code(RC_OK); + $response->content_type("text/html; charset=$ENCODING"); + + my $q; + + if ( $request->method eq 'POST' ) { + $q = new CGI::Simple( $request->content ); + } elsif ( $request->uri =~ /\?(.+)$/ ) { + $q = new CGI::Simple( $1 ); + } else { + $q = new CGI::Simple; + } + + my $search = $q->param('search') || $q->param('grep') || ''; + + $response->content( + qq{$NICK} . + qq{ + + } . + qq{
} . $cloud->html(500) . qq{
} . + qq{

} . + join("

", + get_from_log( + limit => $q->param('last') || 100, + search => $search || undef, + tag => $q->param('tag') || undef, + fmt => { + date => '


%s
', + 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 . ''; + }, + }, + ) + ) . + qq{

} + ); + return RC_OK; +} + POE::Kernel->run;