geoip plugin: only retrieve host info once on WHOIS
[rbot] / data / rbot / plugins / wserver.rb
1 class WserverPlugin < Plugin
2   def help(plugin, topic="")
3     "wserver <uri> => try and determine what webserver <uri> is using"
4   end
5
6   def wserver(m, params)
7     redirect_count = 0
8     hostname = params[:host].dup
9     hostname = "http://#{hostname}" unless hostname =~ /:\/\//
10     begin
11       if(redirect_count > 3)
12         m.reply "cowardly refusing to follow more than 3 redirects"
13         return
14       end
15       
16       begin
17         uri = URI.parse(hostname)
18       rescue URI::InvalidURIError => err
19         m.reply "#{hostname} is not a valid URI"
20         return
21       end
22       
23       unless(uri)
24         m.reply "incorrect usage: " + help(m.plugin)
25         return
26       end
27         
28       
29       resp = @bot.httputil.head(uri)
30       server = resp['Server']
31       if(server && server.length > 0)
32         m.reply "#{uri.host} is running #{server}"
33       else
34         m.reply "couldn't tell what #{uri.host} is running"
35       end
36
37       if(resp.code == "302" || resp.code == "301") 
38         newloc = resp['location']
39         newuri = URI.parse(newloc)
40         # detect and ignore incorrect redirects (to relative paths etc)
41         if (newuri.host != nil)
42           if(uri.host != newuri.host)
43             m.reply "#{uri.host} redirects to #{newuri.scheme}://#{newuri.host}"
44             raise resp['location']
45           end
46         end
47       end
48     rescue TimeoutError => err
49       m.reply "timed out connecting to #{uri.host}:#{uri.port} :("
50       return
51     rescue RuntimeError => err
52       redirect_count += 1
53       hostname = err.message
54       retry
55     rescue StandardError => err
56       error err.inspect
57       m.reply "couldn't connect to #{uri.host}:#{uri.port} :("
58       return
59     end
60   end
61 end
62 plugin = WserverPlugin.new
63 plugin.map 'wserver :host'