Skip to content

Instantly share code, notes, and snippets.

@nTraum
Created August 20, 2012 22:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nTraum/3408627 to your computer and use it in GitHub Desktop.
Save nTraum/3408627 to your computer and use it in GitHub Desktop.
rubycon
require 'steam-condenser'
require 'readline'
Prompt = '> '
Completion_char = ' '
Cvarlist_cmd = 'cvarlist'
def get_for_server_info
address = "localhost"
port = 27015
password= ''
print "Host [#{address}]: "
input = gets.chomp
address = input unless input.empty?
print "Port [#{port}]: "
input = gets.chomp
port = input.to_i unless input.empty?
print "Rcon [#{password}]: "
password = gets.chomp;
password = input unless input.empty?
return address, port, password
end
def prompt_forever server, password
def readline_with_hist_management
line = Readline.readline(Prompt, true)
return nil if line.nil?
if line =~ /^\s*$/ or Readline::HISTORY.to_a[-2] == line
Readline::HISTORY.pop
end
line
end
def auth_with server, password
server.rcon_auth password unless server.rcon_authenticated?
abort 'Bad rcon password!' unless server.rcon_authenticated?
end
def get_cvars server, password
list = []
auth_with server, password
output = server.rcon_exec Cvarlist_cmd
output.lines{|l| list << l.sub(/:.*$/, '').strip if l.include? ':'}
list
end
auth_with server, password
cvars = get_cvars server, password
comp = proc {|s| cvars.grep( /^#{Regexp.escape(s)}/ ) }
Readline.completion_proc = comp
Readline.completion_append_character = Completion_char
while line = readline_with_hist_management
server.rcon_auth password unless server.rcon_authenticated?
abort 'Bad rcon password! Exiting...' unless server.rcon_authenticated?
puts server.rcon_exec line unless line.empty?
end
end
begin
address, port, password = get_for_server_info
server = SourceServer.new(address, port)
prompt_forever server, password
rescue Interrupt
exit
rescue Errno::ECONNREFUSED, SocketError
abort "Connection refused (#{address}:#{port}). Exiting..."
rescue SteamCondenser::TimeoutError
abort "Server timed out."
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment