plugins/admin-flatfile/admin-simple.sp
author David Anderson <dvander@alliedmods.net>
Mon Oct 15 18:51:11 2007 +0000 (2007-10-15)
changeset 1575 408a1174f19a
parent 1403 ecb9c66fd4bb
child 1607 66b03a0e6ba7
permissions -rw-r--r--
filled in data for core's TF2 support
     1 /**
     2  * vim: set ts=4 :
     3  * =============================================================================
     4  * SourceMod Admin File Reader Plugin
     5  * Reads the admins.cfg file.  Do not compile this directly.
     6  *
     7  * SourceMod (C)2004-2007 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 public ReadSimpleUsers()
    35 {
    36 	BuildPath(Path_SM, g_Filename, sizeof(g_Filename), "configs/admins_simple.ini");
    37 	
    38 	new Handle:file = OpenFile(g_Filename, "rt");
    39 	if (file == INVALID_HANDLE)
    40 	{
    41 		ParseError("Could not open file!");
    42 		return;
    43 	}
    44 	
    45 	while (!IsEndOfFile(file))
    46 	{
    47 		decl String:line[255];
    48 		if (!ReadFileLine(file, line, sizeof(line)))
    49 		{
    50 			break;
    51 		}
    52 		
    53 		/* Trim comments */
    54 		new len = strlen(line);
    55 		new bool:ignoring = false;
    56 		for (new i=0; i<len; i++)
    57 		{
    58 			if (ignoring)
    59 			{
    60 				if (line[i] == '"')
    61 				{
    62 					ignoring = false;
    63 				}
    64 			} else {
    65 				if (line[i] == '"')
    66 				{
    67 					ignoring = true;
    68 				} else if (line[i] == ';') {
    69 					line[i] = '\0';
    70 					break;
    71 				} else if (line[i] == '/'
    72 							&& i != len - 1
    73 							&& line[i+1] == '/')
    74 				{
    75 					line[i] = '\0';
    76 					break;
    77 				}
    78 			}
    79 		}
    80 		
    81 		TrimString(line);
    82 		
    83 		if ((line[0] == '/' && line[1] == '/')
    84 			|| (line[0] == ';' || line[0] == '\0'))
    85 		{
    86 			continue;
    87 		}
    88 	
    89 		ReadAdminLine(line);
    90 	}
    91 	
    92 	CloseHandle(file);
    93 }
    94 
    95 ReadAdminLine(const String:line[])
    96 {
    97 	new String:auth[64];
    98 	new cur_idx = BreakString(line, auth, sizeof(auth));
    99 	new idx = cur_idx;
   100 	
   101 	if (cur_idx == -1)
   102 	{
   103 		/* This line is bad... we need at least two parameters */
   104 		return;
   105 	}
   106 	
   107 	/* Create the admin */
   108 	new AdminId:admin = CreateAdmin(auth);
   109 	
   110 	/* Read flags */
   111 	new String:flags[64];	
   112 	cur_idx = BreakString(line[idx], flags, sizeof(flags));
   113 	idx += cur_idx;
   114 
   115 	/* Read immunity level, if any */
   116 	new level, flag_idx;
   117 
   118 	if ((flag_idx = StringToIntEx(flags, level)) > 0)
   119 	{
   120 		SetAdminImmunityLevel(admin, level);
   121 		if (flags[flag_idx] == ':')
   122 		{
   123 			flag_idx++;
   124 		}
   125 	}
   126 
   127 	if (flags[flag_idx] == '@')
   128 	{
   129 		new GroupId:gid = FindAdmGroup(flags[flag_idx + 1]);
   130 		if (gid == INVALID_GROUP_ID)
   131 		{
   132 			ParseError("Invalid group detected: %s", flags[flag_idx + 1]);
   133 			return;
   134 		}
   135 		AdminInheritGroup(admin, gid);
   136 	} else {
   137 		new len = strlen(flags[flag_idx]);
   138 		new bool:is_default = false;
   139 		for (new i=0; i<len; i++)
   140 		{
   141 			if (!level && flags[flag_idx + i] == '$')
   142 			{
   143 				SetAdminImmunityLevel(admin, 1);
   144 			} else {
   145 				new AdminFlag:flag;
   146 				
   147 				if (!FindFlagByChar(flags[flag_idx + i], flag))
   148 				{
   149 					ParseError("Invalid flag detected: %c", flags[flag_idx + i]);
   150 					continue;
   151 				}
   152 				SetAdminFlag(admin, flag, true);
   153 			}
   154 		}
   155 		
   156 		if (is_default)
   157 		{
   158 			new GroupId:gid = FindAdmGroup("Default");
   159 			if (gid != INVALID_GROUP_ID)
   160 			{
   161 				AdminInheritGroup(admin, gid);
   162 			}
   163 		}
   164 	}
   165 	
   166 	/* Lastly, is there a password? */
   167 	if (cur_idx != -1)
   168 	{
   169 		decl String:password[64];
   170 		BreakString(line[idx], password, sizeof(password));
   171 		SetAdminPassword(admin, password);
   172 	}
   173 	
   174 	/* Now, bind the identity to something */
   175 	if (StrContains(auth, "STEAM_") == 0)
   176 	{
   177 		BindAdminIdentity(admin, AUTHMETHOD_STEAM, auth);
   178 	} else {
   179 		if (auth[0] == '!')
   180 		{
   181 			BindAdminIdentity(admin, AUTHMETHOD_IP, auth[1]);
   182 		} else {
   183 			BindAdminIdentity(admin, AUTHMETHOD_NAME, auth);
   184 		}
   185 	}
   186 }
   187