plugins/sql-admin-manager.sp
author David Anderson <dvander@alliedmods.net>
Sat May 17 05:12:52 2008 +0000 (2008-05-17)
changeset 2056 f1fcde46ac7c
parent 1952 b7aa2b626807
child 2289 44045ead61cd
permissions -rw-r--r--
added amb1119 - sm_revote command
ds@1952
     1
/**
ds@1952
     2
 * vim: set ts=4 :
ds@1952
     3
 * =============================================================================
ds@1952
     4
 * SourceMod SQL Admin Manager Plugin
ds@1952
     5
 * Adds/managers admins and groups in an SQL database.
ds@1952
     6
 *
ds@1952
     7
 * SourceMod (C)2004-2008 AlliedModders LLC.  All rights reserved.
ds@1952
     8
 * =============================================================================
ds@1952
     9
 *
ds@1952
    10
 * This program is free software; you can redistribute it and/or modify it under
ds@1952
    11
 * the terms of the GNU General Public License, version 3.0, as published by the
ds@1952
    12
 * Free Software Foundation.
ds@1952
    13
 * 
ds@1952
    14
 * This program is distributed in the hope that it will be useful, but WITHOUT
ds@1952
    15
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
ds@1952
    16
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
ds@1952
    17
 * details.
ds@1952
    18
 *
ds@1952
    19
 * You should have received a copy of the GNU General Public License along with
ds@1952
    20
 * this program.  If not, see <http://www.gnu.org/licenses/>.
ds@1952
    21
 *
ds@1952
    22
 * As a special exception, AlliedModders LLC gives you permission to link the
ds@1952
    23
 * code of this program (as well as its derivative works) to "Half-Life 2," the
ds@1952
    24
 * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
ds@1952
    25
 * by the Valve Corporation.  You must obey the GNU General Public License in
ds@1952
    26
 * all respects for all other code used.  Additionally, AlliedModders LLC grants
ds@1952
    27
 * this exception to all derivative works.  AlliedModders LLC defines further
ds@1952
    28
 * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
ds@1952
    29
 * or <http://www.sourcemod.net/license.php>.
ds@1952
    30
 *
ds@1952
    31
 * Version: $Id$
ds@1952
    32
 */
ds@1952
    33
ds@1952
    34
/* We like semicolons */
ds@1952
    35
#pragma semicolon 1
ds@1952
    36
ds@1952
    37
#include <sourcemod>
ds@1952
    38
ds@1952
    39
#define CURRENT_SCHEMA_VERSION		1409
ds@1952
    40
#define SCHEMA_UPGRADE_1			1409
ds@1952
    41
ds@1952
    42
new current_version[4] = {1, 0, 0, CURRENT_SCHEMA_VERSION};
ds@1952
    43
ds@1952
    44
public Plugin:myinfo = 
ds@1952
    45
{
ds@1952
    46
	name = "SQL Admin Manager",
ds@1952
    47
	author = "AlliedModders LLC",
ds@1952
    48
	description = "Manages SQL admins",
ds@1952
    49
	version = SOURCEMOD_VERSION,
ds@1952
    50
	url = "http://www.sourcemod.net/"
ds@1952
    51
};
ds@1952
    52
ds@1952
    53
public OnPluginStart()
ds@1952
    54
{
ds@1952
    55
	LoadTranslations("common.phrases");
ds@1952
    56
	LoadTranslations("sqladmins.phrases");
ds@1952
    57
ds@1952
    58
	RegAdminCmd("sm_sql_addadmin", Command_AddAdmin, ADMFLAG_ROOT, "Adds an admin to the SQL database");
ds@1952
    59
	RegAdminCmd("sm_sql_deladmin", Command_DelAdmin, ADMFLAG_ROOT, "Removes an admin from the SQL database");
ds@1952
    60
	RegAdminCmd("sm_sql_addgroup", Command_AddGroup, ADMFLAG_ROOT, "Adds a group to the SQL database");
ds@1952
    61
	RegAdminCmd("sm_sql_delgroup", Command_DelGroup, ADMFLAG_ROOT, "Removes a group from the SQL database");
ds@1952
    62
	RegAdminCmd("sm_sql_setadmingroups", Command_SetAdminGroups, ADMFLAG_ROOT, "Sets an admin's groups in the SQL database");
ds@1952
    63
	RegServerCmd("sm_create_adm_tables", Command_CreateTables);
ds@1952
    64
	RegServerCmd("sm_update_adm_tables", Command_UpdateTables);
ds@1952
    65
}
ds@1952
    66
ds@1952
    67
Handle:Connect()
ds@1952
    68
{
ds@1952
    69
	decl String:error[255];
ds@1952
    70
	new Handle:db;
ds@1952
    71
	
ds@1952
    72
	if (SQL_CheckConfig("admins"))
ds@1952
    73
	{
ds@1952
    74
		db = SQL_Connect("admins", true, error, sizeof(error));
ds@1952
    75
	} else {
ds@1952
    76
		db = SQL_Connect("default", true, error, sizeof(error));
ds@1952
    77
	}
ds@1952
    78
	
ds@1952
    79
	if (db == INVALID_HANDLE)
ds@1952
    80
	{
ds@1952
    81
		LogError("Could not connect to database: %s", error);
ds@1952
    82
	}
ds@1952
    83
	
ds@1952
    84
	return db;
ds@1952
    85
}
ds@1952
    86
ds@1952
    87
