plugins/admin-flatfile/admin-users.sp
author David Anderson <dvander@alliedmods.net>
Sun Apr 06 08:20:02 2008 +0000 (2008-04-06)
changeset 1970 316eff8d8164
parent 1952 b7aa2b626807
child 2052 138936f11ab5
permissions -rw-r--r--
added regex versioning to windows
ds@1952
     1
/**
ds@1952
     2
 * vim: set ts=4 :
ds@1952
     3
 * =============================================================================
ds@1952
     4
 * SourceMod Admin File Reader Plugin
ds@1952
     5
 * Reads the admins.cfg file.  Do not compile this directly.
ds@1952
     6
 *
ds@1952
     7
 * SourceMod (C)2004-2008 AlliedModders LLC.  All rights reserved.
ds@1952
     8
 * =============================================================================
ds@1952
     9
 *
ds@1952
    10
 * This program is free software; you can redistribute it and/or modify it under
ds@1952
    11
 * the terms of the GNU General Public License, version 3.0, as published by the
ds@1952
    12
 * Free Software Foundation.
ds@1952
    13
 * 
ds@1952
    14
 * This program is distributed in the hope that it will be useful, but WITHOUT
ds@1952
    15
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
ds@1952
    16
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
ds@1952
    17
 * details.
ds@1952
    18
 *
ds@1952
    19
 * You should have received a copy of the GNU General Public License along with
ds@1952
    20
 * this program.  If not, see <http://www.gnu.org/licenses/>.
ds@1952
    21
 *
ds@1952
    22
 * As a special exception, AlliedModders LLC gives you permission to link the
ds@1952
    23
 * code of this program (as well as its derivative works) to "Half-Life 2," the
ds@1952
    24
 * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
ds@1952
    25
 * by the Valve Corporation.  You must obey the GNU General Public License in
ds@1952
    26
 * all respects for all other code used.  Additionally, AlliedModders LLC grants
ds@1952
    27
 * this exception to all derivative works.  AlliedModders LLC defines further
ds@1952
    28
 * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
ds@1952
    29
 * or <http://www.sourcemod.net/license.php>.
ds@1952
    30
 *
ds@1952
    31
 * Version: $Id$
ds@1952
    32
 */
ds@1952
    33
ds@1952
    34
#define USER_STATE_NONE			0
ds@1952
    35
#define USER_STATE_ADMINS		1
ds@1952
    36
#define USER_STATE_INADMIN		2
ds@1952
    37
ds@1952
    38
static Handle:g_hUserParser = INVALID_HANDLE;
ds@1952
    39
static AdminId:g_CurUser = INVALID_ADMIN_ID;
ds@1952
    40
static g_UserState = USER_STATE_NONE;
ds@1952
    41
static String:g_CurAuth[64];
ds@1952
    42
static String:g_CurIdent[64];
ds@1952
    43
ds@1952
    44
public SMCResult:ReadUsers_NewSection(Handle:smc, const String:name[], bool:opt_quotes)
ds@1952
    45
{
ds@1952
    46
	if (g_IgnoreLevel)
ds@1952
    47
	{
ds@1952
    48
		g_IgnoreLevel++;
ds@1952
    49
		return SMCParse_Continue;
ds@1952
    50
	}
ds@1952
    51
	
ds@1952
    52
	if (g_UserState == USER_STATE_NONE)
ds@1952
    53
	{
ds@1952
    54
		if (StrEqual(name, "Admins"))
ds@1952
    55
		{
ds@1952
    56
			g_UserState = USER_STATE_ADMINS;
ds@1952
    57
			g_CurUser = INVALID_ADMIN_ID;
ds@1952
    58
		} else {
ds@1952
    59
			g_IgnoreLevel++;
ds@1952
    60
		}
ds@1952
    61
	} else if (g_UserState == USER_STATE_ADMINS) {
ds@1952
    62
		g_UserState = USER_STATE_INADMIN;
ds@1952
    63
		g_CurUser = CreateAdmin(name);
ds@1952
    64
		g_CurAuth[0] = '\0';
ds@1952
    65
		g_CurIdent[0] = '\0';
ds@1952
    66
	} else {
ds@1952
    67
		g_IgnoreLevel++;
ds@1952
    68
	}
ds@1952
    69
	
ds@1952
    70
	return SMCParse_Continue;
ds@1952
    71
}
ds@1952
    72
ds@1952
    73
public SMCResult:ReadUsers_KeyValue(Handle:smc, 
ds@1952
    74
									const String:key[], 
ds@1952
    75
									const String:value[], 
ds@1952
    76
									bool:key_quotes, 
ds@1952
    77
									bool:value_quotes)
