]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - lib/rbot/core/userdata.rb
core/userdata: bot_data -> botdata
[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     def set_botdata(key, value=nil, &block)
29       Irc::Utils.bot.plugins['userdata'].set_data(self, key, value, &block)
30     end
31
32   end
33 end
34
35 # User data is stored in registries indexed by BotUser
36 # name and Irc::User nick. This core module takes care
37 # of handling its usage.
38 #
39 class UserDataModule < CoreBotModule
40
41   def initialize
42     super
43     @ircuser = @registry.sub_registry('ircuser')
44     @botuser = @registry.sub_registry('botuser')
45   end
46
47   def get_data_hash(user)
48     iu = user.to_irc_user
49     bu = iu.botuser
50
51     ih = @ircuser[iu.nick] || {}
52
53     if bu.transient? or bu.default?
54       return ih
55     else
56       bh = @botuser[bu.username] || {}
57       return ih.merge! bh
58     end
59   end
60
61   def get_data(user, key=nil)
62     h = get_data_hash(user)
63     debug h
64     return h if key.nil?
65     return h[key]
66   end
67
68   def set_data(user, key, value=nil, &block)
69     h = get_data_hash(user)
70     debug h
71
72     ret = value
73
74     if not block_given?
75       h[key] = value
76     else
77       if value and not h.has_key?(key)
78         h[key] = value
79       end
80       ret = yield h[key]
81     end
82     debug ret
83
84     iu = user.to_irc_user
85     bu = iu.botuser
86
87     if bu.transient? or bu.default?
88       @ircuser[iu.nick] = h
89     else
90       @botuser[bu.username] = h
91     end
92     return ret
93   end
94
95   def handle_get(m, params)
96     user = m.server.get_user(params[:nick]) || m.source
97     key = params[:key].intern
98     data = get_data(user, key)
99     if data
100       m.reply (_("%{key} data for %{user}: %{data}") % {
101         :key => key,
102         :user => user.nick,
103         :data => data
104       })
105     else
106       m.reply (_("sorry, no %{key} data for %{user}") % {
107         :key => key,
108         :user => user.nick,
109       })
110     end
111   end
112
113   ### TODO FIXME not yet: are we going to allow non-string
114   ### values for data? if so, this can't work ...
115   #
116   # def handle_set(m, params)
117   #   user = m.server.get_user(params[:nick]) || m.source
118   #   key = params[:key].intern
119   #   data = params[:data].to_s
120   # end
121
122 end
123
124 plugin = UserDataModule.new
125
126 plugin.map "get [:nick's] :key [data]",   :action => 'handle_get'
127 plugin.map "get :key [data] [for :nick]", :action => 'handle_get'
128 plugin.map "get :key [data] [of :nick]",  :action => 'handle_get'
129
130 # plugin.map "set [:nick's] :key [data] to :data", :action => handle_get
131 # plugin.map "set :key [data] [for :nick] to :data", :action => handle_get
132 # plugin.map "set :key [data] [of :nick] to :data", :action => handle_get