/[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 12 - (hide annotations)
Sun Mar 12 13:33:20 2006 UTC (14 years, 10 months ago) by dpavlin
Original Path: trunk/irc-logger.pl
File MIME type: text/plain
File size: 8457 byte(s)
compress last output
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 12 my $NICK = 'irc-logger-dev';
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 4 ## END CONFIG
39    
40 dpavlin 5
41    
42     use POE qw(Component::IRC Wheel::FollowTail);
43     use DBI;
44 dpavlin 6 use Encode qw/from_to/;
45 dpavlin 5
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 dpavlin 11 =head2 get_from_log
76 dpavlin 5
77 dpavlin 11 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 dpavlin 12 $msg .= "{$t";
134 dpavlin 11 $msg .= ' ' . $row->{channel} if ($last_row->{channel} ne $row->{channel});
135 dpavlin 12 $msg .= "} ";
136 dpavlin 11
137 dpavlin 12 my $append = 1;
138 dpavlin 11
139 dpavlin 12 if ($last_row->{nick} ne $row->{nick}) {
140     $msg .= $row->{nick} . ': ';
141     $append = 0;
142     }
143    
144 dpavlin 11 $msg .= $row->{message};
145    
146 dpavlin 12 if ($append && @msgs) {
147     $msgs[$#msgs] .= " " . $msg;
148     } else {
149     push @msgs, $msg;
150     }
151 dpavlin 11
152     $last_row = $row;
153     }
154    
155     return @msgs;
156     }
157    
158    
159 dpavlin 4 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 dpavlin 7 $_[KERNEL]->post($IRC_ALIAS => register => 'all');
168     $_[KERNEL]->post($IRC_ALIAS => connect => $CONNECT);
169 dpavlin 4 },
170 dpavlin 9 irc_255 => sub { # server is done blabbing
171 dpavlin 7 $_[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 dpavlin 11 $_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "IDENTIFY $NICK" );
176 dpavlin 4 },
177     irc_public => sub {
178 dpavlin 7 my $kernel = $_[KERNEL];
179     my $nick = (split /!/, $_[ARG0])[0];
180     my $channel = $_[ARG1]->[0];
181     my $msg = $_[ARG2];
182 dpavlin 4
183 dpavlin 7 from_to($msg, 'UTF-8', 'ISO-8859-2');
184 dpavlin 6
185 dpavlin 7 print "$channel: <$nick> $msg\n";
186     $sth->execute($channel, $nick, $msg);
187 dpavlin 4 },
188 dpavlin 7 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 dpavlin 8 my $res = "unknown command '$msg', try /msg $NICK help!";
195 dpavlin 11 my @out;
196 dpavlin 7
197     print "<< $msg\n";
198    
199 dpavlin 8 if ($msg =~ m/^help/i) {
200 dpavlin 7
201 dpavlin 11 $res = "usage: /msg $NICK comand | commands: stat - user/message stat | last - show backtrace | grep foobar - find foobar";
202 dpavlin 8
203 dpavlin 10 } 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 dpavlin 8 } elsif ($msg =~ m/^stat.*?\s*(\d*)/i) {
210    
211 dpavlin 7 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 dpavlin 8 my @users;
219 dpavlin 7 while (my $row = $sth->fetchrow_hashref) {
220 dpavlin 8 push @users,$row->{nick} . ': ' . $row->{count};
221 dpavlin 7 }
222 dpavlin 8 $res .= join(" | ", @users);
223     } elsif ($msg =~ m/^last.*?\s*(\d*)/i) {
224    
225 dpavlin 11 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 dpavlin 8 }
230    
231 dpavlin 11 $res = '';
232 dpavlin 8
233 dpavlin 11 } elsif ($msg =~ m/^(search|grep)\s+(.*)$/i) {
234 dpavlin 8
235 dpavlin 11 my $what = $2;
236 dpavlin 8
237 dpavlin 11 foreach my $res (get_from_log( limit => 20, search => "%${what}%" )) {
238     print "search [$what]: $res\n";
239 dpavlin 8 from_to($res, 'ISO-8859-2', 'UTF-8');
240     $_[KERNEL]->post( $IRC_ALIAS => privmsg => $nick, $res );
241     }
242    
243     $res = '';
244 dpavlin 11
245 dpavlin 7 }
246    
247 dpavlin 8 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 dpavlin 7
253     },
254 dpavlin 10 irc_477 => sub {
255     print "# irc_477: ",$_[ARG1], "\n";
256     $_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "register $NICK" );
257     },
258 dpavlin 7 irc_505 => sub {
259 dpavlin 10 print "# irc_505: ",$_[ARG1], "\n";
260 dpavlin 7 $_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "register $NICK" );
261 dpavlin 10 # $_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "set hide email on" );
262     # $_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "set email dpavlin\@rot13.org" );
263 dpavlin 8 },
264     irc_registered => sub {
265 dpavlin 10 warn "## indetify $NICK\n";
266 dpavlin 7 $_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "IDENTIFY $NICK" );
267 dpavlin 10 },
268 dpavlin 11 # 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 dpavlin 10 irc_372 => sub {
274     print "MOTD: ", $_[ARG1], "\n";
275     },
276     irc_snotice => sub {
277     print "(server notice): ", $_[ARG0], "\n";
278     },
279 dpavlin 4 (map
280     {
281     ;"irc_$_" => sub { }}
282 dpavlin 10 qw(
283 dpavlin 5 )),
284 dpavlin 10 # 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 dpavlin 4 _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;

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26