1 # Copyright (C) 2002 Tom Gilbert.
3 # Permission is hereby granted, free of charge, to any person obtaining a copy
4 # of this software and associated documentation files (the "Software"), to
5 # deal in the Software without restriction, including without limitation the
6 # rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7 # sell copies of the Software, and to permit persons to whom the Software is
8 # furnished to do so, subject to the following conditions:
10 # The above copyright notice and this permission notice shall be included in
11 # all copies of the Software and its documentation and acknowledgment shall be
12 # given in the documentation and software packages that this Software was
15 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 # THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19 # IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20 # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 # this is the backend of the RegistryAccessor class, which ties it to a
27 # DBHash object called plugin_registry(.db). All methods are delegated to
33 @db = DBTree.new @bot, "plugin_registry"
37 def method_missing(method, *args, &block)
38 @db.send(method, *args, &block)
41 # check for older versions of rbot with data formats that require updating
42 # NB this function is called _early_ in init(), pretty much all you have to
43 # work with is @bot.botclass.
45 if File.exist?("#{@bot.botclass}/registry.db")
46 puts "upgrading old-style (rbot 0.9.5 or earlier) plugin registry to new format"
47 old = BDB::Hash.open "#{@bot.botclass}/registry.db", nil,
48 "r+", 0600, "set_pagesize" => 1024,
49 "set_cachesize" => [0, 32 * 1024, 0]
50 new = BDB::CIBtree.open "#{@bot.botclass}/plugin_registry.db", nil,
51 BDB::CREATE | BDB::EXCL | BDB::TRUNCATE,
52 0600, "set_pagesize" => 1024,
53 "set_cachesize" => [0, 32 * 1024, 0]
59 File.delete("#{@bot.botclass}/registry.db")
64 # This class provides persistent storage for plugins via a hash interface.
65 # The default mode is an object store, so you can store ruby objects and
66 # reference them with hash keys. This is because the default store/restore
67 # methods of the plugins' RegistryAccessor are calls to Marshal.dump and
72 # @registry[:blah] = blah
73 # then, even after the bot is shut down and disconnected, on the next run you
74 # can access the blah object as it was, with:
75 # blah = @registry[:blah]
76 # The registry can of course be used to store simple strings, fixnums, etc as
77 # well, and should be useful to store or cache plugin data or dynamic plugin
81 # in object store mode, don't make the mistake of treating it like a live
82 # object, e.g. (using the example above)
83 # @registry[:blah][:foo] = "flump"
84 # will NOT modify the object in the registry - remember that BotRegistry#[]
85 # returns a Marshal.restore'd object, the object you just modified in place
86 # will disappear. You would need to:
87 # blah = @registry[:blah]
88 # blah[:foo] = "flump"
89 # @registry[:blah] = blah
91 # If you don't need to store objects, and strictly want a persistant hash of
92 # strings, you can override the store/restore methods to suit your needs, for
93 # example (in your plugin):
104 # Your plugins section of the registry is private, it has its own namespace
105 # (derived from the plugin's class name, so change it and lose your data).
106 # Calls to registry.each etc, will only iterate over your namespace.
107 class BotRegistryAccessor
108 # plugins don't call this - a BotRegistryAccessor is created for them and
109 # is accessible via @registry.
110 def initialize(bot, prefix)
112 @registry = @bot.registry
113 @orig_prefix = prefix
114 @prefix = prefix + "/"
116 # debug "initializing registry accessor with prefix #{@prefix}"
119 # use this to chop up your namespace into bits, so you can keep and
120 # reference separate object stores under the same registry
121 def sub_registry(prefix)
122 return BotRegistryAccessor.new(@bot, @orig_prefix + "+" + prefix)
125 # convert value to string form for storing in the registry
126 # defaults to Marshal.dump(val) but you can override this in your module's
127 # registry object to use any method you like.
128 # For example, if you always just handle strings use:
136 # restores object from string form, restore(store(val)) must return val.
137 # If you override store, you should override restore to reverse the
139 # For example, if you always just handle strings use:
147 # lookup a key in the registry
149 if @registry.has_key?(@prefix + key)
150 return restore(@registry[@prefix + key])
151 elsif @default != nil
152 return restore(@default)
158 # set a key in the registry
160 @registry[@prefix + key] = store(value)
163 # set the default value for registry lookups, if the key sought is not
164 # found, the default will be returned. The default default (har) is nil.
165 def set_default (default)
166 @default = store(default)
169 # just like Hash#each
171 @registry.each {|key,value|
172 if key.gsub!(/^#{Regexp.escape(@prefix)}/, "")
173 block.call(key, restore(value))
178 # just like Hash#each_key
180 @registry.each {|key, value|
181 if key.gsub!(/^#{Regexp.escape(@prefix)}/, "")
187 # just like Hash#each_value
188 def each_value(&block)
189 @registry.each {|key, value|
190 if key =~ /^#{Regexp.escape(@prefix)}/
191 block.call(restore(value))
196 # just like Hash#has_key?
198 return @registry.has_key?(@prefix + key)
200 alias include? has_key?
201 alias member? has_key?
203 # just like Hash#has_both?
204 def has_both?(key, value)
205 return @registry.has_both?(@prefix + key, store(value))
208 # just like Hash#has_value?
209 def has_value?(value)
210 return @registry.has_value?(store(value))
213 # just like Hash#index?
215 ind = @registry.index(store(value))
216 if ind && ind.gsub!(/^#{Regexp.escape(@prefix)}/, "")
223 # delete a key from the registry
225 return @registry.delete(@prefix + key)
228 # returns a list of your keys
230 return @registry.keys.collect {|key|
231 if key.gsub!(/^#{Regexp.escape(@prefix)}/, "")
239 # Return an array of all associations [key, value] in your namespace
242 @registry.each {|key, value|
243 if key.gsub!(/^#{Regexp.escape(@prefix)}/, "")
244 ret << [key, restore(value)]
250 # Return an hash of all associations {key => value} in your namespace
253 @registry.each {|key, value|
254 if key.gsub!(/^#{Regexp.escape(@prefix)}/, "")
255 ret[key] = restore(value)
261 # empties the registry (restricted to your namespace)
263 @registry.each_key {|key|
264 if key =~ /^#{Regexp.escape(@prefix)}/
265 @registry.delete(key)
271 # returns an array of the values in your namespace of the registry
280 # returns the number of keys in your registry namespace