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