plugins/admin-flatfile/admin-groups.sp
changeset 1952 b7aa2b626807
child 2289 44045ead61cd
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/plugins/admin-flatfile/admin-groups.sp	Sun Mar 30 07:00:22 2008 +0000
     1.3 @@ -0,0 +1,244 @@
     1.4 +/**
     1.5 + * vim: set ts=4 :
     1.6 + * =============================================================================
     1.7 + * SourceMod Admin File Reader Plugin
     1.8 + * Reads the admin_groups.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 GROUP_STATE_NONE		0
    1.38 +#define GROUP_STATE_GROUPS		1
    1.39 +#define GROUP_STATE_INGROUP		2
    1.40 +#define GROUP_STATE_OVERRIDES	3
    1.41 +#define GROUP_PASS_FIRST		1
    1.42 +#define GROUP_PASS_SECOND		2
    1.43 +
    1.44 +static Handle:g_hGroupParser = INVALID_HANDLE;
    1.45 +static GroupId:g_CurGrp = INVALID_GROUP_ID;
    1.46 +static g_GroupState = GROUP_STATE_NONE;
    1.47 +static g_GroupPass = 0;
    1.48 +static bool:g_NeedReparse = false;
    1.49 +
    1.50 +public SMCResult:ReadGroups_NewSection(Handle:smc, const String:name[], bool:opt_quotes)
    1.51 +{
    1.52 +	if (g_IgnoreLevel)
    1.53 +	{
    1.54 +		g_IgnoreLevel++;
    1.55 +		return SMCParse_Continue;
    1.56 +	}
    1.57 +	
    1.58 +	if (g_GroupState == GROUP_STATE_NONE)
    1.59 +	{
    1.60 +		if (StrEqual(name, "Groups"))
    1.61 +		{
    1.62 +			g_GroupState = GROUP_STATE_GROUPS;
    1.63 +		} else {
    1.64 +			g_IgnoreLevel++;
    1.65 +		}
    1.66 +	} else if (g_GroupState == GROUP_STATE_GROUPS) {
    1.67 +		if ((g_CurGrp = CreateAdmGroup(name)) == INVALID_GROUP_ID)
    1.68 +		{
    1.69 +			g_CurGrp = FindAdmGroup(name);
    1.70 +		}
    1.71 +		g_GroupState = GROUP_STATE_INGROUP;
    1.72 +	} else if (g_GroupState == GROUP_STATE_INGROUP) {
    1.73 +		if (StrEqual(name, "Overrides"))
    1.74 +		{
    1.75 +			g_GroupState = GROUP_STATE_OVERRIDES;
    1.76 +		} else {
    1.77 +			g_IgnoreLevel++;
    1.78 +		}
    1.79 +	} else {
    1.80 +		g_IgnoreLevel++;
    1.81 +	}
    1.82 +	
    1.83 +	return SMCParse_Continue;
    1.84 +}
    1.85 +
    1.86 +public SMCResult:ReadGroups_KeyValue(Handle:smc, 
    1.87 +										const String:key[], 
    1.88 +										const String:value[], 
    1.89 +										bool:key_quotes, 
    1.90 +										bool:value_quotes)
    1.91 +{
    1.92 +	if (g_CurGrp == INVALID_GROUP_ID
    1.93 +		|| g_IgnoreLevel)
    1.94 +	{
    1.95 +		return SMCParse_Continue;
    1.96 +	}
    1.97 +	
    1.98 +	new AdminFlag:flag;
    1.99 +	
   1.100 +	if (g_GroupPass == GROUP_PASS_FIRST)
   1.101 +	{
   1.102 +		if (g_GroupState == GROUP_STATE_INGROUP)
   1.103 +		{
   1.104 +			if (StrEqual(key, "flags"))
   1.105 +			{
   1.106 +				new len = strlen(value);
   1.107 +				for (new i=0; i<len; i++)
   1.108 +				{
   1.109 +					if (!FindFlagByChar(value[i], flag))
   1.110 +					{
   1.111 +						continue;
   1.112 +					}
   1.113 +					SetAdmGroupAddFlag(g_CurGrp, flag, true);
   1.114 +				}
   1.115 +			} else if (StrEqual(key, "immunity")) {
   1.116 +				g_NeedReparse = true;
   1.117 +			}
   1.118 +		} else if (g_GroupState == GROUP_STATE_OVERRIDES) {
   1.119 +			new OverrideRule:rule = Command_Deny;
   1.120 +			
   1.121 +			if (StrEqual(value, "allow", false))
   1.122 +			{
   1.123 +				rule = Command_Allow;
   1.124 +			}
   1.125 +			
   1.126 +			if (key[0] == '@')
   1.127 +			{
   1.128 +				AddAdmGroupCmdOverride(g_CurGrp, key[1], Override_CommandGroup, rule);
   1.129 +			} else {
   1.130 +				AddAdmGroupCmdOverride(g_CurGrp, key, Override_Command, rule);
   1.131 +			}
   1.132 +		}
   1.133 +	} else if (g_GroupPass == GROUP_PASS_SECOND
   1.134 +			   && g_GroupState == GROUP_STATE_INGROUP) {
   1.135 +		/* Check for immunity again, core should handle double inserts */
   1.136 +		if (StrEqual(key, "immunity"))
   1.137 +		{
   1.138 +			/* If it's a value we know about, use it */
   1.139 +			if (StrEqual(value, "*"))
   1.140 +			{
   1.141 +				SetAdmGroupImmunityLevel(g_CurGrp, 2);
   1.142 +			} else if (StrEqual(value, "$")) {
   1.143 +				SetAdmGroupImmunityLevel(g_CurGrp, 1);
   1.144 +			} else {
   1.145 +				new level;
   1.146 +				if (StringToIntEx(value, level))
   1.147 +				{
   1.148 +					SetAdmGroupImmunityLevel(g_CurGrp, level);
   1.149 +				} else {
   1.150 +					new GroupId:id;
   1.151 +					if (value[0] == '@')
   1.152 +					{
   1.153 +						id = FindAdmGroup(value[1]);
   1.154 +					} else {
   1.155 +						id = FindAdmGroup(value);
   1.156 +					}
   1.157 +					if (id != INVALID_GROUP_ID)
   1.158 +					{
   1.159 +						SetAdmGroupImmuneFrom(g_CurGrp, id);
   1.160 +					} else {
   1.161 +						ParseError("Unable to find group: \"%s\"", value);
   1.162 +					}
   1.163 +				}
   1.164 +			}
   1.165 +		}
   1.166 +	}
   1.167 +	
   1.168 +	return SMCParse_Continue;
   1.169 +}
   1.170 +
   1.171 +public SMCResult:ReadGroups_EndSection(Handle:smc)
   1.172 +{
   1.173 +	/* If we're ignoring, skip out */
   1.174 +	if (g_IgnoreLevel)
   1.175 +	{
   1.176 +		g_IgnoreLevel--;
   1.177 +		return SMCParse_Continue;
   1.178 +	}
   1.179 +	
   1.180 +	if (g_GroupState == GROUP_STATE_OVERRIDES)
   1.181 +	{
   1.182 +		g_GroupState = GROUP_STATE_INGROUP;
   1.183 +	} else if (g_GroupState == GROUP_STATE_INGROUP) {
   1.184 +		g_GroupState = GROUP_STATE_GROUPS;
   1.185 +		g_CurGrp = INVALID_GROUP_ID;
   1.186 +	} else if (g_GroupState == GROUP_STATE_GROUPS) {
   1.187 +		g_GroupState = GROUP_STATE_NONE;
   1.188 +	}
   1.189 +	
   1.190 +	return SMCParse_Continue;
   1.191 +}
   1.192 +
   1.193 +public SMCResult:ReadGroups_CurrentLine(Handle:smc, const String:line[], lineno)
   1.194 +{
   1.195 +	g_CurrentLine = lineno;
   1.196 +	
   1.197 +	return SMCParse_Continue;
   1.198 +}
   1.199 +
   1.200 +static InitializeGroupParser()
   1.201 +{
   1.202 +	if (g_hGroupParser == INVALID_HANDLE)
   1.203 +	{
   1.204 +		g_hGroupParser = SMC_CreateParser();
   1.205 +		SMC_SetReaders(g_hGroupParser,
   1.206 +					   ReadGroups_NewSection,
   1.207 +					   ReadGroups_KeyValue,
   1.208 +					   ReadGroups_EndSection);
   1.209 +		SMC_SetRawLine(g_hGroupParser, ReadGroups_CurrentLine);
   1.210 +	}
   1.211 +}
   1.212 +
   1.213 +static InternalReadGroups(const String:path[], pass)
   1.214 +{
   1.215 +	/* Set states */
   1.216 +	InitGlobalStates();
   1.217 +	g_GroupState = GROUP_STATE_NONE;
   1.218 +	g_CurGrp = INVALID_GROUP_ID;
   1.219 +	g_GroupPass = pass;
   1.220 +	g_NeedReparse = false;
   1.221 +		
   1.222 +	new SMCError:err = SMC_ParseFile(g_hGroupParser, path);
   1.223 +	if (err != SMCError_Okay)
   1.224 +	{
   1.225 +		decl String:buffer[64];
   1.226 +		if (SMC_GetErrorString(err, buffer, sizeof(buffer)))
   1.227 +		{
   1.228 +			ParseError("%s", buffer);
   1.229 +		} else {
   1.230 +			ParseError("Fatal parse error");
   1.231 +		}
   1.232 +	}
   1.233 +}
   1.234 +
   1.235 +ReadGroups()
   1.236 +{
   1.237 +	InitializeGroupParser();
   1.238 +	
   1.239 +	BuildPath(Path_SM, g_Filename, sizeof(g_Filename), "configs/admin_groups.cfg");
   1.240 +	
   1.241 +	InternalReadGroups(g_Filename, GROUP_PASS_FIRST);
   1.242 +	if (g_NeedReparse)
   1.243 +	{
   1.244 +		InternalReadGroups(g_Filename, GROUP_PASS_SECOND);
   1.245 +	}
   1.246 +}
   1.247 +