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 puts "upgrading old-style (rbot 0.9.5 or earlier) plugin registry to new format"
20 old = BDB::Hash.open("#{@bot.botclass}/registry.db", nil,
21 "r+", 0600, "set_pagesize" => 1024,
22 "set_cachesize" => [0, 32 * 1024, 0])
23 new = BDB::CIBtree.open("#{@bot.botclass}/plugin_registry.db", nil,
24 BDB::CREATE | BDB::EXCL | BDB::TRUNCATE,
25 0600, "set_pagesize" => 1024,
26 "set_cachesize" => [0, 32 * 1024, 0])
32 File.delete("#{@bot.botclass}/registry.db")
37 if File.exist?("#{@bot.botclass}/plugin_registry.db")
38 Dir.mkdir("#{@bot.botclass}/registry")
40 puts "upgrading previous (rbot 0.9.9 or earlier) plugin registry to new split format"
41 old = BDB::CIBtree.open("#{@bot.botclass}/plugin_registry.db", nil,
42 "r+", 0600, "set_pagesize" => 1024,
43 "set_cachesize" => [0, 32 * 1024, 0])
45 prefix,key = k.split("/", 2)
47 unless dbs.has_key?(prefix)
48 puts "creating db #{@bot.botclass}/registry/#{prefix}.db"
49 dbs[prefix] = BDB::CIBtree.open("#{@bot.botclass}/registry/#{prefix}.db",
50 nil, BDB::CREATE | BDB::EXCL | BDB::TRUNCATE,
51 0600, "set_pagesize" => 1024,
52 "set_cachesize" => [0, 32 * 1024, 0])
57 File.rename("#{@bot.botclass}/plugin_registry.db", "#{@bot.botclass}/plugin_registry.db.old")
59 puts "closing db #{k}"
67 # This class provides persistent storage for plugins via a hash interface.
68 # The default mode is an object store, so you can store ruby objects and
69 # reference them with hash keys. This is because the default store/restore
70 # methods of the plugins' RegistryAccessor are calls to Marshal.dump and
75 # @registry[:blah] = blah
76 # then, even after the bot is shut down and disconnected, on the next run you
77 # can access the blah object as it was, with:
78 # blah = @registry[:blah]
79 # The registry can of course be used to store simple strings, fixnums, etc as
80 # well, and should be useful to store or cache plugin data or dynamic plugin
84 # in object store mode, don't make the mistake of treating it like a live
85 # object, e.g. (using the example above)
86 # @registry[:blah][:foo] = "flump"
87 # will NOT modify the object in the registry - remember that BotRegistry#[]
88 # returns a Marshal.restore'd object, the object you just modified in place
89 # will disappear. You would need to:
90 # blah = @registry[:blah]
91 # blah[:foo] = "flump"
92 # @registry[:blah] = blah
94 # If you don't need to store objects, and strictly want a persistant hash of
95 # strings, you can override the store/restore methods to suit your needs, for
96 # example (in your plugin):
107 # Your plugins section of the registry is private, it has its own namespace
108 # (derived from the plugin's class name, so change it and lose your data).
109 # Calls to registry.each etc, will only iterate over your namespace.
110 class BotRegistryAccessor
111 # plugins don't call this - a BotRegistryAccessor is created for them and
112 # is accessible via @registry.
113 def initialize(bot, name)
115 @name = name.downcase
116 @registry = DBTree.new bot, "registry/#{@name}"
118 # debug "initializing registry accessor with name #{@name}"
121 # convert value to string form for storing in the registry
122 # defaults to Marshal.dump(val) but you can override this in your module's
123 # registry object to use any method you like.
124 # For example, if you always just handle strings use:
132 # restores object from string form, restore(store(val)) must return val.
133 # If you override store, you should override restore to reverse the
135 # For example, if you always just handle strings use:
143 $stderr.puts "failed to restore marshal data, falling back to default"
146 return Marshal.restore(@default)
156 # lookup a key in the registry
158 if @registry.has_key?(key)
159 return restore(@registry[key])
160 elsif @default != nil
161 return restore(@default)
167 # set a key in the registry
169 @registry[key] = store(value)
172 # set the default value for registry lookups, if the key sought is not
173 # found, the default will be returned. The default default (har) is nil.
174 def set_default (default)
175 @default = store(default)
178 # just like Hash#each
180 @registry.each {|key,value|
181 block.call(key, restore(value))
185 # just like Hash#each_key
187 @registry.each {|key, value|
192 # just like Hash#each_value
193 def each_value(&block)
194 @registry.each {|key, value|
195 block.call(restore(value))
199 # just like Hash#has_key?
201 return @registry.has_key?(key)
203 alias include? has_key?
204 alias member? has_key?
206 # just like Hash#has_both?
207 def has_both?(key, value)
208 return @registry.has_both?(key, store(value))
211 # just like Hash#has_value?
212 def has_value?(value)
213 return @registry.has_value?(store(value))
216 # just like Hash#index?
218 ind = @registry.index(store(value))
226 # delete a key from the registry
228 return @registry.delete(key)
231 # returns a list of your keys
233 return @registry.keys
236 # Return an array of all associations [key, value] in your namespace
239 @registry.each {|key, value|
240 ret << [key, restore(value)]
245 # Return an hash of all associations {key => value} in your namespace
248 @registry.each {|key, value|
249 ret[key] = restore(value)
254 # empties the registry (restricted to your namespace)
260 # returns an array of the values in your namespace of the registry
269 # returns the number of keys in your registry namespace