-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathxylbot
More file actions
executable file
·664 lines (550 loc) · 13.2 KB
/
xylbot
File metadata and controls
executable file
·664 lines (550 loc) · 13.2 KB
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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
#!/usr/bin/perl
use lib "lib";
use warnings;
no warnings 'redefine';
use strict;
use Net::IRC;
# Configuration
our $loaded;
our (%module_status, %module_loaded, @module_files);
our (%commands, %commands_public, %commands_private, %command_help);
our (%handlers);
our ($server, $channel, $nick, $owner, $port, $password, $linedelay, $require_name, $logtofile, $pid_file);
our ($cur_channel, $cur_connection, $cur_fromnick, $cur_forum);
our (%joined_channels, %channel_users);
our (@users);
our $quitting;
# This stuff is for the logfile date (not sure I need it all, but I do need some of it)
our (@months, @nummonths, @weekDays, $logdate, $second, $minute, $hour, $dayOfMonth, $month, $year, $yearOffset, $dayOfWeek, $dayOfYear, $daylightSavings);
@months = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
@nummonths = qw(01 02 03 04 05 06 07 08 09 10 11 12);
@weekDays = qw(Sun Mon Tue Wed Thu Fri Sat Sun);
($second, $minute, $hour, $dayOfMonth, $month, $yearOffset, $dayOfWeek, $dayOfYear, $daylightSavings) = localtime();
$year = 1900 + $yearOffset;
sub bot_log {
$logdate = "$nummonths[$month]-$year";
if ($logtofile)
{
open FILE, ">>", "logs/$pid_file-$logdate.log";
print FILE scalar(localtime), "\t", @_;
close FILE;
}
else
{
print scalar(localtime), "\t", @_;
}
}
if (!defined $loaded || !$loaded)
{
$pid_file= "xylbot";
$server = "irc.globalgamers.net";
$channel = "#xylbot";
$nick = "DazBot";
$owner = "Daz";
$password = "";
$port = 6668;
$linedelay = 1;
for my $arg (@ARGV)
{
if ($arg =~ /^([^=]+)=([^=]+)$/)
{
${$::{$1}} = $2;
}
}
$loaded = 1;
bot_log "$server $channel $nick $owner\n";
}
else
{
bot_log "Reloaded.\n";
return;
}
# Save PID
if ($pid_file) {
open FILE, ">", "pid.$pid_file";
print FILE $$;
close FILE;
}
# Code
autoflush STDOUT;
our $irc = new Net::IRC;
our $connection;
sub create_connection {
$connection = $irc->newconn(
Nick => $nick,
Server => $server,
Port => $port,
Password => $password,
Pacing => $linedelay,
Username => 'dazbot',
Ircname => 'dazbot');
add_commands();
add_handlers();
return $connection;
}
sub on_connect {
my $self = shift;
bot_log "Joining $channel...\n";
$self->join($channel);
#$self->privmsg($channel, "Hello! I'm ${owner}'s useless bot.");
}
sub do_command {
my ($self, $command, $forum, $from, $to, $args) = @_;
# Save current channel and connection
$cur_channel = ($forum eq 'public' ? $to : $channel);
$cur_connection = $self;
$cur_fromnick = (split /!/, $from)[0];
$cur_forum = $forum;
$command = lc $command;
my $userlevel = 0;
foreach my $user (@users)
{
my ($mask, $masklevel) = @$user;
$mask = quotemeta($mask);
$mask =~ s/\\\*/.*/g;
if ($from =~ /^$mask$/)
{
$userlevel = $masklevel;
last;
}
}
eval {
if ($forum eq 'public' && exists $commands_public{$command})
{
foreach my $item (@{$commands_public{$command}})
{
my ($handler, $level) = @$item;
next unless $userlevel >= $level;
my $result = &{$handler}($self, $command, $forum, $from, $to, $args, $userlevel);
return $result if $result;
}
}
if ($forum eq 'private' && exists $commands_private{$command})
{
foreach my $item (@{$commands_private{$command}})
{
my ($handler, $level) = @$item;
next unless $userlevel >= $level;
my $result = &{$handler}($self, $command, $forum, $from, $to, $args, $userlevel);
return $result if $result;
}
}
if (exists $commands{$command})
{
foreach my $item (@{$commands{$command}})
{
my ($handler, $level) = @$item;
next unless $userlevel >= $level;
my $result = &{$handler}($self, $command, $forum, $from, $to, $args, $userlevel);
return $result if $result;
}
}
if ($forum eq 'public' && exists $commands_public{"*default*"})
{
foreach my $item (@{$commands_public{"*default*"}})
{
my ($handler, $level) = @$item;
next unless $userlevel >= $level;
my $result = &{$handler}($self, $command, $forum, $from, $to, $args, $userlevel);
return $result if $result;
}
}
if ($forum eq 'private' && exists $commands_private{"*default*"})
{
foreach my $item (@{$commands_private{"*default*"}})
{
my ($handler, $level) = @$item;
next unless $userlevel >= $level;
my $result = &{$handler}($self, $command, $forum, $from, $to, $args, $userlevel);
return $result if $result;
}
}
if (exists $commands{"*default*"})
{
foreach my $item (@{$commands{"*default*"}})
{
my ($handler, $level) = @$item;
next unless $userlevel >= $level;
my $result = &{$handler}($self, $command, $forum, $from, $to, $args, $userlevel);
return $result if $result;
}
}
}; # eval
if ($@)
{
bot_log "Trapped error: $@\n";
bot_log "Input was: <$from> $command $args\n";
}
return 0;
}
sub on_msg {
my ($self, $event) = @_;
my $msg = ($event->args)[0];
my $from = $event->from;
my $to = ${$event->to}[0];
my $public = ($to =~ /^#/);
#bot_log "MSG from $from to $to: $msg\n";
my $qnick = quotemeta lc $nick;
my $cmdspec = $require_name ? qr/!$qnick\s+/i : qr/!(?:$qnick\s+)?/i;
my $msgcmdspec = qr/!?(?:$qnick\s+)?/i;
$msg = strip_codes($msg);
if ($public ? $msg =~ /^\s*$cmdspec([\w]+)(?:\s+(.*)|\s*)$/ : $msg =~ /^\s*$msgcmdspec([\w]+)(?:\s+(.*)|\s*)$/)
{
my $command = $1;
my $args = $2 || "";
$args =~ s/\s+$//;
eval {
do_command($self, $command, $public ? "public" : "private", $from, $to, $args);
};
if ($@)
{
bot_log "Error while executing command $command: $@";
}
}
}
sub on_kick {
my ($self, $event) = @_;
my $channel = ($event->args)[0];
my $from = $event->from;
my $to = ${$event->to}[0];
if ($to eq $nick)
{
bot_log "KICKED from $channel by $from\n";
delete $joined_channels{$channel};
}
}
sub on_ping {
my ($self, $event) = @_;
my $nick = $event->nick;
$self->ctcp_reply($nick, "PING " . join(' ', ($event->args)));
}
sub on_disconnect {
#### This quits and restarts the bot immediately upon disconnect.
## This was forced into play by several bugs that occured on disconnect/reconnect from IRC
$quitting = 1;
`./xylbot-globalgamers-silent &`;
exit(0);
####
my ($self, $event) = @_;
return if $quitting;
my $sleeptime = 30;
my $sleeptime2 = 120;
bot_log "Disconnected from ", $event->from(), " (", ($event->args())[0], "). Reconnect in $sleeptime seconds.\n";
sleep $sleeptime;
retry:
bot_log "Attempting to reconnect...\n";
create_connection();
unless ($connection->connect())
{
bot_log "Connect attempt failed. Retry in $sleeptime2 seconds.\n";
sleep $sleeptime2;
goto retry;
}
}
sub on_join {
my ($self, $event) = @_;
my $from = $event->from;
my $channel = ${$event->to}[0];
$from =~ /^(.*)!(.*)@(.*)$/;
my ($nick, $login, $host) = ($1, $2, $3);
$channel_users{$channel}{$nick} = [$nick, $login, $host];
if ($nick eq $::nick)
{
$self->who($channel);
}
else
{
if ($handlers{"join"})
{
$cur_connection = $self;
foreach my $handler (@{$handlers{"join"}})
{
&{$handler}($nick);
}
}
}
}
sub on_nick {
my ($self, $event) = @_;
my @args = $event->args;
my $from = $event->from;
my $channel = ${$event->to}[0];
$from =~ /^(.*)!(.*)@(.*)$/;
my ($nick, $login, $host) = ($1, $2, $3);
my $newnick = $args[0];
$channel_users{$channel}{$newnick} = [$newnick, $login, $host];
delete $channel_users{$channel}{$nick};
if ($handlers{"nick"})
{
$cur_connection = $self;
foreach my $handler (@{$handlers{"nick"}})
{
&{$handler}($nick, $newnick);
}
}
}
sub on_whoreply {
my ($self, $event) = @_;
my @args = $event->args;
my $channel = ${$event->to}[0];
my ($login, $host, $nick) = @args;
$channel_users{$channel}{$nick} = [$nick, $login, $host];
}
sub add_handlers {
$connection->add_handler("yourhost", \&on_connect);
$connection->add_handler("msg", \&on_msg);
$connection->add_handler("public", \&on_msg);
$connection->add_handler("kick", \&on_kick);
$connection->add_handler("cping", \&on_ping);
$connection->add_handler("disconnect", \&on_disconnect);
$connection->add_handler("join", \&on_join);
$connection->add_handler("nick", \&on_nick);
foreach my $mod (keys %module_loaded)
{
if ($module_loaded{$mod} and exists &{"${mod}::add_handlers"})
{
eval "&${mod}::add_handlers";
if ($@)
{
bot_log "Error adding handlers for $mod: $@\n";
}
}
}
}
sub add_commands {
%commands = %commands_public = %commands_private = %command_help = ();
# Failsafe commands, overriden in the 'base' module.
&::add_command_any("quit", sub {
my ($self, $command, $forum, $from, $to, $args) = @_;
$self->quit($args ? $args : "Quitting");
$quitting = 1;
return 1;
}, 500);
&::add_command_any("upgrade", sub {
my ($self, $command, $forum, $from, $to, $args) = @_;
&do_reload();
::notice("Reloaded (failsafe).");
return 1;
}, 500);
foreach my $mod (keys %module_loaded)
{
if ($module_loaded{$mod} and exists &{"${mod}::add_commands"})
{
eval "&${mod}::add_commands";
if ($@)
{
bot_log "Error adding commands for $mod: $@\n";
}
}
}
}
sub add_event_handler {
my ($event, $handler) = @_;
$handlers{$event} = [] unless $handlers{$event};
unshift @{$handlers{$event}}, $handler;
}
sub add_command_public {
my ($command, $handler, $level, $help) = @_;
if ($level && $level !~ /^\d+$/)
{
$level = 0;
($command, $handler, $help) = @_;
}
$level = 0 unless $level;
$commands_public{$command} = [] unless $commands_public{$command};
unshift @{$commands_public{$command}}, [$handler, $level];
$command_help{$command} = $help if defined $help;
}
sub add_command_private {
my ($command, $handler, $level, $help) = @_;
if ($level && $level !~ /^\d+$/)
{
$level = 0;
($command, $handler, $help) = @_;
}
$level = 0 unless $level;
$commands_private{$command} = [] unless $commands_private{$command};
unshift @{$commands_private{$command}}, [$handler, $level];
$command_help{$command} = $help if defined $help;
}
sub add_command_any {
my ($command, $handler, $level, $help) = @_;
if ($level && $level !~ /^\d+$/)
{
$level = 0;
($command, $handler, $help) = @_;
}
$level = 0 unless $level;
$commands{$command} = [] unless $commands{$command};
unshift @{$commands{$command}}, [$handler, $level];
$command_help{$command} = $help if defined $help;
}
sub add_help {
my ($command, $help) = @_;
$command_help{$command} = $help;
}
sub load_module {
my ($mod, $quitonerror) = @_;
if (!$module_loaded{$mod})
{
$module_status{$mod} = do "$mod/module.pm";
if ($@)
{
bot_log "Error loading module $mod: $@\n";
die if $quitonerror;
}
elsif ($module_status{$mod})
{
bot_log "Module $mod loaded successfully\n";
$module_loaded{$mod} = 1;
push @module_files, "$mod/module.pm";
if (exists $::{"${mod}::"}{"files"})
{
foreach my $file (@{$::{"${mod}::"}{"files"}})
{
do "$mod/$file";
if ($@)
{
bot_log "Error loading $mod/$file: $@\n";
die if $quitonerror;
}
push @module_files, "$mod/$file";
}
}
}
else
{
bot_log "Module $mod failed to load\n";
die if $quitonerror;
}
if ($module_loaded{$mod} and exists &{"${mod}::postload"})
{
eval "&${mod}::postload";
if ($@)
{
bot_log "Error executing postload for $mod: $@\n";
}
}
}
}
sub load_modules {
open CONFIG, "<", "modules.conf";
while (<CONFIG>)
{
if (/^load\s*=\s*(.*)$/)
{
my @modules_to_load = split /\s+/, $1;
foreach my $mod (@modules_to_load)
{
load_module($mod, 1);
}
}
}
close CONFIG;
}
sub read_users {
@users = ();
open CONFIG, "<", "users";
while (my $line = <CONFIG>)
{
next if $line =~ /^\s*#/;
my ($mask, $level, $comment) = split /\s+/, $line, 3;
push @users, [$mask, $level];
}
close CONFIG;
}
sub do_reload {
load_modules;
foreach my $file ($0, @module_files)
{
bot_log "Reloading from $file\n";
do $file;
if ($@)
{
bot_log "Error loading $file: $@\n";
}
}
foreach my $mod (keys %module_loaded)
{
if ($module_loaded{$mod} and exists &{"${mod}::postload"})
{
eval "&${mod}::postload";
if ($@)
{
bot_log "Error executing postload for $mod: $@\n";
}
}
}
add_commands;
add_handlers;
read_users;
}
# Send a message to the current channel
sub say {
my ($to, $msg);
if (@_ > 1)
{
($to, $msg) = @_;
}
else
{
($to, $msg) = ($cur_channel, @_);
}
$cur_connection->privmsg($to, $msg);
}
# Do an action (/me) in the current channel
sub action {
my ($to, $msg);
if (@_ > 1)
{
($to, $msg) = @_;
}
else
{
($to, $msg) = ($cur_channel, @_);
}
return unless $cur_connection;
$cur_connection->ctcp("ACTION", $to, $msg);
}
# If called with one argument, send a notice to the person who gave the command.
# If called with two arguments, send a notice to a person given by the first argument.
sub notice {
my ($to, $msg);
if (@_ > 1)
{
($to, $msg) = @_;
}
else
{
($to, $msg) = ($cur_fromnick, @_);
}
$to =~ s/!.*//;
return unless $cur_connection;
$cur_connection->notice($to, $msg);
}
sub say_or_notice {
my $msg = shift;
if ($cur_forum eq 'public')
{
say($msg);
}
else
{
notice($msg);
}
}
sub strip_codes {
my $text = shift;
$text =~ s/\003\d+(?:,\d+)?|[\001-\037]//g;
return $text;
}
load_modules;
create_connection;
read_users;
bot_log "Connecting to server...\n";
eval {
$irc->start;
};
if ($@) {
bot_log "ERROR Looks like Net::IRC died: The error was '$@'\n";
}