CreateMySQL(client, Handle:db)
ds@1952
    88
{
ds@1952
    89
	new String:queries[7][] = 
ds@1952
    90
	{
ds@1952
    91
		"CREATE TABLE sm_admins (id int(10) unsigned NOT NULL auto_increment, authtype enum('steam','name','ip') NOT NULL, identity varchar(65) NOT NULL, password varchar(65), flags varchar(30) NOT NULL, name varchar(65) NOT NULL, immunity int(10) unsigned NOT NULL, PRIMARY KEY (id))",
ds@1952
    92
		"CREATE TABLE sm_groups (id int(10) unsigned NOT NULL auto_increment, flags varchar(30) NOT NULL, name varchar(120) NOT NULL, immunity_level int(1) unsigned NOT NULL, PRIMARY KEY (id))",
ds@1952
    93
		"CREATE TABLE sm_group_immunity (group_id int(10) unsigned NOT NULL, other_id int(10) unsigned NOT NULL,  PRIMARY KEY (group_id, other_id))",
ds@1952
    94
		"CREATE TABLE sm_group_overrides (group_id int(10) unsigned NOT NULL, type enum('command','group') NOT NULL, name varchar(32) NOT NULL, access enum('allow','deny') NOT NULL, PRIMARY KEY (group_id, type, name))",
ds@1952
    95
		"CREATE TABLE sm_overrides (type enum('command','group') NOT NULL, name varchar(32) NOT NULL, flags varchar(30) NOT NULL, PRIMARY KEY (type,name))",
ds@1952
    96
		"CREATE TABLE sm_admins_groups (admin_id int(10) unsigned NOT NULL, group_id int(10) unsigned NOT NULL, inherit_order int(10) NOT NULL, PRIMARY KEY (admin_id, group_id))",
ds@1952
    97
		"CREATE TABLE IF NOT EXISTS sm_config (cfg_key varchar(32) NOT NULL, cfg_value varchar(255) NOT NULL, PRIMARY KEY (cfg_key))"
ds@1952
    98
	};
ds@1952
    99
ds@1952
   100
	for (new i = 0; i < 7; i++)
ds@1952
   101
	{
ds@1952
   102
		if (!DoQuery(client, db, queries[i]))
ds@1952
   103
		{
ds@1952
   104
			return;
ds@1952
   105
		}
ds@1952
   106
	}
ds@1952
   107
ds@1952
   108
	decl String:query[256];
ds@1952
   109
	Format(query, 
ds@1952
   110
		sizeof(query), 
ds@1952
   111
		"INSERT INTO sm_config (cfg_key, cfg_value) VALUES ('admin_version', '1.0.0.%d') ON DUPLICATE KEY UPDATE cfg_value = '1.0.0.%d'",
ds@1952
   112
		CURRENT_SCHEMA_VERSION,
ds@1952
   113
		CURRENT_SCHEMA_VERSION);
ds@1952
   114
ds@1952
   115
	if (!DoQuery(client, db, query))
ds@1952
   116
	{
ds@1952
   117
		return;
ds@1952
   118
	}
ds@1952
   119
ds@1952
   120
	ReplyToCommand(client, "[SM] Admin tables have been created.");
ds@1952
   121
}
ds@1952
   122
ds@1952
   123
CreateSQLite(client, Handle:db)
ds@1952
   124
{
ds@1952
   125
	new String:queries[7][] = 
ds@1952
   126
	{
ds@1952
   127
		"CREATE TABLE sm_admins (id INTEGER PRIMARY KEY AUTOINCREMENT, authtype varchar(16) NOT NULL CHECK(authtype IN ('steam', 'ip', 'name')), identity varchar(65) NOT NULL, password varchar(65), flags varchar(30) NOT NULL, name varchar(65) NOT NULL, immunity INTEGER NOT NULL)",
ds@1952
   128
		"CREATE TABLE sm_groups (id INTEGER PRIMARY KEY AUTOINCREMENT, flags varchar(30) NOT NULL, name varchar(120) NOT NULL, immunity_level INTEGER NOT NULL)",
ds@1952
   129
		"CREATE TABLE sm_group_immunity (group_id INTEGER NOT NULL, other_id INTEGER NOT NULL, PRIMARY KEY (group_id, other_id))",
ds@1952
   130
		"CREATE TABLE sm_group_overrides (group_id INTEGER NOT NULL, type varchar(16) NOT NULL CHECK (type IN ('command', 'group')), name varchar(32) NOT NULL, access varchar(16) NOT NULL CHECK (access IN ('allow', 'deny')), PRIMARY KEY (group_id, type, name))",
ds@1952
   131
		"CREATE TABLE sm_overrides (type varchar(16) NOT NULL CHECK (type IN ('command', 'group')), name varchar(32) NOT NULL, flags varchar(30) NOT NULL, PRIMARY KEY (type,name))",
ds@1952
   132
		"CREATE TABLE sm_admins_groups (admin_id INTEGER NOT NULL, group_id INTEGER NOT NULL, inherit_order int(10) NOT NULL, PRIMARY KEY (admin_id, group_id))",
ds@1952
   133
		"CREATE TABLE IF NOT EXISTS sm_config (cfg_key varchar(32) NOT NULL, cfg_value varchar(255) NOT NULL, PRIMARY KEY (cfg_key))"
ds@1952
   134
	};
ds@1952
   135
ds@1952
   136
	for (new i = 0; i < 7; i++)
ds@1952
   137
	{
ds@1952
   138
		if (!DoQuery(client, db, queries[i]))
ds@1952
   139
		{
ds@1952
   140
			return;
ds@1952
   141
		}
ds@1952
   142
	}
ds@1952
   143
ds@1952
   144
	decl String:query[256];
ds@1952
   145
	Format(query, 
ds@1952
   146
		sizeof(query), 
ds@1952
   147
		"REPLACE INTO sm_config (cfg_key, cfg_value) VALUES ('admin_version', '1.0.0.%d')",
ds@1952
   148
		CURRENT_SCHEMA_VERSION);
ds@1952
   149
ds@1952
   150
	if (!DoQuery(client, db, query))
ds@1952
   151
	{
ds@1952
   152
		return;
ds@1952
   153
	}
ds@1952
   154
ds@1952
   155
	ReplyToCommand(client, "[SM] Admin tables have been created.");
ds@1952
   156
}
ds@1952
   157
ds@1952
   158
public Action:Command_CreateTables(args)
ds@1952
   159
{
ds@1952
   160
	new client = 0;
ds@1952
   161
	new Handle:db = Connect();
ds@1952
   162
	if (db == INVALID_HANDLE)
ds@1952
   163
	{
ds@1952
   164
		ReplyToCommand(client, "[SM] %t", "Could not connect to database");
ds@1952
   165
		return Plugin_Handled;
ds@1952
   166
	}
ds@1952
   167
ds@1952
   168
	new String:ident[16];
ds@1952
   169
	SQL_ReadDriver(db, ident, sizeof(ident));
ds@1952
   170
ds@1952
   171
	if (strcmp(ident, "mysql") == 0)
ds@1952
   172
	{
ds@1952
   173
		CreateMySQL(client, db);
ds@1952
   174
	} else if (strcmp(ident, "sqlite") == 0) {
ds@1952
   175
		CreateSQLite(client, db);
ds@1952
   176
	} else {
ds@1952
   177
		ReplyToCommand(client, "[SM] Unknown driver type '%s', cannot create tables.", ident);
ds@1952
   178
	}
ds@1952
   179
ds@1952
   180
	CloseHandle(db);
ds@1952
   181
ds@1952
   182
	return Plugin_Handled;
ds@1952
   183
}
ds@1952
   184
ds@1952
   185
