core/AdminCache.h
author Scott Ehlert <ds@alliedmods.net>
Wed Aug 15 06:19:30 2007 +0000 (2007-08-15)
changeset 1330 ab94316bb52b
parent 1298 9cddbed74581
child 1349 0d84046ec139
permissions -rw-r--r--
Updated license headers on virtually all files with extremely minor changes in the name of some sort of strange consistency.
All plugin and include file headers also have been changed to say about GPL3 instead of GPL2.

(This day shall henceforth be known as the Eighty Column Massacre of '07)
     1 /**
     2  * vim: set ts=4 :
     3  * =============================================================================
     4  * SourceMod
     5  * Copyright (C) 2004-2007 AlliedModders LLC.  All rights reserved.
     6  * =============================================================================
     7  *
     8  * This program is free software; you can redistribute it and/or modify it under
     9  * the terms of the GNU General Public License, version 3.0, as published by the
    10  * Free Software Foundation.
    11  * 
    12  * This program is distributed in the hope that it will be useful, but WITHOUT
    13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    14  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
    15  * details.
    16  *
    17  * You should have received a copy of the GNU General Public License along with
    18  * this program.  If not, see <http://www.gnu.org/licenses/>.
    19  *
    20  * As a special exception, AlliedModders LLC gives you permission to link the
    21  * code of this program (as well as its derivative works) to "Half-Life 2," the
    22  * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
    23  * by the Valve Corporation.  You must obey the GNU General Public License in
    24  * all respects for all other code used.  Additionally, AlliedModders LLC grants
    25  * this exception to all derivative works.  AlliedModders LLC defines further
    26  * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
    27  * or <http://www.sourcemod.net/license.php>.
    28  *
    29  * Version: $Id$
    30  */
    31 
    32 #ifndef _INCLUDE_SOURCEMOD_ADMINCACHE_H_
    33 #define _INCLUDE_SOURCEMOD_ADMINCACHE_H_
    34 
    35 #include "sm_memtable.h"
    36 #include <sm_trie.h>
    37 #include <sh_list.h>
    38 #include <sh_string.h>
    39 #include <IAdminSystem.h>
    40 #include "sm_globals.h"
    41 #include <IForwardSys.h>
    42 
    43 using namespace SourceHook;
    44 
    45 #define GRP_MAGIC_SET		0xDEADFADE
    46 #define GRP_MAGIC_UNSET		0xFACEFACE
    47 #define USR_MAGIC_SET		0xDEADFACE
    48 #define USR_MAGIC_UNSET		0xFADEDEAD
    49 
    50 struct AdminGroup
    51 {
    52 	uint32_t magic;					/* Magic flag, for memory validation (ugh) */
    53 	bool immune_global;				/* Global immunity? */
    54 	bool immune_default;			/* Default immunity? */
    55 	/* Immune from target table (-1 = nonexistent)
    56 	 * [0] = number of entries
    57 	 * [1...N] = immune targets
    58 	 */
    59 	int immune_table;			
    60 	Trie *pCmdTable;				/* Command override table (can be NULL) */
    61 	Trie *pCmdGrpTable;				/* Command group override table (can be NULL) */
    62 	int next_grp;					/* Next group in the chain */
    63 	int prev_grp;					/* Previous group in the chain */
    64 	int nameidx;					/* Name */
    65 	FlagBits addflags;				/* Additive flags */
    66 };
    67 
    68 struct AuthMethod
    69 {
    70 	String name;
    71 	Trie *table;
    72 };
    73 
    74 struct UserAuth
    75 {
    76 	unsigned int index;				/* Index into auth table */
    77 	int identidx;					/* Index into the string table */
    78 };
    79 
    80 struct AdminUser
    81 {
    82 	uint32_t magic;					/* Magic flag, for memory validation */
    83 	FlagBits flags;					/* Flags */
    84 	FlagBits eflags;				/* Effective flags */
    85 	int nameidx;					/* Name index */
    86 	int password;					/* Password index */
    87 	unsigned int grp_count;			/* Number of groups */
    88 	unsigned int grp_size;			/* Size of groups table */
    89 	int grp_table;					/* Group table itself */
    90 	int next_user;					/* Next user in the list */
    91 	int prev_user;					/* Previous user in the list */
    92 	UserAuth auth;					/* Auth method for this user */
    93 	bool immune_global;				/* Whether globally immune */
    94 	bool immune_default;			/* Whether defaultly immune */
    95 };
    96 
    97 class AdminCache : 
    98 	public IAdminSystem,
    99 	public SMGlobalClass
   100 {
   101 public:
   102 	AdminCache();
   103 	~AdminCache();
   104 public: //SMGlobalClass
   105 	void OnSourceModStartup(bool late);
   106 	void OnSourceModAllInitialized();
   107 	void OnSourceModLevelChange(const char *mapName);
   108 	void OnSourceModShutdown();
   109 	void OnSourceModPluginsLoaded();
   110 public: //IAdminSystem
   111 	/** Command cache stuff */
   112 	void AddCommandOverride(const char *cmd, OverrideType type, FlagBits flags);
   113 	bool GetCommandOverride(const char *cmd, OverrideType type, FlagBits *flags);
   114 	void UnsetCommandOverride(const char *cmd, OverrideType type);
   115 	/** Group cache stuff */
   116 	GroupId AddGroup(const char *group_name);
   117 	GroupId FindGroupByName(const char *group_name);
   118 	void SetGroupAddFlag(GroupId id, AdminFlag flag, bool enabled);
   119 	bool GetGroupAddFlag(GroupId id, AdminFlag flag);
   120 	FlagBits GetGroupAddFlags(GroupId id);
   121 	void SetGroupGenericImmunity(GroupId id, ImmunityType type, bool enabled);
   122 	bool GetGroupGenericImmunity(GroupId id, ImmunityType type);
   123 	void InvalidateGroup(GroupId id);
   124 	void AddGroupImmunity(GroupId id, GroupId other_id);
   125 	unsigned int GetGroupImmunityCount(GroupId id);
   126 	GroupId GetGroupImmunity(GroupId id, unsigned int number);
   127 	void AddGroupCommandOverride(GroupId id, const char *name, OverrideType type, OverrideRule rule);
   128 	bool GetGroupCommandOverride(GroupId id, const char *name, OverrideType type, OverrideRule *pRule);
   129 	void DumpAdminCache(AdminCachePart part, bool rebuild);
   130 	void AddAdminListener(IAdminListener *pListener);
   131 	void RemoveAdminListener(IAdminListener *pListener);
   132 	/** User stuff */
   133 	void RegisterAuthIdentType(const char *name);
   134 	AdminId CreateAdmin(const char *name);
   135 	const char *GetAdminName(AdminId id);
   136 	bool BindAdminIdentity(AdminId id, const char *auth, const char *ident);
   137 	virtual void SetAdminFlag(AdminId id, AdminFlag flag, bool enabled);
   138 	bool GetAdminFlag(AdminId id, AdminFlag flag, AccessMode mode);
   139 	FlagBits GetAdminFlags(AdminId id, AccessMode mode);
   140 	bool AdminInheritGroup(AdminId id, GroupId gid);
   141 	unsigned int GetAdminGroupCount(AdminId id);
   142 	GroupId GetAdminGroup(AdminId id, unsigned int index, const char **name);
   143 	void SetAdminPassword(AdminId id, const char *password);
   144 	const char *GetAdminPassword(AdminId id);
   145 	AdminId FindAdminByIdentity(const char *auth, const char *identity);
   146 	bool InvalidateAdmin(AdminId id);
   147 	unsigned int FlagBitsToBitArray(FlagBits bits, bool array[], unsigned int maxSize);
   148 	FlagBits FlagBitArrayToBits(const bool array[], unsigned int maxSize);
   149 	FlagBits FlagArrayToBits(const AdminFlag array[], unsigned int numFlags);
   150 	unsigned int FlagBitsToArray(FlagBits bits, AdminFlag array[], unsigned int maxSize);
   151 	bool CheckAdminFlags(AdminId id, FlagBits bits);
   152 	bool CanAdminTarget(AdminId id, AdminId target);
   153 	void SetAdminFlags(AdminId id, AccessMode mode, FlagBits bits);
   154 	bool FindFlag(const char *str, AdminFlag *pFlag);
   155 	bool FindFlag(char c, AdminFlag *pAdmFlag);
   156 	FlagBits ReadFlagString(const char *flags, const char **end);
   157 public:
   158 	bool IsValidAdmin(AdminId id);
   159 private:
   160 	void _UnsetCommandOverride(const char *cmd);
   161 	void _UnsetCommandGroupOverride(const char *group);
   162 	void InvalidateGroupCache();
   163 	void InvalidateAdminCache(bool unlink_admins);
   164 	void DumpCommandOverrideCache(OverrideType type);
   165 	Trie *GetMethodByIndex(unsigned int index);
   166 	bool GetMethodIndex(const char *name, unsigned int *_index);
   167 	void NameFlag(const char *str, AdminFlag flag);
   168 public:
   169 	BaseStringTable *m_pStrings;
   170 	BaseMemTable *m_pMemory;
   171 	Trie *m_pCmdOverrides;
   172 	Trie *m_pCmdGrpOverrides;
   173 	int m_FirstGroup;
   174 	int m_LastGroup;
   175 	int m_FreeGroupList;
   176 	Trie *m_pGroups;
   177 	List<IAdminListener *> m_hooks;
   178 	List<AuthMethod> m_AuthMethods;
   179 	Trie *m_pAuthTables;
   180 	IForward *m_pCacheFwd;
   181 	int m_FirstUser;
   182 	int m_LastUser;
   183 	int m_FreeUserList;
   184 	bool m_InvalidatingAdmins;
   185 	bool m_destroying;
   186 	Trie *m_pLevelNames;
   187 };
   188 
   189 extern AdminCache g_Admins;
   190 
   191 #endif //_INCLUDE_SOURCEMOD_ADMINCACHE_H_