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)
20 # This method is used to store Bot data associated with the
21 # receiver. If no block is passed, _value_ is stored for the key
22 # _key_; if a block is passed, it will be called with the previous
23 # _key_ value as parameter, and its return value will be stored as
24 # the new value. If _value_ is present in the block form, it will
25 # be used to initialize _key_ if it's missing
27 def set_bot_data(key, value=nil, &block)
28 Irc::Utils.bot.plugins['userdata'].set_data(self, key, value, &block)
33 # User data is stored in registries indexed by BotUser
34 # name and Irc::User nick. This core module takes care
35 # of handling its usage.
37 class UserDataModule < CoreBotModule
41 @ircuser = @registry.sub_registry('ircuser')
42 @botuser = @registry.sub_registry('botuser')
45 def get_data_hash(user)
49 ih = @ircuser[iu.nick] || {}
51 if bu.transient? or bu == Irc::Bot::Auth.defaultbotuser
54 bh = @botuser[bu.username] || {}
59 def get_data(user, key=nil)
60 h = get_data_hash(user)
66 def set_data(user, key, value=nil, &block)
67 h = get_data_hash(user)
75 if value and not h.has_key?(key)
85 if bu.transient? or bu == Irc::Bot::Auth.defaultbotuser
88 @botuser[bu.username] = h
93 def handle_get(m, params)
94 user = m.server.get_user(params[:nick]) || m.source
95 key = params[:key].intern
96 data = get_data(user, key)
98 m.reply (_("%{key} data for %{user}: %{data}") % {
104 m.reply (_("sorry, no %{key} data for %{user}") % {
111 ### TODO FIXME not yet: are we going to allow non-string
112 ### values for data? if so, this can't work ...
114 # def handle_set(m, params)
115 # user = m.server.get_user(params[:nick]) || m.source
116 # key = params[:key].intern
117 # data = params[:data].to_s
122 plugin = UserDataModule.new
124 plugin.map "get [:nick's] :key [data]", :action => 'handle_get'
125 plugin.map "get :key [data] [for :nick]", :action => 'handle_get'
126 plugin.map "get :key [data] [of :nick]", :action => 'handle_get'
128 # plugin.map "set [:nick's] :key [data] to :data", :action => handle_get
129 # plugin.map "set :key [data] [for :nick] to :data", :action => handle_get
130 # plugin.map "set :key [data] [of :nick] to :data", :action => handle_get