plugins/admin-flatfile/admin-groups.sp
author Phong Dang <teame06@gmail.com>
Sun Jul 29 00:26:18 2007 +0000 (2007-07-29)
changeset 1200 623603b49096
parent 908 cd411e8827c4
child 1279 efb63eb26113
permissions -rw-r--r--
Fixed AskPluginLoad would still allowed plugin to load even if it returned false.
     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 (value[i] < 'a' || value[i] > 'z')
    96 					{
    97 						continue;
    98 					}
    99 					if (!g_FlagsSet[value[i] - 'a'])
   100 					{
   101 						continue;
   102 					}
   103 					flag = g_FlagLetters[value[i] - 'a'];
   104 					SetAdmGroupAddFlag(g_CurGrp, flag, true);
   105 				}
   106 			} else if (StrEqual(key, "immunity")) {
   107 				/* If it's a value we know about, use it */
   108 				if (StrEqual(value, "*"))
   109 				{
   110 					SetAdmGroupImmunity(g_CurGrp, Immunity_Global, true);
   111 				} else if (StrEqual(value, "$")) {
   112 					SetAdmGroupImmunity(g_CurGrp, Immunity_Default, true);
   113 				} else {
   114 					/* If we can't find the group, we'll need to schedule a reparse */
   115 					new GroupId:id = FindAdmGroup(value);
   116 					if (id != INVALID_GROUP_ID)
   117 					{
   118 						SetAdmGroupImmuneFrom(g_CurGrp, id);
   119 					} else {
   120 						g_NeedReparse = true;
   121 					}
   122 				}
   123 			}
   124 		} else if (g_GroupState == GROUP_STATE_OVERRIDES) {
   125 			new OverrideRule:rule = Command_Deny;
   126 			
   127 			if (StrEqual(value, "allow", false))
   128 			{
   129 				rule = Command_Allow;
   130 			}
   131 			
   132 			if (key[0] == '@')
   133 			{
   134 				AddAdmGroupCmdOverride(g_CurGrp, key[1], Override_CommandGroup, rule);
   135 			} else {
   136 				AddAdmGroupCmdOverride(g_CurGrp, key, Override_Command, rule);
   137 			}
   138 		}
   139 	} else if (g_GroupPass == GROUP_PASS_SECOND
   140 			   && g_GroupState == GROUP_STATE_INGROUP) {
   141 		/* Check for immunity again, core should handle double inserts */
   142 		if (StrEqual(key, "immunity"))
   143 		{
   144 			new GroupId:id = FindAdmGroup(value);
   145 			if (id != INVALID_GROUP_ID)
   146 			{
   147 				SetAdmGroupImmuneFrom(g_CurGrp, id);
   148 			} else {
   149 				ParseError("Unable to find group: \"%s\"", value);
   150 			}
   151 		}
   152 	}
   153 	
   154 	return SMCParse_Continue;
   155 }
   156 
   157 public SMCResult:ReadGroups_EndSection(Handle:smc)
   158 {
   159 	/* If we're ignoring, skip out */
   160 	if (g_IgnoreLevel)
   161 	{
   162 		g_IgnoreLevel--;
   163 		return SMCParse_Continue;
   164 	}
   165 	
   166 	if (g_GroupState == GROUP_STATE_OVERRIDES)
   167 	{
   168 		g_GroupState = GROUP_STATE_INGROUP;
   169 	} else if (g_GroupState == GROUP_STATE_INGROUP) {
   170 		g_GroupState = GROUP_STATE_GROUPS;
   171 		g_CurGrp = INVALID_GROUP_ID;
   172 	} else if (g_GroupState == GROUP_STATE_GROUPS) {
   173 		g_GroupState = GROUP_STATE_NONE;
   174 	}
   175 	
   176 	return SMCParse_Continue;
   177 }
   178 
   179 static InitializeGroupParser()
   180 {
   181 	if (g_hGroupParser == INVALID_HANDLE)
   182 	{
   183 		g_hGroupParser = SMC_CreateParser();
   184 		SMC_SetReaders(g_hGroupParser,
   185 					   ReadGroups_NewSection,
   186 					   ReadGroups_KeyValue,
   187 					   ReadGroups_EndSection);
   188 	}
   189 }
   190 
   191 static InternalReadGroups(const String:path[], pass)
   192 {
   193 	/* Set states */
   194 	InitGlobalStates();
   195 	g_GroupState = GROUP_STATE_NONE;
   196 	g_CurGrp = INVALID_GROUP_ID;
   197 	g_GroupPass = pass;
   198 	g_NeedReparse = false;
   199 		
   200 	new SMCError:err = SMC_ParseFile(g_hGroupParser, path);
   201 	if (err != SMCError_Okay)
   202 	{
   203 		decl String:buffer[64];
   204 		if (SMC_GetErrorString(err, buffer, sizeof(buffer)))
   205 		{
   206 			ParseError("%s", buffer);
   207 		} else {
   208 			ParseError("Fatal parse error");
   209 		}
   210 	}
   211 }
   212 
   213 ReadGroups()
   214 {
   215 	InitializeGroupParser();
   216 	
   217 	BuildPath(Path_SM, g_Filename, sizeof(g_Filename), "configs/admin_groups.cfg");
   218 	
   219 	InternalReadGroups(g_Filename, GROUP_PASS_FIRST);
   220 	if (g_NeedReparse)
   221 	{
   222 		InternalReadGroups(g_Filename, GROUP_PASS_SECOND);
   223 	}
   224 }
   225