]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blobdiff - lib/rbot/botuser.rb
New Auth Framework: BotUser#default? and owner? methods
[user/henk/code/ruby/rbot.git] / lib / rbot / botuser.rb
index 83fb26240333a0456a743fe662b3c61f26ebce01..f2c48421eb93e540e335e363810bea29c2e0c60c 100644 (file)
@@ -9,6 +9,7 @@
 \r
 require 'singleton'\r
 require 'set'\r
+require 'rbot/maskdb'\r
 \r
 # This would be a good idea if it was failproof, but the truth\r
 # is that other methods can indirectly modify the hash. *sigh*\r
@@ -236,13 +237,19 @@ class Bot
       attr_reader :password\r
       attr_reader :netmasks\r
       attr_reader :perm\r
-      # Please remember to #set_changed() the Auth.manager\r
-      # when modifying data\r
-      attr_reader :data\r
       attr_writer :login_by_mask\r
-      attr_writer :autologin\r
       attr_writer :transient\r
 \r
+      def autologin=(vnew)\r
+        vold = @autologin\r
+        @autologin = vnew\r
+        if vold && !vnew\r
+          @netmasks.each { |n| Auth.manager.maskdb.remove(self, n) }\r
+        elsif vnew && !vold\r
+          @netmasks.each { |n| Auth.manager.maskdb.add(self, n) }\r
+        end\r
+      end\r
+\r
       # Checks if the BotUser is transient\r
       def transient?\r
         @transient\r
@@ -250,7 +257,7 @@ class Bot
 \r
       # Checks if the BotUser is permanent (not transient)\r
       def permanent?\r
-        !@permanent\r
+        !@transient\r
       end\r
 \r
       # Sets if the BotUser is permanent or not\r
@@ -258,6 +265,19 @@ class Bot
         @transient=!bool\r
       end\r
 \r
+      # Make the BotUser permanent\r
+      def make_permanent(name)\r
+        raise TypError, "permanent already" if permanent?\r
+        @username = BotUser.sanitize_username(name)\r
+        @transient = false\r
+        reset_autologin\r
+        reset_password # or not?\r
+        @netmasks.dup.each do |m|\r
+          delete_netmask(m)\r
+          add_netmask(m.generalize)\r
+        end\r
+      end\r
+\r
       # Create a new BotUser with given username\r
       def initialize(username, options={})\r
         opts = {:transient => false}.merge(options)\r
@@ -294,9 +314,6 @@ class Bot
         raise "must provide a usable mask for transient BotUser #{@username}" if @transient and @netmasks.empty?\r
 \r
         @perm = {}\r
-\r
-        # @data = AuthNotifyingHash.new\r
-        @data = {}\r
       end\r
 \r
       # Inspection\r
@@ -309,11 +326,6 @@ class Bot
         str << " @perm=#{@perm.inspect}"\r
         str << " @login_by_mask=#{@login_by_mask}"\r
         str << " @autologin=#{@autologin}"\r
-        if @data.empty?\r
-          str << " no data"\r
-        else\r
-          str << " data for #{@data.keys.join(', ')}"\r
-        end\r
         str << ">"\r
       end\r
 \r
@@ -331,7 +343,6 @@ class Bot
           :perm => @perm,\r
           :login_by_mask => @login_by_mask,\r
           :autologin => @autologin,\r
-          :data => @data\r
         }\r
       end\r
 \r
@@ -363,11 +374,13 @@ class Bot
       def from_hash(h)\r
         @username = h[:username] if h.has_key?(:username)\r
         @password = h[:password] if h.has_key?(:password)\r
-        @netmasks = h[:netmasks] if h.has_key?(:netmasks)\r
-        @perm = h[:perm] if h.has_key?(:perm)\r
         @login_by_mask = h[:login_by_mask] if h.has_key?(:login_by_mask)\r
         @autologin = h[:autologin] if h.has_key?(:autologin)\r
