1 |
#!/usr/bin/perl -w |
2 |
use strict; |
3 |
$|++; |
4 |
|
5 |
=head1 NAME |
6 |
|
7 |
irc-logger.pl |
8 |
|
9 |
=head1 SYNOPSIS |
10 |
|
11 |
./irc-logger.pl |
12 |
|
13 |
=head1 DESCRIPTION |
14 |
|
15 |
log all conversation on irc channel |
16 |
|
17 |
=cut |
18 |
|
19 |
## CONFIG |
20 |
|
21 |
my $NICK = 'irc-logger'; |
22 |
my $CONNECT = |
23 |
{Server => 'irc.freenode.net', |
24 |
Nick => $NICK, |
25 |
Ircname => "try /msg $NICK help", |
26 |
}; |
27 |
my $CHANNEL = '#razmjenavjestina'; |
28 |
my $IRC_ALIAS = "log"; |
29 |
|
30 |
my %FOLLOWS = |
31 |
( |
32 |
ACCESS => "/var/log/apache/access.log", |
33 |
ERROR => "/var/log/apache/error.log", |
34 |
); |
35 |
|
36 |
my $DSN = 'DBI:Pg:dbname=irc-logger'; |
37 |
|
38 |
my $ENCODING = 'ISO-8859-2'; |
39 |
|
40 |
## END CONFIG |
41 |
|
42 |
|
43 |
|
44 |
use POE qw(Component::IRC Wheel::FollowTail Component::Server::HTTP); |
45 |
use HTTP::Status; |
46 |
use DBI; |
47 |
use Encode qw/from_to/; |
48 |
use Regexp::Common qw /URI/; |
49 |
|
50 |
|
51 |
my $dbh = DBI->connect($DSN,"","", { RaiseError => 1, AutoCommit => 1 }) || die $DBI::errstr; |
52 |
|
53 |
=for SQL schema |
54 |
|
55 |
$dbh->do(qq{ |
56 |
create table log ( |
57 |
id serial, |
58 |
time timestamp default now(), |
59 |
channel text not null, |
60 |
nick text not null, |
61 |
message text not null, |
62 |
primary key(id) |
63 |
); |
64 |
|
65 |
create index log_time on log(time); |
66 |
create index log_channel on log(channel); |
67 |
create index log_nick on log(nick); |
68 |
|
69 |
}); |
70 |
|
71 |
=cut |
72 |
|
73 |
my $sth = $dbh->prepare(qq{ |
74 |
insert into log |
75 |
(channel, nick, message) |
76 |
values (?,?,?) |
77 |
}); |
78 |
|
79 |
=head2 get_from_log |
80 |
|
81 |
my @messages = get_from_log( |
82 |
limit => 42, |
83 |
search => '%what to stuff in ilike%', |
84 |
fmt => { |
85 |
time => '{%s} ', |
86 |
time_channel => '{%s %s} ', |
87 |
nick => '%s: ', |
88 |
message => '%s', |
89 |
}, |
90 |
message_filter => sub { |
91 |
# modify message content |
92 |
return shift; |
93 |
} |
94 |
); |
95 |
|
96 |
=cut |
97 |
|
98 |
sub get_from_log { |
99 |
my $args = {@_}; |
100 |
|
101 |
$args->{limit} ||= 10; |
102 |
|
103 |
$args->{fmt} ||= { |
104 |
time => '{%s} ', |
105 |
time_channel => '{%s %s} ', |
106 |
nick => '%s: ', |
107 |
message => '%s', |
108 |
}; |
109 |
|
110 |
my $sql = qq{ |
111 |
select |
112 |
time::date as date, |
113 |
time::time as time, |
114 |
channel, |
115 |
nick, |
116 |
message |
117 |
from log |
118 |
}; |
119 |
$sql .= " where message ilike ? " if ($args->{search}); |
120 |
$sql .= " order by log.time desc"; |
121 |
$sql .= " limit " . $args->{limit}; |
122 |
|
123 |
my $sth = $dbh->prepare( $sql ); |
124 |
if ($args->{search}) { |
125 |
$sth->execute( $args->{search} ); |
126 |
} else { |
127 |
$sth->execute(); |
128 |
} |
129 |
my $last_row = { |
130 |
date => '', |
131 |
time => '', |
132 |
channel => '', |
133 |
nick => '', |
134 |
}; |
135 |
|
136 |
my @rows; |
137 |
|
138 |
while (my $row = $sth->fetchrow_hashref) { |
139 |
unshift @rows, $row; |
140 |
} |
141 |
|
142 |
my @msgs; |
143 |
|
144 |
foreach my $row (@rows) { |
145 |
|
146 |
$row->{time} =~ s#\.\d+##; |
147 |
|
148 |
my $t; |
149 |
$t = $row->{date} . ' ' if ($last_row->{date} ne $row->{date}); |
150 |
$t .= $row->{time}; |
151 |
|
152 |
my $msg = ''; |
153 |
|
154 |
if ($last_row->{channel} ne $row->{channel}) { |
155 |
$msg .= sprintf($args->{fmt}->{time_channel}, $t, $row->{channel}); |
156 |
} else { |
157 |
$msg .= sprintf($args->{fmt}->{time}, $t); |
158 |
} |
159 |
|
160 |
my $append = 1; |
161 |
|
162 |
if ($last_row->{nick} ne $row->{nick}) { |
163 |
$msg .= sprintf($args->{fmt}->{nick}, $row->{nick}); |
164 |
$append = 0; |
165 |
} |
166 |
|
167 |
if (ref($args->{message_filter}) eq 'CODE') { |
168 |
$msg .= sprintf($args->{fmt}->{message}, |
169 |
$args->{message_filter}->( |
170 |
$row->{message} |
171 |
) |
172 |
); |
173 |
} else { |
174 |
$msg .= sprintf($args->{fmt}->{message}, $row->{message}); |
175 |
} |
176 |
|
177 |
if ($append && @msgs) { |
178 |
$msgs[$#msgs] .= " " . $msg; |
179 |
} else { |
180 |
push @msgs, $msg; |
181 |
} |
182 |
|
183 |
$last_row = $row; |
184 |
} |
185 |
|
186 |
return @msgs; |
187 |
} |
188 |
|
189 |
|
190 |
my $SKIPPING = 0; # if skipping, how many we've done |
191 |
my $SEND_QUEUE; # cache |
192 |
|
193 |
POE::Component::IRC->new($IRC_ALIAS); |
194 |
|
195 |
POE::Session->create |
196 |
(inline_states => |
197 |
{_start => sub { |
198 |
$_[KERNEL]->post($IRC_ALIAS => register => 'all'); |
199 |
$_[KERNEL]->post($IRC_ALIAS => connect => $CONNECT); |
200 |
}, |
201 |
irc_255 => sub { # server is done blabbing |
202 |
$_[KERNEL]->post($IRC_ALIAS => join => $CHANNEL); |
203 |
$_[KERNEL]->post($IRC_ALIAS => join => '#logger'); |
204 |
$_[KERNEL]->yield("heartbeat"); # start heartbeat |
205 |
# $_[KERNEL]->yield("my_add", $_) for keys %FOLLOWS; |
206 |
$_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "IDENTIFY $NICK" ); |
207 |
}, |
208 |
irc_public => sub { |
209 |
my $kernel = $_[KERNEL]; |
210 |
my $nick = (split /!/, $_[ARG0])[0]; |
211 |
my $channel = $_[ARG1]->[0]; |
212 |
my $msg = $_[ARG2]; |
213 |
|
214 |
from_to($msg, 'UTF-8', $ENCODING); |
215 |
|
216 |
print "$channel: <$nick> $msg\n"; |
217 |
$sth->execute($channel, $nick, $msg); |
218 |
}, |
219 |
irc_msg => sub { |
220 |
my $kernel = $_[KERNEL]; |
221 |
my $nick = (split /!/, $_[ARG0])[0]; |
222 |
my $msg = $_[ARG2]; |
223 |
from_to($msg, 'UTF-8', $ENCODING); |
224 |
|
225 |
my $res = "unknown command '$msg', try /msg $NICK help!"; |
226 |
my @out; |
227 |
|
228 |
print "<< $msg\n"; |
229 |
|
230 |
if ($msg =~ m/^help/i) { |
231 |
|
232 |
$res = "usage: /msg $NICK comand | commands: stat - user/message stat | last - show backtrace | grep foobar - find foobar"; |
233 |
|
234 |
} elsif ($msg =~ m/^msg\s+(\S+)\s+(.*)$/i) { |
235 |
|
236 |
print ">> /msg $1 $2\n"; |
237 |
$_[KERNEL]->post( $IRC_ALIAS => privmsg => $1, $2 ); |
238 |
$res = ''; |
239 |
|
240 |
} elsif ($msg =~ m/^stat.*?\s*(\d*)/i) { |
241 |
|
242 |
my $nr = $1 || 10; |
243 |
|
244 |
my $sth = $dbh->prepare(qq{ |
245 |
select nick,count(*) from log group by nick order by count desc limit $nr |
246 |
}); |
247 |
$sth->execute(); |
248 |
$res = "Top $nr users: "; |
249 |
my @users; |
250 |
while (my $row = $sth->fetchrow_hashref) { |
251 |
push @users,$row->{nick} . ': ' . $row->{count}; |
252 |
} |
253 |
$res .= join(" | ", @users); |
254 |
} elsif ($msg =~ m/^last.*?\s*(\d*)/i) { |
255 |
|
256 |
foreach my $res (get_from_log( limit => $1 )) { |
257 |
print "last: $res\n"; |
258 |
from_to($res, $ENCODING, 'UTF-8'); |
259 |
$_[KERNEL]->post( $IRC_ALIAS => privmsg => $nick, $res ); |
260 |
} |
261 |
|
262 |
$res = ''; |
263 |
|
264 |
} elsif ($msg =~ m/^(search|grep)\s+(.*)$/i) { |
265 |
|
266 |
my $what = $2; |
267 |
|
268 |
foreach my $res (get_from_log( limit => 20, search => "%${what}%" )) { |
269 |
print "search [$what]: $res\n"; |
270 |
from_to($res, $ENCODING, 'UTF-8'); |
271 |
$_[KERNEL]->post( $IRC_ALIAS => privmsg => $nick, $res ); |
272 |
} |
273 |
|
274 |
$res = ''; |
275 |
|
276 |
} |
277 |
|
278 |
if ($res) { |
279 |
print ">> [$nick] $res\n"; |
280 |
from_to($res, $ENCODING, 'UTF-8'); |
281 |
$_[KERNEL]->post( $IRC_ALIAS => privmsg => $nick, $res ); |
282 |
} |
283 |
|
284 |
}, |
285 |
irc_477 => sub { |
286 |
print "# irc_477: ",$_[ARG1], "\n"; |
287 |
$_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "register $NICK" ); |
288 |
}, |
289 |
irc_505 => sub { |
290 |
print "# irc_505: ",$_[ARG1], "\n"; |
291 |
$_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "register $NICK" ); |
292 |
# $_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "set hide email on" ); |
293 |
# $_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "set email dpavlin\@rot13.org" ); |
294 |
}, |
295 |
irc_registered => sub { |
296 |
warn "## indetify $NICK\n"; |
297 |
$_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "IDENTIFY $NICK" ); |
298 |
}, |
299 |
# irc_433 => sub { |
300 |
# print "# irc_433: ",$_[ARG1], "\n"; |
301 |
# warn "## indetify $NICK\n"; |
302 |
# $_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "IDENTIFY $NICK" ); |
303 |
# }, |
304 |
irc_372 => sub { |
305 |
print "MOTD: ", $_[ARG1], "\n"; |
306 |
}, |
307 |
irc_snotice => sub { |
308 |
print "(server notice): ", $_[ARG0], "\n"; |
309 |
}, |
310 |
(map |
311 |
{ |
312 |
;"irc_$_" => sub { }} |
313 |
qw( |
314 |
)), |
315 |
# join |
316 |
# ctcp_version |
317 |
# connected snotice ctcp_action ping notice mode part quit |
318 |
# 001 002 003 004 005 |
319 |
# 250 251 252 253 254 265 266 |
320 |
# 332 333 353 366 372 375 376 |
321 |
# 477 |
322 |
_child => sub {}, |
323 |
_default => sub { |
324 |
printf "%s: session %s caught an unhandled %s event.\n", |
325 |
scalar localtime(), $_[SESSION]->ID, $_[ARG0]; |
326 |
print "The $_[ARG0] event was given these parameters: ", |
327 |
join(" ", map({"ARRAY" eq ref $_ ? "[@$_]" : "$_"} @{$_[ARG1]})), "\n"; |
328 |
0; # false for signals |
329 |
}, |
330 |
my_add => sub { |
331 |
my $trailing = $_[ARG0]; |
332 |
my $session = $_[SESSION]; |
333 |
POE::Session->create |
334 |
(inline_states => |
335 |
{_start => sub { |
336 |
$_[HEAP]->{wheel} = |
337 |
POE::Wheel::FollowTail->new |
338 |
( |
339 |
Filename => $FOLLOWS{$trailing}, |
340 |
InputEvent => 'got_line', |
341 |
); |
342 |
}, |
343 |
got_line => sub { |
344 |
$_[KERNEL]->post($session => my_tailed => |
345 |
time, $trailing, $_[ARG0]); |
346 |
}, |
347 |
}, |
348 |
); |
349 |
|
350 |
}, |
351 |
my_tailed => sub { |
352 |
my ($time, $file, $line) = @_[ARG0..ARG2]; |
353 |
## $time will be undef on a probe, or a time value if a real line |
354 |
|
355 |
## PoCo::IRC has throttling built in, but no external visibility |
356 |
## so this is reaching "under the hood" |
357 |
$SEND_QUEUE ||= |
358 |
$_[KERNEL]->alias_resolve($IRC_ALIAS)->get_heap->{send_queue}; |
359 |
|
360 |
## handle "no need to keep skipping" transition |
361 |
if ($SKIPPING and @$SEND_QUEUE < 1) { |
362 |
$_[KERNEL]->post($IRC_ALIAS => privmsg => $CHANNEL => |
363 |
"[discarded $SKIPPING messages]"); |
364 |
$SKIPPING = 0; |
365 |
} |
366 |
|
367 |
## handle potential message display |
368 |
if ($time) { |
369 |
if ($SKIPPING or @$SEND_QUEUE > 3) { # 3 msgs per 10 seconds |
370 |
$SKIPPING++; |
371 |
} else { |
372 |
my @time = localtime $time; |
373 |
$_[KERNEL]->post($IRC_ALIAS => privmsg => $CHANNEL => |
374 |
sprintf "%02d:%02d:%02d: %s: %s", |
375 |
($time[2] + 11) % 12 + 1, $time[1], $time[0], |
376 |
$file, $line); |
377 |
} |
378 |
} |
379 |
|
380 |
## handle re-probe/flush if skipping |
381 |
if ($SKIPPING) { |
382 |
$_[KERNEL]->delay($_[STATE] => 0.5); # $time will be undef |
383 |
} |
384 |
|
385 |
}, |
386 |
my_heartbeat => sub { |
387 |
$_[KERNEL]->yield(my_tailed => time, "heartbeat", "beep"); |
388 |
$_[KERNEL]->delay($_[STATE] => 10); |
389 |
} |
390 |
}, |
391 |
); |
392 |
|
393 |
# http server |
394 |
|
395 |
my $httpd = POE::Component::Server::HTTP->new( |
396 |
Port => $NICK =~ m/-dev/ ? 8001 : 8000, |
397 |
ContentHandler => { '/' => \&root_handler }, |
398 |
Headers => { Server => 'irc-logger' }, |
399 |
); |
400 |
|
401 |
my $style = <<'_END_OF_STYLE_'; |
402 |
.time, .channel { color: #808080; font-size: 60%; } |
403 |
.nick { color: #0000ff; font-size: 80%; } |
404 |
.message { color: #000000; font-size: 100%; } |
405 |
_END_OF_STYLE_ |
406 |
|
407 |
sub root_handler { |
408 |
my ($request, $response) = @_; |
409 |
$response->code(RC_OK); |
410 |
$response->content_type("text/html; charset=$ENCODING"); |
411 |
$response->content( |
412 |
qq{<html><head><title>$NICK</title><style type="text/css">$style</style></head><body>} . |
413 |
"irc-logger url: " . $request->uri . '<br/>' . |
414 |
join("<br/>", |
415 |
get_from_log( |
416 |
limit => 100, |
417 |
fmt => { |
418 |
time => '<span class="time">%s</span> ', |
419 |
time_channel => '<span class="channel">%s %s</span> ', |
420 |
nick => '<span class="nick">%s:</span> ', |
421 |
message => '<span class="message">%s</span>', |
422 |
}, |
423 |
message_filter => sub { |
424 |
my $m = shift || return; |
425 |
$m =~ s#($RE{URI}{HTTP})#<a href="$1">$1</a>#gs; |
426 |
return $m; |
427 |
}, |
428 |
) |
429 |
) . |
430 |
qq{</body></html>} |
431 |
); |
432 |
return RC_OK; |
433 |
} |
434 |
|
435 |
POE::Kernel->run; |