plugins/admin-flatfile/admin-groups.sp
author David Anderson <dvander@alliedmods.net>
Fri Aug 10 04:53:30 2007 +0000 (2007-08-10)
changeset 1298 9cddbed74581
parent 1279 efb63eb26113
child 1330 ab94316bb52b
permissions -rw-r--r--
flag letter assignment is now handled by core
     1 /**
     2  * admin-groups.sp
     3  * Reads the admin_groups.cfg file.  Do not compile this directly.
     4  * This file is part of SourceMod, Copyright (C) 2004-2007 AlliedModders LLC
     5  *
     6  * This program is free software; you can redistribute it and/or
     7  * modify it under the terms of the GNU General Public License
     8  * as published by the Free Software Foundation; either version 2
     9  * of the License, or (at your option) any later version.
    10  *
    11  * This program is distributed in the hope that it will be useful,
    12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    14  * GNU General Public License for more details.
    15  * 
    16  * You should have received a copy of the GNU General Public License
    17  * along with this program; if not, write to the Free Software
    18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
    19  *
    20  * Version: $Id$
    21  */
    22 
    23 #define GROUP_STATE_NONE		0
    24 #define GROUP_STATE_GROUPS		1
    25 #define GROUP_STATE_INGROUP		2
    26 #define GROUP_STATE_OVERRIDES	3
    27 #define GROUP_PASS_FIRST		1
    28 #define GROUP_PASS_SECOND		2
    29 
    30 static Handle:g_hGroupParser = INVALID_HANDLE;
    31 static GroupId:g_CurGrp = INVALID_GROUP_ID;
    32 static g_GroupState = GROUP_STATE_NONE;
    33 static g_GroupPass = 0;
    34 static bool:g_NeedReparse = false;
    35 
    36 public SMCResult:ReadGroups_NewSection(Handle:smc, const String:name[], bool:opt_quotes)
    37 {
    38 	if (g_IgnoreLevel)
    39 	{
    40 		g_IgnoreLevel++;
    41 		return SMCParse_Continue;
    42 	}
    43 	
    44 	if (g_GroupState == GROUP_STATE_NONE)
    45 	{
    46 		if (StrEqual(name, "Groups"))
    47 		{
    48 			g_GroupState = GROUP_STATE_GROUPS;
    49 		} else {
    50 			g_IgnoreLevel++;
    51 		}
    52 	} else if (g_GroupState == GROUP_STATE_GROUPS) {
    53 		if ((g_CurGrp = CreateAdmGroup(name)) == INVALID_GROUP_ID)
    54 		{
    55 			g_CurGrp = FindAdmGroup(name);
    56 		}
    57 		g_GroupState = GROUP_STATE_INGROUP;
    58 	} else if (g_GroupState == GROUP_STATE_INGROUP) {
    59 		if (StrEqual(name, "Overrides"))
    60 		{
    61 			g_GroupState = GROUP_STATE_OVERRIDES;
    62 		} else {
    63 			g_IgnoreLevel++;
    64 		}
    65 	} else {
    66 		g_IgnoreLevel++;
    67 	}
    68 	
    69 	return SMCParse_Continue;
    70 }
    71 
    72 public SMCResult:ReadGroups_KeyValue(Handle:smc, 
    73 										const String:key[], 
    74 										const String:value[], 
    75 										bool:key_quotes, 
    76 										bool:value_quotes)
    77 {
    78 	if (g_CurGrp == INVALID_GROUP_ID
    79 		|| g_IgnoreLevel)
    80 	{
    81 		return SMCParse_Continue;
    82 	}
    83 	
    84 	new AdminFlag:flag;
    85 	
    86 	if (g_GroupPass == GROUP_PASS_FIRST)
    87 	{
    88 		if (g_GroupState == GROUP_STATE_INGROUP)
    89 		{
    90 			if (StrEqual(key, "flags"))
    91 			{
    92 				new len = strlen(value);
    93 				for (new i=0; i<len; i++)
    94 				{
    95 					if (!FindFlagByChar(value[i], flag))
    96 					{
    97 						continue;
    98 					}
    99 					SetAdmGroupAddFlag(g_CurGrp, flag, true);
   100 				}
   101 			} else if (StrEqual(key, "immunity")) {
   102 				/* If it's a value we know about, use it */
   103 				if (StrEqual(value, "*"))
   104 				{
   105 					SetAdmGroupImmunity(g_CurGrp, Immunity_Global, true);
   106 				} else if (StrEqual(value, "$")) {
   107 					SetAdmGroupImmunity(g_CurGrp, Immunity_Default, true);
   108 				} else {
   109 					/* If we can't find the group, we'll need to schedule a reparse */
   110 					new GroupId:id = FindAdmGroup(value);
   111 					if (id != INVALID_GROUP_ID)
   112 					{
   113 						SetAdmGroupImmuneFrom(g_CurGrp, id);
   114 					} else {
   115 						g_NeedReparse = true;
   116 					}
   117 				}
   118 			}
   119 		} else if (g_GroupState == GROUP_STATE_OVERRIDES) {
   120 			new OverrideRule:rule = Command_Deny;
   121 			
   122 			if (StrEqual(value, "allow", false))
   123 			{
   124 				rule = Command_Allow;
   125 			}
   126 			
   127 			if (key[0] == '@')
   128 			{
   129 				AddAdmGroupCmdOverride(g_CurGrp, key[1], Override_CommandGroup, rule);
   130 			} else {
   131 				AddAdmGroupCmdOverride(g_CurGrp, key, Override_Command, rule);
   132 			}
   133 		}
   134 	} else if (g_GroupPass == GROUP_PASS_SECOND
   135 			   && g_GroupState == GROUP_STATE_INGROUP) {
   136 		/* Check for immunity again, core should handle double inserts */
   137 		if (StrEqual(key, "immunity"))
   138 		{
   139 			if (StrEqual(value, "$"))
   140 			{
   141 				SetAdmGroupImmunity(g_CurGrp, Immunity_Default, true);
   142 			} else if (StrEqual(value, "*")) {
   143 				SetAdmGroupImmunity(g_CurGrp, Immunity_Global, true);
   144 			} else {
   145 				new GroupId:id = FindAdmGroup(value);
   146 				if (id != INVALID_GROUP_ID)
   147 				{
   148 					SetAdmGroupImmuneFrom(g_CurGrp, id);
   149 				} else {
   150 					ParseError("Unable to find group: \"%s\"", value);
   151 				}
   152 			}
   153 		}
   154 	}
   155 	
   156 	return SMCParse_Continue;
   157 }
   158 
   159 public SMCResult:ReadGroups_EndSection(Handle:smc)
   160 {
   161 	/* If we're ignoring, skip out */
   162 	if (g_IgnoreLevel)
   163 	{
   164 		g_IgnoreLevel--;
   165 		return SMCParse_Continue;
   166 	}
   167 	
   168 	if (g_GroupState == GROUP_STATE_OVERRIDES)
   169 	{
   170 		g_GroupState = GROUP_STATE_INGROUP;
   171 	} else if (g_GroupState == GROUP_STATE_INGROUP) {
   172 		g_GroupState = GROUP_STATE_GROUPS;
   173 		g_CurGrp = INVALID_GROUP_ID;
   174 	} else if (g_GroupState == GROUP_STATE_GROUPS) {
   175 		g_GroupState = GROUP_STATE_NONE;
   176 	}
   177 	
   178 	return SMCParse_Continue;
   179 }
   180 
   181 static InitializeGroupParser()
   182 {
   183 	if (g_hGroupParser == INVALID_HANDLE)
   184 	{
   185 		g_hGroupParser = SMC_CreateParser();
   186 		SMC_SetReaders(g_hGroupParser,
   187 					   ReadGroups_NewSection,
   188 					   ReadGroups_KeyValue,
   189 					   ReadGroups_EndSection);
   190 	}
   191 }
   192 
   193 static InternalReadGroups(const String:path[], pass)
   194 {
   195 	/* Set states */
   196 	InitGlobalStates();
   197 	g_GroupState = GROUP_STATE_NONE;
   198 	g_CurGrp = INVALID_GROUP_ID;
   199 	g_GroupPass = pass;
   200 	g_NeedReparse = false;
   201 		
   202 	new SMCError:err = SMC_ParseFile(g_hGroupParser, path);
   203 	if (err != SMCError_Okay)
   204 	{
   205 		decl String:buffer[64];
   206 		if (SMC_GetErrorString(err, buffer, sizeof(buffer)))
   207 		{
   208 			ParseError("%s", buffer);
   209 		} else {
   210 			ParseError("Fatal parse error");
   211 		}
   212 	}
   213 }
   214 
   215 ReadGroups()
   216 {
   217 	InitializeGroupParser();
   218 	
   219 	BuildPath(Path_SM, g_Filename, sizeof(g_Filename), "configs/admin_groups.cfg");
   220 	
   221 	InternalReadGroups(g_Filename, GROUP_PASS_FIRST);
   222 	if (g_NeedReparse)
   223 	{
   224 		InternalReadGroups(g_Filename, GROUP_PASS_SECOND);
   225 	}
   226 }
   227