2 * admin-sql-prefetch.sp
3 * Prefetches admins from an SQL database without threading.
4 * This file is part of SourceMod, Copyright (C) 2004-2007 AlliedModders LLC
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.
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.
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.
23 /* We like semicolons */
28 public Plugin:myinfo =
30 name = "SQL Admins (Prefetch)",
31 author = "AlliedModders LLC",
32 description = "Reads all admins from SQL",
33 version = SOURCEMOD_VERSION,
34 url = "http://www.sourcemod.net/"
37 public OnRebuildAdminCache(AdminCachePart:part)
39 /* First try to get a database connection */
40 decl String:error[255];
43 if (SQL_CheckConfig("admins"))
45 db = SQL_Connect("admins", true, error, sizeof(error));
47 db = SQL_Connect("default", true, error, sizeof(error));
50 if (db == INVALID_HANDLE)
52 LogError("Could not connect to database \"default\": %s", error);
56 if (part == AdminCache_Overrides)
59 } else if (part == AdminCache_Groups) {
61 } else if (part == AdminCache_Admins) {
68 FetchUserGroups(Handle:hQuery, AdminId:adm, id)
70 SQL_BindParamInt(hQuery, 0, id);
71 if (!SQL_Execute(hQuery))
73 decl String:error[255];
74 SQL_GetError(hQuery, error, sizeof(error));
75 LogError("FetchUserGroups() statement execute failed: %s", error);
81 while (SQL_FetchRow(hQuery))
83 SQL_FetchString(hQuery, 0, name, sizeof(name));
85 if ((gid = FindAdmGroup(name)) == INVALID_GROUP_ID)
87 /* Group wasn't found, don't bother with it. */
92 PrintToServer("Found admin group (%d, %d, %d)", id, gid, adm);
95 AdminInheritGroup(adm, gid);
101 decl String:query[255], String:error[255];
102 new Handle:hQuery, Handle:hGroupQuery;
104 Format(query, sizeof(query), "SELECT id, authtype, identity, passwd, flags, name FROM sm_admins");
105 if ((hQuery = SQL_Query(db, query)) == INVALID_HANDLE)
107 SQL_GetError(db, error, sizeof(error));
108 LogError("FetchUsers() query failed: %s", query);
109 LogError("Query error: %s", error);
113 /* If this fails it's non-fatal */
116 "SELECT g.name FROM sm_admins_groups ag JOIN sm_groups g ON ag.group_id = g.id WHERE ag.admin_id = ? ORDER BY inherit_order ASC");
117 if ((hGroupQuery = SQL_PrepareQuery(db, query, error, sizeof(error))) == INVALID_HANDLE)
119 LogError("FetchUsers() group query preparation failed: %s", query);
120 LogError("Query error: %s", error);
123 decl String:authtype[16];
124 decl String:identity[80];
125 decl String:password[80];
126 decl String:flags[32];
127 decl String:name[80];
130 while (SQL_FetchRow(hQuery))
132 id = SQL_FetchInt(hQuery, 0);
133 SQL_FetchString(hQuery, 1, authtype, sizeof(authtype));
134 SQL_FetchString(hQuery, 2, identity, sizeof(identity));
135 SQL_FetchString(hQuery, 3, password, sizeof(password));
136 SQL_FetchString(hQuery, 4, flags, sizeof(flags));
137 SQL_FetchString(hQuery, 5, name, sizeof(name));
139 /* Use a pre-existing admin if we can */
140 if ((adm = FindAdminByIdentity(authtype, identity)) == INVALID_ADMIN_ID)
142 adm = CreateAdmin(name);
143 if (!BindAdminIdentity(adm, authtype, identity))
145 LogError("Could not bind prefetched SQL admin (authtype \"%s\") (identity \"%s\")", authtype, identity);
151 PrintToServer("Found SQL admin (%d,%s,%s,%s,%s,%s):%d", id, authtype, identity, password, flags, name, adm);
154 /* See if this admin wants a password */
155 if (password[0] != '\0')
157 SetAdminPassword(adm, password);
160 /* Apply each flag */
161 new len = strlen(flags);
163 for (new i=0; i<len; i++)
165 if (!FindFlagByChar(flags[i], flag))
169 SetAdminFlag(adm, flag, true);
173 if (hGroupQuery != INVALID_HANDLE)
175 FetchUserGroups(hGroupQuery, adm, id);
179 CloseHandle(hGroupQuery);
183 FetchGroups(Handle:db)
185 decl String:query[255];
188 Format(query, sizeof(query), "SELECT id, immunity, flags, name, groups_immune FROM sm_groups");
190 if ((hQuery = SQL_Query(db, query)) == INVALID_HANDLE)
192 decl String:error[255];
193 SQL_GetError(db, error, sizeof(error));
194 LogError("FetchGroups() query failed: %s", query);
195 LogError("Query error: %s", error);
199 /* We cache basic group info so we can do reverse lookups */
200 new Handle:groups = CreateArray(3);
202 /* Now start fetching groups */
203 decl String:immunity[16];
204 decl String:flags[32];
205 decl String:name[128];
206 decl String:grp_immunity[256];
207 while (SQL_FetchRow(hQuery))
209 new id = SQL_FetchInt(hQuery, 0);
210 SQL_FetchString(hQuery, 1, immunity, sizeof(immunity));
211 SQL_FetchString(hQuery, 2, flags, sizeof(flags));
212 SQL_FetchString(hQuery, 3, name, sizeof(name));
213 SQL_FetchString(hQuery, 4, grp_immunity, sizeof(grp_immunity));
216 PrintToServer("Adding group (%d, %s, %s, %s, %s)", id, immunity, flags, name, grp_immunity);
219 /* Find or create the group */
221 if ((gid = FindAdmGroup(name)) == INVALID_GROUP_ID)
223 gid = CreateAdmGroup(name);
226 /* Add flags from the database to the group */
227 new num_flag_chars = strlen(flags);
228 for (new i=0; i<num_flag_chars; i++)
231 if (!FindFlagByChar(flags[i], flag))
235 SetAdmGroupAddFlag(gid, flag, true);
238 /* Set the immunity level this group has */
239 if (StrEqual(immunity, "all"))
241 SetAdmGroupImmunity(gid, Immunity_Default, true);
242 SetAdmGroupImmunity(gid, Immunity_Global, true);
243 } else if (StrEqual(immunity, "default")) {
244 SetAdmGroupImmunity(gid, Immunity_Default, true);
245 } else if (StrEqual(immunity, "global")) {
246 SetAdmGroupImmunity(gid, Immunity_Global, true);
249 new Handle:immunity_list = UTIL_ExplodeNumberString(grp_immunity);
251 /* Now, save all this for later */
255 data[2] = _:immunity_list;
257 PushArrayArray(groups, data);
262 /* Second pass - resolve immunity and group overrides */
263 new num_groups = GetArraySize(groups);
264 for (new i=0; i<num_groups; i++)
267 GetArrayArray(groups, i, data);
270 new GroupId:gid = GroupId:data[1];
271 new Handle:immunity_list = Handle:data[2];
273 /* Query to get per-group override list */
276 "SELECT type, name, access FROM sm_group_overrides WHERE group_id = %d",
279 /* Fetch the override query results */
280 if ((hQuery = SQL_Query(db, query)) == INVALID_HANDLE)
282 decl String:error[255];
283 SQL_GetError(db, error, sizeof(error));
284 LogError("FetchOverrides() query failed: %s", query);
285 LogError("Query error: %s", error);
287 decl String:type[16];
289 decl String:access[16];
290 while (SQL_FetchRow(hQuery))
292 SQL_FetchString(hQuery, 0, type, sizeof(type));
293 SQL_FetchString(hQuery, 1, cmd, sizeof(cmd));
294 SQL_FetchString(hQuery, 2, access, sizeof(access));
296 new OverrideType:o_type = Override_Command;
297 if (StrEqual(type, "group"))
299 o_type = Override_CommandGroup;
302 new OverrideRule:o_rule = Command_Deny;
303 if (StrEqual(access, "allow"))
305 o_rule = Command_Allow;
309 PrintToServer("AddAdmGroupCmdOverride(%d, %s, %d, %d)", gid, cmd, o_type, o_rule);
312 AddAdmGroupCmdOverride(gid, cmd, o_type, o_rule);
317 /* Lastly, resolve the immunity list if any */
319 if (immunity_list != INVALID_HANDLE
320 && ((immunity_count = GetArraySize(immunity_list)) > 0))
322 for (new j=0; j<immunity_count; j++)
324 new other_id = GetArrayCell(immunity_list, j);
331 /* See if we can find this other ID */
332 new GroupId:other_gid;
333 if ((other_gid = UTIL_FindGroupInCache(groups, other_id)) != INVALID_GROUP_ID)
336 PrintToServer("SetAdmGroupImmuneFrom(%d, %d)", gid, other_gid);
338 SetAdmGroupImmuneFrom(gid, other_gid);
343 if (immunity_list != INVALID_HANDLE)
345 CloseHandle(immunity_list);
352 FetchOverrides(Handle:db)
354 decl String:query[255];
357 Format(query, sizeof(query), "SELECT type, name, flags FROM sm_overrides");
359 if ((hQuery = SQL_Query(db, query)) == INVALID_HANDLE)
361 decl String:error[255];
362 SQL_GetError(db, error, sizeof(error));
363 LogError("FetchOverrides() query failed: %s", query);
364 LogError("Query error: %s", error);
368 decl String:type[64];
369 decl String:name[64];
370 decl String:flags[32];
372 while (SQL_FetchRow(hQuery))
374 SQL_FetchString(hQuery, 0, type, sizeof(type));
375 SQL_FetchString(hQuery, 1, name, sizeof(name));
376 SQL_FetchString(hQuery, 2, flags, sizeof(flags));
379 PrintToServer("Adding override (%s, %s, %s)", type, name, flags);
382 flag_bits = ReadFlagString(flags);
383 if (StrEqual(type, "command"))
385 AddCommandOverride(name, Override_Command, flag_bits);
386 } else if (StrEqual(type, "group")) {
387 AddCommandOverride(name, Override_CommandGroup, flag_bits);
394 stock GroupId:UTIL_FindGroupInCache(Handle:group_list, id)
396 new size = GetArraySize(group_list);
398 for (new i=0; i<size; i++)
401 GetArrayArray(group_list, i, data);
405 return GroupId:data[1];
409 return INVALID_GROUP_ID;
413 * Breaks a comma-delimited string into a list of numbers, returning a new
416 * @param text The string to split.
417 * @return A new array Handle which must be closed, or
418 * INVALID_HANDLE on no strings found.
420 stock Handle:UTIL_ExplodeNumberString(const String:text[])
422 new Handle:array = INVALID_HANDLE;
423 decl String:buffer[32];
426 while ((idx = SplitString(text[reloc_idx], ",", buffer, sizeof(buffer))) != -1)
429 if (text[reloc_idx] == '\0')
433 if (array == INVALID_HANDLE)
435 array = CreateArray();
437 PushArrayCell(array, StringToInt(buffer));
440 if (text[reloc_idx] != '\0')
442 if (array == INVALID_HANDLE)
444 array = CreateArray();
446 PushArrayCell(array, StringToInt(text[reloc_idx]));