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