4 # :title: rbot user data management from IRC
6 # Author:: Giuseppe "Oblomov" Bilotta <giuseppe.bilotta@gmail.com>
10 # Retrive Bot data associated with the receiver. This method is
11 # intended for data retrieval only. See #set_bot_data() if you
12 # need to alter User data.
15 Irc::Utils.bot.plugins['userdata'].get_data(self,key)
17 alias :get_botdata :botdata
19 # This method is used to store Bot data associated with the
20 # receiver. If no block is passed, _value_ is stored for the key
21 # _key_; if a block is passed, it will be called with the previous
22 # _key_ value as parameter, and its return value will be stored as
23 # the new value. If _value_ is present in the block form, it will
24 # be used to initialize _key_ if it's missing.
26 # If you have to do large-scale editing of the Bot data Hash,
27 # please use with_botdata.
29 def set_botdata(key, value=nil, &block)
30 Irc::Utils.bot.plugins['userdata'].set_data(self, key, value, &block)
33 # This method yields the entire Bot data Hash to the block,
34 # and stores any changes done to it, returning a copy
35 # of the (changed) Hash.
37 def with_botdata(&block)
38 Irc::Utils.bot.plugins['userdata'].with_data(self, &block)
41 # This method removes the data associated with the key, returning
42 # the value of the deleted key.
44 def delete_botdata(*keys)
45 Irc::Utils.bot.plugins['userdata'].delete_data(self, *keys)
51 # User data is stored in registries indexed by BotUser
52 # name and Irc::User nick. This core module takes care
53 # of handling its usage.
55 class UserDataModule < CoreBotModule
59 @ircuser = @registry.sub_registry('ircuser')
60 @transient = @registry.sub_registry('transient')
61 @botuser = @registry.sub_registry('botuser')
64 def get_data_hash(user, opts={})
69 ih = @ircuser[iu.nick] || {}
74 bh = @transient[bu.netmasks.first.fullform] || {}
76 bh = @botuser[bu.username] || {}
82 alias :single_retrieve :[]
83 alias :single_assign :[]=
91 def get_data(user, key=nil)
92 h = get_data_hash(user)
98 def set_data_hash(user, hh)
102 # we .dup the hash to remove singleton methods
103 # and make it dump-able
106 @ircuser[iu.nick] = h
107 return h if bu.default?
110 @transient[bu.netmasks.first.fullform] = h
112 @botuser[bu.username] = h
117 def set_data(user, key, value=nil, &block)
118 h = get_data_hash(user)
126 if value and not h.has_key?(key)
133 set_data_hash(user, h)
138 def with_data(user, &block)
139 h = get_data_hash(user)
143 set_data_hash(user, h)
148 def delete_data(user, *keys)
149 h = get_data_hash(user)
151 rv = keys.map { |k| h.delete k }
152 set_data_hash(user, h)
153 rv.size == 1 ? rv.first : rv
156 def handle_get(m, params)
157 user = m.server.get_user(params[:nick]) || m.source
158 key = params[:key].intern
159 data = get_data(user, key)
161 m.reply(_("%{key} data for %{user}: %{data}") % {
167 m.reply(_("sorry, no %{key} data for %{user}") % {
174 ### TODO FIXME not yet: are we going to allow non-string
175 ### values for data? if so, this can't work ...
177 # def handle_set(m, params)
178 # user = m.server.get_user(params[:nick]) || m.source
179 # key = params[:key].intern
180 # data = params[:data].to_s
183 def event_botuser(action, opts={})
186 source = opts[:source]
187 return unless @botuser.key?(source)
189 @botuser[dest] = @botuser[source].dup
190 @botuser.delete(source) if action == :rename
192 @permification ||= {}
193 k = [opts[:irc_user], opts[:bot_user]]
194 @permification[k] = get_data_hash(opts[:irc_user], :plain => true)
196 @permification ||= {}
197 k = [opts[:irc_user], opts[:bot_user]]
198 if @permification.has_key?(k)
199 @botuser[opts[:bot_user]] = @permification[k]
200 @permification.delete(k)
207 plugin = UserDataModule.new
209 plugin.map "get [:nick's] :key [data]", :action => 'handle_get'
210 plugin.map "get :key [data] [for :nick]", :action => 'handle_get'
211 plugin.map "get :key [data] [of :nick]", :action => 'handle_get'
213 # plugin.map "set [:nick's] :key [data] to :data", :action => handle_get
214 # plugin.map "set :key [data] [for :nick] to :data", :action => handle_get
215 # plugin.map "set :key [data] [of :nick] to :data", :action => handle_get