bool:GetUpdateVersion(client, Handle:db, versions[4])
ds@1952
   186
{
ds@1952
   187
	decl String:query[256];
ds@1952
   188
	new Handle:hQuery;
ds@1952
   189
ds@1952
   190
	Format(query, sizeof(query), "SELECT cfg_value FROM sm_config WHERE cfg_key = 'admin_version'");
ds@1952
   191
	if ((hQuery = SQL_Query(db, query)) == INVALID_HANDLE)
ds@1952
   192
	{
ds@1952
   193
		DoError(client, db, query, "Version lookup query failed");
ds@1952
   194
		return false;
ds@1952
   195
	}
ds@1952
   196
	if (SQL_FetchRow(hQuery))
ds@1952
   197
	{
ds@1952
   198
		decl String:version_string[255];
ds@1952
   199
		SQL_FetchString(hQuery, 0, version_string, sizeof(version_string));
ds@1952
   200
ds@1952
   201
		decl String:version_numbers[4][12];
ds@1952
   202
		if (ExplodeString(version_string, ".", version_numbers, 4, 12) == 4)
ds@1952
   203
		{
ds@1952
   204
			for (new i = 0; i < 4; i++)
ds@1952
   205
			{
ds@1952
   206
				versions[i] = StringToInt(version_numbers[i]);
ds@1952
   207
			}
ds@1952
   208
		}
ds@1952
   209
	}
ds@1952
   210
ds@1952
   211
	CloseHandle(hQuery);
ds@1952
   212
ds@1952
   213
	if (current_version[3] < versions[3])
ds@1952
   214
	{
ds@1952
   215
		ReplyToCommand(client, "[SM] The database is newer than the expected version.");
ds@1952
   216
		return false;
ds@1952
   217
	}
ds@1952
   218
ds@1952
   219
	if (current_version[3] == versions[3])
ds@1952
   220
	{
ds@1952
   221
		ReplyToCommand(client, "[SM] Your tables are already up to date.");
ds@1952
   222
		return false;
ds@1952
   223
	}
ds@1952
   224
ds@1952
   225
ds@1952
   226
	return true;
ds@1952
   227
}
ds@1952
   228
ds@1952
   229
UpdateSQLite(client, Handle:db)
ds@1952
   230
{
ds@1952
   231
	decl String:query[512];
ds@1952
   232
	new Handle:hQuery;
ds@1952
   233
ds@1952
   234
	Format(query, sizeof(query), "SELECT name FROM sqlite_master WHERE type = 'table' AND name = 'sm_config'");
ds@1952
   235
	if ((hQuery = SQL_Query(db, query)) == INVALID_HANDLE)
ds@1952
   236
	{
ds@1952
   237
		DoError(client, db, query, "Table lookup query failed");
ds@1952
   238
		return;
ds@1952
   239
	}
ds@1952
   240
ds@1952
   241
	new bool:found = SQL_FetchRow(hQuery);
ds@1952
   242
ds@1952
   243
	CloseHandle(hQuery);
ds@1952
   244
ds@1952
   245
	new versions[4];
ds@1952
   246
	if (found)
ds@1952
   247
	{
ds@1952
   248
		if (!GetUpdateVersion(client, db, versions))
ds@1952
   249
		{
ds@1952
   250
			return;
ds@1952
   251
		}
ds@1952
   252
	}
ds@1952
   253
ds@1952
   254
	/* We only know about one upgrade path right now... 
ds@1952
   255
	 * 0 => 1
ds@1952
   256
	 */
ds@1952
   257
	if (versions[3] < SCHEMA_UPGRADE_1)
ds@1952
   258
	{
ds@1952
   259
		new String:queries[8][] = 
ds@1952
   260
		{
ds@1952
   261
			"ALTER TABLE sm_admins ADD immunity INTEGER DEFAULT 0 NOT NULL",
ds@1952
   262
			"CREATE TABLE _sm_groups_temp (id INTEGER PRIMARY KEY AUTOINCREMENT, flags varchar(30) NOT NULL, name varchar(120) NOT NULL, immunity_level INTEGER DEFAULT 0 NOT NULL)",
ds@1952
   263
			"INSERT INTO _sm_groups_temp (id, flags, name) SELECT id, flags, name FROM sm_groups",
ds@1952
   264
			"UPDATE _sm_groups_temp SET immunity_level = 2 WHERE id IN (SELECT g.id FROM sm_groups g WHERE g.immunity = 'global')",
ds@1952
   265
			"UPDATE _sm_groups_temp SET immunity_level = 1 WHERE id IN (SELECT g.id FROM sm_groups g WHERE g.immunity = 'default')",
ds@1952
   266
			"DROP TABLE sm_groups",
ds@1952
   267
			"ALTER TABLE _sm_groups_temp RENAME TO sm_groups",
ds@1952
   268
			"CREATE TABLE IF NOT EXISTS sm_config (cfg_key varchar(32) NOT NULL, cfg_value varchar(255) NOT NULL, PRIMARY KEY (cfg_key))"
ds@1952
   269
		};
ds@1952
   270
ds@1952
   271
		for (new i = 0; i < 8; i++)
ds@1952
   272
		{
ds@1952
   273
			if (!DoQuery(client, db, queries[i]))
ds@1952
   274
			{
ds@1952
   275
				return;
ds@1952
   276
			}
ds@1952
   277
		}
ds@1952
   278
ds@1952
   279
		Format(query, 
ds@1952
   280
			sizeof(query), 
ds@1952
   281
			"REPLACE INTO sm_config (cfg_key, cfg_value) VALUES ('admin_version', '1.0.0.%d')",
ds@1952
   282
			SCHEMA_UPGRADE_1);
ds@1952
   283
ds@1952
   284
		if (!DoQuery(client, db, query))
ds@1952
   285
		{
ds@1952
   286
			return;
ds@1952
   287
		}
ds@1952
   288
ds@1952
   289
		versions[3] = SCHEMA_UPGRADE_1;
ds@1952
   290
	}
ds@1952
   291
ds@1952
   292
	ReplyToCommand(client, "[SM] Your tables are now up to date.");
ds@1952
   293
}
ds@1952
   294
ds@1952
   295
