core/AdminCache.h
author David Anderson <dvander@alliedmods.net>
Wed Aug 01 02:12:47 2007 +0000 (2007-08-01)
changeset 1231 17badaf66ce4
parent 729 110c14c99545
child 1298 9cddbed74581
permissions -rw-r--r--
updated more header files (mostly internal now)
     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 
     9  * modify it under the terms of the GNU General Public License, 
    10  * version 3.0, as published by the Free Software Foundation.
    11  * 
    12  * This program is distributed in the hope that it will be useful,
    13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    15  * GNU General Public License for more details.
    16  *
    17  * You should have received a copy of the GNU General Public License
    18  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    19  *
    20  * As a special exception, AlliedModders LLC gives you permission to 
    21  * link the code of this program (as well as its derivative works) to 
    22  * "Half-Life 2," the "Source Engine," the "SourcePawn JIT," and any 
    23  * Game MODs that run on software by the Valve Corporation.  You must 
    24  * obey the GNU General Public License in all respects for all other 
    25  * code used. Additionally, AlliedModders LLC grants this exception 
    26  * to all derivative works. AlliedModders LLC defines further 
    27  * exceptions, found in LICENSE.txt (as of this writing, version 
    28  * JULY-31-2007), or <http://www.sourcemod.net/license.php>.
    29  *
    30  * Version: $Id$
    31  */
    32 
    33 #ifndef _INCLUDE_SOURCEMOD_ADMINCACHE_H_
    34 #define _INCLUDE_SOURCEMOD_ADMINCACHE_H_
    35 
    36 #include "sm_memtable.h"
    37 #include <sm_trie.h>
    38 #include <sh_list.h>
    39 #include <sh_string.h>
    40 #include <IAdminSystem.h>
    41 #include "sm_globals.h"
    42 #include <IForwardSys.h>
    43 
    44 using namespace SourceHook;
    45 
    46 #define GRP_MAGIC_SET		0xDEADFADE
    47 #define GRP_MAGIC_UNSET		0xFACEFACE
    48 #define USR_MAGIC_SET		0xDEADFACE
    49 #define USR_MAGIC_UNSET		0xFADEDEAD
    50 
    51 struct AdminGroup
    52 {
    53 	uint32_t magic;					/* Magic flag, for memory validation (ugh) */
    54 	bool immune_global;				/* Global immunity? */
    55 	bool immune_default;			/* Default immunity? */
    56 	/* Immune from target table (-1 = nonexistent)
    57 	 * [0] = number of entries
    58 	 * [1...N] = immune targets
    59 	 */
    60 	int immune_table;			
    61 	Trie *pCmdTable;				/* Command override table (can be NULL) */
    62 	Trie *pCmdGrpTable;				/* Command group override table (can be NULL) */
    63 	int next_grp;					/* Next group in the chain */
    64 	int prev_grp;					/* Previous group in the chain */
    65 	int nameidx;					/* Name */
    66 	FlagBits addflags;				/* Additive flags */
    67 };
    68 
    69 struct AuthMethod
    70 {
    71 	String name;
    72 	Trie *table;
    73 };
    74 
    75 struct UserAuth
    76 {
    77 	unsigned int index;				/* Index into auth table */
    78 	int identidx;					/* Index into the string table */
    79 };
    80 
    81 struct AdminUser
    82 {
    83 	uint32_t magic;					/* Magic flag, for memory validation */
    84 	FlagBits flags;					/* Flags */
    85 	FlagBits eflags;				/* Effective flags */
    86 	int nameidx;					/* Name index */
    87 	int password;					/* Password index */
    88 	unsigned int grp_count;			/* Number of groups */
    89 	unsigned int grp_size;			/* Size of groups table */
    90 	int grp_table;					/* Group table itself */
    91 	int next_user;					/* Next user in the list */
    92 	int prev_user;					/* Previous user in the list */
    93 	UserAuth auth;					/* Auth method for this user */
    94 	bool immune_global;				/* Whether globally immune */
    95 	bool immune_default;			/* Whether defaultly immune */
    96 };
    97 
    98 class AdminCache : 
    99 	public IAdminSystem,
   100 	public SMGlobalClass
   101 {
   102 public:
   103 	AdminCache();
   104 	~AdminCache();
   105 public: //SMGlobalClass
   106 	void OnSourceModStartup(bool late);
   107 	void OnSourceModAllInitialized();
   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 public:
   155 	bool IsValidAdmin(AdminId id);
   156 private:
   157 	void _UnsetCommandOverride(const char *cmd);
   158 	void _UnsetCommandGroupOverride(const char *group);
   159 	void InvalidateGroupCache();
   160 	void InvalidateAdminCache(bool unlink_admins);
   161 	void DumpCommandOverrideCache(OverrideType type);
   162 	Trie *GetMethodByIndex(unsigned int index);
   163 	bool GetMethodIndex(const char *name, unsigned int *_index);
   164 public:
   165 	BaseStringTable *m_pStrings;
   166 	BaseMemTable *m_pMemory;
   167 	Trie *m_pCmdOverrides;
   168 	Trie *m_pCmdGrpOverrides;
   169 	int m_FirstGroup;
   170 	int m_LastGroup;
   171 	int m_FreeGroupList;
   172 	Trie *m_pGroups;
   173 	List<IAdminListener *> m_hooks;
   174 	List<AuthMethod> m_AuthMethods;
   175 	Trie *m_pAuthTables;
   176 	IForward *m_pCacheFwd;
   177 	int m_FirstUser;
   178 	int m_LastUser;
   179 	int m_FreeUserList;
   180 	bool m_InvalidatingAdmins;
   181 	bool m_destroying;
   182 };
   183 
   184 extern AdminCache g_Admins;
   185 
   186 #endif //_INCLUDE_SOURCEMOD_ADMINCACHE_H_