]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/commitdiff
Add new httputil object to the bot object, to be used by plugins etc that
authorTom Gilbert <tom@linuxbrit.co.uk>
Wed, 20 Jul 2005 00:17:51 +0000 (00:17 +0000)
committerTom Gilbert <tom@linuxbrit.co.uk>
Wed, 20 Jul 2005 00:17:51 +0000 (00:17 +0000)
wish to make http requests. It sets up all the proxies etc for them
according to bot config.

rbot/httputil.rb [new file with mode: 0644]
rbot/ircbot.rb
rbot/plugins/tube.rb

diff --git a/rbot/httputil.rb b/rbot/httputil.rb
new file mode 100644 (file)
index 0000000..000dcf9
--- /dev/null
@@ -0,0 +1,83 @@
+module Irc
+
+require 'net/http'
+Net::HTTP.version_1_2
+  
+# class for making http requests easier (mainly for plugins to use)
+# this class can check the bot proxy configuration to determine if a proxy
+# needs to be used, which includes support for per-url proxy configuration.
+class HttpUtil
+  def initialize(bot)
+    @bot = bot
+  end
+
+  # uri:: Uri to create a proxy for
+  #
+  # return a net/http Proxy object, which is configured correctly for
+  # proxying based on the bot's proxy configuration. 
+  # This will include per-url proxy configuration based on the bot config
+  # +http_proxy_include/exclude+ options.
+  def get_proxy(uri)
+    proxy = nil
+    if (ENV['http_proxy'])
+      proxy = URI.parse ENV['http_proxy']
+    end
+    if (@bot.config["http_proxy"])
+      proxy = URI.parse ENV['http_proxy']
+    end
+
+    # if http_proxy_include or http_proxy_exclude are set, then examine the
+    # uri to see if this is a proxied uri
+    if uri
+      if @bot.config["http_proxy_exclude"]
+        # TODO
+      end
+      if @bot.config["http_proxy_include"]
+      end
+    end
+    
+    proxy_host = nil
+    proxy_port = nil
+    proxy_user = nil
+    proxy_pass = nil
+    if @bot.config["http_proxy_user"]
+      proxy_user = @bot.config["http_proxy_user"]
+      if @bot.config["http_proxy_pass"]
+        proxy_pass = @bot.config["http_proxy_pass"]
+      end
+    end
+    if proxy
+      proxy_host = proxy.host
+      proxy_port = proxy.port
+    end
+    
+    return Net::HTTP.new(uri.host, uri.port, proxy_host, proxy_port, proxy_user, proxy_port)
+  end
+
+  # uri::         uri to query (Uri object)
+  # readtimeout:: timeout for reading the response
+  # opentimeout:: timeout for opening the connection
+  #
+  # simple get request, returns response body if the status code is 200 and
+  # the request doesn't timeout.
+  def get(uri, readtimeout=10, opentimeout=5)
+    proxy = get_proxy(uri)
+    proxy.open_timeout = opentimeout
+    proxy.read_timeout = readtimeout
+   
+    begin
+      proxy.start() {|http|
+        resp = http.get(uri)
+        if resp.code == "200"
+          return resp.body
+        end
+        return nil
+      }
+    rescue StandardError, Timeout::Error => e
+      $stderr.puts "HttpUtil.get exception: #{e}, while trying to get #{uri}"
+    end
+    return nil
+  end
+end
+
+end
index 823a66a6bf4762ea522afe3ab34e8579a62d7131..7129c10515176b8bc3ae2d484504996df05f1ebf 100644 (file)
@@ -34,6 +34,7 @@ require 'rbot/message'
 require 'rbot/language'
 require 'rbot/dbhash'
 require 'rbot/registry'
+require 'rbot/httputil'
 
 module Irc
 
@@ -70,6 +71,10 @@ class IrcBot
   # and restore objects in their own namespaces.)
   attr_reader :registry
 
+  # bot's httputil help object, for fetching resources via http. Sets up
+  # proxies etc as defined by the bot configuration/environment
+  attr_reader :httputil
+
   # create a new IrcBot with botclass +botclass+
   def initialize(botclass)
     @botclass = botclass.gsub(/\/$/, "")
@@ -85,6 +90,7 @@ class IrcBot
     @channels = Hash.new
     @logs = Hash.new
     
+    @httputil = Irc::HttpUtil.new(self)
     @lang = Irc::Language.new(@config["LANGUAGE"])
     @keywords = Irc::Keywords.new(self)
     @auth = Irc::IrcAuth.new(self)
@@ -468,6 +474,13 @@ class IrcBot
     return helpstr
   end
 
+  def status
+    secs_up = Time.new - @startup_time
+    uptime = Utils.secs_to_string secs_up
+    return "Uptime #{uptime}, #{@plugins.length} plugins active, #{@registry.length} items stored in registry, #{@socket.lines_sent} lines sent, #{@socket.lines_received} received."
+  end
+
+
   private
 
   # handle help requests for "core" topics
@@ -725,12 +738,6 @@ class IrcBot
        puts @channels[m.channel].topic
   end
 
-  def status
-    secs_up = Time.new - @startup_time
-    uptime = Utils.secs_to_string secs_up
-    return "Uptime #{uptime}, #{@plugins.length} plugins active, #{@registry.length} items stored in registry, #{@socket.lines_sent} lines sent, #{@socket.lines_received} received."
-  end
-
   # delegate a privmsg to auth, keyword or plugin handlers
   def delegate_privmsg(message)
     [@auth, @plugins, @keywords].each {|m|
index 0244df64f5c5a7b17c8934d133486718d240b3ba..98b90c6c90c4a4863e766da0b178641580f2ab52 100644 (file)
@@ -20,7 +20,7 @@ class TubePlugin < Plugin
   
   def check_tube(m, line)
   begin
-    tube_page = Utils.http_get("http://www.tfl.gov.uk/tfl/service_rt_tube.shtml")
+    tube_page = @bot.httputil.get(URI.parse("http://www.tfl.gov.uk/tfl/service_rt_tube.shtml"), 1, 1)
   rescue URI::InvalidURIError, URI::BadURIError => e
     m.reply "Cannot contact Tube Service Status page"
     return