]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - lib/rbot/core/userdata.rb
844a6c5874c1cf90f6050e9f2b36ff5c5be174df
[user/henk/code/ruby/rbot.git] / lib / rbot / core / userdata.rb
1 #-- vim:sw=2:et
2 #++
3 #
4 # :title: rbot user data management from IRC
5 #
6 # Author:: Giuseppe "Oblomov" Bilotta <giuseppe.bilotta@gmail.com>
7 # Copyright:: (C) 2006,2007 Giuseppe Bilotta
8 # License:: GPL v2
9
10 module ::Irc
11   class User
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.
15     #
16     def botdata(key=nil)
17       Irc::Utils.bot.plugins['userdata'].get_data(self,key)
18     end
19     alias :get_botdata :botdata
20
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.
27     #
28     # If you have to do large-scale editing of the Bot data Hash,
29     # please use with_botdata.
30     # 
31     def set_botdata(key, value=nil, &block)
32       Irc::Utils.bot.plugins['userdata'].set_data(self, key, value, &block)
33     end
34
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.
38     #
39     def with_botdata(&block)
40       Irc::Utils.bot.plugins['userdata'].with_data(self, &block)
41     end
42
43   end
44 end
45
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.
49 #
50 class UserDataModule < CoreBotModule
51
52   def initialize
53     super
54     @ircuser = @registry.sub_registry('ircuser')
55     @botuser = @registry.sub_registry('botuser')
56   end
57
58   def get_data_hash(user)
59     iu = user.to_irc_user
60     bu = iu.botuser
61
62     ih = @ircuser[iu.nick] || {}
63
64     if bu.transient? or bu.default?
65       return ih
66     else
67       bh = @botuser[bu.username] || {}
68       return ih.merge! bh
69     end
70   end
71
72   def get_data(user, key=nil)
73     h = get_data_hash(user)
74     debug h
75     return h if key.nil?
76     return h[key]
77   end
78
79   def set_data(user, key, value=nil, &block)
80     h = get_data_hash(user)
81     debug h
82
83     ret = value
84
85     if not block_given?
86       h[key] = value
87     else
88       if value and not h.has_key?(key)
89         h[key] = value
90       end
91       ret = yield h[key]
92     end
93     debug ret
94
95     iu = user.to_irc_user
96     bu = iu.botuser
97
98     if bu.transient? or bu.default?
99       @ircuser[iu.nick] = h
100     else
101       @botuser[bu.username] = h
102     end
103     return ret
104   end
105
106   def with_data(user, &block)
107     h = get_data_hash(user)
108     debug h
109     yield h
110
111     iu = user.to_irc_user
112     bu = iu.botuser
113
114     if bu.transient? or bu.default?
115       @ircuser[iu.nick] = h
116     else
117       @botuser[bu.username] = h
118     end
119     return h
120   end
121
122   def handle_get(m, params)
123     user = m.server.get_user(params[:nick]) || m.source
124     key = params[:key].intern
125     data = get_data(user, key)
126     if data
127       m.reply (_("%{key} data for %{user}: %{data}") % {
128         :key => key,
129         :user => user.nick,
130         :data => data
131       })
132     else
133       m.reply (_("sorry, no %{key} data for %{user}") % {
134         :key => key,
135         :user => user.nick,
136       })
137     end
138   end
139
140   ### TODO FIXME not yet: are we going to allow non-string
141   ### values for data? if so, this can't work ...
142   #
143   # def handle_set(m, params)
144   #   user = m.server.get_user(params[:nick]) || m.source
145   #   key = params[:key].intern
146   #   data = params[:data].to_s
147   # end
148
149 end
150
151 plugin = UserDataModule.new
152
153 plugin.map "get [:nick's] :key [data]",   :action => 'handle_get'
154 plugin.map "get :key [data] [for :nick]", :action => 'handle_get'
155 plugin.map "get :key [data] [of :nick]",  :action => 'handle_get'
156
157 # plugin.map "set [:nick's] :key [data] to :data", :action => handle_get
158 # plugin.map "set :key [data] [for :nick] to :data", :action => handle_get
159 # plugin.map "set :key [data] [of :nick] to :data", :action => handle_get