3 our $VERSION = '0.4.2 svn $Revision$';
5 # Copyright (c) 2008 Rudolf "divVerent" Polzer
7 # Permission is hereby granted, free of charge, to any person
8 # obtaining a copy of this software and associated documentation
9 # files (the "Software"), to deal in the Software without
10 # restriction, including without limitation the rights to use,
11 # copy, modify, merge, publish, distribute, sublicense, and/or sell
12 # copies of the Software, and to permit persons to whom the
13 # Software is furnished to do so, subject to the following
16 # The above copyright notice and this permission notice shall be
17 # included in all copies or substantial portions of the Software.
19 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
21 # OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
23 # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
24 # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25 # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
26 # OTHER DEALINGS IN THE SOFTWARE.
28 # MISC STRING UTILITY ROUTINES to convert between DarkPlaces and IRC conventions
30 # convert mIRC color codes to DP color codes
31 our $color_utf8_enable = 1;
32 our @color_irc2dp_table = (7, 0, 4, 2, 1, 1, 6, 1, 3, 2, 5, 5, 4, 6, 7, 7);
33 our @color_dp2irc_table = (-1, 4, 9, 8, 12, 11, 13, -1, -1, -1); # not accurate, but legible
34 our @color_dp2ansi_table = ("m", "1;31m", "1;32m", "1;33m", "1;34m", "1;36m", "1;35m", "m", "1m", "1m"); # not accurate, but legible
35 our %color_team2dp_table = (5 => 1, 14 => 4, 13 => 3, 10 => 6);
36 our %color_team2irc_table = (5 => 4, 14 => 12, 13 => 8, 10 => 13);
40 $message =~ s/\^/^^/g;
42 $message =~ s{\003(\d\d?)(?:,(\d?\d?))?|(\017)}{
43 # $1 is FG, $2 is BG, but let's ignore BG
44 my $oldcolor = $color;
51 $color = $color_irc2dp_table[$1];
52 $color = $oldcolor if not defined $color;
54 ($color == $oldcolor) ? '' : '^' . $color;
56 $message =~ s{[\000-\037]}{}gs; # kill bold etc. for now
60 our @text_qfont_table = ( # ripped from DP console.c qfont_table
61 '', '#', '#', '#', '#', '.', '#', '#',
62 '#', 9, 10, '#', ' ', 13, '.', '.',
63 '[', ']', '0', '1', '2', '3', '4', '5',
64 '6', '7', '8', '9', '.', '<', '=', '>',
65 ' ', '!', '"', '#', '$', '%', '&', '\'',
66 '(', ')', '*', '+', ',', '-', '.', '/',
67 '0', '1', '2', '3', '4', '5', '6', '7',
68 '8', '9', ':', ';', '<', '=', '>', '?',
69 '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G',
70 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
71 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
72 'X', 'Y', 'Z', '[', '\\', ']', '^', '_',
73 '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g',
74 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
75 'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
76 'x', 'y', 'z', '{', '|', '}', '~', '<',
77 '<', '=', '>', '#', '#', '.', '#', '#',
78 '#', '#', ' ', '#', ' ', '>', '.', '.',
79 '[', ']', '0', '1', '2', '3', '4', '5',
80 '6', '7', '8', '9', '.', '<', '=', '>',
81 ' ', '!', '"', '#', '$', '%', '&', '\'',
82 '(', ')', '*', '+', ',', '-', '.', '/',
83 '0', '1', '2', '3', '4', '5', '6', '7',
84 '8', '9', ':', ';', '<', '=', '>', '?',
85 '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G',
86 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
87 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
88 'X', 'Y', 'Z', '[', '\\', ']', '^', '_',
89 '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g',
90 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
91 'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
92 'x', 'y', 'z', '{', '|', '}', '~', '<'
94 sub text_qfont_table($)
98 if($color_utf8_enable)
100 return (($o & 0xFF00) == 0xE000) ? $text_qfont_table[$o & 0xFF] : $char;
104 return $text_qfont_table[$o];
110 $message = join '', map { text_qfont_table $_ } split //, $message;
113 sub color_dp_transform(&$)
115 my ($block, $message) = @_;
116 $message =~ s{(?:(\^\^)|\^x([0-9a-fA-F])([0-9a-fA-F])([0-9a-fA-F])|\^([0-9])|(.))(?=([0-9,]?))}{
117 defined $1 ? $block->(char => '^', $7) :
118 defined $2 ? $block->(rgb => [hex $2, hex $3, hex $4], $7) :
119 defined $5 ? $block->(color => $5, $7) :
120 defined $6 ? $block->(char => $6, $7) :
131 return color_dp_transform
133 my ($type, $data, $next) = @_;
135 ? text_qfont_table $data
141 sub color_rgb2basic($)
144 my ($R, $G, $B) = @$data;
145 my $min = [sort { $a <=> $b } ($R, $G, $B)]->[0];
146 my $max = [sort { $a <=> $b } ($R, $G, $B)]->[-1];
149 my $s = ($max == $min) ? 0 : 1 - $min/$max;
153 return 0 if $v < 0.5;
164 $h = (60 * ($G - $B) / ($max - $min)) % 360;
168 $h = (60 * ($B - $R) / ($max - $min)) + 120;
172 $h = (60 * ($R - $G) / ($max - $min)) + 240;
177 return 2 if $h < 150;
178 return 5 if $h < 200;
179 return 4 if $h < 270;
180 return 6 if $h < 330;
184 sub color_dp_rgb2basic($)
187 return color_dp_transform
189 my ($type, $data, $next) = @_;
190 $type eq 'char' ? ($data eq '^' ? '^^' : $data) :
191 $type eq 'color' ? "^$data" :
192 $type eq 'rgb' ? "^" . color_rgb2basic $data :
202 return color_dp_transform
204 my ($type, $data, $next) = @_;
209 $data = color_rgb2basic $data;
212 $type eq 'char' ? text_qfont_table $data :
213 $type eq 'color' ? do {
214 my $oldcolor = $color;
215 $color = $color_dp2irc_table[$data];
217 $color == $oldcolor ? '' :
218 $color < 0 ? "\017" :
219 (index '0123456789,', $next) >= 0 ? "\003$color\002\002" :
231 return color_dp_transform
233 my ($type, $data, $next) = @_;
238 $data = color_rgb2basic $data;
241 $type eq 'char' ? text_qfont_table $data :
242 $type eq 'color' ? do {
243 my $oldcolor = $color;
244 $color = $color_dp2ansi_table[$data];
246 $color eq $oldcolor ? '' :
257 # if the message ends with an odd number of ^, kill one
258 chop $message if $message =~ /(?:^|[^\^])\^(\^\^)*$/;
267 # $conn->sockname() returns a connection type specific representation
268 # string of the local address, or undef if not applicable.
269 # $conn->peername() returns a connection type specific representation
270 # string of the remote address, or undef if not applicable.
271 # $conn->send("string") sends something over the connection.
272 # $conn->recv() receives a string from the connection, or returns "" if no
274 # $conn->fds() returns all file descriptors used by the connection, so one
275 # can use select() on them.
277 # Usually wraps around a connection and implements a command based
278 # structure over it. It usually is constructed using new
279 # ChannelType($connection, someparameters...)
280 # @cmds = $chan->join_commands(@cmds) joins multiple commands to a single
281 # command string if the protocol supports it, or does nothing and leaves
282 # @cmds unchanged if the protocol does not support that usage (this is
283 # meant to save send() invocations).
284 # $chan->send($command, $nothrottle) sends a command over the channel. If
285 # $nothrottle is sent, the command must not be left out even if the channel
286 # is saturated (for example, because of IRC's flood control mechanism).
287 # $chan->quote($str) returns a string in a quoted form so it can safely be
288 # inserted as a substring into a command, or returns $str as is if not
289 # applicable. It is assumed that the result of the quote method is used
290 # as part of a quoted string, if the protocol supports that.
291 # $chan->recv() returns a list of received commands from the channel, or
292 # the empty list if none are available.
293 # $conn->fds() returns all file descriptors used by the channel's
294 # connections, so one can use select() on them.
303 # Represents a connection over a socket.
304 # Mainly used to wrap a channel around it for, in this case, line based or rcon-like operation.
305 package Connection::Socket;
308 use IO::Socket::INET;
312 # my $conn = new Connection::Socket(tcp => "localaddress" => "remoteaddress" => 6667);
313 # If the remote address does not contain a port number, the numeric port is
314 # used (it serves as a default port).
317 my ($class, $proto, $local, $remote, $defaultport) = @_;
318 my $sock = IO::Socket::INET->new(
320 (length($local) ? (LocalAddr => $local) : ()),
322 PeerPort => $defaultport
323 ) or die "socket $proto/$local/$remote/$defaultport: $!";
327 # Mortal fool! Release me from this wretched tomb! I must be set free
328 # or I will haunt you forever! I will hide your keys beneath the
329 # cushions of your upholstered furniture... and NEVERMORE will you be
330 # able to find socks that match!
332 # My demonic powers have made me OMNIPOTENT! Bwahahahahahahaha!
335 bless $you, 'Connection::Socket';
338 # $sock->sockname() returns the local address of the socket.
342 my ($port, $addr) = sockaddr_in $self->{sock}->sockname();
343 return "@{[inet_ntoa $addr]}:$port";
346 # $sock->peername() returns the remote address of the socket.
350 my ($port, $addr) = sockaddr_in $self->{sock}->peername();
351 return "@{[inet_ntoa $addr]}:$port";
354 # $sock->send($data) sends some data over the socket; on success, 1 is returned.
357 my ($self, $data) = @_;
360 if(not eval { $self->{sock}->send($data); })
368 # $sock->recv() receives as much as possible from the socket (or at most 32k). Returns "" if no data is available.
373 if(defined $self->{sock}->recv($data, 32768, 0))
387 # $sock->fds() returns the socket file descriptor.
391 return fileno $self->{sock};
400 # Line-based buffered connectionless FIFO channel.
401 # Whatever is sent to it using send() is echoed back when using recv().
402 package Channel::FIFO;
407 # my $chan = new Channel::FIFO();
415 bless $you, 'Channel::FIFO';
418 sub join_commands($@)
420 my ($self, @data) = @_;
426 my ($self, $line, $nothrottle) = @_;
427 push @{$self->{buffer}}, $line;
432 my ($self, $data) = @_;
439 my $r = $self->{buffer};
440 $self->{buffer} = [];
456 # QW rcon protocol channel.
457 # Wraps around a UDP based Connection and sends commands as rcon commands as
458 # well as receives rcon replies. The quote and join_commands methods are using
459 # DarkPlaces engine specific rcon protocol extensions.
467 # my $chan = new Channel::QW($connection, "password");
470 my ($class, $conn, $password, $secure, $timeout) = @_;
473 password => $password,
479 bless $you, 'Channel::QW';
482 # Note: multiple commands in one rcon packet is a DarkPlaces extension.
483 sub join_commands($@)
485 my ($self, @data) = @_;
486 return join "\0", @data;
491 my ($self, $line, $nothrottle) = @_;
493 if $color_utf8_enable;
494 if($self->{secure} > 1)
496 $self->{connector}->send("\377\377\377\377getchallenge");
497 my $c = $self->recvchallenge();
498 return 0 if not defined $c;
499 my $key = Digest::HMAC::hmac("$c $line", $self->{password}, \&Digest::MD4::md4);
500 return $self->{connector}->send("\377\377\377\377srcon HMAC-MD4 CHALLENGE $key $c $line");
502 elsif($self->{secure})
504 my $t = sprintf "%ld.%06d", time(), int rand 1000000;
505 my $key = Digest::HMAC::hmac("$t $line", $self->{password}, \&Digest::MD4::md4);
506 return $self->{connector}->send("\377\377\377\377srcon HMAC-MD4 TIME $key $t $line");
510 return $self->{connector}->send("\377\377\377\377rcon $self->{password} $line");
514 # Note: backslash and quotation mark escaping is a DarkPlaces extension.
517 my ($self, $data) = @_;
518 $data =~ s/[\000-\037]//g;
519 $data =~ s/([\\"])/\\$1/g;
520 $data =~ s/\$/\$\$/g;
528 my $sel = IO::Select->new($self->fds());
529 my $endtime_max = Time::HiRes::time() + $self->{timeout};
530 my $endtime = $endtime_max;
532 while((my $dt = $endtime - Time::HiRes::time()) > 0)
534 if($sel->can_read($dt))
538 my $s = $self->{connector}->recv();
543 if($s =~ /^\377\377\377\377challenge (.*?)(?:$|\0)/s)
548 if $s !~ /^\377\377\377\377n(.*)$/s;
549 $self->{recvbuf} .= $1;
561 my $s = $self->{connector}->recv();
567 if $s !~ /^\377\377\377\377n(.*)$/s;
568 $self->{recvbuf} .= $1;
571 while($self->{recvbuf} =~ s/^(.*?)(?:\r\n?|\n)//)
575 if $color_utf8_enable;
584 return $self->{connector}->fds();
593 # Line based protocol channel.
594 # Wraps around a TCP based Connection and sends commands as text lines
595 # (separated by CRLF). When reading responses from the Connection, any type of
596 # line ending is accepted.
597 # A flood control mechanism is implemented.
598 package Channel::Line;
601 use Time::HiRes qw/time/;
604 # my $chan = new Channel::Line($connection);
607 my ($class, $conn) = @_;
617 bless $you, 'Channel::Line';
620 sub join_commands($@)
622 my ($self, @data) = @_;
626 # Sets new flood control parameters:
627 # $chan->throttle(maximum lines per second, maximum burst length allowed to
628 # exceed the lines per second limit);
629 # RFC 1459 describes these parameters to be 0.5 and 5 for the IRC protocol.
630 # If the $nothrottle flag is set while sending, the line is sent anyway even
631 # if flooding would take place.
634 my ($self, $linepersec, $maxlines) = @_;
635 $self->{linepersec} = $linepersec;
636 $self->{maxlines} = $maxlines;
637 $self->{capacity} = $maxlines;
642 my ($self, $line, $nothrottle) = @_;
644 if $color_utf8_enable;
646 if(defined $self->{capacity})
648 $self->{capacity} += ($t - $self->{lastsend}) * $self->{linepersec};
649 $self->{lastsend} = $t;
650 $self->{capacity} = $self->{maxlines}
651 if $self->{capacity} > $self->{maxlines};
655 if $self->{capacity} < 0;
657 $self->{capacity} -= 1;
660 return $self->{connector}->send("$line\r\n");
665 my ($self, $data) = @_;
666 $data =~ s/\r\n?/\n/g;
676 my $s = $self->{connector}->recv();
681 $self->{recvbuf} .= $s;
684 while($self->{recvbuf} =~ s/^(.*?)(?:\r\n?|\n)//)
688 if $color_utf8_enable;
697 return $self->{connector}->fds();
705 # main program... a gateway between IRC and DarkPlaces servers
713 use Time::HiRes qw/time/;
715 our @handlers = (); # list of [channel, expression, sub to handle result]
716 our @tasks = (); # list of [time, sub]
720 playernick_byid_0 => "(console)",
725 irc_nick_alternates => "",
727 irc_channel => undef,
728 irc_ping_delay => 120,
731 irc_nickserv_password => "",
732 irc_nickserv_identify => 'PRIVMSG NickServ :IDENTIFY %2$s',
733 irc_nickserv_ghost => 'PRIVMSG NickServ :GHOST %1$s %2$s',
734 irc_nickserv_ghost_attempts => 3,
736 irc_quakenet_authname => "",
737 irc_quakenet_password => "",
738 irc_quakenet_authusers => "",
739 irc_quakenet_getchallenge => 'PRIVMSG Q@CServe.quakenet.org :CHALLENGE',
740 irc_quakenet_challengeauth => 'PRIVMSG Q@CServe.quakenet.org :CHALLENGEAUTH',
741 irc_quakenet_challengeprefix => ':Q!TheQBot@CServe.quakenet.org NOTICE [^:]+ :CHALLENGE',
743 irc_announce_slotsfree => 1,
744 irc_announce_mapchange => 'always',
748 dp_secure_challengetimeout => 1,
750 dp_password => undef,
751 dp_status_delay => 30,
752 dp_server_from_wan => "",
753 dp_listen_from_server => "",
754 dp_utf8_enable => $color_utf8_enable,
758 irc_admin_password => "",
759 irc_admin_timeout => 3600,
760 irc_admin_quote_re => "",
762 irc_reconnect_delay => 300,
770 my ($wan, $lan) = @_;
771 # $wan shall override $lan
775 if $wan =~ /:\d+$/; # full override
777 if $lan !~ /:(\d+)$/;
783 # Xonotic specific parsing of some server messages
785 sub xon_slotsstring()
788 if(defined $store{slots_max})
790 my $slots = $store{slots_max} - $store{slots_active};
791 my $slots_s = ($slots == 1) ? '' : 's';
792 $slotsstr = " ($slots free slot$slots_s)";
793 my $s = pickip($config{dp_server_from_wan}, $config{dp_server});
794 $slotsstr .= "; join now: \002xonotic +connect $s"
795 if $slots >= 1 and not $store{lms_blocked};
802 # Do we have a config file? If yes, read and parse it (syntax: key = value
803 # pairs, separated by newlines), if not, complain.
804 die "Usage: $0 configfile\n"
807 open my $fh, "<", $ARGV[0]
808 or die "open $ARGV[0]: $!";
813 /^(.*?)\s*=(?:\s*(.*))?$/ or next;
814 warn "Undefined config item: $1"
815 unless exists $config{$1};
816 $config{$1} = defined $2 ? $2 : "";
819 my @missing = grep { !defined $config{$_} } keys %config;
820 die "The following config items are missing: @missing"
823 $color_utf8_enable = $config{dp_utf8_enable};
826 # Create a channel for error messages and other internal status messages...
828 $channels{system} = new Channel::FIFO();
830 # for example, quit messages caused by signals (if SIGTERM or SIGINT is first
831 # received, try to shut down cleanly, and if such a signal is received a second
835 exit 1 if $quitting++;
836 $channels{system}->send("quit SIGINT");
839 exit 1 if $quitting++;
840 $channels{system}->send("quit SIGTERM");
845 # Create the two channels to gateway between...
847 $channels{irc} = new Channel::Line(new Connection::Socket(tcp => $config{irc_local} => $config{irc_server} => 6667));
848 $channels{dp} = new Channel::QW(my $dpsock = new Connection::Socket(udp => $config{dp_listen} => $config{dp_server} => 26000), $config{dp_password}, $config{dp_secure}, $config{dp_secure_challengetimeout});
849 $config{dp_listen} = $dpsock->sockname();
850 $config{dp_server} = $dpsock->peername();
851 print "Listening on $config{dp_listen}\n";
853 $channels{irc}->throttle(0.5, 5);
856 # Utility routine to write to a channel by name, also outputting what's been written and some status
860 my $nothrottle = shift;
861 my $chan = $channels{$chanstr};
864 print "UNDEFINED: $chanstr, ignoring message\n";
867 @_ = $chan->join_commands(@_);
870 my $result = $chan->send($_, $nothrottle);
873 print " $chanstr << $_\n";
877 print "FLOOD: $chanstr << $_\n";
881 print "ERROR: $chanstr << $_\n";
882 $channels{system}->send("error $chanstr", 0);
889 # Schedule a task for later execution by the main loop; usage: schedule sub {
890 # task... }, $time; When a scheduled task is run, a reference to the task's own
891 # sub is passed as first argument; that way, the task is able to re-schedule
892 # itself so it gets periodically executed.
895 my ($sub, $time) = @_;
896 push @tasks, [time() + $time, $sub];
899 # On IRC error, delete some data store variables of the connection, and
900 # reconnect to the IRC server soon (but only if someone is actually playing)
903 # prevent multiple instances of this timer
904 return if $store{irc_error_active};
905 $store{irc_error_active} = 1;
907 delete $channels{irc};
910 if(!defined $store{slots_active})
912 # DP is not running, then delay IRC reconnecting
913 #use Data::Dumper; print Dumper \$timer;
914 schedule $timer => 1;
916 # this will keep irc_error_active
918 $channels{irc} = new Channel::Line(new Connection::Socket(tcp => $config{irc_local} => $config{irc_server} => 6667));
919 delete $store{$_} for grep { /^irc_/ } keys %store;
920 $store{irc_nick} = "";
923 out dp => 0, 'sv_cmd banlist', 'status 1', 'log_dest_udp';
924 $store{status_waiting} = -1;
926 # this will clear irc_error_active
927 } => $config{irc_reconnect_delay};
937 next if $found{$_}++;
943 # IRC joining (if this is called as response to a nick name collision, $is433 is set);
944 # among other stuff, it performs NickServ or Quakenet authentication. This is to be called
945 # until the channel has been joined for every message that may be "interesting" (basically,
946 # IRC 001 hello messages, 443 nick collision messages and some notices by services).
952 if $store{irc_joined_channel};
954 #use Data::Dumper; print Dumper \%store;
958 if(length $store{irc_nick})
960 # we already have another nick, but couldn't change to the new one
961 # try ghosting and then get the nick again
962 if(length $config{irc_nickserv_password})
964 if(++$store{irc_nickserv_ghost_attempts} <= $config{irc_nickserv_ghost_attempts})
966 $store{irc_nick_requested} = $config{irc_nick};
967 out irc => 1, sprintf($config{irc_nickserv_ghost}, $config{irc_nick}, $config{irc_nickserv_password});
969 out irc => 1, "NICK $config{irc_nick}";
971 return; # we'll get here again for the NICK success message, or for a 433 failure
973 # otherwise, we failed to ghost and will continue with the wrong
974 # nick... also, no need to try to identify here
976 # otherwise, we can't handle this and will continue with our wrong nick
980 # we failed to get an initial nickname
981 # change ours a bit and try again
983 my @alternates = uniq ($config{irc_nick}, grep { $_ ne "" } split /\s+/, $config{irc_nick_alternates});
984 my $nextnick = undef;
985 for(0..@alternates-2)
987 if($store{irc_nick_requested} eq $alternates[$_])
989 $nextnick = $alternates[$_+1];
992 if($store{irc_nick_requested} eq $alternates[@alternates-1]) # this will only happen once
994 $store{irc_nick_requested} = $alternates[0];
995 # but don't set nextnick, so we edit it
997 if(defined $nextnick)
999 $store{irc_nick_requested} = $nextnick;
1005 if(length $store{irc_nick_requested} < 9)
1007 $store{irc_nick_requested} .= '_';
1011 substr $store{irc_nick_requested}, int(rand length $store{irc_nick_requested}), 1, chr(97 + int rand 26);
1013 last unless grep { $_ eq $store{irc_nick_requested} } @alternates;
1016 out irc => 1, "NICK $store{irc_nick_requested}";
1017 return; # when it fails, we'll get here again, and when it succeeds, we will continue
1021 # we got a 001 or a NICK message, so $store{irc_nick} has been updated
1022 if(length $config{irc_nickserv_password})
1024 if($store{irc_nick} eq $config{irc_nick})
1027 out irc => 1, sprintf($config{irc_nickserv_identify}, $config{irc_nick}, $config{irc_nickserv_password});
1032 if(++$store{irc_nickserv_ghost_attempts} <= $config{irc_nickserv_ghost_attempts})
1034 $store{irc_nick_requested} = $config{irc_nick};
1035 out irc => 1, sprintf($config{irc_nickserv_ghost}, $config{irc_nick}, $config{irc_nickserv_password});
1037 out irc => 1, "NICK $config{irc_nick}";
1039 return; # we'll get here again for the NICK success message, or for a 433 failure
1041 # otherwise, we failed to ghost and will continue with the wrong
1042 # nick... also, no need to try to identify here
1046 # we are on Quakenet. Try to authenticate.
1047 if(length $config{irc_quakenet_password} and length $config{irc_quakenet_authname})
1049 if(defined $store{irc_quakenet_challenge})
1051 if($store{irc_quakenet_challenge} =~ /^([0-9a-f]*)\b.*\bHMAC-SHA-256\b/)
1054 my $hash1 = Digest::SHA::sha256_hex(substr $config{irc_quakenet_password}, 0, 10);
1055 my $key = Digest::SHA::sha256_hex("@{[lc $config{irc_quakenet_authname}]}:$hash1");
1056 my $digest = Digest::HMAC::hmac_hex($challenge, $key, \&Digest::SHA::sha256);
1057 out irc => 1, "$config{irc_quakenet_challengeauth} $config{irc_quakenet_authname} $digest HMAC-SHA-256";
1062 out irc => 1, $config{irc_quakenet_getchallenge};
1064 # we get here again when Q asks us
1068 for(split / *; */, $store{irc_commands})
1070 s/\$nick/$store{irc_nick}/g;
1074 # if we get here, we are on IRC
1075 $store{irc_joined_channel} = 1;
1077 # wait 1 sec to let stuff calm down
1078 out irc => 1, "JOIN $config{irc_channel}";
1083 my $RE_FAIL = qr/$ $/;
1084 my $RE_SUCCEED = qr//;
1087 return $_[0] ? $RE_FAIL : $RE_SUCCEED;
1091 # List of all handlers on the various sockets. Additional handlers can be added by a plugin.
1093 # detect a server restart and set it up again
1094 [ dp => q{ *(?:Warning: Could not expand \$|Unknown command ")(?:rcon2irc_[a-z0-9_]*)[" ]*} => sub {
1096 'alias rcon2irc_eval "$*"',
1098 'sv_logscores_console 0',
1099 'sv_logscores_bots 1',
1101 'sv_eventlog_console 1',
1102 'alias rcon2irc_say_as "set say_as_restorenick \"$sv_adminnick\"; sv_adminnick \"$1^3\"; say \"^7$2\"; rcon2irc_say_as_restore"',
1103 'alias rcon2irc_say_as_restore "set sv_adminnick \"$say_as_restorenick\""',
1104 'alias rcon2irc_quit "echo \"quitting rcon2irc $1: log_dest_udp is $log_dest_udp\""'; # note: \\\\\\" ->perl \\\" ->console \"
1108 # detect missing entry in log_dest_udp and fix it
1109 [ dp => q{"log_dest_udp" is "([^"]*)" \["[^"]*"\]} => sub {
1111 my @dests = split ' ', $dest;
1112 return 0 if grep { $_ eq pickip($config{dp_listen_from_server}, $config{dp_listen}) } @dests;
1113 out dp => 0, 'log_dest_udp "' . join(" ", @dests, pickip($config{dp_listen_from_server}, $config{dp_listen})) . '"';
1117 # retrieve list of banned hosts
1118 [ dp => q{#(\d+): (\S+) is still banned for (\S+) seconds} => sub {
1119 return 0 unless $store{status_waiting} < 0;
1120 my ($id, $ip, $time) = @_;
1121 $store{bans_new} = [] if $id == 0;
1122 $store{bans_new}[$id] = { ip => $ip, 'time' => $time };
1126 # retrieve hostname from status replies
1127 [ dp => q{host: (.*)} => sub {
1128 return 0 unless $store{status_waiting} < 0;
1130 $store{dp_hostname} = $name;
1131 $store{bans} = $store{bans_new};
1135 # retrieve version from status replies
1136 [ dp => q{version: (.*)} => sub {
1137 return 0 unless $store{status_waiting} < 0;
1139 $store{dp_version} = $version;
1143 # retrieve player names
1144 [ dp => q{players: (\d+) active \((\d+) max\)} => sub {
1145 return 0 unless $store{status_waiting} < 0;
1146 my ($active, $max) = @_;
1147 my $full = ($active >= $max);
1148 $store{slots_max} = $max;
1149 $store{slots_active} = $active;
1150 $store{status_waiting} = $active;
1151 $store{playerslots_active_new} = [];
1152 if($store{status_waiting} == 0)
1154 $store{playerslots_active} = $store{playerslots_active_new};
1156 if($full != ($store{slots_full} || 0))
1158 $store{slots_full} = $full;
1159 return 0 if $store{lms_blocked};
1160 return 0 if !$config{irc_announce_slotsfree};
1163 out irc => 0, "PRIVMSG $config{irc_channel} :\001ACTION is full!\001";
1167 my $slotsstr = xon_slotsstring();
1168 out irc => 0, "PRIVMSG $config{irc_channel} :\001ACTION can be joined again$slotsstr!\001";
1174 # retrieve player names
1175 [ dp => q{\^\d(\S+)\s+(\d+)\s+(\d+)\s+(\S+)\s+(-?\d+)\s+\#(\d+)\s+\^\d(.*)} => sub {
1176 return 0 unless $store{status_waiting} > 0;
1177 my ($ip, $pl, $ping, $time, $frags, $no, $name) = ($1, $2, $3, $4, $5, $6, $7);
1178 $store{"playerslot_$no"} = { ip => $ip, pl => $pl, ping => $ping, 'time' => $time, frags => $frags, no => $no, name => $name };
1179 push @{$store{playerslots_active_new}}, $no;
1180 if(--$store{status_waiting} == 0)
1182 $store{playerslots_active} = $store{playerslots_active_new};
1187 # IRC admin commands
1188 [ irc => q{:(([^! ]*)![^ ]*) (?i:PRIVMSG) [^&#%]\S* :(.*)} => sub {
1189 return 0 unless ($config{irc_admin_password} ne '' || $store{irc_quakenet_users});
1191 my ($hostmask, $nick, $command) = @_;
1192 my $dpnick = color_dpfix $nick;
1194 if($command eq "login $config{irc_admin_password}")
1196 $store{logins}{$hostmask} = time() + $config{irc_admin_timeout};
1197 out irc => 0, "PRIVMSG $nick :my wish is your command";
1201 if($command =~ /^login /)
1203 out irc => 0, "PRIVMSG $nick :invalid password";
1207 if(($store{logins}{$hostmask} || 0) < time())
1209 out irc => 0, "PRIVMSG $nick :authentication required";
1213 if($command =~ /^status(?: (.*))?$/)
1218 for my $slot(@{$store{playerslots_active} || []})
1220 my $s = $store{"playerslot_$slot"};
1222 if(not defined $match or index(color_dp2none($s->{name}), $match) >= 0)
1224 out irc => 0, sprintf 'PRIVMSG %s :%-21s %2i %4i %8s %4i #%-3u %s', $nick, $s->{ip}, $s->{pl}, $s->{ping}, $s->{time}, $s->{frags}, $slot, color_dp2irc $s->{name};
1233 out irc => 0, "PRIVMSG $nick :the server is empty";
1237 out irc => 0, "PRIVMSG $nick :no nicknames match";
1243 if($command =~ /^kick # (\d+) (.*)$/)
1245 my ($id, $reason) = ($1, $2);
1246 my $dpreason = color_irc2dp $reason;
1247 $dpreason =~ s/^(~?)(.*)/$1irc $dpnick: $2/g;
1248 $dpreason =~ s/(["\\])/\\$1/g;
1249 out dp => 0, "kick # $id $dpreason";
1250 my $slotnik = "playerslot_$id";
1251 out irc => 0, "PRIVMSG $nick :kicked #$id (@{[color_dp2irc $store{$slotnik}{name}]}\017 @ $store{$slotnik}{ip}) ($reason)";
1255 if($command =~ /^kickban # (\d+) (\d+) (\d+) (.*)$/)
1257 my ($id, $bantime, $mask, $reason) = ($1, $2, $3, $4);
1258 my $dpreason = color_irc2dp $reason;
1259 $dpreason =~ s/^(~?)(.*)/$1irc $dpnick: $2/g;
1260 $dpreason =~ s/(["\\])/\\$1/g;
1261 out dp => 0, "kickban # $id $bantime $mask $dpreason";
1262 my $slotnik = "playerslot_$id";
1263 out irc => 0, "PRIVMSG $nick :kickbanned #$id (@{[color_dp2irc $store{$slotnik}{name}]}\017 @ $store{$slotnik}{ip}), netmask $mask, for $bantime seconds ($reason)";
1267 if($command eq "bans")
1271 map { "$_ ($store{bans}[$_]{ip}, $store{bans}[$_]{time}s)" }
1272 0..@{$store{bans} || []}-1;
1273 $banlist = "no bans"
1275 out irc => 0, "PRIVMSG $nick :$banlist";
1279 if($command =~ /^unban (\d+)$/)
1282 out dp => 0, "unban $id";
1283 out irc => 0, "PRIVMSG $nick :removed ban $id ($store{bans}[$id]{ip})";
1287 if($command =~ /^mute (\d+)$/)
1290 out dp => 0, "mute $id";
1291 my $slotnik = "playerslot_$id";
1292 out irc => 0, "PRIVMSG $nick :muted $id (@{[color_dp2irc $store{$slotnik}{name}]}\017 @ $store{$slotnik}{ip})";
1296 if($command =~ /^unmute (\d+)$/)
1299 out dp => 0, "unmute $id";
1300 my $slotnik = "playerslot_$id";
1301 out irc => 0, "PRIVMSG $nick :unmuted $id (@{[color_dp2irc $store{$slotnik}{name}]}\017 @ $store{$slotnik}{ip})";
1305 if($command =~ /^quote (.*)$/)
1308 if($cmd =~ /^(??{$config{irc_admin_quote_re}})$/si)
1311 out irc => 0, "PRIVMSG $nick :executed your command";
1315 out irc => 0, "PRIVMSG $nick :permission denied";
1320 out irc => 0, "PRIVMSG $nick :unknown command (supported: status [substring], kick # id reason, kickban # id bantime mask reason, bans, unban banid, mute id, unmute id)";
1325 # LMS: detect "no more lives" message
1326 [ dp => q{\^4.*\^4 has no more lives left} => sub {
1327 if(!$store{lms_blocked})
1329 $store{lms_blocked} = 1;
1330 if(!$store{slots_full})
1333 if($store{lms_blocked})
1335 out irc => 0, "PRIVMSG $config{irc_channel} :\001ACTION can't be joined until next round (a player has no more lives left)\001";
1342 # detect IRC errors and reconnect
1343 [ irc => q{ERROR .*} => \&irc_error ],
1344 [ irc => q{:[^ ]* 404 .*} => \&irc_error ], # cannot send to channel
1345 [ system => q{error irc} => \&irc_error ],
1348 [ irc => q{:[^ ]* 433 .*} => sub {
1349 return irc_joinstage(433);
1353 [ irc => q{:[^ ]* 001 .*} => sub {
1354 $store{irc_seen_welcome} = 1;
1355 $store{irc_nick} = $store{irc_nick_requested};
1357 # If users for quakenet are listed, parse them into a hash and schedule a sub to query information
1358 if ($config{irc_quakenet_authusers} ne '') {
1359 $store{irc_quakenet_users} = { map { $_ => 1 } split / /, $config{irc_quakenet_authusers} };
1363 out irc => 0, "PRIVMSG Q :users " . $config{irc_channel};
1364 schedule $timer => 300;;
1368 return irc_joinstage(0);
1371 # IRC my nickname changed
1372 [ irc => q{:(?i:(??{$store{irc_nick}}))![^ ]* (?i:NICK) :(.*)} => sub {
1374 $store{irc_nick} = $n;
1375 return irc_joinstage(0);
1378 # Quakenet: challenge from Q
1379 [ irc => q{(??{$config{irc_quakenet_challengeprefix}}) (.*)} => sub {
1380 $store{irc_quakenet_challenge} = $1;
1381 return irc_joinstage(0);
1384 # Catch joins of people in a channel the bot is in and catch our own joins of a channel,
1385 # detect channel join message and note hostname length to get the maximum allowed line length
1386 [ irc => q{:(([^! ]*)![^ ]*) JOIN (#.+)} => sub {
1387 my ($hostmask, $nick, $chan) = @_;
1389 if ($nick eq $store{irc_nick}) {
1390 $store{irc_maxlen} = 510 - length($hostmask);
1391 if($store{irc_joined_channel} == 1)
1393 $store{irc_joined_channel} = 2;
1395 print "* detected maximum line length for channel messages: $store{irc_maxlen}\n";
1398 return 0 unless ($store{irc_quakenet_users});
1400 if ($nick eq $store{irc_nick}) {
1401 out irc => 0, "PRIVMSG Q :users $chan"; # get auths for all users
1403 $store{quakenet_hosts}->{$nick} = $hostmask;
1404 out irc => 0, "PRIVMSG Q :whois $nick"; # get auth for single user
1410 # Catch response of users request
1411 [ irc => q{:Q!TheQBot@CServe.quakenet.org NOTICE [^:]+ :[@\+\s]?(\S+)\s+(\S+)\s*(\S*)\s*\((.*)\)} => sub {
1412 my ($nick, $username, $flags, $host) = @_;
1413 return 0 unless ($store{irc_quakenet_users});
1415 $store{logins}{"$nick!$host"} = time() + 600 if ($store{irc_quakenet_users}->{$username});
1420 # Catch response of whois request
1421 [ irc => q{:Q!TheQBot@CServe.quakenet.org NOTICE [^:]+ :-Information for user (.*) \(using account (.*)\):} => sub {
1422 my ($nick, $username) = @_;
1423 return 0 unless ($store{irc_quakenet_users});
1425 if ($store{irc_quakenet_users}->{$username}) {
1426 my $hostmask = $store{quakenet_hosts}->{$nick};
1427 $store{logins}{$hostmask} = time() + 600;
1433 # shut down everything on SIGINT
1434 [ system => q{quit (.*)} => sub {
1436 out irc => 1, "QUIT :$cause";
1437 $store{quitcookie} = int rand 1000000000;
1438 out dp => 0, "rcon2irc_quit $store{quitcookie}";
1441 # remove myself from the log destinations and exit everything
1442 [ dp => q{quitting rcon2irc (??{$store{quitcookie}}): log_dest_udp is (.*) *} => sub {
1444 my @dests = grep { $_ ne pickip($config{dp_listen_from_server}, $config{dp_listen}) } split ' ', $dest;
1445 out dp => 0, 'log_dest_udp "' . join(" ", @dests) . '"';
1451 [ irc => q{PING (.*)} => sub {
1453 out irc => 1, "PONG $data";
1458 [ irc => q{:[^ ]* PONG .* :(.*)} => sub {
1461 if not defined $store{irc_pingtime};
1463 if $data ne $store{irc_pingtime};
1464 print "* measured IRC line delay: @{[time() - $store{irc_pingtime}]}\n";
1465 undef $store{irc_pingtime};
1469 # chat: Xonotic server -> IRC channel
1470 [ dp => q{\001(.*?)\^7: (.*)} => sub {
1471 my ($nick, $message) = map { color_dp2irc $_ } @_;
1472 out irc => 0, "PRIVMSG $config{irc_channel} :<$nick\017> $message";
1476 # chat: Xonotic server -> IRC channel, nick set
1477 [ dp => q{:join:(\d+):(\d+):([^:]*):(.*)} => sub {
1478 my ($id, $slot, $ip, $nick) = @_;
1479 $store{"playernickraw_byid_$id"} = $nick;
1480 $nick = color_dp2irc $nick;
1481 $store{"playernick_byid_$id"} = $nick;
1482 $store{"playerip_byid_$id"} = $ip;
1483 $store{"playerslot_byid_$id"} = $slot;
1484 $store{"playerid_byslot_$slot"} = $id;
1488 # chat: Xonotic server -> IRC channel, nick change/set
1489 [ dp => q{:name:(\d+):(.*)} => sub {
1490 my ($id, $nick) = @_;
1491 $store{"playernickraw_byid_$id"} = $nick;
1492 $nick = color_dp2irc $nick;
1493 my $oldnick = $store{"playernick_byid_$id"};
1494 out irc => 0, "PRIVMSG $config{irc_channel} :* $oldnick\017 is now known as $nick";
1495 $store{"playernick_byid_$id"} = $nick;
1499 # chat: Xonotic server -> IRC channel, vote call
1500 [ dp => q{:vote:vcall:(\d+):(.*)} => sub {
1501 my ($id, $command) = @_;
1502 $command = color_dp2irc $command;
1503 my $oldnick = $id ? $store{"playernick_byid_$id"} : "(console)";
1504 out irc => 0, "PRIVMSG $config{irc_channel} :* $oldnick\017 calls a vote for \"$command\017\"";
1508 # chat: Xonotic server -> IRC channel, vote stop
1509 [ dp => q{:vote:vstop:(\d+)} => sub {
1511 my $oldnick = $id ? $store{"playernick_byid_$id"} : "(console)";
1512 out irc => 0, "PRIVMSG $config{irc_channel} :* $oldnick\017 stopped the vote";
1516 # chat: Xonotic server -> IRC channel, master login
1517 [ dp => q{:vote:vlogin:(\d+)} => sub {
1519 my $oldnick = $id ? $store{"playernick_byid_$id"} : "(console)";
1520 out irc => 0, "PRIVMSG $config{irc_channel} :* $oldnick\017 logged in as master";
1524 # chat: Xonotic server -> IRC channel, master do
1525 [ dp => q{:vote:vdo:(\d+):(.*)} => sub {
1526 my ($id, $command) = @_;
1527 $command = color_dp2irc $command;
1528 my $oldnick = $id ? $store{"playernick_byid_$id"} : "(console)";
1529 out irc => 0, "PRIVMSG $config{irc_channel} :* $oldnick\017 used his master status to do \"$command\017\"";
1533 # chat: Xonotic server -> IRC channel, result
1534 [ dp => q{:vote:v(yes|no|timeout):(\d+):(\d+):(\d+):(\d+):(-?\d+)} => sub {
1535 my ($result, $yes, $no, $abstain, $not, $min) = @_;
1536 my $spam = "$yes:$no" . (($min >= 0) ? " ($min needed)" : "") . ", $abstain didn't care, $not didn't vote";
1537 out irc => 0, "PRIVMSG $config{irc_channel} :* the vote ended with $result: $spam";
1541 # chat: IRC channel -> Xonotic server
1542 [ irc => q{:([^! ]*)![^ ]* (?i:PRIVMSG) (?i:(??{$config{irc_channel}})) :(?i:(??{$store{irc_nick}}))(?: |: ?|, ?)(.*)} => sub {
1543 my ($nick, $message) = @_;
1544 $nick = color_dpfix $nick;
1545 # allow the nickname to contain colors in DP format! Therefore, NO color_irc2dp on the nickname!
1546 $message = color_irc2dp $message;
1547 $message =~ s/(["\\])/\\$1/g;
1548 out dp => 0, "rcon2irc_say_as \"$nick on IRC\" \"$message\"";
1553 length $config{irc_trigger}
1555 [ irc => q{:([^! ]*)![^ ]* (?i:PRIVMSG) (?i:(??{$config{irc_channel}})) :(?i:(??{$config{irc_trigger}}))(?: |: ?|, ?)(.*)} => sub {
1556 my ($nick, $message) = @_;
1557 $nick = color_dpfix $nick;
1558 # allow the nickname to contain colors in DP format! Therefore, NO color_irc2dp on the nickname!
1559 $message = color_irc2dp $message;
1560 $message =~ s/(["\\])/\\$1/g;
1561 out dp => 0, "rcon2irc_say_as \"$nick on IRC\" \"$message\"";
1568 # irc: CTCP VERSION reply
1569 [ irc => q{:([^! ]*)![^ ]* (?i:PRIVMSG) (?i:(??{$store{irc_nick}})) :\001VERSION( .*)?\001} => sub {
1571 my $ver = $store{dp_version} or return 0;
1572 $ver .= ", rcon2irc $VERSION";
1573 out irc => 0, "NOTICE $nick :\001VERSION $ver\001";
1576 # on game start, notify the channel
1577 [ dp => q{:gamestart:(.*):[0-9.]*} => sub {
1579 $store{playing} = 1;
1581 $store{map_starttime} = time();
1582 if ($config{irc_announce_mapchange} eq 'always' || ($config{irc_announce_mapchange} eq 'notempty' && $store{slots_active} > 0)) {
1583 my $slotsstr = xon_slotsstring();
1584 out irc => 0, "PRIVMSG $config{irc_channel} :\00304" . $map . "\017 has begun$slotsstr";
1586 delete $store{lms_blocked};
1590 # on game over, clear the current map
1591 [ dp => q{:gameover} => sub {
1592 $store{playing} = 0;
1596 # scores: Xonotic server -> IRC channel (start)
1597 [ dp => q{:scores:(.*):(\d+)} => sub {
1598 my ($map, $time) = @_;
1599 $store{scores} = {};
1600 $store{scores}{map} = $map;
1601 $store{scores}{time} = $time;
1602 $store{scores}{players} = [];
1603 delete $store{lms_blocked};
1607 # scores: Xonotic server -> IRC channel, legacy format
1608 [ dp => q{:player:(-?\d+):(\d+):(\d+):(\d+):(\d+):(.*)} => sub {
1609 my ($frags, $deaths, $time, $team, $id, $name) = @_;
1610 return if not exists $store{scores};
1611 push @{$store{scores}{players}}, [$frags, $team, $name]
1612 unless $frags <= -666; # no spectators
1616 # scores: Xonotic server -> IRC channel (CTF), legacy format
1617 [ dp => q{:teamscores:(\d+:-?\d*(?::\d+:-?\d*)*)} => sub {
1619 return if not exists $store{scores};
1620 $store{scores}{teams} = {split /:/, $teams};
1624 # scores: Xonotic server -> IRC channel, new format
1625 [ dp => q{:player:see-labels:(-?\d+)[-0-9,]*:(\d+):(\d+):(\d+):(.*)} => sub {
1626 my ($frags, $time, $team, $id, $name) = @_;
1627 return if not exists $store{scores};
1628 push @{$store{scores}{players}}, [$frags, $team, $name];
1632 # scores: Xonotic server -> IRC channel (CTF), new format
1633 [ dp => q{:teamscores:see-labels:(-?\d+)[-0-9,]*:(\d+)} => sub {
1634 my ($frags, $team) = @_;
1635 return if not exists $store{scores};
1636 $store{scores}{teams}{$team} = $frags;
1640 # scores: Xonotic server -> IRC channel
1641 [ dp => q{:end} => sub {
1642 return if not exists $store{scores};
1643 my $s = $store{scores};
1644 delete $store{scores};
1645 my $teams_matter = defined $s->{teams};
1652 # put players into teams
1654 for(@{$s->{players}})
1656 my $thisteam = ($t{$_->[1]} ||= {score => 0, team => $_->[1], players => []});
1657 push @{$thisteam->{players}}, [$_->[0], $_->[1], $_->[2]];
1660 $thisteam->{score} = $s->{teams}{$_->[1]};
1664 $thisteam->{score} += $_->[0];
1668 # sort by team score
1669 @t = sort { $b->{score} <=> $a->{score} } values %t;
1671 # sort by player score
1675 @{$_->{players}} = sort { $b->[0] <=> $a->[0] } @{$_->{players}};
1676 push @p, @{$_->{players}};
1681 @p = sort { $b->[0] <=> $a->[0] } @{$s->{players}};
1684 # no display for empty server
1688 # make message fit somehow
1689 for my $maxnamelen(reverse 3..64)
1691 my $scores_string = "PRIVMSG $config{irc_channel} :\00304" . $s->{map} . "\017 ended:";
1697 $scores_string .= $sep . "\003" . $color_team2irc_table{$_->{team}}. "\002\002" . $_->{score} . "\017";
1704 my ($frags, $team, $name) = @$_;
1705 $name = color_dpfix substr($name, 0, $maxnamelen);
1708 $name = "\003" . $color_team2irc_table{$team} . " " . color_dp2none $name;
1712 $name = " " . color_dp2irc $name;
1714 $scores_string .= "$sep$name\017 $frags";
1717 if(length($scores_string) <= ($store{irc_maxlen} || 256))
1719 out irc => 0, $scores_string;
1723 out irc => 0, "PRIVMSG $config{irc_channel} :\001ACTION would have LIKED to put the scores here, but they wouldn't fit :(\001";
1727 # complain when system load gets too high
1728 [ dp => q{timing: (([0-9.]*)% CPU, ([0-9.]*)% lost, offset avg ([0-9.]*)ms, max ([0-9.]*)ms, sdev ([0-9.]*)ms)} => sub {
1729 my ($all, $cpu, $lost, $avg, $max, $sdev) = @_;
1730 return 0 # don't complain when just on the voting screen
1731 if !$store{playing};
1732 if(length $config{dp_timinglog})
1734 open my $fh, '>>', $config{dp_timinglog}
1735 or warn "open >> $config{dp_timinglog}: $!";
1736 print $fh "@{[time]} $cpu $lost $avg $max $sdev $store{slots_active}\n"
1737 or warn "print >> $config{dp_timinglog}: $!";
1739 or warn "close >> $config{dp_timinglog}: $!";
1741 return 0 # don't complain if it was less than 0.5%
1743 return 0 # don't complain if nobody is looking
1744 if $store{slots_active} == 0;
1745 return 0 # don't complain in the first two minutes
1746 if time() - $store{map_starttime} < 120;
1747 return 0 # don't complain if it was already at least half as bad in this round
1748 if $store{map_starttime} == $store{timingerror_map_starttime} and $lost <= 2 * $store{timingerror_lost};
1749 $store{timingerror_map_starttime} = $store{map_starttime};
1750 $store{timingerror_lost} = $lost;
1751 out dp => 0, 'rcon2irc_say_as server "There are currently some severe system load problems. The admins have been notified."';
1752 out irc => 1, "PRIVMSG $config{irc_channel} :\001ACTION has big trouble on $store{map} after @{[int(time() - $store{map_starttime})]}s: $all\001";
1753 #out irc => 1, "PRIVMSG OpBaI :\001ACTION has big trouble on $store{map} after @{[int(time() - $store{map_starttime})]}s: $all\001";
1760 # Load plugins and add them to the handler list in the front.
1761 for my $p(split ' ', $config{plugins})
1763 my @h = eval { do $p; }
1764 or die "Invalid plugin $p: $@";
1767 ref $_ eq 'ARRAY' or die "Invalid plugin $p: did not return a list of arrays";
1768 @$_ == 3 or die "Invalid plugin $p: did not return a list of three-element arrays";
1769 !ref $_->[0] && !ref $_->[1] && ref $_->[2] eq 'CODE' or die "Invalid plugin $p: did not return a list of string-string-sub arrays";
1770 unshift @handlers, $_;
1776 # verify that the server is up by letting it echo back a string that causes
1777 # re-initialization of the required aliases
1778 out dp => 0, 'echo "Unknown command \"rcon2irc_eval\""'; # assume the server has been restarted
1782 # regularily, query the server status and if it still is connected to us using
1783 # the log_dest_udp feature. If not, we will detect the response to this rcon
1784 # command and re-initialize the server's connection to us (either by log_dest_udp
1785 # not containing our own IP:port, or by rcon2irc_eval not being a defined command).
1788 out dp => 0, 'sv_cmd banlist', 'status 1', 'log_dest_udp', 'rcon2irc_eval set dummy 1';
1789 $store{status_waiting} = -1;
1790 schedule $timer => (exists $store{dp_hostname} ? $config{dp_status_delay} : 1);;
1795 # Continue with connecting to IRC as soon as we get our first status reply from
1796 # the DP server (which contains the server's hostname that we'll use as
1797 # realname for IRC).
1801 # log on to IRC when needed
1802 if(exists $store{dp_hostname} && !exists $store{irc_seen_welcome})
1804 $store{irc_nick_requested} = $config{irc_nick};
1805 out irc => 1, "NICK $config{irc_nick}", "USER $config{irc_user} localhost localhost :$store{dp_hostname}";
1806 $store{irc_logged_in} = 1;
1807 undef $store{irc_maxlen};
1808 undef $store{irc_pingtime};
1811 schedule $timer => 1;;
1816 # Regularily ping the IRC server to detect if the connection is down. If it is,
1817 # schedule an IRC error that will cause reconnection later.
1821 if($store{irc_logged_in})
1823 if(defined $store{irc_pingtime})
1825 # IRC connection apparently broke
1826 # so... KILL IT WITH FIRE
1827 $channels{system}->send("error irc", 0);
1831 # everything is fine, send a new ping
1832 $store{irc_pingtime} = time();
1833 out irc => 1, "PING $store{irc_pingtime}";
1837 schedule $timer => $config{irc_ping_delay};;
1845 # Build up an IO::Select object for all our channels.
1846 my $s = IO::Select->new();
1847 for my $chan(values %channels)
1849 $s->add($_) for $chan->fds();
1852 # wait for something to happen on our sockets, or wait 2 seconds without anything happening there
1854 my @errors = $s->has_exception(0);
1856 # on every channel, look for incoming messages
1858 for my $chanstr(keys %channels)
1860 my $chan = $channels{$chanstr};
1861 my @chanfds = $chan->fds();
1863 for my $chanfd(@chanfds)
1865 if(grep { $_ == $chanfd } @errors)
1867 # STOP! This channel errored!
1868 $channels{system}->send("error $chanstr", 0);
1875 for my $line($chan->recv())
1877 # found one! Check if it matches the regular expression of one of
1881 for my $h(@handlers)
1883 my ($chanstr_wanted, $re, $sub) = @$h;
1885 if $chanstr_wanted ne $chanstr;
1887 my @matches = ($line =~ /^$re$/s);
1891 # and if it is a match, handle it.
1893 my $result = $sub->(@matches);
1899 # print the message, together with info on whether it has been handled or not
1902 print " $chanstr >> (private)\n";
1906 print " $chanstr >> $line\n";
1910 print "unhandled: $chanstr >> $line\n";
1915 if($@ eq "read error\n")
1917 $channels{system}->send("error $chanstr", 0);
1928 # handle scheduled tasks...
1931 # by emptying the list of tasks...
1935 my ($time, $sub) = @$_;
1938 # calling them if they are schedled for the "past"...
1943 # or re-adding them to the task list if they still are scheduled for the "future"
1944 push @tasks, [$time, $sub];