4 # :title: Half-Life 2 plugin for rbot
6 # Author:: Ole Christian Rynning <oc@rynning.no>
7 # Author:: Andrew Northall <cubehat@gmail.com>
8 # Copyright:: (C) 2006 Ole Christian Rynning & Andrew Northall
11 # Simple Half-Life 2 (Source Engine) plugin to query online
12 # servers to see if its online and kicking and how many users.
14 # Added 2 seconds timeout to the response. And sockets are now
17 # Server presets can be added by using 'hl2 add name addr:port'
18 # and 'hl2 del name'. Once presets are added they are accessed
24 class HL2Plugin < Plugin
26 A2S_INFO = "\xFF\xFF\xFF\xFF\x54\x53\x6F\x75\x72\x63\x65\x20\x45\x6E\x67\x69\x6E\x65\x20\x51\x75\x65\x72\x79\x00"
30 def a2s_info(addr, port)
31 socket = UDPSocket.new()
33 socket.send(A2S_INFO, 0, addr, port.to_i)
37 response = socket.recvfrom(1400,0)
44 response ? response.first.unpack("iACZ*Z*Z*Z*sCCCaaCCZ*") : nil
47 def help(plugin, topic="")
50 return "hl2 'server:port'/'preset name' => show basic information about the given server. See also 'hl2 add' and 'hl2 del'"
52 return "hl2 add 'name' 'server:port' => add a preset."
54 return "hl2 del 'name' => remove a preset."
59 addr, port = params[:conn_str].split(':')
63 if addr.downcase == key.downcase
64 addr, port = @registry[key]
68 m.reply "invalid server" if port == nil
70 info = a2s_info(addr, port)
72 m.reply "#{info[3]} (#{info[6]}): #{info[8]}/#{info[9]} - #{info[4]}"
74 m.reply "Couldn't connect to #{params[:conn_str]}"
78 def add_server(m, params)
79 @registry[params[:name]] = params[:conn_str].split(':')
83 def rem_server(m, params)
84 if @registry.has_key?(params[:name]) == false
85 m.reply "but i don't know it!"
88 @registry.delete params[:name]
93 plugin = HL2Plugin.new
94 plugin.default_auth('edit', false)
95 plugin.map 'hl2 :conn_str', :thread => true
96 plugin.map 'hl2 add :name :conn_str', :thread => true, :action => :add_server, :auth_path => 'edit'
97 plugin.map 'hl2 del :name', :thread => true, :action => :rem_server, :auth_path => 'edit'