plugins/admin-flatfile/admin-levels.sp
author Scott Ehlert <ds@alliedmods.net>
Sun Jul 15 08:00:26 2007 +0000 (2007-07-15)
changeset 1116 40cc5e9f0f1b
parent 908 cd411e8827c4
permissions -rw-r--r--
Removed strange debugging code
     1 /* 
     2  * admin-levels.sp
     3  * Reads access flags from the admin_levels.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 LEVEL_STATE_NONE		0
    24 #define LEVEL_STATE_LEVELS		1
    25 #define LEVEL_STATE_FLAGS		2
    26 
    27 static Handle:g_hLevelParser = INVALID_HANDLE;
    28 static g_LevelState = LEVEL_STATE_NONE;
    29 
    30 /* :TODO: log line numbers? */
    31 
    32 LoadDefaultLetters()
    33 {
    34 	for (new i='t'; i<'z'; i++)
    35 	{
    36 		g_FlagsSet[i-'a'] = false;
    37 	}
    38 	
    39 	g_FlagLetters['a'-'a'] = Admin_Reservation;
    40 	g_FlagLetters['b'-'a'] = Admin_Generic;
    41 	g_FlagLetters['c'-'a'] = Admin_Kick;
    42 	g_FlagLetters['d'-'a'] = Admin_Ban;
    43 	g_FlagLetters['e'-'a'] = Admin_Unban;
    44 	g_FlagLetters['f'-'a'] = Admin_Slay;
    45 	g_FlagLetters['g'-'a'] = Admin_Changemap;
    46 	g_FlagLetters['h'-'a'] = Admin_Convars;
    47 	g_FlagLetters['i'-'a'] = Admin_Config;
    48 	g_FlagLetters['j'-'a'] = Admin_Chat;
    49 	g_FlagLetters['k'-'a'] = Admin_Vote;
    50 	g_FlagLetters['l'-'a'] = Admin_Password;
    51 	g_FlagLetters['m'-'a'] = Admin_RCON;
    52 	g_FlagLetters['n'-'a'] = Admin_Cheats;
    53 	g_FlagLetters['o'-'a'] = Admin_Custom1;
    54 	g_FlagLetters['p'-'a'] = Admin_Custom2;
    55 	g_FlagLetters['q'-'a'] = Admin_Custom3;
    56 	g_FlagLetters['r'-'a'] = Admin_Custom4;
    57 	g_FlagLetters['s'-'a'] = Admin_Custom5;
    58 	g_FlagLetters['t'-'a'] = Admin_Custom6;
    59 	g_FlagLetters['z'-'a'] = Admin_Root;
    60 }
    61 
    62 public SMCResult:ReadLevels_NewSection(Handle:smc, const String:name[], bool:opt_quotes)
    63 {
    64 	if (g_IgnoreLevel)
    65 	{
    66 		g_IgnoreLevel++;
    67 		return SMCParse_Continue;
    68 	}
    69 	
    70 	if (g_LevelState == LEVEL_STATE_NONE)
    71 	{
    72 		if (StrEqual(name, "Levels"))
    73 		{
    74 			g_LevelState = LEVEL_STATE_LEVELS;
    75 		} else {
    76 			g_IgnoreLevel++;
    77 		}
    78 	} else if (g_LevelState == LEVEL_STATE_LEVELS) {
    79 		if (StrEqual(name, "Flags"))
    80 		{
    81 			g_LevelState = LEVEL_STATE_FLAGS;
    82 		} else {
    83 			g_IgnoreLevel++;
    84 		}
    85 	} else {
    86 		g_IgnoreLevel++;
    87 	}
    88 	
    89 	return SMCParse_Continue;
    90 }
    91 
    92 public SMCResult:ReadLevels_KeyValue(Handle:smc, const String:key[], const String:value[], bool:key_quotes, bool:value_quotes)
    93 {
    94 	if (g_LevelState == LEVEL_STATE_FLAGS && !g_IgnoreLevel)
    95 	{
    96 		new chr = value[0];
    97 		
    98 		if (chr < 'a' || chr > 'z')
    99 		{
   100 			ParseError("Unrecognized character: \"%s\"", value);
   101 			return SMCParse_Continue;
   102 		}
   103 		
   104 		chr -= 'a';
   105 		
   106 		new AdminFlag:flag;
   107 		
   108 		if (StrEqual(key, "reservation"))
   109 		{
   110 			flag = Admin_Reservation;
   111 		} else if (StrEqual(key, "kick")) {
   112 			flag = Admin_Kick;
   113 		} else if (StrEqual(key, "generic")) {
   114 			flag = Admin_Generic;
   115 		} else if (StrEqual(key, "ban")) {
   116 			flag = Admin_Ban;
   117 		} else if (StrEqual(key, "unban")) {
   118 			flag = Admin_Unban;
   119 		} else if (StrEqual(key, "slay")) {
   120 			flag = Admin_Slay;
   121 		} else if (StrEqual(key, "changemap")) {
   122 			flag = Admin_Changemap;
   123 		} else if (StrEqual(key, "cvars")) {
   124 			flag = Admin_Convars;
   125 		} else if (StrEqual(key, "config")) {
   126 			flag = Admin_Config;
   127 		} else if (StrEqual(key, "chat")) {
   128 			flag = Admin_Chat;
   129 		} else if (StrEqual(key, "vote")) {
   130 			flag = Admin_Vote;
   131 		} else if (StrEqual(key, "password")) {
   132 			flag = Admin_Password;
   133 		} else if (StrEqual(key, "rcon")) {
   134 			flag = Admin_RCON;
   135 		} else if (StrEqual(key, "cheats")) {
   136 			flag = Admin_Cheats;
   137 		} else if (StrEqual(key, "root")) {
   138 			flag = Admin_Root;
   139 		} else if (StrEqual(key, "custom1")) {
   140 			flag = Admin_Custom1;
   141 		} else if (StrEqual(key, "custom2")) {
   142 			flag = Admin_Custom2;
   143 		} else if (StrEqual(key, "custom3")) {
   144 			flag = Admin_Custom3;
   145 		} else if (StrEqual(key, "custom4")) {
   146 			flag = Admin_Custom4;
   147 		} else if (StrEqual(key, "custom5")) {
   148 			flag = Admin_Custom5;
   149 		} else if (StrEqual(key, "custom6")) {
   150 			flag = Admin_Custom6;
   151 		} else {
   152 			ParseError("Unrecognized flag type: %s", key);
   153 		}
   154 		
   155 		g_FlagLetters[chr] = flag;
   156 		g_FlagsSet[chr] = true;
   157 	}
   158 	
   159 	return SMCParse_Continue;
   160 }
   161 
   162 public SMCResult:ReadLevels_EndSection(Handle:smc)
   163 {
   164 	/* If we're ignoring, skip out */
   165 	if (g_IgnoreLevel)
   166 	{
   167 		g_IgnoreLevel--;
   168 		return SMCParse_Continue;
   169 	}
   170 	
   171 	if (g_LevelState == LEVEL_STATE_FLAGS)
   172 	{
   173 		/* We're totally done parsing */
   174 		g_LevelState = LEVEL_STATE_LEVELS;
   175 		return SMCParse_Halt;
   176 	} else if (g_LevelState == LEVEL_STATE_LEVELS) {
   177 		g_LevelState = LEVEL_STATE_NONE;
   178 	}
   179 	
   180 	return SMCParse_Continue;
   181 }
   182 
   183 static InitializeLevelParser()
   184 {
   185 	if (g_hLevelParser == INVALID_HANDLE)
   186 	{
   187 		g_hLevelParser = SMC_CreateParser();
   188 		SMC_SetReaders(g_hLevelParser, 
   189 				   	ReadLevels_NewSection,
   190 				   	ReadLevels_KeyValue,
   191 				   	ReadLevels_EndSection);
   192 	}
   193 }
   194 
   195 RefreshLevels()
   196 {
   197 	LoadDefaultLetters();
   198 	InitializeLevelParser();
   199 	
   200 	BuildPath(Path_SM, g_Filename, sizeof(g_Filename), "configs/admin_levels.cfg");
   201 	
   202 	/* Set states */
   203 	InitGlobalStates();
   204 	g_LevelState = LEVEL_STATE_NONE;
   205 		
   206 	new SMCError:err = SMC_ParseFile(g_hLevelParser, g_Filename);
   207 	if (err != SMCError_Okay)
   208 	{
   209 		decl String:buffer[64];
   210 		if (SMC_GetErrorString(err, buffer, sizeof(buffer)))
   211 		{
   212 			ParseError("%s", buffer);
   213 		} else {
   214 			ParseError("Fatal parse error");
   215 		}
   216 	}
   217 }