This class represents the core of the plugin functionality of Cinch. It provides both the methods for users to write their own plugins as well as for the Cinch framework to use them.
The {ClassMethods} module, which will get included automatically in all classes that include `Cinch::Plugin`, includes all class methods that the user will use for creating plugins.
Most of the instance methods are for use by the Cinch framework and part of the private API, but some will also be used by plugin authors, mainly {#config}, {#synchronize} and {#bot}.
@return [Bot]
@return [Array<Handler>] handlers
@return [Array<Cinch::Timer>]
@api private
# File lib/cinch/plugin.rb, line 473 def self.included(by) by.extend ClassMethods end
@api private
# File lib/cinch/plugin.rb, line 436 def initialize(bot) @bot = bot @handlers = [] @timers = [] __register end
@return [void] @api private
# File lib/cinch/plugin.rb, line 412 def __register missing = self.class.check_for_missing_options(@bot) unless missing.empty? @bot.loggers.warn "[plugin] #{self.class.plugin_name}: Could not register plugin because the following options are not set: #{missing.join(", ")}" return end __register_listeners __register_matchers __register_ctcps __register_timers __register_help end
Provides access to plugin-specific options.
@return [Hash] A hash of options
# File lib/cinch/plugin.rb, line 464 def config @bot.config.plugins.options[self.class] || {} end
(see Cinch::Bot#synchronize)
# File lib/cinch/plugin.rb, line 457 def synchronize(*args, &block) @bot.synchronize(*args, &block) end
@since 2.0.0
# File lib/cinch/plugin.rb, line 444 def unregister @bot.loggers.debug "[plugin] #{self.class.plugin_name}: Unloading plugin" @timers.each do |timer| timer.stop end handlers.each do |handler| handler.stop handler.unregister end end
# File lib/cinch/plugin.rb, line 331 def __register_ctcps self.class.ctcps.each do |ctcp| @bot.loggers.debug "[plugin] #{self.class.plugin_name}: Registering CTCP `#{ctcp}`" new_handler = Handler.new(@bot, :ctcp, Pattern.generate(:ctcp, ctcp)) do |message, *args| if self.class.call_hooks(:pre, :ctcp, self, [message]) __send__("ctcp_#{ctcp.downcase}", message, *args) self.class.call_hooks(:post, :ctcp, self, [message]) else @bot.loggers.debug "[plugin] #{self.class.plugin_name}: Dropping message due to hook" end end @handlers << new_handler @bot.handlers.register(new_handler) end end
# File lib/cinch/plugin.rb, line 394 def __register_help prefix = self.class.prefix || @bot.config.plugins.prefix suffix = self.class.suffix || @bot.config.plugins.suffix if self.class.help @bot.loggers.debug "[plugin] #{self.class.plugin_name}: Registering help message" help_pattern = Pattern.new(prefix, "help #{self.class.plugin_name}", suffix) new_handler = Handler.new(@bot, :message, help_pattern) do |message| message.reply(self.class.help) end @handlers << new_handler @bot.handlers.register(new_handler) end end
# File lib/cinch/plugin.rb, line 313 def __register_listeners self.class.listeners.each do |listener| @bot.loggers.debug "[plugin] #{self.class.plugin_name}: Registering listener for type `#{listener.event}`" new_handler = Handler.new(@bot, listener.event, Pattern.new(nil, //, nil)) do |message, *args| if self.class.call_hooks(:pre, :listen_to, self, [message]) __send__(listener.method, message, *args) self.class.call_hooks(:post, :listen_to, self, [message]) else @bot.loggers.debug "[plugin] #{self.class.plugin_name}: Dropping message due to hook" end end @handlers << new_handler @bot.handlers.register(new_handler) end end
# File lib/cinch/plugin.rb, line 360 def __register_matchers prefix = self.class.prefix || @bot.config.plugins.prefix suffix = self.class.suffix || @bot.config.plugins.suffix self.class.matchers.each do |matcher| _prefix = matcher.use_prefix ? matcher.prefix || prefix : nil _suffix = matcher.use_suffix ? matcher.suffix || suffix : nil pattern_to_register = Pattern.new(_prefix, matcher.pattern, _suffix) react_on = matcher.react_on || self.class.react_on || :message @bot.loggers.debug "[plugin] #{self.class.plugin_name}: Registering executor with pattern `#{pattern_to_register.inspect}`, reacting on `#{react_on}`" new_handler = Handler.new(@bot, react_on, pattern_to_register, group: matcher.group) do |message, *args| method = method(matcher.method) arity = method.arity - 1 if arity > 0 args = args[0..arity - 1] elsif arity == 0 args = [] end if self.class.call_hooks(:pre, :match, self, [message]) method.call(message, *args) self.class.call_hooks(:post, :match, self, [message]) else @bot.loggers.debug "[plugin] #{self.class.plugin_name}: Dropping message due to hook" end end @handlers << new_handler @bot.handlers.register(new_handler) end end
# File lib/cinch/plugin.rb, line 349 def __register_timers @timers = self.class.timers.map {|timer_struct| @bot.loggers.debug "[plugin] #{self.class.plugin_name}: Registering timer with interval `#{timer_struct.interval}` for method `#{timer_struct.options[:method]}`" block = self.method(timer_struct.options[:method]) options = timer_struct.options.merge(interval: timer_struct.interval) Cinch::Timer.new(@bot, options, &block) } end