New configuration option plugins.blacklist holding an array of plugins to be blacklis...
[rbot] / 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 is the new, cleaner way to respond to specific message formats
14   #    without littering your plugin code with regexps. examples:
15   #
16   #      plugin.map 'karmastats', :action => 'karma_stats'
17   #
18   #      # while in the plugin...
19   #      def karma_stats(m, params)
20   #        m.reply "..."
21   #      end
22   #
23   #      # the default action is the first component
24   #      plugin.map 'karma'
25   #
26   #      # attributes can be pulled out of the match string
27   #      plugin.map 'karma for :key'
28   #      plugin.map 'karma :key'
29   #
30   #      # while in the plugin...
31   #      def karma(m, params)
32   #        item = params[:key]
33   #        m.reply 'karma for #{item}'
34   #      end
35   #
36   #      # you can setup defaults, to make parameters optional
37   #      plugin.map 'karma :key', :defaults => {:key => 'defaultvalue'}
38   #
39   #      # the default auth check is also against the first component
40   #      # but that can be changed
41   #      plugin.map 'karmastats', :auth => 'karma'
42   #
43   #      # maps can be restricted to public or private message:
44   #      plugin.map 'karmastats', :private false,
45   #      plugin.map 'karmastats', :public false,
46   #    end
47   #
48   #    To activate your maps, you simply register them
49   #    plugin.register_maps
50   #    This also sets the privmsg handler to use the map lookups for
51   #    handling messages. You can still use listen(), kick() etc methods
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 map(*args)
116       @handler.map(*args)
117       # register this map
118       name = @handler.last.items[0]
119       self.register name
120       unless self.respond_to?('privmsg')
121         def self.privmsg(m)
122           @handler.handle(m)
123         end
124       end
125     end
126
127     # return an identifier for this plugin, defaults to a list of the message
128     # prefixes handled (used for error messages etc)
129     def name
130       @names.join("|")
131     end
132
133     # return a help string for your module. for complex modules, you may wish
134     # to break your help into topics, and return a list of available topics if
135     # +topic+ is nil. +plugin+ is passed containing the matching prefix for
136     # this message - if your plugin handles multiple prefixes, make sure your
137     # return the correct help for the prefix requested
138     def help(plugin, topic)
139       "no help"
140     end
141
142     # register the plugin as a handler for messages prefixed +name+
143     # this can be called multiple times for a plugin to handle multiple
144     # message prefixes
145     def register(name)
146       return if Plugins.plugins.has_key?(name)
147       Plugins.plugins[name] = self
148       @names << name
149     end
150
151     # default usage method provided as a utility for simple plugins. The
152     # MessageMapper uses 'usage' as its default fallback method.
153     def usage(m, params = {})
154       m.reply "incorrect usage, ask for help using '#{@bot.nick}: help #{m.plugin}'"
155     end
156
157   end
158
159   # class to manage multiple plugins and delegate messages to them for
160   # handling
161   class Plugins
162     # hash of registered message prefixes and associated plugins
163     @@plugins = Hash.new
164     # associated IrcBot class
165     @@bot = nil
166
167     # bot::     associated IrcBot class
168     # dirlist:: array of directories to scan (in order) for plugins
169     #
170     # create a new plugin handler, scanning for plugins in +dirlist+
171     def initialize(bot, dirlist)
172       @@bot = bot
173       @dirs = dirlist
174       @blacklist = Array.new
175       @@bot.config['plugins.blacklist'].each { |p|
176         @blacklist << p+".rb"
177       }
178       scan
179     end
180
181     # access to associated bot
182     def Plugins.bot
183       @@bot
184     end
185
186     # access to list of plugins
187     def Plugins.plugins
188       @@plugins
189     end
190
191     # load plugins from pre-assigned list of directories
192     def scan
193       processed = @blacklist
194       dirs = Array.new
195       dirs << Config::datadir + "/plugins"
196       dirs += @dirs
197       dirs.reverse.each {|dir|
198         if(FileTest.directory?(dir))
199           d = Dir.new(dir)
200           d.sort.each {|file|
201             next if(file =~ /^\./)
202             next if(processed.include?(file))
203             if(file =~ /^(.+\.rb)\.disabled$/)
204               processed << $1
205               next
206             end
207             next unless(file =~ /\.rb$/)
208             tmpfilename = "#{dir}/#{file}"
209
210             # create a new, anonymous module to "house" the plugin
211             # the idea here is to prevent namespace pollution. perhaps there
212             # is another way?
213             plugin_module = Module.new
214
215             begin
216               plugin_string = IO.readlines(tmpfilename).join("")
217               debug "loading plugin #{tmpfilename}"
218               plugin_module.module_eval(plugin_string)
219               processed << file
220             rescue Exception => err
221               # rescue TimeoutError, StandardError, NameError, LoadError, SyntaxError => err
222               warning "plugin #{tmpfilename} load failed: " + err.inspect
223               warning err.backtrace.join("\n")
224             end
225           }
226         end
227       }
228     end
229
230     # call the save method for each active plugin
231     def save
232       delegate 'flush_registry'
233       delegate 'save'
234     end
235
236     # call the cleanup method for each active plugin
237     def cleanup
238       delegate 'cleanup'
239     end
240
241     # drop all plugins and rescan plugins on disk
242     # calls save and cleanup for each plugin before dropping them
243     def rescan
244       save
245       cleanup
246       @@plugins = Hash.new
247       scan
248     end
249
250     # return list of help topics (plugin names)
251     def helptopics
252       if(@@plugins.length > 0)
253         # return " [plugins: " + @@plugins.keys.sort.join(", ") + "]"
254         return " [#{length} plugins: " + @@plugins.values.uniq.collect{|p| p.name}.sort.join(", ") + "]"
255       else
256         return " [no plugins active]" 
257       end
258     end
259
260     def length
261       @@plugins.values.uniq.length
262     end
263
264     # return help for +topic+ (call associated plugin's help method)
265     def help(topic="")
266       if(topic =~ /^(\S+)\s*(.*)$/)
267         key = $1
268         params = $2
269         if(@@plugins.has_key?(key))
270           begin
271             return @@plugins[key].help(key, params)
272           rescue Exception => err
273           #rescue TimeoutError, StandardError, NameError, SyntaxError => err
274             error "plugin #{@@plugins[key].name} help() failed: #{err.class}: #{err}"
275             error err.backtrace.join("\n")
276           end
277         else
278           return false
279         end
280       end
281     end
282
283     # see if each plugin handles +method+, and if so, call it, passing
284     # +message+ as a parameter
285     def delegate(method, *args)
286       @@plugins.values.uniq.each {|p|
287         if(p.respond_to? method)
288           begin
289             p.send method, *args
290           rescue Exception => err
291             #rescue TimeoutError, StandardError, NameError, SyntaxError => err
292             error "plugin #{p.name} #{method}() failed: #{err.class}: #{err}"
293             error err.backtrace.join("\n")
294           end
295         end
296       }
297     end
298
299     # see if we have a plugin that wants to handle this message, if so, pass
300     # it to the plugin and return true, otherwise false
301     def privmsg(m)
302       return unless(m.plugin)
303       if (@@plugins.has_key?(m.plugin) &&
304           @@plugins[m.plugin].respond_to?("privmsg") &&
305           @@bot.auth.allow?(m.plugin, m.source, m.replyto))
306         begin
307           @@plugins[m.plugin].privmsg(m)
308         rescue Exception => err
309           #rescue TimeoutError, StandardError, NameError, SyntaxError => err
310           error "plugin #{@@plugins[m.plugin].name} privmsg() failed: #{err.class}: #{err}"
311           error err.backtrace.join("\n")
312         end
313         return true
314       end
315       return false
316     end
317   end
318
319 end
320 end