Guest User

Untitled

a guest
Sep 4th, 2018
1,280
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 3.75 KB | None | 0 0
  1. #!/usr/local/bin/perl -w
  2. # Program to ask battle.net if a user is online
  3. # Written by John D. Lewis <garlon@yahoo.com>
  4. # Version: 1.0
  5. #
  6. # bnet-online.pl username [server] [username password]
  7. #
  8. # Tested on WinNT machine with ActiveState Perl 5.005_02
  9.  
  10. $|++;
  11.  
  12. use IO::Socket;
  13.  
  14. ## ----------------------------------------------------------
  15. ## -- Edit these values to customize how this program runs --
  16. ## ----------------------------------------------------------
  17. my $SLEEP_SECONDS = 8;
  18. my $VERBOSE = 0; # Set to "1" if you want to see all of Battle.net's output
  19. my $MAX_RECONNECT_TRIES = 5; # Don't try to reconnect more than this many times
  20. my $DEFAULT_USERNAME_TO_QUERY = "someone"; # If not specified on the command-line, use this ID
  21. my $LOGIN_USERNAME = "guest"; # better netizen logging in as guest
  22. # But you may specify a registered user (and password)
  23. #my $LOGIN_USERNAME = "reguser";
  24. #my $LOGIN_PASSWORD = "somethingclever";
  25.  
  26. ## ---------------------------------------------------
  27. ## ------- DON'T EDIT ANYTHING BELOW THIS LINE -------
  28. ## ---------------------------------------------------
  29. my $DEFAULT_SERVER = "exodus.battle.net";
  30. my $DEFAULT_PORT = 6112;
  31. my $MAX_LOOP_COUNT = 20;
  32. my $POST_LOGON_HEADER_LINE_COUNT = 5;
  33. my $LOGON_HEADER_LINE_COUNT = 4;
  34.  
  35. # Person's name must be supplied on command line, or
  36. # default will be used
  37. my $who = shift || $DEFAULT_USERNAME_TO_QUERY;
  38.  
  39. my $server = shift || $DEFAULT_SERVER;
  40. my $port = $DEFAULT_PORT;
  41.  
  42. my $socket = &login($server, $port);
  43.  
  44. my $x;
  45.  
  46. for ($x = 0; $x <= $LOGON_HEADER_LINE_COUNT; $x++) {
  47.     $from_server = <$socket>;
  48.     print "$from_server" if ($VERBOSE);
  49. };
  50.  
  51. print "Logged on...\n" unless ($VERBOSE);
  52.  
  53. print $socket "\n";
  54. print $socket "$LOGIN_USERNAME\n";
  55.  
  56. $from_server = <$socket>;
  57. #print "$from_server\n";
  58. if ($LOGIN_USERNAME !~ /guest/i) {
  59.     print $socket "$LOGIN_USERNAME\n";
  60.     $from_server = <$socket>; # Password: prompt
  61.     $from_server = <$socket>; # empty line
  62.     print $socket "\n";
  63. } else {
  64.     print $socket "\n";
  65.     $from_server = <$socket>; # empty line
  66.     $from_server = <$socket>; # empty line
  67. };
  68.  
  69. &readUntilInfoDone();
  70.  
  71. my $join_command = "/join temp_channelXY123";
  72. my $reply = 0;
  73. print $socket "$join_command\n";
  74. while ($from_server = <$socket>) {
  75.     last if ($reply);
  76.     $reply++ if ($from_server =~ /^$join_command/);
  77. };
  78. print "$from_server\n" if ($VERBOSE);
  79.  
  80. my $command = "/whois $who";
  81. print "$command: reply = ";
  82. print $socket "$command\n\n";
  83. do {
  84.     # ignore all the other junk
  85.     $from_socket = <$socket>; # repeats command
  86. } until ($from_socket =~ /^$command/);
  87. $from_socket = <$socket>; # real response from server
  88. print "$from_socket";
  89.  
  90. close($socket);
  91.  
  92. 1;
  93.  
  94. sub readUntilInfoDone($?) {
  95.     my $INFO_LINE_COUNT = shift || 7;
  96.     my $line_count = 0;
  97.     while (my $from_server = <$socket>) {
  98.         next if ($from_server =~ /^100[123]/);
  99.         my ($code_num, $code_txt, $text) = split /\s/, $from_server, 3;
  100.         chomp($text);
  101.         $text =~ s/^\s*\"(.+)\"\s*$/$1/;
  102.         print "[$code_txt] $text\n" if ($VERBOSE);
  103.         last if ++$line_count >= $INFO_LINE_COUNT;
  104.     }
  105. };
  106.  
  107. sub login($$$?) {
  108.     my $remote_host = shift;
  109.     my $remote_port = shift;
  110.  
  111.     my $conn_attempt = shift || 1;
  112.    
  113.     return() if ($conn_attempt >= $MAX_RECONNECT_TRIES);
  114.    
  115.     my $args = "c";
  116.     my $from_server;
  117.    
  118.     $socket = IO::Socket::INET->new(PeerAddr => $remote_host,
  119.                                     PeerPort => $remote_port,
  120.                                     Proto    => "tcp",
  121.                                     Type     => SOCK_STREAM);
  122.  
  123.     print $socket "$args\n";
  124.     $from_server = <$socket>;
  125.  
  126.     if ($from_server =~ /refused/) {
  127.         print STDERR "Connection refused (try #" . $conn_attempt . ")! Trying again in $SLEEP_SECONDS seconds...\n";
  128.         sleep($SLEEP_SECONDS);
  129.         undef $from_server;
  130.         undef $socket;
  131.         return(&login($remote_host, $remote_port, ++$conn_attempt));
  132.     };
  133.     return($socket);
  134. };
Add Comment
Please, Sign In to add comment