4 # :title: rbot user data management from IRC
6 # Author:: Giuseppe "Oblomov" Bilotta <giuseppe.bilotta@gmail.com>
7 # Copyright:: (C) 2006,2007 Giuseppe Bilotta
12 # Retrive Bot data associated with the receiver. This method is
13 # intended for data retrieval only. See #set_bot_data() if you
14 # need to alter User data.
17 Irc::Utils.bot.plugins['userdata'].get_data(self,key)
19 alias :get_botdata :botdata
21 # This method is used to store Bot data associated with the
22 # receiver. If no block is passed, _value_ is stored for the key
23 # _key_; if a block is passed, it will be called with the previous
24 # _key_ value as parameter, and its return value will be stored as
25 # the new value. If _value_ is present in the block form, it will
26 # be used to initialize _key_ if it's missing.
28 # If you have to do large-scale editing of the Bot data Hash,
29 # please use with_botdata.
31 def set_botdata(key, value=nil, &block)
32 Irc::Utils.bot.plugins['userdata'].set_data(self, key, value, &block)
35 # This method yields the entire Bot data Hash to the block,
36 # and stores any changes done to it, returning a copy
37 # of the (changed) Hash.
39 def with_botdata(&block)
40 Irc::Utils.bot.plugins['userdata'].with_data(self, &block)
46 # User data is stored in registries indexed by BotUser
47 # name and Irc::User nick. This core module takes care
48 # of handling its usage.
50 class UserDataModule < CoreBotModule
54 @ircuser = @registry.sub_registry('ircuser')
55 @transient = @registry.sub_registry('transient')
56 @botuser = @registry.sub_registry('botuser')
59 def get_data_hash(user)
63 ih = @ircuser[iu.nick] || {}
68 bh = @transient[bu.netmasks.first.fullform] || {}
70 bh = @botuser[bu.username] || {}
75 def get_data(user, key=nil)
76 h = get_data_hash(user)
82 def set_data_hash(user, h)
87 return h if bu.default?
90 @transient[bu.netmasks.first.fullform] = h
92 @botuser[bu.username] = h
97 def set_data(user, key, value=nil, &block)
98 h = get_data_hash(user)
106 if value and not h.has_key?(key)
113 set_data_hash(user, h)
118 def with_data(user, &block)
119 h = get_data_hash(user)
123 set_data_hash(user, h)
128 def handle_get(m, params)
129 user = m.server.get_user(params[:nick]) || m.source
130 key = params[:key].intern
131 data = get_data(user, key)
133 m.reply(_("%{key} data for %{user}: %{data}") % {
139 m.reply(_("sorry, no %{key} data for %{user}") % {
146 ### TODO FIXME not yet: are we going to allow non-string
147 ### values for data? if so, this can't work ...
149 # def handle_set(m, params)
150 # user = m.server.get_user(params[:nick]) || m.source
151 # key = params[:key].intern
152 # data = params[:data].to_s
155 def event_botuser(action, opts={})
156 return unless [:copy, :rename].include?(action)
157 source = opts[:source]
158 return unless @botuser.key?(source)
160 @botuser[dest] = @botuser[source].dup
161 @botuser.delete(source) if action == :rename
166 plugin = UserDataModule.new
168 plugin.map "get [:nick's] :key [data]", :action => 'handle_get'
169 plugin.map "get :key [data] [for :nick]", :action => 'handle_get'
170 plugin.map "get :key [data] [of :nick]", :action => 'handle_get'
172 # plugin.map "set [:nick's] :key [data] to :data", :action => handle_get
173 # plugin.map "set :key [data] [for :nick] to :data", :action => handle_get
174 # plugin.map "set :key [data] [of :nick] to :data", :action => handle_get