Class: Butler::Plugin

Inherits:
Object
  • 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

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.



176
177
178
179
# File 'lib/butler/plugin.rb', line 176

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

Class Attribute Details

.aboutObject (readonly)

Returns the value of attribute about.



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

def about
  @about
end

.baseObject (readonly)

Returns the value of attribute base.



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

def base
  @base
end

.butlerObject (readonly)

Returns the value of attribute butler.



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

def butler
  @butler
end

.configObject (readonly)

Returns the value of attribute config.



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

def config
  @config
end

.helpObject (readonly)

Returns the value of attribute help.



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

def help
  @help
end

.nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

.pathObject (readonly)

Returns the value of attribute path.



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

def path
  @path
end

.stringsObject (readonly)

Returns the value of attribute strings.



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

def strings
  @strings
end

.summaryObject (readonly)

Returns the value of attribute summary.



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

def summary
  @summary
end

.usageObject (readonly)

Returns the value of attribute usage.



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

def usage
  @usage
end

Instance Attribute Details

#butlerObject (readonly)

Returns the value of attribute butler.



173
174
175
# File 'lib/butler/plugin.rb', line 173

def butler
  @butler
end

#messageObject (readonly)

Returns the value of attribute message.



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

def message
  @message
end

Class Method Details

.at(*args, &block) ⇒ Object



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

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

.configuration(settings) ⇒ Object



98
99
100
101
102
# File 'lib/butler/plugin.rb', line 98

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

.create_templates(tmpl, name) ⇒ Object



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

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



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

def every(*args, &block)
	@butler.scheduler.every(*args, &block)
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
# File 'lib/butler/plugin.rb', line 39

def load_plugin(butler, base, path) # :nodoc:
	@butler       = butler
	@base         = base.dup.freeze
	@name         = File.basename(base).freeze
	@commands     = []
	@listener     = []
	@mapping_type = Hash.new { |h,k| MappingTypes[k] }
	@config       = ConfigProxy.new(@butler.config, "plugin/#{name}")
	
	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



125
126
127
128
129
130
131
132
133
# File 'lib/butler/plugin.rb', line 125

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



153
154
# File 'lib/butler/plugin.rb', line 153

def on_disconnect(*args)
end

.on_load(*args) ⇒ Object



109
110
# File 'lib/butler/plugin.rb', line 109

def on_load(*args)
end

.on_login(*args) ⇒ Object



112
113
# File 'lib/butler/plugin.rb', line 112

def (*args)
end

.on_quit(*args) ⇒ Object



156
157
# File 'lib/butler/plugin.rb', line 156

def on_quit(*args)
end

.on_unload(*args) ⇒ Object



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

def on_unload(*args)
end

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



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

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

.subscribe(*args, &block) ⇒ Object



147
148
149
150
151
# File 'lib/butler/plugin.rb', line 147

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

.timed(*args, &block) ⇒ Object



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

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

.trigger(commands) ⇒ Object



115
116
117
118
119
120
121
122
123
# File 'lib/butler/plugin.rb', line 115

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_pluginObject



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

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(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.



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

def answer(text, vars={})
	text = localize(text, vars) if text.kind_of?(Symbol)
	@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

#answer_with_lead(lead, string, vars = {}) ⇒ Object

Same as answer, but ‘more’ gets a lead prefixed (see Butler::Plugin::More for more info)



235
236
237
238
239
240
241
242
243
244
# File 'lib/butler/plugin.rb', line 235

def answer_with_lead(lead, string, vars={})
	text = localize(text, vars) if text.kind_of?(Symbol)
	lead = localize(lead, vars) if text.kind_of?(Symbol)
	@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

#arguments(*params) ⇒ Object

gets the arguments and downcases if possible



184
185
186
187
188
189
190
191
192
193
# File 'lib/butler/plugin.rb', line 184

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: _



195
196
197
198
199
200
# File 'lib/butler/plugin.rb', line 195

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).



277
278
279
280
281
# File 'lib/butler/plugin.rb', line 277

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).



259
260
261
262
263
# File 'lib/butler/plugin.rb', line 259

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



212
213
214
# File 'lib/butler/plugin.rb', line 212

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

#usage(data = {}) ⇒ Object



203
204
205
206
207
208
209
210
# File 'lib/butler/plugin.rb', line 203

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