UpdateMySQL(client, Handle:db)
ds@1952
   296
{
ds@1952
   297
	decl String:query[512];
ds@1952
   298
	new Handle:hQuery;
ds@1952
   299
	
ds@1952
   300
	Format(query, sizeof(query), "SHOW TABLES");
ds@1952
   301
	if ((hQuery = SQL_Query(db, query)) == INVALID_HANDLE)
ds@1952
   302
	{
ds@1952
   303
		DoError(client, db, query, "Table lookup query failed");
ds@1952
   304
		return;
ds@1952
   305
	}
ds@1952
   306
ds@1952
   307
	decl String:table[64];
ds@1952
   308
	new bool:found = false;
ds@1952
   309
	while (SQL_FetchRow(hQuery))
ds@1952
   310
	{
ds@1952
   311
		SQL_FetchString(hQuery, 0, table, sizeof(table));
ds@1952
   312
		if (strcmp(table, "sm_config") == 0)
ds@1952
   313
		{
ds@1952
   314
			found = true;
ds@1952
   315
		}
ds@1952
   316
	}
ds@1952
   317
	CloseHandle(hQuery);
ds@1952
   318
ds@1952
   319
	new versions[4];
ds@1952
   320
ds@1952
   321
	if (found && !GetUpdateVersion(client, db, versions))
ds@1952
   322
	{
ds@1952
   323
		return;
ds@1952
   324
	}
ds@1952
   325
ds@1952
   326
	/* We only know about one upgrade path right now... 
ds@1952
   327
	 * 0 => 1
ds@1952
   328
	 */
ds@1952
   329
	if (versions[3] < SCHEMA_UPGRADE_1)
ds@1952
   330
	{
ds@1952
   331
		new String:queries[6][] = 
ds@1952
   332
		{
ds@1952
   333
			"CREATE TABLE IF NOT EXISTS sm_config (cfg_key varchar(32) NOT NULL, cfg_value varchar(255) NOT NULL, PRIMARY KEY (cfg_key))",
ds@1952
   334
			"ALTER TABLE sm_admins ADD immunity INT UNSIGNED NOT NULL",
ds@1952
   335
			"ALTER TABLE sm_groups ADD immunity_level INT UNSIGNED NOT NULL",
ds@1952
   336
			"UPDATE sm_groups SET immunity_level = 2 WHERE immunity = 'default'",
ds@1952
   337
			"UPDATE sm_groups SET immunity_level = 1 WHERE immunity = 'global'",
ds@1952
   338
			"ALTER TABLE sm_groups DROP immunity"
ds@1952
   339
		};
ds@1952
   340
ds@1952
   341
		for (new i = 0; i < 6; i++)
ds@1952
   342
		{
ds@1952
   343
			if (!DoQuery(client, db, queries[i]))
ds@1952
   344
			{
ds@1952
   345
				return;
ds@1952
   346
			}
ds@1952
   347
		}
ds@1952
   348
ds@1952
   349
		decl String:upgr[48];
ds@1952
   350
		Format(upgr, sizeof(upgr), "1.0.0.%d", SCHEMA_UPGRADE_1);
ds@1952
   351
ds@1952
   352
		Format(query, sizeof(query), "INSERT INTO sm_config (cfg_key, cfg_value) VALUES ('admin_version', '%s') ON DUPLICATE KEY UPDATE cfg_value = '%s'", upgr, upgr);
ds@1952
   353
		if (!DoQuery(client, db, query))
ds@1952
   354
		{
ds@1952
   355
			return;
ds@1952
   356
		}
ds@1952
   357
ds@1952
   358
		versions[3] = SCHEMA_UPGRADE_1;
ds@1952
   359
	}
ds@1952
   360
ds@1952
   361
	ReplyToCommand(client, "[SM] Your tables are now up to date.");
ds@1952
   362
}
ds@1952
   363
ds@1952
   364
public Action:Command_UpdateTables(args)
ds@1952
   365
{
ds@1952
   366
	new client = 0;
ds@1952
   367
	new Handle:db = Connect();
ds@1952
   368
	if (db == INVALID_HANDLE)
ds@1952
   369
	{
ds@1952
   370
		ReplyToCommand(client, "[SM] %t", "Could not connect to database");
ds@1952
   371
		return Plugin_Handled;
ds@1952
   372
	}
ds@1952
   373
ds@1952
   374
	new String:ident[16];
ds@1952
   375
	SQL_ReadDriver(db, ident, sizeof(ident));
ds@1952
   376
ds@1952
   377
	if (strcmp(ident, "mysql") == 0)
ds@1952
   378
	{
ds@1952
   379
		UpdateMySQL(client, db);
ds@1952
   380
	} else if (strcmp(ident, "sqlite") == 0) {
ds@1952
   381
		UpdateSQLite(client, db);
ds@1952
   382
	} else {
ds@1952
   383
		ReplyToCommand(client, "[SM] Unknown driver type, cannot upgrade.");
ds@1952
   384
	}
ds@1952
   385
ds@1952
   386
	CloseHandle(db);
ds@1952
   387
ds@1952
   388
	return Plugin_Handled;
ds@1952
   389
}
ds@1952
   390
ds@1952
   391
