Class: Butler::Plugin

Inherits:
Object
  • Object
show all
Extended by:
Log::Comfort
Includes:
Log::Comfort
Defined in:
lib/butler/plugin.rb,
lib/butler/plugin/mapper.rb,
lib/butler/plugin/trigger.rb,
lib/butler/plugin/configproxy.rb

Defined Under Namespace

Classes: ConfigProxy, Mapper, Trigger

Class Attribute Summary collapse

Instance Attribute Summary collapse

Attributes included from Log::Comfort

#log_device

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Log::Comfort

debug, error, exception, fail, info, log, warn

Constructor Details

#initialize(message) ⇒ Plugin

Returns a new instance of Plugin.



163
164
165
166
# File 'lib/butler/plugin.rb', line 163

def initialize(message)
	@butler  = plugin.butler
	@message = message
end

Class Attribute Details

.aboutObject (readonly)

Returns the value of attribute about.



25
26
27
# File 'lib/butler/plugin.rb', line 25

def about
  @about
end

.baseObject (readonly)

Returns the value of attribute base.



26
27
28
# File 'lib/butler/plugin.rb', line 26

def base
  @base
end

.butlerObject (readonly)

Returns the value of attribute butler.



28
29
30
# File 'lib/butler/plugin.rb', line 28

def butler
  @butler
end

.configObject (readonly)

Returns the value of attribute config.



29
30
31
# File 'lib/butler/plugin.rb', line 29

def config
  @config
end

.helpObject (readonly)

Returns the value of attribute help.



30
31
32
# File 'lib/butler/plugin.rb', line 30

def help
  @help
end

.nameObject (readonly)

Returns the value of attribute name.



27
28
29
# File 'lib/butler/plugin.rb', line 27

def name
  @name
end

.pathObject (readonly)

Returns the value of attribute path.



31
32
33
# File 'lib/butler/plugin.rb', line 31

def path
  @path
end

.stringsObject (readonly)

Returns the value of attribute strings.



32
33
34
# File 'lib/butler/plugin.rb', line 32

def strings
  @strings
end

.summaryObject (readonly)

Returns the value of attribute summary.



33
34
35
# File 'lib/butler/plugin.rb', line 33

def summary
  @summary
end

.usageObject (readonly)

Returns the value of attribute usage.



34
35
36
# File 'lib/butler/plugin.rb', line 34

def usage
  @usage
end

Instance Attribute Details

#butlerObject (readonly)

Returns the value of attribute butler.



160
161
162
# File 'lib/butler/plugin.rb', line 160

def butler
  @butler
end

#messageObject (readonly)

Returns the value of attribute message.



161
162
163
# File 'lib/butler/plugin.rb', line 161

def message
  @message
end

Class Method Details

.at(*args, &block) ⇒ Object



132
133
134
# File 'lib/butler/plugin.rb', line 132

def at(*args, &block)
	@butler.scheduler.at(*args, &block)
end

.configuration(settings) ⇒ Object



96
97
98
99
100
# File 'lib/butler/plugin.rb', line 96

def configuration(settings)
	settings.each { |key, value|
		@config[key] = value unless @config.has_key?(key)
	}
end

.create_templates(tmpl, name) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
# File 'lib/butler/plugin.rb', line 83

def create_templates(tmpl, name)
	tmpl.each { |key,value|
		begin
			tmpl[key] = Templater.new(value)
		rescue Exception => e
			e.extend Exception::Detail
			e.prepend "Could not map #{key} in #{@base} (#{name})."
			exception(e)
		end
	}
end

.every(*args, &block) ⇒ Object



128
129
130
# File 'lib/butler/plugin.rb', line 128

def every(*args, &block)
	@butler.scheduler.every(*args, &block)
end

.load_plugin(butler, base, path) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/butler/plugin.rb', line 38

def load_plugin(butler, base, path) # :nodoc:
	@butler    = butler
	@base      = base.dup.freeze
	@name      = File.basename(base).freeze
	@commands  = []
	@listener  = []
	@config    = ConfigProxy.new(@butler.config, "plugin.#{name}")
	
	if File.directory?(path) then
		raise "Not supported yet" # FIXME
		@path      = OpenStruct.new(
			:data      => (@path+"/data").freeze,
			:strings   => (@path+"/strings").freeze,
			:requires  => (@path+"/requires").freeze
		)
	else
		data         = YAML.load(ScriptFile.read(path)) || {}
		@strings     = data[:strings] || {}
		@about       = data[:about]   || {}
		@help        = data[:help]    || {}
		mappers      = data[:map]     || {}
		@summary     = data[:summary] || {}
		triggers     = data[:trigger] || nil
		@usage       = data[:usage]   || {}
		@path        = OpenStruct.new(
			:data     => nil,
			:strings  => nil,
			:requires => nil
		)
	end

	{:usage=>[@usage],:strings=>@strings.values,:help=>@help.values}.each { |key,tmpls|
		tmpls.each { |tmpl|
			create_templates(tmpl, key)
		}
	}
	
	mappers.each { |meth, expressions|
		map(meth, expressions)
	}
	trigger(triggers) if triggers
	
	info("Loaded plugin '#{@base}' (#{self})")
end

.map(meth, expressions) ⇒ Object



