]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - lib/rbot/plugins.rb
Remove @blacklisted, not needed anymore; include filename in module_eval to get more...
[user/henk/code/ruby/rbot.git] / lib / rbot / plugins.rb
1 module Irc
2     BotConfig.register BotConfigArrayValue.new('plugins.blacklist',
3       :default => [], :wizard => false, :requires_restart => true,
4       :desc => "Plugins that should not be loaded")
5 module Plugins
6   require 'rbot/messagemapper'
7
8   # base class for all rbot plugins
9   # certain methods will be called if they are provided, if you define one of
10   # the following methods, it will be called as appropriate:
11   #
12   # map(template, options)::
13   # map!(template, options)::
14   #    map is the new, cleaner way to respond to specific message formats
15   #    without littering your plugin code with regexps. The difference
16   #    between map and map! is that map! will not register the new command
17   #    as an alternative name for the plugin.
18   #
19   #    Examples:
20   #
21   #      plugin.map 'karmastats', :action => 'karma_stats'
22   #
23   #      # while in the plugin...
24   #      def karma_stats(m, params)
25   #        m.reply "..."
26   #      end
27   #
28   #      # the default action is the first component
29   #      plugin.map 'karma'
30   #
31   #      # attributes can be pulled out of the match string
32   #      plugin.map 'karma for :key'
33   #      plugin.map 'karma :key'
34   #
35   #      # while in the plugin...
36   #      def karma(m, params)
37   #        item = params[:key]
38   #        m.reply 'karma for #{item}'
39   #      end
40   #
41   #      # you can setup defaults, to make parameters optional
42   #      plugin.map 'karma :key', :defaults => {:key => 'defaultvalue'}
43   #
44   #      # the default auth check is also against the first component
45   #      # but that can be changed
46   #      plugin.map 'karmastats', :auth => 'karma'
47   #
48   #      # maps can be restricted to public or private message:
49   #      plugin.map 'karmastats', :private false,
50   #      plugin.map 'karmastats', :public false,
51   #    end
52   #
53   # listen(UserMessage)::
54   #                        Called for all messages of any type. To
55   #                        differentiate them, use message.kind_of? It'll be
56   #                        either a PrivMessage, NoticeMessage, KickMessage,
57   #                        QuitMessage, PartMessage, JoinMessage, NickMessage,
58   #                        etc.
59   #
60   # privmsg(PrivMessage)::
61   #                        called for a PRIVMSG if the first word matches one
62   #                        the plugin register()d for. Use m.plugin to get
63   #                        that word and m.params for the rest of the message,
64   #                        if applicable.
65   #
66   # kick(KickMessage)::
67   #                        Called when a user (or the bot) is kicked from a
68   #                        channel the bot is in.
69   #
70   # join(JoinMessage)::
71   #                        Called when a user (or the bot) joins a channel
72   #
73   # part(PartMessage)::
74   #                        Called when a user (or the bot) parts a channel
75   #
76   # quit(QuitMessage)::
77   #                        Called when a user (or the bot) quits IRC
78   #
79   # nick(NickMessage)::
80   #                        Called when a user (or the bot) changes Nick
81   # topic(TopicMessage)::
82   #                        Called when a user (or the bot) changes a channel
83   #                        topic
84   #
85   # connect()::            Called when a server is joined successfully, but
86   #                        before autojoin channels are joined (no params)
87   #
88   # save::                 Called when you are required to save your plugin's
89   #                        state, if you maintain data between sessions
90   #
91   # cleanup::              called before your plugin is "unloaded", prior to a
92   #                        plugin reload or bot quit - close any open
93   #                        files/connections or flush caches here
94   class Plugin
95     attr_reader :bot   # the associated bot
96     # initialise your plugin. Always call super if you override this method,
97     # as important variables are set up for you
98     def initialize
99       @bot = Plugins.bot
100       @names = Array.new
101       @handler = MessageMapper.new(self)
102       @registry = BotRegistryAccessor.new(@bot, self.class.to_s.gsub(/^.*::/, ""))
103     end
104
105     def flush_registry
106       # debug "Flushing #{@registry}"
107       @registry.flush
108     end
109
110     def cleanup
111       # debug "Closing #{@registry}"
112       @registry.close
113     end
114
115     def handle(m)
116       @handler.handle(m)
117     end
118
119     def map(*args)
120       @handler.map(*args)
121       # register this map
122       name = @handler.last.items[0]
123       self.register name
124       unless self.respond_to?('privmsg')
125         def self.privmsg(m)
126           handle(m)
127         end
128       end
129     end
130
131     def map!(*args)
132       @handler.map(*args)
133       # register this map
134       name = @handler.last.items[0]
135       self.register name, {:hidden => true}
136       unless self.respond_to?('privmsg')
137         def self.privmsg(m)
138           handle(m)
139         end
140       end
141     end
142
143     # return an identifier for this plugin, defaults to a list of the message
144     # prefixes handled (used for error messages etc)
145     def name
146       @names.join("|")
147     end
148
149     # return a help string for your module. for complex modules, you may wish
150     # to break your help into topics, and return a list of available topics if
151     # +topic+ is nil. +plugin+ is passed containing the matching prefix for
152     # this message - if your plugin handles multiple prefixes, make sure you
153     # return the correct help for the prefix requested
154     def help(plugin, topic)
155       "no help"
156     end
157
158     # register the plugin as a handler for messages prefixed +name+
159     # this can be called multiple times for a plugin to handle multiple
160     # message prefixes
161     def register(name,opts={})
162       raise ArgumentError, "Second argument must be a hash!" unless opts.kind_of?(Hash)
163       return if Plugins.plugins.has_key?(name)
164       Plugins.plugins[name] = self
165       @names << name unless opts.fetch(:hidden, false)
166     end
167
168     # default usage method provided as a utility for simple plugins. The
169     # MessageMapper uses 'usage' as its default fallback method.
170     def usage(m, params = {})
171       m.reply "incorrect usage, ask for help using '#{@bot.nick}: help #{m.plugin}'"
172     end
173
174   end
175
176   # class to manage multiple plugins and delegate messages to them for
177   # handling
178   class Plugins
179     # hash of registered message prefixes and associated plugins
180     @@plugins = Hash.new
181     # associated IrcBot class
182     @@bot = nil
183
184     # bot::     associated IrcBot class
185     # dirlist:: array of directories to scan (in order) for plugins
186     #
187     # create a new plugin handler, scanning for plugins in +dirlist+
188     def initialize(bot, dirlist)
189       @@bot = bot
190       @dirs = dirlist
191       scan
192     end
193
194     # access to associated bot
195     def Plugins.bot
196       @@bot
197     end
198
199     # access to list of plugins
200     def Plugins.plugins
201       @@plugins
202     end
203
204     # load plugins from pre-assigned list of directories
205     def scan
206       @failed = Array.new
207       @ignored = Array.new
208       processed = Hash.new
209
210       @@bot.config['plugins.blacklist'].each { |p|
211         pn = p + ".rb"
212         processed[pn.intern] = :blacklisted
213       }
214
215       dirs = Array.new
216       dirs << Config::datadir + "/plugins"
217       dirs += @dirs
218       dirs.reverse.each {|dir|
219         if(FileTest.directory?(dir))
220           d = Dir.new(dir)
221           d.sort.each {|file|
222
223             next if(file =~ /^\./)
224
225             if processed.has_key?(file.intern)
226               @ignored << {:name => file, :dir => dir, :reason => processed[file.intern]}
227               next
228             end
229
230             if(file =~ /^(.+\.rb)\.disabled$/)
231               # GB: Do we want to do this? This means that a disabled plugin in a directory
232               #     will disable in all subsequent directories. This was probably meant
233               #     to be used before plugins.blacklist was implemented, so I think
234               #     we don't need this anymore
235               processed[$1.intern] = :disabled
236               @ignored << {:name => $1, :dir => dir, :reason => processed[$1.intern]}
237               next
238             end
239
240             next unless(file =~ /\.rb$/)
241
242             tmpfilename = "#{dir}/#{file}"
243
244             # create a new, anonymous module to "house" the plugin
245             # the idea here is to prevent namespace pollution. perhaps there
246             # is another way?
247             plugin_module = Module.new
248
249             begin
250               plugin_string = IO.readlines(tmpfilename).join("")
251               debug "loading plugin #{tmpfilename}"
252               plugin_module.module_eval(plugin_string,tmpfilename)
253               processed[file.intern] = :loaded
254             rescue Exception => err
255               # rescue TimeoutError, StandardError, NameError, LoadError, SyntaxError => err
256               warning "plugin #{tmpfilename} load failed\n" + err.inspect
257               debug err.backtrace.join("\n")
258               bt = err.backtrace.select { |line|
259                 line.match(/^(\(eval\)|#{tmpfilename}):\d+/)
260               }
261               bt.map! { |el|
262                 el.gsub(/^\(eval\)(:\d+)(:in `.*')?(:.*)?/) { |m|
263                   "#{tmpfilename}#{$1}#{$3}"
264                 }
265               }
266               msg = err.to_str.gsub(/^\(eval\)(:\d+)(:in `.*')?(:.*)?/) { |m|
267                 "#{tmpfilename}#{$1}#{$3}"
268               }
269               newerr = err.class.new(msg)
270               newerr.set_backtrace(bt)
271               # debug "Simplified error: " << newerr.inspect
272               # debug newerr.backtrace.join("\n")
273               @failed << { :name => file, :dir => dir, :reason => newerr }
274               # debug "Failures: #{@failed.inspect}"
275             end
276           }
277         end
278       }
279     end
280
281     # call the save method for each active plugin
282     def save
283       delegate 'flush_registry'
284       delegate 'save'
285     end
286
287     # call the cleanup method for each active plugin
288     def cleanup
289       delegate 'cleanup'
290     end
291
292     # drop all plugins and rescan plugins on disk
293     # calls save and cleanup for each plugin before dropping them
294     def rescan
295       save
296       cleanup
297       @@plugins = Hash.new
298       scan
299     end
300
301     # return list of help topics (plugin names)
302     def helptopics
303       # Active plugins first
304       if(@@plugins.length > 0)
305         list = " [#{length} plugin#{'s' if length > 1}: " + @@plugins.values.uniq.collect{|p| p.name}.sort.join(", ")
306       else
307         list = " [no plugins active"
308       end
309       # Ignored plugins next
310       list << "; #{Underline}#{@ignored.length} plugin#{'s' if @ignored.length > 1} ignored#{Underline}: use #{Bold}help ignored plugins#{Bold} to see why" unless @ignored.empty?
311       # Failed plugins next
312       list << "; #{Reverse}#{@failed.length} plugin#{'s' if @failed.length > 1} failed to load#{Reverse}: use #{Bold}help failed plugins#{Bold} to see why" unless @failed.empty?
313       list << "]"
314       return list
315     end
316
317     def length
318       @@plugins.values.uniq.length
319     end
320
321     # return help for +topic+ (call associated plugin's help method)
322     def help(topic="")
323       case topic
324       when /fail(?:ed)?\s*plugins?.*(trace(?:back)?s?)?/
325         # debug "Failures: #{@failed.inspect}"
326         return "no plugins failed to load" if @failed.empty?
327         return (@failed.inject(Array.new) { |list, p|
328           list << "#{Bold}#{p[:name]}#{Bold} in #{p[:dir]} failed"
329           list << "with error #{p[:reason].class}: #{p[:reason]}"
330           list << "at #{p[:reason].backtrace.join(', ')}" if $1 and not p[:reason].backtrace.empty?
331           list
332         }).join("\n")
333       when /ignored?\s*plugins?/
334         return "no plugins were ignored" if @ignored.empty?
335         return (@ignored.inject(Array.new) { |list, p|
336           case p[:reason]
337           when :loaded
338             list << "#{p[:name]} in #{p[:dir]} (overruled by previous)"
339           else
340             list << "#{p[:name]} in #{p[:dir]} (#{p[:reason].to_s})"
341           end
342           list
343         }).join(", ")
344       when /^(\S+)\s*(.*)$/
345         key = $1
346         params = $2
347         if(@@plugins.has_key?(key))
348           begin
349             return @@plugins[key].help(key, params)
350           rescue Exception => err
351             #rescue TimeoutError, StandardError, NameError, SyntaxError => err
352             error "plugin #{@@plugins[key].name} help() failed: #{err.class}: #{err}"
353             error err.backtrace.join("\n")
354           end
355         else
356           return false
357         end
358       end
359     end
360
361     # see if each plugin handles +method+, and if so, call it, passing
362     # +message+ as a parameter
363     def delegate(method, *args)
364       @@plugins.values.uniq.each {|p|
365         if(p.respond_to? method)
366           begin
367             p.send method, *args
368           rescue Exception => err
369             #rescue TimeoutError, StandardError, NameError, SyntaxError => err
370             error "plugin #{p.name} #{method}() failed: #{err.class}: #{err}"
371             error err.backtrace.join("\n")
372           end
373         end
374       }
375     end
376
377     # see if we have a plugin that wants to handle this message, if so, pass
378     # it to the plugin and return true, otherwise false
379     def privmsg(m)
380       return unless(m.plugin)
381       if (@@plugins.has_key?(m.plugin) &&
382           @@plugins[m.plugin].respond_to?("privmsg") &&
383           @@bot.auth.allow?(m.plugin, m.source, m.replyto))
384         begin
385           @@plugins[m.plugin].privmsg(m)
386         rescue Exception => err
387           #rescue TimeoutError, StandardError, NameError, SyntaxError => err
388           error "plugin #{@@plugins[m.plugin].name} privmsg() failed: #{err.class}: #{err}"
389           error err.backtrace.join("\n")
390         end
391         return true
392       end
393       return false
394     end
395   end
396
397 end
398 end