5 class WserverPlugin < Plugin
6 def help(plugin, topic="")
7 "wserver <uri> => try and determine what webserver <uri> is using"
10 def wserver(m, params)
12 hostname = params[:host].dup
13 hostname = "http://#{hostname}" unless hostname =~ /:\/\//
15 if(redirect_count > 3)
16 m.reply "cowardly refusing to follow more than 3 redirects"
21 uri = URI.parse(hostname)
22 rescue URI::InvalidURIError => err
23 m.reply "#{hostname} is not a valid URI"
28 m.reply "incorrect usage: " + help(m.plugin)
33 resp = @bot.httputil.head(uri)
34 server = resp['Server']
35 if(server && server.length > 0)
36 m.reply "#{uri.host} is running #{server}"
38 m.reply "couldn't tell what #{uri.host} is running"
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']
52 rescue TimeoutError => err
53 m.reply "timed out connecting to #{uri.host}:#{uri.port} :("
55 rescue RuntimeError => err
57 hostname = err.message
59 rescue StandardError => err
61 m.reply "couldn't connect to #{uri.host}:#{uri.port} :("
66 plugin = WserverPlugin.new
67 plugin.map 'wserver :host'