ds@1952: /** ds@1952: * vim: set ts=4 : ds@1952: * ============================================================================= ds@1952: * SourceMod SQL Admins Plugin (Prefetch) ds@1952: * Prefetches admins from an SQL database without threading. ds@1952: * ds@1952: * SourceMod (C)2004-2008 AlliedModders LLC. All rights reserved. ds@1952: * ============================================================================= ds@1952: * ds@1952: * This program is free software; you can redistribute it and/or modify it under ds@1952: * the terms of the GNU General Public License, version 3.0, as published by the ds@1952: * Free Software Foundation. ds@1952: * ds@1952: * This program is distributed in the hope that it will be useful, but WITHOUT ds@1952: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS ds@1952: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more ds@1952: * details. ds@1952: * ds@1952: * You should have received a copy of the GNU General Public License along with ds@1952: * this program. If not, see . ds@1952: * ds@1952: * As a special exception, AlliedModders LLC gives you permission to link the ds@1952: * code of this program (as well as its derivative works) to "Half-Life 2," the ds@1952: * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software ds@1952: * by the Valve Corporation. You must obey the GNU General Public License in ds@1952: * all respects for all other code used. Additionally, AlliedModders LLC grants ds@1952: * this exception to all derivative works. AlliedModders LLC defines further ds@1952: * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007), ds@1952: * or . ds@1952: * ds@1952: * Version: $Id$ ds@1952: */ ds@1952: ds@1952: /* We like semicolons */ ds@1952: #pragma semicolon 1 ds@1952: ds@1952: #include ds@1952: ds@1952: public Plugin:myinfo = ds@1952: { ds@1952: name = "SQL Admins (Prefetch)", ds@1952: author = "AlliedModders LLC", ds@1952: description = "Reads all admins from SQL", ds@1952: version = SOURCEMOD_VERSION, ds@1952: url = "http://www.sourcemod.net/" ds@1952: }; ds@1952: ds@1952: public OnRebuildAdminCache(AdminCachePart:part) ds@1952: { ds@1952: /* First try to get a database connection */ ds@1952: decl String:error[255]; ds@1952: new Handle:db; ds@1952: ds@1952: if (SQL_CheckConfig("admins")) ds@1952: { ds@1952: db = SQL_Connect("admins", true, error, sizeof(error)); ds@1952: } else { ds@1952: db = SQL_Connect("default", true, error, sizeof(error)); ds@1952: } ds@1952: ds@1952: if (db == INVALID_HANDLE) ds@1952: { ds@1952: LogError("Could not connect to database \"default\": %s", error); ds@1952: return; ds@1952: } ds@1952: ds@1952: if (part == AdminCache_Overrides) ds@1952: { ds@1952: FetchOverrides(db); ds@1952: } else if (part == AdminCache_Groups) { ds@1952: FetchGroups(db); ds@1952: } else if (part == AdminCache_Admins) { ds@1952: FetchUsers(db); ds@1952: } ds@1952: ds@1952: CloseHandle(db); ds@1952: } ds@1952: ds@1952: FetchUserGroups(Handle:hQuery, AdminId:adm, id) ds@1952: { ds@1952: SQL_BindParamInt(hQuery, 0, id); ds@1952: if (!SQL_Execute(hQuery)) ds@1952: { ds@1952: decl String:error[255]; ds@1952: SQL_GetError(hQuery, error, sizeof(error)); ds@1952: LogError("FetchUserGroups() statement execute failed: %s", error); ds@1952: return; ds@1952: } ds@1952: ds@1952: decl String:name[80]; ds@1952: new GroupId:gid; ds@1952: while (SQL_FetchRow(hQuery)) ds@1952: { ds@1952: SQL_FetchString(hQuery, 0, name, sizeof(name)); ds@1952: ds@1952: if ((gid = FindAdmGroup(name)) == INVALID_GROUP_ID) ds@1952: { ds@1952: /* Group wasn't found, don't bother with it. */ ds@1952: continue; ds@1952: } ds@1952: ds@1952: #if defined _DEBUG ds@1952: PrintToServer("Found admin group (%d, %d, %d)", id, gid, adm); ds@1952: #endif ds@1952: ds@1952: AdminInheritGroup(adm, gid); ds@1952: } ds@1952: } ds@1952: ds@1952: FetchUsers(Handle:db) ds@1952: { ds@1952: decl String:query[255], String:error[255]; ds@1952: new Handle:hQuery, Handle:hGroupQuery; ds@1952: ds@1952: Format(query, sizeof(query), "SELECT id, authtype, identity, password, flags, name, immunity FROM sm_admins"); ds@1952: if ((hQuery = SQL_Query(db, query)) == INVALID_HANDLE) ds@1952: { ds@1952: SQL_GetError(db, error, sizeof(error)); ds@1952: LogError("FetchUsers() query failed: %s", query); ds@1952: LogError("Query error: %s", error); ds@1952: return; ds@1952: } ds@1952: ds@1952: /* If this fails it's non-fatal */ ds@1952: Format(query, ds@1952: sizeof(query), ds@1952: "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"); ds@1952: if ((hGroupQuery = SQL_PrepareQuery(db, query, error, sizeof(error))) == INVALID_HANDLE) ds@1952: { ds@1952: LogError("FetchUsers() group query preparation failed: %s", query); ds@1952: LogError("Query error: %s", error); ds@1952: } ds@1952: ds@1952: decl String:authtype[16]; ds@1952: decl String:identity[80]; ds@1952: decl String:password[80]; ds@1952: decl String:flags[32]; ds@1952: decl String:name[80]; ds@1952: new immunity; ds@1952: new AdminId:adm, id; ds@1952: ds@1952: while (SQL_FetchRow(hQuery)) ds@1952: { ds@1952: id = SQL_FetchInt(hQuery, 0); ds@1952: SQL_FetchString(hQuery, 1, authtype, sizeof(authtype)); ds@1952: SQL_FetchString(hQuery, 2, identity, sizeof(identity)); ds@1952: SQL_FetchString(hQuery, 3, password, sizeof(password)); ds@1952: SQL_FetchString(hQuery, 4, flags, sizeof(flags)); ds@1952: SQL_FetchString(hQuery, 5, name, sizeof(name)); ds@1952: immunity = SQL_FetchInt(hQuery, 6); ds@1952: ds@1952: /* Use a pre-existing admin if we can */ ds@1952: if ((adm = FindAdminByIdentity(authtype, identity)) == INVALID_ADMIN_ID) ds@1952: { ds@1952: adm = CreateAdmin(name); ds@1952: if (!BindAdminIdentity(adm, authtype, identity)) ds@1952: { ds@1952: LogError("Could not bind prefetched SQL admin (authtype \"%s\") (identity \"%s\")", authtype, identity); ds@1952: continue; ds@1952: } ds@1952: } ds@1952: ds@1952: #if defined _DEBUG ds@1952: PrintToServer("Found SQL admin (%d,%s,%s,%s,%s,%s,%d):%d", id, authtype, identity, password, flags, name, immunity, adm); ds@1952: #endif ds@1952: ds@1952: /* See if this admin wants a password */ ds@1952: if (password[0] != '\0') ds@1952: { ds@1952: SetAdminPassword(adm, password); ds@1952: } ds@1952: ds@1952: /* Apply each flag */ ds@1952: new len = strlen(flags); ds@1952: new AdminFlag:flag; ds@1952: for (new i=0; i