/[irc-logger]/trunk/bin/irc-logger.pl
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Annotation of /trunk/bin/irc-logger.pl

Parent Directory Parent Directory | Revision Log Revision Log


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

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26