diff options
author | Matthias H <apoc@sixserv.org> | 2014-02-24 01:15:13 +0100 |
---|---|---|
committer | Matthias H <apoc@sixserv.org> | 2014-02-24 01:15:13 +0100 |
commit | 193edc468636e40ae21d1f0ea299f8eb0927ebba (patch) | |
tree | 95dc346c23918eeb030ef976bdc6f8ef77b8502f /lib | |
parent | 77d48adcf5d489f146d81666d9631bf358deb540 (diff) |
[agent] wip core mechanize agent plugin
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 |