summaryrefslogtreecommitdiff
path: root/lib/rbot/botuser.rb
diff options
context:
space:
mode:
authorGiuseppe Bilotta <giuseppe.bilotta@gmail.com>2006-08-02 16:26:29 +0000
committerGiuseppe Bilotta <giuseppe.bilotta@gmail.com>2006-08-02 16:26:29 +0000
commitc9b76ddbc6ada354e0c1f14a14fcd1cdd7c1230c (patch)
treeaa4a4a5a0648a5ec09d8c9a7036d79cb44d2d39d /lib/rbot/botuser.rb
parent7bef95455cfbec9cf77db283a11e177f4c981b6a (diff)
Auth now follows the specs defined in NewAuthModule even though there is no actual auth coremodule. config.rb needs to be split into a class definition file and a coremodule that manages it
Diffstat (limited to 'lib/rbot/botuser.rb')
-rw-r--r--lib/rbot/botuser.rb86
1 files changed, 69 insertions, 17 deletions
diff --git a/lib/rbot/botuser.rb b/lib/rbot/botuser.rb
index 98408a0d..6c84a93b 100644
--- a/lib/rbot/botuser.rb
+++ b/lib/rbot/botuser.rb
@@ -252,13 +252,29 @@ module Irc
@perm = {}
end
+ # Inspection simply inspects the internal hash
+ def inspect
+ @perm.inspect
+ end
+
# Sets the permission for command _cmd_ to _val_,
- # creating intermediate permissions if needed.
#
def set_permission(cmd, val)
- raise TypeError, "#{val.inspect} must be true or false" unless [true,false].include?(val)
Irc::error_if_not_command(cmd)
- @perm[cmd.command] = val
+ case val
+ when true, false
+ @perm[cmd.command] = val
+ when nil
+ @perm.delete(cmd.command)
+ else
+ raise TypeError, "#{val.inspect} must be true or false" unless [true,false].include?(val)
+ end
+ end
+
+ # Resets the permission for command _cmd_
+ #
+ def reset_permission(cmd)
+ set_permission(cmd, nil)
end
# Tells if command _cmd_ is permitted. We do this by returning
@@ -313,6 +329,12 @@ module Irc
end
end
+ # Resets the permission for command _cmd_ on channel _chan_
+ #
+ def reset_permission(cmd, chan ="*")
+ set_permission(cmd, nil, chan)
+ end
+
# Checks if BotUser is allowed to do something on channel _chan_,
# or on all channels if _chan_ is nil
#
@@ -415,17 +437,27 @@ module Irc
end
- # This is the anonymous BotUser: it's used for all users which haven't
+ # This is the default BotUser: it's used for all users which haven't
# identified with the bot
#
- class AnonBotUserClass < BotUser
+ class DefaultBotUserClass < BotUser
include Singleton
def initialize
- super("anonymous")
+ super("everyone")
+ @default_perm = PermissionSet.new
end
private :login, :add_netmask, :delete_netmask
- # Anon knows everybody
+ # Sets the default permission for the default user (i.e. the ones
+ # set by the BotModule writers) on all channels
+ #
+ def set_default_permission(cmd, val)
+ @default_perm.set_permission(Command.new(cmd), val)
+ debug "Default permissions now:\n#{@default_perm.inspect}"
+ end
+
+ # default knows everybody
+ #
def knows?(user)
Irc::error_if_not_user(user)
return true
@@ -436,12 +468,24 @@ module Irc
super
add_netmask("*!*@*")
end
+
+ # DefaultBotUser will check the default_perm after checking
+ # the global ones
+ # or on all channels if _chan_ is nil
+ #
+ def permit?(cmd, chan=nil)
+ allow = super(cmd, chan)
+ if allow.nil? && chan.nil?
+ allow = @default_perm.permit?(cmd)
+ end
+ return allow
+ end
end
- # Returns the only instance of AnonBotUserClass
+ # Returns the only instance of DefaultBotUserClass
#
- def Auth.anonbotuser
- return AnonBotUserClass.instance
+ def Auth.defaultbotuser
+ return DefaultBotUserClass.instance
end
# This is the BotOwner: he can do everything
@@ -470,10 +514,15 @@ module Irc
class AuthManagerClass
include Singleton
+ attr_reader :everyone
+ attr_reader :botowner
+
# The instance manages two <code>Hash</code>es: one that maps
# <code>Irc::User</code>s onto <code>BotUser</code>s, and the other that maps
# usernames onto <code>BotUser</code>
def initialize
+ @everyone = Auth::defaultbotuser
+ @botowner = Auth::botowner
bot_associate(nil)
end
@@ -494,7 +543,9 @@ module Irc
def reset_hashes
@botusers = Hash.new
@allbotusers = Hash.new
- [Auth::anonbotuser, Auth::botowner].each { |x| @allbotusers[x.username.to_sym] = x }
+ [everyone, botowner].each { |x|
+ @allbotusers[x.username.to_sym] = x
+ }
end
# load botlist from userfile
@@ -524,7 +575,8 @@ module Irc
# Maps <code>Irc::User</code> to BotUser
def irc_to_botuser(ircuser)
Irc::error_if_not_user(ircuser)
- return @botusers[ircuser] || Auth::anonbotuser
+ # TODO check netmasks
+ return @botusers[ircuser] || everyone
end
# creates a new BotUser
@@ -569,8 +621,8 @@ module Irc
# is returned:
# * associated BotUser on _chan_
# * associated BotUser on all channels
- # * anonbotuser on _chan_
- # * anonbotuser on all channels
+ # * everyone on _chan_
+ # * everyone on all channels
#
def permit?(user, cmdtxt, chan=nil)
botuser = irc_to_botuser(user)
@@ -590,10 +642,10 @@ module Irc
allow = botuser.permit?(cmd)
return allow unless allow.nil?
- unless botuser == Auth::anonbotuser
- allow = Auth::anonbotuser.permit?(cmd, chan) if chan
+ unless botuser == everyone
+ allow = everyone.permit?(cmd, chan) if chan
return allow unless allow.nil?
- allow = Auth::anonbotuser.permit?(cmd)
+ allow = everyone.permit?(cmd)
return allow unless allow.nil?
end