3 # :title: User management
\r
5 # rbot user management
\r
6 # Author:: Giuseppe Bilotta (giuseppe.bilotta@gmail.com)
\r
7 # Copyright:: Copyright (c) 2006 Giuseppe Bilotta
\r
13 # This would be a good idea if it was failproof, but the truth
\r
14 # is that other methods can indirectly modify the hash. *sigh*
\r
16 # class AuthNotifyingHash < Hash
\r
17 # %w(clear default= delete delete_if replace invert
\r
18 # merge! update rehash reject! replace shift []= store).each { |m|
\r
20 # define_method(m) { |*a|
\r
22 # Irc::Bot::Auth.manager.set_changed
\r
34 # This module contains the actual Authentication stuff
\r
38 Config.register Config::StringValue.new( 'auth.password',
\r
39 :default => 'rbotauth', :wizard => true,
\r
40 :on_change => Proc.new {|bot, v| bot.auth.botowner.password = v},
\r
41 :desc => _('Password for the bot owner'))
\r
42 Config.register Config::BooleanValue.new( 'auth.login_by_mask',
\r
44 :desc => _('Set false to prevent new botusers from logging in without a password when the user netmask is known'))
\r
45 Config.register Config::BooleanValue.new( 'auth.autologin',
\r
47 :desc => _('Set false to prevent new botusers from recognizing IRC users without a need to manually login'))
\r
48 Config.register Config::BooleanValue.new( 'auth.autouser',
\r
49 :default => 'false',
\r
50 :desc => _('Set true to allow new botusers to be created automatically'))
\r
51 # Config.register Config::IntegerValue.new( 'auth.default_level',
\r
52 # :default => 10, :wizard => true,
\r
53 # :desc => 'The default level for new/unknown users' )
\r
55 # Generate a random password of length _l_
\r
57 def Auth.random_password(l=8)
\r
60 pwd << (rand(26) + (rand(2) == 0 ? 65 : 97) ).chr
\r
66 # An Irc::Bot::Auth::Command defines a command by its "path":
\r
68 # base::command::subcommand::subsubcommand::subsubsubcommand
\r
72 attr_reader :command, :path
\r
74 # A method that checks if a given _cmd_ is in a form that can be
\r
75 # reduced into a canonical command path, and if so, returns it
\r
77 def sanitize_command_path(cmd)
\r
78 pre = cmd.to_s.downcase.gsub(/^\*?(?:::)?/,"").gsub(/::$/,"")
\r
79 return pre if pre.empty?
\r
80 return pre if pre =~ /^\S+(::\S+)*$/
\r
81 raise TypeError, "#{cmd.inspect} is not a valid command"
\r
84 # Creates a new Command from a given string; you can then access
\r
85 # the command as a symbol with the :command method and the whole
\r
88 # Command.new("core::auth::save").path => [:"*", :"core", :"core::auth", :"core::auth::save"]
\r
90 # Command.new("core::auth::save").command => :"core::auth::save"
\r
93 cmdpath = sanitize_command_path(cmd).split('::')
\r
94 seq = cmdpath.inject(["*"]) { |list, cmd|
\r
95 list << (list.length > 1 ? list.last + "::" : "") + cmd
\r
97 @path = seq.map { |k|
\r
100 @command = path.last
\r
101 debug "Created command #{@command.inspect} with path #{@path.pretty_inspect}"
\r
105 def to_irc_auth_command
\r
119 # Returns an Irc::Bot::Auth::Comand from the receiver
\r
120 def to_irc_auth_command
\r
121 Irc::Bot::Auth::Command.new(self)
\r
129 # Returns an Irc::Bot::Auth::Comand from the receiver
\r
130 def to_irc_auth_command
\r
131 Irc::Bot::Auth::Command.new(self)
\r
144 # This class describes a permission set
\r
145 class PermissionSet
\r
148 # Create a new (empty) PermissionSet
\r
154 # Inspection simply inspects the internal hash
\r
159 # Sets the permission for command _cmd_ to _val_,
\r
161 def set_permission(str, val)
\r
162 cmd = str.to_irc_auth_command
\r
165 @perm[cmd.command] = val
\r
167 @perm.delete(cmd.command)
\r
169 raise TypeError, "#{val.inspect} must be true or false" unless [true,false].include?(val)
\r
173 # Resets the permission for command _cmd_
\r
175 def reset_permission(cmd)
\r
176 set_permission(cmd, nil)
\r
179 # Tells if command _cmd_ is permitted. We do this by returning
\r
180 # the value of the deepest Command#path that matches.
\r
183 cmd = str.to_irc_auth_command
\r
185 cmd.path.reverse.each { |k|
\r
186 if @perm.has_key?(k)
\r
197 # This is the error that gets raised when an invalid password is met
\r
199 class InvalidPassword < RuntimeError
\r
203 # This is the basic class for bot users: they have a username, a
\r
204 # password, a list of netmasks to match against, and a list of
\r
205 # permissions. A BotUser can be marked as 'transient', usually meaning
\r
206 # it's not intended for permanent storage. Transient BotUsers have lower
\r
207 # priority than nontransient ones for autologin purposes.
\r
209 # To initialize a BotUser, you pass a _username_ and an optional
\r
210 # hash of options. Currently, only two options are recognized:
\r
212 # transient:: true or false, determines if the BotUser is transient or
\r
213 # permanent (default is false, permanent BotUser).
\r
215 # Transient BotUsers are initialized by prepending an
\r
216 # asterisk (*) to the username, and appending a sanitized
\r
217 # version of the object_id. The username can be empty.
\r
218 # A random password is generated.
\r
220 # Permanent Botusers need the username as is, and no
\r
221 # password is generated.
\r
223 # masks:: an array of Netmasks to initialize the NetmaskList. This
\r
224 # list is used as-is for permanent BotUsers.
\r
226 # Transient BotUsers will alter the list elements which are
\r
227 # Irc::User by globbing the nick and any initial nonletter
\r
228 # part of the ident.
\r
230 # The masks option is optional for permanent BotUsers, but
\r
231 # obligatory (non-empty) for transients.
\r
235 attr_reader :username
\r
236 attr_reader :password
\r
237 attr_reader :netmasks
\r
239 # Please remember to #set_changed() the Auth.manager
\r
240 # when modifying data
\r
242 attr_writer :login_by_mask
\r
243 attr_writer :autologin
\r
244 attr_writer :transient
\r
246 # Checks if the BotUser is transient
\r
251 # Checks if the BotUser is permanent (not transient)
\r
256 # Sets if the BotUser is permanent or not
\r
257 def permanent=(bool)
\r
261 # Create a new BotUser with given username
\r
262 def initialize(username, options={})
\r
263 opts = {:transient => false}.merge(options)
\r
264 @transient = opts[:transient]
\r
268 @username << BotUser.sanitize_username(username) if username and not username.to_s.empty?
\r
269 @username << BotUser.sanitize_username(object_id)
\r
271 @login_by_mask=true
\r
274 @username = BotUser.sanitize_username(username)
\r
276 reset_login_by_mask
\r
280 @netmasks = NetmaskList.new
\r
281 if opts.key?(:masks) and opts[:masks]
\r
282 masks = opts[:masks]
\r
283 masks = [masks] unless masks.respond_to?(:each)
\r
285 mask = m.to_irc_netmask
\r
286 if @transient and User === m
\r
288 mask.host = m.host.dup
\r
289 mask.user = "*" + m.user.sub(/^\w?[^\w]+/,'')
\r
291 add_netmask(mask) unless mask.to_s == "*"
\r
294 raise "must provide a usable mask for transient BotUser #{@username}" if @transient and @netmasks.empty?
\r
298 # @data = AuthNotifyingHash.new
\r
304 str = "<#{self.class}:#{'0x%08x' % self.object_id}"
\r
305 str << " (transient)" if @transient
\r
307 str << " @username=#{@username.inspect}"
\r
308 str << " @netmasks=#{@netmasks.inspect}"
\r
309 str << " @perm=#{@perm.inspect}"
\r
310 str << " @login_by_mask=#{@login_by_mask}"
\r
311 str << " @autologin=#{@autologin}"
\r
315 str << " data for #{@data.keys.join(', ')}"
\r
325 # Convert into a hash
\r
328 :username => @username,
\r
329 :password => @password,
\r
330 :netmasks => @netmasks,
\r
332 :login_by_mask => @login_by_mask,
\r
333 :autologin => @autologin,
\r
338 # Do we allow logging in without providing the password?
\r
344 # Reset the login-by-mask option
\r
346 def reset_login_by_mask
\r
347 @login_by_mask = Auth.manager.bot.config['auth.login_by_mask'] unless defined?(@login_by_mask)
\r
350 # Reset the autologin option
\r
352 def reset_autologin
\r
353 @autologin = Auth.manager.bot.config['auth.autologin'] unless defined?(@autologin)
\r
356 # Do we allow automatic logging in?
\r
362 # Restore from hash
\r
364 @username = h[:username] if h.has_key?(:username)
\r
365 @password = h[:password] if h.has_key?(:password)
\r
366 @netmasks = h[:netmasks] if h.has_key?(:netmasks)
\r
367 @perm = h[:perm] if h.has_key?(:perm)
\r
368 @login_by_mask = h[:login_by_mask] if h.has_key?(:login_by_mask)
\r
369 @autologin = h[:autologin] if h.has_key?(:autologin)
\r
370 @data.replace(h[:data]) if h.has_key?(:data)
\r
373 # This method sets the password if the proposed new password
\r
375 def password=(pwd=nil)
\r
381 raise InvalidPassword, "#{pass} contains invalid characters" if pass !~ /^[\x21-\x7e]+$/
\r
382 raise InvalidPassword, "#{pass} too short" if pass.length < 4
\r
384 rescue InvalidPassword => e
\r
387 raise InvalidPassword, "Exception #{e.inspect} while checking #{pass.inspect} (#{pwd.inspect})"
\r
392 # Resets the password by creating a new onw
\r
394 @password = Auth.random_password
\r
397 # Sets the permission for command _cmd_ to _val_ on channel _chan_
\r
399 def set_permission(cmd, val, chan="*")
\r
400 k = chan.to_s.to_sym
\r
401 @perm[k] = PermissionSet.new unless @perm.has_key?(k)
\r
402 @perm[k].set_permission(cmd, val)
\r
405 # Resets the permission for command _cmd_ on channel _chan_
\r
407 def reset_permission(cmd, chan ="*")
\r
408 set_permission(cmd, nil, chan)
\r
411 # Checks if BotUser is allowed to do something on channel _chan_,
\r
412 # or on all channels if _chan_ is nil
\r
414 def permit?(cmd, chan=nil)
\r
416 k = chan.to_s.to_sym
\r
421 if @perm.has_key?(k)
\r
422 allow = @perm[k].permit?(cmd)
\r
429 def add_netmask(mask)
\r
430 @netmasks << mask.to_irc_netmask
\r
433 # Removes a Netmask
\r
435 def delete_netmask(mask)
\r
436 m = mask.to_irc_netmask
\r
437 @netmasks.delete(m)
\r
440 # Removes all <code>Netmask</code>s
\r
443 @netmasks = NetmaskList.new
\r
446 # This method checks if BotUser has a Netmask that matches _user_
\r
449 user = usr.to_irc_user
\r
451 @netmasks.each { |n|
\r
452 if user.matches?(n)
\r
460 # This method gets called when User _user_ wants to log in.
\r
461 # It returns true or false depending on whether the password
\r
462 # is right. If it is, the Netmask of the user is added to the
\r
463 # list of acceptable Netmask unless it's already matched.
\r
464 def login(user, password=nil)
\r
465 if password == @password or (password.nil? and (@login_by_mask || @autologin) and knows?(user))
\r
466 add_netmask(user) unless knows?(user)
\r
467 debug "#{user} logged in as #{self.inspect}"
\r
474 # # This method gets called when User _user_ has logged out as this BotUser
\r
476 # delete_netmask(user) if knows?(user)
\r
479 # This method sanitizes a username by chomping, downcasing
\r
480 # and replacing any nonalphanumeric character with _
\r
482 def BotUser.sanitize_username(name)
\r
483 candidate = name.to_s.chomp.downcase.gsub(/[^a-z0-9]/,"_")
\r
484 raise "sanitized botusername #{candidate} too short" if candidate.length < 3
\r
490 # This is the default BotUser: it's used for all users which haven't
\r
491 # identified with the bot
\r
493 class DefaultBotUserClass < BotUser
\r
495 private :add_netmask, :delete_netmask
\r
499 # The default BotUser is named 'everyone'
\r
502 reset_login_by_mask
\r
505 @default_perm = PermissionSet.new
\r
508 # This method returns without changing anything
\r
510 def login_by_mask=(val)
\r
511 debug "Tried to change the login-by-mask for default bot user, ignoring"
\r
512 return @login_by_mask
\r
515 # The default botuser allows logins by mask
\r
517 def reset_login_by_mask
\r
518 @login_by_mask = true
\r
521 # This method returns without changing anything
\r
523 def autologin=(val)
\r
524 debug "Tried to change the autologin for default bot user, ignoring"
\r
528 # The default botuser doesn't allow autologin (meaningless)
\r
530 def reset_autologin
\r
534 # Sets the default permission for the default user (i.e. the ones
\r
535 # set by the BotModule writers) on all channels
\r
537 def set_default_permission(cmd, val)
\r
538 @default_perm.set_permission(Command.new(cmd), val)
\r
539 debug "Default permissions now: #{@default_perm.pretty_inspect}"
\r
542 # default knows everybody
\r
545 return true if user.to_irc_user
\r
548 # We always allow logging in as the default user
\r
549 def login(user, password)
\r
553 # Resets the NetmaskList
\r
556 add_netmask("*!*@*")
\r
559 # DefaultBotUser will check the default_perm after checking
\r
561 # or on all channels if _chan_ is nil
\r
563 def permit?(cmd, chan=nil)
\r
564 allow = super(cmd, chan)
\r
565 if allow.nil? && chan.nil?
\r
566 allow = @default_perm.permit?(cmd)
\r
573 # Returns the only instance of DefaultBotUserClass
\r
575 def Auth.defaultbotuser
\r
576 return DefaultBotUserClass.instance
\r
579 # This is the BotOwner: he can do everything
\r
581 class BotOwnerClass < BotUser
\r
586 @login_by_mask = false
\r
591 def permit?(cmd, chan=nil)
\r
597 # Returns the only instance of BotOwnerClass
\r
600 return BotOwnerClass.instance
\r
604 # This is the ManagerClass singleton, used to manage
\r
605 # Irc::User/Irc::Bot::Auth::BotUser connections and everything
\r
611 attr_reader :everyone
\r
612 attr_reader :botowner
\r
615 # The instance manages two <code>Hash</code>es: one that maps
\r
616 # <code>Irc::User</code>s onto <code>BotUser</code>s, and the other that maps
\r
617 # usernames onto <code>BotUser</code>
\r
619 @everyone = Auth::defaultbotuser
\r
620 @botowner = Auth::botowner
\r
624 def bot_associate(bot)
\r
625 raise "Cannot associate with a new bot! Save first" if defined?(@has_changes) && @has_changes
\r
632 # This variable is set to true when there have been changes
\r
633 # to the botusers list, so that we know when to save
\r
634 @has_changes = false
\r
638 @has_changes = true
\r
642 @has_changes = false
\r
649 # resets the hashes
\r
651 @botusers = Hash.new
\r
652 @allbotusers = Hash.new
\r
653 [everyone, botowner].each { |x|
\r
654 @allbotusers[x.username.to_sym] = x
\r
656 @transients = Set.new
\r
659 def load_array(ary, forced)
\r
661 warning "Tried to load an empty array"
\r
664 raise "Won't load with unsaved changes" if @has_changes and not forced
\r
667 raise TypeError, "#{x} should be a Hash" unless x.kind_of?(Hash)
\r
672 get_botuser(u).from_hash(x)
\r
673 get_botuser(u).transient = false
\r
679 @allbotusers.values.map { |x|
\r
680 x.transient? ? nil : x.to_hash
\r
684 # checks if we know about a certain BotUser username
\r
685 def include?(botusername)
\r
686 @allbotusers.has_key?(botusername.to_sym)
\r
689 # Maps <code>Irc::User</code> to BotUser
\r
690 def irc_to_botuser(ircuser)
\r
691 logged = @botusers[ircuser.to_irc_user]
\r
692 return logged if logged
\r
693 return autologin(ircuser)
\r
696 # creates a new BotUser
\r
697 def create_botuser(name, password=nil)
\r
698 n = BotUser.sanitize_username(name)
\r
700 raise "botuser #{n} exists" if include?(k)
\r
701 bu = BotUser.new(n)
\r
702 bu.password = password
\r
703 @allbotusers[k] = bu
\r
707 # returns the botuser with name _name_
\r
708 def get_botuser(name)
\r
709 @allbotusers.fetch(BotUser.sanitize_username(name).to_sym)
\r
712 # Logs Irc::User _user_ in to BotUser _botusername_ with password _pwd_
\r
714 # raises an error if _botusername_ is not a known BotUser username
\r
716 # It is possible to autologin by Netmask, on request
\r
718 def login(user, botusername, pwd=nil)
\r
719 ircuser = user.to_irc_user
\r
720 n = BotUser.sanitize_username(botusername)
\r
722 raise "No such BotUser #{n}" unless include?(k)
\r
723 if @botusers.has_key?(ircuser)
\r
724 return true if @botusers[ircuser].username == n
\r
726 # @botusers[ircuser].logout(ircuser)
\r
728 bu = @allbotusers[k]
\r
729 if bu.login(ircuser, pwd)
\r
730 @botusers[ircuser] = bu
\r
736 # Tries to auto-login Irc::User _user_ by looking at the known botusers that allow autologin
\r
737 # and trying to login without a password
\r
739 def autologin(user)
\r
740 ircuser = user.to_irc_user
\r
741 debug "Trying to autologin #{ircuser}"
\r
742 return @botusers[ircuser] if @botusers.has_key?(ircuser)
\r
743 @allbotusers.each { |n, bu|
\r
744 debug "Checking with #{n}"
\r
745 return bu if bu.autologin? and login(ircuser, n)
\r
747 # Check with transient users
\r
748 @transients.each { |bu|
\r
749 return bu if bu.login(ircuser)
\r
751 # Finally, create a transient if we're set to allow it
\r
752 if @bot.config['auth.autouser']
\r
753 bu = create_transient_botuser(ircuser)
\r
759 # Creates a new transient BotUser associated with Irc::User _user_,
\r
760 # automatically logging him in. Note that transient botuser creation can
\r
761 # fail, typically if we don't have the complete user netmask (e.g. for
\r
762 # messages coming in from a linkbot)
\r
764 def create_transient_botuser(user)
\r
765 ircuser = user.to_irc_user
\r
768 bu = BotUser.new(ircuser, :transient => true, :masks => ircuser)
\r
772 warning "failed to create transient for #{user}"
\r
778 # Checks if User _user_ can do _cmd_ on _chan_.
\r
780 # Permission are checked in this order, until a true or false
\r
782 # * associated BotUser on _chan_
\r
783 # * associated BotUser on all channels
\r
784 # * everyone on _chan_
\r
785 # * everyone on all channels
\r
787 def permit?(user, cmdtxt, channel=nil)
\r
788 if user.class <= BotUser
\r
791 botuser = irc_to_botuser(user)
\r
793 cmd = cmdtxt.to_irc_auth_command
\r
805 allow = botuser.permit?(cmd, chan) if chan
\r
806 return allow unless allow.nil?
\r
807 allow = botuser.permit?(cmd)
\r
808 return allow unless allow.nil?
\r
810 unless botuser == everyone
\r
811 allow = everyone.permit?(cmd, chan) if chan
\r
812 return allow unless allow.nil?
\r
813 allow = everyone.permit?(cmd)
\r
814 return allow unless allow.nil?
\r
817 raise "Could not check permission for user #{user.inspect} to run #{cmdtxt.inspect} on #{chan.inspect}"
\r
820 # Checks if command _cmd_ is allowed to User _user_ on _chan_, optionally
\r
821 # telling if the user is authorized
\r
823 def allow?(cmdtxt, user, chan=nil)
\r
824 if permit?(user, cmdtxt, chan)
\r
827 # cmds = cmdtxt.split('::')
\r
828 # @bot.say chan, "you don't have #{cmds.last} (#{cmds.first}) permissions here" if chan
\r
829 @bot.say chan, _("%{user}, you don't have '%{command}' permissions here") %
\r
830 {:user=>user, :command=>cmdtxt} if chan
\r
837 # Returns the only instance of ManagerClass
\r
840 return ManagerClass.instance
\r
848 # A convenience method to automatically found the botuser
\r
849 # associated with the receiver
\r
852 Irc::Bot::Auth.manager.irc_to_botuser(self)
\r
855 # Bot-specific data can be stored with Irc::Users. This is
\r
856 # internally obtained by storing data to the associated BotUser,
\r
857 # but this is a detail plugin writers shouldn't care about.
\r
858 # bot_data(:key) can be used to retrieve a particular data set.
\r
859 # This method is intended for data retrieval, and if the retrieved
\r
860 # data is modified directly there is no guarantee the changes will
\r
861 # be saved back. Use #set_bot_data() for that.
\r
863 def bot_data(key=nil)
\r
864 return self.botuser.data if key.nil?
\r
865 return self.botuser.data[key]
\r
868 # This method is used to store bot-specific data for the receiver.
\r
869 # If no block is passed, _value_ is stored for the key _key_;
\r
870 # if a block is passed, it will be called with the previous
\r
871 # _key_ value as parameter, and its return value will be stored
\r
872 # as the new value. If _value_ is present in the block form, it
\r
873 # will be used to initialize _key_ if it's missing
\r
875 def set_bot_data(key,value=nil,&block)
\r
876 if not block_given?
\r
877 self.botuser.data[key]=value
\r
878 Irc::Bot::Auth.manager.set_changed
\r
881 if value and not bot_data.has_key?(key)
\r
882 set_bot_data(key, value)
\r
886 r = yield bot_data(key)
\r
888 Irc::Bot::Auth.manager.set_changed
\r