1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
|
#!/usr/bin/env perl
use strict;
use warnings;
use POSIX qw(setuid setgid strftime);
use POE;
use POE::Kernel;
use POE::Component::IRC;
use POE::Component::IRC::Plugin::NickServID;
use IdaliusConfig;
use Plugin qw/load_plugin/;
use IRC::Utils qw(strip_color strip_formatting);
my $ignore_suffix = "_yes_really_even_from_ignored_nicks";
my $config_file = "bot.conf";
my $config = IdaliusConfig::parse_config($config_file);
my %laststrike = ();
my $ping_delay = 300;
my %commands = ();
sub log_info {
# FIXME direct to a log file instead of stdout
my $stamp = strftime("%Y-%m-%d %H:%M:%S %z", localtime);
print "$stamp | @_\n";
}
Plugin::set_load_callback(\&module_loaded_callback);
load_configure_all_plugins();
$| = 1;
# New PoCo-IRC object
my $irc = POE::Component::IRC->spawn(
UseSSL => $config->{_}->{usessl},
SSLCert => $config->{_}->{sslcert},
SSLKey => $config->{_}->{sslkey},
nick => $config->{_}->{nick},
ircname => $config->{_}->{ircname},
port => $config->{_}->{port},
server => $config->{_}->{server},
username => $config->{_}->{username},
) or die "Failed to create new PoCo-IRC: $!";
# Plugins
$config->{_}->{password} and $irc->plugin_add(
'NickServID',
POE::Component::IRC::Plugin::NickServID->new(
Password => $config->{_}->{password}
));
POE::Session->create(
package_states => [
main => [ qw(
_default
_start
irc_001
irc_kick
irc_ctcp_action
irc_public
irc_msg
irc_join
irc_part
irc_invite
irc_nick
irc_disconnected
irc_error
irc_socketerr
irc_delay_set
irc_delay_removed
custom_ping) ],
],
heap => { irc => $irc },
);
drop_priv();
$poe_kernel->run();
################################################################################
# Helpers and module framework
sub load_configure_all_plugins {
eval {
for my $module (@{$config->{_}->{plugins}}) {
Plugin::load_plugin(\&log_info, $config->{_}, $module);
}
1;
} or do {
log_info "Error: failed to load module: $@";
die;
};
}
sub module_loaded_callback {
my ($module) = @_;
$module->configure(
\®ister_command,
\&run_command,
$config->{$module},
$config->{_});
}
sub module_is_enabled {
my $module = $_[0];
return grep {$_ eq $module} @{$config->{_}->{active_plugins}};
}
# Register a command name to a certain sub
sub register_command {
my ($owner, $command, $action) = @_;
$command = lc $command;
log_info "Registering command: $command (from $owner)";
$commands{$owner}{$command} = $action;
}
sub run_command {
my ($command_string, $who, $where, $ided, $no_reenter) = @_;
my @arguments;
my $owner;
my $command_verbatim;
my $command;
OUTER: for my $o (keys %commands) {
next unless module_is_enabled($o);
for my $c (keys %{$commands{$o}}) {
if (($command_verbatim) = $command_string =~ m/^(\Q$c\E( |$))/i) {
$command = lc $c;
$owner = $o;
last OUTER;
}
}
}
return "No such command" unless $command;
my $rest = (split "\Q$command_verbatim", $command_string, 2)[1];
@arguments = split /\s+/, $rest if $rest;
my $action = $commands{$owner}{$command};
return $action->($irc, \&log_info, $who, $where, $ided, $rest,
sub { push @$no_reenter, $owner; },
@arguments);
}
# Send an effect-free client->server message as a form of ping to allegedly
# help POE realise when a connection is down. It otherwise seems to not realise
# a connection has fallen over otherwise.
sub custom_ping {
my ($poek) = $_[KERNEL];
$poek->yield(userhost => $irc->nick_name());
$poek->delay(custom_ping => $ping_delay);
}
sub drop_priv {
setgid($config->{_}->{gid}) or die "Failed to setgid: $!\n";
setuid($config->{_}->{uid}) or die "Failed to setuid: $!\n";
}
# Add a strike against a nick for module flood protection
# This differs from antiflood.pm in that it is used only for when users have
# triggered a response from the bot.
sub strike_add {
my $strike_count = 14;
my $strike_period = 45;
my ($who, $channel) = @_;
my $nick = (split /!/, $who)[0];
my $now = time();
push @{$laststrike{$who}}, $now;
if (@{$laststrike{$who}} >= $strike_count) {
@{$laststrike{$who}} = splice @{$laststrike{$who}}, 1, $strike_count - 1;
my $first = @{$laststrike{$who}}[0];
if ($now - $first <= $strike_period) {
log_info "Ignoring $who because of command flood";
$irc->yield(privmsg => $channel => "$nick: I'm ignoring you now, you've caused me to talk too much");
push @{$config->{_}->{ignore}}, $who;
}
}
}
sub should_ignore {
my ($who) = @_;
for my $mask (@{$config->{_}->{ignore}}) {
my $expr = $mask;
$expr =~ s/([^[:alnum:]\*])/$1/g;
$expr =~ s/\*/.*/g;
if ($who =~ /^$expr$/) {
return 1;
}
}
return;
}
sub reconnect {
my $reconnect_delay = 20;
log_info("Reconnecting in $reconnect_delay seconds");
sleep($reconnect_delay);
$irc->yield(connect => { });
}
################################################################################
# Plugin event handling helpers
sub handle_common {
my ($message_type, $who, $where, $what, $ided) = @_;
my $nick = (split /!/, $who)[0];
my $channel = $where->[0];
my $output;
my @no_reenter = ();
$what =~ s/\s+$//g;
# Firstly, trigger commands
my $stripped_what = strip_color(strip_formatting($what));
my $no_prefix_what = $stripped_what;
my $current_nick = $irc->nick_name();
if (!should_ignore($who) && ($config->{_}->{prefix_nick} && $no_prefix_what =~ s/^\Q$current_nick\E[:,]\s+//g ||
($config->{_}->{prefix} && $no_prefix_what =~ s/^\Q$config->{_}->{prefix}//))) {
$output = run_command($no_prefix_what, $who, $where, $ided, \@no_reenter);
$irc->yield(privmsg => $where => $output) if $output;
strike_add($who, $channel) if $output;
}
# Secondly, trigger non-command handlers
trigger_modules($message_type, $who, $where, \@no_reenter, ($who, $where, $what, $stripped_what));
return;
}
# Trigger applicable non-command-bound handlers in any active modules for
# a given message type, passing them only the given arguments
sub trigger_modules {
my ($message_type, $who, $where, $no_reenter, @arguments) = @_;
my $nick = (split /!/, $who)[0];
for my $handler (handlers_for($message_type, $who, $no_reenter)) {
my @base_args = (\&log_info);
push @base_args, @arguments;
push @base_args, $irc;
my $output = $handler->(@base_args);
if ($output and $where) {
$irc->yield(privmsg => $where => $output);
strike_add($who, $where->[0]);
}
}
return;
}
# Return a list of subs capable of handling the given message type for a nick
# excluding those modules that have asked not to be reentered for this message
sub handlers_for {
my ($message_type, $who, $no_reenter) = @_;
my @handlers = ();
my $nick = (split /!/, $who)[0];
$message_type = "on_$message_type";
for my $module (@{$config->{_}->{active_plugins}}) {
if (!(grep {$_ eq $module} @$no_reenter) and module_is_enabled($module)) {
if (!should_ignore($who) and $module->can($message_type)) {
# Leave message type unchanged
} elsif ($module->can($message_type.$ignore_suffix)) {
$message_type = $message_type.$ignore_suffix;
} else {
# No handler
next;
}
push @handlers, sub { $module->$message_type(@_); };
}
}
return @handlers;
}
###############################################################################
# Begin internal/core handlers
sub _start {
my $heap = $_[HEAP];
my $irc = $heap->{irc};
$irc->yield(register => 'all');
$irc->yield(connect => { });
return;
}
sub irc_001 {
my ($irc, $sender) = @_[KERNEL, SENDER];
my $heap = $sender->get_heap();
log
|