]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - rbot/plugins/wserver.rb
eb4effaffc6f0a7940372f332e74f61644614929
[user/henk/code/ruby/rbot.git] / 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     proxy_host = nil
16     proxy_port = nil
17
18     if(ENV['http_proxy'])
19       if(ENV['http_proxy'] =~ /^http:\/\/(.+):(\d+)$/)
20         hh = $1
21         pp = $2
22         unless(m.params =~ /\.db\.com/ || m.params =~ /\.deuba\.com/)
23           proxy_host = hh
24           proxy_port = pp
25         end
26       end
27     end
28
29     redirect_count = 0
30     hostname = m.params.dup
31     begin
32       if(redirect_count > 3)
33         m.reply "cowardly refusing to follow more than 3 redirects"
34         return
35       end
36       
37       begin
38         uri = URI.parse(hostname)
39       rescue URI::InvalidURIError => err
40         m.reply "#{m.params} is not a valid URI"
41         return
42       end
43       
44       unless(uri)
45         m.reply "incorrect usage: " + help(m.plugin)
46         return
47       end
48       if(uri.scheme == "https")
49         m.reply "#{uri.scheme} not supported"
50         return
51       end
52         
53       host = uri.host ? uri.host : hostname
54       port = uri.port ? uri.port : 80
55       path = '/'
56       if(uri.scheme == "http")
57         path = uri.path if uri.path
58       end
59     
60       http = Net::HTTP.new(host, port, proxy_host, proxy_port)
61       http.open_timeout = 5
62       
63       http.start {|http|
64         resp = http.head(path)
65         result = host
66         server = resp['Server']
67         if(server && server.length > 0)
68           m.reply "#{host} is running #{server}"
69         else
70           m.reply "couldn't tell what #{host} is running"
71         end
72         
73         if(resp.code == "302" || resp.code == "301") 
74           if(host != URI.parse(resp['location']).host)
75             m.reply "#{host} redirects to #{resp['location']}"
76             raise resp['location']
77           end
78         end
79       }
80     rescue TimeoutError => err
81       m.reply "timed out connecting to #{host}:#{port} :("
82       return
83     rescue RuntimeError => err
84       redirect_count += 1
85       hostname = err.message
86       retry
87     rescue StandardError => err
88       m.reply "couldn't connect to #{host}:#{port} :("
89       return
90     end
91   end
92 end
93 plugin = WserverPlugin.new
94 plugin.register("wserver")