ds@1952
    78
{
ds@1952
    79
	if (g_UserState != USER_STATE_INADMIN
ds@1952
    80
		|| g_IgnoreLevel
ds@1952
    81
		|| g_CurUser == INVALID_ADMIN_ID)
ds@1952
    82
	{
ds@1952
    83
		return SMCParse_Continue;
ds@1952
    84
	}
ds@1952
    85
	
ds@1952
    86
	new bool:auth = false;
ds@1952
    87
	
ds@1952
    88
	if (StrEqual(key, "auth"))
ds@1952
    89
	{
ds@1952
    90
		auth = true;
ds@1952
    91
		strcopy(g_CurAuth, sizeof(g_CurAuth), value);
ds@1952
    92
	}
ds@1952
    93
	else if (StrEqual(key, "identity"))
ds@1952
    94
	{
ds@1952
    95
		auth = true;
ds@1952
    96
		strcopy(g_CurIdent, sizeof(g_CurIdent), value);
ds@1952
    97
	}
ds@1952
    98
	else if (StrEqual(key, "password")) 
ds@1952
    99
	{
ds@1952
   100
		SetAdminPassword(g_CurUser, value);
ds@1952
   101
	} 
ds@1952
   102
	else if (StrEqual(key, "group")) 
ds@1952
   103
	{
ds@1952
   104
		new GroupId:id = FindAdmGroup(value);
ds@1952
   105
		if (id == INVALID_GROUP_ID)
ds@1952
   106
		{
ds@1952
   107
			ParseError("Unknown group \"%s\"", value);
ds@1952
   108
		} else if (!AdminInheritGroup(g_CurUser, id)) {
ds@1952
   109
			ParseError("Unable to inherit group \"%s\"", value);
ds@1952
   110
		}
ds@1952
   111
	} 
ds@1952
   112
	else if (StrEqual(key, "flags")) 
ds@1952
   113
	{
ds@1952
   114
		new len = strlen(value);
ds@1952
   115
		new AdminFlag:flag;
ds@1952
   116
		
ds@1952
   117
		for (new i=0; i<len; i++)
ds@1952
   118
		{
ds@1952
   119
			if (!FindFlagByChar(value[i], flag))
ds@1952
   120
			{
ds@1952
   121
				ParseError("Invalid flag detected: %c", value[i]);
ds@1952
   122
			}
ds@1952
   123
			SetAdminFlag(g_CurUser, flag, true);
ds@1952
   124
		}
ds@1952
   125
	} 
ds@1952
   126
	else if (StrEqual(key, "immunity")) 
ds@1952
   127
	{
ds@1952
   128
		new level = StringToInt(value);
ds@1952
   129
		SetAdminImmunityLevel(g_CurUser, level);
ds@1952
   130
	}
ds@1952
   131
ds@1952
   132
	if (auth && g_CurIdent[0] && g_CurAuth[0])
ds@1952
   133
	{
ds@1952
   134
		if (BindAdminIdentity(g_CurUser, g_CurAuth, g_CurIdent))
ds@1952
   135
		{
ds@1952
   136
			g_CurAuth[0] = '\0';
ds@1952
   137
			g_CurIdent[0] = '\0';
ds@1952
   138
		} else {
ds@1952
   139
			ParseError("Failed to bind auth \"%s\" to identity \"%s\"", g_CurAuth, g_CurIdent);
ds@1952
   140
		}
ds@1952
   141
	}
ds@1952
   142
	
ds@1952
   143
	return SMCParse_Continue;
ds@1952
   144
}
ds@1952
   145
ds@1952
   146
public SMCResult:ReadUsers_EndSection(Handle:smc)
ds@1952
   147
{
ds@1952
   148
	if (g_IgnoreLevel)
ds@1952
   149
	{
ds@1952
   150
		g_IgnoreLevel--;
ds@1952
   151
		return SMCParse_Continue;
ds@1952
   152
	}
ds@1952
   153
	
ds@1952
   154
	if (g_UserState == USER_STATE_INADMIN)
ds@1952
   155
	{
ds@1952
   156
		g_UserState = USER_STATE_ADMINS;
ds@1952
   157
		g_CurUser = INVALID_ADMIN_ID;
ds@1952
   158
	} else if (g_UserState == USER_STATE_ADMINS) {
ds@1952
   159
		g_UserState = USER_STATE_NONE;
ds@1952
   160
	}
ds@1952
   161
	
ds@1952
   162
	return SMCParse_Continue;
ds@1952
   163
}
ds@1952
   164
ds@1952
   165
public SMCResult:ReadUsers_CurrentLine(Handle:smc, const String:line[], lineno)
ds@1952
   166
{
ds@1952
   167
	g_CurrentLine = lineno;
ds@1952
   168
	
ds@1952
   169
	return SMCParse_Continue;
ds@1952
   170
}
ds@1952
   171
ds@1952
   172
static InitializeUserParser()
ds@1952
   173
{
ds@1952
   174
	if (g_hUserParser == INVALID_HANDLE)
ds@1952
   175
	{
ds@1952
   176
		g_hUserParser = SMC_CreateParser();
ds@1952
   177
		SMC_SetReaders(g_hUserParser,
ds@1952
   178
					   ReadUsers_NewSection,
ds@1952
   179
					   ReadUsers_KeyValue,
ds@1952
   180
					   ReadUsers_EndSection);
ds@1952
   181
		SMC_SetRawLine(g_hUserParser, ReadUsers_CurrentLine);
ds@1952
   182
	}
ds@1952
   183
}
ds@1952
   184
ds@1952
   185
ReadUsers()
ds@1952
   186
{
ds@1952
   187
	InitializeUserParser();
ds@1952
   188
	
ds@1952
   189
	BuildPath(Path_SM, g_Filename, sizeof(g_Filename), "configs/admins.cfg");
ds@1952
   190
ds@1952
   191
	/* Set states */
ds@1952
   192
	InitGlobalStates();
ds@1952
   193
	g_UserState = USER_STATE_NONE;
ds@1952
   194
		
ds@1952
   195
	new SMCError:err = SMC_ParseFile(g_hUserParser, g_Filename);
ds@1952
   196
	if (err != SMCError_Okay)
ds@1952
   197
	{
ds@1952
   198
		decl String:buffer[64];
ds@1952
   199
		if (SMC_GetErrorString(err, buffer, sizeof(buffer)))
ds@1952
   200
		{
ds@1952
   201
			ParseError("%s", buffer);
ds@1952
   202
		} else {
ds@1952
   203
			ParseError("Fatal parse error");
ds@1952
   204
		}
ds@1952
   205
	}
ds@1952
   206
}