-
Notifications
You must be signed in to change notification settings - Fork 1
/
cs-irc.pl
1389 lines (1237 loc) · 48.6 KB
/
cs-irc.pl
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 -w
###############################################################################
# Counterstrike to IRC Bot
#
# Shows a Counterstrike-Game live on IRC (and much more)
#
# (2001) magenbrot
#
# Required modules/programs
#
# - strict-module =)
# - KKrcon
# - POE;
# - POE::Component::IRC;
# - IO::Socket
# - POSIX
# - Time::Local
# - Getopt::Long
# - Net::hostent
# - LWP::UserAgent
# - HTTP::Request
# - HTML::HeadParser
# - qstat (external program)
#
#
# ToDo-List
#
# - Optimize color-usage in IRC
# - Multi-Channel capability
# - Game statistics
# - say something on all channels the bot is on (only for authed admin)
# - maybe build a threading PerlBot, to serve more then one user a time (if possible)
# - quotefunktionen: addquote, quote, delquote - vgl. #nixblicka
# - reconnect to IRC after beeing disconnected
# - some Events in CS are not displayed (ie. Target bombed)
#
#
# Known bugs (note: for this is a first-time release there may be a plenty of bugs I don't know of)
#
# - none =)
#
#
# Features
#
# - forwarding events from an CS-server to IRC
# - forwarding public- and team-chat from the CS-server to IRC (optional, only for authed admin)
# - can authenticate himself with the Q from quakenet-IRC (optional)
# - rejoin channel after being kicked (optional)
# - can query the default- or any other hl-cs server
# - display server-rules
# - display server statistics (RCON status command)
# - capable to work behind a NAT-Firewall (the UDP-receive port must be correctly configured on the firewall)
# - can show fortunes on IRC (man fortune)
# - can be invited to other channels
# - can display current server-password (only for authed admin)
# - can change the map on server (only for authed admin)
# - tries to reconnect to IRC received the disconnected event
# - can be forced to reconnect to the hl-cs server (only for authed admin)
# - automatically gives +v to people joining a channel the bot is on (optional)
# - join other channels via /msg (only for authed admin) (!cjoin)
# - Admin-Auth
# - script_send: on request from a IRC-User the sources (gzipped) of the bot will be send to him via DCC
# - changes of servervariables are shown in IRC
# - !announce #channel (announces the watched game in other channels)
#
#
# Changes:
# v0.1 - the beginning
# v0.2 - complete rewrite of the bot, implemented POE-model
# v0.3 - outsourced the bot-configuration and admin-configuration
# v0.4 - bugfixing
#
#
# Greetings and Thanks to:
# Ian Cass <[email protected]> for the callback {} function
# my clan independent][Lords <http://www.independent-lords.de>
#
###############################################################################
###############################################################################
# Load modules
###############################################################################
use strict;
use POE;
use POE::Component::IRC;
use IO::Socket;
use POSIX;
use Time::Local;
use Getopt::Long;
use Net::hostent;
use KKrcon;
###############################################################################
# Loading current configuration
###############################################################################
my %config = ();
my $debug = 1;
my %irc_name;
my %irc_admins = ();
my %irc_authed = ();
my @irc_quotes = ();
&LoadConfiguration;
my $version = "v0.4";
my $irc_Q_name = "Q\@CServe.quakenet.org";
my $irc_Q_msg = "AUTH $config{irc_Q_username} $config{irc_Q_password}";
my $irc_Q_success = 0;
my $irc_fortune_tmp1 = 0;
my $irc_fortune_tmp2 = time();
my $inet_ip = "";
my $inet_listen_ip = "";
my $hl_type = "new";
my $hl_rcon = "";
my $hl_mapchange = 0;
my $hl_connected = 0;
my $hl_result="";
###############################################################################
# don't edit anything below this line unless you know what you're doing
###############################################################################
# Start with empty stats
my %teams = ();
my %hash_kills = ();
my %hash_deaths = ();
my $timer = 0;
my $timer_status = 0;
my $player1 = "";
my $player2 = "";
my $color = "";
my $weapon = "";
###############################################################################
# Mainprogram
###############################################################################
$| = 1;
Getopt::Long::Configure ("bundling");
print ("INET - Getting public IP... ") if $debug;
$inet_ip = &get_ip();
if ($inet_ip =~ (/^([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})$/) && ($1 <= 255) && ($2 <= 255) && ($3 <= 255) && ($4 <= 255)) {
print ("done: $inet_ip\n") if $debug;
}
else {
die ("No valid IP received: $inet_ip !\n");
}
if ($config{inet_masqued}) {
$inet_listen_ip = $config{inet_local_ip};
}
else {
$inet_listen_ip = $inet_ip;
}
POE::Session->create
( inline_states =>
{ _start => \&udp_server_start,
_stop => \&udp_server_stop,
select_read => \&udp_server_receive,
socket_made => \&udp_server_socket,
socket_error => \&udp_server_error,
},
args => [ $config{inet_listen_port} ],
options => { Debug => 0 },
);
print ("INET - UDP Port opened OK\n") if $debug;
# Connect to the HL-server
if ($config{hl_connect}) {
&server_connect;
}
POE::Component::IRC->new($config{irc_nick}, trace => undef ) || die ("IRC - Can't instantiate new IRC component for \"$config{irc_nick}\"!\n");
POE::Session->new( 'main' => [qw(
_start
_stop
_default
irc_ctcp_version
irc_ctcp_ping
irc_ctcp_time
irc_ctcp_source
irc_ctcp_finger
irc_dcc_done
irc_dcc_error
irc_disconnected
irc_socketerr
irc_error
irc_join
irc_nick
irc_public
irc_msg
irc_invite
irc_notice
irc_part
irc_quit
irc_kick
irc_001)] );
$poe_kernel->run();
exit 0;
sub _start {
my ($kernel, $session) = @_[KERNEL, SESSION];
# $session->option( trace => 1 );
$kernel->alias_set('control');
$irc_name{ $kernel->alias_resolve($config{irc_nick}) } = $config{irc_nick};
$kernel->call($config{irc_nick}, 'register', 'all');
print ("IRC - _start - $config{irc_nick} is connecting to $config{irc_server}...\n") if $debug;
$kernel->call( $config{irc_nick}, 'connect', {
Debug => 0,
Nick => $config{irc_nick},
Server => $config{irc_server},
Port => $config{irc_port},
Username => $config{irc_uname},
Ircname => $config{irc_name},
} );
}
sub _stop {
my ($kernel) = $_[KERNEL];
$kernel->post('control', 'quit', $config{irc_quitmsg});
print ("IRC - _stop - Disconnected all clients.\n") if $debug;
print ("IRC - _stop - Control session stopping.\n") if $debug;
$kernel->alias_remove('control');
}
sub _default {
my ($state, $event, $args) = @_[STATE, ARG0, ARG1];
$args ||= [];
print ("IRC - $state -- $event @$args\n") if $debug;
}
sub irc_001 {
my ($kernel, $sender) = @_[KERNEL, SENDER];
print ("IRC - irc_001 - $irc_name{$sender} is connected.\n") if $debug;
$kernel->post($sender, 'mode', $irc_name{$sender}, '+i' );
$kernel->post($sender, 'away', "magenbrot meint: im westen nichts neues..." );
$kernel->post($sender, 'join', $config{irc_channel1} );
if($config{irc_Q_enabled} && ! $irc_Q_success) {
print ("IRC - irc_001 - Trying to auth with Q: $irc_Q_name -> $irc_Q_msg\n") if $debug;
$kernel->post( $sender, 'privmsg', $irc_Q_name, $irc_Q_msg);
}
}
sub irc_join {
my ($kernel, $sender, $who, $chan) = @_[KERNEL, SENDER, ARG0, ARG1];
$who =~ s/^(.*)!.*$/$1/;
print ("IRC - irc_join - $who has joined $chan\n") if $debug;
if ($who eq $config{irc_nick}) {
my @qstat = `./qstat -hls $config{hl_ip}:$config{hl_port} -nh -raw \";\"`;
my @erg = split(/;/, $qstat[0]);
$kernel->post($sender, 'privmsg', $chan, $config{irc_joinmsg});
$kernel->post($sender, 'privmsg', $chan, "$erg[2] ($erg[1]) $erg[5]/$erg[4] $erg[3] $erg[6]ms");
if ((! $hl_connected) && (! $config{hl_connect})) {
$kernel->post($sender, 'privmsg', $chan, "7Current settings say not to connect to the server...");
$kernel->post($sender, 'privmsg', $chan, "7So I'm waiting here to serve u all...");
}
elsif ((! $hl_connected) && ($config{hl_connect})) {
$kernel->post($sender, 'privmsg', $chan, "4Attention: I could'nt connect to the server at startup.");
$kernel->post($sender, 'privmsg', $chan, "4Please try a manual connect!");
}
else {
$kernel->post($sender, 'privmsg', $chan, "3Connected successfully...");
}
# TESTSTATUS - Join more channels -> cs-irc.cfg
if ($config{irc_channel2}) { $kernel->post($sender, 'join', $config{irc_channel2} ); }
if ($config{irc_channel3}) { $kernel->post($sender, 'join', $config{irc_channel3} ); }
}
else {
if ($config{irc_welcome}) {
$kernel->post($sender, 'privmsg', $who, $config{irc_joinmsg});
$kernel->post($sender, 'privmsg', $who, " ");
$kernel->post($sender, 'privmsg', $who, "Welcome to channel $chan, $who");
$kernel->post($sender, 'privmsg', $who, "Usage: !help");
}
if ($config{irc_autovoice}) {
$kernel->post($sender, 'mode', $chan, "+v $who");
}
}
}
sub irc_disconnected {
my ($sender, $irc_server) = @_[SENDER, ARG0];
die ("IRC - irc_disconnected - $irc_name{$sender} lost connection to server $irc_server.\n");
}
sub irc_nick {
my ($kernel, $sender, $who, $newnick) = @_[KERNEL, SENDER, ARG0, ARG1];
print ("IRC - irc_nick - $who changed his nick to $newnick\n") if $debug;
if (exists($irc_authed{$who})) {
$irc_authed{$newnick} = 1;
delete($irc_authed{$who});
}
}
sub irc_kick {
my ($kernel, $sender, $bitch, $chan, $who, $reason) = @_[KERNEL, SENDER, ARG0 .. ARG3];
$bitch =~ s/^(.*)!.*$/$1/;
if ($who eq $config{irc_nick} && $config{irc_rejoin}) {
print ("IRC - on_kick - We were kicked by $bitch from $chan because: $reason!\n") if $debug;
#sleep 5;
$kernel->post($sender, 'join', $chan);
$kernel->post($sender, 'privmsg', $chan, "$bitch, what r u doing!?");
}
elsif ($who eq $config{irc_nick} && ! $config{irc_rejoin}) {
print ("IRC - on_kick - We were kicked by $bitch from $chan because: $reason!\n") if $debug;
}
else {
print ("IRC - on_kick - $bitch has kicked $who from $chan because: $reason\n") if $debug;
}
}
sub irc_error {
my ($sender, $err) = @_[SENDER, ARG0];
die ("A server error occurred to $irc_name{$sender}! $err\n");
}
sub irc_socketerr {
my ($sender, $err) = @_[SENDER, ARG0];
die ("$irc_name{$sender} couldn't connect to server: $err\n");
}
sub irc_notice {
my ($kernel, $sender, $notice) = @_[KERNEL, SENDER, ARG2];
if ($notice eq "AUTH'd successfully.") { $irc_Q_success = 1; }
print ("IRC - irc_notice - $irc_name{$sender}: $notice\n") if $debug;
}
sub irc_public {
my ($kernel, $sender, $who, $chan, $msg) = @_[KERNEL, SENDER, ARG0 .. ARG2];
my @args=split(/ /, $msg);
$who =~ s/^(.*)!.*$/$1/ || die ("Weird-ass who: $who");
print("IRC - irc_public: @$chan:<$who>: $msg\n") if $debug;
if ($msg =~ /!qs/) {
if ($args[1] eq "") {
my @qstat = `./qstat -hls $config{hl_ip}:$config{hl_port} -nh -raw \";\"`;
my @erg = split(/;/, $qstat[0]);
$kernel->post($sender, 'privmsg', $chan, "$erg[2] ($erg[1]) $erg[5]/$erg[4] $erg[3] $erg[6]ms");
}
else {
my @qstat = `./qstat -hls $args[1] -nh -raw \";\"`;
my @erg = split(/;/, $qstat[0]);
$kernel->post($sender, 'privmsg', $chan, "$erg[2] ($erg[1]) $erg[5]/$erg[4] $erg[3] $erg[6]ms");
}
}
elsif ($msg =~ /!send_script/) {
print ("SEND - $who requested the bot-script\n") if $debug;
$kernel->post($sender, 'dcc', $who, 'send', 'cs-irc.tar.gz' );
}
elsif ($msg =~ /!weichei/) {
$kernel->post($sender, 'ctcp', $chan, "ACTION", "sees that $who calls $args[1] a Weichei");
}
elsif ($msg =~ /!quote/ && $config{irc_quote}) {
my $quote_nr;
my $anzahl = @irc_quotes - 1;
if ($args[1] eq "") {
$quote_nr = sprintf("%.0f", rand $anzahl);
}
else {
$quote_nr = sprintf("%.0f", $args[1] - 1);
}
my $quote_out = $irc_quotes[$quote_nr];
print ("QUOTE - $who requested a quote\n") if $debug;
$quote_nr++;
$anzahl++;
$kernel->post($sender, 'privmsg', $chan, "12i][Lords - Special ($quote_nr/$anzahl): $quote_out");
}
elsif ($msg =~ /!help/) {
&display_help($kernel, $sender, $who);
}
elsif ($msg =~ /!voice/) {
print ("AUTH - $who got +v on @$chan\n") if $debug;
$kernel->post($sender, 'mode', @$chan, "+v $who");
}
elsif ($msg =~ /!version/) {
$kernel->post($sender, 'privmsg', $chan, "12,8 CS-to-IRC Bot - Version : $version from http://www.independent-lords.de ");
}
elsif ($msg =~ /^!uptime$/i) {
`uptime` =~ /.*up\s*(.*?),\s*(\d*):(\d*)/;
$kernel->post($sender, 'privmsg', $chan, "I've been up $1, $2 hours and $3 minutes, honey :)");
}
elsif ($config{irc_fortune} && $msg =~ /!fortune/) {
my @fortune;
$irc_fortune_tmp1 = time - $irc_fortune_tmp2;
if ($irc_fortune_tmp1 > $config{irc_fortune_timeout}) {
if ($args[1]) {
@fortune = `fortune -s $args[1]`;
}
else {
@fortune = `fortune -s -a`;
}
foreach my $item (@fortune) {
$item =~ s/\t/ /g;
$kernel->post($sender, 'privmsg', $chan, "$item");
$irc_fortune_tmp2 = time;
}
}
}
# These commands are only available to authed admins
elsif ($irc_authed{$who}) {
if ($msg =~ /!op/) {
if ($irc_authed{$who}) {
print ("AUTH - $who becomes OP on @$chan\n") if $debug;
$kernel->post($sender, 'mode', @$chan, "+o $who");
}
else {
$kernel->post($sender, 'privmsg', $chan, "No @ for you, $who");
}
}
elsif ($msg =~ /!addquote/ && $config{irc_quote}) {
$kernel->post($sender, 'privmsg', $chan, "Sorry, function not implemented yet...");
}
elsif ($msg =~ /!delquote/ && $config{irc_quote}) {
$kernel->post($sender, 'privmsg', $chan, "Sorry, function not implemented yet...");
}
}
}
sub irc_msg {
my ($kernel, $sender, $who, $recip, $msg) = @_[KERNEL, SENDER, ARG0 .. ARG2];
my @args=split(/ /, $msg);
$who =~ s/^(.*)!.*$/$1/ || die ("Weird-ass who: $who");
print("IRC - irc_msg: $who: $msg\n") if $debug;
if ($msg =~ /!help/) {
&display_help($kernel, $sender, $who);
}
elsif ($msg =~ /!send_script/) {
$kernel->post($sender, 'dcc', $who, 'send', 'test.txt' );
}
elsif ($msg =~ /!whoami/) {
print ("AUTH - $who requests WHOAMI\n") if $debug;
if ($irc_authed{$who}) {
$kernel->post($sender, 'privmsg', $who, "You are successfully authed, $who");
}
else {
$kernel->post($sender, 'privmsg', $who, "You are not authed, $who");
}
}
elsif ($msg =~ /!connected/) {
$kernel->post($sender, 'privmsg', $who, "Sending test-command");
$hl_result = &execute("connected $who");
}
elsif ($msg =~ /!version/) {
$kernel->post($sender, 'privmsg', $who, "12,8 CS-to-IRC Bot - Version : $version from http://www.independent-lords.de ");
}
elsif ($msg =~ /!qs/) {
if ($args[1] eq "") {
my @qstat = `./qstat -hls $config{hl_ip}:$config{hl_port} -nh -raw \";\"`;
my @erg = split(/;/, $qstat[0]);
$kernel->post($sender, 'privmsg', $who, "$erg[2] ($erg[1]) $erg[5]/$erg[4] $erg[3] $erg[6]ms");
}
else {
my @qstat = `./qstat -hls $args[1] -nh -raw \";\"`;
my @erg = split(/;/, $qstat[0]);
$kernel->post($sender, 'privmsg', $who, "$erg[2] ($erg[1]) $erg[5]/$erg[4] $erg[3] $erg[6]ms");
}
}
elsif ($msg =~ /!rules/) {
$kernel->post($sender, 'privmsg', $who, " ");
$kernel->post($sender, 'privmsg', $who, "Querying serverrules from $config{hl_ip}:$config{hl_port}...");
$kernel->post($sender, 'privmsg', $who, " ");
my @qstat = `./qstat -hls $config{hl_ip}:$config{hl_port} -nh -R -raw \";\"`;
my $counter=0;
foreach my $zeile (@qstat) {
my @erg = split(/;/, $zeile);
foreach my $line (@erg) {
$counter++;
if ($counter >= 9) {
$kernel->post($sender, 'privmsg', $who, $line);
sleep 2;
}
}
}
}
elsif ($msg =~ /!auth/) {
if (! $args[2]) {
$kernel->post($sender, 'privmsg', $who, "Usage: !auth username password");
}
else {
if (exists($irc_admins{$args[1]})) {
if ($irc_admins{$args[1]} eq $args[2]) {
$kernel->post($sender, 'privmsg', $who, "Access granted for user $args[1]");
print ("AUTH - Access granted for user $args[1] with password $args[2]\n") if $debug;
$irc_authed{$who} = 1;
}
else {
$kernel->post($sender, 'privmsg', $who, "Access denied for user $args[1] - wrong password");
print ("AUTH - Access denied for user $args[1] with password $args[2]\n") if $debug;
}
}
else {
$kernel->post($sender, 'privmsg', $who, "Access denied for user $args[1]");
print ("AUTH - Access denied for user $args[1] - User not found.\n") if $debug;
}
}
}
elsif ($msg =~ /!status/) {
$kernel->post($sender, 'privmsg', $who, " ");
$kernel->post($sender, 'privmsg', $who, "Querying status from $config{hl_ip}:$config{hl_port}...");
$kernel->post($sender, 'privmsg', $who, " ");
$hl_result = &execute("status");
my @splitresult = split(/\n/, $hl_result);
foreach my $line (@splitresult) {
$kernel->post($sender, 'privmsg', $who, $line);
}
}
elsif ($msg =~ /!stats/) {
$kernel->post($sender, 'privmsg', $who, " ");
$kernel->post($sender, 'privmsg', $who, "Current statistics");
$kernel->post($sender, 'privmsg', $who, " ");
$kernel->post($sender, 'privmsg', $who, "Team Counterterrorists");
$kernel->post($sender, 'privmsg', $who, "--------------------------------");
$kernel->post($sender, 'privmsg', $who, "Team Terrorists");
open (STATS, ">>stats.log") || die "Konnte stats.log nicht oeffnen: $!";
print STATS (map { "$_ => $teams{$_}\n" } keys %teams) if $debug;
print STATS ("\n\n") if $debug;
print STATS (map { "$_ => $hash_kills{$_}\n" } keys %hash_kills) if $debug;
print STATS ("\n\n") if $debug;
print STATS (map { "$_ => $hash_deaths{$_}\n" } keys %hash_deaths) if $debug;
print STATS ("\n\n\n\n") if $debug;
close (STATS);
print (map { "$_ => $teams{$_}\n" } keys %teams) if $debug;
print ("\n\n") if $debug;
print (map { "$_ => $hash_kills{$_}\n" } keys %hash_kills) if $debug;
print ("\n\n") if $debug;
print (map { "$_ => $hash_deaths{$_}\n" } keys %hash_deaths) if $debug;
}
# These commands are only available to authed admins
elsif ($irc_authed{$who}) {
if (($msg =~ /!reconnect/) || ($msg =~ /!connect/)) {
$kernel->post($sender, 'privmsg', $who, "Trying to register at the server...");
&server_connect;
}
elsif ($msg =~ /!disconnect/) {
$kernel->post($sender, 'privmsg', $who, "Stopping transmission from server...");
delete $kernel->{socket_handle};
}
elsif ($msg =~ /!reload/) {
@irc_quotes = ();
%irc_admins = ();
&LoadConfiguration;
$kernel->post($sender, 'privmsg', $who, "Reloading configuration...");
}
elsif ($msg =~ /!quit/) {
$kernel->post($sender, 'privmsg', $who, " ");
$kernel->post($sender, 'privmsg', $who, "Quitting IRC, closing all connections...");
$kernel->post($sender, 'privmsg', $who, " ");
$kernel->post($sender, 'quit', $config{irc_quitmsg} );
}
elsif ($msg =~ /!t_chat/) {
if ($args[1] eq "1") {
$config{ev_t_chat}=1;
$kernel->post($sender, 'privmsg', $who, "Showing Team-Chat...");
}
elsif ($args[1] eq "0") {
$config{ev_t_chat}=0;
$kernel->post($sender, 'privmsg', $who, "NOT Showing Team-Chat...");
}
else {
if ($config{ev_t_chat}) {
$kernel->post($sender, 'privmsg', $who, "Team-Chat is shown.");
}
else {
$kernel->post($sender, 'privmsg', $who, "Team-Chat is NOT shown.");
}
}
}
elsif ($msg =~ /!chat/) {
if ($args[1] eq "1") {
$config{ev_chat}=1;
$kernel->post($sender, 'privmsg', $who, "Showing Public-Chat...");
}
elsif ($args[1] eq "0") {
$config{ev_chat}=0;
$kernel->post($sender, 'privmsg', $who, "NOT Showing Public-Chat...");
}
else {
if ($config{ev_chat}) {
$kernel->post($sender, 'privmsg', $who, "Public-chat is shown.");
}
else {
$kernel->post($sender, 'privmsg', $who, "Public-chat is NOT shown.");
}
}
}
elsif ($msg =~ /!say/) {
my $say = "";
$say = $args[1];
for my $x (2 .. $#args) {
$say = $say . " " . $args[$x];
}
$kernel->post($sender, 'privmsg', $config{irc_channel1}, $say);
}
elsif ($msg =~ /!cjoin/) {
$kernel->post($sender, 'join', $args[1]);
}
elsif ($msg =~ /!announce/) {
# DEBUG: Überprüfung, ob Bot bereits im Channel ist, muss noch eingebaut werden
$kernel->post($sender, 'join', $args[1]);
$kernel->post($sender, 'privmsg', $args[1], "Here's $config{irc_nick} speaking...");
$kernel->post($sender, 'privmsg', $args[1], "I'm currently posting an important game to IRC");
$kernel->post($sender, 'privmsg', $args[1], "Please join $config{irc_channel1} to watch!");
$kernel->post($sender, 'part', $args[1]);
}
elsif ($msg =~ /!password/) {
$kernel->post($sender, 'privmsg', $who, " ");
$kernel->post($sender, 'privmsg', $who, "Querying password from $config{hl_ip}:$config{hl_port}...");
$kernel->post($sender, 'privmsg', $who, " ");
$hl_result = &execute("sv_password");
$kernel->post($sender, 'privmsg', $who, $hl_result);
}
elsif ($msg =~ /!changelevel/) {
$hl_result = &execute("changelevel $args[1]");
if ($hl_result =~ /^changelevel failed: '(.+)' not found on server./) {
$kernel->post($sender, 'privmsg', $who, $hl_result);
}
else {
$kernel->post($sender, 'privmsg', $who, "Loading map $args[1]");
}
}
}
elsif ($msg =~ /.*/) {
$kernel->post($sender, 'privmsg', $who, "Type \"!help\" for help on valid commands");
}
}
sub irc_invite {
my ($kernel, $sender, $who, $chan) = @_[KERNEL, SENDER, ARG0, ARG1];
$who =~ s/^(.*)!.*$/$1/ || die ("Weird-ass who: $who");
print ("IRC - irc_invite - $who invited me to join $chan\n") if $debug;
$kernel->post($sender, 'join', $chan);
}
sub irc_quit {
my ($kernel, $sender, $who, $msg) = @_[KERNEL, SENDER, ARG0, ARG1];
$who =~ s/^(.*)!.*$/$1/ || die ("Weird-ass who: $who");
print ("IRC - irc_quit - $who quit IRC: $msg\n") if $debug;
}
sub irc_part {
my ($kernel, $sender, $who, $chan) = @_[KERNEL, SENDER, ARG0, ARG1];
$who =~ s/^(.*)!.*$/$1/ || die ("Weird-ass who: $who");
print ("IRC - irc_part - $who left channel $chan\n") if $debug;
}
sub irc_ctcp_version {
my ($kernel, $sender, $who, $msg) = @_[KERNEL, SENDER, ARG0];
$who =~ s/^(.*)!.*$/$1/ || die ("Weird-ass who: $who");
print ("IRC - irc_ctcp_version: $who\n") if $debug;
$kernel->post($sender, 'ctcpreply', $who, "VERSION Counterstrike-To-IRC Bot $version (c)2001 magenbrot - http://www.independent-lords.de");
}
sub irc_ctcp_ping {
my ($kernel, $sender, $who, $msg) = @_[KERNEL, SENDER, ARG0, ARG2];
$who =~ s/^(.*)!.*$/$1/ || die ("Weird-ass who: $who");
print ("IRC - irc_ctcp_ping: $who - $msg\n") if $debug;
$kernel->post($sender, 'ctcpreply', $who, "PING $msg");
}
sub irc_ctcp_time {
my ($kernel, $sender, $who, $msg) = @_[KERNEL, SENDER, ARG0, ARG2];
$who =~ s/^(.*)!.*$/$1/ || die ("Weird-ass who: $who");
print ("IRC - irc_ctcp_time: $who\n") if $debug;
$kernel->post($sender, 'ctcpreply', $who, "TIME " . (strftime "%a %b %d %H:%M:%S %G %Z", localtime));
}
sub irc_ctcp_source {
my ($kernel, $sender, $who, $msg) = @_[KERNEL, SENDER, ARG0, ARG2];
$who =~ s/^(.*)!.*$/$1/ || die ("Weird-ass who: $who");
print ("IRC - irc_ctcp_source: $who\n") if $debug;
$kernel->post($sender, 'ctcpreply', $who, "SOURCE http://www.independent-lords.de");
}
sub irc_ctcp_finger {
my ($kernel, $sender, $who, $msg) = @_[KERNEL, SENDER, ARG0, ARG2];
$who =~ s/^(.*)!.*$/$1/ || die ("Weird-ass who: $who");
print ("IRC - irc_ctcp_finger: $who\n") if $debug;
$kernel->post($sender, 'ctcpreply', $who, "FINGER Hey! Don't touch me!");
}
sub irc_dcc_done {
my ($magic, $nick, $type, $port, $file, $size, $done) = @_[ARG0 .. ARG6];
print ("DCC $type to $nick ($file) done: $done bytes transferred.\n") if $debug;
}
sub irc_dcc_error {
my ($err, $nick, $type, $file) = @_[ARG0 .. ARG2, ARG4];
print ("DCC $type to $nick ($file) failed: $err.\n") if $debug;
}
###############################################################################
# Display help in IRC
###############################################################################
sub display_help {
my ($kernel, $sender, $who) = @_;
$kernel->post($sender, 'privmsg', $who, " ");
$kernel->post($sender, 'privmsg', $who, "$config{irc_nick} Commands on PRIVATE-channel:");
$kernel->post($sender, 'privmsg', $who, " ");
$kernel->post($sender, 'privmsg', $who, "!stats - Show current statistics");
$kernel->post($sender, 'privmsg', $who, "!status - Serverstatus");
$kernel->post($sender, 'privmsg', $who, "!connected - Are we connected or not?");
$kernel->post($sender, 'privmsg', $who, "!rules - Serverrules");
$kernel->post($sender, 'privmsg', $who, "!qs ip:port - Queries a server");
$kernel->post($sender, 'privmsg', $who, "!qs - Queries directly the $config{hl_server_name} Server");
$kernel->post($sender, 'privmsg', $who, "!auth username password - Authenticate with $config{irc_nick}");
$kernel->post($sender, 'privmsg', $who, "!send_script - Sends the perl-script of the bot via DCC");
$kernel->post($sender, 'privmsg', $who, "!version - Displays the version of the bot");
if ($irc_authed{$who}) {
$kernel->post($sender, 'privmsg', $who, "!reconnect - reconnect to the hl-cs server");
$kernel->post($sender, 'privmsg', $who, "!password - Shows current serverpassword");
$kernel->post($sender, 'privmsg', $who, "!chat 0/1 - Switch public_chat in IRC on (1) and off (0)");
$kernel->post($sender, 'privmsg', $who, "!t_chat 0/1 - Switch team_chat in IRC on (1) and off (0)");
$kernel->post($sender, 'privmsg', $who, "!say - say something as $config{irc_nick}");
$kernel->post($sender, 'privmsg', $who, "!cjoin #channel - Force $config{irc_nick} to join #channel");
$kernel->post($sender, 'privmsg', $who, "!changelevel xxx - Load map xxx on the hl-cs server");
$kernel->post($sender, 'privmsg', $who, "!changelevel xxx - Load map xxx on the hl-cs server");
$kernel->post($sender, 'privmsg', $who, "!quit - Force the bot to quit IRC");
}
$kernel->post($sender, 'privmsg', $who, " ");
$kernel->post($sender, 'privmsg', $who, "$config{irc_nick} Commands on the PUBLIC-channel:");
$kernel->post($sender, 'privmsg', $who, " ");
$kernel->post($sender, 'privmsg', $who, "!qs ip:port - Queries a server");
$kernel->post($sender, 'privmsg', $who, "!qs - Queries directly the $config{hl_server_name} Server");
if ($config{irc_fortune}) {
$kernel->post($sender, 'privmsg', $who, "!fortune - Print out a random fortune");
}
if ($config{irc_quote}) {
$kernel->post($sender, 'privmsg', $who, "!quote [nr.] - Print out a random quote or the specified quote");
if ($irc_authed{$who}) {
$kernel->post($sender, 'privmsg', $who, "!addquote - Adds a new quote");
$kernel->post($sender, 'privmsg', $who, "!delquote ## - Deletes quote number ##");
}
}
$kernel->post($sender, 'privmsg', $who, "!send_script - Sends the perl-script of the bot via DCC");
$kernel->post($sender, 'privmsg', $who, "!version - Displays the version of the bot");
$kernel->post($sender, 'privmsg', $who, " ");
$kernel->post($sender, 'privmsg', $who, "If you get an \"Rcon timeout\" issue the command again.");
$kernel->post($sender, 'privmsg', $who, " ");
$kernel->post($sender, 'privmsg', $who, "Problems, Hints, Tips:");
$kernel->post($sender, 'privmsg', $who, "magenbrot\@independent-lords.de - Counterstrike-To-IRC Bot $version (c)2001");
}
###############################################################################
# Getting your internet-ip from external script
###############################################################################
sub get_ip {
use LWP::UserAgent;
use HTTP::Request;
my $ua = LWP::UserAgent->new();
$ua->agent("Braindead/v1.0");
my $anf = HTTP::Request->new(GET => $config{inet_url});
$anf->referer("http://www.independent-lords.de");
my $antwort = $ua->request($anf);
if ($antwort->is_error()) {
return($antwort->status_line);
}
else {
return($antwort->content());
}
}
###############################################################################
# Connecting to the HL-server
###############################################################################
sub server_connect {
print ("\nRCON - Sending 'logaddress' command to server...\n") if $debug;
my $counter = 0;
$hl_result = "Rcon timeout";
while (($hl_result eq "Rcon timeout") && ($counter < $config{hl_retry})) {
$hl_result = &execute("logaddress $inet_ip $config{inet_listen_port}");
if ($hl_result eq "Rcon timeout") {
print ("RCON - Got Rcon timeout, retrying...\n") if $debug;
$counter++;
}
}
if ($counter eq $config{hl_retry}) {
print ("RCON - Could not send 'logaddress' command. Giving up...\n\n") if $debug;
$hl_connected = 0;
}
else {
print ("RCON - 'logaddress' command sent\n\n") if $debug;
print ("RCON - Sending 'log on' command to server...\n") if $debug;
$counter = 0;
$hl_result = "Rcon timeout";
while (($hl_result eq "Rcon timeout") && ($counter < $config{hl_retry})) {
$hl_result = &execute("log on");
if ($hl_result eq "Rcon timeout") {
print ("RCON - Got Rcon timeout, retrying...\n") if $debug;
$counter++;
}
}
if ($counter eq $config{hl_retry}) {
print ("RCON - Could not send 'log on' command. Giving up...\n\n") if $debug;
$hl_connected = 0;
}
else {
print ("RCON - 'log on' command sent\n\n") if $debug;
print ("RCON - Sending 'mp_logmessages 1' command to server...\n") if $debug;
$counter = 0;
$hl_result = "Rcon timeout";
while (($hl_result eq "Rcon timeout") && ($counter < $config{hl_retry})) {
$hl_result = &execute("mp_logmessages 1");
if ($hl_result eq "Rcon timeout") {
print ("RCON - Got Rcon timeout, retrying...\n") if $debug;
$counter++;
}
}
if ($counter eq $config{hl_retry}) {
print ("RCON - Could not send 'mp_logmessages 1' command. Giving up...\n\n") if $debug;
$hl_connected = 0;
}
else {
print ("RCON - 'mp_logmessages' command sent\n\n") if $debug;
$hl_connected = 1;
}
}
}
if (! $hl_connected) {
print ("RCON - We are not listening to the server. Please connect manually...\n\n") if $debug;
}
else {
print ("RCON - Successfully registered us to the server...\n\n") if $debug;
# DEBUG: # rausnehmen, damit sich der bot auf dem server anmeldet
&execute("say This server is now transmitting to $config{irc_channel1} via $config{irc_nick}");
}
}
###############################################################################
# RCON-Routine
###############################################################################
sub execute {
my ($command) = @_;
my $hl_rcon = new KKrcon(
Host => $config{hl_ip},
Port => $config{hl_port},
Password => $config{hl_rcon_pw},
Type => $hl_type
);
print ("RCON - execute: $command\n") if $debug;
my $answer = $hl_rcon->execute($command);
if (my $error = $hl_rcon->error()) {
print ("RCON - Error: $error\n") if $debug;
return ($error);
}
else
{
print ("RCON - Answer: $answer") if $debug;
return ($answer);
}
}
###############################################################################
# UDP listening socket (to receive the cs-server messages
###############################################################################
sub udp_server_start {
my ($kernel, $heap, $port) = @_[KERNEL, HEAP, ARG0];
print ("INET - udp_server_start - Opening UDP listen socket on $inet_listen_ip:$config{inet_listen_port} ...\n") if $debug;
if (defined ($heap->{socket_handle} = IO::Socket::INET->new( Proto => 'udp', LocalPort => $config{inet_listen_port} ))) {
$kernel->select_read($heap->{socket_handle}, 'select_read');
}
else {
die ("INET - udp_server_start - error ", ($!+0), " creating socket: $!\n");
}
}
sub udp_server_stop {
print ("INET - Closing UDP listen socket.\n") if $debug;
delete $_[HEAP]->{socket_handle};
}
sub udp_server_receive {
my ($kernel, $heap, $socket) = @_[KERNEL, HEAP, ARG0];
my $remote_socket = recv( $heap->{socket_handle}, my $message = '', POSIX::BUFSIZ, 0);
if (defined $remote_socket) {
my ($remote_port, $remote_addr) = unpack_sockaddr_in($remote_socket);
my $human_addr = inet_ntoa($remote_addr);
#print ("server: received message from $human_addr : $remote_port\n") if $debug;
#print ("server: message=($message)\n") if $debug;
&callback($kernel, $message);
}
}
sub udp_server_error {
my ($heap, $operation, $errnum, $errstr) = @_[HEAP, ARG0, ARG1, ARG2];
print("INET - udp_server_error - $operation error $errnum: $errstr\n") if $debug;
delete $heap->{socket_handle};
}
###############################################################################
# Load Bot-Configuration, Admins and Quotes
###############################################################################
sub LoadConfiguration {
print ("\nCONFIG - Reading configuration... ") if $debug;
open (CONFIG, "cs-irc.cfg") or die ("Error! No config file could be found: $!");
READ: while(<CONFIG>) {
next READ if /^#/;
$config{$1} = $2 if /^(.*?)\s*=\s*(.*)$/;
}
close (CONFIG);
print ("done loading configuration\n\n") if $debug;
print ("ADMIN - Reading admin-configuration... ") if $debug;
open (ADMIN, "admin.cfg") or die ("Error! No admin file could be found: $!");
READ: while(<ADMIN>) {
next READ if /^#/;
$irc_admins{$1} = $2 if /^(.*?)\s*=\s*(.*)$/;
}
close (ADMIN);
print ("done loading admin-configuration\n\n") if $debug;
if ($config{irc_quote}) {
my $counter = 0;
print ("QUOTES - Reading quotes-file... ") if $debug;
open (QUOTE, $config{irc_quote_file}) || die ("Can't load quotes: $!");
while (<QUOTE>) {
chomp;
push(@irc_quotes, $_);
$counter++;
}
close (QUOTE);
if($counter eq 1) {
print ("done loading $counter quote\n\n") if $debug;
}
else {
print ("done loading $counter quotes\n\n") if $debug;
}
}
}
###############################################################################
# CS-Server to IRC Parser
###############################################################################
sub callback {
my ($kernel, $data) = @_;
$data =~ s/\n//g;
$data =~ s/\r//g;
$data = substr($data,33,length($data) - 34);
print ("RCON - DATA: $data\n") if $debug;