Class: Butler::Plugin
- Inherits:
-
Object
show all
- Extended by:
- Log::Comfort
- Includes:
- Log::Comfort
- Defined in:
- lib/butler/plugin.rb,
lib/butler/plugin/more.rb,
lib/butler/plugin/trigger.rb,
lib/butler/plugin/configproxy.rb
Defined Under Namespace
Classes: ConfigProxy, More, Trigger
Class Attribute Summary collapse
Instance Attribute Summary collapse
Attributes included from Log::Comfort
#logger
Class Method Summary
collapse
-
.at(*args, &block) ⇒ Object
-
.configuration(settings) ⇒ Object
-
.create_templates(tmpl, name) ⇒ Object
-
.every(*args, &block) ⇒ Object
-
.load_plugin(butler, base, path) ⇒ Object
-
.map(meth, expressions) ⇒ Object
-
.on_disconnect(*args) ⇒ Object
-
.on_load(*args) ⇒ Object
-
.on_login(*args) ⇒ Object
-
.on_quit(*args) ⇒ Object
-
.on_unload(*args) ⇒ Object
-
.plugin_accessor(name, initial_value = nil) ⇒ Object
-
.plugin_attribute(name, initial_value = nil) ⇒ Object
-
.plugin_usage(message, data = {}) ⇒ Object
-
.subscribe(*args, &block) ⇒ Object
-
.timed(*args, &block) ⇒ Object
-
.trigger(commands) ⇒ Object
-
.unload_plugin ⇒ Object
Instance Method Summary
collapse
debug, error, exception, fail, info, log, warn
Constructor Details
#initialize(message) ⇒ Plugin
Returns a new instance of Plugin.
202
203
204
205
|
# File 'lib/butler/plugin.rb', line 202
def initialize(message)
@butler = plugin.butler
@message = message
end
|
Class Attribute Details
.about ⇒ Object
Returns the value of attribute about.
26
27
28
|
# File 'lib/butler/plugin.rb', line 26
def about
@about
end
|
.base ⇒ Object
Returns the value of attribute base.
27
28
29
|
# File 'lib/butler/plugin.rb', line 27
def base
@base
end
|
.butler ⇒ Object
Returns the value of attribute butler.
29
30
31
|
# File 'lib/butler/plugin.rb', line 29
def butler
@butler
end
|
.config ⇒ Object
Returns the value of attribute config.
30
31
32
|
# File 'lib/butler/plugin.rb', line 30
def config
@config
end
|
.help ⇒ Object
Returns the value of attribute help.
31
32
33
|
# File 'lib/butler/plugin.rb', line 31
def help
@help
end
|
.name ⇒ Object
Returns the value of attribute name.
28
29
30
|
# File 'lib/butler/plugin.rb', line 28
def name
@name
end
|
.path ⇒ Object
Returns the value of attribute path.
32
33
34
|
# File 'lib/butler/plugin.rb', line 32
def path
@path
end
|
.strings ⇒ Object
Returns the value of attribute strings.
33
34
35
|
# File 'lib/butler/plugin.rb', line 33
def strings
@strings
end
|
.summary ⇒ Object
Returns the value of attribute summary.
34
35
36
|
# File 'lib/butler/plugin.rb', line 34
def summary
@summary
end
|
.usage ⇒ Object
Returns the value of attribute usage.
35
36
37
|
# File 'lib/butler/plugin.rb', line 35
def usage
@usage
end
|
Instance Attribute Details
#butler ⇒ Object
Returns the value of attribute butler.
199
200
201
|
# File 'lib/butler/plugin.rb', line 199
def butler
@butler
end
|
#message ⇒ Object
Returns the value of attribute message.
200
201
202
|
# File 'lib/butler/plugin.rb', line 200
def message
@message
end
|
Class Method Details
.at(*args, &block) ⇒ Object
160
161
162
163
164
|
# File 'lib/butler/plugin.rb', line 160
def at(*args, &block)
scheduler = @butler.scheduler.at(*args, &block)
@schedules << scheduler
scheduler
end
|
.configuration(settings) ⇒ Object
117
118
119
120
121
|
# File 'lib/butler/plugin.rb', line 117
def configuration(settings)
settings.each { |key, value|
@config[key] = value unless @config.has_key?(key)
}
end
|
.create_templates(tmpl, name) ⇒ Object
104
105
106
107
108
109
110
111
112
113
114
|
# File 'lib/butler/plugin.rb', line 104
def create_templates(tmpl, name) tmpl.each { |key,value|
begin
tmpl[key] = Templater.new(value)
rescue Exception => e
e.extend Exception::Detailed
e.prepend "Could not map #{key} in #{@base} (#{name})."
exception(e)
end
}
end
|
.every(*args, &block) ⇒ Object
154
155
156
157
158
|
# File 'lib/butler/plugin.rb', line 154
def every(*args, &block)
scheduler = @butler.scheduler.every(*args, &block)
@schedules << scheduler
scheduler
end
|
.load_plugin(butler, base, path) ⇒ Object
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
82
83
84
|
# File 'lib/butler/plugin.rb', line 39
def load_plugin(butler, base, path) @butler = butler
@base = base.dup.freeze
@name = File.basename(base).freeze
@commands = []
@listener = []
@schedules = []
@mapping_type = Hash.new { |h,k| MappingTypes[k] }
@config = ConfigProxy.new(@butler.config, "plugins/#{base}")
if File.directory?(path) then
raise "Not supported yet"
@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
144
145
146
147
148
149
150
151
152
|
# File 'lib/butler/plugin.rb', line 144
def map(meth, expressions)
expressions = { "en" => expressions } unless expressions.kind_of?(Hash)
expressions.each { |lang, expression|
raise "Invalid map, language must be a string, method a symbol and expression a string" unless String === lang and String === expression and Symbol === meth
mapper = Mapper.new(self, meth, lang, expression)
@butler.add_command(mapper)
@commands << mapper
}
end
|
.on_disconnect(*args) ⇒ Object
178
179
|
# File 'lib/butler/plugin.rb', line 178
def on_disconnect(*args)
end
|
.on_load(*args) ⇒ Object
128
129
|
# File 'lib/butler/plugin.rb', line 128
def on_load(*args)
end
|
.on_login(*args) ⇒ Object
131
132
|
# File 'lib/butler/plugin.rb', line 131
def on_login(*args)
end
|
.on_quit(*args) ⇒ Object
181
182
|
# File 'lib/butler/plugin.rb', line 181
def on_quit(*args)
end
|
.on_unload(*args) ⇒ Object
184
185
|
# File 'lib/butler/plugin.rb', line 184
def on_unload(*args)
end
|
.plugin_accessor(name, initial_value = nil) ⇒ Object
97
98
99
100
101
102
|
# File 'lib/butler/plugin.rb', line 97
def plugin_accessor(name, initial_value=nil)
(class <<self; self; end).instance_eval {
attr_accessor(name)
}
instance_variable_set(:"@#{name}", initial_value)
end
|
.plugin_attribute(name, initial_value = nil) ⇒ Object
88
89
90
91
92
93
|
# File 'lib/butler/plugin.rb', line 88
def plugin_attribute(name, initial_value=nil)
(class <<self; self; end).instance_eval {
attr_reader(name)
}
instance_variable_set(:"@#{name}", initial_value)
end
|
.plugin_usage(message, data = {}) ⇒ Object
124
125
126
|
# File 'lib/butler/plugin.rb', line 124
def plugin_usage(message, data={})
new(message).usage(data)
end
|
.subscribe(*args, &block) ⇒ Object
172
173
174
175
176
|
# File 'lib/butler/plugin.rb', line 172
def subscribe(*args, &block)
listener = @butler.subscribe(*args, &block)
@listener << listener
listener
end
|
.timed(*args, &block) ⇒ Object
166
167
168
169
170
|
# File 'lib/butler/plugin.rb', line 166
def timed(*args, &block)
scheduler = @butler.scheduler.timed(*args, &block)
@schedules << scheduler
scheduler
end
|
.trigger(commands) ⇒ Object
134
135
136
137
138
139
140
141
142
|
# File 'lib/butler/plugin.rb', line 134
def trigger(commands)
commands = { "en" => commands } unless commands.kind_of?(Hash)
commands.each { |lang, command|
raise "Invalid trigger, language must be a string" unless String === lang
trigger = Trigger.new(self, lang, command)
@butler.add_command(trigger)
@commands << trigger
}
end
|
.unload_plugin ⇒ Object
187
188
189
190
191
192
|
# File 'lib/butler/plugin.rb', line 187
def unload_plugin
info("Unloading plugin '#{@base}' (#{self})")
@commands.each { |command| @butler.delete_command(command) }
@listener.each { |listener| listener.unsubscribe }
@schedules.each { |scheduler| scheduler.finished }
end
|
Instance Method Details
#answer(text, 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. Another feature of answer is, that it is ‘moreified’, that means if your answer-text is longer than 300 chars, butler will display ‘more…’ at the end and using the plugin ‘more’ the user can see the rest of the text.
254
255
256
257
258
259
260
261
262
263
264
265
266
|
# File 'lib/butler/plugin.rb', line 254
def answer(text, vars={})
text = localize(text, vars) if text.kind_of?(Symbol)
if @message.remote? then
@message.answer(text)
else
@message.from.session["more"] = More.new(
@message,
nil,
text.mirc_formatted
)
@message.answer("#{@message.from.to_s+': ' if @message.public?}#{@message.from.session['more'].show}")
end
end
|
#answer_with_lead(lead, text, vars = {}) ⇒ Object
Same as answer, but ‘more’ gets a lead prefixed (see Butler::Plugin::More for more info)
269
270
271
272
273
274
275
276
277
278
279
280
281
282
|
# File 'lib/butler/plugin.rb', line 269
def answer_with_lead(lead, text, vars={})
lead = localize(lead, vars) if lead.kind_of?(Symbol)
text = localize(text, vars) if text.kind_of?(Symbol)
if @message.remote? then
@message.answer(lead+text) else
@message.from.session["more"] = More.new(
@message,
lead.mirc_formatted,
text.mirc_formatted
)
@message.answer("#{@message.from.to_s+': ' if @message.public?}#{@message.from.session['more'].show}")
end
end
|
#arguments(*params) ⇒ Object
gets the arguments and downcases if possible
210
211
212
213
214
215
216
217
218
219
|
# File 'lib/butler/plugin.rb', line 210
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:
_
221
222
223
224
225
226
|
# File 'lib/butler/plugin.rb', line 221
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 (UserList or ChannelList)
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).
330
331
332
333
334
|
# File 'lib/butler/plugin.rb', line 330
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 (UserList or ChannelList)
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).
312
313
314
315
316
|
# File 'lib/butler/plugin.rb', line 312
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
|
#say(text, vars = {}) ⇒ Object
Same as answer, but will not prefix the nick in public messages
285
286
287
288
289
290
291
292
293
294
295
296
297
|
# File 'lib/butler/plugin.rb', line 285
def say(text, vars={})
text = localize(text, vars) if text.kind_of?(Symbol)
if @message.remote? then
@message.answer(text)
else
@message.from.session["more"] = More.new(
@message,
nil,
text.mirc_formatted
)
@message.answer(@message.from.session['more'].show)
end
end
|
#service(name) ⇒ Object
238
239
240
|
# File 'lib/butler/plugin.rb', line 238
def service(name)
@butler.services[name]
end
|
#unknown(n = -1)) ⇒ Object
242
243
244
|
# File 'lib/butler/plugin.rb', line 242
def unknown(n=-1)
"Unknown argument #{@message.arguments[n]} for #{@message.arguments[0...n].join(' ')}"
end
|
#usage(data = {}) ⇒ Object
229
230
231
232
233
234
235
236
|
# File 'lib/butler/plugin.rb', line 229
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
|