-        @data.replace(h[:data]) if h.has_key?(:data)\r
+        if h.has_key?(:netmasks)\r
+          @netmasks = h[:netmasks]\r
+          @netmasks.each { |n| Auth.manager.maskdb.add(self, n) } if @autologin\r
+        end\r
+        @perm = h[:perm] if h.has_key?(:perm)\r
       end\r
 \r
       # This method sets the password if the proposed new password\r
@@ -427,7 +440,12 @@ class Bot
       # Adds a Netmask\r
       #\r
       def add_netmask(mask)\r
-        @netmasks << mask.to_irc_netmask\r
+        m = mask.to_irc_netmask\r
+        @netmasks << m\r
+        if self.autologin?\r
+          Auth.manager.maskdb.add(self, m)\r
+          Auth.manager.logout_transients(m) if self.permanent?\r
+        end\r
       end\r
 \r
       # Removes a Netmask\r
@@ -435,26 +453,14 @@ class Bot
       def delete_netmask(mask)\r
         m = mask.to_irc_netmask\r
         @netmasks.delete(m)\r
-      end\r
-\r
-      # Removes all <code>Netmask</code>s\r
-      #\r
-      def reset_netmasks\r
-        @netmasks = NetmaskList.new\r
+        Auth.manager.maskdb.remove(self, m) if self.autologin?\r
       end\r
 \r
       # This method checks if BotUser has a Netmask that matches _user_\r
       #\r
       def knows?(usr)\r
         user = usr.to_irc_user\r
-        known = false\r
-        @netmasks.each { |n|\r
-          if user.matches?(n)\r
-            known = true\r
-            break\r
-          end\r
-        }\r
-        return known\r
+        !!@netmasks.find { |n| user.matches? n }\r
       end\r
 \r
       # This method gets called when User _user_ wants to log in.\r
@@ -550,12 +556,6 @@ class Bot
         return true\r
       end\r
 \r
-      # Resets the NetmaskList\r
-      def reset_netmasks\r
-        super\r
-        add_netmask("*!*@*")\r
-      end\r
-\r
       # DefaultBotUser will check the default_perm after checking\r
       # the global ones\r
       # or on all channels if _chan_ is nil\r
@@ -601,6 +601,19 @@ class Bot
     end\r
 \r
 \r
+    class BotUser\r
+      # Check if the current BotUser is the default one\r
+      def default?\r
+        return DefaultBotUserClass === self\r
+      end\r
+\r
+      # Check if the current BotUser is the owner\r
+      def owner?\r
+        return BotOwnerClass === self\r
+      end\r
+    end\r
+\r
+\r
     # This is the ManagerClass singleton, used to manage\r
     # Irc::User/Irc::Bot::Auth::BotUser connections and everything\r
     #\r
@@ -608,6 +621,7 @@ class Bot
 \r
       include Singleton\r
 \r
+      attr_reader :maskdb\r
       attr_reader :everyone\r
       attr_reader :botowner\r
       attr_reader :bot\r
@@ -649,11 +663,11 @@ class Bot
       # resets the hashes\r
       def reset_hashes\r
         @botusers = Hash.new\r
+        @maskdb = NetmaskDb.new\r
         @allbotusers = Hash.new\r
-        [everyone, botowner].each { |x|\r
+        [everyone, botowner].each do |x|\r
           @allbotusers[x.username.to_sym] = x\r
-        }\r
-        @transients = Set.new\r
+        end\r
       end\r
 \r
       def load_array(ary, forced)\r
@@ -740,17 +754,17 @@ class Bot
         ircuser = user.to_irc_user\r
         debug "Trying to autologin #{ircuser}"\r
         return @botusers[ircuser] if @botusers.has_key?(ircuser)\r
-        @allbotusers.each { |n, bu|\r
-          debug "Checking with #{n}"\r
-          return bu if bu.autologin? and login(ircuser, n)\r
-        }\r
-        # Check with transient users\r
-        @transients.each { |bu|\r
-          return bu if bu.login(ircuser)\r
-        }\r
+        bu = maskdb.find(ircuser)\r
+        if bu\r
+          debug "trying #{bu}"\r
+          bu.login(ircuser) or raise '...what?!'\r
+          @botusers[ircuser] = bu\r
+          return bu\r
+        end\r
         # Finally, create a transient if we're set to allow it\r
         if @bot.config['auth.autouser']\r
           bu = create_transient_botuser(ircuser)\r
