18 |
|
|
19 |
## CONFIG |
## CONFIG |
20 |
|
|
21 |
my $NICK = 'irc-logger-dev'; |
my $NICK = 'irc-logger'; |
22 |
my $CONNECT = |
my $CONNECT = |
23 |
{Server => 'irc.freenode.net', |
{Server => 'irc.freenode.net', |
24 |
Nick => $NICK, |
Nick => $NICK, |
33 |
ERROR => "/var/log/apache/error.log", |
ERROR => "/var/log/apache/error.log", |
34 |
); |
); |
35 |
|
|
36 |
my $DSN = 'DBI:Pg:dbname=irc-logger'; |
my $DSN = 'DBI:Pg:dbname=' . $NICK; |
37 |
|
|
38 |
my $ENCODING = 'ISO-8859-2'; |
my $ENCODING = 'ISO-8859-2'; |
39 |
|
|
51 |
|
|
52 |
my $dbh = DBI->connect($DSN,"","", { RaiseError => 1, AutoCommit => 1 }) || die $DBI::errstr; |
my $dbh = DBI->connect($DSN,"","", { RaiseError => 1, AutoCommit => 1 }) || die $DBI::errstr; |
53 |
|
|
54 |
=for SQL schema |
eval { |
55 |
|
$dbh->do(qq{ select count(*) from log }); |
56 |
|
}; |
57 |
|
|
58 |
|
if ($@) { |
59 |
|
warn "creating database table in $DSN\n"; |
60 |
|
$dbh->do(<<'_SQL_SCHEMA_'); |
61 |
|
|
|
$dbh->do(qq{ |
|
62 |
create table log ( |
create table log ( |
63 |
id serial, |
id serial, |
64 |
time timestamp default now(), |
time timestamp default now(), |
65 |
channel text not null, |
channel text not null, |
66 |
|
me boolean default false, |
67 |
nick text not null, |
nick text not null, |
68 |
message text not null, |
message text not null, |
69 |
primary key(id) |
primary key(id) |
73 |
create index log_channel on log(channel); |
create index log_channel on log(channel); |
74 |
create index log_nick on log(nick); |
create index log_nick on log(nick); |
75 |
|
|
76 |
}); |
_SQL_SCHEMA_ |
77 |
|
} |
|
=cut |
|
78 |
|
|
79 |
my $sth = $dbh->prepare(qq{ |
my $sth = $dbh->prepare(qq{ |
80 |
insert into log |
insert into log |
81 |
(channel, nick, message) |
(channel, me, nick, message) |
82 |
values (?,?,?) |
values (?,?,?,?) |
83 |
}); |
}); |
84 |
|
|
85 |
=head2 get_from_log |
=head2 get_from_log |
91 |
time => '{%s} ', |
time => '{%s} ', |
92 |
time_channel => '{%s %s} ', |
time_channel => '{%s %s} ', |
93 |
nick => '%s: ', |
nick => '%s: ', |
94 |
|
me_nick => '***%s ', |
95 |
message => '%s', |
message => '%s', |
96 |
}, |
}, |
97 |
message_filter => sub { |
filter => { |
98 |
# modify message content |
message => sub { |
99 |
return shift; |
# modify message content |
100 |
} |
return shift; |
101 |
|
} |
102 |
|
}, |
103 |
|
context => 5, |
104 |
); |
); |
105 |
|
|
106 |
|
Order is important. Fields are first passed through C<filter> (if available) and |
107 |
|
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 { |
116 |
$args->{limit} ||= 10; |
$args->{limit} ||= 10; |
117 |
|
|
118 |
$args->{fmt} ||= { |
$args->{fmt} ||= { |
119 |
|
date => '[%s] ', |
120 |
time => '{%s} ', |
time => '{%s} ', |
121 |
time_channel => '{%s %s} ', |
time_channel => '{%s %s} ', |
122 |
nick => '%s: ', |
nick => '%s: ', |
123 |
|
me_nick => '***%s ', |
124 |
message => '%s', |
message => '%s', |
125 |
}; |
}; |
126 |
|
|
127 |
my $sql = qq{ |
my $sql_message = qq{ |
128 |
select |
select |
129 |
time::date as date, |
time::date as date, |
130 |
time::time as time, |
time::time as time, |
131 |
channel, |
channel, |
132 |
|
me, |
133 |
nick, |
nick, |
134 |
message |
message |
135 |
from log |
from log |
136 |
}; |
}; |
137 |
$sql .= " where message ilike ? " if ($args->{search}); |
|
138 |
|
my $sql_context = qq{ |
139 |
|
select |
140 |
|
id |
141 |
|
from log |
142 |
|
}; |
143 |
|
|
144 |
|
my $context = $1 if ($args->{search} && $args->{search} =~ s/\s*\+(\d+)\s*/ /); |
145 |
|
|
146 |
|
my $sql = $context ? $sql_context : $sql_message; |
147 |
|
|
148 |
|
$sql .= " where message ilike ? or nick ilike ? " if ($args->{search}); |
149 |
$sql .= " order by log.time desc"; |
$sql .= " order by log.time desc"; |
150 |
$sql .= " limit " . $args->{limit}; |
$sql .= " limit " . $args->{limit}; |
151 |
|
|
152 |
my $sth = $dbh->prepare( $sql ); |
my $sth = $dbh->prepare( $sql ); |
153 |
if ($args->{search}) { |
if (my $search = $args->{search}) { |
154 |
$sth->execute( '%' . $args->{search} . '%' ); |
$search =~ s/^\s+//; |
155 |
warn "search for '$args->{search}' returned ", $sth->rows, " results\n"; |
$search =~ s/\s+$//; |
156 |
|
$sth->execute( ( '%' . $search . '%' ) x 2 ); |
157 |
|
warn "search for '$search' returned ", $sth->rows, " results ", $context || '', "\n"; |
158 |
} else { |
} else { |
159 |
$sth->execute(); |
$sth->execute(); |
160 |
} |
} |
175 |
"Showing " . ($#rows + 1) . " messages..." |
"Showing " . ($#rows + 1) . " messages..." |
176 |
); |
); |
177 |
|
|
178 |
|
if ($context) { |
179 |
|
my @ids = @rows; |
180 |
|
@rows = (); |
181 |
|
|
182 |
|
my $last_to = 0; |
183 |
|
|
184 |
|
my $sth = $dbh->prepare( $sql_message . qq{ where id >= ? and id < ? } ); |
185 |
|
foreach my $row_id (sort { $a->{id} <=> $b->{id} } @ids) { |
186 |
|
my $id = $row_id->{id} || die "can't find id in row"; |
187 |
|
|
188 |
|
my ($from, $to) = ($id - $context, $id + $context); |
189 |
|
$from = $last_to if ($from < $last_to); |
190 |
|
$last_to = $to; |
191 |
|
$sth->execute( $from, $to ); |
192 |
|
|
193 |
|
#warn "## id: $id from: $from to: $to returned: ", $sth->rows, "\n"; |
194 |
|
|
195 |
|
while (my $row = $sth->fetchrow_hashref) { |
196 |
|
push @rows, $row; |
197 |
|
} |
198 |
|
|
199 |
|
} |
200 |
|
} |
201 |
|
|
202 |
foreach my $row (@rows) { |
foreach my $row (@rows) { |
203 |
|
|
204 |
$row->{time} =~ s#\.\d+##; |
$row->{time} =~ s#\.\d+##; |
205 |
|
|
|
my $t; |
|
|
$t = $row->{date} . ' ' if ($last_row->{date} ne $row->{date}); |
|
|
$t .= $row->{time}; |
|
|
|
|
206 |
my $msg = ''; |
my $msg = ''; |
207 |
|
|
208 |
|
$msg = sprintf($args->{fmt}->{date}, $row->{date}) . ' ' if ($last_row->{date} ne $row->{date}); |
209 |
|
my $t = $row->{time}; |
210 |
|
|
211 |
if ($last_row->{channel} ne $row->{channel}) { |
if ($last_row->{channel} ne $row->{channel}) { |
212 |
$msg .= sprintf($args->{fmt}->{time_channel}, $t, $row->{channel}); |
$msg .= sprintf($args->{fmt}->{time_channel}, $t, $row->{channel}); |
213 |
} else { |
} else { |
216 |
|
|
217 |
my $append = 1; |
my $append = 1; |
218 |
|
|
219 |
if ($last_row->{nick} ne $row->{nick}) { |
my $nick = $row->{nick}; |
220 |
$msg .= sprintf($args->{fmt}->{nick}, $row->{nick}); |
if ($nick =~ s/^_*(.*?)_*$/$1/) { |
221 |
|
$row->{nick} = $nick; |
222 |
|
} |
223 |
|
|
224 |
|
if ($last_row->{nick} ne $nick) { |
225 |
|
# obfu way to find format for me_nick if needed or fallback to default |
226 |
|
my $fmt = $row->{me} ? ( $args->{fmt}->{me_nick} || $args->{fmt}->{nick} ) : $args->{fmt}->{nick}; |
227 |
|
$fmt ||= '%s'; |
228 |
|
|
229 |
|
$nick = $args->{filter}->{nick}->($nick) if (ref($args->{filter}->{nick}) eq 'CODE'); |
230 |
|
|
231 |
|
$msg .= sprintf( $fmt, $nick ); |
232 |
$append = 0; |
$append = 0; |
233 |
} |
} |
234 |
|
|
235 |
if (ref($args->{message_filter}) eq 'CODE') { |
$args->{fmt}->{message} ||= '%s'; |
236 |
|
if (ref($args->{filter}->{message}) eq 'CODE') { |
237 |
$msg .= sprintf($args->{fmt}->{message}, |
$msg .= sprintf($args->{fmt}->{message}, |
238 |
$args->{message_filter}->( |
$args->{filter}->{message}->( |
239 |
$row->{message} |
$row->{message} |
240 |
) |
) |
241 |
); |
); |
283 |
from_to($msg, 'UTF-8', $ENCODING); |
from_to($msg, 'UTF-8', $ENCODING); |
284 |
|
|
285 |
print "$channel: <$nick> $msg\n"; |
print "$channel: <$nick> $msg\n"; |
286 |
$sth->execute($channel, $nick, $msg); |
$sth->execute($channel, 0, $nick, $msg); |
287 |
|
}, |
288 |
|
irc_ctcp_action => sub { |
289 |
|
my $kernel = $_[KERNEL]; |
290 |
|
my $nick = (split /!/, $_[ARG0])[0]; |
291 |
|
my $channel = $_[ARG1]->[0]; |
292 |
|
my $msg = $_[ARG2]; |
293 |
|
|
294 |
|
from_to($msg, 'UTF-8', $ENCODING); |
295 |
|
|
296 |
|
print "$channel ***$nick $msg\n"; |
297 |
|
$sth->execute($channel, 1, $nick, $msg); |
298 |
}, |
}, |
299 |
irc_msg => sub { |
irc_msg => sub { |
300 |
my $kernel = $_[KERNEL]; |
my $kernel = $_[KERNEL]; |
341 |
|
|
342 |
$res = ''; |
$res = ''; |
343 |
|
|
344 |
} elsif ($msg =~ m/^(search|grep)\s+(.*)$/i) { |
} elsif ($msg =~ m/^(search|grep)\s+(.*)\s*$/i) { |
345 |
|
|
346 |
my $what = $2; |
my $what = $2; |
347 |
|
|
348 |
foreach my $res (get_from_log( limit => 20, search => $what )) { |
foreach my $res (get_from_log( |
349 |
|
limit => 20, |
350 |
|
search => $what, |
351 |
|
)) { |
352 |
print "search [$what]: $res\n"; |
print "search [$what]: $res\n"; |
353 |
from_to($res, $ENCODING, 'UTF-8'); |
from_to($res, $ENCODING, 'UTF-8'); |
354 |
$_[KERNEL]->post( $IRC_ALIAS => privmsg => $nick, $res ); |
$_[KERNEL]->post( $IRC_ALIAS => privmsg => $nick, $res ); |
487 |
my $style = <<'_END_OF_STYLE_'; |
my $style = <<'_END_OF_STYLE_'; |
488 |
p { margin: 0; padding: 0.1em; } |
p { margin: 0; padding: 0.1em; } |
489 |
.time, .channel { color: #808080; font-size: 60%; } |
.time, .channel { color: #808080; font-size: 60%; } |
490 |
.nick { color: #0000ff; font-size: 80%; } |
.date { float: right; clear: right; background: #e0e0e0; color: #404040; font-size: 120%; padding: 0.25em; border: 1px dashed #808080; } |
491 |
|
.nick { color: #000000; font-size: 80%; padding: 2px; font-family: courier, courier new, monospace ; } |
492 |
.message { color: #000000; font-size: 100%; } |
.message { color: #000000; font-size: 100%; } |
493 |
.search { float: right; } |
.search { float: right; } |
494 |
|
.col-0 { background: #ffff66 } |
495 |
|
.col-1 { background: #a0ffff } |
496 |
|
.col-2 { background: #99ff99 } |
497 |
|
.col-3 { background: #ff9999 } |
498 |
|
.col-4 { background: #ff66ff } |
499 |
_END_OF_STYLE_ |
_END_OF_STYLE_ |
500 |
|
|
501 |
|
my $max_color = 4; |
502 |
|
|
503 |
|
my %nick_enumerator; |
504 |
|
|
505 |
sub root_handler { |
sub root_handler { |
506 |
my ($request, $response) = @_; |
my ($request, $response) = @_; |
507 |
$response->code(RC_OK); |
$response->code(RC_OK); |
529 |
} . |
} . |
530 |
join("</p><p>", |
join("</p><p>", |
531 |
get_from_log( |
get_from_log( |
532 |
limit => $q->param('limit') || 100, |
limit => $q->param('last') || 100, |
533 |
search => $q->param('search') || $q->param('grep') || undef, |
search => $q->param('search') || $q->param('grep') || undef, |
534 |
fmt => { |
fmt => { |
535 |
|
date => '<hr size="1" style="clear: both;"/><div class="date">%s</div> ', |
536 |
time => '<span class="time">%s</span> ', |
time => '<span class="time">%s</span> ', |
537 |
time_channel => '<span class="channel">%s %s</span> ', |
time_channel => '<span class="channel">%s %s</span> ', |
538 |
nick => '<span class="nick">%s:</span> ', |
nick => '%s: ', |
539 |
|
me_nick => '***%s ', |
540 |
message => '<span class="message">%s</span>', |
message => '<span class="message">%s</span>', |
541 |
}, |
}, |
542 |
message_filter => sub { |
filter => { |
543 |
my $m = shift || return; |
message => sub { |
544 |
$m =~ s/($escape_re)/$escape{$1}/gs; |
my $m = shift || return; |
545 |
$m =~ s#($RE{URI}{HTTP})#<a href="$1">$1</a>#gs; |
$m =~ s/($escape_re)/$escape{$1}/gs; |
546 |
return $m; |
$m =~ s#($RE{URI}{HTTP})#<a href="$1">$1</a>#gs; |
547 |
|
return $m; |
548 |
|
}, |
549 |
|
nick => sub { |
550 |
|
my $n = shift || return; |
551 |
|
if (! $nick_enumerator{$n}) { |
552 |
|
my $max = scalar keys %nick_enumerator; |
553 |
|
$nick_enumerator{$n} = $max + 1; |
554 |
|
} |
555 |
|
return '<span class="nick col-' . |
556 |
|
( $nick_enumerator{$n} % $max_color ) . |
557 |
|
'">' . $n . '</span>'; |
558 |
|
}, |
559 |
}, |
}, |
560 |
) |
) |
561 |
) . |
) . |