--- trunk/irc-logger.pl 2006/03/14 17:17:53 20
+++ trunk/irc-logger.pl 2006/05/20 10:12:19 25
@@ -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
};
- $sql .= " where message ilike ? " if ($args->{search});
+
+ 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 .= " 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 . '%' ) x 2 );
+ 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+##;
@@ -176,12 +216,16 @@
my $append = 1;
- if ($last_row->{nick} ne $row->{nick}) {
+ 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';
- my $nick = $row->{nick};
$nick = $args->{filter}->{nick}->($nick) if (ref($args->{filter}->{nick}) eq 'CODE');
$msg .= sprintf( $fmt, $nick );
@@ -297,11 +341,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 );
@@ -481,7 +528,7 @@
} .
join("
",
get_from_log(
- limit => $q->param('limit') || 100,
+ limit => $q->param('last') || 100,
search => $q->param('search') || $q->param('grep') || undef,
fmt => {
time => '%s ',