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