]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - data/rbot/plugins/hl2.rb
plugin(search): fix wolfram and gdef, removed some
[user/henk/code/ruby/rbot.git] / data / rbot / plugins / hl2.rb
1 #-- vim:sw=2:et
2 #++
3 #
4 # :title: Half-Life 2 plugin for rbot
5 #
6 # Author:: Ole Christian Rynning <oc@rynning.no>
7 # Author:: Andrew Northall <cubehat@gmail.com>
8 # Copyright:: (C) 2006 Ole Christian Rynning & Andrew Northall
9 # License:: GPL v2
10 #
11 # Simple Half-Life 2 (Source Engine) plugin to query online
12 # servers to see if its online and kicking and how many users.
13 #
14 # Added 2 seconds timeout to the response. And sockets are now
15 # closing properly.
16 #
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
19 # as 'hl2 name'.
20
21 require 'socket'
22 require 'timeout'
23
24 class HL2Plugin < Plugin
25
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"
27
28   TIMEOUT = 2
29
30   def a2s_info(addr, port)
31     socket = UDPSocket.new()
32     begin
33       socket.send(A2S_INFO, 0, addr, port.to_i)
34       response = nil
35
36       timeout(TIMEOUT) do
37         response = socket.recvfrom(1400,0)
38       end
39     rescue Exception => e
40       error e
41     end
42
43     socket.close()
44     response ? response.first.unpack("iACZ*Z*Z*Z*sCCCaaCCZ*") : nil
45   end
46
47   def help(plugin, topic="")
48     case topic
49     when ""
50       return "hl2 'server:port'/'preset name' => show basic information about the given server. See also 'hl2 add' and 'hl2 del'"
51     when "add"
52       return "hl2 add 'name' 'server:port' => add a preset."
53     when "del"
54       return "hl2 del 'name' => remove a preset."
55     end
56   end
57
58   def hl2(m, params)
59     addr, port = params[:conn_str].split(':')
60     if port == nil
61       @registry.each_key do
62         |key|
63         if addr.downcase == key.downcase
64           addr, port = @registry[key]
65         end
66       end
67     end
68     m.reply "invalid server" if port == nil
69     return if port == nil
70     info = a2s_info(addr, port)
71     if info
72       m.reply "#{info[3]} (#{info[6]}): #{info[8]}/#{info[9]} - #{info[4]}"
73     else
74       m.reply "Couldn't connect to #{params[:conn_str]}"
75     end
76   end
77
78   def add_server(m, params)
79     @registry[params[:name]] = params[:conn_str].split(':')
80     m.okay
81   end
82
83   def rem_server(m, params)
84     if @registry.has_key?(params[:name]) == false
85       m.reply "but i don't know it!"
86       return
87     end
88     @registry.delete params[:name]
89     m.okay
90   end
91 end
92
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'