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,
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 puts "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 puts "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")
57 puts "closing db #{k}"
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}"
124 # convert value to string form for storing in the registry
125 # defaults to Marshal.dump(val) but you can override this in your module's
126 # registry object to use any method you like.
127 # For example, if you always just handle strings use:
135 # restores object from string form, restore(store(val)) must return val.
136 # If you override store, you should override restore to reverse the
138 # For example, if you always just handle strings use:
146 $stderr.puts "failed to restore marshal data, falling back to default"
149 return Marshal.restore(@default)
159 # lookup a key in the registry
161 if @registry.has_key?(key)
162 return restore(@registry[key])
163 elsif @default != nil
164 return restore(@default)
170 # set a key in the registry
172 @registry[key] = store(value)
175 # set the default value for registry lookups, if the key sought is not
176 # found, the default will be returned. The default default (har) is nil.
177 def set_default (default)
178 @default = store(default)
181 # just like Hash#each
183 @registry.each {|key,value|
184 block.call(key, restore(value))
188 # just like Hash#each_key
190 @registry.each {|key, value|
195 # just like Hash#each_value
196 def each_value(&block)
197 @registry.each {|key, value|
198 block.call(restore(value))
202 # just like Hash#has_key?
204 return @registry.has_key?(key)
206 alias include? has_key?
207 alias member? has_key?
209 # just like Hash#has_both?
210 def has_both?(key, value)
211 return @registry.has_both?(key, store(value))
214 # just like Hash#has_value?
215 def has_value?(value)
216 return @registry.has_value?(store(value))
219 # just like Hash#index?
221 ind = @registry.index(store(value))
229 # delete a key from the registry
231 return @registry.delete(key)
234 # returns a list of your keys
236 return @registry.keys
239 # Return an array of all associations [key, value] in your namespace
242 @registry.each {|key, value|
243 ret << [key, restore(value)]
248 # Return an hash of all associations {key => value} in your namespace
251 @registry.each {|key, value|
252 ret[key] = restore(value)
257 # empties the registry (restricted to your namespace)
263 # returns an array of the values in your namespace of the registry
272 # returns the number of keys in your registry namespace