/addons/sourcemod/scripting/admin-sql-threaded.sp

https://bitbucket.org/kimoto/sushi · Unknown · 857 lines · 735 code · 122 blank · 0 comment · 0 complexity · 4e8ad4dacfbf1fa505f906ee6cc1e7e5 MD5 · raw file

  1. /**
  2. * vim: set ts=4 :
  3. * =============================================================================
  4. * SourceMod SQL Admins Plugin (Threaded)
  5. * Fetches admins from an SQL database dynamically.
  6. *
  7. * SourceMod (C)2004-2008 AlliedModders LLC. All rights reserved.
  8. * =============================================================================
  9. *
  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.
  13. *
  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
  17. * details.
  18. *
  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/>.
  21. *
  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>.
  30. *
  31. * Version: $Id$
  32. */
  33. /* We like semicolons */
  34. #pragma semicolon 1
  35. #include <sourcemod>
  36. public Plugin:myinfo =
  37. {
  38. name = "SQL Admins (Threaded)",
  39. author = "AlliedModders LLC",
  40. description = "Reads admins from SQL dynamically",
  41. version = SOURCEMOD_VERSION,
  42. url = "http://www.sourcemod.net/"
  43. };
  44. /**
  45. * Notes:
  46. *
  47. * 1) All queries in here are high priority. This is because the admin stuff
  48. * is very important. Do not take this to mean that in your script,
  49. * everything should be high priority.
  50. *
  51. * 2) All callbacks are locked with "sequence numbers." This is to make sure
  52. * that multiple calls to sm_reloadadmins and the like do not make us
  53. * store the results from two or more callbacks accidentally. Instead, we
  54. * check the sequence number in each callback with the current "allowed"
  55. * sequence number, and if it doesn't match, the callback is cancelled.
  56. *
  57. * 3) Sequence numbers for groups and overrides are not cleared unless there
  58. * was a 100% success in the fetch. This is so we can potentially implement
  59. * connection retries in the future.
  60. *
  61. * 4) Sequence numbers for the user cache are ignored except for being
  62. * non-zero, which means players in-game should be re-checked for admin
  63. * powers.
  64. */
  65. new Handle:hDatabase = INVALID_HANDLE; /** Database connection */
  66. new g_sequence = 0; /** Global unique sequence number */
  67. new ConnectLock = 0; /** Connect sequence number */
  68. new RebuildCachePart[3] = {0}; /** Cache part sequence numbers */
  69. new PlayerSeq[MAXPLAYERS+1]; /** Player-specific sequence numbers */
  70. new bool:PlayerAuth[MAXPLAYERS+1]; /** Whether a player has been "pre-authed" */
  71. //#define _DEBUG
  72. public OnMapEnd()
  73. {
  74. /**
  75. * Clean up on map end just so we can start a fresh connection when we need it later.
  76. */
  77. if (hDatabase != INVALID_HANDLE)
  78. {
  79. CloseHandle(hDatabase);
  80. hDatabase = INVALID_HANDLE;
  81. }
  82. }
  83. public bool:OnClientConnect(client, String:rejectmsg[], maxlen)
  84. {
  85. PlayerSeq[client] = 0;
  86. PlayerAuth[client] = false;
  87. return true;
  88. }
  89. public OnClientDisconnect(client)
  90. {
  91. PlayerSeq[client] = 0;
  92. PlayerAuth[client] = false;
  93. }
  94. public OnDatabaseConnect(Handle:owner, Handle:hndl, const String:error[], any:data)
  95. {
  96. #if defined _DEBUG
  97. PrintToServer("OnDatabaseConnect(%x,%x,%d) ConnectLock=%d", owner, hndl, data, ConnectLock);
  98. #endif
  99. /**
  100. * If this happens to be an old connection request, ignore it.
  101. */
  102. if (data != ConnectLock || hDatabase != INVALID_HANDLE)
  103. {
  104. if (hndl != INVALID_HANDLE)
  105. {
  106. CloseHandle(hndl);
  107. }
  108. return;
  109. }
  110. ConnectLock = 0;
  111. hDatabase = hndl;
  112. /**
  113. * See if the connection is valid. If not, don't un-mark the caches
  114. * as needing rebuilding, in case the next connection request works.
  115. */
  116. if (hDatabase == INVALID_HANDLE)
  117. {
  118. LogError("Failed to connect to database: %s", error);
  119. return;
  120. }
  121. /**
  122. * See if we need to get any of the cache stuff now.
  123. */
  124. new sequence;
  125. if ((sequence = RebuildCachePart[_:AdminCache_Overrides]) != 0)
  126. {
  127. FetchOverrides(hDatabase, sequence);
  128. }
  129. if ((sequence = RebuildCachePart[_:AdminCache_Groups]) != 0)
  130. {
  131. FetchGroups(hDatabase, sequence);
  132. }
  133. if ((sequence = RebuildCachePart[_:AdminCache_Admins]) != 0)
  134. {
  135. FetchUsersWeCan(hDatabase);
  136. }
  137. }
  138. RequestDatabaseConnection()
  139. {
  140. ConnectLock = ++g_sequence;
  141. if (SQL_CheckConfig("admins"))
  142. {
  143. SQL_TConnect(OnDatabaseConnect, "admins", ConnectLock);
  144. } else {
  145. SQL_TConnect(OnDatabaseConnect, "default", ConnectLock);
  146. }
  147. }
  148. public OnRebuildAdminCache(AdminCachePart:part)
  149. {
  150. /**
  151. * Mark this part of the cache as being rebuilt. This is used by the
  152. * callback system to determine whether the results should still be
  153. * used.
  154. */
  155. new sequence = ++g_sequence;
  156. RebuildCachePart[_:part] = sequence;
  157. /**
  158. * If we don't have a database connection, we can't do any lookups just yet.
  159. */
  160. if (!hDatabase)
  161. {
  162. /**
  163. * Ask for a new connection if we need it.
  164. */
  165. if (!ConnectLock)
  166. {
  167. RequestDatabaseConnection();
  168. }
  169. return;
  170. }
  171. if (part == AdminCache_Overrides)
  172. {
  173. FetchOverrides(hDatabase, sequence);
  174. } else if (part == AdminCache_Groups) {
  175. FetchGroups(hDatabase, sequence);
  176. } else if (part == AdminCache_Admins) {
  177. FetchUsersWeCan(hDatabase);
  178. }
  179. }
  180. public Action:OnClientPreAdminCheck(client)
  181. {
  182. PlayerAuth[client] = true;
  183. /**
  184. * Play nice with other plugins. If there's no database, don't delay the
  185. * connection process. Unfortunately, we can't attempt anything else and
  186. * we just have to hope either the database is waiting or someone will type
  187. * sm_reloadadmins.
  188. */
  189. if (hDatabase == INVALID_HANDLE)
  190. {
  191. return Plugin_Continue;
  192. }
  193. /**
  194. * Similarly, if the cache is in the process of being rebuilt, don't delay
  195. * the user's normal connection flow. The database will soon auth the user
  196. * normally.
  197. */
  198. if (RebuildCachePart[_:AdminCache_Admins] != 0)
  199. {
  200. return Plugin_Continue;
  201. }
  202. /**
  203. * If someone has already assigned an admin ID (bad bad bad), don't
  204. * bother waiting.
  205. */
  206. if (GetUserAdmin(client) != INVALID_ADMIN_ID)
  207. {
  208. return Plugin_Continue;
  209. }
  210. FetchUser(hDatabase, client);
  211. return Plugin_Handled;
  212. }
  213. public OnReceiveUserGroups(Handle:owner, Handle:hndl, const String:error[], any:data)
  214. {
  215. new Handle:pk = Handle:data;
  216. ResetPack(pk);
  217. new client = ReadPackCell(pk);
  218. new sequence = ReadPackCell(pk);
  219. /**
  220. * Make sure it's the same client.
  221. */
  222. if (PlayerSeq[client] != sequence)
  223. {
  224. CloseHandle(pk);
  225. return;
  226. }
  227. new AdminId:adm = AdminId:ReadPackCell(pk);
  228. /**
  229. * Someone could have sneakily changed the admin id while we waited.
  230. */
  231. if (GetUserAdmin(client) != adm)
  232. {
  233. NotifyPostAdminCheck(client);
  234. CloseHandle(pk);
  235. return;
  236. }
  237. /**
  238. * See if we got results.
  239. */
  240. if (hndl == INVALID_HANDLE)
  241. {
  242. decl String:query[255];
  243. ReadPackString(pk, query, sizeof(query));
  244. LogError("SQL error receiving user: %s", error);
  245. LogError("Query dump: %s", query);
  246. NotifyPostAdminCheck(client);
  247. CloseHandle(pk);
  248. return;
  249. }
  250. decl String:name[80];
  251. new GroupId:gid;
  252. while (SQL_FetchRow(hndl))
  253. {
  254. SQL_FetchString(hndl, 0, name, sizeof(name));
  255. if ((gid = FindAdmGroup(name)) == INVALID_GROUP_ID)
  256. {
  257. continue;
  258. }
  259. #if defined _DEBUG
  260. PrintToServer("Binding user group (%d, %d, %d, %s, %d)", client, sequence, adm, name, gid);
  261. #endif
  262. AdminInheritGroup(adm, gid);
  263. }
  264. /**
  265. * We're DONE! Omg.
  266. */
  267. NotifyPostAdminCheck(client);
  268. CloseHandle(pk);
  269. }
  270. public OnReceiveUser(Handle:owner, Handle:hndl, const String:error[], any:data)
  271. {
  272. new Handle:pk = Handle:data;
  273. ResetPack(pk);
  274. new client = ReadPackCell(pk);
  275. /**
  276. * Check if this is the latest result request.
  277. */
  278. new sequence = ReadPackCell(pk);
  279. if (PlayerSeq[client] != sequence)
  280. {
  281. /* Discard everything, since we're out of sequence. */
  282. CloseHandle(pk);
  283. return;
  284. }
  285. /**
  286. * If we need to use the results, make sure they succeeded.
  287. */
  288. if (hndl == INVALID_HANDLE)
  289. {
  290. decl String:query[255];
  291. ReadPackString(pk, query, sizeof(query));
  292. LogError("SQL error receiving user: %s", error);
  293. LogError("Query dump: %s", query);
  294. RunAdminCacheChecks(client);
  295. NotifyPostAdminCheck(client);
  296. CloseHandle(pk);
  297. return;
  298. }
  299. new num_accounts = SQL_GetRowCount(hndl);
  300. if (num_accounts == 0)
  301. {
  302. RunAdminCacheChecks(client);
  303. NotifyPostAdminCheck(client);
  304. CloseHandle(pk);
  305. return;
  306. }
  307. decl String:authtype[16];
  308. decl String:identity[80];
  309. decl String:password[80];
  310. decl String:flags[32];
  311. decl String:name[80];
  312. new AdminId:adm, id;
  313. new immunity;
  314. /**
  315. * Cache user info -- [0] = db id, [1] = cache id, [2] = groups
  316. */
  317. decl user_lookup[num_accounts][3];
  318. new total_users = 0;
  319. while (SQL_FetchRow(hndl))
  320. {
  321. id = SQL_FetchInt(hndl, 0);
  322. SQL_FetchString(hndl, 1, authtype, sizeof(authtype));
  323. SQL_FetchString(hndl, 2, identity, sizeof(identity));
  324. SQL_FetchString(hndl, 3, password, sizeof(password));
  325. SQL_FetchString(hndl, 4, flags, sizeof(flags));
  326. SQL_FetchString(hndl, 5, name, sizeof(name));
  327. immunity = SQL_FetchInt(hndl, 7);
  328. /* For dynamic admins we clear anything already in the cache. */
  329. if ((adm = FindAdminByIdentity(authtype, identity)) != INVALID_ADMIN_ID)
  330. {
  331. RemoveAdmin(adm);
  332. }
  333. adm = CreateAdmin(name);
  334. if (!BindAdminIdentity(adm, authtype, identity))
  335. {
  336. LogError("Could not bind prefetched SQL admin (authtype \"%s\") (identity \"%s\")", authtype, identity);
  337. continue;
  338. }
  339. user_lookup[total_users][0] = id;
  340. user_lookup[total_users][1] = _:adm;
  341. user_lookup[total_users][2] = SQL_FetchInt(hndl, 6);
  342. total_users++;
  343. #if defined _DEBUG
  344. PrintToServer("Found SQL admin (%d,%s,%s,%s,%s,%s,%d):%d:%d", id, authtype, identity, password, flags, name, immunity, adm, user_lookup[total_users-1][2]);
  345. #endif
  346. /* See if this admin wants a password */
  347. if (password[0] != '\0')
  348. {
  349. SetAdminPassword(adm, password);
  350. }
  351. SetAdminImmunityLevel(adm, immunity);
  352. /* Apply each flag */
  353. new len = strlen(flags);
  354. new AdminFlag:flag;
  355. for (new i=0; i<len; i++)
  356. {
  357. if (!FindFlagByChar(flags[i], flag))
  358. {
  359. continue;
  360. }
  361. SetAdminFlag(adm, flag, true);
  362. }
  363. }
  364. /**
  365. * Try binding the user.
  366. */
  367. new group_count = 0;
  368. RunAdminCacheChecks(client);
  369. adm = GetUserAdmin(client);
  370. id = 0;
  371. for (new i=0; i<total_users; i++)
  372. {
  373. if (user_lookup[i][1] == _:adm)
  374. {
  375. id = user_lookup[i][0];
  376. group_count = user_lookup[i][2];
  377. break;
  378. }
  379. }
  380. #if defined _DEBUG
  381. PrintToServer("Binding client (%d, %d) resulted in: (%d, %d, %d)", client, sequence, id, adm, group_count);
  382. #endif
  383. /**
  384. * If we can't verify that we assigned a database admin, or the user has no
  385. * groups, don't bother doing anything.
  386. */
  387. if (!id || !group_count)
  388. {
  389. NotifyPostAdminCheck(client);
  390. CloseHandle(pk);
  391. return;
  392. }
  393. /**
  394. * The user has groups -- we need to fetch them!
  395. */
  396. decl String:query[255];
  397. Format(query, sizeof(query), "SELECT g.name FROM sm_admins_groups ag JOIN sm_groups g ON ag.group_id = g.id WHERE ag.admin_id = %d", id);
  398. ResetPack(pk);
  399. WritePackCell(pk, client);
  400. WritePackCell(pk, sequence);
  401. WritePackCell(pk, _:adm);
  402. WritePackString(pk, query);
  403. SQL_TQuery(owner, OnReceiveUserGroups, query, pk, DBPrio_High);
  404. }
  405. FetchUser(Handle:db, client)
  406. {
  407. decl String:name[65];
  408. decl String:safe_name[140];
  409. decl String:steamid[32];
  410. decl String:steamidalt[32];
  411. decl String:ipaddr[24];
  412. /**
  413. * Get authentication information from the client.
  414. */
  415. GetClientName(client, name, sizeof(name));
  416. GetClientIP(client, ipaddr, sizeof(ipaddr));
  417. steamid[0] = '\0';
  418. if (GetClientAuthString(client, steamid, sizeof(steamid)))
  419. {
  420. if (StrEqual(steamid, "STEAM_ID_LAN"))
  421. {
  422. steamid[0] = '\0';
  423. }
  424. }
  425. SQL_EscapeString(db, name, safe_name, sizeof(safe_name));
  426. /**
  427. * Construct the query using the information the user gave us.
  428. */
  429. decl String:query[512];
  430. new len = 0;
  431. len += Format(query[len], sizeof(query)-len, "SELECT a.id, a.authtype, a.identity, a.password, a.flags, a.name, COUNT(ag.group_id), immunity");
  432. len += Format(query[len], sizeof(query)-len, " FROM sm_admins a LEFT JOIN sm_admins_groups ag ON a.id = ag.admin_id WHERE ");
  433. len += Format(query[len], sizeof(query)-len, " (a.authtype = 'ip' AND a.identity = '%s')", ipaddr);
  434. len += Format(query[len], sizeof(query)-len, " OR (a.authtype = 'name' AND a.identity = '%s')", safe_name);
  435. if (steamid[0] != '\0')
  436. {
  437. strcopy(steamidalt, sizeof(steamidalt), steamid);
  438. steamidalt[6] = (steamid[6] == '0') ? '1' : '0';
  439. len += Format(query[len], sizeof(query)-len, " OR (a.authtype = 'steam' AND (a.identity = '%s' OR a.identity = '%s'))", steamid, steamidalt);
  440. }
  441. len += Format(query[len], sizeof(query)-len, " GROUP BY a.id");
  442. /**
  443. * Send the actual query.
  444. */
  445. PlayerSeq[client] = ++g_sequence;
  446. new Handle:pk;
  447. pk = CreateDataPack();
  448. WritePackCell(pk, client);
  449. WritePackCell(pk, PlayerSeq[client]);
  450. WritePackString(pk, query);
  451. #if defined _DEBUG
  452. PrintToServer("Sending user query: %s", query);
  453. #endif
  454. SQL_TQuery(db, OnReceiveUser, query, pk, DBPrio_High);
  455. }
  456. FetchUsersWeCan(Handle:db)
  457. {
  458. for (new i=1; i<=MaxClients; i++)
  459. {
  460. if (PlayerAuth[i] && GetUserAdmin(i) == INVALID_ADMIN_ID)
  461. {
  462. FetchUser(db, i);
  463. }
  464. }
  465. /**
  466. * This round of updates is done. Go in peace.
  467. */
  468. RebuildCachePart[_:AdminCache_Admins] = 0;
  469. }
  470. public OnReceiveGroupImmunity(Handle:owner, Handle:hndl, const String:error[], any:data)
  471. {
  472. new Handle:pk = Handle:data;
  473. ResetPack(pk);
  474. /**
  475. * Check if this is the latest result request.
  476. */
  477. new sequence = ReadPackCell(pk);
  478. if (RebuildCachePart[_:AdminCache_Groups] != sequence)
  479. {
  480. /* Discard everything, since we're out of sequence. */
  481. CloseHandle(pk);
  482. return;
  483. }
  484. /**
  485. * If we need to use the results, make sure they succeeded.
  486. */
  487. if (hndl == INVALID_HANDLE)
  488. {
  489. decl String:query[255];
  490. ReadPackString(pk, query, sizeof(query));
  491. LogError("SQL error receiving group immunity: %s", error);
  492. LogError("Query dump: %s", query);
  493. CloseHandle(pk);
  494. return;
  495. }
  496. /* We're done with the pack forever. */
  497. CloseHandle(pk);
  498. while (SQL_FetchRow(hndl))
  499. {
  500. decl String:group1[80];
  501. decl String:group2[80];
  502. new GroupId:gid1, GroupId:gid2;
  503. SQL_FetchString(hndl, 0, group1, sizeof(group1));
  504. SQL_FetchString(hndl, 1, group2, sizeof(group2));
  505. if (((gid1 = FindAdmGroup(group1)) == INVALID_GROUP_ID)
  506. || (gid2 = FindAdmGroup(group2)) == INVALID_GROUP_ID)
  507. {
  508. continue;
  509. }
  510. SetAdmGroupImmuneFrom(gid1, gid2);
  511. #if defined _DEBUG
  512. PrintToServer("SetAdmGroupImmuneFrom(%d, %d)", gid1, gid2);
  513. #endif
  514. }
  515. /* Clear the sequence so another connect doesn't refetch */
  516. RebuildCachePart[_:AdminCache_Groups] = 0;
  517. }
  518. public OnReceiveGroupOverrides(Handle:owner, Handle:hndl, const String:error[], any:data)
  519. {
  520. new Handle:pk = Handle:data;
  521. ResetPack(pk);
  522. /**
  523. * Check if this is the latest result request.
  524. */
  525. new sequence = ReadPackCell(pk);
  526. if (RebuildCachePart[_:AdminCache_Groups] != sequence)
  527. {
  528. /* Discard everything, since we're out of sequence. */
  529. CloseHandle(pk);
  530. return;
  531. }
  532. /**
  533. * If we need to use the results, make sure they succeeded.
  534. */
  535. if (hndl == INVALID_HANDLE)
  536. {
  537. decl String:query[255];
  538. ReadPackString(pk, query, sizeof(query));
  539. LogError("SQL error receiving group overrides: %s", error);
  540. LogError("Query dump: %s", query);
  541. CloseHandle(pk);
  542. return;
  543. }
  544. /**
  545. * Fetch the overrides.
  546. */
  547. decl String:name[80];
  548. decl String:type[16];
  549. decl String:command[64];
  550. decl String:access[16];
  551. new GroupId:gid;
  552. while (SQL_FetchRow(hndl))
  553. {
  554. SQL_FetchString(hndl, 0, name, sizeof(name));
  555. SQL_FetchString(hndl, 1, type, sizeof(type));
  556. SQL_FetchString(hndl, 2, command, sizeof(command));
  557. SQL_FetchString(hndl, 3, access, sizeof(access));
  558. /* Find the group. This is actually faster than doing the ID lookup. */
  559. if ((gid = FindAdmGroup(name)) == INVALID_GROUP_ID)
  560. {
  561. /* Oh well, just ignore it. */
  562. continue;
  563. }
  564. new OverrideType:o_type = Override_Command;
  565. if (StrEqual(type, "group"))
  566. {
  567. o_type = Override_CommandGroup;
  568. }
  569. new OverrideRule:o_rule = Command_Deny;
  570. if (StrEqual(access, "allow"))
  571. {
  572. o_rule = Command_Allow;
  573. }
  574. #if defined _DEBUG
  575. PrintToServer("AddAdmGroupCmdOverride(%d, %s, %d, %d)", gid, command, o_type, o_rule);
  576. #endif
  577. AddAdmGroupCmdOverride(gid, command, o_type, o_rule);
  578. }
  579. /**
  580. * It's time to get the group immunity list.
  581. */
  582. new len = 0;
  583. decl String:query[256];
  584. len += Format(query[len], sizeof(query)-len, "SELECT g1.name, g2.name FROM sm_group_immunity gi");
  585. len += Format(query[len], sizeof(query)-len, " LEFT JOIN sm_groups g1 ON g1.id = gi.group_id ");
  586. len += Format(query[len], sizeof(query)-len, " LEFT JOIN sm_groups g2 ON g2.id = gi.other_id");
  587. ResetPack(pk);
  588. WritePackCell(pk, sequence);
  589. WritePackString(pk, query);
  590. SQL_TQuery(owner, OnReceiveGroupImmunity, query, pk, DBPrio_High);
  591. }
  592. public OnReceiveGroups(Handle:owner, Handle:hndl, const String:error[], any:data)
  593. {
  594. new Handle:pk = Handle:data;
  595. ResetPack(pk);
  596. /**
  597. * Check if this is the latest result request.
  598. */
  599. new sequence = ReadPackCell(pk);
  600. if (RebuildCachePart[_:AdminCache_Groups] != sequence)
  601. {
  602. /* Discard everything, since we're out of sequence. */
  603. CloseHandle(pk);
  604. return;
  605. }
  606. /**
  607. * If we need to use the results, make sure they succeeded.
  608. */
  609. if (hndl == INVALID_HANDLE)
  610. {
  611. decl String:query[255];
  612. ReadPackString(pk, query, sizeof(query));
  613. LogError("SQL error receiving groups: %s", error);
  614. LogError("Query dump: %s", query);
  615. CloseHandle(pk);
  616. return;
  617. }
  618. /**
  619. * Now start fetching groups.
  620. */
  621. decl String:flags[32];
  622. decl String:name[128];
  623. new immunity;
  624. while (SQL_FetchRow(hndl))
  625. {
  626. SQL_FetchString(hndl, 0, flags, sizeof(flags));
  627. SQL_FetchString(hndl, 1, name, sizeof(name));
  628. immunity = SQL_FetchInt(hndl, 2);
  629. #if defined _DEBUG
  630. PrintToServer("Adding group (%d, %s, %s)", immunity, flags, name);
  631. #endif
  632. /* Find or create the group */
  633. new GroupId:gid;
  634. if ((gid = FindAdmGroup(name)) == INVALID_GROUP_ID)
  635. {
  636. gid = CreateAdmGroup(name);
  637. }
  638. /* Add flags from the database to the group */
  639. new num_flag_chars = strlen(flags);
  640. for (new i=0; i<num_flag_chars; i++)
  641. {
  642. decl AdminFlag:flag;
  643. if (!FindFlagByChar(flags[i], flag))
  644. {
  645. continue;
  646. }
  647. SetAdmGroupAddFlag(gid, flag, true);
  648. }
  649. SetAdmGroupImmunityLevel(gid, immunity);
  650. }
  651. /**
  652. * It's time to get the group override list.
  653. */
  654. decl String:query[255];
  655. Format(query,
  656. sizeof(query),
  657. "SELECT g.name, og.type, og.name, og.access FROM sm_group_overrides og JOIN sm_groups g ON og.group_id = g.id ORDER BY g.id DESC");
  658. ResetPack(pk);
  659. WritePackCell(pk, sequence);
  660. WritePackString(pk, query);
  661. SQL_TQuery(owner, OnReceiveGroupOverrides, query, pk, DBPrio_High);
  662. }
  663. FetchGroups(Handle:db, sequence)
  664. {
  665. decl String:query[255];
  666. new Handle:pk;
  667. Format(query, sizeof(query), "SELECT flags, name, immunity_level FROM sm_groups");
  668. pk = CreateDataPack();
  669. WritePackCell(pk, sequence);
  670. WritePackString(pk, query);
  671. SQL_TQuery(db, OnReceiveGroups, query, pk, DBPrio_High);
  672. }
  673. public OnReceiveOverrides(Handle:owner, Handle:hndl, const String:error[], any:data)
  674. {
  675. new Handle:pk = Handle:data;
  676. ResetPack(pk);
  677. /**
  678. * Check if this is the latest result request.
  679. */
  680. new sequence = ReadPackCell(pk);
  681. if (RebuildCachePart[_:AdminCache_Overrides] != sequence)
  682. {
  683. /* Discard everything, since we're out of sequence. */
  684. CloseHandle(pk);
  685. return;
  686. }
  687. /**
  688. * If we need to use the results, make sure they succeeded.
  689. */
  690. if (hndl == INVALID_HANDLE)
  691. {
  692. decl String:query[255];
  693. ReadPackString(pk, query, sizeof(query));
  694. LogError("SQL error receiving overrides: %s", error);
  695. LogError("Query dump: %s", query);
  696. CloseHandle(pk);
  697. return;
  698. }
  699. /**
  700. * We're done with you, now.
  701. */
  702. CloseHandle(pk);
  703. decl String:type[64];
  704. decl String:name[64];
  705. decl String:flags[32];
  706. new flag_bits;
  707. while (SQL_FetchRow(hndl))
  708. {
  709. SQL_FetchString(hndl, 0, type, sizeof(type));
  710. SQL_FetchString(hndl, 1, name, sizeof(name));
  711. SQL_FetchString(hndl, 2, flags, sizeof(flags));
  712. #if defined _DEBUG
  713. PrintToServer("Adding override (%s, %s, %s)", type, name, flags);
  714. #endif
  715. flag_bits = ReadFlagString(flags);
  716. if (StrEqual(type, "command"))
  717. {
  718. AddCommandOverride(name, Override_Command, flag_bits);
  719. } else if (StrEqual(type, "group")) {
  720. AddCommandOverride(name, Override_CommandGroup, flag_bits);
  721. }
  722. }
  723. /* Clear the sequence so another connect doesn't refetch */
  724. RebuildCachePart[_:AdminCache_Overrides] = 0;
  725. }
  726. FetchOverrides(Handle:db, sequence)
  727. {
  728. decl String:query[255];
  729. new Handle:pk;
  730. Format(query, sizeof(query), "SELECT type, name, flags FROM sm_overrides");
  731. pk = CreateDataPack();
  732. WritePackCell(pk, sequence);
  733. WritePackString(pk, query);
  734. SQL_TQuery(db, OnReceiveOverrides, query, pk, DBPrio_High);
  735. }