-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathirc-patch.pl
More file actions
2228 lines (1958 loc) · 67.5 KB
/
Copy pathirc-patch.pl
File metadata and controls
2228 lines (1958 loc) · 67.5 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
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/perl
#
# _ __ __
# (_)_________ ____ ____ _/ /______/ /_
# / / ___/ ___/_____/ __ \/ __ `/ __/ ___/ __ \
# / / / / /__/_____/ /_/ / /_/ / /_/ /__/ / / /
# /_/_/ \___/ / .___/\__,_/\__/\___/_/ /_/
# /_/ v0.15.25 - "bamboo"
#
# IRC Network-to-Network Channel Link
#
# An IRC bot that can link channels on different IRC
# networks by using the bot as a relay; chat from
# every linked channel is broadcast to every
# other linked channel, allowing users on
# different networks to chat to each other.
#
# Copyright 2018 Dan Hetrick
#
# Author: Dan Hetrick (dhetrick@gmail.com)
#
# ====================================================================
# This program is free software; you can redistribute it and/or modify
# it under the same terms as Perl itself.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See either the
# GNU General Public License or the Artistic License for more details.
# ====================================================================
#
# Also includes XML::TreePP by Yusuke Kawasaki (http://www.kawa.net/)
#
# =======================================================
# | XML::TreePP |
# | By Yusuke Kawasaki |
# | Copyright (c) 2006-2008 Yusuke Kawasaki. |
# | All rights reserved. This program is free software; |
# | you can redistribute it and/or modify it under the |
# | same terms as Perl itself. |
# =======================================================
# =================
# | MODULES BEGIN |
# =================
use strict;
use POE qw(Component::IRC);
use FindBin qw($Bin $RealBin);
# ===============
# | MODULES END |
# ===============
# ===================
# | CONSTANTS BEGIN |
# ===================
# User list
use constant USER_NICK => 0;
use constant USER_CHANNEL => 1;
use constant USER_SERVER => 2;
use constant USER_IS_AN_OP => 3;
use constant USER_IS_VOICED => 4;
use constant USER_LAST_REFRESH => 5;
# Blacklist
use constant BLACKLIST_HOSTNAME => 0;
use constant BLACKLIST_LOCKOUT => 1;
use constant BLACKLIST_LENGTH_MINIMUM => 60;
use constant BLACKLIST_LENGTH_MAXIMUM => 120;
# =================
# | CONSTANTS END |
# =================
# =================
# | GLOBALS BEGIN |
# =================
# -------
# Scalars
# -------
my $APPLICATION = "irc-patch.pl";
my $VERSION = "0.15.25";
my $RELEASE = "bamboo";
my $DESCRIPTION = "IRC network-channel relay bot";
my $TIME = 0;
# ------
# Arrays
# ------
my @USERS = (); # User list for all channels/servers
my @NETWORK = (); # List of POE objects for server/channel connections
my @ADMIN = ( '','' ); # User who is logged in as admin
my @MUTE = (); # List of muted channels
my @BLACKLIST = (); # List of blacklisted users
# ------
# Hashes
# ------
my %servers = {};
# ============================
# | PATCHFILE SETTINGS BEGIN |
# ============================
# These scalars are set to values from the patchfile
my $VERBOSE = 0;
my $LOG = "";
my $NICKNAME = 'irc-patch' . $$;
my $ALTERNATE_NICK = 'irc-patch'. $$;
my $CURRENT_NICK = $NICKNAME;
my $USERNAME = "$APPLICATION $RELEASE-$VERSION ($DESCRIPTION)";
my $ADMIN_PASSWORD ="changeme";
my $MOTD = '';
my $BOT_OUTPUT_SYMBOL = '*** ';
my $PRIVATE_MESSAGE = 1;
my $TIMESTAMP = 1;
my $INFORMATION_CMDS = 1;
# ==========================
# | PATCHFILE SETTINGS END |
# ==========================
# ===============
# | GLOBALS END |
# ===============
# ======================
# | MAIN PROGRAM BEGIN |
# ======================
# Default patchfile name
my $PATCHFILE = 'default.patch';
# If a patchfile file name is passed as the first argument, use it,
# else use the default, "default.patch"
if($#ARGV>=0){ $PATCHFILE=$ARGV[0]; }
# If no patchfile is found, display error and exit.
if((-e $PATCHFILE) && (-f $PATCHFILE)){} else {
print "$APPLICATION $RELEASE-$VERSION ($DESCRIPTION)\n";
print "Patchfile \"$PATCHFILE\" not found!\n";
print "Usage: perl $0 FILENAME\n";
exit 1;
}
# Load in the patchfile
load_patchfile($PATCHFILE);
# Display our banner if verbose is turned on
if($VERBOSE==1){
my $banner = banner_ascii_graphic();
print $banner."$DESCRIPTION\n$APPLICATION (v$VERSION - \"$RELEASE\")\n";
print "(c) Copyright Daniel Hetrick 2018\n\n";
}
# Create a POE object for each server we're connecting to
foreach my $server ( keys %servers ) {
POE::Component::IRC->spawn(
alias => $server,
nick => $NICKNAME,
ircname => $USERNAME,
);
}
# Register which events we want to receive...
POE::Session->create(
package_states =>
[ 'main' => [qw(_start irc_registered irc_001 irc_public irc_msg irc_join irc_part irc_433 irc_ctcp_action irc_352 _beat)], ],
heap => { config => \%servers },
);
# Start up!
$poe_kernel->run();
exit 0;
# ====================
# | MAIN PROGRAM END |
# ====================
# ====================
# | POE EVENTS BEGIN |
# ====================
# _start()
# irc_registered()
# irc_001()
# irc_join()
# irc_part()
# irc_msg()
# irc_public()
# irc_433()
# irc_ctcp_action()
# irc_352()
# _beat()
# ======
# _beat
# ======
# Triggered once a second.
sub _beat {
my ( $kernel, $session ) = @_[ KERNEL, SESSION ];
# Increment server time by one
$TIME=$TIME+1;
# Make sure the _beat is executed again, in one second
$kernel->delay( _beat => 1 );
# Remove users from the blacklist that have timed out
clean_blacklist();
undef;
}
# ======
# _start
# ======
# Triggered when the bot is initially started.
sub _start {
my ( $kernel, $session ) = @_[ KERNEL, SESSION ];
# Make sure that all POE objects get all signals
$kernel->signal( $kernel, 'POCOIRC_REGISTER', $session->ID(), 'all' );
# Start up the heartbeat
$kernel->delay( _beat => 1 );
# Display/log the bot startup
display("Starting up IRC bot...\n");
undef;
}
# ==============
# irc_registered
# ==============
# Triggered every time one of the POE objects is registered.
sub irc_registered {
my ( $kernel, $heap, $sender, $irc_object ) =
@_[ KERNEL, HEAP, SENDER, ARG0 ];
my $alias = $irc_object->session_alias();
my %conn_hash = (
server => $alias,
port => $heap->{config}->{$alias}->{port},
);
# Connect the object to the IRC server...
$kernel->post( $sender, 'connect', \%conn_hash );
undef;
}
# ========
# irc_001
# ========
# Triggered every time the bot connects to an IRC server.
sub irc_001 {
my ( $kernel, $heap, $sender ) = @_[ KERNEL, HEAP, SENDER ];
my $poco_object = $sender->get_heap();
# Display/log the successful connection
display("Connected to ".$poco_object->server_name()."\n");
# Add the POE object to the "network"
my @entry = ($kernel,$sender);
push(@NETWORK,\@entry);
# Join channels named in the patchfile
my $alias = $poco_object->session_alias();
my @channels = @{ $heap->{config}->{$alias}->{channels} };
foreach my $c (@channels) {
# Join the channel
$kernel->post( $sender => join => $c);
# Display/log the channel joined
display("Joining channel \"$c\" on \"".$poco_object->server_name()."\"\n");
# Update the internal user list
update_user_list($poco_object->server_name(),$c,$kernel,$sender);
}
undef;
}
# ========
# irc_join
# ========
# Triggered every time the bot receives a JOIN message.
sub irc_join {
my ( $kernel, $sender, $who, $where ) =
@_[ KERNEL, SENDER, ARG0, ARG1 ];
my $nick = ( split /!/, $who )[0];
my $hostmask = ( split /!/, $who )[1];
my $poco = $sender->get_heap();
my $server_name = $poco->server_name();
# Don't do anything if the joining client is the bot
if($nick eq $CURRENT_NICK){ return undef; }
# Update the internal user list
update_user_list($server_name,$where,$kernel,$sender);
# If there's an MOTD, prepare and send to the joining client as a notice
if($MOTD ne ''){
# Interpolate the MOTD from patchfile
my $mg = $MOTD;
$mg=~s/\%CHANNEL\%/$where/g;
$mg=~s/\%SERVER\%/$server_name/g;
$mg=~s/\%NICK\%/$nick/g;
$mg=~s/\%HOSTMASK\%/$hostmask/g;
my @s = get_server_list(); my $servers = join(', ',@s);
$mg=~s/\%NETWORK\%/$servers/g;
# Generate the channel remote user list
my @ulist = get_channel_user_list($where);
if($#ulist>=0){
my $u = join(', ',@ulist);
$mg=~s/\%USERS\%/$u/g;
} else {
$mg=~s/\%USERS\%/No users found/g;
}
# Send the MOTD to the joining client as a notice
$kernel->post( $sender => notice => $nick => $mg );
}
# Display/log and broadcast join information
display("$nick joined channel $where ($server_name)\n");
broadcast("$nick has joined the channel ($server_name)",$where,$server_name);
undef;
}
# ========
# irc_part
# ========
# Triggered every time the bot receives a PART message.
sub irc_part {
my ( $kernel, $sender, $who, $where ) =
@_[ KERNEL, SENDER, ARG0, ARG1 ];
my $nick = ( split /!/, $who )[0];
my $hostmask = ( split /!/, $who )[1];
my $poco = $sender->get_heap();
my $server_name = $poco->server_name();
# Display/log and broadcast part information
display("$nick left channel $where ($server_name)\n");
broadcast($BOT_OUTPUT_SYMBOL."$nick has left the channel ($server_name)",$where,$server_name);
# Update the internal user list
update_user_list($server_name,$where,$kernel,$sender);
undef;
}
# =======
# irc_msg
# =======
# Triggered every time the bot receives a private message.
# Here's where bot commands are handled.
sub irc_msg {
my ( $kernel, $sender, $who, $where, $what ) =
@_[ KERNEL, SENDER, ARG0, ARG1, ARG2 ];
my $nick = ( split /!/, $who )[0];
my $hostmask = ( split /!/, $who )[1];
my $poco = $sender->get_heap();
my $channel = $where->[0];
my $server_name = $poco->server_name();
# .login PASSWORD
# Logs in as the administrator. If the user enters the wrong password, they
# have to wait 60 seconds before they can try again.
if ( my ($admin) = $what =~ /^\.login (.+)/ ) {
# Is the user blacklisted?
if(is_user_blacklisted($hostmask)==1){
# Display/log/notify that the user is blacklisted, and tried to login.
$kernel->post( $sender => privmsg => $nick => $BOT_OUTPUT_SYMBOL."Incorrect password entered. Try again later." );
display("$nick ($hostmask) tried to log into bot while blacklisted\n");
return undef;
}
# Check to see if the entered password is correct, and if so, log the user in.
if($admin eq $ADMIN_PASSWORD){
if(($ADMIN[0] eq '')&&($ADMIN[1] eq '')) {
$ADMIN[0] = $nick;
$ADMIN[1] = $hostmask;
# Display/log/notify the user that they've logged in successfully.
$kernel->post( $sender => privmsg => $nick => $BOT_OUTPUT_SYMBOL."Logged in!" );
display("$nick ($hostmask) logged into the bot\n");
} else {
# Display/log/notify the user that someone is already logged in.
$kernel->post( $sender => privmsg => $nick => $BOT_OUTPUT_SYMBOL."Someone is already logged in as admin." );
display("$nick ($hostmask) tried to log into bot but $ADMIN[0] ($ADMIN[1]) is already logged in\n");
}
return undef;
} else {
# Add user to blacklist; users can only try one incorrect password a minute.
my $timeout = add_user_to_blacklist($hostmask);
# Display/log/notify the user that they entered the wrong password.
$kernel->post( $sender => privmsg => $nick => $BOT_OUTPUT_SYMBOL."Login incorrect. Try again in $timeout seconds." );
display("$nick ($hostmask) tried to log into bot with wrong password; blacklisted for $timeout seconds\n");
return undef;
}
}
# .logout
# If you're the admin, it logs you out.
if( $what =~ /^\.logout$/ ) {
if(($ADMIN[0] eq $nick)&&($ADMIN[1] eq $hostmask)){
# Logout the user
@ADMIN = ( '','' );
# Display/log/notify the user that they logged out.
$kernel->post( $sender => privmsg => $nick => $BOT_OUTPUT_SYMBOL."Logged out." );
display("$nick ($hostmask) logged out of the bot\n");
return undef;
}
}
# .refresh
# Refreshes the bot's internal user list manually.
# Only available to admins.
if( $what =~ /^\.refresh$/ ) {
if(($ADMIN[0] eq $nick)&&($ADMIN[1] eq $hostmask)){
update_user_list($server_name,$channel,$kernel,$sender);
$kernel->post( $sender => notice => $nick => $BOT_OUTPUT_SYMBOL."User list refreshed." );
}
return undef;
}
# .private USERNAME MESSAGE -or- .private USERNAME SERVER MESSAGE
# Sends a private message via the patch network. This command can
# also take three arguments; if there's more than one client with
# the same nick, the client will be asked to provide the server
# that the desired target is one. That variation of .private is
# not handled here; it's handled in private_message().
if ( my ($private) = $what =~ /^\.private (.+)/ ) {
if($PRIVATE_MESSAGE != 1){
# Private messaging is turned off.
$kernel->post( $sender => notice => $nick => $BOT_OUTPUT_SYMBOL."Private messaging has been turned off" );
return undef;
}
# Parse out the command input.
my @p = split(' ',$private);
my $target = '';
my $message = '';
if($#p>=1){
$target = shift @p;
$message = join(' ',@p);
} else {
# No arguments passed to the .private command
# Notify the client with command usage information.
$kernel->post( $sender => notice => $nick => $BOT_OUTPUT_SYMBOL."USAGE: .private USERNAME MESSAGE" );
return undef;
}
# Now we've got a private message to send, so send it.
private_message($target,$message,$nick,$kernel,$sender);
return undef;
}
# .p USERNAME MESSAGE
# Sends a private message via the patch network.
if ( my ($private) = $what =~ /^\.p (.+)/ ) {
if($PRIVATE_MESSAGE != 1){
# Private messaging is turned off.
$kernel->post( $sender => notice => $nick => $BOT_OUTPUT_SYMBOL."Private messaging has been turned off" );
return undef;
}
# Parse out the command input.
my @p = split(' ',$private);
my $target = '';
my $message = '';
if($#p>=1){
$target = shift @p;
$message = join(' ',@p);
} else {
# No arguments passed to the .p command
# Notify the client with command usage information.
$kernel->post( $sender => notice => $nick => $BOT_OUTPUT_SYMBOL."USAGE: .p USERNAME MESSAGE" );
return undef;
}
# Now we've got a private message to send
private_message($target,$message,$nick,$kernel,$sender);
return undef;
}
# .help
# Displays help text.
if( $what =~ /^\.help$/ ) {
# Non-admin commands.
$kernel->post( $sender => privmsg => $nick => $BOT_OUTPUT_SYMBOL."$APPLICATION IRC Bot" );
$kernel->post( $sender => privmsg => $nick => $BOT_OUTPUT_SYMBOL.".help Display this text" );
$kernel->post( $sender => privmsg => $nick => $BOT_OUTPUT_SYMBOL.".login PASSWORD Logs you in as a bot admin" );
# Only display information commands if they are turned on.
if($INFORMATION_CMDS==1){
$kernel->post( $sender => privmsg => $nick => $BOT_OUTPUT_SYMBOL.".version Display software version" );
$kernel->post( $sender => privmsg => $nick => $BOT_OUTPUT_SYMBOL.".who CHANNEL Display a list of users in CHANNEL" );
$kernel->post( $sender => privmsg => $nick => $BOT_OUTPUT_SYMBOL.".links List the servers currently linked" );
}
# Only display private message commands if they are turned on.
if($PRIVATE_MESSAGE == 1){
$kernel->post( $sender => privmsg => $nick => $BOT_OUTPUT_SYMBOL.".private USER MSG Sends a private message via the link" );
$kernel->post( $sender => privmsg => $nick => $BOT_OUTPUT_SYMBOL.".p USER MSG Sends a private message via the link" );
}
# Admin-only commands will only be displayed to users who are logged in as admins.
if(($ADMIN[0] eq $nick)&&($ADMIN[1] eq $hostmask)){
$kernel->post( $sender => privmsg => $nick => $BOT_OUTPUT_SYMBOL.".refresh Refreshes the internal user list" );
$kernel->post( $sender => privmsg => $nick => $BOT_OUTPUT_SYMBOL.".logout Log out of the bot" );
}
return undef;
}
# Only allow information commands if they are turned on.
if($INFORMATION_CMDS==1){
# .version
# Displays bot version
if( $what =~ /^\.version$/ ) {
$kernel->post( $sender => privmsg => $nick => $BOT_OUTPUT_SYMBOL."$APPLICATION $VERSION ($RELEASE)" );
return undef;
}
# .links
# Display what servers the bot is connected to
if( $what =~ /^\.links$/ ) {
my @s = get_server_list();
$kernel->post( $sender => privmsg => $nick => $BOT_OUTPUT_SYMBOL."This channel is linked to ".join(", ",@s) );
return undef;
}
# .who CHANNEL
# Causes the bot to send a list of all users (on all
# servers) in a channel via a private notice.
if ( my ($target) = $what =~ /^\.who (.+)/ ) {
# If the internal userlist is empty, display an error and tell the user to try again later.
if($#USERS>=0){}else{
$kernel->post( $sender => privmsg => $nick => $BOT_OUTPUT_SYMBOL."Error retrieving user list. Please try again." );
return undef;
}
# Compile the userlist for display. Op'd users get a "@" in front of their nick, and voice'd users get a "+" in
# in front of their nick.
my @ulist = get_channel_user_list($target);
# Compiled userlist is empty, so let the user know (this probably won't happen; empty user list should be caught by the
# first conditional in this statement) and exit the statement
if($#ulist>=0){}else{
$kernel->post( $sender => privmsg => $nick => $BOT_OUTPUT_SYMBOL."No users found" );
return undef;
}
# Send the compiled userlist to the user
foreach my $ul (@ulist) {
$kernel->post( $sender => privmsg => $nick => $BOT_OUTPUT_SYMBOL."$ul" );
}
return undef;
}
}
}
# ==========
# irc_public
# ==========
# Triggered whenever the bot receives a public message.
sub irc_public {
my ( $kernel, $sender, $who, $where, $what ) =
@_[ KERNEL, SENDER, ARG0, ARG1, ARG2 ];
my $nick = ( split /!/, $who )[0];
my $hostmask = ( split /!/, $who )[1];
my $channel = $where->[0];
my $poco = $sender->get_heap();
my $server_name = $poco->server_name();
# .refresh
# Refreshes the bot's internal user list manually.
# Only available to admins.
if( $what =~ /^\.refresh$/ ) {
if(($ADMIN[0] eq $nick)&&($ADMIN[1] eq $hostmask)){
update_user_list($server_name,$channel,$kernel,$sender);
$kernel->post( $sender => notice => $nick => $BOT_OUTPUT_SYMBOL."User list refreshed." );
}
return undef;
}
# .mute
# "Mutes" a channel; the channel on the server where this command is issued is the only one "muted".
# Only available to admins.
# "Muted" channels won't send chat or join/part text to the patch network.
# Issued the "mute" channel again to "unmute".
if( $what =~ /^\.mute$/ ) {
if(($ADMIN[0] eq $nick)&&($ADMIN[1] eq $hostmask)){
my @new = ();
my $muted = 0;
# Check the muted chanel list to see if this channel is in there.
foreach my $m (@MUTE) {
# Step through the mute list and look if the desired channel
# is on it; create a copy of the mute list that contains all
# muted channels *except* for the desired channel.
my @ma = @{$m};
if(($ma[0] eq $channel)&&($ma[1] eq $server_name)){
$muted = 1;
next;
}
push(@new,$m);
}
# @new now contains a list of all muted channels *except* for the channel
# being looked for.
# Mute/unmute the channel and let the channel know.
if($muted==1){
# Channel was already muted; unmute it.
# Set the mute list to @new, since it's already been scrubbed of any
# reference to the desired channel.
@MUTE=@new;
# Now that the channel is off the muted list, the broadcast will reach it.
broadcast($BOT_OUTPUT_SYMBOL."Channel $channel on $server_name unmuted.",$channel,$server_name);
# Display/log the action.
display("$nick ($hostmask) unmuted $channel ($server_name)\n");
} else {
# Channel was not muted. Mute it.
# Send the broadcast *before* the channel is muted, so the broadcast will reach it.
broadcast($BOT_OUTPUT_SYMBOL."Channel $channel on $server_name muted.",$channel,$server_name);
# Add the channel and server to the mute list.
my @entry = ($channel,$server_name);
push(@MUTE,\@entry);
# Display/log the action.
display("$nick ($hostmask) muted $channel ($server_name)\n");
}
return undef;
}
return undef;
}
# .help
# Displays help text via a private notice.
if( $what =~ /^\.help$/ ) {
# Non-admin commands.
$kernel->post( $sender => notice => $nick => $BOT_OUTPUT_SYMBOL."$APPLICATION IRC Bot" );
$kernel->post( $sender => notice => $nick => $BOT_OUTPUT_SYMBOL.".help Display this text" );
# Only display information commands if they are turned on.
if($INFORMATION_CMDS==1){
$kernel->post( $sender => notice => $nick => $BOT_OUTPUT_SYMBOL.".version Display software version" );
$kernel->post( $sender => notice => $nick => $BOT_OUTPUT_SYMBOL.".who Display a list of users in the channel" );
$kernel->post( $sender => notice => $nick => $BOT_OUTPUT_SYMBOL.".links Display the servers this channel is linked to" );
}
# Admin-only commands will only be displayed to users who are logged in as admins.
if(($ADMIN[0] eq $nick)&&($ADMIN[1] eq $hostmask)){
$kernel->post( $sender => notice => $nick => $BOT_OUTPUT_SYMBOL.".refresh Manually refresh the internal user list" );
$kernel->post( $sender => notice => $nick => $BOT_OUTPUT_SYMBOL.".mute Toggles channel muting" );
}
return undef;
}
# Only allow information commands if they are turned on.
if($INFORMATION_CMDS==1){
# .version
# Displays bot version via a private notice.
if( $what =~ /^\.version$/ ) {
$kernel->post( $sender => notice => $nick => $BOT_OUTPUT_SYMBOL."$APPLICATION $VERSION ($RELEASE)" );
return undef;
}
# .links
# Display what servers the bot is connected to via a private notice.
if( $what =~ /^\.links$/ ) {
my @s = get_server_list();
$kernel->post( $sender => notice => $nick => $BOT_OUTPUT_SYMBOL."This channel is linked to ".join(",",@s) );
return undef;
}
# .who
# Causes the bot to send a list of all users (on every
# server but the caller's server) to the calling user via a private notice.
if( $what =~ /^\.who$/ ) {
# If the internal userlist is empty, display an error and tell the user to try again later.
if($#USERS>=0){}else{
$kernel->post( $sender => notice => $nick => $BOT_OUTPUT_SYMBOL."Error retrieving user list. Please try again." );
}
# Compile the userlist for display. Op'd users get a "@" in front of their nick, and voice'd users get a "+" in
# in front of their nick.
my @ulist = get_channel_user_list($channel);
# Compiled userlist is empty, so let the user know (this probably won't happen; empty user list should be caught by the
# first conditional in this statement) and exit the statement.
if($#ulist>=0){}else{
$kernel->post( $sender => notice => $nick => $BOT_OUTPUT_SYMBOL."No users found." );
return undef;
}
# Send the compiled userlist to the user.
foreach my $ul (@ulist) {
$kernel->post( $sender => notice => $nick => $BOT_OUTPUT_SYMBOL."$ul" );
}
return undef;
}
}
# Incoming chat doesn't contain a command, so pass the chat to the patch network and display to all users.
broadcast($BOT_OUTPUT_SYMBOL."<$nick> $what",$channel,$server_name);
# Display/log broadcasted chat
display("$channel $server_name <$nick> $what\n");
undef;
}
# =======
# irc_433
# =======
# Triggered whenever the bot's nick is already in use.
sub irc_433 {
my ($kernel,$sender) = @_[KERNEL,SENDER];
# Bot's initial nick is in use, so try the alternate nickname...
if($CURRENT_NICK eq $NICKNAME) {
display("Changing nick to \"$ALTERNATE_NICK\"...\n");
$CURRENT_NICK=$ALTERNATE_NICK;
foreach my $n (@NETWORK) {
my @na = @{ $n };
$na[0]->post( $na[1] => nick => $CURRENT_NICK );
}
# Bot's alternate nick is in use, so append a random number
# to the altername nick and try again.
}elsif($CURRENT_NICK eq $ALTERNATE_NICK){
$CURRENT_NICK=$ALTERNATE_NICK.$$;
display("Changing nick to \"$CURRENT_NICK\"...\n");
foreach my $n (@NETWORK) {
my @na = @{ $n };
$na[0]->post( $na[1] => nick => $CURRENT_NICK );
}
}
}
# ===============
# irc_ctcp_action
# ===============
# Triggered whenever the server sends the client a CTCP 'action' message.
sub irc_ctcp_action {
my ( $kernel, $sender, $who, $where, $msg ) = @_[ KERNEL, SENDER, ARG0, ARG1, ARG2 ];
my $nick = ( split /!/, $who )[0];
my $hostmask = ( split /!/, $who )[1];
my $channel = $where->[0];
my $poco = $sender->get_heap();
my $server_name = $poco->server_name();
broadcast($BOT_OUTPUT_SYMBOL."$nick $msg",$channel,$server_name);
}
# =======
# irc_352
# =======
# Triggered whenever 'who' data is received by the client.
sub irc_352 {
my ( $kernel, $sender, $serv, $data ) = @_[ KERNEL, SENDER, ARG0, ARG1 ];
my $poco_object = $sender->get_heap();
my $chan = ( split / /, $data )[0];
my $server = $poco_object->server_name();
my $nick = ( split / /, $data )[4];
my $code = ( split / /, $data )[5];
# Bot's current username is left out of the list
if($nick eq $CURRENT_NICK){ return undef; }
# Check to see if the nick is voice'd or op'd, and add them
# to the user list
my $is_op = 0;
my $is_v = 0;
if ( $code =~ /\@/ ) { $is_op=1; }
if ( $code =~ /\+/ ) { $is_v=1; }
add_user_to_channel_user_list($nick,$chan,$server,$is_op,$is_v);
}
# ==================
# | POE EVENTS END |
# ==================
# =============================
# | SUPPORT SUBROUTINES START |
# =============================
# get_channel_user_list()
# load_patchfile()
# clean_blacklist()
# add_user_to_blacklist()
# is_user_blacklisted()
# banner_ascii_graphic()
# error()
# display()
# timestamp()
# broadcast()
# private_message()
# get_server_list()
# update_user_list()
# remove_channel_from_user_list()
# add_user_to_channel_user_list()
# get_channel_user_list()
# Syntax: get_channel_user_list($channel)
# Arguments: 1 (scalar)
# Returns: Array
# Description: Builds a user list, on all networks, for a
# specific channel. List is "pretty"; it also contains whether
# a user is op'd and/or voice'd.
sub get_channel_user_list {
my $channel = shift;
my @ulist = ();
foreach my $u (@USERS) {
my @ua = @{$u};
if($ua[USER_CHANNEL] eq $channel) {
my $n = $ua[USER_NICK];
my $s = $ua[USER_SERVER];
if($ua[USER_IS_VOICED]==1){ $n='+'.$n; }
if($ua[USER_IS_AN_OP]==1){ $n='@'.$n; }
push(@ulist,"$n ($s)" );
}
}
return @ulist;
}
# load_patchfile()
# Syntax: load_patchfile($filename)
# Arguments: 1 (scalar)
# Returns: Nothing
# Description: Loads and parses a patchfile, which contains all the settings needed for
# the bot to operate.
sub load_patchfile {
my $filename = shift;
display("Loading patchfile \"$filename\"...\n");
my @xchannels = ();
my @xservers = ();
my $password = '';
my $motd = '';
my $xnick = '';
my $xinfo = '';
my $xanick = '';
# Load and parse XML patchfile
my $tpp = XML::TreePP->new();
my $tree = $tpp->parsefile( $filename );
# ============================
# | PATCHFILE ELEMENTS BEGIN |
# ============================
# Verbose element
# Turns verbose mode on or off
# Not required
if(ref($tree->{patch}->{verbose}) eq 'ARRAY'){
error("Only one verbose element per patchfile.");
}
if($tree->{patch}->{verbose}){
if( ($tree->{patch}->{verbose} eq "1") || (lc($tree->{patch}->{verbose}) eq "yes") || (lc($tree->{patch}->{verbose}) eq "on") ) {
$VERBOSE = 1;
}
}
# Private_messaging element
# Turns verbose mode on or off
# Not required
if(ref($tree->{patch}->{private_messaging}) eq 'ARRAY'){
error("Only one private_messaging element per patchfile.");
}
if($tree->{patch}->{private_messaging}){
if( ($tree->{patch}->{private_messaging} eq "1") || (lc($tree->{patch}->{private_messaging}) eq "yes") || (lc($tree->{patch}->{private_messaging}) eq "on") ) {
$PRIVATE_MESSAGE = 1;
} elsif ( ($tree->{patch}->{private_messaging} eq "0") || (lc($tree->{patch}->{private_messaging}) eq "no") || (lc($tree->{patch}->{private_messaging}) eq "off") ) {
$PRIVATE_MESSAGE = 0;
}
}
# Information element
# Turns information commands on or off
# Not required
if(ref($tree->{patch}->{information}) eq 'ARRAY'){
error("Only one information element per patchfile.");
}
if($tree->{patch}->{information}){
if( ($tree->{patch}->{information} eq "1") || (lc($tree->{patch}->{information}) eq "yes") || (lc($tree->{patch}->{information}) eq "on") ) {
$INFORMATION_CMDS = 1;
} elsif ( ($tree->{patch}->{information} eq "0") || (lc($tree->{patch}->{information}) eq "no") || (lc($tree->{patch}->{information}) eq "off") ) {
$INFORMATION_CMDS = 0;
}
}
# Timestamp element
# Turns timestamps on logging and verbose messages on or off
# Not required
if(ref($tree->{patch}->{timestamp}) eq 'ARRAY'){
error("Only one timestamp element per patchfile.");
}
if($tree->{patch}->{timestamp}){
if( ($tree->{patch}->{timestamp} eq "1") || (lc($tree->{patch}->{timestamp}) eq "yes") || (lc($tree->{patch}->{timestamp}) eq "on") ) {
$TIMESTAMP = 1;
} elsif ( ($tree->{patch}->{timestamp} eq "0") || (lc($tree->{patch}->{timestamp}) eq "no") || (lc($tree->{patch}->{timestamp}) eq "off") ) {
$TIMESTAMP = 0;
}
}
# Log element
# Turns logging on or off
# Not required
if(ref($tree->{patch}->{log}) eq 'ARRAY'){
error("Only one log element per patchfile.");
}
if($tree->{patch}->{log}){
$LOG = $tree->{patch}->{log};
}
# bot_chat_id element
# Sets a symbol that is prefaced on every bot chat text
# Not required
# Default: "*** "
if(ref($tree->{patch}->{bot_chat_id}) eq 'ARRAY'){
error("Only one bot_chat_id element per patchfile.");
}
if($tree->{patch}->{bot_chat_id}){
$BOT_OUTPUT_SYMBOL = $tree->{patch}->{bot_chat_id};
if(substr($BOT_OUTPUT_SYMBOL, -1) != " "){
$BOT_OUTPUT_SYMBOL .= " ";
}
}
# Channel element(s)
# Sets the IRC channel(s) to patch
# Required
if($tree->{patch}->{channel}){
if(ref($tree->{patch}->{channel}) eq 'ARRAY'){
foreach my $c (@{$tree->{patch}->{channel}}) {
push(@xchannels,$c);
}
} else {
push(@xchannels,$tree->{patch}->{channel});
}
} else {
error("Patchfile is missing a channel element.");
}
# Server element(s)
# Sets the IRC servers to patch, in "server:port" format
# Required
if($tree->{patch}->{server}){
if(ref($tree->{patch}->{server}) eq 'ARRAY'){
foreach my $c (@{$tree->{patch}->{server}}) {
push(@xservers,$c);
}
} else {
push(@xservers,$tree->{patch}->{server});
}
} else {
error("Patchfile is missing a server element.");
}