Module: Cinch::Plugin::ClassMethods

Defined in:
lib/cinch/plugin.rb

Defined Under Namespace

Classes: Listener, Pattern

Instance Method Summary collapse

Instance Method Details

#__plugin_nameString

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:



92
93
94
# File 'lib/cinch/plugin.rb', line 92

def __plugin_name
  @__cinch_name || self.name.split("::").last.downcase
end

#__register_with_bot(bot, instance) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/cinch/plugin.rb', line 98

def __register_with_bot(bot, instance)
  plugin_name = __plugin_name

  (@__cinch_listeners || []).each do |listener|
    bot.debug "[plugin] #{plugin_name}: Registering listener for type `#{listener.event}`"
    bot.on(listener.event, [], instance) do |message, plugin|
      plugin.__send__(listener.method, message) if plugin.respond_to?(listener.method)
    end
  end

  if (@__cinch_patterns ||= []).empty?
    @__cinch_patterns << Pattern.new(plugin_name, true, nil)
  end

  prefix = @__cinch_prefix || bot.config.plugins.prefix
  if prefix.is_a?(String)
    prefix = Regexp.escape(prefix)
  end
  @__cinch_patterns.each do |pattern|
    pattern_to_register = nil

    if pattern.use_prefix && prefix
      case pattern.pattern
      when Regexp
        pattern_to_register = /^#{prefix}#{pattern.pattern}/
      when String
        pattern_to_register = prefix + pattern.pattern
      end
    else
      pattern_to_register = pattern.pattern
    end

    react_on = @__cinch_react_on || :message

    bot.debug "[plugin] #{plugin_name}: Registering executor with pattern `#{pattern_to_register}`, reacting on `#{react_on}`"

    bot.on(react_on, pattern_to_register, instance, pattern) do |message, plugin, pattern, *args|
      if plugin.respond_to?(pattern.method)
        method = plugin.method(pattern.method)
        arity = method.arity - 1
        if arity > 0
          args = args[0..arity - 1]
        elsif arity == 0
          args = []
        end
        method.call(message, *args)
      end
    end
  end

  (@__cinch_ctcps || []).each do |ctcp|
    bot.debug "[plugin] #{plugin_name}: Registering CTCP `#{ctcp}`"
    bot.on(:ctcp, ctcp, instance, ctcp) do |message, plugin, ctcp, *args|
      plugin.__send__("ctcp_#{ctcp.downcase}", message, *args)
    end
  end

  if @__cinch_help_message
    bot.debug "[plugin] #{plugin_name}: Registering help message"
    bot.on(:message, /#{prefix}help #{Regexp.escape(plugin_name)}/, @__cinch_help_message) do |message, help_message|
      message.reply(help_message)
    end
  end
end

#ctcp(command) ⇒ Object



52
53
54
# File 'lib/cinch/plugin.rb', line 52

def ctcp(command)
  (@__cinch_ctcps ||= []) << command.to_s.upcase
end

#help(message) ⇒ void

This method returns an undefined value.

Define a help message which will be returned on "help ".

Parameters:



61
62
63
# File 'lib/cinch/plugin.rb', line 61

def help(message)
  @__cinch_help_message = message
end

#listen_to(*types, options = {}) ⇒ void

Events to listen to.

This method returns an undefined value.

Parameters:

  • *types (String, Symbol, Integer)

    Events to listen to. Available events are all IRC commands in lowercase as symbols, all numeric replies, and the following:

    - :channel (a channel message)
    - :private (a private message)
    - :message (both channel and private messages)
    - :error   (IRC errors)
    - :ctcp    (ctcp requests)
    
  • options (Hash) (defaults to: {})

Options Hash (options):

  • :method (Symbol) — default: :listen

    The method to execute



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/cinch/plugin.rb', line 39

def listen_to(*types)
  options = {:method => :listen}
  if types.last.is_a?(Hash)
    options.merge!(types.pop)
  end

  @__cinch_listeners ||= []

  types.each do |type|
    @__cinch_listeners << Listener.new(type, options[:method])
  end
end

#match(pattern, options = {}) ⇒ void

This method returns an undefined value.

Set a match pattern.

Parameters:

  • pattern (Regexp, String)

    A pattern

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :method (Symbol) — default: :execute

    The method to execute

  • :use_prefix (Boolean) — default: true

    If true, the plugin prefix will automatically be prepended to the pattern.



17
18
19
20
21
# File 'lib/cinch/plugin.rb', line 17

def match(pattern, options = {})
  options = {:use_prefix => true, :method => :execute}.merge(options)
  @__cinch_patterns ||= []
  @__cinch_patterns << Pattern.new(pattern, options[:use_prefix], options[:method])
end

#plugin(name) ⇒ void

This method returns an undefined value.

Define the plugin name.

Parameters:



86
87
88
# File 'lib/cinch/plugin.rb', line 86

def plugin(name)
  @__cinch_name = name
end

#prefix(prefix) ⇒ void

This method returns an undefined value.

Set the plugin prefix.

Parameters:



69
70
71
# File 'lib/cinch/plugin.rb', line 69

def prefix(prefix)
  @__cinch_prefix = prefix
end

#react_on(target) ⇒ void

This method returns an undefined value.

Set which kind of messages to react on (i.e. call Cinch::Plugin#execute)

Parameters:

  • target (Symbol<:message, :channel, :private>)

    React to all, only public or only private messages?



78
79
80
# File 'lib/cinch/plugin.rb', line 78

def react_on(target)
  @__cinch_react_on = target
end