public Action:Command_SetAdminGroups(client, args)
ds@1952
   392
{
ds@1952
   393
	if (args < 2)
ds@1952
   394
	{
ds@1952
   395
		ReplyToCommand(client, "[SM] Usage: sm_sql_setadmingroups <authtype> <identity> [group1] ... [group N]");
ds@1952
   396
		return Plugin_Handled;
ds@1952
   397
	}
ds@1952
   398
	
ds@1952
   399
	decl String:authtype[16];
ds@1952
   400
	GetCmdArg(1, authtype, sizeof(authtype));
ds@1952
   401
	
ds@1952
   402
	if (!StrEqual(authtype, "steam")
ds@1952
   403
		&& !StrEqual(authtype, "ip")
ds@1952
   404
		&& !StrEqual(authtype, "name"))
ds@1952
   405
	{
ds@1952
   406
		ReplyToCommand(client, "[SM] %t", "Invalid authtype");
ds@1952
   407
		return Plugin_Handled;
ds@1952
   408
	}
ds@1952
   409
	
ds@1952
   410
	new Handle:db = Connect();
ds@1952
   411
	if (db == INVALID_HANDLE)
ds@1952
   412
	{
ds@1952
   413
		ReplyToCommand(client, "[SM] %t", "Could not connect to database");
ds@1952
   414
		return Plugin_Handled;
ds@1952
   415
	}
ds@1952
   416
	
ds@1952
   417
	decl String:identity[65];
ds@1952
   418
	decl String:safe_identity[140];
ds@1952
   419
	GetCmdArg(2, identity, sizeof(identity));
ds@1952
   420
	SQL_EscapeString(db, identity, safe_identity, sizeof(safe_identity));
ds@1952
   421
	
ds@1952
   422
	decl String:query[255];
ds@1952
   423
	Format(query, 
ds@1952
   424
		sizeof(query),
ds@1952
   425
		"SELECT id FROM sm_admins WHERE authtype = '%s' AND identity = '%s'",
ds@1952
   426
		authtype,
ds@1952
   427
		safe_identity);
ds@1952
   428
		
ds@1952
   429
	new Handle:hQuery;
ds@1952
   430
	if ((hQuery = SQL_Query(db, query)) == INVALID_HANDLE)
ds@1952
   431
	{
ds@1952
   432
		return DoError(client, db, query, "Admin lookup query failed");
ds@1952
   433
	}
ds@1952
   434
	
ds@1952
   435
	if (!SQL_FetchRow(hQuery))
ds@1952
   436
	{
ds@1952
   437
		ReplyToCommand(client, "[SM] %t", "SQL Admin not found");
ds@1952
   438
		CloseHandle(hQuery);
ds@1952
   439
		CloseHandle(db);
ds@1952
   440
		return Plugin_Handled;
ds@1952
   441
	}
ds@1952
   442
	
ds@1952
   443
	new id = SQL_FetchInt(hQuery, 0);
ds@1952
   444
	
ds@1952
   445
	CloseHandle(hQuery);
ds@1952
   446
	
ds@1952
   447
	/**
ds@1952
   448
	 * First delete all of the user's existing groups.
ds@1952
   449
	 */
ds@1952
   450
	Format(query, sizeof(query), "DELETE FROM sm_admins_groups WHERE admin_id = %d", id);
ds@1952
   451
	if (!SQL_FastQuery(db, query))
ds@1952
   452
	{
ds@1952
   453
		return DoError(client, db, query, "Admin group deletion query failed");
ds@1952
   454
	}
ds@1952
   455
	
ds@1952
   456
	if (args < 3)
ds@1952
   457
	{
ds@1952
   458
		ReplyToCommand(client, "[SM] %t", "SQL Admin groups reset");
ds@1952
   459
		CloseHandle(db);
ds@1952
   460
		return Plugin_Handled;
ds@1952
   461
	}
ds@1952
   462
	
ds@1952
   463
	decl String:error[256];
ds@1952
   464
	new Handle:hAddQuery, Handle:hFindQuery;
ds@1952
   465
	
ds@1952
   466
	Format(query, sizeof(query), "SELECT id FROM sm_groups WHERE name = ?");
ds@1952
   467
	if ((hFindQuery = SQL_PrepareQuery(db, query, error, sizeof(error))) == INVALID_HANDLE)
ds@1952
   468
	{
ds@1952
   469
		return DoStmtError(client, db, query, error, "Group search prepare failed");
ds@1952
   470
	}
ds@1952
   471
	
ds@1952
   472
	Format(query, 
ds@1952
   473
		sizeof(query), 
ds@1952
   474
		"INSERT INTO sm_admins_groups (admin_id, group_id, inherit_order) VALUES (%d, ?, ?)",
ds@1952
   475
		id);
ds@1952
   476
	if ((hAddQuery = SQL_PrepareQuery(db, query, error, sizeof(error))) == INVALID_HANDLE)
ds@1952
   477
	{
ds@1952
   478
		CloseHandle(hFindQuery);
ds@1952
   479
		return DoStmtError(client, db, query, error, "Add admin group prepare failed");
ds@1952
   480
	}
ds@1952
   481
	
ds@1952
   482
	decl String:name[80];
ds@1952
   483
	new inherit_order = 0;
ds@1952
   484
	for (new i=3; i<=args; i++)
ds@1952
   485
	{
ds@1952
   486
		GetCmdArg(i, name, sizeof(name));
ds@1952
   487
		
ds@1952
   488
		SQL_BindParamString(hFindQuery, 0, name, false);
ds@1952
   489
		if (!SQL_Execute(hFindQuery) || !SQL_FetchRow(hFindQuery))
ds@1952
   490
		{
ds@1952
   491
			ReplyToCommand(client, "[SM] %t", "SQL Group X not found", name);
ds@1952
   492
		} else {
ds@1952
   493
			new gid = SQL_FetchInt(hFindQuery, 0);
ds@1952
   494
			
ds@1952
   495
			SQL_BindParamInt(hAddQuery, 0, gid);
ds@1952
   496
			SQL_BindParamInt(hAddQuery, 1, ++inherit_order);
ds@1952
   497
			if (!SQL_Execute(hAddQuery))
ds@1952
   498
			{
ds@1952
   499
				ReplyToCommand(client, "[SM] %t", "SQL Group X failed to bind", name);
ds@1952
   500
				inherit_order--;
ds@1952
   501
			}
ds@1952
   502
		}
ds@1952
   503
	}
ds@1952
   504
	
ds@1952
   505
	CloseHandle(hAddQuery);
ds@1952
   506
	CloseHandle(hFindQuery);
ds@1952
   507
	CloseHandle(db);
ds@1952
   508
	
ds@1952
   509
	if (inherit_order == 1)
ds@1952
   510
	{
ds@1952
   511
		ReplyToCommand(client, "[SM] %t", "Added group to user");
ds@1952
   512
	} else if (inherit_order > 1) {
ds@1952
   513
		ReplyToCommand(client, "[SM] %t", "Added groups to user", inherit_order);
ds@1952
   514
	}
ds@1952
   515
	
ds@1952
   516
	return Plugin_Handled;
ds@1952
   517
}
ds@1952
   518
ds@1952
   519
