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.delete("#{@bot.botclass}/registry.db")
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)
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 dirs = File.dirname("#{@bot.botclass}/registry/#{prefix}.db").split("/")
48 dirs.length.times { |i|
49 dir = dirs[0,i+1].join("/")+"/"
50 unless File.exist?(dir)
51 log "creating subregistry directory #{dir}"
56 unless dbs.has_key?(prefix)
57 log "creating db #{@bot.botclass}/registry/#{prefix}.db"
58 dbs[prefix] = BDB::CIBtree.open("#{@bot.botclass}/registry/#{prefix}.db",
59 nil, BDB::CREATE | BDB::EXCL,
65 File.rename("#{@bot.botclass}/plugin_registry.db", "#{@bot.botclass}/plugin_registry.db.old")
76 # This class provides persistent storage for plugins via a hash interface.
77 # The default mode is an object store, so you can store ruby objects and
78 # reference them with hash keys. This is because the default store/restore
79 # methods of the plugins' RegistryAccessor are calls to Marshal.dump and
84 # @registry[:blah] = blah
85 # then, even after the bot is shut down and disconnected, on the next run you
86 # can access the blah object as it was, with:
87 # blah = @registry[:blah]
88 # The registry can of course be used to store simple strings, fixnums, etc as
89 # well, and should be useful to store or cache plugin data or dynamic plugin
93 # in object store mode, don't make the mistake of treating it like a live
94 # object, e.g. (using the example above)
95 # @registry[:blah][:foo] = "flump"
96 # will NOT modify the object in the registry - remember that BotRegistry#[]
97 # returns a Marshal.restore'd object, the object you just modified in place
98 # will disappear. You would need to:
99 # blah = @registry[:blah]
100 # blah[:foo] = "flump"
101 # @registry[:blah] = blah
103 # If you don't need to store objects, and strictly want a persistant hash of
104 # strings, you can override the store/restore methods to suit your needs, for
105 # example (in your plugin):
116 # Your plugins section of the registry is private, it has its own namespace
117 # (derived from the plugin's class name, so change it and lose your data).
118 # Calls to registry.each etc, will only iterate over your namespace.
119 class BotRegistryAccessor
120 # plugins don't call this - a BotRegistryAccessor is created for them and
121 # is accessible via @registry.
122 def initialize(bot, name)
124 @name = name.downcase
125 @registry = DBTree.new bot, "registry/#{@name}"
127 # debug "initializing registry accessor with name #{@name}"
139 # convert value to string form for storing in the registry
140 # defaults to Marshal.dump(val) but you can override this in your module's
141 # registry object to use any method you like.
142 # For example, if you always just handle strings use:
150 # restores object from string form, restore(store(val)) must return val.
151 # If you override store, you should override restore to reverse the
153 # For example, if you always just handle strings use:
160 rescue Exception => e
161 warning "failed to restore marshal data for #{val.inspect}, falling back to default"
163 debug e.backtrace.join("\n")
166 return Marshal.restore(@default)
176 # lookup a key in the registry
178 if @registry.has_key?(key)
179 return restore(@registry[key])
180 elsif @default != nil
181 return restore(@default)
187 # set a key in the registry
189 @registry[key] = store(value)
192 # set the default value for registry lookups, if the key sought is not
193 # found, the default will be returned. The default default (har) is nil.
194 def set_default (default)
195 @default = store(default)
198 # just like Hash#each
200 @registry.each {|key,value|
201 block.call(key, restore(value))
205 # just like Hash#each_key
207 @registry.each {|key, value|
212 # just like Hash#each_value
213 def each_value(&block)
214 @registry.each {|key, value|
215 block.call(restore(value))
219 # just like Hash#has_key?
221 return @registry.has_key?(key)
223 alias include? has_key?
224 alias member? has_key?
226 # just like Hash#has_both?
227 def has_both?(key, value)
228 return @registry.has_both?(key, store(value))
231 # just like Hash#has_value?
232 def has_value?(value)
233 return @registry.has_value?(store(value))
236 # just like Hash#index?
238 ind = @registry.index(store(value))
246 # delete a key from the registry
248 return @registry.delete(key)
251 # returns a list of your keys
253 return @registry.keys
256 # Return an array of all associations [key, value] in your namespace
259 @registry.each {|key, value|
260 ret << [key, restore(value)]
265 # Return an hash of all associations {key => value} in your namespace
268 @registry.each {|key, value|
269 ret[key] = restore(value)
274 # empties the registry (restricted to your namespace)
280 # returns an array of the values in your namespace of the registry
289 def sub_registry(prefix)
290 return BotRegistryAccessor.new(@bot, @name + "/" + prefix)
293 # returns the number of keys in your registry namespace