plugins/admin-flatfile/admin-users.sp
branchrefac-jit
changeset 2168 c24a6fa957db
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/plugins/admin-flatfile/admin-users.sp	Sun Jul 06 04:49:55 2008 +0000
     1.3 @@ -0,0 +1,247 @@
     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 g_UserState = USER_STATE_NONE;
    1.43 +static String:g_CurAuth[64];
    1.44 +static String:g_CurIdent[64];
    1.45 +static String:g_CurName[64];
    1.46 +static String:g_CurPass[64];
    1.47 +static Handle:g_GroupArray;
    1.48 +static g_CurFlags;
    1.49 +static g_CurImmunity;
    1.50 +
    1.51 +public SMCResult:ReadUsers_NewSection(Handle:smc, const String:name[], bool:opt_quotes)
    1.52 +{
    1.53 +	if (g_IgnoreLevel)
    1.54 +	{
    1.55 +		g_IgnoreLevel++;
    1.56 +		return SMCParse_Continue;
    1.57 +	}
    1.58 +	
    1.59 +	if (g_UserState == USER_STATE_NONE)
    1.60 +	{
    1.61 +		if (StrEqual(name, "Admins"))
    1.62 +		{
    1.63 +			g_UserState = USER_STATE_ADMINS;
    1.64 +		}
    1.65 +		else
    1.66 +		{
    1.67 +			g_IgnoreLevel++;
    1.68 +		}
    1.69 +	}
    1.70 +	else if (g_UserState == USER_STATE_ADMINS)
    1.71 +	{
    1.72 +		g_UserState = USER_STATE_INADMIN;
    1.73 +		strcopy(g_CurName, sizeof(g_CurName), name);
    1.74 +		g_CurAuth[0] = '\0';
    1.75 +		g_CurIdent[0] = '\0';
    1.76 +		g_CurPass[0] = '\0';
    1.77 +		ClearArray(g_GroupArray);
    1.78 +		g_CurFlags = 0;
    1.79 +		g_CurImmunity = 0;
    1.80 +	}
    1.81 +	else
    1.82 +	{
    1.83 +		g_IgnoreLevel++;
    1.84 +	}
    1.85 +	
    1.86 +	return SMCParse_Continue;
    1.87 +}
    1.88 +
    1.89 +public SMCResult:ReadUsers_KeyValue(Handle:smc, 
    1.90 +									const String:key[], 
    1.91 +									const String:value[], 
    1.92 +									bool:key_quotes, 
    1.93 +									bool:value_quotes)
    1.94 +{
    1.95 +	if (g_UserState != USER_STATE_INADMIN || g_IgnoreLevel)
    1.96 +	{
    1.97 +		return SMCParse_Continue;
    1.98 +	}
    1.99 +	
   1.100 +	if (StrEqual(key, "auth"))
   1.101 +	{
   1.102 +		strcopy(g_CurAuth, sizeof(g_CurAuth), value);
   1.103 +	}
   1.104 +	else if (StrEqual(key, "identity"))
   1.105 +	{
   1.106 +		strcopy(g_CurIdent, sizeof(g_CurIdent), value);
   1.107 +	}
   1.108 +	else if (StrEqual(key, "password")) 
   1.109 +	{
   1.110 +		strcopy(g_CurPass, sizeof(g_CurPass), value);
   1.111 +	} 
   1.112 +	else if (StrEqual(key, "group")) 
   1.113 +	{
   1.114 +		new GroupId:id = FindAdmGroup(value);
   1.115 +		if (id == INVALID_GROUP_ID)
   1.116 +		{
   1.117 +			ParseError("Unknown group \"%s\"", value);
   1.118 +		}
   1.119 +
   1.120 +		PushArrayCell(g_GroupArray, id);
   1.121 +	} 
   1.122 +	else if (StrEqual(key, "flags")) 
   1.123 +	{
   1.124 +		new len = strlen(value);
   1.125 +		new AdminFlag:flag;
   1.126 +		
   1.127 +		for (new i = 0; i < len; i++)
   1.128 +		{
   1.129 +			if (!FindFlagByChar(value[i], flag))
   1.130 +			{
   1.131 +				ParseError("Invalid flag detected: %c", value[i]);
   1.132 +			}
   1.133 +			else
   1.134 +			{
   1.135 +				g_CurFlags |= FlagToBit(flag);
   1.136 +			}
   1.137 +		}
   1.138 +	} 
   1.139 +	else if (StrEqual(key, "immunity")) 
   1.140 +	{
   1.141 +		g_CurImmunity = StringToInt(value);
   1.142 +	}
   1.143 +	
   1.144 +	return SMCParse_Continue;
   1.145 +}
   1.146 +
   1.147 +public SMCResult:ReadUsers_EndSection(Handle:smc)
   1.148 +{
   1.149 +	if (g_IgnoreLevel)
   1.150 +	{
   1.151 +		g_IgnoreLevel--;
   1.152 +		return SMCParse_Continue;
   1.153 +	}
   1.154 +	
   1.155 +	if (g_UserState == USER_STATE_INADMIN)
   1.156 +	{
   1.157 +		/* Dump this user to memory */
   1.158 +		if (g_CurIdent[0] != '\0' && g_CurAuth[0] != '\0')
   1.159 +		{
   1.160 +			decl AdminFlag:flags[26];
   1.161 +			new AdminId:id, i, num_groups, num_flags;
   1.162 +			
   1.163 +			if ((id = FindAdminByIdentity(g_CurAuth, g_CurIdent)) == INVALID_ADMIN_ID)
   1.164 +			{
   1.165 +				id = CreateAdmin(g_CurName);
   1.166 +				if (!BindAdminIdentity(id, g_CurAuth, g_CurIdent))
   1.167 +				{
   1.168 +					ParseError("Failed to bind auth \"%s\" to identity \"%s\"", g_CurAuth, g_CurIdent);
   1.169 +				}
   1.170 +			}
   1.171 +			
   1.172 +			num_groups = GetArraySize(g_GroupArray);
   1.173 +			for (i = 0; i < num_groups; i++)
   1.174 +			{
   1.175 +				AdminInheritGroup(id, GetArrayCell(g_GroupArray, i));
   1.176 +			}
   1.177 +			
   1.178 +			SetAdminPassword(id, g_CurPass);
   1.179 +			if (GetAdminImmunityLevel(id) < g_CurImmunity)
   1.180 +			{
   1.181 +				SetAdminImmunityLevel(id, g_CurImmunity);
   1.182 +			}
   1.183 +			
   1.184 +			num_flags = FlagBitsToArray(g_CurFlags, flags, sizeof(flags));
   1.185 +			for (i = 0; i < num_flags; i++)
   1.186 +			{
   1.187 +				SetAdminFlag(id, flags[i], true);
   1.188 +			}
   1.189 +		}
   1.190 +		else
   1.191 +		{
   1.192 +			ParseError("Failed to create admin: did you forget either the auth or identity properties?");
   1.193 +		}
   1.194 +		
   1.195 +		g_UserState = USER_STATE_ADMINS;
   1.196 +	}
   1.197 +	else if (g_UserState == USER_STATE_ADMINS)
   1.198 +	{
   1.199 +		g_UserState = USER_STATE_NONE;
   1.200 +	}
   1.201 +	
   1.202 +	return SMCParse_Continue;
   1.203 +}
   1.204 +
   1.205 +public SMCResult:ReadUsers_CurrentLine(Handle:smc, const String:line[], lineno)
   1.206 +{
   1.207 +	g_CurrentLine = lineno;
   1.208 +	
   1.209 +	return SMCParse_Continue;
   1.210 +}
   1.211 +
   1.212 +static InitializeUserParser()
   1.213 +{
   1.214 +	if (g_hUserParser == INVALID_HANDLE)
   1.215 +	{
   1.216 +		g_hUserParser = SMC_CreateParser();
   1.217 +		SMC_SetReaders(g_hUserParser,
   1.218 +					   ReadUsers_NewSection,
   1.219 +					   ReadUsers_KeyValue,
   1.220 +					   ReadUsers_EndSection);
   1.221 +		SMC_SetRawLine(g_hUserParser, ReadUsers_CurrentLine);
   1.222 +		
   1.223 +		g_GroupArray = CreateArray();
   1.224 +	}
   1.225 +}
   1.226 +
   1.227 +ReadUsers()
   1.228 +{
   1.229 +	InitializeUserParser();
   1.230 +	
   1.231 +	BuildPath(Path_SM, g_Filename, sizeof(g_Filename), "configs/admins.cfg");
   1.232 +
   1.233 +	/* Set states */
   1.234 +	InitGlobalStates();
   1.235 +	g_UserState = USER_STATE_NONE;
   1.236 +		
   1.237 +	new SMCError:err = SMC_ParseFile(g_hUserParser, g_Filename);
   1.238 +	if (err != SMCError_Okay)
   1.239 +	{
   1.240 +		decl String:buffer[64];
   1.241 +		if (SMC_GetErrorString(err, buffer, sizeof(buffer)))
   1.242 +		{
   1.243 +			ParseError("%s", buffer);
   1.244 +		}
   1.245 +		else
   1.246 +		{
   1.247 +			ParseError("Fatal parse error");
   1.248 +		}
   1.249 +	}
   1.250 +}