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)
44 # User data is stored in registries indexed by BotUser
45 # name and Irc::User nick. This core module takes care
46 # of handling its usage.
48 class UserDataModule < CoreBotModule
52 @ircuser = @registry.sub_registry('ircuser')
53 @transient = @registry.sub_registry('transient')
54 @botuser = @registry.sub_registry('botuser')
57 def get_data_hash(user, opts={})
62 ih = @ircuser[iu.nick] || {}
67 bh = @transient[bu.netmasks.first.fullform] || {}
69 bh = @botuser[bu.username] || {}
75 alias :single_retrieve :[]
76 alias :single_assign :[]=
84 def get_data(user, key=nil)
85 h = get_data_hash(user)
91 def set_data_hash(user, hh)
95 # we .dup the hash to remove singleton methods
96 # and make it dump-able
100 return h if bu.default?
103 @transient[bu.netmasks.first.fullform] = h
105 @botuser[bu.username] = h
110 def set_data(user, key, value=nil, &block)
111 h = get_data_hash(user)
119 if value and not h.has_key?(key)
126 set_data_hash(user, h)
131 def with_data(user, &block)
132 h = get_data_hash(user)
136 set_data_hash(user, h)
141 def handle_get(m, params)
142 user = m.server.get_user(params[:nick]) || m.source
143 key = params[:key].intern
144 data = get_data(user, key)
146 m.reply(_("%{key} data for %{user}: %{data}") % {
152 m.reply(_("sorry, no %{key} data for %{user}") % {
159 ### TODO FIXME not yet: are we going to allow non-string
160 ### values for data? if so, this can't work ...
162 # def handle_set(m, params)
163 # user = m.server.get_user(params[:nick]) || m.source
164 # key = params[:key].intern
165 # data = params[:data].to_s
168 def event_botuser(action, opts={})
171 source = opts[:source]
172 return unless @botuser.key?(source)
174 @botuser[dest] = @botuser[source].dup
175 @botuser.delete(source) if action == :rename
177 @permification ||= {}
178 k = [opts[:irc_user], opts[:bot_user]]
179 @permification[k] = get_data_hash(opts[:irc_user], :plain => true)
181 @permification ||= {}
182 k = [opts[:irc_user], opts[:bot_user]]
183 if @permification.has_key?(k)
184 @botuser[opts[:bot_user]] = @permification[k]
185 @permification.delete(k)
192 plugin = UserDataModule.new
194 plugin.map "get [:nick's] :key [data]", :action => 'handle_get'
195 plugin.map "get :key [data] [for :nick]", :action => 'handle_get'
196 plugin.map "get :key [data] [of :nick]", :action => 'handle_get'
198 # plugin.map "set [:nick's] :key [data] to :data", :action => handle_get
199 # plugin.map "set :key [data] [for :nick] to :data", :action => handle_get
200 # plugin.map "set :key [data] [of :nick] to :data", :action => handle_get