6 # This class is now used purely for upgrading from prior versions of rbot
7 # the new registry is split into multiple DBHash objects, one per plugin
15 # check for older versions of rbot with data formats that require updating
16 # NB this function is called _early_ in init(), pretty much all you have to
17 # work with is @bot.botclass.
19 oldreg = @bot.path 'registry.db'
20 newreg = @bot.path 'plugin_registry.db'
21 if File.exist?(oldreg)
22 log _("upgrading old-style (rbot 0.9.5 or earlier) plugin registry to new format")
23 old = BDB::Hash.open(oldreg, nil, "r+", 0600)
24 new = BDB::CIBtree.open(newreg, nil, BDB::CREATE | BDB::EXCL, 0600)
30 File.rename(oldreg, oldreg + ".old")
35 oldreg = @bot.path 'plugin_registry.db'
36 newdir = @bot.path 'registry'
37 if File.exist?(oldreg)
38 Dir.mkdir(newdir) unless File.exist?(newdir)
39 env = BDB::Env.open(@bot.botclass, BDB::INIT_TRANSACTION | BDB::CREATE | BDB::RECOVER)# | BDB::TXN_NOSYNC)
41 log _("upgrading previous (rbot 0.9.9 or earlier) plugin registry to new split format")
42 old = BDB::CIBtree.open(oldreg, nil, "r+", 0600, "env" => env)
44 prefix,key = k.split("/", 2)
46 # subregistries were split with a +, now they are in separate folders
47 if prefix.gsub!(/\+/, "/")
48 # Ok, this code needs to be put in the db opening routines
49 dirs = File.dirname("#{@bot.botclass}/registry/#{prefix}.db").split("/")
50 dirs.length.times { |i|
51 dir = dirs[0,i+1].join("/")+"/"
52 unless File.exist?(dir)
53 log _("creating subregistry directory #{dir}")
58 unless dbs.has_key?(prefix)
59 log _("creating db #{@bot.botclass}/registry/#{prefix}.db")
60 dbs[prefix] = BDB::CIBtree.open("#{@bot.botclass}/registry/#{prefix}.db",
61 nil, BDB::CREATE | BDB::EXCL,
67 File.rename(oldreg, oldreg + ".old")
69 log _("closing db #{k}")
77 # This class provides persistent storage for plugins via a hash interface.
78 # The default mode is an object store, so you can store ruby objects and
79 # reference them with hash keys. This is because the default store/restore
80 # methods of the plugins' RegistryAccessor are calls to Marshal.dump and
85 # @registry[:blah] = blah
86 # then, even after the bot is shut down and disconnected, on the next run you
87 # can access the blah object as it was, with:
88 # blah = @registry[:blah]
89 # The registry can of course be used to store simple strings, fixnums, etc as
90 # well, and should be useful to store or cache plugin data or dynamic plugin
94 # in object store mode, don't make the mistake of treating it like a live
95 # object, e.g. (using the example above)
96 # @registry[:blah][:foo] = "flump"
97 # will NOT modify the object in the registry - remember that Registry#[]
98 # returns a Marshal.restore'd object, the object you just modified in place
99 # will disappear. You would need to:
100 # blah = @registry[:blah]
101 # blah[:foo] = "flump"
102 # @registry[:blah] = blah
104 # If you don't need to store objects, and strictly want a persistant hash of
105 # strings, you can override the store/restore methods to suit your needs, for
106 # example (in your plugin):
117 # Your plugins section of the registry is private, it has its own namespace
118 # (derived from the plugin's class name, so change it and lose your data).
119 # Calls to registry.each etc, will only iterate over your namespace.
122 attr_accessor :recovery
124 # plugins don't call this - a Registry::Accessor is created for them and
125 # is accessible via @registry.
126 def initialize(bot, name)
128 @name = name.downcase
129 @filename = @bot.path 'registry', @name
130 dirs = File.dirname(@filename).split("/")
131 dirs.length.times { |i|
132 dir = dirs[0,i+1].join("/")+"/"
133 unless File.exist?(dir)
134 debug "creating subregistry directory #{dir}"
142 # debug "initializing registry accessor with name #{@name}"
146 @registry ||= DBTree.new @bot, "registry/#{@name}"
150 # debug "fushing registry #{registry}"
157 # debug "closing registry #{registry}"
162 # convert value to string form for storing in the registry
163 # defaults to Marshal.dump(val) but you can override this in your module's
164 # registry object to use any method you like.
165 # For example, if you always just handle strings use:
173 # restores object from string form, restore(store(val)) must return val.
174 # If you override store, you should override restore to reverse the
176 # For example, if you always just handle strings use:
183 rescue Exception => e
184 error _("failed to restore marshal data for #{val.inspect}, attempting recovery or fallback to default")
186 if defined? @recovery and @recovery
188 return @recovery.call(val)
189 rescue Exception => ee
190 error _("marshal recovery failed, trying default")
198 # lookup a key in the registry
200 if File.exist?(@filename) && registry.has_key?(key)
201 return restore(registry[key])
207 # set a key in the registry
209 registry[key] = store(value)
212 # set the default value for registry lookups, if the key sought is not
213 # found, the default will be returned. The default default (har) is nil.
214 def set_default (default)
219 @default && (@default.dup rescue @default)
222 # just like Hash#each
223 def each(set=nil, bulk=0, &block)
224 return nil unless File.exist?(@filename)
225 registry.each(set, bulk) {|key,value|
226 block.call(key, restore(value))
230 # just like Hash#each_key
231 def each_key(set=nil, bulk=0, &block)
232 return nil unless File.exist?(@filename)
233 registry.each_key(set, bulk) {|key|
238 # just like Hash#each_value
239 def each_value(set=nil, bulk=0, &block)
240 return nil unless File.exist?(@filename)
241 registry.each_value(set, bulk) { |value|
242 block.call(restore(value))
246 # just like Hash#has_key?
248 return false unless File.exist?(@filename)
249 return registry.has_key?(key)
251 alias include? has_key?
252 alias member? has_key?
255 # just like Hash#has_both?
256 def has_both?(key, value)
257 return false unless File.exist?(@filename)
258 return registry.has_both?(key, store(value))
261 # just like Hash#has_value?
262 def has_value?(value)
263 return false unless File.exist?(@filename)
264 return registry.has_value?(store(value))
267 # just like Hash#index?
269 return nil unless File.exist?(@filename)
270 ind = registry.index(store(value))
278 # delete a key from the registry
280 return default unless File.exist?(@filename)
281 return registry.delete(key)
284 # returns a list of your keys
286 return [] unless File.exist?(@filename)
290 # Return an array of all associations [key, value] in your namespace
292 return [] unless File.exist?(@filename)
294 registry.each {|key, value|
295 ret << [key, restore(value)]
300 # Return an hash of all associations {key => value} in your namespace
302 return {} unless File.exist?(@filename)
304 registry.each {|key, value|
305 ret[key] = restore(value)
310 # empties the registry (restricted to your namespace)
312 return true unless File.exist?(@filename)
317 # returns an array of the values in your namespace of the registry
319 return [] unless File.exist?(@filename)
327 def sub_registry(prefix)
328 return Accessor.new(@bot, @name + "/" + prefix.to_s)
331 # returns the number of keys in your registry namespace