core/AdminCache.cpp
author David Anderson <dvander@alliedmods.net>
Mon Mar 12 00:06:20 2007 +0000 (2007-03-12)
changeset 599 3a098eff4890
parent 591 86e97f3eda1f
child 653 ef60870067ff
permissions -rw-r--r--
used a better error number
     1 /**
     2  * ===============================================================
     3  * SourceMod (C)2004-2007 AlliedModders LLC.  All rights reserved.
     4  * ===============================================================
     5  *
     6  * This file is not open source and may not be copied without explicit
     7  * written permission of AlliedModders LLC.  This file may not be redistributed 
     8  * in whole or significant part.
     9  * For information, see LICENSE.txt or http://www.sourcemod.net/license.php
    10  *
    11  * Version: $Id$
    12  */
    13 
    14 #include <string.h>
    15 #include <assert.h>
    16 #include "AdminCache.h"
    17 #include "ShareSys.h"
    18 #include "ForwardSys.h"
    19 #include "PlayerManager.h"
    20 #include "ConCmdManager.h"
    21 
    22 AdminCache g_Admins;
    23 
    24 AdminCache::AdminCache()
    25 {
    26 	m_pCmdOverrides = sm_trie_create();
    27 	m_pCmdGrpOverrides = sm_trie_create();
    28 	m_pStrings = new BaseStringTable(1024);
    29 	m_pMemory = m_pStrings->GetMemTable();
    30 	m_FreeGroupList = m_FirstGroup = m_LastGroup = INVALID_GROUP_ID;
    31 	m_FreeUserList = m_FirstUser = m_LastUser = INVALID_ADMIN_ID;
    32 	m_pGroups = sm_trie_create();
    33 	m_pCacheFwd = NULL;
    34 	m_FirstGroup = -1;
    35 	m_pAuthTables = sm_trie_create();
    36 	m_InvalidatingAdmins = false;
    37 	m_destroying = false;
    38 }
    39 
    40 AdminCache::~AdminCache()
    41 {
    42 	m_destroying = true;
    43 	DumpAdminCache(AdminCache_Overrides, false);
    44 	DumpAdminCache(AdminCache_Groups, false);
    45 
    46 	sm_trie_destroy(m_pCmdGrpOverrides);
    47 	sm_trie_destroy(m_pCmdOverrides);
    48 
    49 	if (m_pGroups)
    50 	{
    51 		sm_trie_destroy(m_pGroups);
    52 	}
    53 
    54 	List<AuthMethod>::iterator iter;
    55 	for (iter=m_AuthMethods.begin();
    56 		 iter!=m_AuthMethods.end();
    57 		 iter++)
    58 	{
    59 		sm_trie_destroy((*iter).table);
    60 	}
    61 
    62 	sm_trie_destroy(m_pAuthTables);
    63 
    64 	delete m_pStrings;
    65 }
    66 
    67 void AdminCache::OnSourceModStartup(bool late)
    68 {
    69 	RegisterAuthIdentType("steam");
    70 	RegisterAuthIdentType("name");
    71 	RegisterAuthIdentType("ip");
    72 }
    73 
    74 void AdminCache::OnSourceModAllInitialized()
    75 {
    76 	m_pCacheFwd = g_Forwards.CreateForward("OnRebuildAdminCache", ET_Ignore, 1, NULL, Param_Cell);
    77 	g_ShareSys.AddInterface(NULL, this);
    78 }
    79 
    80 void AdminCache::OnSourceModShutdown()
    81 {
    82 	g_Forwards.ReleaseForward(m_pCacheFwd);
    83 	m_pCacheFwd = NULL;
    84 }
    85 
    86 void AdminCache::AddCommandOverride(const char *cmd, OverrideType type, FlagBits flags)
    87 {
    88 	Trie *pTrie = NULL;
    89 	if (type == Override_Command)
    90 	{
    91 		pTrie = m_pCmdOverrides;
    92 	} else if (type == Override_CommandGroup) {
    93 		pTrie = m_pCmdGrpOverrides;
    94 	} else {
    95 		return;
    96 	}
    97 
    98 	sm_trie_insert(pTrie, cmd, (void *)(unsigned int)flags);
    99 
   100 	g_ConCmds.UpdateAdminCmdFlags(cmd, type, flags);
   101 }
   102 
   103 bool AdminCache::GetCommandOverride(const char *cmd, OverrideType type, FlagBits *pFlags)
   104 {
   105 	Trie *pTrie = NULL;
   106 
   107 	if (type == Override_Command)
   108 	{
   109 		pTrie = m_pCmdOverrides;
   110 	} else if (type == Override_CommandGroup) {
   111 		pTrie = m_pCmdGrpOverrides;
   112 	} else {
   113 		return false;
   114 	}
   115 
   116 	void *object;
   117 	if (sm_trie_retrieve(pTrie, cmd, &object))
   118 	{
   119 		if (pFlags)
   120 		{
   121 			*pFlags = (FlagBits)object;
   122 		}
   123 		return true;
   124 	}
   125 
   126 	return false;
   127 }
   128 
   129 void AdminCache::UnsetCommandOverride(const char *cmd, OverrideType type)
   130 {
   131 	if (type == Override_Command)
   132 	{
   133 		return _UnsetCommandOverride(cmd);
   134 	} else if (type == Override_CommandGroup) {
   135 		return _UnsetCommandGroupOverride(cmd);
   136 	}
   137 }
   138 
   139 void AdminCache::_UnsetCommandGroupOverride(const char *group)
   140 {
   141 	if (!m_pCmdGrpOverrides)
   142 	{
   143 		return;
   144 	}
   145 
   146 	sm_trie_delete(m_pCmdGrpOverrides, group);
   147 
   148 	g_ConCmds.UpdateAdminCmdFlags(group, Override_CommandGroup, 0);
   149 }
   150 
   151 void AdminCache::_UnsetCommandOverride(const char *cmd)
   152 {
   153 	if (!m_pCmdOverrides)
   154 	{
   155 		return;
   156 	}
   157 
   158 	sm_trie_delete(m_pCmdOverrides, cmd);
   159 
   160 	g_ConCmds.UpdateAdminCmdFlags(cmd, Override_Command, 0);
   161 }
   162 
   163 void AdminCache::DumpCommandOverrideCache(OverrideType type)
   164 {
   165 	if (type == Override_Command && m_pCmdOverrides)
   166 	{
   167 		sm_trie_clear(m_pCmdOverrides);
   168 	} else if (type == Override_CommandGroup && m_pCmdGrpOverrides) {
   169 		sm_trie_clear(m_pCmdGrpOverrides);
   170 	}
   171 }
   172 
   173 AdminId AdminCache::CreateAdmin(const char *name)
   174 {
   175 	AdminId id;
   176 	AdminUser *pUser;
   177 
   178 	if (m_FreeUserList != INVALID_ADMIN_ID)
   179 	{
   180 		pUser = (AdminUser *)m_pMemory->GetAddress(m_FreeUserList);
   181 		assert(pUser->magic == USR_MAGIC_UNSET);
   182 		id = m_FreeUserList;
   183 		m_FreeUserList = pUser->next_user;
   184 	} else {
   185 		id = m_pMemory->CreateMem(sizeof(AdminUser), (void **)&pUser);
   186 		pUser->grp_size = 0;
   187 		pUser->grp_table = -1;
   188 	}
   189 
   190 	pUser->flags = 0;
   191 	pUser->eflags = 0;
   192 	pUser->grp_count = 0;
   193 	pUser->password = -1;
   194 	pUser->magic = USR_MAGIC_SET;
   195 	pUser->auth.identidx = -1;
   196 	pUser->auth.index = 0;
   197 	pUser->immune_default = false;
   198 	pUser->immune_global = false;
   199 
   200 	if (m_FirstUser == INVALID_ADMIN_ID)
   201 	{
   202 		m_FirstUser = id;
   203 		m_LastUser = id;
   204 	} else {
   205 		AdminUser *pPrev = (AdminUser *)m_pMemory->GetAddress(m_LastUser);
   206 		pPrev->next_user = id;
   207 		pUser->prev_user = m_LastUser;
   208 		m_LastUser = id;
   209 	}
   210 
   211 	if (name && name[0] != '\0')
   212 	{
   213 		pUser->nameidx = m_pStrings->AddString(name);
   214 	}
   215 
   216 	return id;
   217 }
   218 
   219 GroupId AdminCache::AddGroup(const char *group_name)
   220 {
   221 	if (sm_trie_retrieve(m_pGroups, group_name, NULL))
   222 	{
   223 		return INVALID_GROUP_ID;
   224 	}
   225 
   226 	GroupId id;
   227 	AdminGroup *pGroup;
   228 	if (m_FreeGroupList != INVALID_GROUP_ID)
   229 	{
   230 		pGroup = (AdminGroup *)m_pMemory->GetAddress(m_FreeGroupList);
   231 		assert(pGroup->magic == GRP_MAGIC_UNSET);
   232 		id = m_FreeGroupList;
   233 		m_FreeGroupList = pGroup->next_grp;
   234 	} else {
   235 		id = m_pMemory->CreateMem(sizeof(AdminGroup), (void **)&pGroup);
   236 	}
   237 
   238 	pGroup->immune_default = false;
   239 	pGroup->immune_global = false;
   240 	pGroup->immune_table = -1;
   241 	pGroup->magic = GRP_MAGIC_SET;
   242 	pGroup->next_grp = INVALID_GROUP_ID;
   243 	pGroup->pCmdGrpTable = NULL;
   244 	pGroup->pCmdTable = NULL;
   245 	pGroup->addflags = 0;
   246 
   247 	if (m_FirstGroup == INVALID_GROUP_ID)
   248 	{
   249 		m_FirstGroup = id;
   250 		m_LastGroup = id;
   251 		pGroup->prev_grp = INVALID_GROUP_ID;
   252 	} else {
   253 		AdminGroup *pPrev = (AdminGroup *)m_pMemory->GetAddress(m_LastGroup);
   254 		assert(pPrev->magic == GRP_MAGIC_SET);
   255 		pPrev->next_grp = id;
   256 		pGroup->prev_grp = m_LastGroup;
   257 		m_LastGroup = id;
   258 	}
   259 
   260 	pGroup->nameidx = m_pStrings->AddString(group_name);
   261 
   262 	sm_trie_insert(m_pGroups, group_name, (void *)id);
   263 
   264 	return id;
   265 }
   266 
   267 GroupId AdminCache::FindGroupByName(const char *group_name)
   268 {
   269 	void *object;
   270 
   271 	if (!sm_trie_retrieve(m_pGroups, group_name, &object))
   272 	{
   273 		return INVALID_GROUP_ID;
   274 	}
   275 
   276 	GroupId id = (GroupId)object;
   277 	AdminGroup *pGroup = (AdminGroup *)m_pMemory->GetAddress(id);
   278 
   279 	if (!pGroup || pGroup->magic != GRP_MAGIC_SET)
   280 	{
   281 		return INVALID_GROUP_ID;
   282 	}
   283 
   284 	return id;
   285 }
   286 
   287 void AdminCache::SetGroupAddFlag(GroupId id, AdminFlag flag, bool enabled)
   288 {
   289 	AdminGroup *pGroup = (AdminGroup *)m_pMemory->GetAddress(id);
   290 	if (!pGroup || pGroup->magic != GRP_MAGIC_SET)
   291 	{
   292 		return;
   293 	}
   294 
   295 	if (flag < Admin_Reservation || flag >= AdminFlags_TOTAL)
   296 	{
   297 		return;
   298 	}
   299 
   300 	FlagBits bits = (1<<(FlagBits)flag);
   301 
   302 	if (enabled)
   303 	{
   304 		pGroup->addflags |= bits;
   305 	} else {
   306 		pGroup->addflags &= ~bits;
   307 	}
   308 }
   309 
   310 bool AdminCache::GetGroupAddFlag(GroupId id, AdminFlag flag)
   311 {
   312 	AdminGroup *pGroup = (AdminGroup *)m_pMemory->GetAddress(id);
   313 	if (!pGroup || pGroup->magic != GRP_MAGIC_SET)
   314 	{
   315 		return false;
   316 	}
   317 
   318 	if (flag < Admin_Reservation || flag >= AdminFlags_TOTAL)
   319 	{
   320 		return false;
   321 	}
   322 
   323 	FlagBits bit = 1<<(FlagBits)flag;
   324 	return ((pGroup->addflags & bit) == bit);
   325 }
   326 
   327 FlagBits AdminCache::GetGroupAddFlags(GroupId id)
   328 {
   329 	AdminGroup *pGroup = (AdminGroup *)m_pMemory->GetAddress(id);
   330 	if (!pGroup || pGroup->magic != GRP_MAGIC_SET)
   331 	{
   332 		return 0;
   333 	}
   334 
   335 	return pGroup->addflags;
   336 }
   337 
   338 void AdminCache::SetGroupGenericImmunity(GroupId id, ImmunityType type, bool enabled)
   339 {
   340 	AdminGroup *pGroup = (AdminGroup *)m_pMemory->GetAddress(id);
   341 	if (!pGroup || pGroup->magic != GRP_MAGIC_SET)
   342 	{
   343 		return;
   344 	}
   345 
   346 	if (type == Immunity_Default)
   347 	{
   348 		pGroup->immune_default = enabled;
   349 	} else if (type == Immunity_Global) {
   350 		pGroup->immune_global = enabled;
   351 	} 
   352 }
   353 
   354 bool AdminCache::GetGroupGenericImmunity(GroupId id, ImmunityType type)
   355 {
   356 	AdminGroup *pGroup = (AdminGroup *)m_pMemory->GetAddress(id);
   357 	if (!pGroup || pGroup->magic != GRP_MAGIC_SET)
   358 	{
   359 		return false;
   360 	}
   361 
   362 	if (type == Immunity_Default)
   363 	{
   364 		return pGroup->immune_default;
   365 	} else if (type == Immunity_Global) {
   366 		return pGroup->immune_global;
   367 	}
   368 
   369 	return false;
   370 }
   371 
   372 void AdminCache::AddGroupImmunity(GroupId id, GroupId other_id)
   373 {
   374 	AdminGroup *pGroup = (AdminGroup *)m_pMemory->GetAddress(id);
   375 	if (!pGroup || pGroup->magic != GRP_MAGIC_SET)
   376 	{
   377 		return;
   378 	}
   379 
   380 	AdminGroup *pOther = (AdminGroup *)m_pMemory->GetAddress(id);
   381 	if (!pOther || pOther->magic != GRP_MAGIC_SET)
   382 	{
   383 		return;
   384 	}
   385 
   386 	/* We always need to resize the immunity table */
   387 	int *table, tblidx;
   388 	if (pOther->immune_table == -1)
   389 	{
   390 		tblidx = m_pMemory->CreateMem(sizeof(int) * 2, (void **)&table);
   391 		table[0] = 0;
   392 	} else {
   393 		int *old_table = (int *)m_pMemory->GetAddress(pOther->immune_table);
   394 		/* Break out if this group is already in the list */
   395 		for (int i=0; i<old_table[0]; i++)
   396 		{
   397 			if (old_table[1+i] == other_id)
   398 			{
   399 				return;
   400 			}
   401 		}
   402 		tblidx = m_pMemory->CreateMem(sizeof(int) * (old_table[0] + 2), (void **)&table);
   403 		/* Get the old address again in case of resize */
   404 		old_table = (int *)m_pMemory->GetAddress(pOther->immune_table);
   405 		table[0] = old_table[0];
   406 		for (unsigned int i=1; i<=(unsigned int)old_table[0]; i++)
   407 		{
   408 			table[i] = old_table[i];
   409 		}
   410 	}
   411 
   412 	/* Assign */
   413 	pOther->immune_table = tblidx;
   414 
   415 	/* Add to the array */
   416 	table[0]++;
   417 	table[table[0]] = other_id;
   418 }
   419 
   420 unsigned int AdminCache::GetGroupImmunityCount(GroupId id)
   421 {
   422 	AdminGroup *pGroup = (AdminGroup *)m_pMemory->GetAddress(id);
   423 	if (!pGroup || pGroup->magic != GRP_MAGIC_SET)
   424 	{
   425 		return 0;
   426 	}
   427 
   428 	if (pGroup->immune_table == -1)
   429 	{
   430 		return 0;
   431 	}
   432 
   433 	int *table = (int *)m_pMemory->GetAddress(pGroup->immune_table);
   434 
   435 	return table[0];
   436 }
   437 
   438 GroupId AdminCache::GetGroupImmunity(GroupId id, unsigned int number)
   439 {
   440 	AdminGroup *pGroup = (AdminGroup *)m_pMemory->GetAddress(id);
   441 	if (!pGroup || pGroup->magic != GRP_MAGIC_SET)
   442 	{
   443 		return INVALID_GROUP_ID;
   444 	}
   445 
   446 	if (pGroup->immune_table == -1)
   447 	{
   448 		return INVALID_GROUP_ID;
   449 	}
   450 
   451 	int *table = (int *)m_pMemory->GetAddress(pGroup->immune_table);
   452 	if (number >= (unsigned int)table[0])
   453 	{
   454 		return INVALID_GROUP_ID;
   455 	}
   456 
   457 	return table[1 + number];
   458 }
   459 
   460 void AdminCache::AddGroupCommandOverride(GroupId id, const char *name, OverrideType type, OverrideRule rule)
   461 {
   462 	AdminGroup *pGroup = (AdminGroup *)m_pMemory->GetAddress(id);
   463 	if (!pGroup || pGroup->magic != GRP_MAGIC_SET)
   464 	{
   465 		return;
   466 	}
   467 
   468 	Trie *pTrie = NULL;
   469 	if (type == Override_Command)
   470 	{
   471 		if (pGroup->pCmdTable == NULL)
   472 		{
   473 			pGroup->pCmdTable = sm_trie_create();
   474 		}
   475 		pTrie = pGroup->pCmdTable;
   476 	} else if (type == Override_CommandGroup) {
   477 		if (pGroup->pCmdGrpTable == NULL)
   478 		{
   479 			pGroup->pCmdGrpTable = sm_trie_create();
   480 		}
   481 		pTrie = pGroup->pCmdGrpTable;
   482 	} else {
   483 		return;
   484 	}
   485 
   486 	sm_trie_insert(pTrie, name, (void *)(int)rule);
   487 }
   488 
   489 bool AdminCache::GetGroupCommandOverride(GroupId id, const char *name, OverrideType type, OverrideRule *pRule)
   490 {
   491 	AdminGroup *pGroup = (AdminGroup *)m_pMemory->GetAddress(id);
   492 	if (!pGroup || pGroup->magic != GRP_MAGIC_SET)
   493 	{
   494 		return false;
   495 	}
   496 
   497 	Trie *pTrie = NULL;
   498 	if (type == Override_Command)
   499 	{
   500 		if (pGroup->pCmdTable == NULL)
   501 		{
   502 			return false;
   503 		}
   504 		pTrie = pGroup->pCmdTable;
   505 	} else if (type == Override_CommandGroup) {
   506 		if (pGroup->pCmdGrpTable == NULL)
   507 		{
   508 			return false;
   509 		}
   510 		pTrie = pGroup->pCmdGrpTable;
   511 	} else {
   512 		return false;
   513 	}
   514 
   515 	void *object;
   516 	if (!sm_trie_retrieve(pTrie, name, &object))
   517 	{
   518 		return false;
   519 	}
   520 
   521 	if (pRule)
   522 	{
   523 		*pRule = (OverrideRule)(int)object;
   524 	}
   525 	
   526 	return true;
   527 }
   528 
   529 Trie *AdminCache::GetMethodByIndex(unsigned int index)
   530 {
   531 	List<AuthMethod>::iterator iter;
   532 	for (iter=m_AuthMethods.begin();
   533 		 iter!=m_AuthMethods.end();
   534 		 iter++)
   535 	{
   536 		if (index-- == 0)
   537 		{
   538 			return (*iter).table;
   539 		}
   540 	}
   541 
   542 	return NULL;
   543 }
   544 
   545 bool AdminCache::InvalidateAdmin(AdminId id)
   546 {
   547 	AdminUser *pUser = (AdminUser *)m_pMemory->GetAddress(id);
   548 	AdminUser *pOther;
   549 	if (!pUser || pUser->magic != USR_MAGIC_SET)
   550 	{
   551 		return false;
   552 	}
   553 
   554 	if (!m_InvalidatingAdmins && !m_destroying)
   555 	{
   556 		g_Players.ClearAdminId(id);
   557 	}
   558 
   559 	/* Unlink from the dbl link list */
   560 	if (id == m_FirstUser && id == m_LastUser)
   561 	{
   562 		m_FirstUser = INVALID_ADMIN_ID;
   563 		m_LastUser = INVALID_ADMIN_ID;
   564 	} else if (id == m_FirstUser) {
   565 		m_FirstUser = pUser->next_user;
   566 		pOther = (AdminUser *)m_pMemory->GetAddress(m_FirstUser);
   567 		pOther->prev_user = INVALID_ADMIN_ID;
   568 	} else if (id == m_LastUser) {
   569 		m_LastUser = pUser->prev_user;
   570 		pOther = (AdminUser *)m_pMemory->GetAddress(m_LastUser);
   571 		pOther->next_user = INVALID_ADMIN_ID;
   572 	} else {
   573 		pOther = (AdminUser *)m_pMemory->GetAddress(pUser->prev_user);
   574 		pOther->next_user = pUser->next_user;
   575 		pOther = (AdminUser *)m_pMemory->GetAddress(pUser->next_user);
   576 		pOther->prev_user = pUser->prev_user;
   577 	}
   578 
   579 	/* Unlink from auth tables */
   580 	if (pUser->auth.identidx != -1)
   581 	{
   582 		Trie *pTrie = GetMethodByIndex(pUser->auth.index);
   583 		if (pTrie)
   584 		{
   585 			sm_trie_delete(pTrie, m_pStrings->GetString(pUser->auth.identidx));
   586 		}
   587 	}
   588 
   589 	/* Clear table counts */
   590 	pUser->grp_count = 0;
   591 	
   592 	/* Link into free list */
   593 	pUser->magic = USR_MAGIC_UNSET;
   594 	pUser->next_user = m_FreeUserList;
   595 	m_FreeUserList = id;
   596 
   597 	return true;
   598 }
   599 
   600 
   601 void AdminCache::InvalidateGroup(GroupId id)
   602 {
   603 	AdminGroup *pGroup = (AdminGroup *)m_pMemory->GetAddress(id);
   604 	AdminGroup *pOther;
   605 
   606 	if (!pGroup || pGroup->magic != GRP_MAGIC_SET)
   607 	{
   608 		return;
   609 	}
   610 
   611 	const char *str = m_pStrings->GetString(pGroup->nameidx);
   612 	sm_trie_delete(m_pGroups, str);
   613 
   614 	/* Unlink from the live dbllink list */
   615 	if (id == m_FirstGroup && id == m_LastGroup)
   616 	{
   617 		m_LastGroup = INVALID_GROUP_ID;
   618 		m_FirstGroup = INVALID_GROUP_ID;
   619 	} else if (id == m_FirstGroup) {
   620 		m_FirstGroup = pGroup->next_grp;
   621 		pOther = (AdminGroup *)m_pMemory->GetAddress(m_FirstGroup);
   622 		pOther->prev_grp = INVALID_GROUP_ID;
   623 	} else if (id == m_LastGroup) {
   624 		m_LastGroup = pGroup->prev_grp;
   625 		pOther = (AdminGroup *)m_pMemory->GetAddress(m_LastGroup);
   626 		pOther->next_grp = INVALID_GROUP_ID;
   627 	} else {
   628 		pOther = (AdminGroup *)m_pMemory->GetAddress(pGroup->prev_grp);
   629 		pOther->next_grp = pGroup->next_grp;
   630 		pOther = (AdminGroup *)m_pMemory->GetAddress(pGroup->next_grp);
   631 		pOther->prev_grp = pGroup->prev_grp;
   632 	}
   633 
   634 	/* Free any used memory to be safe */
   635 	if (pGroup->pCmdGrpTable)
   636 	{
   637 		sm_trie_destroy(pGroup->pCmdGrpTable);
   638 		pGroup->pCmdGrpTable = NULL;
   639 	}
   640 	if (pGroup->pCmdTable)
   641 	{
   642 		sm_trie_destroy(pGroup->pCmdTable);
   643 		pGroup->pCmdTable = NULL;
   644 	}
   645 
   646 	/* Link into the free list */
   647 	pGroup->magic = GRP_MAGIC_UNSET;
   648 	pGroup->next_grp = m_FreeGroupList;
   649 	m_FreeGroupList = id;
   650 
   651 	int idx = m_FirstUser;
   652 	AdminUser *pUser;
   653 	int *table;
   654 	while (idx != INVALID_ADMIN_ID)
   655 	{
   656 		pUser = (AdminUser *)m_pMemory->GetAddress(idx);
   657 		if (pUser->grp_count > 0)
   658 		{
   659 			table = (int *)m_pMemory->GetAddress(pUser->grp_table);
   660 			for (unsigned int i=0; i<pUser->grp_count; i++)
   661 			{
   662 				if (table[i] == id)
   663 				{
   664 					/* We have to remove this entry */
   665 					for (unsigned int j=i+1; j<pUser->grp_count; j++)
   666 					{
   667 						/* Move everything down by one */
   668 						table[j-1] = table[j];
   669 					}
   670 					/* Decrease count */
   671 					pUser->grp_count--;
   672 					/* Recalculate effective flags */
   673 					pUser->eflags = pUser->flags;
   674 					for (unsigned int j=0; j<pUser->grp_count; j++)
   675 					{
   676 						pOther = (AdminGroup *)m_pMemory->GetAddress(table[j]);
   677 						pUser->eflags |= pOther->addflags;
   678 					}
   679 					/* Break now, duplicates aren't allowed */
   680 					break;
   681 				}
   682 			}
   683 		}
   684 		idx = pUser->next_user;
   685 	}
   686 }
   687 
   688 void AdminCache::InvalidateGroupCache()
   689 {
   690 	/* Nuke the free list */
   691 	m_FreeGroupList = -1;
   692 
   693 	/* Nuke reverse lookups */
   694 	sm_trie_clear(m_pGroups);
   695 
   696 	/* Free memory on groups */
   697 	GroupId cur = m_FirstGroup;
   698 	AdminGroup *pGroup;
   699 	while (cur != INVALID_GROUP_ID)
   700 	{
   701 		pGroup = (AdminGroup *)m_pMemory->GetAddress(cur);
   702 		assert(pGroup->magic == GRP_MAGIC_SET);
   703 		if (pGroup->pCmdGrpTable)
   704 		{
   705 			sm_trie_destroy(pGroup->pCmdGrpTable);
   706 		}
   707 		if (pGroup->pCmdTable)
   708 		{
   709 			sm_trie_destroy(pGroup->pCmdTable);
   710 		}
   711 		cur = pGroup->next_grp;
   712 	}
   713 
   714 	m_FirstGroup = INVALID_GROUP_ID;
   715 	m_LastGroup = INVALID_GROUP_ID;
   716 
   717 	InvalidateAdminCache(false);
   718 
   719 	/* Reset the memory table */
   720 	m_pMemory->Reset();
   721 }
   722 
   723 void AdminCache::AddAdminListener(IAdminListener *pListener)
   724 {
   725 	m_hooks.push_back(pListener);
   726 }
   727 
   728 void AdminCache::RemoveAdminListener(IAdminListener *pListener)
   729 {
   730 	m_hooks.remove(pListener);
   731 }
   732 
   733 void AdminCache::RegisterAuthIdentType(const char *name)
   734 {
   735 	if (sm_trie_retrieve(m_pAuthTables, name, NULL))
   736 	{
   737 		return;
   738 	}
   739 
   740 	Trie *pAuth = sm_trie_create();
   741 
   742 	AuthMethod method;
   743 	method.name.assign(name);
   744 	method.table = pAuth;
   745 
   746 	m_AuthMethods.push_back(method);
   747 
   748 	sm_trie_insert(m_pAuthTables, name, pAuth);
   749 }
   750 
   751 void AdminCache::InvalidateAdminCache(bool unlink_admins)
   752 {
   753 	m_InvalidatingAdmins = true;
   754 	if (!m_destroying)
   755 	{
   756 		g_Players.ClearAllAdmins();
   757 	}
   758 	/* Wipe the identity cache first */
   759 	List<AuthMethod>::iterator iter;
   760 	for (iter=m_AuthMethods.begin();
   761 		 iter!=m_AuthMethods.end();
   762 		 iter++)
   763 	{
   764 		sm_trie_clear((*iter).table);
   765 	}
   766 	
   767 	if (unlink_admins)
   768 	{
   769 		while (m_FirstUser != INVALID_ADMIN_ID)
   770 		{
   771 			InvalidateAdmin(m_FirstUser);
   772 		}
   773 	} else {
   774 		m_FirstUser = -1;
   775 		m_LastUser = -1;
   776 		m_FreeUserList = -1;
   777 	}
   778 	m_InvalidatingAdmins = false;
   779 }
   780 
   781 void AdminCache::DumpAdminCache(AdminCachePart part, bool rebuild)
   782 {
   783 	List<IAdminListener *>::iterator iter;
   784 	IAdminListener *pListener;
   785 	cell_t result;
   786 
   787 	if (part == AdminCache_Overrides)
   788 	{
   789 		DumpCommandOverrideCache(Override_Command);
   790 		DumpCommandOverrideCache(Override_CommandGroup);
   791 		if (rebuild && !m_destroying)
   792 		{
   793 			for (iter=m_hooks.begin(); iter!=m_hooks.end(); iter++)
   794 			{
   795 				pListener = (*iter);
   796 				pListener->OnRebuildOverrideCache();
   797 			}
   798 			m_pCacheFwd->PushCell(part);
   799 			m_pCacheFwd->Execute(&result);
   800 		}
   801 	} else if (part == AdminCache_Groups || part == AdminCache_Admins) {
   802 		if (part == AdminCache_Groups)
   803 		{
   804 			InvalidateGroupCache();
   805 			if (rebuild && !m_destroying)
   806 			{
   807 				for (iter=m_hooks.begin(); iter!=m_hooks.end(); iter++)
   808 				{
   809 					pListener = (*iter);
   810 					pListener->OnRebuildGroupCache();
   811 				}
   812 				m_pCacheFwd->PushCell(part);
   813 				m_pCacheFwd->Execute(&result);
   814 			}
   815 		}
   816 		InvalidateAdminCache(true);
   817 		if (rebuild && !m_destroying)
   818 		{
   819 			for (iter=m_hooks.begin(); iter!=m_hooks.end(); iter++)
   820 			{
   821 				pListener = (*iter);
   822 				pListener->OnRebuildAdminCache((part == AdminCache_Groups));
   823 			}
   824 			m_pCacheFwd->PushCell(AdminCache_Admins);
   825 			m_pCacheFwd->Execute(&result);
   826 		}
   827 	}
   828 }
   829 
   830 const char *AdminCache::GetAdminName(AdminId id)
   831 {
   832 	AdminUser *pUser = (AdminUser *)m_pMemory->GetAddress(id);
   833 	if (!pUser || pUser->magic != USR_MAGIC_SET)
   834 	{
   835 		return NULL;
   836 	}
   837 
   838 	return m_pStrings->GetString(pUser->nameidx);
   839 }
   840 
   841 bool AdminCache::GetMethodIndex(const char *name, unsigned int *_index)
   842 {
   843 	List<AuthMethod>::iterator iter;
   844 	unsigned int index = 0;
   845 	for (iter=m_AuthMethods.begin();
   846 		 iter!=m_AuthMethods.end();
   847 		 iter++,index++)
   848 	{
   849 		if ((*iter).name.compare(name) == 0)
   850 		{
   851 			*_index = index;
   852 			return true;
   853 		}
   854 	}
   855 
   856 	return false;
   857 }
   858 
   859 bool AdminCache::BindAdminIdentity(AdminId id, const char *auth, const char *ident)
   860 {
   861 	AdminUser *pUser = (AdminUser *)m_pMemory->GetAddress(id);
   862 	if (!pUser || pUser->magic != USR_MAGIC_SET)
   863 	{
   864 		return false;
   865 	}
   866 
   867 	Trie *pTable;
   868 	if (!sm_trie_retrieve(m_pAuthTables, auth, (void **)&pTable))
   869 	{
   870 		return false;
   871 	}
   872 
   873 	if (sm_trie_retrieve(pTable, ident, NULL))
   874 	{
   875 		return false;
   876 	}
   877 
   878 	pUser->auth.identidx = m_pStrings->AddString(ident);
   879 	GetMethodIndex(auth, &pUser->auth.index);
   880 
   881 	return sm_trie_insert(pTable, ident, (void **)id);
   882 }
   883 
   884 AdminId AdminCache::FindAdminByIdentity(const char *auth, const char *identity)
   885 {
   886 	Trie *pTable;
   887 	if (!sm_trie_retrieve(m_pAuthTables, auth, (void **)&pTable))
   888 	{
   889 		return INVALID_ADMIN_ID;
   890 	}
   891 
   892 	void *object;
   893 	if (!sm_trie_retrieve(pTable, identity, &object))
   894 	{
   895 		return INVALID_ADMIN_ID;
   896 	}
   897 
   898 	return (AdminId)object;
   899 }
   900 
   901 void AdminCache::SetAdminFlag(AdminId id, AdminFlag flag, bool enabled)
   902 {
   903 	AdminUser *pUser = (AdminUser *)m_pMemory->GetAddress(id);
   904 	if (!pUser || pUser->magic != USR_MAGIC_SET)
   905 	{
   906 		return;
   907 	}
   908 
   909 	if (flag < Admin_Reservation
   910 		|| flag >= AdminFlags_TOTAL)
   911 	{
   912 		return;
   913 	}
   914 	
   915 	FlagBits bits = (1<<(FlagBits)flag);
   916 
   917 	if (enabled)
   918 	{
   919 		pUser->flags |= bits;
   920 		pUser->eflags |= bits;
   921 	} else {
   922 		pUser->flags &= ~bits;
   923 		pUser->eflags &= ~bits;
   924 	}
   925 }
   926 
   927 bool AdminCache::GetAdminFlag(AdminId id, AdminFlag flag, AccessMode mode)
   928 {
   929 	AdminUser *pUser = (AdminUser *)m_pMemory->GetAddress(id);
   930 	if (!pUser || pUser->magic != USR_MAGIC_SET)
   931 	{
   932 		return false;
   933 	}
   934 
   935 	if (flag < Admin_Reservation
   936 		|| flag >= AdminFlags_TOTAL)
   937 	{
   938 		return false;
   939 	}
   940 
   941 	FlagBits bit = (1<<(FlagBits)flag);
   942 
   943 	if (mode == Access_Real)
   944 	{
   945 		return ((pUser->flags & bit) == bit);
   946 	} else if (mode == Access_Effective) {
   947 		return ((pUser->eflags & bit) == bit);
   948 	}
   949 
   950 	return false;
   951 }
   952 
   953 FlagBits AdminCache::GetAdminFlags(AdminId id, AccessMode mode)
   954 {
   955 	AdminUser *pUser = (AdminUser *)m_pMemory->GetAddress(id);
   956 	if (!pUser || pUser->magic != USR_MAGIC_SET)
   957 	{
   958 		return 0;
   959 	}
   960 
   961 	if (mode == Access_Real)
   962 	{
   963 		return pUser->flags;
   964 	} else if (mode == Access_Effective) {
   965 		return pUser->eflags;
   966 	}
   967 
   968 	return 0;
   969 }
   970 
   971 void AdminCache::SetAdminFlags(AdminId id, AccessMode mode, FlagBits bits)
   972 {
   973 	AdminUser *pUser = (AdminUser *)m_pMemory->GetAddress(id);
   974 	if (!pUser || pUser->magic != USR_MAGIC_SET)
   975 	{
   976 		return;
   977 	}
   978 
   979 	if (mode == Access_Real)
   980 	{
   981 		pUser->flags = bits;
   982 		pUser->eflags = bits;
   983 	} else if (mode == Access_Effective) {
   984 		pUser->eflags = bits;
   985 	}
   986 }
   987 
   988 bool AdminCache::AdminInheritGroup(AdminId id, GroupId gid)
   989 {
   990 	AdminUser *pUser = (AdminUser *)m_pMemory->GetAddress(id);
   991 	if (!pUser || pUser->magic != USR_MAGIC_SET)
   992 	{
   993 		return false;
   994 	}
   995 
   996 	AdminGroup *pGroup = (AdminGroup *)m_pMemory->GetAddress(gid);
   997 	if (!pGroup || pGroup->magic != GRP_MAGIC_SET)
   998 	{
   999 		return false;
  1000 	}
  1001 
  1002 	/* First check for duplicates */
  1003 	if (pUser->grp_count != 0)
  1004 	{
  1005 		int *temp_table = (int *)m_pMemory->GetAddress(pUser->grp_table);
  1006 		for (unsigned int i=0; i<pUser->grp_count; i++)
  1007 		{
  1008 			if (temp_table[i] == gid)
  1009 			{
  1010 				return false;
  1011 			}
  1012 		}
  1013 	}
  1014 
  1015 	int *table;
  1016 	if (pUser->grp_count + 1 > pUser->grp_size)
  1017 	{
  1018 		int new_size = 0;
  1019 		int tblidx;
  1020 		
  1021 		if (pUser->grp_size == 0)
  1022 		{
  1023 			new_size = 2;
  1024 		} else {
  1025 			new_size = pUser->grp_size * 2;
  1026 		}
  1027 		
  1028 		/* Create and refresh pointers */
  1029 		tblidx = m_pMemory->CreateMem(new_size * sizeof(int), (void **)&table);
  1030 		pUser = (AdminUser *)m_pMemory->GetAddress(id);
  1031 		pGroup = (AdminGroup *)m_pMemory->GetAddress(gid);
  1032 
  1033 		/* Copy old data if necessary */
  1034 		if (pUser->grp_table != -1)
  1035 		{
  1036 			int *old_table = (int *)m_pMemory->GetAddress(pUser->grp_table);
  1037 			memcpy(table, old_table, sizeof(int) * pUser->grp_count);
  1038 		}
  1039 		pUser->grp_table = tblidx;
  1040 		pUser->grp_size = new_size;
  1041 	} else {
  1042 		table = (int *)m_pMemory->GetAddress(pUser->grp_table);
  1043 	}
  1044 
  1045 	table[pUser->grp_count] = gid;
  1046 	pUser->grp_count++;
  1047 
  1048 	/* Compute new effective permissions */
  1049 	pUser->eflags |= pGroup->addflags;
  1050 
  1051 	if (pGroup->immune_default)
  1052 	{
  1053 		pUser->immune_default = true;
  1054 	}
  1055 	if (pGroup->immune_global)
  1056 	{
  1057 		pUser->immune_global = true;
  1058 	}
  1059 
  1060 	return true;
  1061 }
  1062 
  1063 bool AdminCache::IsValidAdmin(AdminId id)
  1064 {
  1065 	AdminUser *pUser = (AdminUser *)m_pMemory->GetAddress(id);
  1066 	return (pUser != NULL && pUser->magic == USR_MAGIC_SET);
  1067 }
  1068 
  1069 unsigned int AdminCache::GetAdminGroupCount(AdminId id)
  1070 {
  1071 	AdminUser *pUser = (AdminUser *)m_pMemory->GetAddress(id);
  1072 	if (!pUser || pUser->magic != USR_MAGIC_SET)
  1073 	{
  1074 		return 0;
  1075 	}
  1076 
  1077 	return pUser->grp_count;
  1078 }
  1079 
  1080 GroupId AdminCache::GetAdminGroup(AdminId id, unsigned int index, const char **name)
  1081 {
  1082 	AdminUser *pUser = (AdminUser *)m_pMemory->GetAddress(id);
  1083 	if (!pUser || pUser->magic != USR_MAGIC_SET || index >= pUser->grp_count)
  1084 	{
  1085 		return INVALID_GROUP_ID;
  1086 	}
  1087 
  1088 	int *table = (int *)m_pMemory->GetAddress(pUser->grp_table);
  1089 
  1090 	return table[index];
  1091 }
  1092 
  1093 const char *AdminCache::GetAdminPassword(AdminId id)
  1094 {
  1095 	AdminUser *pUser = (AdminUser *)m_pMemory->GetAddress(id);
  1096 	if (!pUser || pUser->magic != USR_MAGIC_SET)
  1097 	{
  1098 		return NULL;
  1099 	}
  1100 
  1101 	return m_pStrings->GetString(pUser->password);
  1102 }
  1103 
  1104 void AdminCache::SetAdminPassword(AdminId id, const char *password)
  1105 {	
  1106 	AdminUser *pUser = (AdminUser *)m_pMemory->GetAddress(id);
  1107 	if (!pUser || pUser->magic != USR_MAGIC_SET)
  1108 	{
  1109 		return;
  1110 	}
  1111 
  1112 	pUser->password = m_pStrings->AddString(password);
  1113 }
  1114 
  1115 unsigned int AdminCache::FlagBitsToBitArray(FlagBits bits, bool array[], unsigned int maxSize)
  1116 {
  1117 	unsigned int i;
  1118 	for (i=0; i<maxSize && i<AdminFlags_TOTAL; i++)
  1119 	{
  1120 		array[i] = ((bits & (1<<i)) == (unsigned)(1<<i));
  1121 	}
  1122 
  1123 	return i;
  1124 }
  1125 
  1126 FlagBits AdminCache::FlagBitArrayToBits(const bool array[], unsigned int maxSize)
  1127 {
  1128 	FlagBits bits = 0;
  1129 	for (unsigned int i=0; i<maxSize && i<AdminFlags_TOTAL; i++)
  1130 	{
  1131 		if (array[i])
  1132 		{
  1133 			bits |= (1<<i);
  1134 		}
  1135 	}
  1136 
  1137 	return bits;
  1138 }
  1139 
  1140 FlagBits AdminCache::FlagArrayToBits(const AdminFlag array[], unsigned int numFlags)
  1141 {
  1142 	FlagBits bits = 0;
  1143 	for (unsigned int i=0; i<numFlags && i<AdminFlags_TOTAL; i++)
  1144 	{
  1145 		bits |= (1 << (FlagBits)array[i]);
  1146 	}
  1147 
  1148 	return bits;
  1149 }
  1150 
  1151 unsigned int AdminCache::FlagBitsToArray(FlagBits bits, AdminFlag array[], unsigned int maxSize)
  1152 {
  1153 	unsigned int i, num=0;
  1154 	for (i=0; i<maxSize && i<AdminFlags_TOTAL; i++)
  1155 	{
  1156 		if ((bits & (1<<i)) == (unsigned)(1<<i))
  1157 		{
  1158 			array[num++] = (AdminFlag)i;
  1159 		}
  1160 	}
  1161 
  1162 	return num;
  1163 }
  1164 
  1165 bool AdminCache::CheckAdminFlags(AdminId id, FlagBits bits)
  1166 {
  1167 	AdminUser *pUser = (AdminUser *)m_pMemory->GetAddress(id);
  1168 	if (!pUser || pUser->magic != USR_MAGIC_SET)
  1169 	{
  1170 		return false;
  1171 	}
  1172 
  1173 	return ((pUser->eflags & bits) == bits);
  1174 }
  1175 
  1176 bool AdminCache::CanAdminTarget(AdminId id, AdminId target)
  1177 {
  1178 	/** 
  1179 	 * Zeroth, if the targeting AdminId is INVALID_ADMIN_ID, targeting fails.
  1180 	 * First, if the targetted AdminId is INVALID_ADMIN_ID, targeting succeeds.
  1181 	 */
  1182 
  1183 	if (id == INVALID_ADMIN_ID)
  1184 	{
  1185 		return false;
  1186 	}
  1187 
  1188 	if (target == INVALID_ADMIN_ID)
  1189 	{
  1190 		return true;
  1191 	}
  1192 
  1193 	AdminUser *pUser = (AdminUser *)m_pMemory->GetAddress(id);
  1194 	if (!pUser || pUser->magic != USR_MAGIC_SET)
  1195 	{
  1196 		return false;
  1197 	}
  1198 
  1199 	AdminUser *pTarget = (AdminUser *)m_pMemory->GetAddress(target);
  1200 	if (!pTarget || pTarget->magic != USR_MAGIC_SET)
  1201 	{
  1202 		return false;
  1203 	}
  1204 
  1205 	/** 
  1206 	 * Second, if the targeting admin is root, targeting suceeds.
  1207 	 */
  1208 	if (pUser->eflags & ADMFLAG_ROOT)
  1209 	{
  1210 		return true;
  1211 	}
  1212 
  1213 	/** Fourth, if the targetted admin has global immunity, targeting fails. */
  1214 	if (pTarget->immune_global)
  1215 	{
  1216 		return false;
  1217 	}
  1218 
  1219 	/** 
  1220 	 * Fifth, if the targetted admin has default immunity 
  1221 	 *  and the admin belongs to no groups, targeting fails.
  1222 	 */
  1223 	if (pTarget->immune_default && pUser->grp_count < 1)
  1224 	{
  1225 		return false;
  1226 	}
  1227 
  1228 	/** 
  1229 	 * Sixth, if the targetted admin has specific immunity from the
  1230 	 *  targeting admin via group immunities, targeting fails.
  1231 	 */
  1232 	//:TODO: speed this up... maybe with trie hacks.
  1233 	//idea is to insert %d.%d in the trie after computing this and use it as a cache lookup.
  1234 	//problem is the trie cannot delete prefixes, so we'd have a problem with invalidations.
  1235 	if (pTarget->grp_count > 0 && pUser->grp_count > 0)
  1236 	{
  1237 		int *grp_table = (int *)m_pMemory->GetAddress(pTarget->grp_table);
  1238 		int *src_table = (int *)m_pMemory->GetAddress(pUser->grp_table);
  1239 		GroupId id, other;
  1240 		unsigned int num;
  1241 		for (unsigned int i=0; i<pTarget->grp_count; i++)
  1242 		{
  1243 			id = grp_table[i];
  1244 			num = GetGroupImmunityCount(id);
  1245 			for (unsigned int j=0; j<num; i++)
  1246 			{
  1247 				other = GetGroupImmunity(id, j);
  1248 				for (unsigned int k=0; k<pUser->grp_count; k++)
  1249 				{
  1250 					if (other == src_table[k])
  1251 					{
  1252 						return false;
  1253 					}
  1254 				}
  1255 			}
  1256 		}
  1257 	}
  1258 	
  1259 	return true;
  1260 }