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