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
|
#!/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 config_file;
use IRC::Utils qw(strip_color strip_formatting);
use Module::Pluggable search_path => "Plugin", instantiate => 'configure';
my $config_file = "bot.conf";
my %config = config_file::parse_config($config_file);
my %laststrike = ();
my $ping_delay = 300;
my %commands = ();
$| = 1;
my $current_nick = $config{nick};
# Hack: coerce into numeric type
+$config{url_on};
+$config{url_len};
my @plugin_list = plugins("dummy", \®ister_command, \%config, \&run_command);
# New PoCo-IRC object
my $irc = POE::Component::IRC->spawn(
UseSSL => $config{usessl},
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_invite
irc_nick
irc_disconnected
irc_error
irc_socketerr
custom_ping) ],
],
heap => { irc => $irc },
);
drop_priv();
$poe_kernel->run();
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";
}
# Register a command name to a certain sub
sub register_command {
my ($command, $action) = @_;
log_info "Registering command: $command";
$commands{$command} = $action;
}
sub run_command {
my ($command_string, $who, $where) = @_;
my @arguments;
my ($command, $rest) = split /\s+/, $command_string, 2;
@arguments = split /\s+/, $rest if $rest;
if ($commands{$command}) {
return ($commands{$command})->(\&log_info, $who, $where, $rest, @arguments);
} else {
return "No such command \"$command\"";
}
}
sub custom_ping {
my ($irc, $heap) = @_[KERNEL, HEAP];
$irc->yield(userhost => $current_nick);
$irc->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 = 15;
my $strike_period = 30;
my ($nick) = @_;
my $now = time();
push @{$laststrike{$nick}}, $now;
if (@{$laststrike{$nick}} >= $strike_count) {
@{$laststrike{$nick}} = splice @{$laststrike{$nick}}, 1, $strike_count - 1;
my $first = @{$laststrike{$nick}}[0];
if ($now - $first <= $strike_period) {
log_info "Ignoring $nick because of command flood";
push @{$config{ignore}}, $nick;
}
}
}
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_info("Connected to server ", $heap->server_name());
$heap->yield(join => $_) for @{$config{channels}};
$irc->delay(custom_ping => $ping_delay);
return;
}
sub irc_nick {
my ($who, $new_nick) = @_[ARG0 .. ARG1];
my $oldnick = (split /!/, $who)[0];
if ($oldnick eq $current_nick) {
$current_nick = $new_nick;
}
return;
}
sub irc_kick {
my ($kicker, $channel, $kickee, $reason) = @_[ARG0 .. ARG3];
if ($kickee eq $current_nick) {
log_info("I was kicked by $kicker ($reason). Rejoining now.");
$irc->yield(join => $channel);
}
return;
}
sub irc_ctcp_action {
irc_public(@_);
}
sub irc_public {
my ($sender, $who, $where, $what) = @_[SENDER, ARG0 .. ARG2];
my $nick = ( split /!/, $who )[0];
my $channel = $where->[0];
my $output;
log_info("[$channel] $who: $what");
# reject ignored nicks first
return if (grep {$_ eq $nick} @{$config{ignore}});
my $stripped_what = strip_color(strip_formatting($what));
if ($stripped_what =~ s/^$config{prefix}//) {
$output = run_command($stripped_what, $who, $where);
$irc->yield(privmsg => $where => $output) if $output;
}
for my $module (@plugin_list) {
$output = "";
if ($module->can("message")) {
$output = $module->message(\&log_info, $irc->nick_name, $who, $where, $what, $stripped_what, $irc);
}
strike_add $nick if $output;
$irc->yield(privmsg => $where => $output) if $output;
}
return;
}
sub irc_msg {
my ($who, $to, $what, $ided) = @_[ARG0 .. ARG3];
my $nick = (split /!/, $who)[0];
my $is_admin = grep {$_ eq $who} @{$config{admins}};
# reject ignored nicks who aren't also admins (prevent lockout)
return if (grep {$_ eq $nick} @{$config{ignore}} and not $is_admin);
if ($config{must_id} && $ided != 1) {
$irc->yield(privmsg => $nick => "You must identify with services");
return;
}
return unless $is_admin;
# FIXME this needs tidying. Some of this can be factored out, surely.
if ($what =~ /^nick\s/) {
my ($newnick) = $what =~ /^nick\s+(\S+)$/;
if ($newnick) {
$irc->yield(nick => $newnick);
$irc->yield(privmsg => $nick => "Requested.");
} else {
$irc->yield(privmsg => $nick => "Syntax: nick <nick>");
}
}
if ($what =~ /^ignore\s/) {
my ($target) = $what =~ /^ignore\s+(\S+)$/;
if ($target) {
push @{$config{ignore}}, $target;
$irc->yield(privmsg => $nick => "Ignoring $target.");
} else {
$irc->yield(privmsg => $nick => "Syntax: ignore <nick>");
}
}
if ($what =~ /^don't ignore\s/) {
my ($target) = $what =~ /^don't ignore\s+(\S+)$/;
if ($target) {
if (grep { $_ eq $target} @{$config{ignore}}) {
@{$config{ignore}} = grep { $_ ne $target } @{$config{ignore}};
$irc->yield(privmsg => $nick => "No longer ignoring $target.");
} else {
$irc->yield(privmsg => $nick => "I wasn't ignoring $target anyway.");
}
} else {
$irc->yield(privmsg => $nick => "Syntax: don't ignore <nick>");
}
}
if ($what =~ /^part\s/) {
my $message;
if ($what =~ /^part(\s+(\S+))+$/m) {
$what =~ s/^part\s+//;
my ($chan_str, $reason) = split /\s+(?!#)/, $what, 2;
my @channels = split /\s+/, $chan_str;
$reason = "Commanded by $nick" unless $reason;
$irc->yield(part => @channels => $reason);
$irc->yield(privmsg => $nick => "Requested.");
} else {
$irc->yield(privmsg => $nick =>
"Syntax: part <channel1> [channel2 ...] [partmsg]");
}
}
if ($what =~ /^join\s/) {
if ($what =~ /^join(\s+(\S+))+$/) {
$what =~ s/^join\s+//;
my @channels = split /\s+/, $what;
$irc->yield(join => $_) for @channels;
$irc->yield(privmsg => $nick => "Requested.");
} else {
$irc->yield(privmsg => $nick =>
"Syntax: join <channel1> [channel2 ...]");
}
}
if ($what =~ /^say\s/) {
my ($channel, $message) = $what =~ /^say\s+(\S+)\s(.*)$/;
if ($channel and $message) {
$irc->yield(privmsg => $channel => $message);
|