plugins/admin-flatfile/admin-simple.sp
author David Anderson <dvander@alliedmods.net>
Fri Jul 27 00:28:43 2007 +0000 (2007-07-27)
changeset 1186 5abd46b5b5a6
parent 1107 3789127b580e
child 1268 9360f5ea2289
permissions -rw-r--r--
added ferret's new plugins to the build script
     1 /**
     2  * admin-simple.sp
     3  * Reads the admins.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 public ReadSimpleUsers()
    24 {
    25 	BuildPath(Path_SM, g_Filename, sizeof(g_Filename), "configs/admins_simple.ini");
    26 	
    27 	new Handle:file = OpenFile(g_Filename, "rt");
    28 	if (file == INVALID_HANDLE)
    29 	{
    30 		ParseError("Could not open file!");
    31 		return;
    32 	}
    33 	
    34 	while (!IsEndOfFile(file))
    35 	{
    36 		decl String:line[255];
    37 		if (!ReadFileLine(file, line, sizeof(line)))
    38 		{
    39 			break;
    40 		}
    41 		
    42 		/* Trim comments */
    43 		new len = strlen(line);
    44 		new bool:ignoring = false;
    45 		for (new i=0; i<len; i++)
    46 		{
    47 			if (ignoring)
    48 			{
    49 				if (line[i] == '"')
    50 				{
    51 					ignoring = false;
    52 				}
    53 			} else {
    54 				if (line[i] == '"')
    55 				{
    56 					ignoring = true;
    57 				} else if (line[i] == ';') {
    58 					line[i] = '\0';
    59 					break;
    60 				} else if (line[i] == '/'
    61 							&& i != len - 1
    62 							&& line[i+1] == '/')
    63 				{
    64 					line[i] = '\0';
    65 					break;
    66 				}
    67 			}
    68 		}
    69 		
    70 		TrimString(line);
    71 		
    72 		if ((line[0] == '/' && line[1] == '/')
    73 			|| (line[0] == ';' || line[0] == '\0'))
    74 		{
    75 			continue;
    76 		}
    77 	
    78 		ReadAdminLine(line);
    79 	}
    80 	
    81 	CloseHandle(file);
    82 }
    83 
    84 ReadAdminLine(const String:line[])
    85 {
    86 	new String:auth[64];
    87 	new cur_idx = StrBreak(line, auth, sizeof(auth));
    88 	new idx = cur_idx;
    89 	
    90 	if (cur_idx == -1)
    91 	{
    92 		/* This line is bad... we need at least two parameters */
    93 		return;
    94 	}
    95 	
    96 	/* Create the admin */
    97 	new AdminId:admin = CreateAdmin(auth);
    98 	
    99 	/* Read flags */
   100 	new String:flags[64];	
   101 	cur_idx = StrBreak(line[idx], flags, sizeof(flags));
   102 	idx += cur_idx;
   103 	
   104 	if (flags[0] == '@')
   105 	{
   106 		new GroupId:gid = FindAdmGroup(flags[1]);
   107 		if (gid == INVALID_GROUP_ID)
   108 		{
   109 			ParseError("Invalid group detected: %s", flags[1]);
   110 			return;
   111 		}
   112 		AdminInheritGroup(admin, gid);
   113 	} else {
   114 		new len = strlen(flags);
   115 		new bool:is_default = false;
   116 		for (new i=0; i<len; i++)
   117 		{
   118 			if (flags[i] < 'a' || flags[i] > 'z')
   119 			{
   120 				if (flags[i] == '$')
   121 				{
   122 					is_default = true;
   123 				} else {
   124 					ParseError("Invalid flag detected: %c", flags[i]);
   125 				}
   126 				continue;
   127 			}
   128 			new val = flags[i] - 'a';
   129 			if (!g_FlagsSet[val])
   130 			{
   131 				ParseError("Invalid flag detected: %c", flags[i]);
   132 				continue;
   133 			}
   134 			SetAdminFlag(admin, g_FlagLetters[val], true);
   135 		}
   136 		
   137 		if (is_default)
   138 		{
   139 			new GroupId:gid = FindAdmGroup("Default");
   140 			if (gid != INVALID_GROUP_ID)
   141 			{
   142 				AdminInheritGroup(admin, gid);
   143 			}
   144 		}
   145 	}
   146 	
   147 	/* Lastly, is there a password? */
   148 	if (cur_idx != -1)
   149 	{
   150 		decl String:password[64];
   151 		StrBreak(line[idx], password, sizeof(password));
   152 		SetAdminPassword(admin, password);
   153 	}
   154 	
   155 	/* Now, bind the identity to something */
   156 	if (StrContains(auth, "STEAM_") == 0)
   157 	{
   158 		BindAdminIdentity(admin, AUTHMETHOD_STEAM, auth);
   159 	} else {
   160 		if (auth[0] == '!')
   161 		{
   162 			BindAdminIdentity(admin, AUTHMETHOD_IP, auth[1]);
   163 		} else {
   164 			BindAdminIdentity(admin, AUTHMETHOD_NAME, auth);
   165 		}
   166 	}
   167 }