/[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 10 - (hide annotations)
Thu Mar 2 00:19:12 2006 UTC (14 years, 10 months ago) by dpavlin
Original Path: trunk/irc-logger.pl
File MIME type: text/plain
File size: 7638 byte(s)
removed all void handling, implemented some of my (notice, server notice), msg to pretend
that you are irc-logger
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 10 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 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    
76 dpavlin 4 my $SKIPPING = 0; # if skipping, how many we've done
77     my $SEND_QUEUE; # cache
78    
79     POE::Component::IRC->new($IRC_ALIAS);
80    
81     POE::Session->create
82     (inline_states =>
83     {_start => sub {
84 dpavlin 7 $_[KERNEL]->post($IRC_ALIAS => register => 'all');
85     $_[KERNEL]->post($IRC_ALIAS => connect => $CONNECT);
86 dpavlin 4 },
87 dpavlin 9 irc_255 => sub { # server is done blabbing
88 dpavlin 7 $_[KERNEL]->post($IRC_ALIAS => join => $CHANNEL);
89     $_[KERNEL]->post($IRC_ALIAS => join => '#logger');
90     $_[KERNEL]->yield("heartbeat"); # start heartbeat
91     # $_[KERNEL]->yield("my_add", $_) for keys %FOLLOWS;
92 dpavlin 4 },
93     irc_public => sub {
94 dpavlin 7 my $kernel = $_[KERNEL];
95     my $nick = (split /!/, $_[ARG0])[0];
96     my $channel = $_[ARG1]->[0];
97     my $msg = $_[ARG2];
98 dpavlin 4
99 dpavlin 7 from_to($msg, 'UTF-8', 'ISO-8859-2');
100 dpavlin 6
101 dpavlin 7 print "$channel: <$nick> $msg\n";
102     $sth->execute($channel, $nick, $msg);
103 dpavlin 4 },
104 dpavlin 7 irc_msg => sub {
105     my $kernel = $_[KERNEL];
106     my $nick = (split /!/, $_[ARG0])[0];
107     my $msg = $_[ARG2];
108     from_to($msg, 'UTF-8', 'ISO-8859-2');
109    
110 dpavlin 8 my $res = "unknown command '$msg', try /msg $NICK help!";
111 dpavlin 7
112     print "<< $msg\n";
113    
114 dpavlin 8 if ($msg =~ m/^help/i) {
115 dpavlin 7
116 dpavlin 8 $res = "usage: /msg $NICK stat - shows user statistics | /msg $NICK last - show backtrace of conversations";
117    
118 dpavlin 10 } elsif ($msg =~ m/^msg\s+(\S+)\s+(.*)$/i) {
119    
120     print ">> /msg $1 $2\n";
121     $_[KERNEL]->post( $IRC_ALIAS => privmsg => $1, $2 );
122     $res = '';
123    
124 dpavlin 8 } elsif ($msg =~ m/^stat.*?\s*(\d*)/i) {
125    
126 dpavlin 7 my $nr = $1 || 10;
127    
128     my $sth = $dbh->prepare(qq{
129     select nick,count(*) from log group by nick order by count desc limit $nr
130     });
131     $sth->execute();
132     $res = "Top $nr users: ";
133 dpavlin 8 my @users;
134 dpavlin 7 while (my $row = $sth->fetchrow_hashref) {
135 dpavlin 8 push @users,$row->{nick} . ': ' . $row->{count};
136 dpavlin 7 }
137 dpavlin 8 $res .= join(" | ", @users);
138     } elsif ($msg =~ m/^last.*?\s*(\d*)/i) {
139    
140     my $nr = $1 || 10;
141    
142     my $sth = $dbh->prepare(qq{
143     select
144     time::date as date,
145     time::time as time,
146     channel,
147     nick,
148     message
149 dpavlin 9 from log order by log.time desc limit $nr
150 dpavlin 8 });
151     $sth->execute();
152     $res = "Last $nr messages: ";
153     my $last_row = {
154     date => '',
155     time => '',
156     channel => '',
157     nick => '',
158     };
159    
160     my @rows;
161    
162     while (my $row = $sth->fetchrow_hashref) {
163     unshift @rows, $row;
164     }
165    
166     my @msgs;
167    
168     foreach my $row (@rows) {
169    
170     $row->{time} =~ s#\.\d+##;
171    
172     my $t;
173     $t = $row->{date} . ' ' if ($last_row->{date} ne $row->{date});
174     $t .= $row->{time};
175    
176     my $msg = '';
177    
178     $msg .= "($t";
179     $msg .= ' ' . $row->{channel} if ($last_row->{channel} ne $row->{channel});
180     $msg .= ") ";
181    
182     $msg .= $row->{nick} . ': ' if ($last_row->{nick} ne $row->{nick});
183    
184     $msg .= $row->{message};
185    
186     push @msgs, $msg;
187    
188     $last_row = $row;
189     }
190    
191     foreach my $res (@msgs) {
192     print "last: $res\n";
193     from_to($res, 'ISO-8859-2', 'UTF-8');
194     $_[KERNEL]->post( $IRC_ALIAS => privmsg => $nick, $res );
195     }
196    
197     $res = '';
198 dpavlin 7 }
199    
200 dpavlin 8 if ($res) {
201     print ">> [$nick] $res\n";
202     from_to($res, 'ISO-8859-2', 'UTF-8');
203     $_[KERNEL]->post( $IRC_ALIAS => privmsg => $nick, $res );
204     }
205 dpavlin 7
206     },
207 dpavlin 10 irc_477 => sub {
208     print "# irc_477: ",$_[ARG1], "\n";
209     $_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "register $NICK" );
210     },
211 dpavlin 7 irc_505 => sub {
212 dpavlin 10 print "# irc_505: ",$_[ARG1], "\n";
213 dpavlin 7 $_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "register $NICK" );
214 dpavlin 10 # $_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "set hide email on" );
215     # $_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "set email dpavlin\@rot13.org" );
216 dpavlin 8 },
217     irc_registered => sub {
218 dpavlin 10 warn "## indetify $NICK\n";
219 dpavlin 7 $_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "IDENTIFY $NICK" );
220 dpavlin 10 },
221     irc_433 => sub {
222     print "# irc_433: ",$_[ARG1], "\n";
223 dpavlin 7 warn "## indetify $NICK\n";
224 dpavlin 10 $_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "IDENTIFY $NICK" );
225 dpavlin 7 },
226 dpavlin 10 irc_372 => sub {
227     print "MOTD: ", $_[ARG1], "\n";
228     },
229     irc_snotice => sub {
230     print "(server notice): ", $_[ARG0], "\n";
231     },
232 dpavlin 4 (map
233     {
234     ;"irc_$_" => sub { }}
235 dpavlin 10 qw(
236 dpavlin 5 )),
237 dpavlin 10 # join
238     # ctcp_version
239     # connected snotice ctcp_action ping notice mode part quit
240     # 001 002 003 004 005
241     # 250 251 252 253 254 265 266
242     # 332 333 353 366 372 375 376
243     # 477
244 dpavlin 4 _child => sub {},
245     _default => sub {
246     printf "%s: session %s caught an unhandled %s event.\n",
247     scalar localtime(), $_[SESSION]->ID, $_[ARG0];
248     print "The $_[ARG0] event was given these parameters: ",
249     join(" ", map({"ARRAY" eq ref $_ ? "[@$_]" : "$_"} @{$_[ARG1]})), "\n";
250     0; # false for signals
251     },
252     my_add => sub {
253     my $trailing = $_[ARG0];
254     my $session = $_[SESSION];
255     POE::Session->create
256     (inline_states =>
257     {_start => sub {
258     $_[HEAP]->{wheel} =
259     POE::Wheel::FollowTail->new
260     (
261     Filename => $FOLLOWS{$trailing},
262     InputEvent => 'got_line',
263     );
264     },
265     got_line => sub {
266     $_[KERNEL]->post($session => my_tailed =>
267     time, $trailing, $_[ARG0]);
268     },
269     },
270     );
271    
272     },
273     my_tailed => sub {
274     my ($time, $file, $line) = @_[ARG0..ARG2];
275     ## $time will be undef on a probe, or a time value if a real line
276    
277     ## PoCo::IRC has throttling built in, but no external visibility
278     ## so this is reaching "under the hood"
279     $SEND_QUEUE ||=
280     $_[KERNEL]->alias_resolve($IRC_ALIAS)->get_heap->{send_queue};
281    
282     ## handle "no need to keep skipping" transition
283     if ($SKIPPING and @$SEND_QUEUE < 1) {
284     $_[KERNEL]->post($IRC_ALIAS => privmsg => $CHANNEL =>
285     "[discarded $SKIPPING messages]");
286     $SKIPPING = 0;
287     }
288    
289     ## handle potential message display
290     if ($time) {
291     if ($SKIPPING or @$SEND_QUEUE > 3) { # 3 msgs per 10 seconds
292     $SKIPPING++;
293     } else {
294     my @time = localtime $time;
295     $_[KERNEL]->post($IRC_ALIAS => privmsg => $CHANNEL =>
296     sprintf "%02d:%02d:%02d: %s: %s",
297     ($time[2] + 11) % 12 + 1, $time[1], $time[0],
298     $file, $line);
299     }
300     }
301    
302     ## handle re-probe/flush if skipping
303     if ($SKIPPING) {
304     $_[KERNEL]->delay($_[STATE] => 0.5); # $time will be undef
305     }
306    
307     },
308     my_heartbeat => sub {
309     $_[KERNEL]->yield(my_tailed => time, "heartbeat", "beep");
310     $_[KERNEL]->delay($_[STATE] => 10);
311     }
312     },
313     );
314    
315     POE::Kernel->run;

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26