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