]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - data/rbot/plugins/hl2.rb
script plugin: script echo and eval mark the message as replied
[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 # Copyright:: (C) 2006 Ole Christian Rynning
8 # License:: GPL v2
9 #
10 # Simple Half-Life 2 (Source Engine) plugin to query online
11 # servers to see if its online and kicking and how many users.
12 #
13 # Added 2 seconds timeout to the response. And sockets are now
14 # closing properly.
15
16 require 'socket'
17 require 'timeout'
18
19 class HL2Plugin < Plugin
20
21   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"
22
23   TIMEOUT = 2
24
25   def a2s_info(addr, port)
26     socket = UDPSocket.new()
27     socket.send(A2S_INFO, 0, addr, port.to_i)
28     response = nil
29
30     begin
31       timeout(TIMEOUT) do
32         response = socket.recvfrom(1400,0)
33       end
34     rescue Exception
35     end
36
37     socket.close()
38     response ? response.first.unpack("iACZ*Z*Z*Z*sCCCaaCCZ*") : nil
39   end
40
41   def help(plugin, topic="")
42     "hl2 'server:port' => show basic information about the given server"
43   end
44
45   def hl2(m, params)
46     addr, port = params[:conn_str].split(':')
47     info = a2s_info(addr, port)
48     if info != nil
49       m.reply "#{info[3]} is online with #{info[8]}/#{info[9]} players."
50     else
51       m.reply "Couldn't connect to #{params[:conn_str]}"
52     end
53   end
54
55 end
56
57 plugin = HL2Plugin.new
58 plugin.map 'hl2 :conn_str', :thread => true
59