]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - lib/rbot/core/userdata.rb
UserData cote botmodule to handle user data storage/retrieval
[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 bot_data(key=nil)
17       Irc::Utils.bot.plugins['userdata'].get_data(self,key)
18     end
19
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
26     # 
27     def set_bot_data(key, value=nil, &block)
28       Irc::Utils.bot.plugins['userdata'].set_data(self, key, value, &block)
29     end
30   end
31 end
32
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.
36 #
37 class UserDataModule < CoreBotModule
38
39   def initialize
40     super
41     @ircuser = @registry.sub_registry('ircuser')
42     @botuser = @registry.sub_registry('botuser')
43   end
44
45   def get_data_hash(user)
46     iu = user.to_irc_user
47     bu = iu.botuser
48
49     ih = @ircuser[iu.nick] || {}
50
51     if bu.transient? or bu == Irc::Bot::Auth.defaultbotuser
52       return ih
53     else
54       bh = @botuser[bu.username] || {}
55       return ih.merge! bh
56     end
57   end
58
59   def get_data(user, key=nil)
60     h = get_data_hash(user)
61     debug h
62     return h if key.nil?
63     return h[key]
64   end
65
66   def set_data(user, key, value=nil, &block)
67     h = get_data_hash(user)
68     debug h
69
70     ret = value
71
72     if not block_given?
73       h[key] = value
74     else
75       if value and not h.has_key?(key)
76         h[key] = value
77       end
78       ret = yield h[key]
79     end
80     debug ret
81
82     iu = user.to_irc_user
83     bu = iu.botuser
84
85     if bu.transient? or bu == Irc::Bot::Auth.defaultbotuser
86       @ircuser[iu.nick] = h
87     else
88       @botuser[bu.username] = h
89     end
90     return ret
91   end
92
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)
97     if data
98       m.reply (_("%{key} data for %{user}: %{data}") % {
99         :key => key,
100         :user => user.nick,
101         :data => data
102       })
103     else
104       m.reply (_("sorry, no %{key} data for %{user}") % {
105         :key => key,
106         :user => user.nick,
107       })
108     end
109   end
110
111   ### TODO FIXME not yet: are we going to allow non-string
112   ### values for data? if so, this can't work ...
113   #
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
118   # end
119
120 end
121
122 plugin = UserDataModule.new
123
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'
127
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