core/AdminCache.h
author David Anderson <dvander@alliedmods.net>
Mon Oct 15 00:38:25 2007 +0000 (2007-10-15)
changeset 1568 ecbaee393b0b
parent 1477 b2d8f676a902
child 1947 e73a1e51833b
permissions -rw-r--r--
extensions can now be built for multiple engines, and will autoload from auto.X.Y folders
     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 	unsigned int immunity_level;	/* Immunity level */
    54 	/* Immune from target table (-1 = nonexistent)
    55 	 * [0] = number of entries
    56 	 * [1...N] = immune targets
    57 	 */
    58 	int immune_table;			
    59 	Trie *pCmdTable;				/* Command override table (can be NULL) */
    60 	Trie *pCmdGrpTable;				/* Command group override table (can be NULL) */
    61 	int next_grp;					/* Next group in the chain */
    62 	int prev_grp;					/* Previous group in the chain */
    63 	int nameidx;					/* Name */
    64 	FlagBits addflags;				/* Additive flags */
    65 };
    66 
    67 struct AuthMethod
    68 {
    69 	String name;
    70 	Trie *table;
    71 };
    72 
    73 struct UserAuth
    74 {
    75 	unsigned int index;				/* Index into auth table */
    76 	int identidx;					/* Index into the string table */
    77 };
    78 
    79 struct AdminUser
    80 {
    81 	uint32_t magic;					/* Magic flag, for memory validation */
    82 	FlagBits flags;					/* Flags */
    83 	FlagBits eflags;				/* Effective flags */
    84 	int nameidx;					/* Name index */
    85 	int password;					/* Password index */
    86 	unsigned int grp_count;			/* Number of groups */
    87 	unsigned int grp_size;			/* Size of groups table */
    88 	int grp_table;					/* Group table itself */
    89 	int next_user;					/* Next user in the list */
    90 	int prev_user;					/* Previous user in the list */
    91 	UserAuth auth;					/* Auth method for this user */
    92 	unsigned int immunity_level;	/* Immunity level */
    93 	unsigned int serialchange;		/* Serial # for changes */
    94 };
    95 
    96 class AdminCache : 
    97 	public IAdminSystem,
    98 	public SMGlobalClass
    99 {
   100 public:
   101 	AdminCache();
   102 	~AdminCache();
   103 public: //SMGlobalClass
   104 	void OnSourceModStartup(bool late);
   105 	void OnSourceModAllInitialized();
   106 	void OnSourceModLevelChange(const char *mapName);
   107 	void OnSourceModShutdown();
   108 	void OnSourceModPluginsLoaded();
   109 public: //IAdminSystem
   110 	/** Command cache stuff */
   111 	void AddCommandOverride(const char *cmd, OverrideType type, FlagBits flags);
   112 	bool GetCommandOverride(const char *cmd, OverrideType type, FlagBits *flags);
   113 	void UnsetCommandOverride(const char *cmd, OverrideType type);
   114 	/** Group cache stuff */
   115 	GroupId AddGroup(const char *group_name);
   116 	GroupId FindGroupByName(const char *group_name);
   117 	void SetGroupAddFlag(GroupId id, AdminFlag flag, bool enabled);
   118 	bool GetGroupAddFlag(GroupId id, AdminFlag flag);
   119 	FlagBits GetGroupAddFlags(GroupId id);
   120 	void SetGroupGenericImmunity(GroupId id, ImmunityType type, bool enabled);
   121 	bool GetGroupGenericImmunity(GroupId id, ImmunityType type);
   122 	void InvalidateGroup(GroupId id);
   123 	void AddGroupImmunity(GroupId id, GroupId other_id);
   124 	unsigned int GetGroupImmunityCount(GroupId id);
   125 	GroupId GetGroupImmunity(GroupId id, unsigned int number);
   126 	void AddGroupCommandOverride(GroupId id, const char *name, OverrideType type, OverrideRule rule);
   127 	bool GetGroupCommandOverride(GroupId id, const char *name, OverrideType type, OverrideRule *pRule);
   128 	void DumpAdminCache(AdminCachePart part, bool rebuild);
   129 	void AddAdminListener(IAdminListener *pListener);
   130 	void RemoveAdminListener(IAdminListener *pListener);
   131 	/** User stuff */
   132 	void RegisterAuthIdentType(const char *name);
   133 	AdminId CreateAdmin(const char *name);
   134 	const char *GetAdminName(AdminId id);
   135 	bool BindAdminIdentity(AdminId id, const char *auth, const char *ident);
   136 	virtual void SetAdminFlag(AdminId id, AdminFlag flag, bool enabled);
   137 	bool GetAdminFlag(AdminId id, AdminFlag flag, AccessMode mode);
   138 	FlagBits GetAdminFlags(AdminId id, AccessMode mode);
   139 	bool AdminInheritGroup(AdminId id, GroupId gid);
   140 	unsigned int GetAdminGroupCount(AdminId id);
   141 	GroupId GetAdminGroup(AdminId id, unsigned int index, const char **name);
   142 	void SetAdminPassword(AdminId id, const char *password);
   143 	const char *GetAdminPassword(AdminId id);
   144 	AdminId FindAdminByIdentity(const char *auth, const char *identity);
   145 	bool InvalidateAdmin(AdminId id);
   146 	unsigned int FlagBitsToBitArray(FlagBits bits, bool array[], unsigned int maxSize);
   147 	FlagBits FlagBitArrayToBits(const bool array[], unsigned int maxSize);
   148 	FlagBits FlagArrayToBits(const AdminFlag array[], unsigned int numFlags);
   149 	unsigned int FlagBitsToArray(FlagBits bits, AdminFlag array[], unsigned int maxSize);
   150 	bool CheckAdminFlags(AdminId id, FlagBits bits);
   151 	bool CanAdminTarget(AdminId id, AdminId target);
   152 	void SetAdminFlags(AdminId id, AccessMode mode, FlagBits bits);
   153 	bool FindFlag(const char *str, AdminFlag *pFlag);
   154 	bool FindFlag(char c, AdminFlag *pAdmFlag);
   155 	FlagBits ReadFlagString(const char *flags, const char **end);
   156 	unsigned int GetAdminSerialChange(AdminId id);
   157 	bool CanAdminUseCommand(int client, const char *cmd);
   158 	const char *GetGroupName(GroupId gid);
   159 	unsigned int SetGroupImmunityLevel(GroupId gid, unsigned int level);
   160 	unsigned int GetGroupImmunityLevel(GroupId gid);
   161 	unsigned int SetAdminImmunityLevel(AdminId id, unsigned int level);
   162 	unsigned int GetAdminImmunityLevel(AdminId id);
   163 	bool CheckAccess(int client, 
   164 		const char *cmd, 
   165 		FlagBits flags, 
   166 		bool override_only);
   167 public:
   168 	bool IsValidAdmin(AdminId id);
   169 private:
   170 	void _UnsetCommandOverride(const char *cmd);
   171 	void _UnsetCommandGroupOverride(const char *group);
   172 	void InvalidateGroupCache();
   173 	void InvalidateAdminCache(bool unlink_admins);
   174 	void DumpCommandOverrideCache(OverrideType type);
   175 	Trie *GetMethodByIndex(unsigned int index);
   176 	bool GetMethodIndex(const char *name, unsigned int *_index);
   177 	void NameFlag(const char *str, AdminFlag flag);
   178 public:
   179 	BaseStringTable *m_pStrings;
   180 	BaseMemTable *m_pMemory;
   181 	Trie *m_pCmdOverrides;
   182 	Trie *m_pCmdGrpOverrides;
   183 	int m_FirstGroup;
   184 	int m_LastGroup;
   185 	int m_FreeGroupList;
   186 	Trie *m_pGroups;
   187 	List<IAdminListener *> m_hooks;
   188 	List<AuthMethod> m_AuthMethods;
   189 	Trie *m_pAuthTables;
   190 	IForward *m_pCacheFwd;
   191 	int m_FirstUser;
   192 	int m_LastUser;
   193 	int m_FreeUserList;
   194 	bool m_InvalidatingAdmins;
   195 	bool m_destroying;
   196 	Trie *m_pLevelNames;
   197 };
   198 
   199 extern AdminCache g_Admins;
   200 
   201 #endif //_INCLUDE_SOURCEMOD_ADMINCACHE_H_