plugins/admin-flatfile/admin-groups.sp
author David Anderson <dvander@alliedmods.net>
Fri Jul 11 05:14:32 2008 +0000 (2008-07-11)
branchrefac-jit
changeset 2190 880e81f30b5e
parent 2168 c24a6fa957db
child 2289 44045ead61cd
permissions -rw-r--r--
GetContext() now returns 'this' as a hack (shouldn't+can't be accessed anyway)
     1 /**
     2  * vim: set ts=4 :
     3  * =============================================================================
     4  * SourceMod Admin File Reader Plugin
     5  * Reads the admin_groups.cfg file.  Do not compile this directly.
     6  *
     7  * SourceMod (C)2004-2008 AlliedModders LLC.  All rights reserved.
     8  * =============================================================================
     9  *
    10  * This program is free software; you can redistribute it and/or modify it under
    11  * the terms of the GNU General Public License, version 3.0, as published by the
    12  * Free Software Foundation.
    13  * 
    14  * This program is distributed in the hope that it will be useful, but WITHOUT
    15  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    16  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
    17  * details.
    18  *
    19  * You should have received a copy of the GNU General Public License along with
    20  * this program.  If not, see <http://www.gnu.org/licenses/>.
    21  *
    22  * As a special exception, AlliedModders LLC gives you permission to link the
    23  * code of this program (as well as its derivative works) to "Half-Life 2," the
    24  * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
    25  * by the Valve Corporation.  You must obey the GNU General Public License in
    26  * all respects for all other code used.  Additionally, AlliedModders LLC grants
    27  * this exception to all derivative works.  AlliedModders LLC defines further
    28  * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
    29  * or <http://www.sourcemod.net/license.php>.
    30  *
    31  * Version: $Id$
    32  */
    33 
    34 #define GROUP_STATE_NONE		0
    35 #define GROUP_STATE_GROUPS		1
    36 #define GROUP_STATE_INGROUP		2
    37 #define GROUP_STATE_OVERRIDES	3
    38 #define GROUP_PASS_FIRST		1
    39 #define GROUP_PASS_SECOND		2
    40 
    41 static Handle:g_hGroupParser = INVALID_HANDLE;
    42 static GroupId:g_CurGrp = INVALID_GROUP_ID;
    43 static g_GroupState = GROUP_STATE_NONE;
    44 static g_GroupPass = 0;
    45 static bool:g_NeedReparse = false;
    46 
    47 public SMCResult:ReadGroups_NewSection(Handle:smc, const String:name[], bool:opt_quotes)
    48 {
    49 	if (g_IgnoreLevel)
    50 	{
    51 		g_IgnoreLevel++;
    52 		return SMCParse_Continue;
    53 	}
    54 	
    55 	if (g_GroupState == GROUP_STATE_NONE)
    56 	{
    57 		if (StrEqual(name, "Groups"))
    58 		{
    59 			g_GroupState = GROUP_STATE_GROUPS;
    60 		} else {
    61 			g_IgnoreLevel++;
    62 		}
    63 	} else if (g_GroupState == GROUP_STATE_GROUPS) {
    64 		if ((g_CurGrp = CreateAdmGroup(name)) == INVALID_GROUP_ID)
    65 		{
    66 			g_CurGrp = FindAdmGroup(name);
    67 		}
    68 		g_GroupState = GROUP_STATE_INGROUP;
    69 	} else if (g_GroupState == GROUP_STATE_INGROUP) {
    70 		if (StrEqual(name, "Overrides"))
    71 		{
    72 			g_GroupState = GROUP_STATE_OVERRIDES;
    73 		} else {
    74 			g_IgnoreLevel++;
    75 		}
    76 	} else {
    77 		g_IgnoreLevel++;
    78 	}
    79 	
    80 	return SMCParse_Continue;
    81 }
    82 
    83 public SMCResult:ReadGroups_KeyValue(Handle:smc, 
    84 										const String:key[], 
    85 										const String:value[], 
    86 										bool:key_quotes, 
    87 										bool:value_quotes)
    88 {
    89 	if (g_CurGrp == INVALID_GROUP_ID
    90 		|| g_IgnoreLevel)
    91 	{
    92 		return SMCParse_Continue;
    93 	}
    94 	
    95 	new AdminFlag:flag;
    96 	
    97 	if (g_GroupPass == GROUP_PASS_FIRST)
    98 	{
    99 		if (g_GroupState == GROUP_STATE_INGROUP)
   100 		{
   101 			if (StrEqual(key, "flags"))
   102 			{
   103 				new len = strlen(value);
   104 				for (new i=0; i<len; i++)
   105 				{
   106 					if (!FindFlagByChar(value[i], flag))
   107 					{
   108 						continue;
   109 					}
   110 					SetAdmGroupAddFlag(g_CurGrp, flag, true);
   111 				}
   112 			} else if (StrEqual(key, "immunity")) {
   113 				g_NeedReparse = true;
   114 			}
   115 		} else if (g_GroupState == GROUP_STATE_OVERRIDES) {
   116 			new OverrideRule:rule = Command_Deny;
   117 			
   118 			if (StrEqual(value, "allow", false))
   119 			{
   120 				rule = Command_Allow;
   121 			}
   122 			
   123 			if (key[0] == '@')
   124 			{
   125 				AddAdmGroupCmdOverride(g_CurGrp, key[1], Override_CommandGroup, rule);
   126 			} else {
   127 				AddAdmGroupCmdOverride(g_CurGrp, key, Override_Command, rule);
   128 			}
   129 		}
   130 	} else if (g_GroupPass == GROUP_PASS_SECOND
   131 			   && g_GroupState == GROUP_STATE_INGROUP) {
   132 		/* Check for immunity again, core should handle double inserts */
   133 		if (StrEqual(key, "immunity"))
   134 		{
   135 			/* If it's a value we know about, use it */
   136 			if (StrEqual(value, "*"))
   137 			{
   138 				SetAdmGroupImmunityLevel(g_CurGrp, 2);
   139 			} else if (StrEqual(value, "$")) {
   140 				SetAdmGroupImmunityLevel(g_CurGrp, 1);
   141 			} else {
   142 				new level;
   143 				if (StringToIntEx(value, level))
   144 				{
   145 					SetAdmGroupImmunityLevel(g_CurGrp, level);
   146 				} else {
   147 					new GroupId:id;
   148 					if (value[0] == '@')
   149 					{
   150 						id = FindAdmGroup(value[1]);
   151 					} else {
   152 						id = FindAdmGroup(value);
   153 					}
   154 					if (id != INVALID_GROUP_ID)
   155 					{
   156 						SetAdmGroupImmuneFrom(g_CurGrp, id);
   157 					} else {
   158 						ParseError("Unable to find group: \"%s\"", value);
   159 					}
   160 				}
   161 			}
   162 		}
   163 	}
   164 	
   165 	return SMCParse_Continue;
   166 }
   167 
   168 public SMCResult:ReadGroups_EndSection(Handle:smc)
   169 {
   170 	/* If we're ignoring, skip out */
   171 	if (g_IgnoreLevel)
   172 	{
   173 		g_IgnoreLevel--;
   174 		return SMCParse_Continue;
   175 	}
   176 	
   177 	if (g_GroupState == GROUP_STATE_OVERRIDES)
   178 	{
   179 		g_GroupState = GROUP_STATE_INGROUP;
   180 	} else if (g_GroupState == GROUP_STATE_INGROUP) {
   181 		g_GroupState = GROUP_STATE_GROUPS;
   182 		g_CurGrp = INVALID_GROUP_ID;
   183 	} else if (g_GroupState == GROUP_STATE_GROUPS) {
   184 		g_GroupState = GROUP_STATE_NONE;
   185 	}
   186 	
   187 	return SMCParse_Continue;
   188 }
   189 
   190 public SMCResult:ReadGroups_CurrentLine(Handle:smc, const String:line[], lineno)
   191 {
   192 	g_CurrentLine = lineno;
   193 	
   194 	return SMCParse_Continue;
   195 }
   196 
   197 static InitializeGroupParser()
   198 {
   199 	if (g_hGroupParser == INVALID_HANDLE)
   200 	{
   201 		g_hGroupParser = SMC_CreateParser();
   202 		SMC_SetReaders(g_hGroupParser,
   203 					   ReadGroups_NewSection,
   204 					   ReadGroups_KeyValue,
   205 					   ReadGroups_EndSection);
   206 		SMC_SetRawLine(g_hGroupParser, ReadGroups_CurrentLine);
   207 	}
   208 }
   209 
   210 static InternalReadGroups(const String:path[], pass)
   211 {
   212 	/* Set states */
   213 	InitGlobalStates();
   214 	g_GroupState = GROUP_STATE_NONE;
   215 	g_CurGrp = INVALID_GROUP_ID;
   216 	g_GroupPass = pass;
   217 	g_NeedReparse = false;
   218 		
   219 	new SMCError:err = SMC_ParseFile(g_hGroupParser, path);
   220 	if (err != SMCError_Okay)
   221 	{
   222 		decl String:buffer[64];
   223 		if (SMC_GetErrorString(err, buffer, sizeof(buffer)))
   224 		{
   225 			ParseError("%s", buffer);
   226 		} else {
   227 			ParseError("Fatal parse error");
   228 		}
   229 	}
   230 }
   231 
   232 ReadGroups()
   233 {
   234 	InitializeGroupParser();
   235 	
   236 	BuildPath(Path_SM, g_Filename, sizeof(g_Filename), "configs/admin_groups.cfg");
   237 	
   238 	InternalReadGroups(g_Filename, GROUP_PASS_FIRST);
   239 	if (g_NeedReparse)
   240 	{
   241 		InternalReadGroups(g_Filename, GROUP_PASS_SECOND);
   242 	}
   243 }
   244