5 # this is the backend of the RegistryAccessor class, which ties it to a
6 # DBHash object called plugin_registry(.db). All methods are delegated to
12 @db = DBTree.new @bot, "plugin_registry"
16 def method_missing(method, *args, &block)
17 @db.send(method, *args, &block)
20 # check for older versions of rbot with data formats that require updating
21 # NB this function is called _early_ in init(), pretty much all you have to
22 # work with is @bot.botclass.
24 if File.exist?("#{@bot.botclass}/registry.db")
25 puts "upgrading old-style (rbot 0.9.5 or earlier) plugin registry to new format"
26 old = BDB::Hash.open "#{@bot.botclass}/registry.db", nil,
27 "r+", 0600, "set_pagesize" => 1024,
28 "set_cachesize" => [0, 32 * 1024, 0]
29 new = BDB::CIBtree.open "#{@bot.botclass}/plugin_registry.db", nil,
30 BDB::CREATE | BDB::EXCL | BDB::TRUNCATE,
31 0600, "set_pagesize" => 1024,
32 "set_cachesize" => [0, 32 * 1024, 0]
38 File.delete("#{@bot.botclass}/registry.db")
43 # This class provides persistent storage for plugins via a hash interface.
44 # The default mode is an object store, so you can store ruby objects and
45 # reference them with hash keys. This is because the default store/restore
46 # methods of the plugins' RegistryAccessor are calls to Marshal.dump and
51 # @registry[:blah] = blah
52 # then, even after the bot is shut down and disconnected, on the next run you
53 # can access the blah object as it was, with:
54 # blah = @registry[:blah]
55 # The registry can of course be used to store simple strings, fixnums, etc as
56 # well, and should be useful to store or cache plugin data or dynamic plugin
60 # in object store mode, don't make the mistake of treating it like a live
61 # object, e.g. (using the example above)
62 # @registry[:blah][:foo] = "flump"
63 # will NOT modify the object in the registry - remember that BotRegistry#[]
64 # returns a Marshal.restore'd object, the object you just modified in place
65 # will disappear. You would need to:
66 # blah = @registry[:blah]
67 # blah[:foo] = "flump"
68 # @registry[:blah] = blah
70 # If you don't need to store objects, and strictly want a persistant hash of
71 # strings, you can override the store/restore methods to suit your needs, for
72 # example (in your plugin):
83 # Your plugins section of the registry is private, it has its own namespace
84 # (derived from the plugin's class name, so change it and lose your data).
85 # Calls to registry.each etc, will only iterate over your namespace.
86 class BotRegistryAccessor
87 # plugins don't call this - a BotRegistryAccessor is created for them and
88 # is accessible via @registry.
89 def initialize(bot, prefix)
91 @registry = @bot.registry
93 @prefix = prefix + "/"
95 # debug "initializing registry accessor with prefix #{@prefix}"
98 # use this to chop up your namespace into bits, so you can keep and
99 # reference separate object stores under the same registry
100 def sub_registry(prefix)
101 return BotRegistryAccessor.new(@bot, @orig_prefix + "+" + prefix)
104 # convert value to string form for storing in the registry
105 # defaults to Marshal.dump(val) but you can override this in your module's
106 # registry object to use any method you like.
107 # For example, if you always just handle strings use:
115 # restores object from string form, restore(store(val)) must return val.
116 # If you override store, you should override restore to reverse the
118 # For example, if you always just handle strings use:
126 $stderr.puts "failed to restore marshal data, falling back to default"
129 return Marshal.restore(@default)
139 # lookup a key in the registry
141 if @registry.has_key?(@prefix + key)
142 return restore(@registry[@prefix + key])
143 elsif @default != nil
144 return restore(@default)
150 # set a key in the registry
152 @registry[@prefix + key] = store(value)
155 # set the default value for registry lookups, if the key sought is not
156 # found, the default will be returned. The default default (har) is nil.
157 def set_default (default)
158 @default = store(default)
161 # just like Hash#each
163 @registry.each {|key,value|
164 if key.gsub!(/^#{Regexp.escape(@prefix)}/, "")
165 block.call(key, restore(value))
170 # just like Hash#each_key
172 @registry.each {|key, value|
173 if key.gsub!(/^#{Regexp.escape(@prefix)}/, "")
179 # just like Hash#each_value
180 def each_value(&block)
181 @registry.each {|key, value|
182 if key =~ /^#{Regexp.escape(@prefix)}/
183 block.call(restore(value))
188 # just like Hash#has_key?
190 return @registry.has_key?(@prefix + key)
192 alias include? has_key?
193 alias member? has_key?
195 # just like Hash#has_both?
196 def has_both?(key, value)
197 return @registry.has_both?(@prefix + key, store(value))
200 # just like Hash#has_value?
201 def has_value?(value)
202 return @registry.has_value?(store(value))
205 # just like Hash#index?
207 ind = @registry.index(store(value))
208 if ind && ind.gsub!(/^#{Regexp.escape(@prefix)}/, "")
215 # delete a key from the registry
217 return @registry.delete(@prefix + key)
220 # returns a list of your keys
222 return @registry.keys.collect {|key|
223 if key.gsub!(/^#{Regexp.escape(@prefix)}/, "")
231 # Return an array of all associations [key, value] in your namespace
234 @registry.each {|key, value|
235 if key.gsub!(/^#{Regexp.escape(@prefix)}/, "")
236 ret << [key, restore(value)]
242 # Return an hash of all associations {key => value} in your namespace
245 @registry.each {|key, value|
246 if key.gsub!(/^#{Regexp.escape(@prefix)}/, "")
247 ret[key] = restore(value)
253 # empties the registry (restricted to your namespace)
255 @registry.each_key {|key|
256 if key =~ /^#{Regexp.escape(@prefix)}/
257 @registry.delete(key)
263 # returns an array of the values in your namespace of the registry
272 # returns the number of keys in your registry namespace