Debian Codegraph @

tools/quake3/q3map2/listen.pl

raw
1 #!/usr/bin/perl -w
2
3 use IO::Socket;
4 use Net::hostent;
5
6 my $port = shift || 13131;
7
8 my $server = IO::Socket::INET->new(
9 Proto => 'tcp',
10 LocalPort => $port,
11 Listen => SOMAXCONN,
12 Reuse => 1 )
13 || die "can't setup server";
14 print "[Q3Map2 listener $0 is now active on port $port]\n";
15
16 while( $client = $server->accept() )
17 {
18
19 $client->autoflush( 1 );
20
21 $hostinfo = gethostbyaddr( $client->peeraddr );
22 printf "[Connect from %s]\n\n", $hostinfo ? $hostinfo->name : $client->peerhost;
23
24 #ask the client for a command
25 print $client "[server]\$";
26 my $text = "";
27 while( <$client> )
28 {
29 $text .= $_;
30 while( $text =~ s|<message[^>]*>([^<]+)</message>|| )
31 {
32 my $msg = $1;
33
34 # fix xml ents
35 $msg =~ s|&lt;|<|g;
36 $msg =~ s|&gt;|>|g;
37 $msg =~ s|&quot;|"|g;#"
38 $msg =~ s|&apos;|'|g;#'
39
40 print $msg;
41 }
42 }
43
44 printf "\n[Disconnected: %s]\n\n", $hostinfo ? $hostinfo->name : $client->peerhost;
45 close $client;
46 }