Include exception class when plugins fail
[rbot] / lib / rbot / plugins.rb
1 module Irc
2 module Plugins
3   require 'rbot/messagemapper'
4
5   # base class for all rbot plugins
6   # certain methods will be called if they are provided, if you define one of
7   # the following methods, it will be called as appropriate:
8   #
9   # map(template, options)::
10   #    map is the new, cleaner way to respond to specific message formats
11   #    without littering your plugin code with regexps. examples:
12   #
13   #      plugin.map 'karmastats', :action => 'karma_stats'
14   #
15   #      # while in the plugin...
16   #      def karma_stats(m, params)
17   #        m.reply "..."
18   #      end
19   #
20   #      # the default action is the first component
21   #      plugin.map 'karma'
22   #
23   #      # attributes can be pulled out of the match string
24   #      plugin.map 'karma for :key'
25   #      plugin.map 'karma :key'
26   #
27   #      # while in the plugin...
28   #      def karma(m, params)
29   #        item = params[:key]
30   #        m.reply 'karma for #{item}'
31   #      end
32   #
33   #      # you can setup defaults, to make parameters optional
34   #      plugin.map 'karma :key', :defaults => {:key => 'defaultvalue'}
35   #
36   #      # the default auth check is also against the first component
37   #      # but that can be changed
38   #      plugin.map 'karmastats', :auth => 'karma'
39   #
40   #      # maps can be restricted to public or private message:
41   #      plugin.map 'karmastats', :private false,
42   #      plugin.map 'karmastats', :public false,
43   #    end
44   #
45   #    To activate your maps, you simply register them
46   #    plugin.register_maps
47   #    This also sets the privmsg handler to use the map lookups for
48   #    handling messages. You can still use listen(), kick() etc methods
49   #
50   # listen(UserMessage)::
51   #                        Called for all messages of any type. To
52   #                        differentiate them, use message.kind_of? It'll be
53   #                        either a PrivMessage, NoticeMessage, KickMessage,
54   #                        QuitMessage, PartMessage, JoinMessage, NickMessage,
55   #                        etc.
56   #
57   # privmsg(PrivMessage)::
58   #                        called for a PRIVMSG if the first word matches one
59   #                        the plugin register()d for. Use m.plugin to get
60   #                        that word and m.params for the rest of the message,
61   #                        if applicable.
62   #
63   # kick(KickMessage)::
64   #                        Called when a user (or the bot) is kicked from a
65   #                        channel the bot is in.
66   #
67   # join(JoinMessage)::
68   #                        Called when a user (or the bot) joins a channel
69   #
70   # part(PartMessage)::
71   #                        Called when a user (or the bot) parts a channel
72   #
73   # quit(QuitMessage)::
74   #                        Called when a user (or the bot) quits IRC
75   #
76   # nick(NickMessage)::
77   #                        Called when a user (or the bot) changes Nick
78   # topic(TopicMessage)::
79   #                        Called when a user (or the bot) changes a channel
80   #                        topic
81   #
82   # connect()::            Called when a server is joined successfully, but
83   #                        before autojoin channels are joined (no params)
84   #
85   # save::                 Called when you are required to save your plugin's
86   #                        state, if you maintain data between sessions
87   #
88   # cleanup::              called before your plugin is "unloaded", prior to a
89   #                        plugin reload or bot quit - close any open
90   #                        files/connections or flush caches here
91   class Plugin
92     attr_reader :bot   # the associated bot
93     # initialise your plugin. Always call super if you override this method,
94     # as important variables are set up for you
95     def initialize
96       @bot = Plugins.bot
97       @names = Array.new
98       @handler = MessageMapper.new(self)
99       @registry = BotRegistryAccessor.new(@bot, self.class.to_s.gsub(/^.*::/, ""))
100     end
101
102     def flush_registry
103       # debug "Flushing #{@registry}"
104       @registry.flush
105     end
106
107     def cleanup
108       # debug "Closing #{@registry}"
109       @registry.close
110     end
111
112     def map(*args)
113       @handler.map(*args)
114       # register this map
115       name = @handler.last.items[0]
116       self.register name
117       unless self.respond_to?('privmsg')
118         def self.privmsg(m)
119           @handler.handle(m)
120         end
121       end
122     end
123
124     # return an identifier for this plugin, defaults to a list of the message
125     # prefixes handled (used for error messages etc)
126     def name
127       @names.join("|")
128     end
129
130     # return a help string for your module. for complex modules, you may wish
131     # to break your help into topics, and return a list of available topics if
132     # +topic+ is nil. +plugin+ is passed containing the matching prefix for
133     # this message - if your plugin handles multiple prefixes, make sure your
134     # return the correct help for the prefix requested
135     def help(plugin, topic)
136       "no help"
137     end
138
139     # register the plugin as a handler for messages prefixed +name+
140     # this can be called multiple times for a plugin to handle multiple
141     # message prefixes
142     def register(name)
143       return if Plugins.plugins.has_key?(name)
144       Plugins.plugins[name] = self
145       @names << name
146     end
147
148     # default usage method provided as a utility for simple plugins. The
149     # MessageMapper uses 'usage' as its default fallback method.
150     def usage(m, params = {})
151       m.reply "incorrect usage, ask for help using '#{@bot.nick}: help #{m.plugin}'"
152     end
153
154   end
155
156   # class to manage multiple plugins and delegate messages to them for
157   # handling
158   class Plugins
159     # hash of registered message prefixes and associated plugins
160     @@plugins = Hash.new
161     # associated IrcBot class
162     @@bot = nil
163
164     # bot::     associated IrcBot class
165     # dirlist:: array of directories to scan (in order) for plugins
166     #
167     # create a new plugin handler, scanning for plugins in +dirlist+
168     def initialize(bot, dirlist)
169       @@bot = bot
170       @dirs = dirlist
171       scan
172     end
173
174     # access to associated bot
175     def Plugins.bot
176       @@bot
177     end
178
179     # access to list of plugins
180     def Plugins.plugins
181       @@plugins
182     end
183
184     # load plugins from pre-assigned list of directories
185     def scan
186       processed = Array.new
187       dirs = Array.new
188       dirs << Config::datadir + "/plugins"
189       dirs += @dirs
190       dirs.reverse.each {|dir|
191         if(FileTest.directory?(dir))
192           d = Dir.new(dir)
193           d.sort.each {|file|
194             next if(file =~ /^\./)
195             next if(processed.include?(file))
196             if(file =~ /^(.+\.rb)\.disabled$/)
197               processed << $1
198               next
199             end
200             next unless(file =~ /\.rb$/)
201             tmpfilename = "#{dir}/#{file}"
202
203             # create a new, anonymous module to "house" the plugin
204             # the idea here is to prevent namespace pollution. perhaps there
205             # is another way?
206             plugin_module = Module.new
207
208             begin
209               plugin_string = IO.readlines(tmpfilename).join("")
210               debug "loading plugin #{tmpfilename}"
211               plugin_module.module_eval(plugin_string)
212               processed << file
213             rescue Exception => err
214               # rescue TimeoutError, StandardError, NameError, LoadError, SyntaxError => err
215               puts "warning: plugin #{tmpfilename} load failed: " + err
216               puts err.backtrace.join("\n")
217             end
218           }
219         end
220       }
221     end
222
223     # call the save method for each active plugin
224     def save
225       delegate 'flush_registry'
226       delegate 'save'
227     end
228
229     # call the cleanup method for each active plugin
230     def cleanup
231       delegate 'cleanup'
232     end
233
234     # drop all plugins and rescan plugins on disk
235     # calls save and cleanup for each plugin before dropping them
236     def rescan
237       save
238       cleanup
239       @@plugins = Hash.new
240       scan
241     end
242
243     # return list of help topics (plugin names)
244     def helptopics
245       if(@@plugins.length > 0)
246         # return " [plugins: " + @@plugins.keys.sort.join(", ") + "]"
247         return " [#{length} plugins: " + @@plugins.values.uniq.collect{|p| p.name}.sort.join(", ") + "]"
248       else
249         return " [no plugins active]" 
250       end
251     end
252
253     def length
254       @@plugins.values.uniq.length
255     end
256
257     # return help for +topic+ (call associated plugin's help method)
258     def help(topic="")
259       if(topic =~ /^(\S+)\s*(.*)$/)
260         key = $1
261         params = $2
262         if(@@plugins.has_key?(key))
263           begin
264             return @@plugins[key].help(key, params)
265           rescue Exception => err
266           #rescue TimeoutError, StandardError, NameError, SyntaxError => err
267             puts "plugin #{@@plugins[key].name} help() failed: #{err.class}: #{err}"
268             puts err.backtrace.join("\n")
269           end
270         else
271           return false
272         end
273       end
274     end
275
276     # see if each plugin handles +method+, and if so, call it, passing
277     # +message+ as a parameter
278     def delegate(method, *args)
279       @@plugins.values.uniq.each {|p|
280         if(p.respond_to? method)
281           begin
282             p.send method, *args
283           rescue Exception => err
284             #rescue TimeoutError, StandardError, NameError, SyntaxError => err
285             puts "plugin #{p.name} #{method}() failed: #{err.class}: #{err}"
286             puts err.backtrace.join("\n")
287           end
288         end
289       }
290     end
291
292     # see if we have a plugin that wants to handle this message, if so, pass
293     # it to the plugin and return true, otherwise false
294     def privmsg(m)
295       return unless(m.plugin)
296       if (@@plugins.has_key?(m.plugin) &&
297           @@plugins[m.plugin].respond_to?("privmsg") &&
298           @@bot.auth.allow?(m.plugin, m.source, m.replyto))
299         begin
300           @@plugins[m.plugin].privmsg(m)
301         rescue Exception => err
302           #rescue TimeoutError, StandardError, NameError, SyntaxError => err
303           puts "plugin #{@@plugins[m.plugin].name} privmsg() failed: #{err.class}: #{err}"
304           puts err.backtrace.join("\n")
305         end
306         return true
307       end
308       return false
309     end
310   end
311
312 end
313 end