Class: Campfire::PollingBot::Plugin

Inherits:
Object
  • Object
show all
Defined in:
lib/campfire/polling_bot/plugin.rb

Constant Summary collapse

HALT =

returned by a command when command processing should halt (continues by default)

1

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePlugin

Returns a new instance of Plugin.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/campfire/polling_bot/plugin.rb', line 14

def initialize
  # load the config file if we have one
  name = self.to_s.gsub(/([[:upper:]]+)([[:upper:]][[:lower:]])/,'\1_\2').
      gsub(/([[:lower:]\d])([[:upper:]])/,'\1_\2').
      tr("-", "_").
      downcase
  config_dir = bot.config.config_dir || self.class.directory
  filepath = File.join(config_dir, "#{name}.yml")
  if File.exists?(filepath)
    logger.debug "loading config file: #{filepath}"
    self.config = YAML.load_file(filepath)
  else
    self.config = {}
  end
end

Instance Attribute Details

#botObject



63
64
65
# File 'lib/campfire/polling_bot/plugin.rb', line 63

def bot
  @bot || self.class.bot
end

#configObject

Returns the value of attribute config.



12
13
14
# File 'lib/campfire/polling_bot/plugin.rb', line 12

def config
  @config
end

Class Method Details

.accepts(message_type, params = {}) ⇒ Object

called from Plugin objects to indicate what kinds of messages they accept if the :addressed_to_me flag is true, it will only accept messages addressed to the bot (e.g. “Wes, __” or “__, Wes”) Examples:

accepts :text_message, :addressed_to_me => true
accepts :enter_message
accepts :all


154
155
156
157
158
159
160
161
162
# File 'lib/campfire/polling_bot/plugin.rb', line 154

def self.accepts(message_type, params = {})
  @accepts ||= {}
  if message_type == :all
    @accepts[:all] = params[:addressed_to_me] ? :addressed_to_me : :for_anyone
  else
    klass = Campfire.const_get(message_type.to_s.gsub(/(?:^|_)(\S)/) {$1.upcase})
    @accepts[klass] = params[:addressed_to_me] ? :addressed_to_me : :for_anyone
  end
end

.accepts?(message) ⇒ Boolean

returns true if the plugin accepts the given message type

Returns:

  • (Boolean)


165
166
167
168
169
170
171
# File 'lib/campfire/polling_bot/plugin.rb', line 165

def self.accepts?(message)
  if @accepts[:all]
    @accepts[:all] == :addressed_to_me ? bot.addressed_to_me?(message) : true
  elsif @accepts[message.class]
    @accepts[message.class] == :addressed_to_me ? bot.addressed_to_me?(message) : true
  end
end

.botObject

bot accessor



56
57
58
# File 'lib/campfire/polling_bot/plugin.rb', line 56

def self.bot
  @@bot
end

.bot=(bot) ⇒ Object



59
60
61
# File 'lib/campfire/polling_bot/plugin.rb', line 59

def self.bot=(bot)
  @@bot = bot
end

.directoryObject



51
52
53
# File 'lib/campfire/polling_bot/plugin.rb', line 51

def self.directory
  @directory
end

.directory=(dir) ⇒ Object



47
48
49
# File 'lib/campfire/polling_bot/plugin.rb', line 47

def self.directory=(dir)
  @directory = dir
end

.inherited(klass) ⇒ Object

keep track of subclasses



31
32
33
34
35
36
37
38
# File 'lib/campfire/polling_bot/plugin.rb', line 31

def self.inherited(klass)
  # save the plugin's directory
  filepath = caller[0].split(':')[0]
  klass.directory = File.dirname(caller[0].split(':')[0])
  super if defined? super
ensure
  ( @subclasses ||= [] ).push(klass).uniq!
end

.load_all(bot) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/campfire/polling_bot/plugin.rb', line 77

def self.load_all(bot)
  self.bot = bot

  load_plugin_classes

  # set up the database now that the plugins are loaded
  setup_database(bot.config.datauri)

  plugin_classes = self.subclasses.sort {|a,b| b.priority <=> a.priority }
  # initialize plugins
  plugins = plugin_classes.map { |p_class| p_class.new }
  # remove any plugins that require a config and don't have one
  plugins.reject! {|p| p.requires_config? and p.config.empty?}
  return plugins
end

.load_plugin_classesObject



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/campfire/polling_bot/plugin.rb', line 93

def self.load_plugin_classes
  # add each plugin dir to the load path
  Dir.glob(File.dirname(__FILE__) + "/plugins/*").each {|dir| $LOAD_PATH << dir }
  # load core first
  paths  = Dir.glob(File.dirname(__FILE__) + "/plugins/shared/*.rb")
  # load all models & plugins
  paths += Dir.glob(File.dirname(__FILE__) + "/plugins/*/*.rb")
  paths.each do |path|
    begin
      path.match(/(.*?)\.rb$/) && (require $1)
      logger.debug "loaded plugin #{$1}"
    rescue Exception => e
      logger.error "Unable to load #{path}: #{e.class}: #{e.message}\n\t#{e.backtrace.join("\n\t")}"
    end
  end
end

.loggerObject



71
72
73
# File 'lib/campfire/polling_bot/plugin.rb', line 71

def self.logger
  self.bot.logger
end

.priority(value = nil) ⇒ Object

method to set or get the priority. Higher value == higher priority. Default is 0 command subclasses set their priority like so:

class FooPlugin << Campfire::PollingBot::Plugin
  priority 10
...


123
124
125
126
127
128
# File 'lib/campfire/polling_bot/plugin.rb', line 123

def self.priority(value = nil)
  if value
    @priority = value
  end
  return @priority || 0
end

.requires_config(flag = true) ⇒ Object



135
136
137
# File 'lib/campfire/polling_bot/plugin.rb', line 135

def self.requires_config(flag = true)
  @requires_config = flag
end

.requires_config?Boolean

Returns:

  • (Boolean)


139
140
141
# File 'lib/campfire/polling_bot/plugin.rb', line 139

def self.requires_config?
  @requires_config
end

.setup_database(datauri) ⇒ Object

set up the plugin database



111
112
113
114
115
116
# File 'lib/campfire/polling_bot/plugin.rb', line 111

def self.setup_database(datauri)
  DataMapper.setup(:default, datauri)
  DataMapper.auto_upgrade!
  # not related to setting up the database, but for lack of a better place....
  DataMapper::Model.raise_on_save_failure = true
end

.subclassesObject



40
41
42
43
44
45
# File 'lib/campfire/polling_bot/plugin.rb', line 40

def self.subclasses
  @subclasses ||= []
  @subclasses.inject( [] ) do |list, subclass|
    list.push(subclass, *subclass.subclasses)
  end
end

Instance Method Details

#accepts?(message) ⇒ Boolean

convenience method to call accepts on a plugin instance

Returns:

  • (Boolean)


174
175
176
# File 'lib/campfire/polling_bot/plugin.rb', line 174

def accepts?(message)
  self.class.accepts?(message)
end

#loggerObject



67
68
69
# File 'lib/campfire/polling_bot/plugin.rb', line 67

def logger
  bot.logger
end

#priorityObject

convenience method to get the priority of a plugin instance



131
132
133
# File 'lib/campfire/polling_bot/plugin.rb', line 131

def priority
  self.class.priority
end

#requires_config?Boolean

Returns:

  • (Boolean)


143
144
145
# File 'lib/campfire/polling_bot/plugin.rb', line 143

def requires_config?
  self.class.requires_config?
end

#to_sObject



178
179
180
# File 'lib/campfire/polling_bot/plugin.rb', line 178

def to_s
  self.class.to_s
end