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 unless dbs.has_key?(prefix)
46 log "creating db #{@bot.botclass}/registry/#{prefix}.db"
47 dbs[prefix] = BDB::CIBtree.open("#{@bot.botclass}/registry/#{prefix}.db",
48 nil, BDB::CREATE | BDB::EXCL,
55 File.rename("#{@bot.botclass}/plugin_registry.db", "#{@bot.botclass}/plugin_registry.db.old")
66 # This class provides persistent storage for plugins via a hash interface.
67 # The default mode is an object store, so you can store ruby objects and
68 # reference them with hash keys. This is because the default store/restore
69 # methods of the plugins' RegistryAccessor are calls to Marshal.dump and
74 # @registry[:blah] = blah
75 # then, even after the bot is shut down and disconnected, on the next run you
76 # can access the blah object as it was, with:
77 # blah = @registry[:blah]
78 # The registry can of course be used to store simple strings, fixnums, etc as
79 # well, and should be useful to store or cache plugin data or dynamic plugin
83 # in object store mode, don't make the mistake of treating it like a live
84 # object, e.g. (using the example above)
85 # @registry[:blah][:foo] = "flump"
86 # will NOT modify the object in the registry - remember that BotRegistry#[]
87 # returns a Marshal.restore'd object, the object you just modified in place
88 # will disappear. You would need to:
89 # blah = @registry[:blah]
90 # blah[:foo] = "flump"
91 # @registry[:blah] = blah
93 # If you don't need to store objects, and strictly want a persistant hash of
94 # strings, you can override the store/restore methods to suit your needs, for
95 # example (in your plugin):
106 # Your plugins section of the registry is private, it has its own namespace
107 # (derived from the plugin's class name, so change it and lose your data).
108 # Calls to registry.each etc, will only iterate over your namespace.
109 class BotRegistryAccessor
110 # plugins don't call this - a BotRegistryAccessor is created for them and
111 # is accessible via @registry.
112 def initialize(bot, name)
114 @name = name.downcase
115 @registry = DBTree.new bot, "registry/#{@name}"
117 # debug "initializing registry accessor with name #{@name}"
129 # convert value to string form for storing in the registry
130 # defaults to Marshal.dump(val) but you can override this in your module's
131 # registry object to use any method you like.
132 # For example, if you always just handle strings use:
140 # restores object from string form, restore(store(val)) must return val.
141 # If you override store, you should override restore to reverse the
143 # For example, if you always just handle strings use:
150 rescue Exception => e
151 warning "failed to restore marshal data for #{val.inspect}, falling back to default"
153 debug e.backtrace.join("\n")
156 return Marshal.restore(@default)
166 # lookup a key in the registry
168 if @registry.has_key?(key)
169 return restore(@registry[key])
170 elsif @default != nil
171 return restore(@default)
177 # set a key in the registry
179 @registry[key] = store(value)
182 # set the default value for registry lookups, if the key sought is not
183 # found, the default will be returned. The default default (har) is nil.
184 def set_default (default)
185 @default = store(default)
188 # just like Hash#each
190 @registry.each {|key,value|
191 block.call(key, restore(value))
195 # just like Hash#each_key
197 @registry.each {|key, value|
202 # just like Hash#each_value
203 def each_value(&block)
204 @registry.each {|key, value|
205 block.call(restore(value))
209 # just like Hash#has_key?
211 return @registry.has_key?(key)
213 alias include? has_key?
214 alias member? has_key?
216 # just like Hash#has_both?
217 def has_both?(key, value)
218 return @registry.has_both?(key, store(value))
221 # just like Hash#has_value?
222 def has_value?(value)
223 return @registry.has_value?(store(value))
226 # just like Hash#index?
228 ind = @registry.index(store(value))
236 # delete a key from the registry
238 return @registry.delete(key)
241 # returns a list of your keys
243 return @registry.keys
246 # Return an array of all associations [key, value] in your namespace
249 @registry.each {|key, value|
250 ret << [key, restore(value)]
255 # Return an hash of all associations {key => value} in your namespace
258 @registry.each {|key, value|
259 ret[key] = restore(value)
264 # empties the registry (restricted to your namespace)
270 # returns an array of the values in your namespace of the registry
279 def sub_registry(prefix)
280 return BotRegistryAccessor.new(@bot, @name + "/" + prefix)
283 # returns the number of keys in your registry namespace