]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - lib/rbot/plugins/wserver.rb
move rbot into lib - still rearranging for packaging/installation
[user/henk/code/ruby/rbot.git] / lib / 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   def privmsg(m)
10     unless(m.params && m.params =~ /^\S+$/)
11       m.reply "incorrect usage: " + help(m.plugins)
12       return
13     end
14
15     redirect_count = 0
16     hostname = m.params.dup
17     hostname = "http://#{hostname}" unless hostname =~ /:\/\//
18     begin
19       if(redirect_count > 3)
20         m.reply "cowardly refusing to follow more than 3 redirects"
21         return
22       end
23       
24       begin
25         uri = URI.parse(hostname)
26       rescue URI::InvalidURIError => err
27         m.reply "#{m.params} is not a valid URI"
28         return
29       end
30       
31       unless(uri)
32         m.reply "incorrect usage: " + help(m.plugin)
33         return
34       end
35         
36       http = @bot.httputil.get_proxy(uri)
37       http.open_timeout = 5
38       
39       http.start {|http|
40         resp = http.head('/')
41         server = resp['Server']
42         if(server && server.length > 0)
43           m.reply "#{uri.host} is running #{server}"
44         else
45           m.reply "couldn't tell what #{uri.host} is running"
46         end
47         
48         if(resp.code == "302" || resp.code == "301") 
49           newloc = resp['location']
50           newuri = URI.parse(newloc)
51           # detect and ignore incorrect redirects (to relative paths etc)
52           if (newuri.host != nil)
53             if(uri.host != newuri.host)
54               m.reply "#{uri.host} redirects to #{newuri.scheme}://#{newuri.host}"
55               raise resp['location']
56             end
57           end
58         end
59       }
60     rescue TimeoutError => err
61       m.reply "timed out connecting to #{uri.host}:#{uri.port} :("
62       return
63     rescue RuntimeError => err
64       redirect_count += 1
65       hostname = err.message
66       retry
67     rescue StandardError => err
68       puts err
69       m.reply "couldn't connect to #{uri.host}:#{uri.port} :("
70       return
71     end
72   end
73 end
74 plugin = WserverPlugin.new
75 plugin.register("wserver")