]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - lib/rbot/plugins.rb
attempt to resolve #65
[user/henk/code/ruby/rbot.git] / 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       @registry.flush
104     end
105
106     def map(*args)
107       @handler.map(*args)
108       # register this map
109       name = @handler.last.items[0]
110       self.register name
111       unless self.respond_to?('privmsg')
112         def self.privmsg(m)
113           @handler.handle(m)
114         end
115       end
116     end
117
118     # return an identifier for this plugin, defaults to a list of the message
119     # prefixes handled (used for error messages etc)
120     def name
121       @names.join("|")
122     end
123     
124     # return a help string for your module. for complex modules, you may wish
125     # to break your help into topics, and return a list of available topics if
126     # +topic+ is nil. +plugin+ is passed containing the matching prefix for
127     # this message - if your plugin handles multiple prefixes, make sure your
128     # return the correct help for the prefix requested
129     def help(plugin, topic)
130       "no help"
131     end
132     
133     # register the plugin as a handler for messages prefixed +name+
134     # this can be called multiple times for a plugin to handle multiple
135     # message prefixes
136     def register(name)
137       return if Plugins.plugins.has_key?(name)
138       Plugins.plugins[name] = self
139       @names << name
140     end
141
142     # default usage method provided as a utility for simple plugins. The
143     # MessageMapper uses 'usage' as its default fallback method.
144     def usage(m, params = {})
145       m.reply "incorrect usage, ask for help using '#{@bot.nick}: help #{m.plugin}'"
146     end
147
148   end
149
150   # class to manage multiple plugins and delegate messages to them for
151   # handling
152   class Plugins
153     # hash of registered message prefixes and associated plugins
154     @@plugins = Hash.new
155     # associated IrcBot class
156     @@bot = nil
157
158     # bot::     associated IrcBot class
159     # dirlist:: array of directories to scan (in order) for plugins
160     #
161     # create a new plugin handler, scanning for plugins in +dirlist+
162     def initialize(bot, dirlist)
163       @@bot = bot
164       @dirs = dirlist
165       scan
166     end
167     
168     # access to associated bot
169     def Plugins.bot
170       @@bot
171     end
172
173     # access to list of plugins
174     def Plugins.plugins
175       @@plugins
176     end
177
178     # load plugins from pre-assigned list of directories
179     def scan
180       processed = Array.new
181       dirs = Array.new
182       dirs << Config::datadir + "/plugins"
183       dirs += @dirs
184       dirs.reverse.each {|dir|
185         if(FileTest.directory?(dir))
186           d = Dir.new(dir)
187           d.sort.each {|file|
188             next if(file =~ /^\./)
189             next if(processed.include?(file))
190             if(file =~ /^(.+\.rb)\.disabled$/)
191               processed << $1
192               next
193             end
194             next unless(file =~ /\.rb$/)
195             tmpfilename = "#{dir}/#{file}"
196
197             # create a new, anonymous module to "house" the plugin
198             # the idea here is to prevent namespace pollution. perhaps there
199             # is another way?
200             plugin_module = Module.new
201             
202             begin
203               plugin_string = IO.readlines(tmpfilename).join("")
204               debug "loading plugin #{tmpfilename}"
205               plugin_module.module_eval(plugin_string)
206               processed << file
207             rescue Exception => err
208               # rescue TimeoutError, StandardError, NameError, LoadError, SyntaxError => err
209               puts "warning: plugin #{tmpfilename} load failed: " + err
210               puts err.backtrace.join("\n")
211             end
212           }
213         end
214       }
215     end
216
217     # call the save method for each active plugin
218     def save
219       delegate 'flush_registry'
220       delegate 'save'
221     end
222
223     # call the cleanup method for each active plugin
224     def cleanup
225       delegate 'cleanup'
226     end
227
228     # drop all plugins and rescan plugins on disk
229     # calls save and cleanup for each plugin before dropping them
230     def rescan
231       save
232       cleanup
233       @@plugins = Hash.new
234       scan
235     end
236
237     # return list of help topics (plugin names)
238     def helptopics
239       if(@@plugins.length > 0)
240         # return " [plugins: " + @@plugins.keys.sort.join(", ") + "]"
241         return " [#{length} plugins: " + @@plugins.values.uniq.collect{|p| p.name}.sort.join(", ") + "]"
242       else
243         return " [no plugins active]" 
244       end
245     end
246
247     def length
248       @@plugins.values.uniq.length
249     end
250
251     # return help for +topic+ (call associated plugin's help method)
252     def help(topic="")
253       if(topic =~ /^(\S+)\s*(.*)$/)
254         key = $1
255         params = $2
256         if(@@plugins.has_key?(key))
257           begin
258             return @@plugins[key].help(key, params)
259           rescue Exception => err
260           #rescue TimeoutError, StandardError, NameError, SyntaxError => err
261             puts "plugin #{@@plugins[key].name} help() failed: " + err
262             puts err.backtrace.join("\n")
263           end
264         else
265           return false
266         end
267       end
268     end
269     
270     # see if each plugin handles +method+, and if so, call it, passing
271     # +message+ as a parameter
272     def delegate(method, *args)
273       @@plugins.values.uniq.each {|p|
274         if(p.respond_to? method)
275           begin
276             p.send method, *args
277           rescue Exception => err
278             #rescue TimeoutError, StandardError, NameError, SyntaxError => err
279             puts "plugin #{p.name} #{method}() failed: " + err
280             puts err.backtrace.join("\n")
281           end
282         end
283       }
284     end
285
286     # see if we have a plugin that wants to handle this message, if so, pass
287     # it to the plugin and return true, otherwise false
288     def privmsg(m)
289       return unless(m.plugin)
290       if (@@plugins.has_key?(m.plugin) &&
291           @@plugins[m.plugin].respond_to?("privmsg") &&
292           @@bot.auth.allow?(m.plugin, m.source, m.replyto))
293         begin
294           @@plugins[m.plugin].privmsg(m)
295         rescue Exception => err
296           #rescue TimeoutError, StandardError, NameError, SyntaxError => err
297           puts "plugin #{@@plugins[m.plugin].name} privmsg() failed: " + err
298           puts err.backtrace.join("\n")
299         end
300         return true
301       end
302       return false
303     end
304   end
305
306 end
307 end