X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;f=lib%2Frbot%2Fbotuser.rb;h=defb3a47afa9b119f7cbaeb626d41dce29d2cbc2;hb=43864ec494a9c2538934690f32eacb8bfb5cc921;hp=146912b43587093325e4625fe6ae02c0df73fb4e;hpb=6f5528a63b44e610a3d25d7fe583399163d7d2da;p=user%2Fhenk%2Fcode%2Fruby%2Frbot.git diff --git a/lib/rbot/botuser.rb b/lib/rbot/botuser.rb index 146912b4..defb3a47 100644 --- a/lib/rbot/botuser.rb +++ b/lib/rbot/botuser.rb @@ -9,6 +9,7 @@ require 'singleton' require 'set' +require 'rbot/maskdb' # This would be a good idea if it was failproof, but the truth # is that other methods can indirectly modify the hash. *sigh* @@ -19,7 +20,7 @@ require 'set' # class_eval { # define_method(m) { |*a| # r = super(*a) -# Irc::Bot::Auth.authmanager.set_changed +# Irc::Bot::Auth.manager.set_changed # r # } # } @@ -181,6 +182,9 @@ class Bot # def permit?(str) cmd = str.to_irc_auth_command + # TODO user-configurable list of always-allowed commands, + # for admins that want to set permissions -* for everybody + return true if cmd.command == :login allow = nil cmd.path.reverse.each { |k| if @perm.has_key?(k) @@ -236,13 +240,19 @@ class Bot attr_reader :password attr_reader :netmasks attr_reader :perm - # Please remember to #set_changed() the authmanager - # when modifying data - attr_reader :data attr_writer :login_by_mask - attr_writer :autologin attr_writer :transient + def autologin=(vnew) + vold = @autologin + @autologin = vnew + if vold && !vnew + @netmasks.each { |n| Auth.manager.maskdb.remove(self, n) } + elsif vnew && !vold + @netmasks.each { |n| Auth.manager.maskdb.add(self, n) } + end + end + # Checks if the BotUser is transient def transient? @transient @@ -250,7 +260,7 @@ class Bot # Checks if the BotUser is permanent (not transient) def permanent? - !@permanent + !@transient end # Sets if the BotUser is permanent or not @@ -258,6 +268,19 @@ class Bot @transient=!bool end + # Make the BotUser permanent + def make_permanent(name) + raise TypeError, "permanent already" if permanent? + @username = BotUser.sanitize_username(name) + @transient = false + reset_autologin + reset_password # or not? + @netmasks.dup.each do |m| + delete_netmask(m) + add_netmask(m.generalize) + end + end + # Create a new BotUser with given username def initialize(username, options={}) opts = {:transient => false}.merge(options) @@ -294,14 +317,11 @@ class Bot raise "must provide a usable mask for transient BotUser #{@username}" if @transient and @netmasks.empty? @perm = {} - - # @data = AuthNotifyingHash.new - @data = {} end # Inspection def inspect - str = "<#{self.class}:#{'0x%08x' % self.object_id}" + str = self.__to_s__[0..-2] str << " (transient)" if @transient str << ":" str << " @username=#{@username.inspect}" @@ -309,11 +329,6 @@ class Bot str << " @perm=#{@perm.inspect}" str << " @login_by_mask=#{@login_by_mask}" str << " @autologin=#{@autologin}" - if @data.empty? - str << " no data" - else - str << " data for #{@data.keys.join(', ')}" - end str << ">" end @@ -331,7 +346,6 @@ class Bot :perm => @perm, :login_by_mask => @login_by_mask, :autologin => @autologin, - :data => @data } end @@ -344,13 +358,13 @@ class Bot # Reset the login-by-mask option # def reset_login_by_mask - @login_by_mask = Auth.authmanager.bot.config['auth.login_by_mask'] unless defined?(@login_by_mask) + @login_by_mask = Auth.manager.bot.config['auth.login_by_mask'] unless defined?(@login_by_mask) end # Reset the autologin option # def reset_autologin - @autologin = Auth.authmanager.bot.config['auth.autologin'] unless defined?(@autologin) + @autologin = Auth.manager.bot.config['auth.autologin'] unless defined?(@autologin) end # Do we allow automatic logging in? @@ -363,11 +377,13 @@ class Bot def from_hash(h) @username = h[:username] if h.has_key?(:username) @password = h[:password] if h.has_key?(:password) - @netmasks = h[:netmasks] if h.has_key?(:netmasks) - @perm = h[:perm] if h.has_key?(:perm) @login_by_mask = h[:login_by_mask] if h.has_key?(:login_by_mask) @autologin = h[:autologin] if h.has_key?(:autologin) - @data.replace(h[:data]) if h.has_key?(:data) + if h.has_key?(:netmasks) + @netmasks = h[:netmasks] + @netmasks.each { |n| Auth.manager.maskdb.add(self, n) } if @autologin + end + @perm = h[:perm] if h.has_key?(:perm) end # This method sets the password if the proposed new password @@ -427,7 +443,12 @@ class Bot # Adds a Netmask # def add_netmask(mask) - @netmasks << mask.to_irc_netmask + m = mask.to_irc_netmask + @netmasks << m + if self.autologin? + Auth.manager.maskdb.add(self, m) + Auth.manager.logout_transients(m) if self.permanent? + end end # Removes a Netmask @@ -435,26 +456,14 @@ class Bot def delete_netmask(mask) m = mask.to_irc_netmask @netmasks.delete(m) - end - - # Removes all Netmasks - # - def reset_netmasks - @netmasks = NetmaskList.new + Auth.manager.maskdb.remove(self, m) if self.autologin? end # This method checks if BotUser has a Netmask that matches _user_ # def knows?(usr) user = usr.to_irc_user - known = false - @netmasks.each { |n| - if user.matches?(n) - known = true - break - end - } - return known + !!@netmasks.find { |n| user.matches? n } end # This method gets called when User _user_ wants to log in. @@ -550,12 +559,6 @@ class Bot return true end - # Resets the NetmaskList - def reset_netmasks - super - add_netmask("*!*@*") - end - # DefaultBotUser will check the default_perm after checking # the global ones # or on all channels if _chan_ is nil @@ -601,13 +604,27 @@ class Bot end - # This is the AuthManagerClass singleton, used to manage User/BotUser connections and - # everything + class BotUser + # Check if the current BotUser is the default one + def default? + return DefaultBotUserClass === self + end + + # Check if the current BotUser is the owner + def owner? + return BotOwnerClass === self + end + end + + + # This is the ManagerClass singleton, used to manage + # Irc::User/Irc::Bot::Auth::BotUser connections and everything # - class AuthManagerClass + class ManagerClass include Singleton + attr_reader :maskdb attr_reader :everyone attr_reader :botowner attr_reader :bot @@ -649,11 +666,11 @@ class Bot # resets the hashes def reset_hashes @botusers = Hash.new + @maskdb = NetmaskDb.new @allbotusers = Hash.new - [everyone, botowner].each { |x| + [everyone, botowner].each do |x| @allbotusers[x.username.to_sym] = x - } - @transients = Set.new + end end def load_array(ary, forced) @@ -740,33 +757,81 @@ class Bot ircuser = user.to_irc_user debug "Trying to autologin #{ircuser}" return @botusers[ircuser] if @botusers.has_key?(ircuser) - @allbotusers.each { |n, bu| - debug "Checking with #{n}" - return bu if bu.autologin? and login(ircuser, n) - } - # Check with transient users - @transients.each { |bu| - return bu if bu.login(ircuser) - } + bu = maskdb.find(ircuser) + if bu + debug "trying #{bu}" + bu.login(ircuser) or raise '...what?!' + @botusers[ircuser] = bu + return bu + end # Finally, create a transient if we're set to allow it if @bot.config['auth.autouser'] bu = create_transient_botuser(ircuser) + @botusers[ircuser] = bu return bu end return everyone end # Creates a new transient BotUser associated with Irc::User _user_, - # automatically logging him in + # automatically logging him in. Note that transient botuser creation can + # fail, typically if we don't have the complete user netmask (e.g. for + # messages coming in from a linkbot) # def create_transient_botuser(user) ircuser = user.to_irc_user - bu = BotUser.new(ircuser, :transient => true, :masks => ircuser) - bu.login(ircuser) - @transients << bu + bu = everyone + begin + bu = BotUser.new(ircuser, :transient => true, :masks => ircuser) + bu.login(ircuser) + rescue + warning "failed to create transient for #{user}" + error $! + end return bu end + # Logs out any Irc::User matching Irc::Netmask _m_ and logged in + # to a transient BotUser + # + def logout_transients(m) + debug "to check: #{@botusers.keys.join ' '}" + @botusers.keys.each do |iu| + debug "checking #{iu.fullform} against #{m.fullform}" + bu = @botusers[iu] + bu.transient? or next + iu.matches?(m) or next + @botusers.delete(iu).autologin = false + end + end + + # Makes transient BotUser _user_ into a permanent BotUser + # named _name_; if _user_ is an Irc::User, act on the transient + # BotUser (if any) it's logged in as + # + def make_permanent(user, name) + buname = BotUser.sanitize_username(name) + # TODO merge BotUser instead? + raise "there's already a BotUser called #{name}" if include?(buname) + + tuser = nil + case user + when String, Irc::User + tuser = irc_to_botuser(user) + when BotUser + tuser = user + else + raise TypeError, "sorry, don't know how to make #{user.class} into a permanent BotUser" + end + return nil unless tuser + raise TypeError, "#{tuser} is not transient" unless tuser.transient? + + tuser.make_permanent(buname) + @allbotusers[tuser.username.to_sym] = tuser + + return tuser + end + # Checks if User _user_ can do _cmd_ on _chan_. # # Permission are checked in this order, until a true or false @@ -826,10 +891,10 @@ class Bot end - # Returns the only instance of AuthManagerClass + # Returns the only instance of ManagerClass # - def Auth.authmanager - return AuthManagerClass.instance + def Auth.manager + return ManagerClass.instance end end @@ -841,45 +906,7 @@ end # associated with the receiver # def botuser - Irc::Bot::Auth.authmanager.irc_to_botuser(self) - end - - # Bot-specific data can be stored with Irc::Users. This is - # internally obtained by storing data to the associated BotUser, - # but this is a detail plugin writers shouldn't care about. - # bot_data(:key) can be used to retrieve a particular data set. - # This method is intended for data retrieval, and if the retrieved - # data is modified directly there is no guarantee the changes will - # be saved back. Use #set_bot_data() for that. - # - def bot_data(key=nil) - return self.botuser.data if key.nil? - return self.botuser.data[key] - end - - # This method is used to store bot-specific data for the receiver. - # If no block is passed, _value_ is stored for the key _key_; - # if a block is passed, it will be called with the previous - # _key_ value as parameter, and its return value will be stored - # as the new value. If _value_ is present in the block form, it - # will be used to initialize _key_ if it's missing - # - def set_bot_data(key,value=nil,&block) - if not block_given? - self.botuser.data[key]=value - Irc::Bot::Auth.authmanager.set_changed - return value - end - if value and not bot_data.has_key?(key) - set_bot_data(key, value) - end - r = value - begin - r = yield bot_data(key) - ensure - Irc::Bot::Auth.authmanager.set_changed - end - return r + Irc::Bot::Auth.manager.irc_to_botuser(self) end end