]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - lib/rbot/plugins.rb
d4e5be9fa614bbef4ee35f3b6542897dc701aa7d
[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.sort.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             # the idea here is to prevent namespace pollution. perhaps there
185             # is another way?
186             plugin_module = Module.new
187             
188             begin
189               plugin_string = IO.readlines(@tmpfilename).join("")
190               debug "loading module: #{@tmpfilename}"
191               plugin_module.module_eval(plugin_string)
192             rescue StandardError, NameError, LoadError, SyntaxError => err
193               puts "warning: plugin #{@tmpfilename} load failed: " + err
194               puts err.backtrace.join("\n")
195             end
196           }
197         end
198       }
199     end
200
201     # call the save method for each active plugin
202     def save
203       delegate 'save'
204     end
205
206     # call the cleanup method for each active plugin
207     def cleanup
208       delegate 'cleanup'
209     end
210
211     # drop all plugins and rescan plugins on disk
212     # calls save and cleanup for each plugin before dropping them
213     def rescan
214       save
215       cleanup
216       @@plugins = Hash.new
217       scan
218     end
219
220     # return list of help topics (plugin names)
221     def helptopics
222       if(@@plugins.length > 0)
223         # return " [plugins: " + @@plugins.keys.sort.join(", ") + "]"
224         return " [#{length} plugins: " + @@plugins.values.uniq.collect{|p| p.name}.sort.join(", ") + "]"
225       else
226         return " [no plugins active]" 
227       end
228     end
229
230     def length
231       @@plugins.values.uniq.length
232     end
233
234     # return help for +topic+ (call associated plugin's help method)
235     def help(topic="")
236       if(topic =~ /^(\S+)\s*(.*)$/)
237         key = $1
238         params = $2
239         if(@@plugins.has_key?(key))
240           begin
241             return @@plugins[key].help(key, params)
242           rescue StandardError, NameError, SyntaxError => err
243             puts "plugin #{@@plugins[key].name} help() failed: " + err
244             puts err.backtrace.join("\n")
245           end
246         else
247           return false
248         end
249       end
250     end
251     
252     # see if each plugin handles +method+, and if so, call it, passing
253     # +message+ as a parameter
254     def delegate(method, *args)
255       @@plugins.values.uniq.each {|p|
256         if(p.respond_to? method)
257           begin
258             p.send method, *args
259           rescue StandardError, NameError, SyntaxError => err
260             puts "plugin #{p.name} #{method}() failed: " + err
261             puts err.backtrace.join("\n")
262           end
263         end
264       }
265     end
266
267     # see if we have a plugin that wants to handle this message, if so, pass
268     # it to the plugin and return true, otherwise false
269     def privmsg(m)
270       return unless(m.plugin)
271       if (@@plugins.has_key?(m.plugin) &&
272           @@plugins[m.plugin].respond_to?("privmsg") &&
273           @@bot.auth.allow?(m.plugin, m.source, m.replyto))
274         begin
275           @@plugins[m.plugin].privmsg(m)
276         rescue StandardError, NameError, SyntaxError => err
277           puts "plugin #{@@plugins[m.plugin].name} privmsg() failed: " + err
278           puts err.backtrace.join("\n")
279         end
280         return true
281       end
282       return false
283     end
284   end
285
286 end