public Action:Command_DelGroup(client, args)
ds@1952
   520
{
ds@1952
   521
	if (args < 1)
ds@1952
   522
	{
ds@1952
   523
		ReplyToCommand(client, "[SM] Usage: sm_sql_delgroup <name>");
ds@1952
   524
		return Plugin_Handled;
ds@1952
   525
	}
ds@1952
   526
ds@1952
   527
	new Handle:db = Connect();
ds@1952
   528
	if (db == INVALID_HANDLE)
ds@1952
   529
	{
ds@1952
   530
		ReplyToCommand(client, "[SM] %t", "Could not connect to database");
ds@1952
   531
		return Plugin_Handled;
ds@1952
   532
	}
ds@1952
   533
	
ds@1952
   534
	new len;
ds@1952
   535
	decl String:name[80];
ds@1952
   536
	decl String:safe_name[180];
ds@1952
   537
	GetCmdArgString(name, sizeof(name));
ds@1952
   538
	
ds@1952
   539
	/* Strip quotes in case the user tries to use them */
ds@1952
   540
	len = strlen(name);
ds@1952
   541
	if (len > 1 && (name[0] == '"' && name[len-1] == '"'))
ds@1952
   542
	{
ds@1952
   543
		name[--len] = '\0';
ds@1952
   544
		SQL_EscapeString(db, name[1], safe_name, sizeof(safe_name));
ds@1952
   545
	} else {
ds@1952
   546
		SQL_EscapeString(db, name, safe_name, sizeof(safe_name));
ds@1952
   547
	}
ds@1952
   548
	
ds@1952
   549
	decl String:query[256];
ds@1952
   550
	
ds@1952
   551
	new Handle:hQuery;
ds@1952
   552
	Format(query, sizeof(query), "SELECT id FROM sm_groups WHERE name = '%s'", safe_name);
ds@1952
   553
	if ((hQuery = SQL_Query(db, query)) == INVALID_HANDLE)
ds@1952
   554
	{
ds@1952
   555
		return DoError(client, db, query, "Group retrieval query failed");
ds@1952
   556
	}
ds@1952
   557
	
ds@1952
   558
	if (!SQL_FetchRow(hQuery))
ds@1952
   559
	{
ds@1952
   560
		ReplyToCommand(client, "[SM] %t", "SQL Group not found");
ds@1952
   561
		CloseHandle(hQuery);
ds@1952
   562
		CloseHandle(db);
ds@1952
   563
		return Plugin_Handled;
ds@1952
   564
	}
ds@1952
   565
	
ds@1952
   566
	new id = SQL_FetchInt(hQuery, 0);
ds@1952
   567
	
ds@1952
   568
	CloseHandle(hQuery);
ds@1952
   569
	
ds@1952
   570
	/* Delete admin inheritance for this group */
ds@1952
   571
	Format(query, sizeof(query), "DELETE FROM sm_admins_groups WHERE group_id = %d", id);
ds@1952
   572
	if (!SQL_FastQuery(db, query))
ds@1952
   573
	{
ds@1952
   574
		return DoError(client, db, query, "Admin group deletion query failed");
ds@1952
   575
	}
ds@1952
   576
	
ds@1952
   577
	/* Delete group overrides */
ds@1952
   578
	Format(query, sizeof(query), "DELETE FROM sm_group_overrides WHERE group_id = %d", id);
ds@1952
   579
	if (!SQL_FastQuery(db, query))
ds@1952
   580
	{
ds@1952
   581
		return DoError(client, db, query, "Group override deletion query failed");
ds@1952
   582
	}
ds@1952
   583
	
ds@1952
   584
	/* Delete immunity */
ds@1952
   585
	Format(query, sizeof(query), "DELETE FROM sm_group_immunity WHERE group_id = %d OR other_id = %d", id, id);
ds@1952
   586
	if (!SQL_FastQuery(db, query))
ds@1952
   587
	{
ds@1952
   588
		return DoError(client, db, query, "Group immunity deletion query failed");
ds@1952
   589
	}
ds@1952
   590
	
ds@1952
   591
	/* Finally delete the group */
ds@1952
   592
	Format(query, sizeof(query), "DELETE FROM sm_groups WHERE id = %d", id);
ds@1952
   593
	if (!SQL_FastQuery(db, query))
ds@1952
   594
	{
ds@1952
   595
		return DoError(client, db, query, "Group deletion query failed");
ds@1952
   596
	}
ds@1952
   597
	
ds@1952
   598
	ReplyToCommand(client, "[SM] %t", "SQL Group deleted");
ds@1952
   599
	
ds@1952
   600
	CloseHandle(db);
ds@1952
   601
	
ds@1952
   602
	return Plugin_Handled;
ds@1952
   603
}
ds@1952
   604
ds@1952
   605
public Action:Command_AddGroup(client, args)
ds@1952
   606
{
ds@1952
   607
	if (args < 2)
ds@1952
   608
	{
ds@1952
   609
		ReplyToCommand(client, "[SM] Usage: sm_sql_addgroup <flags> <name> [immunity]");
ds@1952
   610
		return Plugin_Handled;
ds@1952
   611
	}
ds@1952
   612
ds@1952
   613
	new immunity;
ds@1952
   614
	if (args >= 3)
ds@1952
   615
	{
ds@1952
   616
		new String:arg3[32];
ds@1952
   617
		GetCmdArg(3, arg3, sizeof(arg3));
ds@1952
   618
		if (!StringToIntEx(arg3, immunity))
ds@1952
   619
		{
ds@1952
   620
			ReplyToCommand(client, "[SM] %t", "Invalid immunity");
ds@1952
   621
			return Plugin_Handled;
ds@1952
   622
		}
ds@1952
   623
	}
ds@1952
   624
	
ds@1952
   625
	new Handle:db = Connect();
ds@1952
   626
	if (db == INVALID_HANDLE)
ds@1952
   627
	{
ds@1952
   628
		ReplyToCommand(client, "[SM] %t", "Could not connect to database");
ds@1952
   629
		return Plugin_Handled;
ds@1952
   630
	}
ds@1952
   631
	
ds@1952
   632
	decl String:name[64];
ds@1952
   633
	decl String:safe_name[64];
ds@1952
   634
	GetCmdArg(1, name, sizeof(name));
ds@1952
   635
	SQL_EscapeString(db, name, safe_name, sizeof(safe_name));
ds@1952
   636
	
ds@1952
   637
	new Handle:hQuery;
ds@1952
   638
	decl String:query[256];
ds@1952
   639
	Format(query, sizeof(query), "SELECT id FROM sm_groups WHERE name = '%s'", safe_name);
ds@1952
   640
	if ((hQuery = SQL_Query(db, query)) == INVALID_HANDLE)
ds@1952
   641
	{
ds@1952
   642
		return DoError(client, db, query, "Group retrieval query failed");
ds@1952
   643
	}
ds@1952
   644
	
ds@1952
   645
	if (SQL_GetRowCount(hQuery) > 0)
ds@1952
   646
	{
ds@1952
   647
		ReplyToCommand(client, "[SM] %t", "SQL Group already exists");
ds@1952
   648
		CloseHandle(hQuery);
ds@1952
   649
		CloseHandle(db);
ds@1952
   650
		return Plugin_Handled;
ds@1952
   651
	}
ds@1952
   652
	
ds@1952
   653
	CloseHandle(hQuery);
ds@1952
   654
	
ds@1952
   655
	decl String:flags[30];
ds@1952
   656
	decl String:safe_flags[64];
ds@1952
   657
	GetCmdArg(2, flags, sizeof(safe_flags));
ds@1952
   658
	SQL_EscapeString(db, flags, safe_flags, sizeof(safe_flags));
ds@1952
   659
	
ds@1952
   660
	Format(query, 
ds@1952
   661
		sizeof(query),
ds@1952
   662
		"INSERT INTO sm_groups (flags, name, immunity_level) VALUES ('%s', '%s', '%d')",
ds@1952
   663
		safe_name,
ds@1952
   664
		safe_flags,
ds@1952
   665
		immunity);
ds@1952
   666
	
ds@1952
   667
	if (!SQL_FastQuery(db, query))
ds@1952
   668
	{
ds@1952
   669
		return DoError(client, db, query, "Group insertion query failed");
ds@1952
   670
	}
ds@1952
   671
	
ds@1952
   672
	ReplyToCommand(client, "[SM] %t", "SQL Group added");
ds@1952
   673
	
ds@1952
   674
	CloseHandle(db);
ds@1952
   675
		
ds@1952
   676
	return Plugin_Handled;
ds@1952
   677
}	
ds@1952
   678
