18 |
|
|
19 |
## CONFIG |
## CONFIG |
20 |
|
|
21 |
my $NICK = 'irc-logger'; |
my $NICK = 'irc-logger-dev'; |
22 |
my $CONNECT = |
my $CONNECT = |
23 |
{Server => 'irc.freenode.net', |
{Server => 'irc.freenode.net', |
24 |
Nick => $NICK, |
Nick => $NICK, |
99 |
# modify message content |
# modify message content |
100 |
return shift; |
return shift; |
101 |
} |
} |
102 |
} |
}, |
103 |
|
context => 5, |
104 |
); |
); |
105 |
|
|
106 |
Order is important. Fields are first passed through C<filter> (if available) and |
Order is important. Fields are first passed through C<filter> (if available) and |
107 |
then throgh C<< sprintf($fmt->{message}, $message >> if available. |
then throgh C<< sprintf($fmt->{message}, $message >> if available. |
108 |
|
|
109 |
|
C<context> defines number of messages around each search hit for display. |
110 |
|
|
111 |
=cut |
=cut |
112 |
|
|
113 |
sub get_from_log { |
sub get_from_log { |
123 |
message => '%s', |
message => '%s', |
124 |
}; |
}; |
125 |
|
|
126 |
my $sql = qq{ |
my $sql_message = qq{ |
127 |
select |
select |
128 |
time::date as date, |
time::date as date, |
129 |
time::time as time, |
time::time as time, |
133 |
message |
message |
134 |
from log |
from log |
135 |
}; |
}; |
136 |
|
|
137 |
|
my $sql_context = qq{ |
138 |
|
select |
139 |
|
id |
140 |
|
from log |
141 |
|
}; |
142 |
|
|
143 |
|
my $context = $1 if ($args->{search} && $args->{search} =~ s/\s*\+(\d+)\s*/ /); |
144 |
|
|
145 |
|
my $sql = $context ? $sql_context : $sql_message; |
146 |
|
|
147 |
$sql .= " where message ilike ? " if ($args->{search}); |
$sql .= " where message ilike ? " if ($args->{search}); |
148 |
$sql .= " order by log.time desc"; |
$sql .= " order by log.time desc"; |
149 |
$sql .= " limit " . $args->{limit}; |
$sql .= " limit " . $args->{limit}; |
150 |
|
|
151 |
my $sth = $dbh->prepare( $sql ); |
my $sth = $dbh->prepare( $sql ); |
152 |
if ($args->{search}) { |
if (my $search = $args->{search}) { |
153 |
$sth->execute( '%' . $args->{search} . '%' ); |
$search =~ s/^\s+//; |
154 |
warn "search for '$args->{search}' returned ", $sth->rows, " results\n"; |
$search =~ s/\s+$//; |
155 |
|
$sth->execute( '%' . $search . '%' ); |
156 |
|
warn "search for '$search' returned ", $sth->rows, " results ", $context || '', "\n"; |
157 |
} else { |
} else { |
158 |
$sth->execute(); |
$sth->execute(); |
159 |
} |
} |
174 |
"Showing " . ($#rows + 1) . " messages..." |
"Showing " . ($#rows + 1) . " messages..." |
175 |
); |
); |
176 |
|
|
177 |
|
if ($context) { |
178 |
|
my @ids = @rows; |
179 |
|
@rows = (); |
180 |
|
|
181 |
|
my $last_to = 0; |
182 |
|
|
183 |
|
my $sth = $dbh->prepare( $sql_message . qq{ where id >= ? and id < ? } ); |
184 |
|
foreach my $row_id (sort { $a->{id} <=> $b->{id} } @ids) { |
185 |
|
my $id = $row_id->{id} || die "can't find id in row"; |
186 |
|
|
187 |
|
my ($from, $to) = ($id - $context, $id + $context); |
188 |
|
$from = $last_to if ($from < $last_to); |
189 |
|
$last_to = $to; |
190 |
|
$sth->execute( $from, $to ); |
191 |
|
|
192 |
|
#warn "## id: $id from: $from to: $to returned: ", $sth->rows, "\n"; |
193 |
|
|
194 |
|
while (my $row = $sth->fetchrow_hashref) { |
195 |
|
push @rows, $row; |
196 |
|
} |
197 |
|
|
198 |
|
} |
199 |
|
} |
200 |
|
|
201 |
foreach my $row (@rows) { |
foreach my $row (@rows) { |
202 |
|
|
203 |
$row->{time} =~ s#\.\d+##; |
$row->{time} =~ s#\.\d+##; |
216 |
|
|
217 |
my $append = 1; |
my $append = 1; |
218 |
|
|
219 |
if ($last_row->{nick} ne $row->{nick}) { |
my $nick = $row->{nick}; |
220 |
|
$nick =~ s/^_*(.*?)_*$/$1/; |
221 |
|
|
222 |
|
if ($last_row->{nick} ne $nick) { |
223 |
# obfu way to find format for me_nick if needed or fallback to default |
# obfu way to find format for me_nick if needed or fallback to default |
224 |
my $fmt = $row->{me} ? ( $args->{fmt}->{me_nick} || $args->{fmt}->{nick} ) : $args->{fmt}->{nick}; |
my $fmt = $row->{me} ? ( $args->{fmt}->{me_nick} || $args->{fmt}->{nick} ) : $args->{fmt}->{nick}; |
225 |
$fmt ||= '%s'; |
$fmt ||= '%s'; |
226 |
|
|
|
my $nick = $row->{nick}; |
|
227 |
$nick = $args->{filter}->{nick}->($nick) if (ref($args->{filter}->{nick}) eq 'CODE'); |
$nick = $args->{filter}->{nick}->($nick) if (ref($args->{filter}->{nick}) eq 'CODE'); |
228 |
|
|
229 |
$msg .= sprintf( $fmt, $nick ); |
$msg .= sprintf( $fmt, $nick ); |
339 |
|
|
340 |
$res = ''; |
$res = ''; |
341 |
|
|
342 |
} elsif ($msg =~ m/^(search|grep)\s+(.*)$/i) { |
} elsif ($msg =~ m/^(search|grep)\s+(.*)\s*$/i) { |
343 |
|
|
344 |
my $what = $2; |
my $what = $2; |
345 |
|
|
346 |
foreach my $res (get_from_log( limit => 20, search => $what )) { |
foreach my $res (get_from_log( |
347 |
|
limit => 20, |
348 |
|
search => $what, |
349 |
|
)) { |
350 |
print "search [$what]: $res\n"; |
print "search [$what]: $res\n"; |
351 |
from_to($res, $ENCODING, 'UTF-8'); |
from_to($res, $ENCODING, 'UTF-8'); |
352 |
$_[KERNEL]->post( $IRC_ALIAS => privmsg => $nick, $res ); |
$_[KERNEL]->post( $IRC_ALIAS => privmsg => $nick, $res ); |