]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - data/rbot/plugins/wserver.rb
Fri Jul 29 13:07:56 BST 2005 Tom Gilbert <tom@linuxbrit.co.uk>
[user/henk/code/ruby/rbot.git] / data / rbot / plugins / wserver.rb
1 require 'net/http'
2 require 'uri'
3 Net::HTTP.version_1_2
4
5 class WserverPlugin < Plugin
6   def help(plugin, topic="")
7     "wserver <uri> => try and determine what webserver <uri> is using"
8   end
9
10   def wserver(m, params)
11     redirect_count = 0
12     hostname = params[:host].dup
13     hostname = "http://#{hostname}" unless hostname =~ /:\/\//
14     begin
15       if(redirect_count > 3)
16         m.reply "cowardly refusing to follow more than 3 redirects"
17         return
18       end
19       
20       begin
21         uri = URI.parse(hostname)
22       rescue URI::InvalidURIError => err
23         m.reply "#{hostname} is not a valid URI"
24         return
25       end
26       
27       unless(uri)
28         m.reply "incorrect usage: " + help(m.plugin)
29         return
30       end
31         
32       http = @bot.httputil.get_proxy(uri)
33       http.open_timeout = 5
34       
35       http.start {|http|
36         resp = http.head('/')
37         server = resp['Server']
38         if(server && server.length > 0)
39           m.reply "#{uri.host} is running #{server}"
40         else
41           m.reply "couldn't tell what #{uri.host} is running"
42         end
43         
44         if(resp.code == "302" || resp.code == "301") 
45           newloc = resp['location']
46           newuri = URI.parse(newloc)
47           # detect and ignore incorrect redirects (to relative paths etc)
48           if (newuri.host != nil)
49             if(uri.host != newuri.host)
50               m.reply "#{uri.host} redirects to #{newuri.scheme}://#{newuri.host}"
51               raise resp['location']
52             end
53           end
54         end
55       }
56     rescue TimeoutError => err
57       m.reply "timed out connecting to #{uri.host}:#{uri.port} :("
58       return
59     rescue RuntimeError => err
60       redirect_count += 1
61       hostname = err.message
62       retry
63     rescue StandardError => err
64       puts err
65       m.reply "couldn't connect to #{uri.host}:#{uri.port} :("
66       return
67     end
68   end
69 end
70 plugin = WserverPlugin.new
71 plugin.map 'wserver :host'