5 # this class is now used purely for upgrading from prior versions of rbot
6 # the new registry is split into multiple DBHash objects, one per plugin
14 # check for older versions of rbot with data formats that require updating
15 # NB this function is called _early_ in init(), pretty much all you have to
16 # work with is @bot.botclass.
18 if File.exist?("#{@bot.botclass}/registry.db")
19 log "upgrading old-style (rbot 0.9.5 or earlier) plugin registry to new format"
20 old = BDB::Hash.open("#{@bot.botclass}/registry.db", nil,
22 new = BDB::CIBtree.open("#{@bot.botclass}/plugin_registry.db", nil,
23 BDB::CREATE | BDB::EXCL,
30 File.rename("#{@bot.botclass}/registry.db", "#{@bot.botclass}/registry.db.old")
35 if File.exist?("#{@bot.botclass}/plugin_registry.db")
36 Dir.mkdir("#{@bot.botclass}/registry") unless File.exist?("#{@bot.botclass}/registry")
37 env = BDB::Env.open("#{@bot.botclass}", BDB::INIT_TRANSACTION | BDB::CREATE | BDB::RECOVER)# | BDB::TXN_NOSYNC)
39 log "upgrading previous (rbot 0.9.9 or earlier) plugin registry to new split format"
40 old = BDB::CIBtree.open("#{@bot.botclass}/plugin_registry.db", nil,
41 "r+", 0600, "env" => env)
43 prefix,key = k.split("/", 2)
45 # subregistries were split with a +, now they are in separate folders
46 if prefix.gsub!(/\+/, "/")
47 # Ok, this code needs to be put in the db opening routines
48 dirs = File.dirname("#{@bot.botclass}/registry/#{prefix}.db").split("/")
49 dirs.length.times { |i|
50 dir = dirs[0,i+1].join("/")+"/"
51 unless File.exist?(dir)
52 log "creating subregistry directory #{dir}"
57 unless dbs.has_key?(prefix)
58 log "creating db #{@bot.botclass}/registry/#{prefix}.db"
59 dbs[prefix] = BDB::CIBtree.open("#{@bot.botclass}/registry/#{prefix}.db",
60 nil, BDB::CREATE | BDB::EXCL,
66 File.rename("#{@bot.botclass}/plugin_registry.db", "#{@bot.botclass}/plugin_registry.db.old")
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 BotRegistry#[]
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.
120 class BotRegistryAccessor
122 attr_accessor :recovery
124 # plugins don't call this - a BotRegistryAccessor is created for them and
125 # is accessible via @registry.
126 def initialize(bot, name)
128 @name = name.downcase
129 dirs = File.dirname("#{@bot.botclass}/registry/#{@name}").split("/")
130 dirs.length.times { |i|
131 dir = dirs[0,i+1].join("/")+"/"
132 unless File.exist?(dir)
133 debug "creating subregistry directory #{dir}"
140 # debug "initializing registry accessor with name #{@name}"
144 @registry ||= DBTree.new @bot, "registry/#{@name}"
148 # debug "fushing registry #{registry}"
155 # debug "closing registry #{registry}"
160 # convert value to string form for storing in the registry
161 # defaults to Marshal.dump(val) but you can override this in your module's
162 # registry object to use any method you like.
163 # For example, if you always just handle strings use:
171 # restores object from string form, restore(store(val)) must return val.
172 # If you override store, you should override restore to reverse the
174 # For example, if you always just handle strings use:
181 rescue Exception => e
182 error "failed to restore marshal data for #{val.inspect}, attempting recovery or fallback to default"
184 debug e.backtrace.join("\n")
187 return @recovery.call(val)
188 rescue Exception => ee
189 error "marshal recovery failed, trying default"
191 debug ee.backtrace.join("\n")
196 return Marshal.restore(@default)
206 # lookup a key in the registry
208 if registry.has_key?(key)
209 return restore(registry[key])
210 elsif @default != nil
211 return restore(@default)
217 # set a key in the registry
219 registry[key] = store(value)
222 # set the default value for registry lookups, if the key sought is not
223 # found, the default will be returned. The default default (har) is nil.
224 def set_default (default)
225 @default = store(default)
228 # just like Hash#each
230 registry.each {|key,value|
231 block.call(key, restore(value))
235 # just like Hash#each_key
237 registry.each {|key, value|
242 # just like Hash#each_value
243 def each_value(&block)
244 registry.each {|key, value|
245 block.call(restore(value))
249 # just like Hash#has_key?
251 return registry.has_key?(key)
253 alias include? has_key?
254 alias member? has_key?
256 # just like Hash#has_both?
257 def has_both?(key, value)
258 return registry.has_both?(key, store(value))
261 # just like Hash#has_value?
262 def has_value?(value)
263 return registry.has_value?(store(value))
266 # just like Hash#index?
268 ind = registry.index(store(value))
276 # delete a key from the registry
278 return registry.delete(key)
281 # returns a list of your keys
286 # Return an array of all associations [key, value] in your namespace
289 registry.each {|key, value|
290 ret << [key, restore(value)]
295 # Return an hash of all associations {key => value} in your namespace
298 registry.each {|key, value|
299 ret[key] = restore(value)
304 # empties the registry (restricted to your namespace)
310 # returns an array of the values in your namespace of the registry
319 def sub_registry(prefix)
320 return BotRegistryAccessor.new(@bot, @name + "/" + prefix.to_s)
323 # returns the number of keys in your registry namespace