]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - data/rbot/plugins/wserver.rb
imdb plugin: search on the 'aka' sites, allowing the correct movie to be found even...
[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       
33       resp = @bot.httputil.head(uri)
34       server = resp['Server']
35       if(server && server.length > 0)
36         m.reply "#{uri.host} is running #{server}"
37       else
38         m.reply "couldn't tell what #{uri.host} is running"
39       end
40
41       if(resp.code == "302" || resp.code == "301") 
42         newloc = resp['location']
43         newuri = URI.parse(newloc)
44         # detect and ignore incorrect redirects (to relative paths etc)
45         if (newuri.host != nil)
46           if(uri.host != newuri.host)
47             m.reply "#{uri.host} redirects to #{newuri.scheme}://#{newuri.host}"
48             raise resp['location']
49           end
50         end
51       end
52     rescue TimeoutError => err
53       m.reply "timed out connecting to #{uri.host}:#{uri.port} :("
54       return
55     rescue RuntimeError => err
56       redirect_count += 1
57       hostname = err.message
58       retry
59     rescue StandardError => err
60       error err.inspect
61       m.reply "couldn't connect to #{uri.host}:#{uri.port} :("
62       return
63     end
64   end
65 end
66 plugin = WserverPlugin.new
67 plugin.map 'wserver :host'