--- trunk/irc-logger.pl 2006/03/14 17:17:53 20 +++ trunk/irc-logger.pl 2006/05/22 16:19:00 27 @@ -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 { @@ -113,6 +116,7 @@ $args->{limit} ||= 10; $args->{fmt} ||= { + date => '[%s] ', time => '{%s} ', time_channel => '{%s %s} ', nick => '%s: ', @@ -120,7 +124,7 @@ message => '%s', }; - my $sql = qq{ + my $sql_message = qq{ select time::date as date, time::time as time, @@ -130,14 +134,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,16 +175,39 @@ "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 $t; - $t = $row->{date} . ' ' if ($last_row->{date} ne $row->{date}); - $t .= $row->{time}; - 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 { @@ -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 ); @@ -440,6 +487,7 @@ my $style = <<'_END_OF_STYLE_'; p { margin: 0; padding: 0.1em; } .time, .channel { color: #808080; font-size: 60%; } +.date { float: right; clear: 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; } @@ -481,9 +529,10 @@ } . join("

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


%s
', time => '%s ', time_channel => '%s %s ', nick => '%s: ',