/[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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 11 - (show annotations)
Thu Mar 2 00:52:22 2006 UTC (18 years, 1 month ago) by dpavlin
Original Path: trunk/irc-logger.pl
File MIME type: text/plain
File size: 8336 byte(s)
implemeted get_from_log, and using that grep
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 ## 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 $msg .= $row->{nick} . ': ' if ($last_row->{nick} ne $row->{nick});
138
139 $msg .= $row->{message};
140
141 push @msgs, $msg;
142
143 $last_row = $row;
144 }
145
146 return @msgs;
147 }
148
149
150 my $SKIPPING = 0; # if skipping, how many we've done
151 my $SEND_QUEUE; # cache
152
153 POE::Component::IRC->new($IRC_ALIAS);
154
155 POE::Session->create
156 (inline_states =>
157 {_start => sub {
158 $_[KERNEL]->post($IRC_ALIAS => register => 'all');
159 $_[KERNEL]->post($IRC_ALIAS => connect => $CONNECT);
160 },
161 irc_255 => sub { # server is done blabbing
162 $_[KERNEL]->post($IRC_ALIAS => join => $CHANNEL);
163 $_[KERNEL]->post($IRC_ALIAS => join => '#logger');
164 $_[KERNEL]->yield("heartbeat"); # start heartbeat
165 # $_[KERNEL]->yield("my_add", $_) for keys %FOLLOWS;
166 $_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "IDENTIFY $NICK" );
167 },
168 irc_public => sub {
169 my $kernel = $_[KERNEL];
170 my $nick = (split /!/, $_[ARG0])[0];
171 my $channel = $_[ARG1]->[0];
172 my $msg = $_[ARG2];
173
174 from_to($msg, 'UTF-8', 'ISO-8859-2');
175
176 print "$channel: <$nick> $msg\n";
177 $sth->execute($channel, $nick, $msg);
178 },
179 irc_msg => sub {
180 my $kernel = $_[KERNEL];
181 my $nick = (split /!/, $_[ARG0])[0];
182 my $msg = $_[ARG2];
183 from_to($msg, 'UTF-8', 'ISO-8859-2');
184
185 my $res = "unknown command '$msg', try /msg $NICK help!";
186 my @out;
187
188 print "<< $msg\n";
189
190 if ($msg =~ m/^help/i) {
191
192 $res = "usage: /msg $NICK comand | commands: stat - user/message stat | last - show backtrace | grep foobar - find foobar";
193
194 } elsif ($msg =~ m/^msg\s+(\S+)\s+(.*)$/i) {
195
196 print ">> /msg $1 $2\n";
197 $_[KERNEL]->post( $IRC_ALIAS => privmsg => $1, $2 );
198 $res = '';
199
200 } elsif ($msg =~ m/^stat.*?\s*(\d*)/i) {
201
202 my $nr = $1 || 10;
203
204 my $sth = $dbh->prepare(qq{
205 select nick,count(*) from log group by nick order by count desc limit $nr
206 });
207 $sth->execute();
208 $res = "Top $nr users: ";
209 my @users;
210 while (my $row = $sth->fetchrow_hashref) {
211 push @users,$row->{nick} . ': ' . $row->{count};
212 }
213 $res .= join(" | ", @users);
214 } elsif ($msg =~ m/^last.*?\s*(\d*)/i) {
215
216 foreach my $res (get_from_log( limit => $1 )) {
217 print "last: $res\n";
218 from_to($res, 'ISO-8859-2', 'UTF-8');
219 $_[KERNEL]->post( $IRC_ALIAS => privmsg => $nick, $res );
220 }
221
222 $res = '';
223
224 } elsif ($msg =~ m/^(search|grep)\s+(.*)$/i) {
225
226 my $what = $2;
227
228 foreach my $res (get_from_log( limit => 20, search => "%${what}%" )) {
229 print "search [$what]: $res\n";
230 from_to($res, 'ISO-8859-2', 'UTF-8');
231 $_[KERNEL]->post( $IRC_ALIAS => privmsg => $nick, $res );
232 }
233
234 $res = '';
235
236 }
237
238 if ($res) {
239 print ">> [$nick] $res\n";
240 from_to($res, 'ISO-8859-2', 'UTF-8');
241 $_[KERNEL]->post( $IRC_ALIAS => privmsg => $nick, $res );
242 }
243
244 },
245 irc_477 => sub {
246 print "# irc_477: ",$_[ARG1], "\n";
247 $_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "register $NICK" );
248 },
249 irc_505 => sub {
250 print "# irc_505: ",$_[ARG1], "\n";
251 $_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "register $NICK" );
252 # $_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "set hide email on" );
253 # $_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "set email dpavlin\@rot13.org" );
254 },
255 irc_registered => sub {
256 warn "## indetify $NICK\n";
257 $_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "IDENTIFY $NICK" );
258 },
259 # irc_433 => sub {
260 # print "# irc_433: ",$_[ARG1], "\n";
261 # warn "## indetify $NICK\n";
262 # $_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "IDENTIFY $NICK" );
263 # },
264 irc_372 => sub {
265 print "MOTD: ", $_[ARG1], "\n";
266 },
267 irc_snotice => sub {
268 print "(server notice): ", $_[ARG0], "\n";
269 },
270 (map
271 {
272 ;"irc_$_" => sub { }}
273 qw(
274 )),
275 # join
276 # ctcp_version
277 # connected snotice ctcp_action ping notice mode part quit
278 # 001 002 003 004 005
279 # 250 251 252 253 254 265 266
280 # 332 333 353 366 372 375 376
281 # 477
282 _child => sub {},
283 _default => sub {
284 printf "%s: session %s caught an unhandled %s event.\n",
285 scalar localtime(), $_[SESSION]->ID, $_[ARG0];
286 print "The $_[ARG0] event was given these parameters: ",
287 join(" ", map({"ARRAY" eq ref $_ ? "[@$_]" : "$_"} @{$_[ARG1]})), "\n";
288 0; # false for signals
289 },
290 my_add => sub {
291 my $trailing = $_[ARG0];
292 my $session = $_[SESSION];
293 POE::Session->create
294 (inline_states =>
295 {_start => sub {
296 $_[HEAP]->{wheel} =
297 POE::Wheel::FollowTail->new
298 (
299 Filename => $FOLLOWS{$trailing},
300 InputEvent => 'got_line',
301 );
302 },
303 got_line => sub {
304 $_[KERNEL]->post($session => my_tailed =>
305 time, $trailing, $_[ARG0]);
306 },
307 },
308 );
309
310 },
311 my_tailed => sub {
312 my ($time, $file, $line) = @_[ARG0..ARG2];
313 ## $time will be undef on a probe, or a time value if a real line
314
315 ## PoCo::IRC has throttling built in, but no external visibility
316 ## so this is reaching "under the hood"
317 $SEND_QUEUE ||=
318 $_[KERNEL]->alias_resolve($IRC_ALIAS)->get_heap->{send_queue};
319
320 ## handle "no need to keep skipping" transition
321 if ($SKIPPING and @$SEND_QUEUE < 1) {
322 $_[KERNEL]->post($IRC_ALIAS => privmsg => $CHANNEL =>
323 "[discarded $SKIPPING messages]");
324 $SKIPPING = 0;
325 }
326
327 ## handle potential message display
328 if ($time) {
329 if ($SKIPPING or @$SEND_QUEUE > 3) { # 3 msgs per 10 seconds
330 $SKIPPING++;
331 } else {
332 my @time = localtime $time;
333 $_[KERNEL]->post($IRC_ALIAS => privmsg => $CHANNEL =>
334 sprintf "%02d:%02d:%02d: %s: %s",
335 ($time[2] + 11) % 12 + 1, $time[1], $time[0],
336 $file, $line);
337 }
338 }
339
340 ## handle re-probe/flush if skipping
341 if ($SKIPPING) {
342 $_[KERNEL]->delay($_[STATE] => 0.5); # $time will be undef
343 }
344
345 },
346 my_heartbeat => sub {
347 $_[KERNEL]->yield(my_tailed => time, "heartbeat", "beep");
348 $_[KERNEL]->delay($_[STATE] => 10);
349 }
350 },
351 );
352
353 POE::Kernel->run;

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26