X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;f=lib%2Frbot%2Fcore%2Fuserdata.rb;h=f7c74a4a5fa7f9b0657910e7c5c01241fe016413;hb=2ecf2f58c843895ce4ad143d0a05283c4b7e37e8;hp=8a05682047f21f79584481aa12ae797dcb77dff1;hpb=d82b04ba5d10cff7f9b601fc6d4b2b01795016f7;p=user%2Fhenk%2Fcode%2Fruby%2Frbot.git diff --git a/lib/rbot/core/userdata.rb b/lib/rbot/core/userdata.rb index 8a056820..f7c74a4a 100644 --- a/lib/rbot/core/userdata.rb +++ b/lib/rbot/core/userdata.rb @@ -52,21 +52,35 @@ class UserDataModule < CoreBotModule def initialize super @ircuser = @registry.sub_registry('ircuser') + @transient = @registry.sub_registry('transient') @botuser = @registry.sub_registry('botuser') end - def get_data_hash(user) + def get_data_hash(user, opts={}) + plain = opts[:plain] iu = user.to_irc_user bu = iu.botuser ih = @ircuser[iu.nick] || {} - if bu.transient? or bu.default? + if bu.default? return ih + elsif bu.transient? + bh = @transient[bu.netmasks.first.fullform] || {} else bh = @botuser[bu.username] || {} - return ih.merge! bh end + ih.merge!(bh) + + unless plain + class << ih + alias :single_retrieve :[] + alias :single_assign :[]= + include DottedIndex + end + end + + return ih end def get_data(user, key=nil) @@ -76,14 +90,23 @@ class UserDataModule < CoreBotModule return h[key] end - def set_data_hash(user, h) + def set_data_hash(user, hh) iu = user.to_irc_user bu = iu.botuser + # we .dup the hash to remove singleton methods + # and make it dump-able + h = hh.dup + @ircuser[iu.nick] = h - unless bu.transient? or bu.default? + return h if bu.default? + + if bu.transient? + @transient[bu.netmasks.first.fullform] = h + else @botuser[bu.username] = h end + return h end def set_data(user, key, value=nil, &block) @@ -122,13 +145,13 @@ class UserDataModule < CoreBotModule key = params[:key].intern data = get_data(user, key) if data - m.reply (_("%{key} data for %{user}: %{data}") % { + m.reply(_("%{key} data for %{user}: %{data}") % { :key => key, :user => user.nick, :data => data }) else - m.reply (_("sorry, no %{key} data for %{user}") % { + m.reply(_("sorry, no %{key} data for %{user}") % { :key => key, :user => user.nick, }) @@ -144,6 +167,28 @@ class UserDataModule < CoreBotModule # data = params[:data].to_s # end + def event_botuser(action, opts={}) + case action + when :copy, :rename + source = opts[:source] + return unless @botuser.key?(source) + dest = opts[:dest] + @botuser[dest] = @botuser[source].dup + @botuser.delete(source) if action == :rename + when :pre_perm + @permification ||= {} + k = [opts[:irc_user], opts[:bot_user]] + @permification[k] = get_data_hash(opts[:irc_user], :plain => true) + when :post_perm + @permification ||= {} + k = [opts[:irc_user], opts[:bot_user]] + if @permification.has_key?(k) + @botuser[opts[:bot_user]] = @permification[k] + @permification.delete(k) + end + end + end + end plugin = UserDataModule.new