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-dev'; |
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 |
## END CONFIG |
39 |
|
40 |
|
41 |
|
42 |
use POE qw(Component::IRC Wheel::FollowTail); |
43 |
use DBI; |
44 |
use Encode qw/from_to/; |
45 |
|
46 |
|
47 |
my $dbh = DBI->connect($DSN,"","", { RaiseError => 1, AutoCommit => 1 }) || die $DBI::errstr; |
48 |
|
49 |
=for SQL schema |
50 |
|
51 |
$dbh->do(qq{ |
52 |
create table log ( |
53 |
id serial, |
54 |
time timestamp default now(), |
55 |
channel text not null, |
56 |
nick text not null, |
57 |
message text not null, |
58 |
primary key(id) |
59 |
); |
60 |
|
61 |
create index log_time on log(time); |
62 |
create index log_channel on log(channel); |
63 |
create index log_nick on log(nick); |
64 |
|
65 |
}); |
66 |
|
67 |
=cut |
68 |
|
69 |
my $sth = $dbh->prepare(qq{ |
70 |
insert into log |
71 |
(channel, nick, message) |
72 |
values (?,?,?) |
73 |
}); |
74 |
|
75 |
=head2 get_from_log |
76 |
|
77 |
my @messages = get_from_log( |
78 |
limit => 42, |
79 |
search => '%what to stuff in ilike%', |
80 |
); |
81 |
|
82 |
=cut |
83 |
|
84 |
sub get_from_log { |
85 |
my $args = {@_}; |
86 |
|
87 |
$args->{limit} ||= 10; |
88 |
|
89 |
my $sql = qq{ |
90 |
select |
91 |
time::date as date, |
92 |
time::time as time, |
93 |
channel, |
94 |
nick, |
95 |
message |
96 |
from log |
97 |
}; |
98 |
$sql .= " where message ilike ? " if ($args->{search}); |
99 |
$sql .= " order by log.time desc"; |
100 |
$sql .= " limit " . $args->{limit}; |
101 |
|
102 |
my $sth = $dbh->prepare( $sql ); |
103 |
if ($args->{search}) { |
104 |
$sth->execute( $args->{search} ); |
105 |
} else { |
106 |
$sth->execute(); |
107 |
} |
108 |
my $last_row = { |
109 |
date => '', |
110 |
time => '', |
111 |
channel => '', |
112 |
nick => '', |
113 |
}; |
114 |
|
115 |
my @rows; |
116 |
|
117 |
while (my $row = $sth->fetchrow_hashref) { |
118 |
unshift @rows, $row; |
119 |
} |
120 |
|
121 |
my @msgs; |
122 |
|
123 |
foreach my $row (@rows) { |
124 |
|
125 |
$row->{time} =~ s#\.\d+##; |
126 |
|
127 |
my $t; |
128 |
$t = $row->{date} . ' ' if ($last_row->{date} ne $row->{date}); |
129 |
$t .= $row->{time}; |
130 |
|
131 |
my $msg = ''; |
132 |
|
133 |
$msg .= "{$t"; |
134 |
$msg .= ' ' . $row->{channel} if ($last_row->{channel} ne $row->{channel}); |
135 |
$msg .= "} "; |
136 |
|
137 |
my $append = 1; |
138 |
|
139 |
if ($last_row->{nick} ne $row->{nick}) { |
140 |
$msg .= $row->{nick} . ': '; |
141 |
$append = 0; |
142 |
} |
143 |
|
144 |
$msg .= $row->{message}; |
145 |
|
146 |
if ($append && @msgs) { |
147 |
$msgs[$#msgs] .= " " . $msg; |
148 |
} else { |
149 |
push @msgs, $msg; |
150 |
} |
151 |
|
152 |
$last_row = $row; |
153 |
} |
154 |
|
155 |
return @msgs; |
156 |
} |
157 |
|
158 |
|
159 |
my $SKIPPING = 0; # if skipping, how many we've done |
160 |
my $SEND_QUEUE; # cache |
161 |
|
162 |
POE::Component::IRC->new($IRC_ALIAS); |
163 |
|
164 |
POE::Session->create |
165 |
(inline_states => |
166 |
{_start => sub { |
167 |
$_[KERNEL]->post($IRC_ALIAS => register => 'all'); |
168 |
$_[KERNEL]->post($IRC_ALIAS => connect => $CONNECT); |
169 |
}, |
170 |
irc_255 => sub { # server is done blabbing |
171 |
$_[KERNEL]->post($IRC_ALIAS => join => $CHANNEL); |
172 |
$_[KERNEL]->post($IRC_ALIAS => join => '#logger'); |
173 |
$_[KERNEL]->yield("heartbeat"); # start heartbeat |
174 |
# $_[KERNEL]->yield("my_add", $_) for keys %FOLLOWS; |
175 |
$_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "IDENTIFY $NICK" ); |
176 |
}, |
177 |
irc_public => sub { |
178 |
my $kernel = $_[KERNEL]; |
179 |
my $nick = (split /!/, $_[ARG0])[0]; |
180 |
my $channel = $_[ARG1]->[0]; |
181 |
my $msg = $_[ARG2]; |
182 |
|
183 |
from_to($msg, 'UTF-8', 'ISO-8859-2'); |
184 |
|
185 |
print "$channel: <$nick> $msg\n"; |
186 |
$sth->execute($channel, $nick, $msg); |
187 |
}, |
188 |
irc_msg => sub { |
189 |
my $kernel = $_[KERNEL]; |
190 |
my $nick = (split /!/, $_[ARG0])[0]; |
191 |
my $msg = $_[ARG2]; |
192 |
from_to($msg, 'UTF-8', 'ISO-8859-2'); |
193 |
|
194 |
my $res = "unknown command '$msg', try /msg $NICK help!"; |
195 |
my @out; |
196 |
|
197 |
print "<< $msg\n"; |
198 |
|
199 |
if ($msg =~ m/^help/i) { |
200 |
|
201 |
$res = "usage: /msg $NICK comand | commands: stat - user/message stat | last - show backtrace | grep foobar - find foobar"; |
202 |
|
203 |
} elsif ($msg =~ m/^msg\s+(\S+)\s+(.*)$/i) { |
204 |
|
205 |
print ">> /msg $1 $2\n"; |
206 |
$_[KERNEL]->post( $IRC_ALIAS => privmsg => $1, $2 ); |
207 |
$res = ''; |
208 |
|
209 |
} elsif ($msg =~ m/^stat.*?\s*(\d*)/i) { |
210 |
|
211 |
my $nr = $1 || 10; |
212 |
|
213 |
my $sth = $dbh->prepare(qq{ |
214 |
select nick,count(*) from log group by nick order by count desc limit $nr |
215 |
}); |
216 |
$sth->execute(); |
217 |
$res = "Top $nr users: "; |
218 |
my @users; |
219 |
while (my $row = $sth->fetchrow_hashref) { |
220 |
push @users,$row->{nick} . ': ' . $row->{count}; |
221 |
} |
222 |
$res .= join(" | ", @users); |
223 |
} elsif ($msg =~ m/^last.*?\s*(\d*)/i) { |
224 |
|
225 |
foreach my $res (get_from_log( limit => $1 )) { |
226 |
print "last: $res\n"; |
227 |
from_to($res, 'ISO-8859-2', 'UTF-8'); |
228 |
$_[KERNEL]->post( $IRC_ALIAS => privmsg => $nick, $res ); |
229 |
} |
230 |
|
231 |
$res = ''; |
232 |
|
233 |
} elsif ($msg =~ m/^(search|grep)\s+(.*)$/i) { |
234 |
|
235 |
my $what = $2; |
236 |
|
237 |
foreach my $res (get_from_log( limit => 20, search => "%${what}%" )) { |
238 |
print "search [$what]: $res\n"; |
239 |
from_to($res, 'ISO-8859-2', 'UTF-8'); |
240 |
$_[KERNEL]->post( $IRC_ALIAS => privmsg => $nick, $res ); |
241 |
} |
242 |
|
243 |
$res = ''; |
244 |
|
245 |
} |
246 |
|
247 |
if ($res) { |
248 |
print ">> [$nick] $res\n"; |
249 |
from_to($res, 'ISO-8859-2', 'UTF-8'); |
250 |
$_[KERNEL]->post( $IRC_ALIAS => privmsg => $nick, $res ); |
251 |
} |
252 |
|
253 |
}, |
254 |
irc_477 => sub { |
255 |
print "# irc_477: ",$_[ARG1], "\n"; |
256 |
$_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "register $NICK" ); |
257 |
}, |
258 |
irc_505 => sub { |
259 |
print "# irc_505: ",$_[ARG1], "\n"; |
260 |
$_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "register $NICK" ); |
261 |
# $_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "set hide email on" ); |
262 |
# $_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "set email dpavlin\@rot13.org" ); |
263 |
}, |
264 |
irc_registered => sub { |
265 |
warn "## indetify $NICK\n"; |
266 |
$_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "IDENTIFY $NICK" ); |
267 |
}, |
268 |
# irc_433 => sub { |
269 |
# print "# irc_433: ",$_[ARG1], "\n"; |
270 |
# warn "## indetify $NICK\n"; |
271 |
# $_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "IDENTIFY $NICK" ); |
272 |
# }, |
273 |
irc_372 => sub { |
274 |
print "MOTD: ", $_[ARG1], "\n"; |
275 |
}, |
276 |
irc_snotice => sub { |
277 |
print "(server notice): ", $_[ARG0], "\n"; |
278 |
}, |
279 |
(map |
280 |
{ |
281 |
;"irc_$_" => sub { }} |
282 |
qw( |
283 |
)), |
284 |
# join |
285 |
# ctcp_version |
286 |
# connected snotice ctcp_action ping notice mode part quit |
287 |
# 001 002 003 004 005 |
288 |
# 250 251 252 253 254 265 266 |
289 |
# 332 333 353 366 372 375 376 |
290 |
# 477 |
291 |
_child => sub {}, |
292 |
_default => sub { |
293 |
printf "%s: session %s caught an unhandled %s event.\n", |
294 |
scalar localtime(), $_[SESSION]->ID, $_[ARG0]; |
295 |
print "The $_[ARG0] event was given these parameters: ", |
296 |
join(" ", map({"ARRAY" eq ref $_ ? "[@$_]" : "$_"} @{$_[ARG1]})), "\n"; |
297 |
0; # false for signals |
298 |
}, |
299 |
my_add => sub { |
300 |
my $trailing = $_[ARG0]; |
301 |
my $session = $_[SESSION]; |
302 |
POE::Session->create |
303 |
(inline_states => |
304 |
{_start => sub { |
305 |
$_[HEAP]->{wheel} = |
306 |
POE::Wheel::FollowTail->new |
307 |
( |
308 |
Filename => $FOLLOWS{$trailing}, |
309 |
InputEvent => 'got_line', |
310 |
); |
311 |
}, |
312 |
got_line => sub { |
313 |
$_[KERNEL]->post($session => my_tailed => |
314 |
time, $trailing, $_[ARG0]); |
315 |
}, |
316 |
}, |
317 |
); |
318 |
|
319 |
}, |
320 |
my_tailed => sub { |
321 |
my ($time, $file, $line) = @_[ARG0..ARG2]; |
322 |
## $time will be undef on a probe, or a time value if a real line |
323 |
|
324 |
## PoCo::IRC has throttling built in, but no external visibility |
325 |
## so this is reaching "under the hood" |
326 |
$SEND_QUEUE ||= |
327 |
$_[KERNEL]->alias_resolve($IRC_ALIAS)->get_heap->{send_queue}; |
328 |
|
329 |
## handle "no need to keep skipping" transition |
330 |
if ($SKIPPING and @$SEND_QUEUE < 1) { |
331 |
$_[KERNEL]->post($IRC_ALIAS => privmsg => $CHANNEL => |
332 |
"[discarded $SKIPPING messages]"); |
333 |
$SKIPPING = 0; |
334 |
} |
335 |
|
336 |
## handle potential message display |
337 |
if ($time) { |
338 |
if ($SKIPPING or @$SEND_QUEUE > 3) { # 3 msgs per 10 seconds |
339 |
$SKIPPING++; |
340 |
} else { |
341 |
my @time = localtime $time; |
342 |
$_[KERNEL]->post($IRC_ALIAS => privmsg => $CHANNEL => |
343 |
sprintf "%02d:%02d:%02d: %s: %s", |
344 |
($time[2] + 11) % 12 + 1, $time[1], $time[0], |
345 |
$file, $line); |
346 |
} |
347 |
} |
348 |
|
349 |
## handle re-probe/flush if skipping |
350 |
if ($SKIPPING) { |
351 |
$_[KERNEL]->delay($_[STATE] => 0.5); # $time will be undef |
352 |
} |
353 |
|
354 |
}, |
355 |
my_heartbeat => sub { |
356 |
$_[KERNEL]->yield(my_tailed => time, "heartbeat", "beep"); |
357 |
$_[KERNEL]->delay($_[STATE] => 10); |
358 |
} |
359 |
}, |
360 |
); |
361 |
|
362 |
POE::Kernel->run; |