119
120
121
122
123
124
125
126
# File 'lib/butler/plugin.rb', line 119

def map(meth, expressions)
	expressions = { "en" => expressions } unless expressions.kind_of?(Hash)
	expressions.each { |lang, expression|
		mapper = Mapper.new(self, meth, lang, expression)
		@butler.add_command(mapper)
		@commands << mapper
	}
end

.on_loadObject



107
108
# File 'lib/butler/plugin.rb', line 107

def on_load
end

.on_unloadObject



146
147
# File 'lib/butler/plugin.rb', line 146

def on_unload
end

.plugin_usage(message, data = {}) ⇒ Object



103
104
105
# File 'lib/butler/plugin.rb', line 103

def plugin_usage(message, data={})
	new(message).usage(data)
end

.subscribe(*args, &block) ⇒ Object



140
141
142
143
144
# File 'lib/butler/plugin.rb', line 140

def subscribe(*args, &block)
	listener = @butler.subscribe(*args, &block)
	@listener << listener
	listener
end

.timed(*args, &block) ⇒ Object



136
137
138
# File 'lib/butler/plugin.rb', line 136

def timed(*args, &block)
	@butler.scheduler.timed(*args, &block)
end

.trigger(commands) ⇒ Object



110
111
112
113
114
115
116
117
# File 'lib/butler/plugin.rb', line 110

def trigger(commands)
	commands = { "en" => commands } unless commands.kind_of?(Hash)
	commands.each { |lang, command|
		trigger = Trigger.new(self, lang, command)
		@butler.add_command(trigger)
		@commands << trigger
	}
end

.unload_pluginObject



149
150
151
152
153
# File 'lib/butler/plugin.rb', line 149

def unload_plugin
	info("Unloading plugin '#{@base}' (#{self})")
	@commands.each { |command| @butler.delete_command(command) }
	@listener.each { |listener| listener.unsubscribe }
end

Instance Method Details

#answer(string, vars = {}) ⇒ Object

Only works if @message is set and conditions of Messag#answer are met. If string is a Symbol it considers it a key for localization, looks it up and translates it using vars as variables for the string interpolation. The string is subsequently mirc_formatted (see String#mirc_formatted). Besides that it works like Message#answer.



208
209
210
211
# File 'lib/butler/plugin.rb', line 208

def answer(string, vars={})
	string = localize(string, vars) if string.kind_of?(Symbol)
	@message.answer(string.mirc_formatted)
end

#arguments(*params) ⇒ Object

gets the arguments and downcases if possible



171
172
173
174
175
176
177
178
179
180
# File 'lib/butler/plugin.rb', line 171

def arguments(*params)
	args = @message.arguments[*params]
	if args.kind_of?(Array) then
		args.map { |arg| arg.downcase }
	elsif args then
		args.downcase
	else
		nil
	end
end

#localize(index, data = {}) ⇒ Object Also known as: _



182
183
184
185
186
187
# File 'lib/butler/plugin.rb', line 182

def localize(index, data={})
	string   = plugin.strings[index]
	string &&= (string[@message.language] || string["en"])
	return "Unknown String '#{index}'" unless string
	string.result(self, data)
end

#notice(string, *args) ⇒ Object

About

Sends a notice (localized and formatted) to any number of recipients (Users or Channels)

Synopsis

notice(:greet, "#some_channel", :from => @butler.myself.nick)

Description

If string is a Symbol it considers it a key for localization, looks it up and translates it using the last argument as variables for the string interpolation, iff the last argument is a Hash. The string is subsequently mirc_formatted (see String#mirc_formatted).



243
244
245
246
247
# File 'lib/butler/plugin.rb', line 243

def notice(string, *args)
	vars   = args.last.kind_of?(Hash) ? args.pop : {}
	string = localize(string, vars) if string.kind_of?(Symbol)
	@butler.notice(string.mirc_formatted, *args)
end

#privmsg(string, *args) ⇒ Object

About

Sends a privmsg (localized and formatted) to any number of recipients (Users or Channels)

Synopsis

privmsg(:greet, "#some_channel", :from => @butler.myself.nick)

Description

If string is a Symbol it considers it a key for localization, looks it up and translates it using the last argument as variables for the string interpolation, iff the last argument is a Hash. The string is subsequently mirc_formatted (see String#mirc_formatted).



225
226
227
228
229
# File 'lib/butler/plugin.rb', line 225

def privmsg(string, *args)
	vars   = args.last.kind_of?(Hash) ? args.pop : {}
	string = localize(string, vars) if string.kind_of?(Symbol)
	@butler.privmsg(string.mirc_formatted, *args)
end

#unknown(n = -1)) ⇒ Object



199
200
201
# File 'lib/butler/plugin.rb', line 199

def unknown(n=-1)
	"Unknown argument #{@message.arguments[n]} for #{@message.arguments[0...n].join(' ')}"
end

#usage(data = {}) ⇒ Object



190
191
192
193
194
195
196
197
# File 'lib/butler/plugin.rb', line 190

def usage(data={})
	string = plugin.usage
	if string = string[@message.language] || string["en"] then
		("Usage '#{plugin.name}': #{@message.invocation}"+string.result(self, data)).mirc_formatted
	else
		"No usage provided"
	end
end