3 * =============================================================================
4 * SourceMod SQL Admin Manager Plugin
5 * Adds/managers admins and groups in an SQL database.
7 * SourceMod (C)2004-2007 AlliedModders LLC. All rights reserved.
8 * =============================================================================
10 * This program is free software; you can redistribute it and/or modify it under
11 * the terms of the GNU General Public License, version 3.0, as published by the
12 * Free Software Foundation.
14 * This program is distributed in the hope that it will be useful, but WITHOUT
15 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
19 * You should have received a copy of the GNU General Public License along with
20 * this program. If not, see <http://www.gnu.org/licenses/>.
22 * As a special exception, AlliedModders LLC gives you permission to link the
23 * code of this program (as well as its derivative works) to "Half-Life 2," the
24 * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
25 * by the Valve Corporation. You must obey the GNU General Public License in
26 * all respects for all other code used. Additionally, AlliedModders LLC grants
27 * this exception to all derivative works. AlliedModders LLC defines further
28 * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
29 * or <http://www.sourcemod.net/license.php>.
34 /* We like semicolons */
39 #define CURRENT_SCHEMA_VERSION 1409
40 #define SCHEMA_UPGRADE_1 1409
42 new current_version[4] = {1, 0, 0, CURRENT_SCHEMA_VERSION};
44 public Plugin:myinfo =
46 name = "SQL Admin Manager",
47 author = "AlliedModders LLC",
48 description = "Manages SQL admins",
49 version = SOURCEMOD_VERSION,
50 url = "http://www.sourcemod.net/"
53 public OnPluginStart()
55 LoadTranslations("common.phrases");
56 LoadTranslations("sqladmins.phrases");
58 RegAdminCmd("sm_sql_addadmin", Command_AddAdmin, ADMFLAG_ROOT, "Adds an admin to the SQL database");
59 RegAdminCmd("sm_sql_deladmin", Command_DelAdmin, ADMFLAG_ROOT, "Removes an admin from the SQL database");
60 RegAdminCmd("sm_sql_addgroup", Command_AddGroup, ADMFLAG_ROOT, "Adds a group to the SQL database");
61 RegAdminCmd("sm_sql_delgroup", Command_DelGroup, ADMFLAG_ROOT, "Removes a group from the SQL database");
62 RegAdminCmd("sm_sql_setadmingroups", Command_SetAdminGroups, ADMFLAG_ROOT, "Sets an admin's groups in the SQL database");
63 RegServerCmd("sm_create_adm_tables", Command_CreateTables);
64 RegServerCmd("sm_update_adm_tables", Command_UpdateTables);
69 decl String:error[255];
72 if (SQL_CheckConfig("admins"))
74 db = SQL_Connect("admins", true, error, sizeof(error));
76 db = SQL_Connect("default", true, error, sizeof(error));
79 if (db == INVALID_HANDLE)
81 LogError("Could not connect to database: %s", error);
87 CreateMySQL(client, Handle:db)
89 new String:queries[7][] =
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))",
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))",
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))",
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))",
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))",
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))",
97 "CREATE TABLE IF NOT EXISTS sm_config (cfg_key varchar(32) NOT NULL, cfg_value varchar(255) NOT NULL, PRIMARY KEY (cfg_key))"
100 for (new i = 0; i < 7; i++)
102 if (!DoQuery(client, db, queries[i]))
108 decl String:query[256];
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'",
112 CURRENT_SCHEMA_VERSION,
113 CURRENT_SCHEMA_VERSION);
115 if (!DoQuery(client, db, query))
120 ReplyToCommand(client, "[SM] Admin tables have been created.");
123 CreateSQLite(client, Handle:db)
125 new String:queries[7][] =
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)",
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)",
129 "CREATE TABLE sm_group_immunity (group_id INTEGER NOT NULL, other_id INTEGER NOT NULL, PRIMARY KEY (group_id, other_id))",
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))",
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))",
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))",
133 "CREATE TABLE IF NOT EXISTS sm_config (cfg_key varchar(32) NOT NULL, cfg_value varchar(255) NOT NULL, PRIMARY KEY (cfg_key))"
136 for (new i = 0; i < 7; i++)
138 if (!DoQuery(client, db, queries[i]))
144 decl String:query[256];
147 "REPLACE INTO sm_config (cfg_key, cfg_value) VALUES ('admin_version', '1.0.0.%d')",
148 CURRENT_SCHEMA_VERSION);
150 if (!DoQuery(client, db, query))
155 ReplyToCommand(client, "[SM] Admin tables have been created.");
158 public Action:Command_CreateTables(args)
161 new Handle:db = Connect();
162 if (db == INVALID_HANDLE)
164 ReplyToCommand(client, "[SM] %t", "Could not connect to database");
165 return Plugin_Handled;
168 new String:ident[16];
169 SQL_ReadDriver(db, ident, sizeof(ident));
171 if (strcmp(ident, "mysql") == 0)
173 CreateMySQL(client, db);
174 } else if (strcmp(ident, "sqlite") == 0) {
175 CreateSQLite(client, db);
177 ReplyToCommand(client, "[SM] Unknown driver type '%s', cannot create tables.", ident);
182 return Plugin_Handled;
185 bool:GetUpdateVersion(client, Handle:db, versions[4])
187 decl String:query[256];
190 Format(query, sizeof(query), "SELECT cfg_value FROM sm_config WHERE cfg_key = 'admin_version'");
191 if ((hQuery = SQL_Query(db, query)) == INVALID_HANDLE)
193 DoError(client, db, query, "Version lookup query failed");
196 if (SQL_FetchRow(hQuery))
198 decl String:version_string[255];
199 SQL_FetchString(hQuery, 0, version_string, sizeof(version_string));
201 decl String:version_numbers[4][12];
202 if (ExplodeString(version_string, ".", version_numbers, 4, 12) == 4)
204 for (new i = 0; i < 4; i++)
206 versions[i] = StringToInt(version_numbers[i]);
213 if (current_version[3] < versions[3])
215 ReplyToCommand(client, "[SM] The database is newer than the expected version.");
219 if (current_version[3] == versions[3])
221 ReplyToCommand(client, "[SM] Your tables are already up to date.");
229 UpdateSQLite(client, Handle:db)
231 decl String:query[512];
234 Format(query, sizeof(query), "SELECT name FROM sqlite_master WHERE type = 'table' AND name = 'sm_config'");
235 if ((hQuery = SQL_Query(db, query)) == INVALID_HANDLE)
237 DoError(client, db, query, "Table lookup query failed");
241 new bool:found = SQL_FetchRow(hQuery);
248 if (!GetUpdateVersion(client, db, versions))
254 /* We only know about one upgrade path right now...
257 if (versions[3] < SCHEMA_UPGRADE_1)
259 new String:queries[8][] =
261 "ALTER TABLE sm_admins ADD immunity INTEGER DEFAULT 0 NOT NULL",
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)",
263 "INSERT INTO _sm_groups_temp (id, flags, name) SELECT id, flags, name FROM sm_groups",
264 "UPDATE _sm_groups_temp SET immunity_level = 2 WHERE id IN (SELECT g.id FROM sm_groups g WHERE g.immunity = 'global')",
265 "UPDATE _sm_groups_temp SET immunity_level = 1 WHERE id IN (SELECT g.id FROM sm_groups g WHERE g.immunity = 'default')",
266 "DROP TABLE sm_groups",
267 "ALTER TABLE _sm_groups_temp RENAME TO sm_groups",
268 "CREATE TABLE IF NOT EXISTS sm_config (cfg_key varchar(32) NOT NULL, cfg_value varchar(255) NOT NULL, PRIMARY KEY (cfg_key))"
271 for (new i = 0; i < 8; i++)
273 if (!DoQuery(client, db, queries[i]))
281 "REPLACE INTO sm_config (cfg_key, cfg_value) VALUES ('admin_version', '1.0.0.%d')",
284 if (!DoQuery(client, db, query))
289 versions[3] = SCHEMA_UPGRADE_1;
292 ReplyToCommand(client, "[SM] Your tables are now up to date.");
295 UpdateMySQL(client, Handle:db)
297 decl String:query[512];
300 Format(query, sizeof(query), "SHOW TABLES");
301 if ((hQuery = SQL_Query(db, query)) == INVALID_HANDLE)
303 DoError(client, db, query, "Table lookup query failed");
307 decl String:table[64];
308 new bool:found = false;
309 while (SQL_FetchRow(hQuery))
311 SQL_FetchString(hQuery, 0, table, sizeof(table));
312 if (strcmp(table, "sm_config") == 0)
321 if (found && !GetUpdateVersion(client, db, versions))
326 /* We only know about one upgrade path right now...
329 if (versions[3] < SCHEMA_UPGRADE_1)
331 new String:queries[6][] =
333 "CREATE TABLE IF NOT EXISTS sm_config (cfg_key varchar(32) NOT NULL, cfg_value varchar(255) NOT NULL, PRIMARY KEY (cfg_key))",
334 "ALTER TABLE sm_admins ADD immunity INT UNSIGNED NOT NULL",
335 "ALTER TABLE sm_groups ADD immunity_level INT UNSIGNED NOT NULL",
336 "UPDATE sm_groups SET immunity_level = 2 WHERE immunity = 'default'",
337 "UPDATE sm_groups SET immunity_level = 1 WHERE immunity = 'global'",
338 "ALTER TABLE sm_groups DROP immunity"
341 for (new i = 0; i < 6; i++)
343 if (!DoQuery(client, db, queries[i]))
349 decl String:upgr[48];
350 Format(upgr, sizeof(upgr), "1.0.0.%d", SCHEMA_UPGRADE_1);
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);
353 if (!DoQuery(client, db, query))
358 versions[3] = SCHEMA_UPGRADE_1;
361 ReplyToCommand(client, "[SM] Your tables are now up to date.");
364 public Action:Command_UpdateTables(args)
367 new Handle:db = Connect();
368 if (db == INVALID_HANDLE)
370 ReplyToCommand(client, "[SM] %t", "Could not connect to database");
371 return Plugin_Handled;
374 new String:ident[16];
375 SQL_ReadDriver(db, ident, sizeof(ident));
377 if (strcmp(ident, "mysql") == 0)
379 UpdateMySQL(client, db);
380 } else if (strcmp(ident, "sqlite") == 0) {
381 UpdateSQLite(client, db);
383 ReplyToCommand(client, "[SM] Unknown driver type, cannot upgrade.");
388 return Plugin_Handled;
391 public Action:Command_SetAdminGroups(client, args)
395 ReplyToCommand(client, "[SM] Usage: sm_sql_setadmingroups <authtype> <identity> [group1] ... [group N]");
396 return Plugin_Handled;
399 decl String:authtype[16];
400 GetCmdArg(1, authtype, sizeof(authtype));
402 if (!StrEqual(authtype, "steam")
403 && !StrEqual(authtype, "ip")
404 && !StrEqual(authtype, "name"))
406 ReplyToCommand(client, "[SM] %t", "Invalid authtype");
407 return Plugin_Handled;
410 new Handle:db = Connect();
411 if (db == INVALID_HANDLE)
413 ReplyToCommand(client, "[SM] %t", "Could not connect to database");
414 return Plugin_Handled;
417 decl String:identity[65];
418 decl String:safe_identity[140];
419 GetCmdArg(2, identity, sizeof(identity));
420 SQL_QuoteString(db, identity, safe_identity, sizeof(safe_identity));
422 decl String:query[255];
425 "SELECT id FROM sm_admins WHERE authtype = '%s' AND identity = '%s'",
430 if ((hQuery = SQL_Query(db, query)) == INVALID_HANDLE)
432 return DoError(client, db, query, "Admin lookup query failed");
435 if (!SQL_FetchRow(hQuery))
437 ReplyToCommand(client, "[SM] %t", "SQL Admin not found");
440 return Plugin_Handled;
443 new id = SQL_FetchInt(hQuery, 0);
448 * First delete all of the user's existing groups.
450 Format(query, sizeof(query), "DELETE FROM sm_admins_groups WHERE admin_id = %d", id);
451 if (!SQL_FastQuery(db, query))
453 return DoError(client, db, query, "Admin group deletion query failed");
458 ReplyToCommand(client, "[SM] %t", "SQL Admin groups reset");
460 return Plugin_Handled;
463 decl String:error[256];
464 new Handle:hAddQuery, Handle:hFindQuery;
466 Format(query, sizeof(query), "SELECT id FROM sm_groups WHERE name = ?");
467 if ((hFindQuery = SQL_PrepareQuery(db, query, error, sizeof(error))) == INVALID_HANDLE)
469 return DoStmtError(client, db, query, error, "Group search prepare failed");
474 "INSERT INTO sm_admins_groups (admin_id, group_id, inherit_order) VALUES (%d, ?, ?)",
476 if ((hAddQuery = SQL_PrepareQuery(db, query, error, sizeof(error))) == INVALID_HANDLE)
478 CloseHandle(hFindQuery);
479 return DoStmtError(client, db, query, error, "Add admin group prepare failed");
482 decl String:name[80];
483 new inherit_order = 0;
484 for (new i=3; i<=args; i++)
486 GetCmdArg(i, name, sizeof(name));
488 SQL_BindParamString(hFindQuery, 0, name, false);
489 if (!SQL_Execute(hFindQuery) || !SQL_FetchRow(hFindQuery))
491 ReplyToCommand(client, "[SM] %t", "SQL Group X not found", name);
493 new gid = SQL_FetchInt(hFindQuery, 0);
495 SQL_BindParamInt(hAddQuery, 0, gid);
496 SQL_BindParamInt(hAddQuery, 1, ++inherit_order);
497 if (!SQL_Execute(hAddQuery))
499 ReplyToCommand(client, "[SM] %t", "SQL Group X failed to bind", name);
505 CloseHandle(hAddQuery);
506 CloseHandle(hFindQuery);
509 if (inherit_order == 1)
511 ReplyToCommand(client, "[SM] %t", "Added group to user");
512 } else if (inherit_order > 1) {
513 ReplyToCommand(client, "[SM] %t", "Added groups to user", inherit_order);
516 return Plugin_Handled;
519 public Action:Command_DelGroup(client, args)
523 ReplyToCommand(client, "[SM] Usage: sm_sql_delgroup <name>");
524 return Plugin_Handled;
527 new Handle:db = Connect();
528 if (db == INVALID_HANDLE)
530 ReplyToCommand(client, "[SM] %t", "Could not connect to database");
531 return Plugin_Handled;
535 decl String:name[80];
536 decl String:safe_name[180];
537 GetCmdArgString(name, sizeof(name));
539 /* Strip quotes in case the user tries to use them */
541 if (len > 1 && (name[0] == '"' && name[len-1] == '"'))
544 SQL_QuoteString(db, name[1], safe_name, sizeof(safe_name));
546 SQL_QuoteString(db, name, safe_name, sizeof(safe_name));
549 decl String:query[256];
552 Format(query, sizeof(query), "SELECT id FROM sm_groups WHERE name = '%s'", safe_name);
553 if ((hQuery = SQL_Query(db, query)) == INVALID_HANDLE)
555 return DoError(client, db, query, "Group retrieval query failed");
558 if (!SQL_FetchRow(hQuery))
560 ReplyToCommand(client, "[SM] %t", "SQL Group not found");
563 return Plugin_Handled;
566 new id = SQL_FetchInt(hQuery, 0);
570 /* Delete admin inheritance for this group */
571 Format(query, sizeof(query), "DELETE FROM sm_admins_groups WHERE group_id = %d", id);
572 if (!SQL_FastQuery(db, query))
574 return DoError(client, db, query, "Admin group deletion query failed");
577 /* Delete group overrides */
578 Format(query, sizeof(query), "DELETE FROM sm_group_overrides WHERE group_id = %d", id);
579 if (!SQL_FastQuery(db, query))
581 return DoError(client, db, query, "Group override deletion query failed");
584 /* Delete immunity */
585 Format(query, sizeof(query), "DELETE FROM sm_group_immunity WHERE group_id = %d OR other_id = %d", id, id);
586 if (!SQL_FastQuery(db, query))
588 return DoError(client, db, query, "Group immunity deletion query failed");
591 /* Finally delete the group */
592 Format(query, sizeof(query), "DELETE FROM sm_groups WHERE id = %d", id);
593 if (!SQL_FastQuery(db, query))
595 return DoError(client, db, query, "Group deletion query failed");
598 ReplyToCommand(client, "[SM] %t", "SQL Group deleted");
602 return Plugin_Handled;
605 public Action:Command_AddGroup(client, args)
609 ReplyToCommand(client, "[SM] Usage: sm_sql_addgroup <name> <flags> [immunity]");
610 return Plugin_Handled;
617 GetCmdArg(3, arg3, sizeof(arg3));
618 if (!StringToIntEx(arg3, immunity))
620 ReplyToCommand(client, "[SM] %t", "Invalid immunity");
621 return Plugin_Handled;
625 new Handle:db = Connect();
626 if (db == INVALID_HANDLE)
628 ReplyToCommand(client, "[SM] %t", "Could not connect to database");
629 return Plugin_Handled;
632 decl String:name[64];
633 decl String:safe_name[64];
634 GetCmdArg(1, name, sizeof(name));
635 SQL_QuoteString(db, name, safe_name, sizeof(safe_name));
638 decl String:query[256];
639 Format(query, sizeof(query), "SELECT id FROM sm_groups WHERE name = '%s'", safe_name);
640 if ((hQuery = SQL_Query(db, query)) == INVALID_HANDLE)
642 return DoError(client, db, query, "Group retrieval query failed");
645 if (SQL_GetRowCount(hQuery) > 0)
647 ReplyToCommand(client, "[SM] %t", "SQL Group already exists");
650 return Plugin_Handled;
655 decl String:flags[30];
656 decl String:safe_flags[64];
657 GetCmdArg(2, flags, sizeof(safe_flags));
658 SQL_QuoteString(db, flags, safe_flags, sizeof(safe_flags));
662 "INSERT INTO sm_groups (flags, name, immunity_level) VALUES ('%s', '%s', '%d')",
666 if (!SQL_FastQuery(db, query))
668 return DoError(client, db, query, "Group insertion query failed");
671 ReplyToCommand(client, "[SM] %t", "SQL Group added");
675 return Plugin_Handled;
678 public Action:Command_DelAdmin(client, args)
682 ReplyToCommand(client, "[SM] Usage: sm_sql_deladmin <authtype> <identity>");
683 ReplyToCommand(client, "[SM] %t", "Invalid authtype");
684 return Plugin_Handled;
687 decl String:authtype[16];
688 GetCmdArg(1, authtype, sizeof(authtype));
690 if (!StrEqual(authtype, "steam")
691 && !StrEqual(authtype, "ip")
692 && !StrEqual(authtype, "name"))
694 ReplyToCommand(client, "[SM] %t", "Invalid authtype");
695 return Plugin_Handled;
698 new Handle:db = Connect();
699 if (db == INVALID_HANDLE)
701 ReplyToCommand(client, "[SM] %t", "Could not connect to database");
702 return Plugin_Handled;
705 decl String:identity[65];
706 decl String:safe_identity[140];
707 GetCmdArg(2, identity, sizeof(identity));
708 SQL_QuoteString(db, identity, safe_identity, sizeof(safe_identity));
710 decl String:query[255];
713 "SELECT id FROM sm_admins WHERE authtype = '%s' AND identity = '%s'",
718 if ((hQuery = SQL_Query(db, query)) == INVALID_HANDLE)
720 return DoError(client, db, query, "Admin lookup query failed");
723 if (!SQL_FetchRow(hQuery))
725 ReplyToCommand(client, "[SM] %t", "SQL Admin not found");
728 return Plugin_Handled;
731 new id = SQL_FetchInt(hQuery, 0);
735 /* Delete group bindings */
736 Format(query, sizeof(query), "DELETE FROM sm_admins_groups WHERE admin_id = %d", id);
737 if (!SQL_FastQuery(db, query))
739 return DoError(client, db, query, "Admin group deletion query failed");
742 Format(query, sizeof(query), "DELETE FROM sm_admins WHERE id = %d", id);
743 if (!SQL_FastQuery(db, query))
745 return DoError(client, db, query, "Admin deletion query failed");
750 ReplyToCommand(client, "[SM] %t", "SQL Admin deleted");
752 return Plugin_Handled;
755 public Action:Command_AddAdmin(client, args)
759 ReplyToCommand(client, "[SM] Usage: sm_sql_addadmin <alias> <authtype> <identity> <flags> [immunity] [password]");
760 ReplyToCommand(client, "[SM] %t", "Invalid authtype");
761 return Plugin_Handled;
764 decl String:authtype[16];
765 GetCmdArg(2, authtype, sizeof(authtype));
767 if (!StrEqual(authtype, "steam")
768 && !StrEqual(authtype, "ip")
769 && !StrEqual(authtype, "name"))
771 ReplyToCommand(client, "[SM] %t", "Invalid authtype");
772 return Plugin_Handled;
779 GetCmdArg(5, arg5, sizeof(arg5));
780 if (!StringToIntEx(arg5, immunity))
782 ReplyToCommand(client, "[SM] %t", "Invalid immunity");
783 return Plugin_Handled;
787 decl String:identity[65];
788 decl String:safe_identity[140];
789 GetCmdArg(3, identity, sizeof(identity));
791 decl String:query[256];
793 new Handle:db = Connect();
794 if (db == INVALID_HANDLE)
796 ReplyToCommand(client, "[SM] %t", "Could not connect to database");
797 return Plugin_Handled;
800 SQL_QuoteString(db, identity, safe_identity, sizeof(safe_identity));
802 Format(query, sizeof(query), "SELECT id FROM sm_admins WHERE authtype = '%s' AND identity = '%s'", authtype, identity);
803 if ((hQuery = SQL_Query(db, query)) == INVALID_HANDLE)
805 return DoError(client, db, query, "Admin retrieval query failed");
808 if (SQL_GetRowCount(hQuery) > 0)
810 ReplyToCommand(client, "[SM] %t", "SQL Admin already exists");
813 return Plugin_Handled;
818 decl String:alias[64];
819 decl String:safe_alias[140];
820 GetCmdArg(1, alias, sizeof(alias));
821 SQL_QuoteString(db, alias, safe_alias, sizeof(safe_alias));
823 decl String:flags[30];
824 decl String:safe_flags[64];
825 GetCmdArg(4, flags, sizeof(flags));
826 SQL_QuoteString(db, flags, safe_flags, sizeof(safe_flags));
828 decl String:password[32];
829 decl String:safe_password[80];
832 GetCmdArg(5, password, sizeof(password));
833 SQL_QuoteString(db, password, safe_password, sizeof(safe_password));
835 safe_password[0] = '\0';
839 len += Format(query[len], sizeof(query)-len, "INSERT INTO sm_admins (authtype, identity, password, flags, name, immunity) VALUES");
840 if (safe_password[0] == '\0')
842 len += Format(query[len], sizeof(query)-len, " ('%s', '%s', NULL, '%s', '%s', %d)", authtype, safe_identity, safe_flags, safe_alias, immunity);
844 len += Format(query[len], sizeof(query)-len, " ('%s', '%s', '%s', '%s', '%s', %d)", authtype, safe_identity, safe_password, safe_flags, safe_alias, immunity);
847 if (!SQL_FastQuery(db, query))
849 return DoError(client, db, query, "Admin insertion query failed");
852 ReplyToCommand(client, "[SM] %t", "SQL Admin added");
856 return Plugin_Handled;
859 stock bool:DoQuery(client, Handle:db, const String:query[])
861 if (!SQL_FastQuery(db, query))
863 decl String:error[255];
864 SQL_GetError(db, error, sizeof(error));
865 LogError("Query failed: %s", error);
866 LogError("Query dump: %s", query);
867 ReplyToCommand(client, "[SM] %t", "Failed to query database");
874 stock Action:DoError(client, Handle:db, const String:query[], const String:msg[])
876 decl String:error[255];
877 SQL_GetError(db, error, sizeof(error));
878 LogError("%s: %s", msg, error);
879 LogError("Query dump: %s", query);
881 ReplyToCommand(client, "[SM] %t", "Failed to query database");
882 return Plugin_Handled;
885 stock Action:DoStmtError(client, Handle:db, const String:query[], const String:error[], const String:msg[])
887 LogError("%s: %s", msg, error);
888 LogError("Query dump: %s", query);
890 ReplyToCommand(client, "[SM] %t", "Failed to query database");
891 return Plugin_Handled;