18 |
|
|
19 |
## CONFIG |
## CONFIG |
20 |
|
|
21 |
my $NICK = 'irc-logger-dev'; |
my $NICK = 'irc-logger'; |
22 |
my $CONNECT = |
my $CONNECT = |
23 |
{Server => 'irc.freenode.net', |
{Server => 'irc.freenode.net', |
24 |
Nick => $NICK, |
Nick => $NICK, |
72 |
values (?,?,?) |
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 |
my $SKIPPING = 0; # if skipping, how many we've done |
151 |
my $SEND_QUEUE; # cache |
my $SEND_QUEUE; # cache |
163 |
$_[KERNEL]->post($IRC_ALIAS => join => '#logger'); |
$_[KERNEL]->post($IRC_ALIAS => join => '#logger'); |
164 |
$_[KERNEL]->yield("heartbeat"); # start heartbeat |
$_[KERNEL]->yield("heartbeat"); # start heartbeat |
165 |
# $_[KERNEL]->yield("my_add", $_) for keys %FOLLOWS; |
# $_[KERNEL]->yield("my_add", $_) for keys %FOLLOWS; |
166 |
|
$_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "IDENTIFY $NICK" ); |
167 |
}, |
}, |
168 |
irc_public => sub { |
irc_public => sub { |
169 |
my $kernel = $_[KERNEL]; |
my $kernel = $_[KERNEL]; |
183 |
from_to($msg, 'UTF-8', 'ISO-8859-2'); |
from_to($msg, 'UTF-8', 'ISO-8859-2'); |
184 |
|
|
185 |
my $res = "unknown command '$msg', try /msg $NICK help!"; |
my $res = "unknown command '$msg', try /msg $NICK help!"; |
186 |
|
my @out; |
187 |
|
|
188 |
print "<< $msg\n"; |
print "<< $msg\n"; |
189 |
|
|
190 |
if ($msg =~ m/^help/i) { |
if ($msg =~ m/^help/i) { |
191 |
|
|
192 |
$res = "usage: /msg $NICK stat - shows user statistics | /msg $NICK last - show backtrace of conversations"; |
$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) { |
} elsif ($msg =~ m/^stat.*?\s*(\d*)/i) { |
201 |
|
|
213 |
$res .= join(" | ", @users); |
$res .= join(" | ", @users); |
214 |
} elsif ($msg =~ m/^last.*?\s*(\d*)/i) { |
} elsif ($msg =~ m/^last.*?\s*(\d*)/i) { |
215 |
|
|
216 |
my $nr = $1 || 10; |
foreach my $res (get_from_log( limit => $1 )) { |
217 |
|
print "last: $res\n"; |
218 |
my $sth = $dbh->prepare(qq{ |
from_to($res, 'ISO-8859-2', 'UTF-8'); |
219 |
select |
$_[KERNEL]->post( $IRC_ALIAS => privmsg => $nick, $res ); |
|
time::date as date, |
|
|
time::time as time, |
|
|
channel, |
|
|
nick, |
|
|
message |
|
|
from log order by log.time desc limit $nr |
|
|
}); |
|
|
$sth->execute(); |
|
|
$res = "Last $nr messages: "; |
|
|
my $last_row = { |
|
|
date => '', |
|
|
time => '', |
|
|
channel => '', |
|
|
nick => '', |
|
|
}; |
|
|
|
|
|
my @rows; |
|
|
|
|
|
while (my $row = $sth->fetchrow_hashref) { |
|
|
unshift @rows, $row; |
|
220 |
} |
} |
221 |
|
|
222 |
my @msgs; |
$res = ''; |
|
|
|
|
foreach my $row (@rows) { |
|
|
|
|
|
$row->{time} =~ s#\.\d+##; |
|
|
|
|
|
my $t; |
|
|
$t = $row->{date} . ' ' if ($last_row->{date} ne $row->{date}); |
|
|
$t .= $row->{time}; |
|
|
|
|
|
my $msg = ''; |
|
|
|
|
|
$msg .= "($t"; |
|
|
$msg .= ' ' . $row->{channel} if ($last_row->{channel} ne $row->{channel}); |
|
|
$msg .= ") "; |
|
|
|
|
|
$msg .= $row->{nick} . ': ' if ($last_row->{nick} ne $row->{nick}); |
|
|
|
|
|
$msg .= $row->{message}; |
|
223 |
|
|
224 |
push @msgs, $msg; |
} elsif ($msg =~ m/^(search|grep)\s+(.*)$/i) { |
225 |
|
|
226 |
$last_row = $row; |
my $what = $2; |
|
} |
|
227 |
|
|
228 |
foreach my $res (@msgs) { |
foreach my $res (get_from_log( limit => 20, search => "%${what}%" )) { |
229 |
print "last: $res\n"; |
print "search [$what]: $res\n"; |
230 |
from_to($res, 'ISO-8859-2', 'UTF-8'); |
from_to($res, 'ISO-8859-2', 'UTF-8'); |
231 |
$_[KERNEL]->post( $IRC_ALIAS => privmsg => $nick, $res ); |
$_[KERNEL]->post( $IRC_ALIAS => privmsg => $nick, $res ); |
232 |
} |
} |
233 |
|
|
234 |
$res = ''; |
$res = ''; |
235 |
|
|
236 |
} |
} |
237 |
|
|
238 |
if ($res) { |
if ($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 { |
irc_505 => sub { |
250 |
print "# irc_505: ",$_[ARG1], "\n"; |
print "# irc_505: ",$_[ARG1], "\n"; |
251 |
$_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "register $NICK" ); |
$_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "register $NICK" ); |
252 |
warn "## register $NICK\n"; |
# $_[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 { |
irc_registered => sub { |
|
$_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "IDENTIFY $NICK" ); |
|
256 |
warn "## indetify $NICK\n"; |
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 |
(map |
271 |
{ |
{ |
272 |
;"irc_$_" => sub { }} |
;"irc_$_" => sub { }} |
273 |
qw(join |
qw( |
|
ctcp_version |
|
|
connected snotice ctcp_action ping notice mode part quit |
|
|
001 002 003 004 005 |
|
|
250 251 252 253 254 265 266 |
|
|
332 333 353 366 372 375 376 |
|
|
477 |
|
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 {}, |
_child => sub {}, |
283 |
_default => sub { |
_default => sub { |
284 |
printf "%s: session %s caught an unhandled %s event.\n", |
printf "%s: session %s caught an unhandled %s event.\n", |