/**
* vim: set ts=4 :
* =============================================================================
* SourceMod SQL Admins Plugin (Prefetch)
* Prefetches admins from an SQL database without threading.
*
* SourceMod (C)2004-2008 AlliedModders LLC. All rights reserved.
* =============================================================================
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, version 3.0, as published by the
* Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see .
*
* As a special exception, AlliedModders LLC gives you permission to link the
* code of this program (as well as its derivative works) to "Half-Life 2," the
* "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
* by the Valve Corporation. You must obey the GNU General Public License in
* all respects for all other code used. Additionally, AlliedModders LLC grants
* this exception to all derivative works. AlliedModders LLC defines further
* exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
* or .
*
* Version: $Id$
*/
/* We like semicolons */
#pragma semicolon 1
#include
public Plugin myinfo =
{
name = "SQL Admins (Prefetch)",
author = "AlliedModders LLC",
description = "Reads all admins from SQL",
version = SOURCEMOD_VERSION,
url = "http://www.sourcemod.net/"
};
public void OnRebuildAdminCache(AdminCachePart part)
{
/* First try to get a database connection */
char error[255];
Database db;
if (SQL_CheckConfig("admins"))
{
db = SQL_Connect("admins", true, error, sizeof(error));
} else {
db = SQL_Connect("default", true, error, sizeof(error));
}
if (db == null)
{
LogError("Could not connect to database \"default\": %s", error);
return;
}
if (part == AdminCache_Overrides)
{
FetchOverrides(db);
} else if (part == AdminCache_Groups) {
FetchGroups(db);
} else if (part == AdminCache_Admins) {
FetchUsers(db);
}
delete db;
}
void FetchUsers(Database db)
{
char query[255], error[255];
DBResultSet rs;
Format(query, sizeof(query), "SELECT id, authtype, identity, password, flags, name, immunity FROM sm_admins");
if ((rs = SQL_Query(db, query)) == null)
{
SQL_GetError(db, error, sizeof(error));
LogError("FetchUsers() query failed: %s", query);
LogError("Query error: %s", error);
return;
}
char authtype[16];
char identity[80];
char password[80];
char flags[32];
char name[80];
int immunity;
AdminId adm;
GroupId grp;
int id;
/* Keep track of a mapping from admin DB IDs to internal AdminIds to
* enable group lookups en masse */
StringMap htAdmins = new StringMap();
char key[16];
while (rs.FetchRow())
{
id = rs.FetchInt(0);
IntToString(id, key, sizeof(key));
rs.FetchString(1, authtype, sizeof(authtype));
rs.FetchString(2, identity, sizeof(identity));
rs.FetchString(3, password, sizeof(password));
rs.FetchString(4, flags, sizeof(flags));
rs.FetchString(5, name, sizeof(name));
immunity = rs.FetchInt(6);
/* Use a pre-existing admin if we can */
if ((adm = FindAdminByIdentity(authtype, identity)) == INVALID_ADMIN_ID)
{
adm = CreateAdmin(name);
if (!adm.BindIdentity(authtype, identity))
{
LogError("Could not bind prefetched SQL admin (authtype \"%s\") (identity \"%s\")", authtype, identity);
continue;
}
}
htAdmins.SetValue(key, adm);
#if defined _DEBUG
PrintToServer("Found SQL admin (%d,%s,%s,%s,%s,%s,%d):%d", id, authtype, identity, password, flags, name, immunity, adm);
#endif
/* See if this admin wants a password */
if (password[0] != '\0')
{
adm.SetPassword(password);
}
/* Apply each flag */
int len = strlen(flags);
AdminFlag flag;
for (int i=0; i