plugins/admin-flatfile/admin-users.sp
changeset 1952 b7aa2b626807
child 2052 138936f11ab5
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/plugins/admin-flatfile/admin-users.sp	Sun Mar 30 07:00:22 2008 +0000
     1.3 @@ -0,0 +1,206 @@
     1.4 +/**
     1.5 + * vim: set ts=4 :
     1.6 + * =============================================================================
     1.7 + * SourceMod Admin File Reader Plugin
     1.8 + * Reads the admins.cfg file.  Do not compile this directly.
     1.9 + *
    1.10 + * SourceMod (C)2004-2008 AlliedModders LLC.  All rights reserved.
    1.11 + * =============================================================================
    1.12 + *
    1.13 + * This program is free software; you can redistribute it and/or modify it under
    1.14 + * the terms of the GNU General Public License, version 3.0, as published by the
    1.15 + * Free Software Foundation.
    1.16 + * 
    1.17 + * This program is distributed in the hope that it will be useful, but WITHOUT
    1.18 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    1.19 + * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
    1.20 + * details.
    1.21 + *
    1.22 + * You should have received a copy of the GNU General Public License along with
    1.23 + * this program.  If not, see <http://www.gnu.org/licenses/>.
    1.24 + *
    1.25 + * As a special exception, AlliedModders LLC gives you permission to link the
    1.26 + * code of this program (as well as its derivative works) to "Half-Life 2," the
    1.27 + * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
    1.28 + * by the Valve Corporation.  You must obey the GNU General Public License in
    1.29 + * all respects for all other code used.  Additionally, AlliedModders LLC grants
    1.30 + * this exception to all derivative works.  AlliedModders LLC defines further
    1.31 + * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
    1.32 + * or <http://www.sourcemod.net/license.php>.
    1.33 + *
    1.34 + * Version: $Id$
    1.35 + */
    1.36 +
    1.37 +#define USER_STATE_NONE			0
    1.38 +#define USER_STATE_ADMINS		1
    1.39 +#define USER_STATE_INADMIN		2
    1.40 +
    1.41 +static Handle:g_hUserParser = INVALID_HANDLE;
    1.42 +static AdminId:g_CurUser = INVALID_ADMIN_ID;
    1.43 +static g_UserState = USER_STATE_NONE;
    1.44 +static String:g_CurAuth[64];
    1.45 +static String:g_CurIdent[64];
    1.46 +
    1.47 +public SMCResult:ReadUsers_NewSection(Handle:smc, const String:name[], bool:opt_quotes)
    1.48 +{
    1.49 +	if (g_IgnoreLevel)
    1.50 +	{
    1.51 +		g_IgnoreLevel++;
    1.52 +		return SMCParse_Continue;
    1.53 +	}
    1.54 +	
    1.55 +	if (g_UserState == USER_STATE_NONE)
    1.56 +	{
    1.57 +		if (StrEqual(name, "Admins"))
    1.58 +		{
    1.59 +			g_UserState = USER_STATE_ADMINS;
    1.60 +			g_CurUser = INVALID_ADMIN_ID;
    1.61 +		} else {
    1.62 +			g_IgnoreLevel++;
    1.63 +		}
    1.64 +	} else if (g_UserState == USER_STATE_ADMINS) {
    1.65 +		g_UserState = USER_STATE_INADMIN;
    1.66 +		g_CurUser = CreateAdmin(name);
    1.67 +		g_CurAuth[0] = '\0';
    1.68 +		g_CurIdent[0] = '\0';
    1.69 +	} else {
    1.70 +		g_IgnoreLevel++;
    1.71 +	}
    1.72 +	
    1.73 +	return SMCParse_Continue;
    1.74 +}
    1.75 +
    1.76 +public SMCResult:ReadUsers_KeyValue(Handle:smc, 
    1.77 +									const String:key[], 
    1.78 +									const String:value[], 
    1.79 +									bool:key_quotes, 
    1.80 +									bool:value_quotes)
    1.81 +{
    1.82 +	if (g_UserState != USER_STATE_INADMIN
    1.83 +		|| g_IgnoreLevel
    1.84 +		|| g_CurUser == INVALID_ADMIN_ID)
    1.85 +	{
    1.86 +		return SMCParse_Continue;
    1.87 +	}
    1.88 +	
    1.89 +	new bool:auth = false;
    1.90 +	
    1.91 +	if (StrEqual(key, "auth"))
    1.92 +	{
    1.93 +		auth = true;
    1.94 +		strcopy(g_CurAuth, sizeof(g_CurAuth), value);
    1.95 +	}
    1.96 +	else if (StrEqual(key, "identity"))
    1.97 +	{
    1.98 +		auth = true;
    1.99 +		strcopy(g_CurIdent, sizeof(g_CurIdent), value);
   1.100 +	}
   1.101 +	else if (StrEqual(key, "password")) 
   1.102 +	{
   1.103 +		SetAdminPassword(g_CurUser, value);
   1.104 +	} 
   1.105 +	else if (StrEqual(key, "group")) 
   1.106 +	{
   1.107 +		new GroupId:id = FindAdmGroup(value);
   1.108 +		if (id == INVALID_GROUP_ID)
   1.109 +		{
   1.110 +			ParseError("Unknown group \"%s\"", value);
   1.111 +		} else if (!AdminInheritGroup(g_CurUser, id)) {
   1.112 +			ParseError("Unable to inherit group \"%s\"", value);
   1.113 +		}
   1.114 +	} 
   1.115 +	else if (StrEqual(key, "flags")) 
   1.116 +	{
   1.117 +		new len = strlen(value);
   1.118 +		new AdminFlag:flag;
   1.119 +		
   1.120 +		for (new i=0; i<len; i++)
   1.121 +		{
   1.122 +			if (!FindFlagByChar(value[i], flag))
   1.123 +			{
   1.124 +				ParseError("Invalid flag detected: %c", value[i]);
   1.125 +			}
   1.126 +			SetAdminFlag(g_CurUser, flag, true);
   1.127 +		}
   1.128 +	} 
   1.129 +	else if (StrEqual(key, "immunity")) 
   1.130 +	{
   1.131 +		new level = StringToInt(value);
   1.132 +		SetAdminImmunityLevel(g_CurUser, level);
   1.133 +	}
   1.134 +
   1.135 +	if (auth && g_CurIdent[0] && g_CurAuth[0])
   1.136 +	{
   1.137 +		if (BindAdminIdentity(g_CurUser, g_CurAuth, g_CurIdent))
   1.138 +		{
   1.139 +			g_CurAuth[0] = '\0';
   1.140 +			g_CurIdent[0] = '\0';
   1.141 +		} else {
   1.142 +			ParseError("Failed to bind auth \"%s\" to identity \"%s\"", g_CurAuth, g_CurIdent);
   1.143 +		}
   1.144 +	}
   1.145 +	
   1.146 +	return SMCParse_Continue;
   1.147 +}
   1.148 +
   1.149 +public SMCResult:ReadUsers_EndSection(Handle:smc)
   1.150 +{
   1.151 +	if (g_IgnoreLevel)
   1.152 +	{
   1.153 +		g_IgnoreLevel--;
   1.154 +		return SMCParse_Continue;
   1.155 +	}
   1.156 +	
   1.157 +	if (g_UserState == USER_STATE_INADMIN)
   1.158 +	{
   1.159 +		g_UserState = USER_STATE_ADMINS;
   1.160 +		g_CurUser = INVALID_ADMIN_ID;
   1.161 +	} else if (g_UserState == USER_STATE_ADMINS) {
   1.162 +		g_UserState = USER_STATE_NONE;
   1.163 +	}
   1.164 +	
   1.165 +	return SMCParse_Continue;
   1.166 +}
   1.167 +
   1.168 +public SMCResult:ReadUsers_CurrentLine(Handle:smc, const String:line[], lineno)
   1.169 +{
   1.170 +	g_CurrentLine = lineno;
   1.171 +	
   1.172 +	return SMCParse_Continue;
   1.173 +}
   1.174 +
   1.175 +static InitializeUserParser()
   1.176 +{
   1.177 +	if (g_hUserParser == INVALID_HANDLE)
   1.178 +	{
   1.179 +		g_hUserParser = SMC_CreateParser();
   1.180 +		SMC_SetReaders(g_hUserParser,
   1.181 +					   ReadUsers_NewSection,
   1.182 +					   ReadUsers_KeyValue,
   1.183 +					   ReadUsers_EndSection);
   1.184 +		SMC_SetRawLine(g_hUserParser, ReadUsers_CurrentLine);
   1.185 +	}
   1.186 +}
   1.187 +
   1.188 +ReadUsers()
   1.189 +{
   1.190 +	InitializeUserParser();
   1.191 +	
   1.192 +	BuildPath(Path_SM, g_Filename, sizeof(g_Filename), "configs/admins.cfg");
   1.193 +
   1.194 +	/* Set states */
   1.195 +	InitGlobalStates();
   1.196 +	g_UserState = USER_STATE_NONE;
   1.197 +		
   1.198 +	new SMCError:err = SMC_ParseFile(g_hUserParser, g_Filename);
   1.199 +	if (err != SMCError_Okay)
   1.200 +	{
   1.201 +		decl String:buffer[64];
   1.202 +		if (SMC_GetErrorString(err, buffer, sizeof(buffer)))
   1.203 +		{
   1.204 +			ParseError("%s", buffer);
   1.205 +		} else {
   1.206 +			ParseError("Fatal parse error");
   1.207 +		}
   1.208 +	}
   1.209 +}