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
16 # This module contains the actual Authentication stuff
\r
20 BotConfig.register BotConfigStringValue.new( 'auth.password',
\r
21 :default => 'rbotauth', :wizard => true,
\r
22 :desc => 'Password for the bot owner' )
\r
23 # BotConfig.register BotConfigIntegerValue.new( 'auth.default_level',
\r
24 # :default => 10, :wizard => true,
\r
25 # :desc => 'The default level for new/unknown users' )
\r
27 # Generate a random password of length _l_
\r
29 def random_password(l=8)
\r
32 pwd += (rand(26) + (rand(2) == 0 ? 65 : 97) ).chr
\r
38 # An Irc::Auth::Command defines a command by its "path":
\r
40 # base::command::subcommand::subsubcommand::subsubsubcommand
\r
44 attr_reader :command, :path
\r
46 # A method that checks if a given _cmd_ is in a form that can be
\r
47 # reduced into a canonical command path, and if so, returns it
\r
49 def sanitize_command_path(cmd)
\r
50 pre = cmd.to_s.downcase.gsub(/^\*?(?:::)?/,"").gsub(/::$/,"")
\r
51 return pre if pre.empty?
\r
52 return pre if pre =~ /^\S+(::\S+)*$/
\r
53 raise TypeError, "#{cmd.inspect} is not a valid command"
\r
56 # Creates a new Command from a given string; you can then access
\r
57 # the command as a symbol with the :command method and the whole
\r
60 # Command.new("core::auth::save").path => [:"*", :"core", :"core::auth", :"core::auth::save"]
\r
62 # Command.new("core::auth::save").command => :"core::auth::save"
\r
65 cmdpath = sanitize_command_path(cmd).split('::')
\r
66 seq = cmdpath.inject(["*"]) { |list, cmd|
\r
67 list << (list.length > 1 ? list.last + "::" : "") + cmd
\r
69 @path = seq.map { |k|
\r
72 @command = path.last
\r
73 debug "Created command #{@command.inspect} with path #{@path.join(', ')}"
\r
77 def to_irc_auth_command
\r
90 # Returns an Irc::Auth::Comand from the receiver
\r
91 def to_irc_auth_command
\r
92 Irc::Auth::Command.new(self)
\r
104 # This class describes a permission set
\r
105 class PermissionSet
\r
107 # Create a new (empty) PermissionSet
\r
113 # Inspection simply inspects the internal hash
\r
118 # Sets the permission for command _cmd_ to _val_,
\r
120 def set_permission(str, val)
\r
121 cmd = str.to_irc_auth_command
\r
124 @perm[cmd.command] = val
\r
126 @perm.delete(cmd.command)
\r
128 raise TypeError, "#{val.inspect} must be true or false" unless [true,false].include?(val)
\r
132 # Resets the permission for command _cmd_
\r
134 def reset_permission(cmd)
\r
135 set_permission(cmd, nil)
\r
138 # Tells if command _cmd_ is permitted. We do this by returning
\r
139 # the value of the deepest Command#path that matches.
\r
142 cmd = str.to_irc_auth_command
\r
144 cmd.path.reverse.each { |k|
\r
145 if @perm.has_key?(k)
\r
156 # This is the basic class for bot users: they have a username, a password, a
\r
157 # list of netmasks to match against, and a list of permissions.
\r
161 attr_reader :username
\r
162 attr_reader :password
\r
163 attr_reader :netmasks
\r
165 # Create a new BotUser with given username
\r
166 def initialize(username)
\r
167 @username = BotUser.sanitize_username(username)
\r
169 @netmasks = NetmaskList.new
\r
175 str = "<#{self.class}:#{'0x%08x' % self.object_id}:"
\r
176 str << " @username=#{@username.inspect}"
\r
177 str << " @netmasks=#{@netmasks.inspect}"
\r
178 str << " @perm=#{@perm.inspect}"
\r
182 # Convert into a hash
\r
185 :username => @username,
\r
186 :password => @password,
\r
187 :netmasks => @netmasks,
\r
192 # Restore from hash
\r
194 @username = h[:username] if h.has_key?(:username)
\r
195 @password = h[:password] if h.has_key?(:password)
\r
196 @netmasks = h[:netmasks] if h.has_key?(:netmasks)
\r
197 @perm = h[:perm] if h.has_key?(:perm)
\r
200 # This method sets the password if the proposed new password
\r
202 def password=(pwd=nil)
\r
205 raise InvalidPassword, "#{pwd} contains invalid characters" if pwd !~ /^[A-Za-z0-9]+$/
\r
206 raise InvalidPassword, "#{pwd} too short" if pwd.length < 4
\r
208 rescue InvalidPassword => e
\r
211 raise InvalidPassword, "Exception #{e.inspect} while checking #{pwd}"
\r
218 # Resets the password by creating a new onw
\r
220 @password = random_password
\r
223 # Sets the permission for command _cmd_ to _val_ on channel _chan_
\r
225 def set_permission(cmd, val, chan="*")
\r
226 k = chan.to_s.to_sym
\r
227 @perm[k] = PermissionSet.new unless @perm.has_key?(k)
\r
228 @perm[k].set_permission(cmd, val)
\r
231 # Resets the permission for command _cmd_ on channel _chan_
\r
233 def reset_permission(cmd, chan ="*")
\r
234 set_permission(cmd, nil, chan)
\r
237 # Checks if BotUser is allowed to do something on channel _chan_,
\r
238 # or on all channels if _chan_ is nil
\r
240 def permit?(cmd, chan=nil)
\r
242 k = chan.to_s.to_sym
\r
247 if @perm.has_key?(k)
\r
248 allow = @perm[k].permit?(cmd)
\r
255 def add_netmask(mask)
\r
256 @netmasks << mask.to_irc_netmask
\r
259 # Removes a Netmask
\r
261 def delete_netmask(mask)
\r
262 m = mask.to_irc_netmask
\r
263 @netmasks.delete(m)
\r
266 # Removes all <code>Netmask</code>s
\r
268 def reset_netmask_list
\r
269 @netmasks = NetmaskList.new
\r
272 # This method checks if BotUser has a Netmask that matches _user_
\r
275 user = usr.to_irc_user
\r
277 @netmasks.each { |n|
\r
278 if user.matches?(n)
\r
286 # This method gets called when User _user_ wants to log in.
\r
287 # It returns true or false depending on whether the password
\r
288 # is right. If it is, the Netmask of the user is added to the
\r
289 # list of acceptable Netmask unless it's already matched.
\r
290 def login(user, password)
\r
291 if password == @password
\r
292 add_netmask(user) unless knows?(user)
\r
293 debug "#{user} logged in as #{self.inspect}"
\r
300 # # This method gets called when User _user_ has logged out as this BotUser
\r
302 # delete_netmask(user) if knows?(user)
\r
305 # This method sanitizes a username by chomping, downcasing
\r
306 # and replacing any nonalphanumeric character with _
\r
308 def BotUser.sanitize_username(name)
\r
309 return name.to_s.chomp.downcase.gsub(/[^a-z0-9]/,"_")
\r
315 # This is the default BotUser: it's used for all users which haven't
\r
316 # identified with the bot
\r
318 class DefaultBotUserClass < BotUser
\r
320 private :login, :add_netmask, :delete_netmask
\r
326 @default_perm = PermissionSet.new
\r
329 # Sets the default permission for the default user (i.e. the ones
\r
330 # set by the BotModule writers) on all channels
\r
332 def set_default_permission(cmd, val)
\r
333 @default_perm.set_permission(Command.new(cmd), val)
\r
334 debug "Default permissions now:\n#{@default_perm.inspect}"
\r
337 # default knows everybody
\r
340 return true if user.to_irc_user
\r
343 # Resets the NetmaskList
\r
344 def reset_netmask_list
\r
346 add_netmask("*!*@*")
\r
349 # DefaultBotUser will check the default_perm after checking
\r
351 # or on all channels if _chan_ is nil
\r
353 def permit?(cmd, chan=nil)
\r
354 allow = super(cmd, chan)
\r
355 if allow.nil? && chan.nil?
\r
356 allow = @default_perm.permit?(cmd)
\r
363 # Returns the only instance of DefaultBotUserClass
\r
365 def Auth.defaultbotuser
\r
366 return DefaultBotUserClass.instance
\r
369 # This is the BotOwner: he can do everything
\r
371 class BotOwnerClass < BotUser
\r
379 def permit?(cmd, chan=nil)
\r
385 # Returns the only instance of BotOwnerClass
\r
388 return BotOwnerClass.instance
\r
392 # This is the AuthManagerClass singleton, used to manage User/BotUser connections and
\r
395 class AuthManagerClass
\r
399 attr_reader :everyone
\r
400 attr_reader :botowner
\r
402 # The instance manages two <code>Hash</code>es: one that maps
\r
403 # <code>Irc::User</code>s onto <code>BotUser</code>s, and the other that maps
\r
404 # usernames onto <code>BotUser</code>
\r
406 @everyone = Auth::defaultbotuser
\r
407 @botowner = Auth::botowner
\r
411 def bot_associate(bot)
\r
412 raise "Cannot associate with a new bot! Save first" if defined?(@has_changes) && @has_changes
\r
419 # This variable is set to true when there have been changes
\r
420 # to the botusers list, so that we know when to save
\r
421 @has_changes = false
\r
425 @has_changes = true
\r
429 @has_changes = false
\r
436 # resets the hashes
\r
438 @botusers = Hash.new
\r
439 @allbotusers = Hash.new
\r
440 [everyone, botowner].each { |x|
\r
441 @allbotusers[x.username.to_sym] = x
\r
445 def load_array(ary, forced)
\r
446 raise "Won't load with unsaved changes" if @has_changes and not forced
\r
449 raise TypeError, "#{x} should be a Hash" unless x.kind_of?(Hash)
\r
454 get_botuser(u).from_hash(x)
\r
460 @allbotusers.values.map { |x|
\r
465 # checks if we know about a certain BotUser username
\r
466 def include?(botusername)
\r
467 @allbotusers.has_key?(botusername.to_sym)
\r
470 # Maps <code>Irc::User</code> to BotUser
\r
471 def irc_to_botuser(ircuser)
\r
472 # TODO check netmasks
\r
473 @botusers[ircuser.to_irc_user] || everyone
\r
476 # creates a new BotUser
\r
477 def create_botuser(name, password=nil)
\r
478 n = BotUser.sanitize_username(name)
\r
480 raise "BotUser #{n} exists" if include?(k)
\r
481 bu = BotUser.new(n)
\r
482 bu.password = password
\r
483 @allbotusers[k] = bu
\r
486 # returns the botuser with name _name_
\r
487 def get_botuser(name)
\r
488 @allbotusers.fetch(BotUser.sanitize_username(name).to_sym)
\r
491 # Logs Irc::User _ircuser_ in to BotUser _botusername_ with password _pwd_
\r
493 # raises an error if _botusername_ is not a known BotUser username
\r
495 # It is possible to autologin by Netmask, on request
\r
497 def login(user, botusername, pwd, bymask = false)
\r
498 ircuser = user.to_irc_user
\r
499 n = BotUser.sanitize_username(botusername)
\r
501 raise "No such BotUser #{n}" unless include?(k)
\r
502 if @botusers.has_key?(ircuser)
\r
504 # @botusers[ircuser].logout(ircuser)
\r
506 bu = @allbotusers[k]
\r
507 if bymask && bu.knows?(ircuser)
\r
508 @botusers[ircuser] = bu
\r
510 elsif bu.login(ircuser, pwd)
\r
511 @botusers[ircuser] = bu
\r
517 # Checks if User _user_ can do _cmd_ on _chan_.
\r
519 # Permission are checked in this order, until a true or false
\r
521 # * associated BotUser on _chan_
\r
522 # * associated BotUser on all channels
\r
523 # * everyone on _chan_
\r
524 # * everyone on all channels
\r
526 def permit?(user, cmdtxt, channel=nil)
\r
527 botuser = irc_to_botuser(user)
\r
528 cmd = cmdtxt.to_irc_auth_command
\r
540 allow = botuser.permit?(cmd, chan) if chan
\r
541 return allow unless allow.nil?
\r
542 allow = botuser.permit?(cmd)
\r
543 return allow unless allow.nil?
\r
545 unless botuser == everyone
\r
546 allow = everyone.permit?(cmd, chan) if chan
\r
547 return allow unless allow.nil?
\r
548 allow = everyone.permit?(cmd)
\r
549 return allow unless allow.nil?
\r
552 raise "Could not check permission for user #{user.inspect} to run #{cmdtxt.inspect} on #{chan.inspect}"
\r
555 # Checks if command _cmd_ is allowed to User _user_ on _chan_
\r
556 def allow?(cmdtxt, user, chan=nil)
\r
557 permit?(user, cmdtxt, chan)
\r
562 # Returns the only instance of AuthManagerClass
\r
564 def Auth.authmanager
\r
565 return AuthManagerClass.instance
\r