#!/usr/bin/ruby require "telnet_patched.rb" class ElTelnet def initialize() @telnet = Net::Telnet_Patched::new( "Host" => $config.opt["host"], "Port" => $config.opt["port"], "Binmode" => $config.opt["binmode"], "Output_log" => $config.opt["output_log"], "Dump_log" => $config.opt["dump_log"], "Prompt" => $config.opt["prompt"], "Telnetmode" => $config.opt["telnetmode"], "Timeout" => $config.opt["timeout"], "Waittime" => $config.opt["waittime"] ) end def send_and_rec(string) @telnet.write(string) begin recv = @telnet.waitfor("String" => "") rescue Exception => e # puts e.backtrace.join('\n') print "Hrm, had to swallow an error because we timed out waiting for data.\n" recv = " " end return recv end def login() # Wait for our first prompt resp = @telnet.waitfor("String" => "=>") # Send the login command print "Got our prompt\n" resp = send_and_rec("l") print "Logging in...\n" # If resp contains "Username" then continue if (resp.include? "Please enter your username.") print "Sending username\n" resp = send_and_rec($config.opt["name"] + "\n") # If all is good, enter the password if (resp.include? "Please enter your password.") print "Sending password\n" resp = send_and_rec($config.opt["pass"] + "\n") # If we succeeded, return true if (resp.include? "Logged in as:") print "Logged in successfully!\n" return true else print "Bad login credentials. Please check the username/password and try again.\n" end end end # If we got this far, something failed, and we couldn't log in. print "Couldn't log in to Nethack server.\n" return false end def startgame() resp = send_and_rec("p") if (resp.include? "Shall I pick a character's race, role, gender and alignment for you?") resp = send_and_rec("y") if (resp.include? "Pick a role for your character") resp = send_and_rec($config.opt["role"]) end if (resp.include? "Pick the race of your") resp = send_and_rec($config.opt["race"]) end if (resp.include? "Pick the gender of your") resp = send_and_rec($config.opt["gender"]) end if (resp.include? "Pick the alignment of your") resp = send_and_rec($config.opt["align"]) end if (resp.include? "--More--") print "Successfully started a game!\n"; resp = send_and_rec(" "); end end end def sendrandmove() possible_moves = [" . ", "h ", "j ", "k ", "l ", "H ", "J ", "K ", "L ", "Fh ", "Fj ", "Fk ", "Fl ", "Fh ", "Fj ", "Fk ", "Fl ", "_>. > ", "_#. ", "_#. ", "_#. ", "_#. ", "_#. ", "_|. ", "_-. "] next_move = possible_moves[rand(possible_moves.length)] print "Sending next move of '#{next_move}'\n" resp = send_and_rec(next_move) return resp end def quitgame() resp = send_and_rec(" #quit\ny "); end end