ds@1952
   679
public Action:Command_DelAdmin(client, args)
ds@1952
   680
{
ds@1952
   681
	if (args < 2)
ds@1952
   682
	{
ds@1952
   683
		ReplyToCommand(client, "[SM] Usage: sm_sql_deladmin <authtype> <identity>");
ds@1952
   684
		ReplyToCommand(client, "[SM] %t", "Invalid authtype");
ds@1952
   685
		return Plugin_Handled;
ds@1952
   686
	}
ds@1952
   687
	
ds@1952
   688
	decl String:authtype[16];
ds@1952
   689
	GetCmdArg(1, authtype, sizeof(authtype));
ds@1952
   690
	
ds@1952
   691
	if (!StrEqual(authtype, "steam")
ds@1952
   692
		&& !StrEqual(authtype, "ip")
ds@1952
   693
		&& !StrEqual(authtype, "name"))
ds@1952
   694
	{
ds@1952
   695
		ReplyToCommand(client, "[SM] %t", "Invalid authtype");
ds@1952
   696
		return Plugin_Handled;
ds@1952
   697
	}
ds@1952
   698
	
ds@1952
   699
	new Handle:db = Connect();
ds@1952
   700
	if (db == INVALID_HANDLE)
ds@1952
   701
	{
ds@1952
   702
		ReplyToCommand(client, "[SM] %t", "Could not connect to database");
ds@1952
   703
		return Plugin_Handled;
ds@1952
   704
	}
ds@1952
   705
	
ds@1952
   706
	decl String:identity[65];
ds@1952
   707
	decl String:safe_identity[140];
ds@1952
   708
	GetCmdArg(2, identity, sizeof(identity));
ds@1952
   709
	SQL_EscapeString(db, identity, safe_identity, sizeof(safe_identity));
ds@1952
   710
	
ds@1952
   711
	decl String:query[255];
ds@1952
   712
	Format(query, 
ds@1952
   713
		sizeof(query),
ds@1952
   714
		"SELECT id FROM sm_admins WHERE authtype = '%s' AND identity = '%s'",
ds@1952
   715
		authtype,
ds@1952
   716
		safe_identity);
ds@1952
   717
		
ds@1952
   718
	new Handle:hQuery;
ds@1952
   719
	if ((hQuery = SQL_Query(db, query)) == INVALID_HANDLE)
ds@1952
   720
	{
ds@1952
   721
		return DoError(client, db, query, "Admin lookup query failed");
ds@1952
   722
	}
ds@1952
   723
	
ds@1952
   724
	if (!SQL_FetchRow(hQuery))
ds@1952
   725
	{
ds@1952
   726
		ReplyToCommand(client, "[SM] %t", "SQL Admin not found");
ds@1952
   727
		CloseHandle(hQuery);
ds@1952
   728
		CloseHandle(db);
ds@1952
   729
		return Plugin_Handled;
ds@1952
   730
	}
ds@1952
   731
	
ds@1952
   732
	new id = SQL_FetchInt(hQuery, 0);
ds@1952
   733
	
ds@1952
   734
	CloseHandle(hQuery);
ds@1952
   735
	
ds@1952
   736
	/* Delete group bindings */
ds@1952
   737
	Format(query, sizeof(query), "DELETE FROM sm_admins_groups WHERE admin_id = %d", id);
ds@1952
   738
	if (!SQL_FastQuery(db, query))
ds@1952
   739
	{
ds@1952
   740
		return DoError(client, db, query, "Admin group deletion query failed");
ds@1952
   741
	}
ds@1952
   742
	
ds@1952
   743
	Format(query, sizeof(query), "DELETE FROM sm_admins WHERE id = %d", id);
ds@1952
   744
	if (!SQL_FastQuery(db, query))
ds@1952
   745
	{
ds@1952
   746
		return DoError(client, db, query, "Admin deletion query failed");
ds@1952
   747
	}
ds@1952
   748
	
ds@1952
   749
	CloseHandle(db);
ds@1952
   750
	
ds@1952
   751
	ReplyToCommand(client, "[SM] %t", "SQL Admin deleted");
ds@1952
   752
	
ds@1952
   753
	return Plugin_Handled;
ds@1952
   754
}
ds@1952
   755
ds@1952
   756
