plugins/admin-flatfile/admin-groups.sp
author Scott Ehlert <ds@alliedmods.net>
Tue May 01 05:29:37 2007 +0000 (2007-05-01)
changeset 714 93b19876407e
parent 637 22f19e5a6408
child 908 cd411e8827c4
permissions -rw-r--r--
- Removed GetConVarMin/Max() and replaced them with GetConVarBounds()
- Added SetConVarBounds() to set convar constraints after convar has already been created
     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 
    21 #define GROUP_STATE_NONE		0
    22 #define GROUP_STATE_GROUPS		1
    23 #define GROUP_STATE_INGROUP		2
    24 #define GROUP_STATE_OVERRIDES	3
    25 #define GROUP_PASS_FIRST		1
    26 #define GROUP_PASS_SECOND		2
    27 
    28 static Handle:g_hGroupParser = INVALID_HANDLE;
    29 static GroupId:g_CurGrp = INVALID_GROUP_ID;
    30 static g_GroupState = GROUP_STATE_NONE;
    31 static g_GroupPass = 0;
    32 static bool:g_NeedReparse = false;
    33 
    34 public SMCResult:ReadGroups_NewSection(Handle:smc, const String:name[], bool:opt_quotes)
    35 {
    36 	if (g_IgnoreLevel)
    37 	{
    38 		g_IgnoreLevel++;
    39 		return SMCParse_Continue;
    40 	}
    41 	
    42 	if (g_GroupState == GROUP_STATE_NONE)
    43 	{
    44 		if (StrEqual(name, "Groups"))
    45 		{
    46 			g_GroupState = GROUP_STATE_GROUPS;
    47 		} else {
    48 			g_IgnoreLevel++;
    49 		}
    50 	} else if (g_GroupState == GROUP_STATE_GROUPS) {
    51 		if ((g_CurGrp = CreateAdmGroup(name)) == INVALID_GROUP_ID)
    52 		{
    53 			g_CurGrp = FindAdmGroup(name);
    54 		}
    55 		g_GroupState = GROUP_STATE_INGROUP;
    56 	} else if (g_GroupState == GROUP_STATE_INGROUP) {
    57 		if (StrEqual(name, "Overrides"))
    58 		{
    59 			g_GroupState = GROUP_STATE_OVERRIDES;
    60 		} else {
    61 			g_IgnoreLevel++;
    62 		}
    63 	} else {
    64 		g_IgnoreLevel++;
    65 	}
    66 	
    67 	return SMCParse_Continue;
    68 }
    69 
    70 public SMCResult:ReadGroups_KeyValue(Handle:smc, 
    71 										const String:key[], 
    72 										const String:value[], 
    73 										bool:key_quotes, 
    74 										bool:value_quotes)
    75 {
    76 	if (g_CurGrp == INVALID_GROUP_ID
    77 		|| g_IgnoreLevel)
    78 	{
    79 		return SMCParse_Continue;
    80 	}
    81 	
    82 	new AdminFlag:flag;
    83 	
    84 	if (g_GroupPass == GROUP_PASS_FIRST)
    85 	{
    86 		if (g_GroupState == GROUP_STATE_INGROUP)
    87 		{
    88 			if (StrEqual(key, "flags"))
    89 			{
    90 				new len = strlen(value);
    91 				for (new i=0; i<len; i++)
    92 				{
    93 					if (value[i] < 'a' || value[i] > 'z')
    94 					{
    95 						continue;
    96 					}
    97 					if (!g_FlagsSet[value[i] - 'a'])
    98 					{
    99 						continue;
   100 					}
   101 					flag = g_FlagLetters[value[i] - 'a'];
   102 					SetAdmGroupAddFlag(g_CurGrp, flag, true);
   103 				}
   104 			} else if (StrEqual(key, "immunity")) {
   105 				/* If it's a value we know about, use it */
   106 				if (StrEqual(value, "*"))
   107 				{
   108 					SetAdmGroupImmunity(g_CurGrp, Immunity_Global, true);
   109 				} else if (StrEqual(value, "$")) {
   110 					SetAdmGroupImmunity(g_CurGrp, Immunity_Default, true);
   111 				} else {
   112 					/* If we can't find the group, we'll need to schedule a reparse */
   113 					new GroupId:id = FindAdmGroup(value);
   114 					if (id != INVALID_GROUP_ID)
   115 					{
   116 						SetAdmGroupImmuneFrom(g_CurGrp, id);
   117 					} else {
   118 						g_NeedReparse = true;
   119 					}
   120 				}
   121 			}
   122 		} else if (g_GroupState == GROUP_STATE_OVERRIDES) {
   123 			new OverrideRule:rule = Command_Deny;
   124 			
   125 			if (StrEqual(value, "allow", false))
   126 			{
   127 				rule = Command_Allow;
   128 			}
   129 			
   130 			if (key[0] == '@')
   131 			{
   132 				AddAdmGroupCmdOverride(g_CurGrp, key[1], Override_CommandGroup, rule);
   133 			} else {
   134 				AddAdmGroupCmdOverride(g_CurGrp, key, Override_Command, rule);
   135 			}
   136 		}
   137 	} else if (g_GroupPass == GROUP_PASS_SECOND
   138 			   && g_GroupState == GROUP_STATE_INGROUP) {
   139 		/* Check for immunity again, core should handle double inserts */
   140 		if (StrEqual(key, "immunity"))
   141 		{
   142 			new GroupId:id = FindAdmGroup(value);
   143 			if (id != INVALID_GROUP_ID)
   144 			{
   145 				SetAdmGroupImmuneFrom(g_CurGrp, id);
   146 			} else {
   147 				ParseError("Unable to find group: \"%s\"", value);
   148 			}
   149 		}
   150 	}
   151 	
   152 	return SMCParse_Continue;
   153 }
   154 
   155 public SMCResult:ReadGroups_EndSection(Handle:smc)
   156 {
   157 	/* If we're ignoring, skip out */
   158 	if (g_IgnoreLevel)
   159 	{
   160 		g_IgnoreLevel--;
   161 		return SMCParse_Continue;
   162 	}
   163 	
   164 	if (g_GroupState == GROUP_STATE_OVERRIDES)
   165 	{
   166 		g_GroupState = GROUP_STATE_INGROUP;
   167 	} else if (g_GroupState == GROUP_STATE_INGROUP) {
   168 		g_GroupState = GROUP_STATE_GROUPS;
   169 		g_CurGrp = INVALID_GROUP_ID;
   170 	} else if (g_GroupState == GROUP_STATE_GROUPS) {
   171 		g_GroupState = GROUP_STATE_NONE;
   172 	}
   173 	
   174 	return SMCParse_Continue;
   175 }
   176 
   177 static InitializeGroupParser()
   178 {
   179 	if (g_hGroupParser == INVALID_HANDLE)
   180 	{
   181 		g_hGroupParser = SMC_CreateParser();
   182 		SMC_SetReaders(g_hGroupParser,
   183 					   ReadGroups_NewSection,
   184 					   ReadGroups_KeyValue,
   185 					   ReadGroups_EndSection);
   186 	}
   187 }
   188 
   189 static InternalReadGroups(const String:path[], pass)
   190 {
   191 	/* Set states */
   192 	InitGlobalStates();
   193 	g_GroupState = GROUP_STATE_NONE;
   194 	g_CurGrp = INVALID_GROUP_ID;
   195 	g_GroupPass = pass;
   196 	g_NeedReparse = false;
   197 		
   198 	new SMCError:err = SMC_ParseFile(g_hGroupParser, path);
   199 	if (err != SMCError_Okay)
   200 	{
   201 		decl String:buffer[64];
   202 		if (SMC_GetErrorString(err, buffer, sizeof(buffer)))
   203 		{
   204 			ParseError("%s", buffer);
   205 		} else {
   206 			ParseError("Fatal parse error");
   207 		}
   208 	}
   209 }
   210 
   211 ReadGroups()
   212 {
   213 	InitializeGroupParser();
   214 	
   215 	BuildPath(Path_SM, g_Filename, sizeof(g_Filename), "configs/admin_groups.cfg");
   216 	
   217 	InternalReadGroups(g_Filename, GROUP_PASS_FIRST);
   218 	if (g_NeedReparse)
   219 	{
   220 		InternalReadGroups(g_Filename, GROUP_PASS_SECOND);
   221 	}
   222 }
   223