/[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 5 - (show annotations)
Mon Feb 27 12:10:07 2006 UTC (14 years, 10 months ago) by dpavlin
Original Path: trunk/irc-logger.pl
File MIME type: text/plain
File size: 4567 byte(s)
added storage into database

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 => 'logger: ask dpavlin@rot13.org'
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
45
46 my $dbh = DBI->connect($DSN,"","", { RaiseError => 1, AutoCommit => 1 }) || die $DBI::errstr;
47
48 =for SQL schema
49
50 $dbh->do(qq{
51 create table log (
52 id serial,
53 time timestamp default now(),
54 channel text not null,
55 nick text not null,
56 message text not null,
57 primary key(id)
58 );
59
60 create index log_time on log(time);
61 create index log_channel on log(channel);
62 create index log_nick on log(nick);
63
64 });
65
66 =cut
67
68 my $sth = $dbh->prepare(qq{
69 insert into log
70 (channel, nick, message)
71 values (?,?,?)
72 });
73
74
75 my $SKIPPING = 0; # if skipping, how many we've done
76 my $SEND_QUEUE; # cache
77
78 POE::Component::IRC->new($IRC_ALIAS);
79
80 POE::Session->create
81 (inline_states =>
82 {_start => sub {
83 $_[KERNEL]->post($IRC_ALIAS => register => 'all');
84 $_[KERNEL]->post($IRC_ALIAS => connect => $CONNECT);
85 },
86 irc_255 => sub { # server is done blabbing
87 $_[KERNEL]->post($IRC_ALIAS => join => $CHANNEL);
88 $_[KERNEL]->post($IRC_ALIAS => join => '#logger');
89 $_[KERNEL]->yield("heartbeat"); # start heartbeat
90 # $_[KERNEL]->yield("my_add", $_) for keys %FOLLOWS;
91 },
92 irc_public => sub {
93 my $kernel = $_[KERNEL];
94 my $nick = (split /!/, $_[ARG0])[0];
95 my $channel = $_[ARG1]->[0];
96 my $msg = $_[ARG2];
97
98 print "$channel: <$nick> $msg\n";
99 $sth->execute($channel, $nick, $msg);
100 },
101 (map
102 {
103 ;"irc_$_" => sub { }}
104 qw(join
105 ctcp_version
106 connected snotice ctcp_action ping notice mode part quit
107 001 002 003 004 005
108 250 251 252 253 254 265 266
109 332 333 353 366 372 375 376
110 477
111 )),
112 _child => sub {},
113 _default => sub {
114 printf "%s: session %s caught an unhandled %s event.\n",
115 scalar localtime(), $_[SESSION]->ID, $_[ARG0];
116 print "The $_[ARG0] event was given these parameters: ",
117 join(" ", map({"ARRAY" eq ref $_ ? "[@$_]" : "$_"} @{$_[ARG1]})), "\n";
118 0; # false for signals
119 },
120 my_add => sub {
121 my $trailing = $_[ARG0];
122 my $session = $_[SESSION];
123 POE::Session->create
124 (inline_states =>
125 {_start => sub {
126 $_[HEAP]->{wheel} =
127 POE::Wheel::FollowTail->new
128 (
129 Filename => $FOLLOWS{$trailing},
130 InputEvent => 'got_line',
131 );
132 },
133 got_line => sub {
134 $_[KERNEL]->post($session => my_tailed =>
135 time, $trailing, $_[ARG0]);
136 },
137 },
138 );
139
140 },
141 my_tailed => sub {
142 my ($time, $file, $line) = @_[ARG0..ARG2];
143 ## $time will be undef on a probe, or a time value if a real line
144
145 ## PoCo::IRC has throttling built in, but no external visibility
146 ## so this is reaching "under the hood"
147 $SEND_QUEUE ||=
148 $_[KERNEL]->alias_resolve($IRC_ALIAS)->get_heap->{send_queue};
149
150 ## handle "no need to keep skipping" transition
151 if ($SKIPPING and @$SEND_QUEUE < 1) {
152 $_[KERNEL]->post($IRC_ALIAS => privmsg => $CHANNEL =>
153 "[discarded $SKIPPING messages]");
154 $SKIPPING = 0;
155 }
156
157 ## handle potential message display
158 if ($time) {
159 if ($SKIPPING or @$SEND_QUEUE > 3) { # 3 msgs per 10 seconds
160 $SKIPPING++;
161 } else {
162 my @time = localtime $time;
163 $_[KERNEL]->post($IRC_ALIAS => privmsg => $CHANNEL =>
164 sprintf "%02d:%02d:%02d: %s: %s",
165 ($time[2] + 11) % 12 + 1, $time[1], $time[0],
166 $file, $line);
167 }
168 }
169
170 ## handle re-probe/flush if skipping
171 if ($SKIPPING) {
172 $_[KERNEL]->delay($_[STATE] => 0.5); # $time will be undef
173 }
174
175 },
176 my_heartbeat => sub {
177 $_[KERNEL]->yield(my_tailed => time, "heartbeat", "beep");
178 $_[KERNEL]->delay($_[STATE] => 10);
179 }
180 },
181 );
182
183 POE::Kernel->run;

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26