]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - lib/rbot/core/userdata.rb
New Irc Framework: netmask generalization now takes into consideration AzzurraNet...
[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     @transient = @registry.sub_registry('transient')
56     @botuser = @registry.sub_registry('botuser')
57   end
58
59   def get_data_hash(user, opts={})
60     plain = opts[:plain]
61     iu = user.to_irc_user
62     bu = iu.botuser
63
64     ih = @ircuser[iu.nick] || {}
65
66     if bu.default?
67       return ih
68     elsif bu.transient?
69       bh = @transient[bu.netmasks.first.fullform] || {}
70     else
71       bh = @botuser[bu.username] || {}
72     end
73     ih.merge!(bh)
74
75     unless plain
76       class << ih
77         alias :single_retrieve :[]
78         alias :single_assign :[]=
79           include DottedIndex
80       end
81     end
82
83     return ih
84   end
85
86   def get_data(user, key=nil)
87     h = get_data_hash(user)
88     debug h
89     return h if key.nil?
90     return h[key]
91   end
92
93   def set_data_hash(user, h)
94     iu = user.to_irc_user
95     bu = iu.botuser
96
97     @ircuser[iu.nick] = h
98     return h if bu.default?
99
100     if bu.transient?
101       @transient[bu.netmasks.first.fullform] = h
102     else
103       @botuser[bu.username] = h
104     end
105     return h
106   end
107
108   def set_data(user, key, value=nil, &block)
109     h = get_data_hash(user)
110     debug h
111
112     ret = value
113
114     if not block_given?
115       h[key] = value
116     else
117       if value and not h.has_key?(key)
118         h[key] = value
119       end
120       ret = yield h[key]
121     end
122     debug ret
123
124     set_data_hash(user, h)
125
126     return ret
127   end
128
129   def with_data(user, &block)
130     h = get_data_hash(user)
131     debug h
132     yield h
133
134     set_data_hash(user, h)
135
136     return h
137   end
138
139   def handle_get(m, params)
140     user = m.server.get_user(params[:nick]) || m.source
141     key = params[:key].intern
142     data = get_data(user, key)
143     if data
144       m.reply(_("%{key} data for %{user}: %{data}") % {
145         :key => key,
146         :user => user.nick,
147         :data => data
148       })
149     else
150       m.reply(_("sorry, no %{key} data for %{user}") % {
151         :key => key,
152         :user => user.nick,
153       })
154     end
155   end
156
157   ### TODO FIXME not yet: are we going to allow non-string
158   ### values for data? if so, this can't work ...
159   #
160   # def handle_set(m, params)
161   #   user = m.server.get_user(params[:nick]) || m.source
162   #   key = params[:key].intern
163   #   data = params[:data].to_s
164   # end
165
166   def event_botuser(action, opts={})
167     case action
168     when :copy, :rename
169       source = opts[:source]
170       return unless @botuser.key?(source)
171       dest = opts[:dest]
172       @botuser[dest] = @botuser[source].dup
173       @botuser.delete(source) if action == :rename
174     when :pre_perm
175       @permification ||= {}
176       k = [opts[:irc_user], opts[:bot_user]]
177       @permification[k] = get_data_hash(opts[:irc_user], :plain => true)
178     when :post_perm
179       @permification ||= {}
180       k = [opts[:irc_user], opts[:bot_user]]
181       if @permification.has_key?(k)
182         @botuser[opts[:bot_user]] = @permification[k]
183         @permification.delete(k)
184       end
185     end
186   end
187
188 end
189
190 plugin = UserDataModule.new
191
192 plugin.map "get [:nick's] :key [data]",   :action => 'handle_get'
193 plugin.map "get :key [data] [for :nick]", :action => 'handle_get'
194 plugin.map "get :key [data] [of :nick]",  :action => 'handle_get'
195
196 # plugin.map "set [:nick's] :key [data] to :data", :action => handle_get
197 # plugin.map "set :key [data] [for :nick] to :data", :action => handle_get
198 # plugin.map "set :key [data] [of :nick] to :data", :action => handle_get