public Action:Command_AddAdmin(client, args)
ds@1952
   757
{
ds@1952
   758
	if (args < 4)
ds@1952
   759
	{
ds@1952
   760
		ReplyToCommand(client, "[SM] Usage: sm_sql_addadmin <alias> <authtype> <identity> <flags> [immunity] [password]");
ds@1952
   761
		ReplyToCommand(client, "[SM] %t", "Invalid authtype");
ds@1952
   762
		return Plugin_Handled;
ds@1952
   763
	}
ds@1952
   764
	
ds@1952
   765
	decl String:authtype[16];
ds@1952
   766
	GetCmdArg(2, authtype, sizeof(authtype));
ds@1952
   767
	
ds@1952
   768
	if (!StrEqual(authtype, "steam")
ds@1952
   769
		&& !StrEqual(authtype, "ip")
ds@1952
   770
		&& !StrEqual(authtype, "name"))
ds@1952
   771
	{
ds@1952
   772
		ReplyToCommand(client, "[SM] %t", "Invalid authtype");
ds@1952
   773
		return Plugin_Handled;
ds@1952
   774
	}
ds@1952
   775
ds@1952
   776
	new immunity;
ds@1952
   777
	if (args >= 5)
ds@1952
   778
	{
ds@1952
   779
		new String:arg5[32];
ds@1952
   780
		GetCmdArg(5, arg5, sizeof(arg5));
ds@1952
   781
		if (!StringToIntEx(arg5, immunity))
ds@1952
   782
		{
ds@1952
   783
			ReplyToCommand(client, "[SM] %t", "Invalid immunity");
ds@1952
   784
			return Plugin_Handled;
ds@1952
   785
		}
ds@1952
   786
	}
ds@1952
   787
	
ds@1952
   788
	decl String:identity[65];
ds@1952
   789
	decl String:safe_identity[140];
ds@1952
   790
	GetCmdArg(3, identity, sizeof(identity));
ds@1952
   791
	
ds@1952
   792
	decl String:query[256];
ds@1952
   793
	new Handle:hQuery;
ds@1952
   794
	new Handle:db = Connect();
ds@1952
   795
	if (db == INVALID_HANDLE)
ds@1952
   796
	{
ds@1952
   797
		ReplyToCommand(client, "[SM] %t", "Could not connect to database");
ds@1952
   798
		return Plugin_Handled;
ds@1952
   799
	}
ds@1952
   800
	
ds@1952
   801
	SQL_EscapeString(db, identity, safe_identity, sizeof(safe_identity));
ds@1952
   802
	
ds@1952
   803
	Format(query, sizeof(query), "SELECT id FROM sm_admins WHERE authtype = '%s' AND identity = '%s'", authtype, identity);
ds@1952
   804
	if ((hQuery = SQL_Query(db, query)) == INVALID_HANDLE)
ds@1952
   805
	{
ds@1952
   806
		return DoError(client, db, query, "Admin retrieval query failed");
ds@1952
   807
	}
ds@1952
   808
	
ds@1952
   809
	if (SQL_GetRowCount(hQuery) > 0)
ds@1952
   810
	{
ds@1952
   811
		ReplyToCommand(client, "[SM] %t", "SQL Admin already exists");
ds@1952
   812
		CloseHandle(hQuery);
ds@1952
   813
		CloseHandle(db);
ds@1952
   814
		return Plugin_Handled;
ds@1952
   815
	}
ds@1952
   816
	
ds@1952
   817
	CloseHandle(hQuery);
ds@1952
   818
	
ds@1952
   819
	decl String:alias[64];
ds@1952
   820
	decl String:safe_alias[140];
ds@1952
   821
	GetCmdArg(1, alias, sizeof(alias));
ds@1952
   822
	SQL_EscapeString(db, alias, safe_alias, sizeof(safe_alias));
ds@1952
   823
	
ds@1952
   824
	decl String:flags[30];
ds@1952
   825
	decl String:safe_flags[64];
ds@1952
   826
	GetCmdArg(4, flags, sizeof(flags));
ds@1952
   827
	SQL_EscapeString(db, flags, safe_flags, sizeof(safe_flags));
ds@1952
   828
	
ds@1952
   829
	decl String:password[32];
ds@1952
   830
	decl String:safe_password[80];
ds@1952
   831
	if (args > 4)
ds@1952
   832
	{
ds@1952
   833
		GetCmdArg(5, password, sizeof(password));
ds@1952
   834
		SQL_EscapeString(db, password, safe_password, sizeof(safe_password));
ds@1952
   835
	} else {
ds@1952
   836
		safe_password[0] = '\0';
ds@1952
   837
	}
ds@1952
   838
	
ds@1952
   839
	new len = 0;
ds@1952
   840
	len += Format(query[len], sizeof(query)-len, "INSERT INTO sm_admins (authtype, identity, password, flags, name, immunity) VALUES");
ds@1952
   841
	if (safe_password[0] == '\0')
ds@1952
   842
	{
ds@1952
   843
		len += Format(query[len], sizeof(query)-len, " ('%s', '%s', NULL, '%s', '%s', %d)", authtype, safe_identity, safe_flags, safe_alias, immunity);
ds@1952
   844
	} else {
ds@1952
   845
		len += Format(query[len], sizeof(query)-len, " ('%s', '%s', '%s', '%s', '%s', %d)", authtype, safe_identity, safe_password, safe_flags, safe_alias, immunity);
ds@1952
   846
	}
ds@1952
   847
	
ds@1952
   848
	if (!SQL_FastQuery(db, query))
ds@1952
   849
	{
ds@1952
   850
		return DoError(client, db, query, "Admin insertion query failed");
ds@1952
   851
	}
ds@1952
   852
	
ds@1952
   853
	ReplyToCommand(client, "[SM] %t", "SQL Admin added");
ds@1952
   854
	
ds@1952
   855
	CloseHandle(db);
ds@1952
   856
		
ds@1952
   857
	return Plugin_Handled;
ds@1952
   858
}
ds@1952
   859
ds@1952
   860
stock bool:DoQuery(client, Handle:db, const String:query[])
ds@1952
   861
{
ds@1952
   862
	if (!SQL_FastQuery(db, query))
ds@1952
   863
	{
ds@1952
   864
		decl String:error[255];
ds@1952
   865
		SQL_GetError(db, error, sizeof(error));
ds@1952
   866
		LogError("Query failed: %s", error);
ds@1952
   867
		LogError("Query dump: %s", query);
ds@1952
   868
		ReplyToCommand(client, "[SM] %t", "Failed to query database");
ds@1952
   869
		return false;
ds@1952
   870
	}
ds@1952
   871
ds@1952
   872
	return true;
ds@1952
   873
}
ds@1952
   874
ds@1952
   875
stock Action:DoError(client, Handle:db, const String:query[], const String:msg[])
ds@1952
   876
{
ds@1952
   877
		decl String:error[255];
ds@1952
   878
		SQL_GetError(db, error, sizeof(error));
ds@1952
   879
		LogError("%s: %s", msg, error);
ds@1952
   880
		LogError("Query dump: %s", query);
ds@1952
   881
		CloseHandle(db);
ds@1952
   882
		ReplyToCommand(client, "[SM] %t", "Failed to query database");
ds@1952
   883
		return Plugin_Handled;
ds@1952
   884
}
ds@1952
   885
ds@1952
   886
stock Action:DoStmtError(client, Handle:db, const String:query[], const String:error[], const String:msg[])
ds@1952
   887
{
ds@1952
   888
		LogError("%s: %s", msg, error);
ds@1952
   889
		LogError("Query dump: %s", query);
ds@1952
   890
		CloseHandle(db);
ds@1952
   891
		ReplyToCommand(client, "[SM] %t", "Failed to query database");
ds@1952
   892
		return Plugin_Handled;
ds@1952
   893
}
ds@1952
   894