]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blobdiff - data/rbot/plugins/wserver.rb
*** (httputil) major rework, new caching implementation, unified request
[user/henk/code/ruby/rbot.git] / data / rbot / plugins / wserver.rb
index e1fe10bd341f1eafc675659c566612c3d4db8793..fdb4207dfe1d005ba021d04ca8cccc12c4e2facc 100644 (file)
@@ -6,14 +6,10 @@ class WserverPlugin < Plugin
   def help(plugin, topic="")
     "wserver <uri> => try and determine what webserver <uri> is using"
   end
-  def privmsg(m)
-    unless(m.params && m.params =~ /^\S+$/)
-      m.reply "incorrect usage: " + help(m.plugins)
-      return
-    end
 
+  def wserver(m, params)
     redirect_count = 0
-    hostname = m.params.dup
+    hostname = params[:host].dup
     hostname = "http://#{hostname}" unless hostname =~ /:\/\//
     begin
       if(redirect_count > 3)
@@ -24,7 +20,7 @@ class WserverPlugin < Plugin
       begin
         uri = URI.parse(hostname)
       rescue URI::InvalidURIError => err
-        m.reply "#{m.params} is not a valid URI"
+        m.reply "#{hostname} is not a valid URI"
         return
       end
       
@@ -33,30 +29,26 @@ class WserverPlugin < Plugin
         return
       end
         
-      http = @bot.httputil.get_proxy(uri)
-      http.open_timeout = 5
       
-      http.start {|http|
-        resp = http.head('/')
-        server = resp['Server']
-        if(server && server.length > 0)
-          m.reply "#{uri.host} is running #{server}"
-        else
-          m.reply "couldn't tell what #{uri.host} is running"
-        end
-        
-        if(resp.code == "302" || resp.code == "301") 
-          newloc = resp['location']
-          newuri = URI.parse(newloc)
-          # detect and ignore incorrect redirects (to relative paths etc)
-          if (newuri.host != nil)
-            if(uri.host != newuri.host)
-              m.reply "#{uri.host} redirects to #{newuri.scheme}://#{newuri.host}"
-              raise resp['location']
-            end
+      resp = @bot.httputil.head(uri)
+      server = resp['Server']
+      if(server && server.length > 0)
+        m.reply "#{uri.host} is running #{server}"
+      else
+        m.reply "couldn't tell what #{uri.host} is running"
+      end
+
+      if(resp.code == "302" || resp.code == "301") 
+        newloc = resp['location']
+        newuri = URI.parse(newloc)
+        # detect and ignore incorrect redirects (to relative paths etc)
+        if (newuri.host != nil)
+          if(uri.host != newuri.host)
+            m.reply "#{uri.host} redirects to #{newuri.scheme}://#{newuri.host}"
+            raise resp['location']
           end
         end
-      }
+      end
     rescue TimeoutError => err
       m.reply "timed out connecting to #{uri.host}:#{uri.port} :("
       return
@@ -65,11 +57,11 @@ class WserverPlugin < Plugin
       hostname = err.message
       retry
     rescue StandardError => err
-      puts err
+      error err.inspect
       m.reply "couldn't connect to #{uri.host}:#{uri.port} :("
       return
     end
   end
 end
 plugin = WserverPlugin.new
-plugin.register("wserver")
+plugin.map 'wserver :host'