--- trunk/irc-logger.pl 2006/03/14 17:17:53 20 +++ trunk/irc-logger.pl 2006/03/18 16:02:32 21 @@ -18,7 +18,7 @@ ## CONFIG -my $NICK = 'irc-logger'; +my $NICK = 'irc-logger-dev'; my $CONNECT = {Server => 'irc.freenode.net', Nick => $NICK, @@ -99,12 +99,15 @@ # 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 { @@ -120,7 +123,7 @@ message => '%s', }; - my $sql = qq{ + my $sql_message = qq{ select time::date as date, time::time as time, @@ -130,14 +133,27 @@ 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 ? " if ($args->{search}); $sql .= " order by log.time desc"; $sql .= " limit " . $args->{limit}; my $sth = $dbh->prepare( $sql ); - if ($args->{search}) { - $sth->execute( '%' . $args->{search} . '%' ); - warn "search for '$args->{search}' returned ", $sth->rows, " results\n"; + if (my $search = $args->{search}) { + $search =~ s/^\s+//; + $search =~ s/\s+$//; + $sth->execute( '%' . $search . '%' ); + warn "search for '$search' returned ", $sth->rows, " results ", $context || '', "\n"; } else { $sth->execute(); } @@ -158,6 +174,30 @@ "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+##; @@ -297,11 +337,14 @@ $res = ''; - } elsif ($msg =~ m/^(search|grep)\s+(.*)$/i) { + } elsif ($msg =~ m/^(search|grep)\s+(.*)\s*$/i) { my $what = $2; - foreach my $res (get_from_log( limit => 20, search => $what )) { + 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 );