+          @botusers[ircuser] = bu\r
           return bu\r
         end\r
         return everyone\r
@@ -767,7 +781,6 @@ class Bot
         begin\r
           bu = BotUser.new(ircuser, :transient => true, :masks => ircuser)\r
           bu.login(ircuser)\r
-          @transients << bu\r
         rescue\r
           warning "failed to create transient for #{user}"\r
           error $!\r
@@ -775,6 +788,46 @@ class Bot
         return bu\r
       end\r
 \r
+      # Logs out any Irc::User matching Irc::Netmask _m_ and logged in\r
+      # to a transient BotUser\r
+      #\r
+      def logout_transients(m)\r
+        debug "to check: #{@botusers.keys.join ' '}"\r
+        @botusers.keys.each do |iu|\r
+          debug "checking #{iu.fullform} against #{m.fullform}"\r
+          bu = @botusers[iu]\r
+          bu.transient? or next\r
+          iu.matches?(m) or next\r
+          @botusers.delete(iu).autologin = false\r
+        end\r
+      end\r
+\r
+      # Makes transient BotUser _user_ into a permanent BotUser\r
+      # named _name_; if _user_ is an Irc::User, act on the transient\r
+      # BotUser (if any) it's logged in as\r
+      #\r
+      def make_permanent(user, name)\r
+        # TODO merge BotUser instead?\r
+        raise "there's already a BotUser called #{name}" if include?(name)\r
+\r
+        tuser = nil\r
+        case user\r
+        when String, Irc::User\r
+          tuser = irc_to_botuser(user)\r
+        when BotUser\r
+          tuser = user\r
+        else\r
+          raise TypeError, "sorry, don't know how to make #{user.class} into a permanent BotUser"\r
+        end\r
+        return nil unless tuser\r
+        raise TypeError, "#{tuser} is not transient" unless tuser.transient?\r
+\r
+        tuser.make_permanent(name)\r
+        @allbotusers[tuser.username.to_sym] = tuser\r
+\r
+        return tuser\r
+      end\r
+\r
       # Checks if User _user_ can do _cmd_ on _chan_.\r
       #\r
       # Permission are checked in this order, until a true or false\r
@@ -851,44 +904,6 @@ end
     def botuser\r
       Irc::Bot::Auth.manager.irc_to_botuser(self)\r
     end\r
-\r
-    # Bot-specific data can be stored with Irc::Users. This is\r
-    # internally obtained by storing data to the associated BotUser,\r
-    # but this is a detail plugin writers shouldn't care about.\r
-    # bot_data(:key) can be used to retrieve a particular data set.\r
-    # This method is intended for data retrieval, and if the retrieved\r
-    # data is modified directly there is no guarantee the changes will\r
-    # be saved back. Use #set_bot_data() for that.\r
-    #\r
-    def bot_data(key=nil)\r
-      return self.botuser.data if key.nil?\r
-      return self.botuser.data[key]\r
-    end\r
-\r
-    # This method is used to store bot-specific data for the receiver.\r
-    # If no block is passed, _value_ is stored for the key _key_;\r
-    # if a block is passed, it will be called with the previous\r
-    # _key_ value as parameter, and its return value will be stored\r
-    # as the new value. If _value_ is present in the block form, it\r
-    # will be used to initialize _key_ if it's missing\r
-    # \r
-    def set_bot_data(key,value=nil,&block)\r
-      if not block_given?\r
-        self.botuser.data[key]=value\r
-        Irc::Bot::Auth.manager.set_changed\r
-        return value\r
-      end\r
-      if value and not bot_data.has_key?(key)\r
-        set_bot_data(key, value)\r
-      end\r
-      r = value\r
-      begin\r
-        r = yield bot_data(key)\r
-      ensure\r
-        Irc::Bot::Auth.manager.set_changed\r
-      end\r
-      return r\r
-    end\r
   end\r
 \r
 end\r