core/AdminCache.cpp
changeset 1403 ecb9c66fd4bb
parent 1356 4688fae59bcf
child 1406 f5c07ec3c052
     1.1 --- a/core/AdminCache.cpp	Mon Sep 10 22:28:22 2007 +0000
     1.2 +++ b/core/AdminCache.cpp	Mon Sep 10 23:38:58 2007 +0000
     1.3 @@ -40,6 +40,7 @@
     1.4  #include "Logger.h"
     1.5  #include "sourcemod.h"
     1.6  #include "sm_stringutil.h"
     1.7 +#include "sourcemm_api.h"
     1.8  
     1.9  #define LEVEL_STATE_NONE		0
    1.10  #define LEVEL_STATE_LEVELS		1
    1.11 @@ -49,6 +50,8 @@
    1.12  AdminFlag g_FlagLetters[26];
    1.13  bool g_FlagSet[26];
    1.14  
    1.15 +ConVar sm_immunity_mode("sm_immunity_mode", "1", FCVAR_SPONLY|FCVAR_PROTECTED, "Mode for deciding immunity protection");
    1.16 +
    1.17  /* Default flags */
    1.18  AdminFlag g_DefaultFlags[26] = 
    1.19  {
    1.20 @@ -443,8 +446,7 @@
    1.21  	pUser->magic = USR_MAGIC_SET;
    1.22  	pUser->auth.identidx = -1;
    1.23  	pUser->auth.index = 0;
    1.24 -	pUser->immune_default = false;
    1.25 -	pUser->immune_global = false;
    1.26 +	pUser->immunity_level = 0;
    1.27  	pUser->serialchange = 1;
    1.28  
    1.29  	if (m_FirstUser == INVALID_ADMIN_ID)
    1.30 @@ -487,8 +489,7 @@
    1.31  		id = m_pMemory->CreateMem(sizeof(AdminGroup), (void **)&pGroup);
    1.32  	}
    1.33  
    1.34 -	pGroup->immune_default = false;
    1.35 -	pGroup->immune_global = false;
    1.36 +	pGroup->immunity_level = 0;
    1.37  	pGroup->immune_table = -1;
    1.38  	pGroup->magic = GRP_MAGIC_SET;
    1.39  	pGroup->next_grp = INVALID_GROUP_ID;
    1.40 @@ -594,7 +595,7 @@
    1.41  	AdminGroup *pGroup = (AdminGroup *)m_pMemory->GetAddress(gid);
    1.42  	if (!pGroup || pGroup->magic != GRP_MAGIC_SET)
    1.43  	{
    1.44 -		return 0;
    1.45 +		return NULL;
    1.46  	}
    1.47  
    1.48  	return m_pStrings->GetString(pGroup->nameidx);
    1.49 @@ -608,12 +609,23 @@
    1.50  		return;
    1.51  	}
    1.52  
    1.53 -	if (type == Immunity_Default)
    1.54 +	unsigned int level = 0;
    1.55 +
    1.56 +	if (enabled)
    1.57  	{
    1.58 -		pGroup->immune_default = enabled;
    1.59 -	} else if (type == Immunity_Global) {
    1.60 -		pGroup->immune_global = enabled;
    1.61 -	} 
    1.62 +		if (type == Immunity_Default)
    1.63 +		{
    1.64 +			level = 1;
    1.65 +		} else if (type == Immunity_Global) {
    1.66 +			level = 2;
    1.67 +		}
    1.68 +		if (level > pGroup->immunity_level)
    1.69 +		{
    1.70 +			pGroup->immunity_level = level;
    1.71 +		}
    1.72 +	} else {
    1.73 +		pGroup->immunity_level = 0;
    1.74 +	}
    1.75  }
    1.76  
    1.77  bool AdminCache::GetGroupGenericImmunity(GroupId id, ImmunityType type)
    1.78 @@ -624,11 +636,11 @@
    1.79  		return false;
    1.80  	}
    1.81  
    1.82 -	if (type == Immunity_Default)
    1.83 +	if (type == Immunity_Default && pGroup->immunity_level >= 1)
    1.84  	{
    1.85 -		return pGroup->immune_default;
    1.86 -	} else if (type == Immunity_Global) {
    1.87 -		return pGroup->immune_global;
    1.88 +		return true;
    1.89 +	} else if (type == Immunity_Global && pGroup->immunity_level >= 2) {
    1.90 +		return true;
    1.91  	}
    1.92  
    1.93  	return false;
    1.94 @@ -1333,13 +1345,9 @@
    1.95  	/* Compute new effective permissions */
    1.96  	pUser->eflags |= pGroup->addflags;
    1.97  
    1.98 -	if (pGroup->immune_default)
    1.99 +	if (pGroup->immunity_level > pUser->immunity_level)
   1.100  	{
   1.101 -		pUser->immune_default = true;
   1.102 -	}
   1.103 -	if (pGroup->immune_global)
   1.104 -	{
   1.105 -		pUser->immune_global = true;
   1.106 +		pUser->immunity_level = pGroup->immunity_level;
   1.107  	}
   1.108  
   1.109  	pUser->serialchange++;
   1.110 @@ -1486,6 +1494,11 @@
   1.111  		return true;
   1.112  	}
   1.113  
   1.114 +	if (id == target)
   1.115 +	{
   1.116 +		return true;
   1.117 +	}
   1.118 +
   1.119  	AdminUser *pUser = (AdminUser *)m_pMemory->GetAddress(id);
   1.120  	if (!pUser || pUser->magic != USR_MAGIC_SET)
   1.121  	{
   1.122 @@ -1506,23 +1519,39 @@
   1.123  		return true;
   1.124  	}
   1.125  
   1.126 -	/** Fourth, if the targeted admin has global immunity, targeting fails. */
   1.127 -	if (pTarget->immune_global)
   1.128 +	/** Fourth, if the targeted admin is immune from targeting admin. */
   1.129 +	int mode = sm_immunity_mode.GetInt();
   1.130 +	switch (mode)
   1.131  	{
   1.132 -		return false;
   1.133 +		case 1:
   1.134 +		{
   1.135 +			if (pTarget->immunity_level > pUser->immunity_level)
   1.136 +			{
   1.137 +				return false;
   1.138 +			}
   1.139 +			break;
   1.140 +		}
   1.141 +		case 3:
   1.142 +		{
   1.143 +			/* If neither has any immunity, let this pass. */
   1.144 +			if (!pUser->immunity_level && !pTarget->immunity_level)
   1.145 +			{
   1.146 +				return true;
   1.147 +			}
   1.148 +			/* Don't break, go to the next case. */
   1.149 +		}
   1.150 +		case 2:
   1.151 +		{
   1.152 +			if (pTarget->immunity_level >= pUser->immunity_level)
   1.153 +			{
   1.154 +				return false;
   1.155 +			}
   1.156 +			break;
   1.157 +		}
   1.158  	}
   1.159  
   1.160  	/** 
   1.161 -	 * Fifth, if the targeted admin has default immunity 
   1.162 -	 *  and the admin belongs to no groups, targeting fails.
   1.163 -	 */
   1.164 -	if (pTarget->immune_default && pUser->grp_count < 1)
   1.165 -	{
   1.166 -		return false;
   1.167 -	}
   1.168 -
   1.169 -	/** 
   1.170 -	 * Sixth, if the targeted admin has specific immunity from the
   1.171 +	 * Fifth, if the targeted admin has specific immunity from the
   1.172  	 *  targeting admin via group immunities, targeting fails.
   1.173  	 */
   1.174  	//:TODO: speed this up... maybe with trie hacks.
   1.175 @@ -1625,3 +1654,56 @@
   1.176  
   1.177  	return g_ConCmds.CheckCommandAccess(client, cmd, bits);
   1.178  }
   1.179 +
   1.180 +unsigned int AdminCache::SetGroupImmunityLevel(GroupId gid, unsigned int level)
   1.181 +{
   1.182 +	AdminGroup *pGroup = (AdminGroup *)m_pMemory->GetAddress(gid);
   1.183 +	if (!pGroup || pGroup->magic != GRP_MAGIC_SET)
   1.184 +	{
   1.185 +		return 0;
   1.186 +	}
   1.187 +
   1.188 +	unsigned int old_level = pGroup->immunity_level;
   1.189 +
   1.190 +	pGroup->immunity_level = level;
   1.191 +
   1.192 +	return old_level;
   1.193 +}
   1.194 +
   1.195 +unsigned int AdminCache::GetGroupImmunityLevel(GroupId gid)
   1.196 +{
   1.197 +	AdminGroup *pGroup = (AdminGroup *)m_pMemory->GetAddress(gid);
   1.198 +	if (!pGroup || pGroup->magic != GRP_MAGIC_SET)
   1.199 +	{
   1.200 +		return 0;
   1.201 +	}
   1.202 +
   1.203 +	return pGroup->immunity_level;
   1.204 +}
   1.205 +
   1.206 +unsigned int AdminCache::SetAdminImmunityLevel(AdminId id, unsigned int level)
   1.207 +{
   1.208 +	AdminUser *pUser = (AdminUser *)m_pMemory->GetAddress(id);
   1.209 +	if (!pUser || pUser->magic != USR_MAGIC_SET)
   1.210 +	{
   1.211 +		return 0;
   1.212 +	}
   1.213 +
   1.214 +	unsigned int old_level = pUser->immunity_level;
   1.215 +
   1.216 +	pUser->immunity_level = level;
   1.217 +
   1.218 +	return old_level;
   1.219 +}
   1.220 +
   1.221 +unsigned int AdminCache::GetAdminImmunityLevel(AdminId id)
   1.222 +{
   1.223 +	AdminUser *pUser = (AdminUser *)m_pMemory->GetAddress(id);
   1.224 +	if (!pUser || pUser->magic != USR_MAGIC_SET)
   1.225 +	{
   1.226 +		return 0;
   1.227 +	}
   1.228 +
   1.229 +	return pUser->immunity_level;
   1.230 +}
   1.231 +