core/AdminCache.cpp
author Scott Ehlert <ds@alliedmods.net>
Thu Mar 22 21:50:20 2007 +0000 (2007-03-22)
changeset 653 ef60870067ff
parent 591 86e97f3eda1f
child 673 fc1d50579d06
permissions -rw-r--r--
More consistency of some sort...

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