diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/rbot/core/utils/agent.rb | 58 | ||||
-rw-r--r-- | lib/rbot/ircbot.rb | 3 |
2 files changed, 61 insertions, 0 deletions
diff --git a/lib/rbot/core/utils/agent.rb b/lib/rbot/core/utils/agent.rb new file mode 100644 index 00000000..fc73c288 --- /dev/null +++ b/lib/rbot/core/utils/agent.rb @@ -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 + diff --git a/lib/rbot/ircbot.rb b/lib/rbot/ircbot.rb index a342e8c1..ea6a57c8 100644 --- a/lib/rbot/ircbot.rb +++ b/lib/rbot/ircbot.rb @@ -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 |