#!/usr/local/bin/perl -w # Program to ask battle.net if a user is online # Written by John D. Lewis # Version: 1.0 # # bnet-online.pl username [server] [username password] # # Tested on WinNT machine with ActiveState Perl 5.005_02 $|++; use IO::Socket; ## ---------------------------------------------------------- ## -- Edit these values to customize how this program runs -- ## ---------------------------------------------------------- my $SLEEP_SECONDS = 8; my $VERBOSE = 0; # Set to "1" if you want to see all of Battle.net's output my $MAX_RECONNECT_TRIES = 5; # Don't try to reconnect more than this many times my $DEFAULT_USERNAME_TO_QUERY = "someone"; # If not specified on the command-line, use this ID my $LOGIN_USERNAME = "guest"; # better netizen logging in as guest # But you may specify a registered user (and password) #my $LOGIN_USERNAME = "reguser"; #my $LOGIN_PASSWORD = "somethingclever"; ## --------------------------------------------------- ## ------- DON'T EDIT ANYTHING BELOW THIS LINE ------- ## --------------------------------------------------- my $DEFAULT_SERVER = "exodus.battle.net"; my $DEFAULT_PORT = 6112; my $MAX_LOOP_COUNT = 20; my $POST_LOGON_HEADER_LINE_COUNT = 5; my $LOGON_HEADER_LINE_COUNT = 4; # Person's name must be supplied on command line, or # default will be used my $who = shift || $DEFAULT_USERNAME_TO_QUERY; my $server = shift || $DEFAULT_SERVER; my $port = $DEFAULT_PORT; my $socket = &login($server, $port); my $x; for ($x = 0; $x <= $LOGON_HEADER_LINE_COUNT; $x++) { $from_server = <$socket>; print "$from_server" if ($VERBOSE); }; print "Logged on...\n" unless ($VERBOSE); print $socket "\n"; print $socket "$LOGIN_USERNAME\n"; $from_server = <$socket>; #print "$from_server\n"; if ($LOGIN_USERNAME !~ /guest/i) { print $socket "$LOGIN_USERNAME\n"; $from_server = <$socket>; # Password: prompt $from_server = <$socket>; # empty line print $socket "\n"; } else { print $socket "\n"; $from_server = <$socket>; # empty line $from_server = <$socket>; # empty line }; &readUntilInfoDone(); my $join_command = "/join temp_channelXY123"; my $reply = 0; print $socket "$join_command\n"; while ($from_server = <$socket>) { last if ($reply); $reply++ if ($from_server =~ /^$join_command/); }; print "$from_server\n" if ($VERBOSE); my $command = "/whois $who"; print "$command: reply = "; print $socket "$command\n\n"; do { # ignore all the other junk $from_socket = <$socket>; # repeats command } until ($from_socket =~ /^$command/); $from_socket = <$socket>; # real response from server print "$from_socket"; close($socket); 1; sub readUntilInfoDone($?) { my $INFO_LINE_COUNT = shift || 7; my $line_count = 0; while (my $from_server = <$socket>) { next if ($from_server =~ /^100[123]/); my ($code_num, $code_txt, $text) = split /\s/, $from_server, 3; chomp($text); $text =~ s/^\s*\"(.+)\"\s*$/$1/; print "[$code_txt] $text\n" if ($VERBOSE); last if ++$line_count >= $INFO_LINE_COUNT; } }; sub login($$$?) { my $remote_host = shift; my $remote_port = shift; my $conn_attempt = shift || 1; return() if ($conn_attempt >= $MAX_RECONNECT_TRIES); my $args = "c"; my $from_server; $socket = IO::Socket::INET->new(PeerAddr => $remote_host, PeerPort => $remote_port, Proto => "tcp", Type => SOCK_STREAM); print $socket "$args\n"; $from_server = <$socket>; if ($from_server =~ /refused/) { print STDERR "Connection refused (try #" . $conn_attempt . ")! Trying again in $SLEEP_SECONDS seconds...\n"; sleep($SLEEP_SECONDS); undef $from_server; undef $socket; return(&login($remote_host, $remote_port, ++$conn_attempt)); }; return($socket); };