]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/commitdiff
[agent] wip core mechanize agent plugin
authorMatthias H <apoc@sixserv.org>
Mon, 24 Feb 2014 00:15:13 +0000 (01:15 +0100)
committerMatthias H <apoc@sixserv.org>
Mon, 24 Feb 2014 00:15:13 +0000 (01:15 +0100)
lib/rbot/core/utils/agent.rb [new file with mode: 0644]
lib/rbot/ircbot.rb

diff --git a/lib/rbot/core/utils/agent.rb b/lib/rbot/core/utils/agent.rb
new file mode 100644 (file)
index 0000000..fc73c28
--- /dev/null
@@ -0,0 +1,58 @@
+# encoding: UTF-8
+#-- vim:sw=2:et
+#++
+#
+# :title: Mechanize Agent Factory
+#
+# Author:: Matthias Hecker <apoc@sixserv.org>
+#
+# Central repository for Mechanize agent instances, creates
+# pre-configured agents, allows for persistent caching,
+# cookie and page serialization.
+
+require 'mechanize'
+
+module ::Irc
+module Utils
+
+class AgentFactory
+  Bot::Config.register Bot::Config::IntegerValue.new('agent.max_redir',
+    :default => 5,
+    :desc => "Maximum number of redirections to be used when getting a document")
+
+  def initialize(bot)
+    @bot = bot
+  end
+
+  def cleanup
+  end
+
+  # Returns a new, unique instance of Mechanize.
+  def get_instance
+    agent = Mechanize.new
+    agent.redirection_limit = @bot.config['agent.max_redir']
+
+    agent
+  end
+end
+
+end # Utils
+end # Irc
+
+class AgentPlugin < CoreBotModule
+  def initialize(*a)
+    super(*a)
+    debug 'initializing agent factory'
+    @bot.agent = Irc::Utils::AgentFactory.new(@bot)
+  end
+
+  def cleanup
+    debug 'shutting down agent factory'
+    @bot.agent.cleanup
+    @bot.agent = nil
+    super
+  end
+end
+
+AgentPlugin.new
+
index a342e8c136151c0ed2e26422f3930a0ecd9f91db..ea6a57c80be31fc6e0b5b0640111fbc61bea31e9 100644 (file)
@@ -195,6 +195,9 @@ class Bot
   # proxies etc as defined by the bot configuration/environment
   attr_accessor :httputil
 
+  # mechanize agent factory
+  attr_accessor :agent
+
   # server we are connected to
   # TODO multiserver
   def server