PageRenderTime 62ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 1ms

/mani_admin_plugin/mani_client.cpp

http://maniadminplugin.googlecode.com/
C++ | 8173 lines | 8079 code | 51 blank | 43 comment | 63 complexity | d5fca107aa2295d573d9572edff9b86f MD5 | raw file
Possible License(s): GPL-3.0

Large files files are truncated, but you can click here to view the full file

  1. //
  2. // Mani Admin Plugin
  3. //
  4. // Copyright Š 2009-2014 Giles Millward (Mani). All rights reserved.
  5. //
  6. // This file is part of ManiAdminPlugin.
  7. //
  8. // Mani Admin Plugin is free software: you can redistribute it and/or modify
  9. // it under the terms of the GNU General Public License as published by
  10. // the Free Software Foundation, either version 3 of the License, or
  11. // (at your option) any later version.
  12. //
  13. // Mani Admin Plugin is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. // GNU General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU General Public License
  19. // along with Mani Admin Plugin. If not, see <http://www.gnu.org/licenses/>.
  20. //
  21. //
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #include <math.h>
  26. #include <time.h>
  27. #include "interface.h"
  28. #include "filesystem.h"
  29. #include "engine/iserverplugin.h"
  30. #include "iplayerinfo.h"
  31. #include "eiface.h"
  32. #include "inetchannelinfo.h"
  33. #include "mani_client_flags.h"
  34. #include "mani_memory.h"
  35. #include "mani_convar.h"
  36. #include "mani_player.h"
  37. #include "mani_output.h"
  38. #include "mani_parser.h"
  39. #include "mani_main.h"
  40. #include "mani_mysql.h"
  41. #include "mani_database.h"
  42. #include "mani_language.h"
  43. #include "mani_keyvalues.h"
  44. #include "mani_commands.h"
  45. #include "mani_util.h"
  46. #include "mani_help.h"
  47. #include "mani_client_util.h"
  48. #include "mani_client.h"
  49. #include "mani_client_sql.h"
  50. #include "mani_reservedslot.h"
  51. #include <map>
  52. #ifdef SOURCEMM
  53. #include "mani_client_interface.h"
  54. #endif
  55. extern IFileSystem *filesystem;
  56. extern IVEngineServer *engine; // helper functions (messaging clients, loading content, making entities, running commands, etc)
  57. extern IPlayerInfoManager *playerinfomanager;
  58. extern int con_command_index;
  59. extern bool war_mode;
  60. extern int max_players;
  61. #ifdef SOURCEMM
  62. extern unsigned int g_CallBackCount;
  63. extern SourceHook::CVector<AdminInterfaceListnerStruct *>g_CallBackList;
  64. #endif
  65. ConVar mani_disable_old_clients("mani_disable_old_clients","1",0, "Disallows the engine to read the old client files (adminlist.txt, admingroups.txt, immunitylist.txt)");
  66. static int sort_mask_list ( const void *m1, const void *m2);
  67. static int sort_mask_list ( const void *m1, const void *m2)
  68. {
  69. struct mask_level_t *mi1 = (struct mask_level_t *) m1;
  70. struct mask_level_t *mi2 = (struct mask_level_t *) m2;
  71. int result = strcmp(mi1->class_type, mi2->class_type);
  72. if (result != 0) return result;
  73. return mi1->level_id - mi2->level_id;
  74. }
  75. inline bool FStruEq(const char *sz1, const char *sz2)
  76. {
  77. return(Q_strcmp(sz1, sz2) == 0);
  78. }
  79. inline bool FStrEq(const char *sz1, const char *sz2)
  80. {
  81. return(Q_stricmp(sz1, sz2) == 0);
  82. }
  83. ManiClient::ManiClient()
  84. {
  85. // Init
  86. for (int i = 0; i < MANI_MAX_PLAYERS; i++)
  87. {
  88. active_client_list[i] = NULL;
  89. }
  90. this->InitAdminFlags();
  91. this->InitImmunityFlags();
  92. gpManiClient = this;
  93. }
  94. ManiClient::~ManiClient()
  95. {
  96. // Cleanup
  97. FreeClients();
  98. }
  99. //---------------------------------------------------------------------------------
  100. // Purpose: Player active on server
  101. //---------------------------------------------------------------------------------
  102. void ManiClient::NetworkIDValidated(player_t *player_ptr)
  103. {
  104. int index;
  105. index = this->FindClientIndex(player_ptr);
  106. if (index == -1)
  107. {
  108. active_client_list[player_ptr->index - 1] = NULL;
  109. }
  110. else
  111. {
  112. active_client_list[player_ptr->index - 1] = c_list[index];
  113. // Check levels to see if masking needs to be applied to other players
  114. if (!c_list[index]->level_list.IsEmpty())
  115. {
  116. this->SetupMasked();
  117. }
  118. #ifdef SOURCEMM
  119. for(unsigned int i=0;i<g_CallBackCount;i++)
  120. {
  121. AdminInterfaceListner *ptr = (AdminInterfaceListner *)g_CallBackList[i]->ptr;
  122. if(!ptr)
  123. continue;
  124. ptr->Client_Authorized(player_ptr->index);
  125. }
  126. #endif
  127. }
  128. return;
  129. }
  130. //---------------------------------------------------------------------------------
  131. // Purpose: Check if client is officially labelled as admin
  132. //---------------------------------------------------------------------------------
  133. void ManiClient::ClientDisconnect(player_t *player_ptr)
  134. {
  135. if (active_client_list[player_ptr->index - 1] != NULL)
  136. {
  137. // Check if they had any level ids
  138. if (!active_client_list[player_ptr->index - 1]->level_list.IsEmpty())
  139. {
  140. // Reset pointer then call SetupMasks to reset other
  141. // active player masks
  142. active_client_list[player_ptr->index - 1] = NULL;
  143. this->SetupMasked();
  144. }
  145. else
  146. {
  147. active_client_list[player_ptr->index - 1] = NULL;
  148. }
  149. }
  150. UpdatePlayer( player_ptr );
  151. return;
  152. }
  153. void ManiClient::LevelShutdown() {
  154. WriteIPList();
  155. }
  156. //---------------------------------------------------------------------------------
  157. // Purpose: Parses the Admin config line setting flags
  158. //---------------------------------------------------------------------------------
  159. bool ManiClient::OldAddClient
  160. (
  161. char *file_details,
  162. old_style_client_t *client_ptr,
  163. bool is_admin
  164. )
  165. {
  166. char steam_id[MAX_NETWORKID_LENGTH]="";
  167. char ip_address[128]="";
  168. char name[MAX_PLAYER_NAME_LENGTH]="";
  169. char password[128]="";
  170. int i,j;
  171. Q_memset(client_ptr, 0, sizeof(old_style_client_t));
  172. // Setup flags for individual access
  173. if (is_admin)
  174. {
  175. for (int i = 0; i < MAX_ADMIN_FLAGS; i ++)
  176. {
  177. Q_strcpy(client_ptr->flags[i].flag_name, admin_flag_list[i].flag);
  178. if (mani_reverse_admin_flags.GetInt() == 1)
  179. {
  180. client_ptr->flags[i].enabled = false;
  181. }
  182. else
  183. {
  184. client_ptr->flags[i].enabled = true;
  185. }
  186. }
  187. client_ptr->flags[ALLOW_CLIENT_ADMIN].enabled = true;
  188. }
  189. else
  190. {
  191. for (int i = 0; i < MAX_IMMUNITY_FLAGS; i ++)
  192. {
  193. Q_strcpy(client_ptr->flags[i].flag_name, immunity_flag_list[i].flag);
  194. if (mani_reverse_immunity_flags.GetInt() == 1)
  195. {
  196. client_ptr->flags[i].enabled = true;
  197. }
  198. else
  199. {
  200. client_ptr->flags[i].enabled = false;
  201. }
  202. }
  203. client_ptr->flags[IMMUNITY_ALLOW_BASIC_IMMUNITY].enabled = true;
  204. }
  205. i = 0;
  206. if (file_details[i] != ';')
  207. {
  208. j = 0;
  209. for (;;)
  210. {
  211. if (file_details[i] == '\0')
  212. {
  213. // No more data
  214. steam_id[j] = '\0';
  215. Q_strcpy(client_ptr->steam_id, steam_id);
  216. return true;
  217. }
  218. // If reached space or tab break out of loop
  219. if (file_details[i] == ' ' ||
  220. file_details[i] == '\t' ||
  221. file_details[i] == ';')
  222. {
  223. steam_id[j] = '\0';
  224. break;
  225. }
  226. steam_id[j] = file_details[i];
  227. i++;
  228. j++;
  229. }
  230. Q_strcpy(client_ptr->steam_id, steam_id);
  231. }
  232. MMsg("%s ", steam_id);
  233. if (file_details[i] == ';')
  234. {
  235. i++;
  236. j = 0;
  237. for (;;)
  238. {
  239. if (file_details[i] == '\0')
  240. {
  241. // No more data
  242. ip_address[j] = '\0';
  243. Q_strcpy(client_ptr->ip_address, ip_address);
  244. return true;
  245. }
  246. // If reached space or tab break out of loop
  247. if (file_details[i] == ' ' ||
  248. file_details[i] == ';' ||
  249. file_details[i] == '\t')
  250. {
  251. ip_address[j] = '\0';
  252. break;
  253. }
  254. ip_address[j] = file_details[i];
  255. i++;
  256. j++;
  257. }
  258. Q_strcpy(client_ptr->ip_address, ip_address);
  259. }
  260. MMsg("%s ", ip_address);
  261. if (file_details[i] == ';' && file_details[i + 1] == '\"')
  262. {
  263. i += 2;
  264. j = 0;
  265. for (;;)
  266. {
  267. if (file_details[i] == '\0')
  268. {
  269. // No more data
  270. name[j] = '\0';
  271. Q_strcpy(client_ptr->name, name);
  272. return true;
  273. }
  274. // If reached space or tab break out of loop
  275. if (file_details[i] == '"')
  276. {
  277. i++;
  278. name[j] = '\0';
  279. break;
  280. }
  281. name[j] = file_details[i];
  282. i++;
  283. j++;
  284. }
  285. Q_strcpy(client_ptr->name, name);
  286. }
  287. MMsg("%s ", name);
  288. if (file_details[i] == ';')
  289. {
  290. i++;
  291. j = 0;
  292. for (;;)
  293. {
  294. if (file_details[i] == '\0')
  295. {
  296. // No more data
  297. password[j] = '\0';
  298. if (!client_ptr->steam_id && !client_ptr->ip_address && !client_ptr->name)
  299. {
  300. return false;
  301. }
  302. Q_strcpy(client_ptr->password, password);
  303. return true;
  304. }
  305. // If reached space or tab break out of loop
  306. if (file_details[i] == ' ' ||
  307. file_details[i] == '\t')
  308. {
  309. password[j] = '\0';
  310. break;
  311. }
  312. password[j] = file_details[i];
  313. i++;
  314. j++;
  315. }
  316. if (!client_ptr->steam_id && !client_ptr->ip_address && !client_ptr->name)
  317. {
  318. return false;
  319. }
  320. Q_strcpy(client_ptr->password, password);
  321. }
  322. MMsg("%s ", password);
  323. i++;
  324. while (file_details[i] == ' ' || file_details[i] == '\t')
  325. {
  326. i++;
  327. }
  328. const char *flags_string = &(file_details[i]);
  329. if (is_admin)
  330. {
  331. if (group_list.Find(ADMIN, flags_string))
  332. {
  333. strcpy(client_ptr->group_id, flags_string);
  334. for (int k = 0; k < MAX_ADMIN_FLAGS; k ++)
  335. {
  336. client_ptr->flags[k].enabled = false;
  337. }
  338. return true;
  339. }
  340. }
  341. else
  342. {
  343. if (group_list.Find(IMMUNITY, flags_string))
  344. {
  345. strcpy(client_ptr->group_id, flags_string);
  346. for (int k = 0; k < MAX_IMMUNITY_FLAGS; k ++)
  347. {
  348. client_ptr->flags[k].enabled = false;
  349. }
  350. return true;
  351. }
  352. }
  353. while (file_details[i] != '\0')
  354. {
  355. if (is_admin)
  356. {
  357. for (int k = 0; k < MAX_ADMIN_FLAGS; k ++)
  358. {
  359. if (file_details[i] == admin_flag_list[k].flag[0])
  360. {
  361. if (mani_reverse_admin_flags.GetInt() == 1)
  362. {
  363. client_ptr->flags[k].enabled = true;
  364. }
  365. else
  366. {
  367. client_ptr->flags[k].enabled = false;
  368. }
  369. break;
  370. }
  371. }
  372. }
  373. else
  374. {
  375. for (int k = 0; k < MAX_IMMUNITY_FLAGS; k ++)
  376. {
  377. if (file_details[i] == immunity_flag_list[k].flag[0])
  378. {
  379. if (mani_reverse_immunity_flags.GetInt() == 1)
  380. {
  381. client_ptr->flags[k].enabled = false;
  382. }
  383. else
  384. {
  385. client_ptr->flags[k].enabled = true;
  386. }
  387. break;
  388. }
  389. }
  390. }
  391. i++;
  392. }
  393. MMsg("\n");
  394. return true;
  395. }
  396. //---------------------------------------------------------------------------------
  397. // Purpose: Parses the Admin Group config line setting flags
  398. //---------------------------------------------------------------------------------
  399. void ManiClient::OldAddGroup(char *file_details, char *class_type)
  400. {
  401. char group_id[128]="";
  402. int i,j;
  403. bool reverse_flags = mani_reverse_admin_flags.GetBool();
  404. if (file_details[0] != '\"') return;
  405. i = 1;
  406. j = 0;
  407. for (;;)
  408. {
  409. if (file_details[i] == '\0')
  410. {
  411. // No more data
  412. group_id[j] = '\0';
  413. if (reverse_flags)
  414. {
  415. // No point adding group
  416. return;
  417. }
  418. GlobalGroupFlag *g_flag = group_list.AddGroup(class_type, group_id);
  419. const DualStrKey *key_value = NULL;
  420. for (const char *desc = flag_desc_list.FindFirst(class_type, &key_value); desc != NULL; desc = flag_desc_list.FindNext(class_type, &key_value))
  421. {
  422. g_flag->SetFlag(key_value->key2, true);
  423. }
  424. return;
  425. }
  426. // If reached end quote
  427. if (file_details[i] == '\"')
  428. {
  429. group_id[j] = '\0';
  430. break;
  431. }
  432. group_id[j] = file_details[i];
  433. i++;
  434. j++;
  435. }
  436. // Populate all the flags if needed
  437. if (!reverse_flags)
  438. {
  439. group_list.AddGroup(class_type, group_id);
  440. GlobalGroupFlag *g_flag = group_list.AddGroup(class_type, group_id);
  441. const DualStrKey *key_value = NULL;
  442. for (const char *desc = flag_desc_list.FindFirst(class_type, &key_value); desc != NULL; desc = flag_desc_list.FindNext(class_type, &key_value))
  443. {
  444. g_flag->SetFlag(key_value->key2, true);
  445. }
  446. }
  447. i++;
  448. while (file_details[i] != '\0')
  449. {
  450. char temp_string[8];
  451. snprintf(temp_string, sizeof(temp_string), "%c", file_details[i]);
  452. if (flag_desc_list.IsValidFlag(class_type, temp_string))
  453. {
  454. if (!reverse_flags)
  455. {
  456. GlobalGroupFlag *g_flag = group_list.AddGroup(class_type, group_id);
  457. if (g_flag)
  458. {
  459. g_flag->SetFlag(temp_string, false);
  460. }
  461. }
  462. else
  463. {
  464. GlobalGroupFlag *g_flag = group_list.AddGroup(class_type, group_id);
  465. if (g_flag)
  466. {
  467. g_flag->SetFlag(temp_string, true);
  468. }
  469. }
  470. }
  471. i++;
  472. }
  473. }
  474. //---------------------------------------------------------------------------------
  475. // Purpose: Parses the Admin Group config line setting flags
  476. //---------------------------------------------------------------------------------
  477. bool ManiClient::Init(void)
  478. {
  479. // Setup the flags
  480. flag_desc_list.LoadFlags();
  481. this->AddBuiltInFlags();
  482. FreeClients();
  483. if (!mani_disable_old_clients.GetBool() && LoadOldStyle())
  484. {
  485. // Loaded from old style adminlist.txt etc so write in new file format
  486. WriteClients();
  487. if (gpManiDatabase->GetDBEnabled())
  488. {
  489. if (this->CreateDBTables(NULL))
  490. {
  491. if (this->CreateDBFlags(NULL))
  492. {
  493. this->ExportDataToDB(NULL);
  494. }
  495. }
  496. }
  497. }
  498. FreeClients();
  499. if (gpManiDatabase->GetDBEnabled())
  500. {
  501. if (!this->GetClientsFromDatabase(NULL))
  502. {
  503. FreeClients();
  504. LoadClients();
  505. }
  506. else
  507. {
  508. WriteClients();
  509. this->SetupUnMasked();
  510. this->SetupMasked();
  511. }
  512. }
  513. else
  514. {
  515. // Load up new style clients
  516. LoadClients();
  517. }
  518. flag_desc_list.WriteFlags();
  519. this->SetupPlayersOnServer();
  520. LoadIPList();
  521. return true;
  522. }
  523. //---------------------------------------------------------------------------------
  524. // Purpose: Finds any players on the server and creates a link ptr to the client class
  525. //---------------------------------------------------------------------------------
  526. void ManiClient::SetupPlayersOnServer(void)
  527. {
  528. for (int i = 1; i <= max_players; i++)
  529. {
  530. active_client_list[i - 1] = NULL;
  531. player_t player;
  532. player.index = i;
  533. if (!FindPlayerByIndex(&player)) continue;
  534. if (player.is_bot) continue;
  535. if (strcmp(player.steam_id, "STEAM_ID_PENDING") == 0) continue;
  536. this->NetworkIDValidated(&player);
  537. }
  538. }
  539. //---------------------------------------------------------------------------------
  540. // Purpose: Loads into memory the old style admin flags
  541. //---------------------------------------------------------------------------------
  542. bool ManiClient::LoadOldStyle(void)
  543. {
  544. FileHandle_t file_handle;
  545. char base_filename[512];
  546. char old_base_filename[512];
  547. char data_in[2048];
  548. bool loaded_old_style = false;
  549. //check to see if clients.txt exists first - if so, DO NOT RUN THIS
  550. snprintf(base_filename, sizeof (base_filename), "./cfg/%s/clients.txt", mani_path.GetString());
  551. file_handle = filesystem->Open (base_filename,"rt",NULL);
  552. if ( file_handle != NULL )
  553. {
  554. filesystem->Close(file_handle);
  555. return false;
  556. }
  557. //Get admin groups list
  558. snprintf(base_filename, sizeof (base_filename), "./cfg/%s/admingroups.txt", mani_path.GetString());
  559. file_handle = filesystem->Open (base_filename,"rt",NULL);
  560. if (file_handle == NULL)
  561. {
  562. // MMsg("Old style admingroups.txt file does not exist, using V1.2+ style\n");
  563. }
  564. else
  565. {
  566. MMsg("Admin Group list\n");
  567. while (filesystem->ReadLine (data_in, sizeof(data_in), file_handle) != NULL)
  568. {
  569. if (!ParseLine(data_in, true, false))
  570. {
  571. // String is empty after parsing
  572. continue;
  573. }
  574. OldAddGroup(data_in, ADMIN);
  575. }
  576. filesystem->Close(file_handle);
  577. snprintf(old_base_filename, sizeof(old_base_filename), "%s.old", base_filename);
  578. filesystem->RenameFile(base_filename, old_base_filename);
  579. loaded_old_style = true;
  580. }
  581. //Get immunity groups list
  582. snprintf(base_filename, sizeof (base_filename), "./cfg/%s/immunitygroups.txt", mani_path.GetString());
  583. file_handle = filesystem->Open (base_filename,"rt",NULL);
  584. if (file_handle == NULL)
  585. {
  586. // MMsg("Old style immunitygroups.txt file does not exist, using V1.2+ style\n");
  587. }
  588. else
  589. {
  590. MMsg("Immunity Group list\n");
  591. while (filesystem->ReadLine (data_in, sizeof(data_in), file_handle) != NULL)
  592. {
  593. if (!ParseLine(data_in, true, false))
  594. {
  595. // String is empty after parsing
  596. continue;
  597. }
  598. OldAddGroup(data_in, IMMUNITY);
  599. }
  600. filesystem->Close(file_handle);
  601. snprintf(old_base_filename, sizeof(old_base_filename), "%s.old", base_filename);
  602. filesystem->RenameFile(base_filename, old_base_filename);
  603. loaded_old_style = true;
  604. }
  605. //Get admin list
  606. snprintf(base_filename, sizeof (base_filename), "./cfg/%s/adminlist.txt", mani_path.GetString());
  607. file_handle = filesystem->Open (base_filename,"rt",NULL);
  608. if (file_handle == NULL)
  609. {
  610. // MMsg("Old style adminlist.txt file does not exist, using V1.2+ style\n");
  611. }
  612. else
  613. {
  614. MMsg("Admin steam id list\n");
  615. while (filesystem->ReadLine (data_in, sizeof(data_in), file_handle) != NULL)
  616. {
  617. if (!ParseLine(data_in, true, false))
  618. {
  619. // String is empty after parsing
  620. continue;
  621. }
  622. old_style_client_t temp_client;
  623. if (!OldAddClient(data_in, &temp_client, true)) continue;
  624. // Client data is okay, add it to client list
  625. ConvertOldClientToNewClient(&temp_client, true);
  626. }
  627. filesystem->Close(file_handle);
  628. snprintf(old_base_filename, sizeof(old_base_filename), "%s.old", base_filename);
  629. filesystem->RenameFile(base_filename, old_base_filename);
  630. loaded_old_style = true;
  631. }
  632. //Get immunity list
  633. snprintf(base_filename, sizeof (base_filename), "./cfg/%s/immunitylist.txt", mani_path.GetString());
  634. file_handle = filesystem->Open (base_filename,"rt",NULL);
  635. if (file_handle == NULL)
  636. {
  637. // MMsg("Old style immunitylist.txt file does not exist, using V1.2+ style\n");
  638. }
  639. else
  640. {
  641. MMsg("Immunity list\n");
  642. while (filesystem->ReadLine (data_in, sizeof(data_in), file_handle) != NULL)
  643. {
  644. if (!ParseLine(data_in, true, false))
  645. {
  646. // String is empty after parsing
  647. continue;
  648. }
  649. old_style_client_t temp_client;
  650. if (!OldAddClient(data_in, &temp_client, false)) continue;
  651. // Client data is okay, add it to client list
  652. ConvertOldClientToNewClient(&temp_client, false);
  653. }
  654. filesystem->Close(file_handle);
  655. snprintf(old_base_filename, sizeof(old_base_filename), "%s.old", base_filename);
  656. filesystem->RenameFile(base_filename, old_base_filename);
  657. loaded_old_style = true;
  658. }
  659. // We have to botch in player names here that need to be unqiue
  660. for (int i = 0; i != (int) c_list.size(); i++)
  661. {
  662. // No name set up
  663. if (gpManiDatabase->GetDBEnabled())
  664. {
  665. /* Need unique name !! */
  666. char new_name[128];
  667. snprintf(new_name, sizeof(new_name), "Client_%i_%s", i+1,
  668. gpManiDatabase->GetServerGroupID());
  669. c_list[i]->SetName(new_name);
  670. }
  671. else
  672. {
  673. char new_name[128];
  674. snprintf(new_name, sizeof(new_name), "Client_%i", i+1);
  675. c_list[i]->SetName(new_name);
  676. }
  677. }
  678. this->SetupUnMasked();
  679. this->SetupMasked();
  680. return (loaded_old_style);
  681. }
  682. //---------------------------------------------------------------------------------
  683. // Purpose: Add the client to an already existing entry or create a new one
  684. //---------------------------------------------------------------------------------
  685. void ManiClient::ConvertOldClientToNewClient
  686. (
  687. old_style_client_t *old_client_ptr,
  688. bool is_admin
  689. )
  690. {
  691. ClientPlayer *client_ptr;
  692. int client_index = -1;
  693. bool by_steam = false;
  694. bool by_ip = false;
  695. bool by_name = false;
  696. // Find existing client record
  697. client_index = FindClientIndex(old_client_ptr->steam_id);
  698. if (client_index == -1)
  699. {
  700. client_index = FindClientIndex(old_client_ptr->ip_address);
  701. if (client_index == -1)
  702. {
  703. client_index = FindClientIndex(old_client_ptr->name);
  704. if (client_index != -1)
  705. {
  706. by_name = true;
  707. }
  708. }
  709. else
  710. {
  711. by_ip = true;
  712. }
  713. }
  714. else
  715. {
  716. by_steam = true;
  717. }
  718. if (client_index == -1)
  719. {
  720. // Create new client record as we didn't find one
  721. MMsg("Adding client *********\n");
  722. client_ptr = new ClientPlayer;
  723. c_list.push_back(client_ptr);
  724. }
  725. else
  726. {
  727. // We found a client so point there
  728. client_ptr = c_list[client_index];
  729. MMsg("Found client *********\n");
  730. }
  731. // Copy core information about player
  732. if (old_client_ptr->steam_id && !FStrEq(old_client_ptr->steam_id,""))
  733. {
  734. if (!by_steam)
  735. {
  736. client_ptr->steam_list.Add(old_client_ptr->steam_id);
  737. }
  738. }
  739. if (old_client_ptr->ip_address && !FStrEq(old_client_ptr->ip_address,""))
  740. {
  741. if (!by_ip)
  742. {
  743. client_ptr->ip_address_list.Add(old_client_ptr->ip_address);
  744. }
  745. }
  746. if (old_client_ptr->name && !FStrEq(old_client_ptr->name,""))
  747. {
  748. if (!by_name)
  749. {
  750. client_ptr->nick_list.Add(old_client_ptr->name);
  751. }
  752. }
  753. if (old_client_ptr->password && !FStrEq(old_client_ptr->password,""))
  754. {
  755. client_ptr->SetPassword(old_client_ptr->password);
  756. }
  757. if (old_client_ptr->group_id && !FStrEq(old_client_ptr->group_id, ""))
  758. {
  759. if (is_admin)
  760. {
  761. client_ptr->group_list.Add(ADMIN, old_client_ptr->group_id);
  762. }
  763. else
  764. {
  765. client_ptr->group_list.Add(IMMUNITY, old_client_ptr->group_id);
  766. }
  767. }
  768. // Handle flags for client
  769. if (is_admin)
  770. {
  771. for (int i = 0; i < MAX_ADMIN_FLAGS; i++)
  772. {
  773. if (old_client_ptr->flags[i].enabled)
  774. {
  775. client_ptr->personal_flag_list.SetFlag(ADMIN, old_client_ptr->flags[i].flag_name, true);
  776. }
  777. }
  778. }
  779. else
  780. {
  781. for (int i = 0; i < MAX_IMMUNITY_FLAGS; i++)
  782. {
  783. if (old_client_ptr->flags[i].enabled)
  784. {
  785. client_ptr->personal_flag_list.SetFlag(IMMUNITY, old_client_ptr->flags[i].flag_name, true);
  786. }
  787. }
  788. }
  789. }
  790. //---------------------------------------------------------------------------------
  791. // Purpose: Free's up admin list memory
  792. //---------------------------------------------------------------------------------
  793. void ManiClient::FreeClients(void)
  794. {
  795. // New free clients section
  796. for (int i = 0; i != (int) c_list.size(); i++)
  797. {
  798. delete c_list[i];
  799. }
  800. c_list.clear();
  801. group_list.Kill();
  802. level_list.Kill();
  803. for (int i = 0; i < MANI_MAX_PLAYERS; i++)
  804. {
  805. active_client_list[i] = NULL;
  806. }
  807. }
  808. //---------------------------------------------------------------------------------
  809. // Purpose: Sets up admin flags, if you add a new flag it needs to be initialised
  810. // here !!
  811. //---------------------------------------------------------------------------------
  812. bool IsCommandIssuedByServerAdmin( void )
  813. {
  814. if ( engine->IsDedicatedServer() && con_command_index > -1 )
  815. return false;
  816. if ( !engine->IsDedicatedServer() && con_command_index > 0 )
  817. return false;
  818. return true;
  819. }
  820. //---------------------------------------------------------------------------------
  821. // Purpose: Creates database tables
  822. //---------------------------------------------------------------------------------
  823. bool ManiClient::CreateDBTables(player_t *player_ptr)
  824. {
  825. ManiMySQL *mani_mysql = new ManiMySQL();
  826. OutputHelpText(GREEN_CHAT, player_ptr, "Creating DB tables if not existing....");
  827. if (!mani_mysql->Init(player_ptr))
  828. {
  829. delete mani_mysql;
  830. return false;
  831. }
  832. OutputHelpText(GREEN_CHAT, player_ptr, "Creating %s%s", gpManiDatabase->GetDBTablePrefix(), gpManiDatabase->GetDBTBClient());
  833. if (!mani_mysql->ExecuteQuery(player_ptr,
  834. "CREATE TABLE IF NOT EXISTS %s%s ( "
  835. "user_id mediumint(8) NOT NULL auto_increment, "
  836. "name varchar(32) NOT NULL, "
  837. "password varchar(32) default '', "
  838. "email varchar(255) default '', "
  839. "notes varchar(255) default '', "
  840. "PRIMARY KEY (user_id), "
  841. "UNIQUE KEY (name) "
  842. ") ENGINE=MyISAM AUTO_INCREMENT=1",
  843. gpManiDatabase->GetDBTablePrefix(),
  844. gpManiDatabase->GetDBTBClient()))
  845. {
  846. delete mani_mysql;
  847. return false;
  848. }
  849. OutputHelpText(GREEN_CHAT, player_ptr, "Creating %s%s", gpManiDatabase->GetDBTablePrefix(), gpManiDatabase->GetDBTBSteam());
  850. if (!mani_mysql->ExecuteQuery(player_ptr,
  851. "CREATE TABLE IF NOT EXISTS %s%s( "
  852. "user_id mediumint(8) NOT NULL default '0', "
  853. "steam_id varchar(32) NOT NULL default '', "
  854. "PRIMARY KEY (user_id, steam_id) "
  855. ") ENGINE=MyISAM"
  856. , gpManiDatabase->GetDBTablePrefix(),
  857. gpManiDatabase->GetDBTBSteam()))
  858. {
  859. delete mani_mysql;
  860. return false;
  861. }
  862. OutputHelpText(GREEN_CHAT, player_ptr, "Creating %s%s", gpManiDatabase->GetDBTablePrefix(), gpManiDatabase->GetDBTBNick());
  863. if (!mani_mysql->ExecuteQuery(player_ptr,
  864. "CREATE TABLE IF NOT EXISTS %s%s ( "
  865. "user_id mediumint(8) NOT NULL default '0', "
  866. "nick varchar(32) NOT NULL default '', "
  867. "PRIMARY KEY (user_id, nick) "
  868. ") ENGINE=MyISAM"
  869. , gpManiDatabase->GetDBTablePrefix(),
  870. gpManiDatabase->GetDBTBNick()))
  871. {
  872. delete mani_mysql;
  873. return false;
  874. }
  875. OutputHelpText(GREEN_CHAT, player_ptr, "Creating %s%s", gpManiDatabase->GetDBTablePrefix(), gpManiDatabase->GetDBTBIP());
  876. if (!mani_mysql->ExecuteQuery(player_ptr,
  877. "CREATE TABLE IF NOT EXISTS %s%s ( "
  878. "user_id mediumint(8) NOT NULL default '0', "
  879. "ip_address varchar(32) NOT NULL default '', "
  880. "PRIMARY KEY (user_id, ip_address) "
  881. ") ENGINE=MyISAM"
  882. , gpManiDatabase->GetDBTablePrefix(),
  883. gpManiDatabase->GetDBTBIP()))
  884. {
  885. delete mani_mysql;
  886. return false;
  887. }
  888. OutputHelpText(GREEN_CHAT, player_ptr, "Creating %s%s", gpManiDatabase->GetDBTablePrefix(), gpManiDatabase->GetDBTBFlag());
  889. if (!mani_mysql->ExecuteQuery(player_ptr,
  890. "CREATE TABLE IF NOT EXISTS %s%s ( "
  891. "flag_id varchar(20) BINARY NOT NULL default '', "
  892. "type varchar(32) NOT NULL default '', "
  893. "description varchar(128) NOT NULL default '', "
  894. "PRIMARY KEY (flag_id, type) "
  895. ") ENGINE=MyISAM",
  896. gpManiDatabase->GetDBTablePrefix(),
  897. gpManiDatabase->GetDBTBFlag()))
  898. {
  899. delete mani_mysql;
  900. return false;
  901. }
  902. OutputHelpText(GREEN_CHAT, player_ptr, "Creating %s%s", gpManiDatabase->GetDBTablePrefix(), gpManiDatabase->GetDBTBServer());
  903. if (!mani_mysql->ExecuteQuery(player_ptr,
  904. "CREATE TABLE IF NOT EXISTS %s%s ( "
  905. "server_id mediumint(8) NOT NULL default '0', "
  906. "name varchar(128) NOT NULL default '', "
  907. "ip_address varchar(32) NOT NULL default '', "
  908. "port mediumint(8) NOT NULL default '0', "
  909. "mod_name varchar(64) NOT NULL default '', "
  910. "rcon_password varchar(64) default '', "
  911. "server_group_id varchar(32) NOT NULL default '', "
  912. "PRIMARY KEY (server_id), "
  913. "UNIQUE KEY (name) "
  914. ") ENGINE=MyISAM"
  915. , gpManiDatabase->GetDBTablePrefix(),
  916. gpManiDatabase->GetDBTBServer()))
  917. {
  918. delete mani_mysql;
  919. return false;
  920. }
  921. OutputHelpText(GREEN_CHAT, player_ptr, "Creating %s%s", gpManiDatabase->GetDBTablePrefix(), gpManiDatabase->GetDBTBGroup());
  922. if (!mani_mysql->ExecuteQuery(player_ptr,
  923. "CREATE TABLE IF NOT EXISTS %s%s ( "
  924. "group_id varchar(32) NOT NULL default '', "
  925. "flag_string text, "
  926. "type varchar(32) NOT NULL default '', "
  927. "server_group_id varchar(32) NOT NULL default '', "
  928. "PRIMARY KEY (group_id, type, server_group_id) "
  929. ") ENGINE=MyISAM"
  930. , gpManiDatabase->GetDBTablePrefix(),
  931. gpManiDatabase->GetDBTBGroup()))
  932. {
  933. delete mani_mysql;
  934. return false;
  935. }
  936. OutputHelpText(GREEN_CHAT, player_ptr, "Creating %s%s", gpManiDatabase->GetDBTablePrefix(), gpManiDatabase->GetDBTBClientGroup());
  937. if (!mani_mysql->ExecuteQuery(player_ptr,
  938. "CREATE TABLE IF NOT EXISTS %s%s ( "
  939. "user_id mediumint(8) NOT NULL default '0', "
  940. "group_id varchar(32) NOT NULL default '', "
  941. "type varchar(32) NOT NULL default '', "
  942. "server_group_id varchar(32) NOT NULL default '', "
  943. "PRIMARY KEY (user_id, group_id, type, server_group_id) "
  944. ") ENGINE=MyISAM",
  945. gpManiDatabase->GetDBTablePrefix(),
  946. gpManiDatabase->GetDBTBClientGroup()))
  947. {
  948. delete mani_mysql;
  949. return false;
  950. }
  951. OutputHelpText(GREEN_CHAT, player_ptr, "Creating %s%s", gpManiDatabase->GetDBTablePrefix(), gpManiDatabase->GetDBTBClientFlag());
  952. if (!mani_mysql->ExecuteQuery(player_ptr,
  953. "CREATE TABLE IF NOT EXISTS %s%s ( "
  954. "user_id mediumint(8) NOT NULL default '0', "
  955. "flag_string text, "
  956. "type varchar(32) NOT NULL default '', "
  957. "server_group_id varchar(32) NOT NULL default '', "
  958. "PRIMARY KEY (user_id, type, server_group_id) "
  959. ") ENGINE=MyISAM"
  960. , gpManiDatabase->GetDBTablePrefix(),
  961. gpManiDatabase->GetDBTBClientFlag()))
  962. {
  963. delete mani_mysql;
  964. return false;
  965. }
  966. OutputHelpText(GREEN_CHAT, player_ptr, "Creating %s%s", gpManiDatabase->GetDBTablePrefix(), gpManiDatabase->GetDBTBClientLevel());
  967. if (!mani_mysql->ExecuteQuery(player_ptr,
  968. "CREATE TABLE IF NOT EXISTS %s%s( "
  969. "user_id mediumint(8) NOT NULL default '0', "
  970. "level_id mediumint(8) NOT NULL default '-1', "
  971. "type varchar(32) NOT NULL default '', "
  972. "server_group_id varchar(32) NOT NULL default '', "
  973. "PRIMARY KEY (user_id, level_id, type, server_group_id) "
  974. ") ENGINE=MyISAM"
  975. , gpManiDatabase->GetDBTablePrefix(),
  976. gpManiDatabase->GetDBTBClientLevel()))
  977. {
  978. delete mani_mysql;
  979. return false;
  980. }
  981. OutputHelpText(GREEN_CHAT, player_ptr, "Creating %s%s", gpManiDatabase->GetDBTablePrefix(), gpManiDatabase->GetDBTBLevel());
  982. if (!mani_mysql->ExecuteQuery(player_ptr,
  983. "CREATE TABLE IF NOT EXISTS %s%s ( "
  984. "level_id mediumint(8) NOT NULL default '-1', "
  985. "type varchar(32) NOT NULL default '', "
  986. "flag_string text, "
  987. "server_group_id varchar(32) NOT NULL default '', "
  988. "PRIMARY KEY (level_id, type, server_group_id) "
  989. ") ENGINE=MyISAM"
  990. , gpManiDatabase->GetDBTablePrefix(),
  991. gpManiDatabase->GetDBTBLevel()))
  992. {
  993. delete mani_mysql;
  994. return false;
  995. }
  996. OutputHelpText(GREEN_CHAT, player_ptr, "Creating %s%s", gpManiDatabase->GetDBTablePrefix(), gpManiDatabase->GetDBTBClientServer());
  997. if (!mani_mysql->ExecuteQuery(player_ptr,
  998. "CREATE TABLE IF NOT EXISTS %s%s ( "
  999. "user_id mediumint(8) NOT NULL default '0', "
  1000. "server_group_id varchar(32) NOT NULL default '0', "
  1001. "PRIMARY KEY (user_id, server_group_id) "
  1002. ") ENGINE=MyISAM"
  1003. , gpManiDatabase->GetDBTablePrefix(),
  1004. gpManiDatabase->GetDBTBClientServer()))
  1005. {
  1006. delete mani_mysql;
  1007. return false;
  1008. }
  1009. OutputHelpText(GREEN_CHAT, player_ptr, "Creating %s%s", gpManiDatabase->GetDBTablePrefix(), gpManiDatabase->GetDBTBVersion());
  1010. if (!mani_mysql->ExecuteQuery(player_ptr,
  1011. "CREATE TABLE IF NOT EXISTS %s%s ( "
  1012. "version_id varchar(20) NOT NULL)",
  1013. gpManiDatabase->GetDBTablePrefix(),
  1014. gpManiDatabase->GetDBTBVersion()))
  1015. {
  1016. delete mani_mysql;
  1017. return false;
  1018. }
  1019. OutputHelpText(GREEN_CHAT, player_ptr, "Checking %s%s", gpManiDatabase->GetDBTablePrefix(), gpManiDatabase->GetDBTBVersion());
  1020. int row_count;
  1021. if (mani_mysql->ExecuteQuery(player_ptr, &row_count, "SELECT 1 FROM %s%s", gpManiDatabase->GetDBTablePrefix(), gpManiDatabase->GetDBTBVersion()))
  1022. {
  1023. if (row_count == 0)
  1024. {
  1025. OutputHelpText(GREEN_CHAT, player_ptr, "No rows found, inserting into %s%s", gpManiDatabase->GetDBTablePrefix(), gpManiDatabase->GetDBTBVersion());
  1026. // No rows so insert one
  1027. if (!mani_mysql->ExecuteQuery(player_ptr,
  1028. "INSERT INTO %s%s VALUES ('%s')",
  1029. gpManiDatabase->GetDBTablePrefix(),
  1030. gpManiDatabase->GetDBTBVersion(),
  1031. PLUGIN_VERSION_ID2))
  1032. {
  1033. delete mani_mysql;
  1034. return false;
  1035. }
  1036. }
  1037. else
  1038. {
  1039. OutputHelpText(GREEN_CHAT, player_ptr, "Row found, updating %s%s", gpManiDatabase->GetDBTablePrefix(), gpManiDatabase->GetDBTBVersion());
  1040. if (!mani_mysql->ExecuteQuery(player_ptr,
  1041. "UPDATE %s%s SET version_id = '%s'",
  1042. gpManiDatabase->GetDBTablePrefix(),
  1043. gpManiDatabase->GetDBTBVersion(),
  1044. PLUGIN_VERSION_ID2))
  1045. {
  1046. delete mani_mysql;
  1047. return false;
  1048. }
  1049. }
  1050. }
  1051. else
  1052. {
  1053. delete mani_mysql;
  1054. return false;
  1055. }
  1056. delete mani_mysql;
  1057. return true;
  1058. }
  1059. //---------------------------------------------------------------------------------
  1060. // Purpose: Creates flag information in tables
  1061. //---------------------------------------------------------------------------------
  1062. bool ManiClient::CreateDBFlags(player_t *player_ptr)
  1063. {
  1064. ManiMySQL *mani_mysql = new ManiMySQL();
  1065. if (!mani_mysql->Init(player_ptr))
  1066. {
  1067. delete mani_mysql;
  1068. return false;
  1069. }
  1070. OutputHelpText(GREEN_CHAT, player_ptr, "Generating DB access flags if not existing....");
  1071. for (const char *c_type = class_type_list.FindFirst(); c_type != NULL; c_type = class_type_list.FindNext())
  1072. {
  1073. const DualStrKey *key_value = NULL;
  1074. for (const char *desc = flag_desc_list.FindFirst(c_type, &key_value); desc != NULL; desc = flag_desc_list.FindNext(c_type, &key_value))
  1075. {
  1076. OutputHelpText(ORANGE_CHAT, player_ptr, "Checking class [%s] flag_id [%s]", c_type, key_value->key2);
  1077. int row_count = 0;
  1078. if (!mani_mysql->ExecuteQuery(player_ptr, &row_count, "SELECT f.description "
  1079. "FROM %s%s f "
  1080. "where f.flag_id = '%s' "
  1081. "and f.type = '%s'",
  1082. gpManiDatabase->GetDBTablePrefix(), gpManiDatabase->GetDBTBFlag(),
  1083. key_value->key2, c_type))
  1084. {
  1085. delete mani_mysql;
  1086. return false;
  1087. }
  1088. if (row_count == 0)
  1089. {
  1090. OutputHelpText(ORANGE_CHAT, player_ptr, "Inserting class [%s] flag_id [%s]", c_type, key_value->key2);
  1091. // Setup flag record
  1092. if (!mani_mysql->ExecuteQuery(player_ptr, "INSERT INTO %s%s (flag_id, type, description) VALUES ('%s', '%s', '%s')",
  1093. gpManiDatabase->GetDBTablePrefix(),
  1094. gpManiDatabase->GetDBTBFlag(),
  1095. key_value->key2,
  1096. c_type,
  1097. desc))
  1098. {
  1099. delete mani_mysql;
  1100. return false;
  1101. }
  1102. }
  1103. else
  1104. {
  1105. mani_mysql->FetchRow();
  1106. if (strcmp(mani_mysql->GetString(0), desc) != 0)
  1107. {
  1108. OutputHelpText(ORANGE_CHAT, player_ptr, "Updating class [%s] flag_id [%s] with new description [%s]", c_type, key_value->key2, desc);
  1109. // Update to the new description
  1110. if (!mani_mysql->ExecuteQuery(player_ptr, "UPDATE %s%s SET description = '%s' WHERE flag_id = '%s' AND type = '%s'",
  1111. gpManiDatabase->GetDBTablePrefix(),
  1112. gpManiDatabase->GetDBTBFlag(),
  1113. desc,
  1114. key_value->key2,
  1115. c_type))
  1116. {
  1117. delete mani_mysql;
  1118. return false;
  1119. }
  1120. }
  1121. }
  1122. }
  1123. }
  1124. OutputHelpText(GREEN_CHAT, player_ptr, "Updating version id..");
  1125. mani_mysql->ExecuteQuery(player_ptr,
  1126. "UPDATE %s%s "
  1127. "SET version_id = '%s'",
  1128. gpManiDatabase->GetDBTablePrefix(),
  1129. gpManiDatabase->GetDBTBVersion(),
  1130. PLUGIN_CORE_VERSION);
  1131. delete mani_mysql;
  1132. return true;
  1133. }
  1134. //---------------------------------------------------------------------------------
  1135. // Purpose: Export data in memory to the database
  1136. //---------------------------------------------------------------------------------
  1137. bool ManiClient::ExportDataToDB(player_t *player_ptr)
  1138. {
  1139. char flag_string[2048];
  1140. OutputHelpText(GREEN_CHAT, player_ptr, "Exporting data from clients.txt to DB....");
  1141. ManiMySQL *mani_mysql = new ManiMySQL();
  1142. if (!mani_mysql->Init(player_ptr))
  1143. {
  1144. delete mani_mysql;
  1145. return false;
  1146. }
  1147. // Clean out tables for this server id
  1148. if (!mani_mysql->ExecuteQuery(player_ptr, "DELETE FROM %s%s WHERE server_group_id = '%s'", gpManiDatabase->GetDBTablePrefix(), gpManiDatabase->GetDBTBGroup(), gpManiDatabase->GetServerGroupID()))
  1149. {
  1150. delete mani_mysql;
  1151. return false;
  1152. }
  1153. if (!mani_mysql->ExecuteQuery(player_ptr, "DELETE FROM %s%s WHERE server_group_id = '%s'", gpManiDatabase->GetDBTablePrefix(), gpManiDatabase->GetDBTBClientGroup(), gpManiDatabase->GetServerGroupID()))
  1154. {
  1155. delete mani_mysql;
  1156. return false;
  1157. }
  1158. if (!mani_mysql->ExecuteQuery(player_ptr, "DELETE FROM %s%s WHERE server_group_id = '%s'", gpManiDatabase->GetDBTablePrefix(), gpManiDatabase->GetDBTBLevel(), gpManiDatabase->GetServerGroupID()))
  1159. {
  1160. delete mani_mysql;
  1161. return false;
  1162. }
  1163. if (!mani_mysql->ExecuteQuery(player_ptr, "DELETE FROM %s%s WHERE server_group_id = '%s'", gpManiDatabase->GetDBTablePrefix(), gpManiDatabase->GetDBTBClientLevel(), gpManiDatabase->GetServerGroupID()))
  1164. {
  1165. delete mani_mysql;
  1166. return false;
  1167. }
  1168. if (!mani_mysql->ExecuteQuery(player_ptr, "DELETE FROM %s%s WHERE server_group_id = '%s'", gpManiDatabase->GetDBTablePrefix(), gpManiDatabase->GetDBTBClientFlag(), gpManiDatabase->GetServerGroupID()))
  1169. {
  1170. delete mani_mysql;
  1171. return false;
  1172. }
  1173. if (!mani_mysql->ExecuteQuery(player_ptr, "DELETE FROM %s%s WHERE server_group_id = '%s'", gpManiDatabase->GetDBTablePrefix(), gpManiDatabase->GetDBTBClientServer(), gpManiDatabase->GetServerGroupID()))
  1174. {
  1175. delete mani_mysql;
  1176. return false;
  1177. }
  1178. if (!mani_mysql->ExecuteQuery(player_ptr, "DELETE FROM %s%s WHERE server_id = %i", gpManiDatabase->GetDBTablePrefix(), gpManiDatabase->GetDBTBServer(), gpManiDatabase->GetServerID()))
  1179. {
  1180. delete mani_mysql;
  1181. return false;
  1182. }
  1183. OutputHelpText(GREEN_CHAT, player_ptr, "Deleted existing DB data for this server....");
  1184. // Do server details
  1185. if (!mani_mysql->ExecuteQuery(player_ptr,
  1186. "INSERT INTO %s%s VALUES (%i, '%s', '%s', %i, '%s', '%s', '%s')",
  1187. gpManiDatabase->GetDBTablePrefix(),
  1188. gpManiDatabase->GetDBTBServer(),
  1189. gpManiDatabase->GetServerID(),
  1190. gpManiDatabase->GetServerName(),
  1191. gpManiDatabase->GetServerIPAddress(),
  1192. gpManiDatabase->GetServerPort(),
  1193. gpManiDatabase->GetModName(),
  1194. gpManiDatabase->GetRCONPassword(),
  1195. gpManiDatabase->GetServerGroupID()
  1196. ))
  1197. {
  1198. delete mani_mysql;
  1199. return false;
  1200. }
  1201. OutputHelpText(GREEN_CHAT, player_ptr, "Generated server details....");
  1202. for (const char *c_type = class_type_list.FindFirst(); c_type != NULL; c_type = class_type_list.FindNext())
  1203. {
  1204. const DualStrIntKey *key_value = NULL;
  1205. for (GlobalGroupFlag *g_flag = level_list.FindFirst(c_type, &key_value); g_flag != NULL; g_flag = level_list.FindNext(c_type, &key_value))
  1206. {
  1207. Q_strcpy(flag_string, "");
  1208. if (g_flag->CatFlags(flag_string))
  1209. {
  1210. if (!mani_mysql->ExecuteQuery(player_ptr,
  1211. "INSERT IGNORE INTO %s%s (level_id, type, flag_string, server_group_id) VALUES (%i, '%s', '%s', '%s')",
  1212. gpManiDatabase->GetDBTablePrefix(),
  1213. gpManiDatabase->GetDBTBLevel(),
  1214. key_value->key2,
  1215. key_value->key1,
  1216. flag_string,
  1217. gpManiDatabase->GetServerGroupID()))
  1218. {
  1219. delete mani_mysql;
  1220. return false;
  1221. }
  1222. }
  1223. }
  1224. }
  1225. OutputHelpText(GREEN_CHAT, player_ptr, "Generated level groups....");
  1226. // Do the flag groups next
  1227. for (const char *c_type = class_type_list.FindFirst(); c_type != NULL; c_type = class_type_list.FindNext())
  1228. {
  1229. const DualStriKey *key_value = NULL;
  1230. for (GlobalGroupFlag *g_flag = group_list.FindFirst(c_type, &key_value); g_flag != NULL; g_flag = group_list.FindNext(c_type, &key_value))
  1231. {
  1232. Q_strcpy(flag_string, "");
  1233. if (g_flag->CatFlags(flag_string))
  1234. {
  1235. if (!mani_mysql->ExecuteQuery(player_ptr,
  1236. "INSERT IGNORE INTO %s%s (group_id, flag_string, type, server_group_id) VALUES ('%s', '%s', '%s', '%s')",
  1237. gpManiDatabase->GetDBTablePrefix(),
  1238. gpManiDatabase->GetDBTBGroup(),
  1239. key_value->key2,
  1240. flag_string,
  1241. key_value->key1,
  1242. gpManiDatabase->GetServerGroupID()))
  1243. {
  1244. delete mani_mysql;
  1245. return false;
  1246. }
  1247. }
  1248. }
  1249. }
  1250. OutputHelpText(GREEN_CHAT, player_ptr, "Generated DB global groups....");
  1251. OutputHelpText(GREEN_CHAT, player_ptr, "Building DB client data for %i clients", (int) c_list.size());
  1252. // Populate client list for players that already exist on the server
  1253. // and generate new clients if necessary with user ids
  1254. for (int i = 0; i != (int) c_list.size(); i ++)
  1255. {
  1256. int row_count;
  1257. OutputHelpText(GREEN_CHAT, player_ptr, "%i", (int) c_list.size() - i);
  1258. c_list[i]->SetUserID(-1);
  1259. if (!mani_mysql->ExecuteQuery(player_ptr,
  1260. &row_count,
  1261. "SELECT user_id FROM %s%s WHERE name = '%s'", gpManiDatabase->GetDBTablePrefix(), gpManiDatabase->GetDBTBClient(), c_list[i]->GetName()))
  1262. {
  1263. delete mani_mysql;
  1264. return false;
  1265. }
  1266. if (row_count != 0)
  1267. {
  1268. if (!mani_mysql->FetchRow())
  1269. {
  1270. // Should get at least 1 row
  1271. delete mani_mysql;
  1272. return false;
  1273. }
  1274. c_list[i]->SetUserID(mani_mysql->GetInt(0));
  1275. }
  1276. else
  1277. {
  1278. if (!mani_mysql->ExecuteQuery(player_ptr,
  1279. "INSERT IGNORE INTO %s%s "
  1280. "(name, password, email, notes) "
  1281. "VALUES "
  1282. "('%s', '%s', '%s', '%s')",
  1283. gpManiDatabase->GetDBTablePrefix(),
  1284. gpManiDatabase->GetDBTBClient(),
  1285. c_list[i]->GetName(),
  1286. c_list[i]->GetPassword(),
  1287. c_list[i]->GetEmailAddress(),
  1288. c_list[i]->GetNotes()))
  1289. {
  1290. delete mani_mysql;
  1291. return false;
  1292. }
  1293. c_list[i]->SetUserID(mani_mysql->GetRowID());
  1294. }
  1295. // Setup steam ids
  1296. if (!mani_mysql->ExecuteQuery(player_ptr,
  1297. "DELETE FROM %s%s WHERE user_id = %i",
  1298. gpManiDatabase->GetDBTablePrefix(),
  1299. gpManiDatabase->GetDBTBSteam(),
  1300. c_list[i]->GetUserID()))
  1301. {
  1302. delete mani_mysql;
  1303. return false;
  1304. }
  1305. for (const char *steam_id = c_list[i]->steam_list.FindFirst(); steam_id != NULL; steam_id = c_list[i]->steam_list.FindNext())
  1306. {
  1307. if (!mani_mysql->ExecuteQuery(player_ptr,
  1308. "INSERT IGNORE INTO %s%s (user_id, steam_id) VALUES (%i, '%s')",
  1309. gpManiDatabase->GetDBTablePrefix(),
  1310. gpManiDatabase->GetDBTBSteam(),
  1311. c_list[i]->GetUserID(),
  1312. steam_id))
  1313. {
  1314. delete mani_mysql;
  1315. return false;
  1316. }
  1317. }
  1318. // Setup ip addresses
  1319. if (!mani_mysql->ExecuteQuery(player_ptr,
  1320. "DELETE FROM %s%s WHERE user_id = %i",
  1321. gpManiDatabase->GetDBTablePrefix(),
  1322. gpManiDatabase->GetDBTBIP(),
  1323. c_list[i]->GetUserID()))
  1324. {
  1325. delete mani_mysql;
  1326. return false;
  1327. }
  1328. for (const char *ip_address = c_list[i]->ip_address_list.FindFirst(); ip_address != NULL; ip_address = c_list[i]->ip_address_list.FindNext())
  1329. {
  1330. if (!mani_mysql->ExecuteQuery(player_ptr,
  1331. "INSERT IGNORE INTO %s%s (user_id, ip_address) VALUES (%i, '%s')",
  1332. gpManiDatabase->GetDBTablePrefix(),
  1333. gpManiDatabase->GetDBTBIP(),
  1334. c_list[i]->GetUserID(),
  1335. ip_address))
  1336. {
  1337. delete mani_mysql;
  1338. return false;
  1339. }
  1340. }
  1341. // Setup nickname ids
  1342. if (!mani_mysql->ExecuteQuery(player_ptr,
  1343. "DELETE FROM %s%s WHERE user_id = %i",
  1344. gpManiDatabase->GetDBTablePrefix(),
  1345. gpManiDatabase->GetDBTBNick(),
  1346. c_list[i]->GetUserID()))
  1347. {
  1348. delete mani_mysql;
  1349. return false;
  1350. }
  1351. for (const char *nick = c_list[i]->nick_list.FindFirst(); nick != NULL; nick = c_list[i]->nick_list.FindNext())
  1352. {
  1353. if (!mani_mysql->ExecuteQuery(player_ptr,
  1354. "INSERT IGNORE INTO %s%s (user_id, nick) VALUES (%i, '%s')",
  1355. gpManiDatabase->GetDBTablePrefix(),
  1356. gpManiDatabase->GetDBTBNick(),
  1357. c_list[i]->GetUserID(),
  1358. nick))
  1359. {
  1360. delete mani_mysql;
  1361. return false;
  1362. }
  1363. }
  1364. // Setup client_server record
  1365. if (!mani_mysql->ExecuteQuery(player_ptr,
  1366. "INSERT INTO %s%s (user_id, server_group_id) VALUES (%i, '%s')",
  1367. gpManiDatabase->GetDBTablePrefix(),
  1368. gpManiDatabase->GetDBTBClientServer(),
  1369. c_list[i]->GetUserID(),
  1370. gpManiDatabase->GetServerGroupID()))
  1371. {
  1372. delete mani_mysql;
  1373. return false;
  1374. }
  1375. Q_strcpy(flag_string, "");
  1376. // Client personal flags
  1377. // Search through known about flags
  1378. for (const char *c_type = class_type_list.FindFirst(); c_type != NULL; c_type = class_type_list.FindNext())
  1379. {
  1380. if (c_list[i]->personal_flag_list.CatFlags(flag_string, c_type))
  1381. {
  1382. if (!mani_mysql->ExecuteQuery(player_ptr,
  1383. "INSERT IGNORE INTO %s%s (user_id, flag_string, type, server_group_id) VALUES (%i,'%s','%s','%s')",
  1384. gpManiDatabase->GetDBTablePrefix(),
  1385. gpManiDatabase->GetDBTBClientFlag(),
  1386. c_list[i]->GetUserID(),
  1387. flag_string,
  1388. c_type,
  1389. gpManiDatabase->GetServerGroupID()))
  1390. {
  1391. delete mani_mysql;
  1392. return false;
  1393. }
  1394. }
  1395. }
  1396. // Do the client_group flags next
  1397. const char *group_id = NULL;
  1398. for (const char *c_type = c_list[i]->group_list.FindFirst(&group_id); c_type != NULL; c_type = c_list[i]->group_list.FindNext(&group_id))
  1399. {
  1400. if (!mani_mysql->ExecuteQuery(player_ptr,
  1401. "INSERT IGNORE INTO %s%s (user_id, group_id, type, server_group_id) VALUES (%i,'%s','%s','%s')",
  1402. gpManiDatabase->GetDBTablePrefix(),
  1403. gpManiDatabase->GetDBTBClientGroup(),
  1404. c_list[i]->GetUserID(),
  1405. group_id,
  1406. c_type,
  1407. gpManiDatabase->GetServerGroupID()))
  1408. {
  1409. delete mani_mysql;
  1410. return false;
  1411. }
  1412. }
  1413. // Do the client_level flags next
  1414. const char *cl_type = NULL;
  1415. for (int level_id = c_list[i]->level_list.FindFirst(&cl_type); level_id != -99999; level_id = c_list[i]->level_list.FindNext(&cl_type))
  1416. {
  1417. if (!mani_mysql->ExecuteQuery(player_ptr,
  1418. "INSERT IGNORE INTO %s%s (user_id, level_id, type, server_group_id) VALUES (%i,%i,'%s','%s')",
  1419. gpManiDatabase->GetDBTablePrefix(),
  1420. gpManiDatabase->GetDBTBClientLevel(),
  1421. c_list[i]->GetUserID(),
  1422. level_id,
  1423. cl_type,
  1424. gpManiDatabase->GetServerGroupID()))
  1425. {
  1426. delete mani_mysql;
  1427. return false;
  1428. }
  1429. }
  1430. }
  1431. OutputHelpText(GREEN_CHAT, player_ptr, "Clients built on DB");
  1432. delete mani_mysql;
  1433. return true;
  1434. }
  1435. //---------------------------------------------------------------------------------
  1436. // Purpose: Export server id information
  1437. //---------------------------------------------------------------------------------
  1438. bool ManiClient::UploadServerID(player_t *player_ptr)
  1439. {
  1440. OutputHelpText(GREEN_CHAT, player_ptr, "Exporting data from database.txt to DB....");
  1441. ManiMySQL *mani_mysql = new ManiMySQL();
  1442. if (!mani_mysql->Init(player_ptr))
  1443. {
  1444. delete mani_mysql;
  1445. return false;
  1446. }
  1447. if (!mani_mysql->ExecuteQuery(player_ptr, "DELETE FROM %s%s WHERE server_id = %i",
  1448. gpManiDatabase->GetDBTablePrefix(), gpManiDatabase->GetDBTBServer(), gpManiDatabase->GetServerID()))
  1449. {
  1450. delete mani_mysql;
  1451. return false;
  1452. }
  1453. OutputHelpText(GREEN_CHAT, player_ptr, "Deleted existing server information for this server....");
  1454. // Do server details
  1455. if (!mani_mysql->ExecuteQuery(player_ptr,
  1456. "INSERT INTO %s%s VALUES (%i, '%s', '%s', %i, '%s', '%s', '%s')",
  1457. gpManiDatabase->GetDBTablePrefix(),
  1458. gpManiDatabase->GetDBTBServer(),
  1459. gpManiDatabase->GetServerID(),
  1460. gpManiDatabase->GetServerName(),
  1461. gpManiDatabase->GetServerIPAddress(),
  1462. gpManiDatabase->GetServerPort(),
  1463. gpManiDatabase->GetModName(),
  1464. gpManiDatabase->GetRCONPassword(),
  1465. gpManiDatabase->GetServerGroupID()
  1466. ))
  1467. {
  1468. delete mani_mysql;
  1469. return false;
  1470. }
  1471. OutputHelpText(GREEN_CHAT, player_ptr, "Generated server details....");
  1472. return true;
  1473. }
  1474. //---------------------------------------------------------------------------------
  1475. // Purpose: Get Client data from database
  1476. //---------------------------------------------------------------------------------
  1477. bool ManiClient::GetClientsFromDatabase(player_t *player_ptr)
  1478. {
  1479. bool found_flag;
  1480. int row_count;
  1481. char flags_string[2048];
  1482. // Upgrade the database to V1.2 Beta M functionality
  1483. UpgradeDB1();
  1484. OutputHelpText(GREEN_CHAT, player_ptr, "Getting client info from the database....");
  1485. ManiMySQL *mani_mysql = new ManiMySQL();
  1486. if (!mani_mysql->Init(player_ptr))
  1487. {
  1488. delete mani_mysql;
  1489. return false;
  1490. }
  1491. if (!mani_mysql->ExecuteQuery(player_ptr,
  1492. "UPDATE %s%s SET server_group_id = '%s' WHERE server_id = %i",
  1493. gpManiDatabase->GetDBTablePrefix(),
  1494. gpManiDatabase->GetDBTBServer(),
  1495. gpManiDatabase->GetServerGroupID(),
  1496. gpManiDatabase->GetServerID()))
  1497. {
  1498. delete mani_mysql;
  1499. mani_mysql = new ManiMySQL();
  1500. if (!mani_mysql->Init(player_ptr))
  1501. {
  1502. delete mani_mysql;
  1503. return false;
  1504. }
  1505. }
  1506. // Get admin groups
  1507. if (!mani_mysql->ExecuteQuery(player_ptr,
  1508. &row_count,
  1509. "SELECT g.group_id, g.flag_string, g.type "
  1510. "FROM %s%s g "
  1511. "WHERE g.server_group_id = '%s' ",
  1512. gpManiDatabase->GetDBTablePrefix(),
  1513. gpManiDatabase->GetDBTBGroup(),
  1514. gpManiDatabase->GetServerGroupID()))
  1515. {
  1516. delete mani_mysql;
  1517. return false;
  1518. }
  1519. if (row_count != 0)
  1520. {
  1521. // Found rows
  1522. while (mani_mysql->FetchRow())
  1523. {
  1524. char *group_id = mani_mysql->GetString(0);
  1525. Q_strcpy(flags_string, mani_mysql->GetString(1));
  1526. char *class_type = mani_mysql->GetString(2);
  1527. int flag_index = 0;
  1528. for (;;)
  1529. {
  1530. char *flag_id = SplitFlagString(flags_string, &flag_index);
  1531. if (flag_id == NULL)
  1532. {
  1533. break;
  1534. }
  1535. if (flag_desc_list.IsValidFlag(class_type, flag_id))
  1536. {
  1537. // Create/Update group/level
  1538. GlobalGroupFlag *g_flag = group_list.AddGroup(class_type, group_id);
  1539. if (g_flag)
  1540. {
  1541. g_flag->SetFlag(flag_id, true);
  1542. }
  1543. }
  1544. }
  1545. }
  1546. }
  1547. // Get admin levels
  1548. if (!mani_mysql->ExecuteQuery(player_ptr,
  1549. &row_count,
  1550. "SELECT l.level_id, l.flag_string, l.type "
  1551. "FROM %s%s l "
  1552. "WHERE l.server_group_id = '%s' "
  1553. "ORDER BY l.level_id",
  1554. gpManiDatabase->GetDBTablePrefix(),
  1555. gpManiDatabase->GetDBTBLevel(),
  1556. gpManiDatabase->GetServerGroupID()))
  1557. {
  1558. delete mani_mysql;
  1559. return false;
  1560. }
  1561. if (row_count != 0)
  1562. {
  1563. // Found rows
  1564. while (mani_mysql->FetchRow())
  1565. {
  1566. char *level_id = mani_mysql->GetString(0);
  1567. Q_strcpy(flags_string, mani_mysql->GetString(1));
  1568. char *class_type = mani_mysql->GetString(2);
  1569. int flag_index = 0;
  1570. for (;;)
  1571. {
  1572. char *flag_id = SplitFlagString(flags_string, &flag_index);
  1573. if (flag_id == NULL)
  1574. {
  1575. break;
  1576. }
  1577. if (flag_desc_list.IsValidFlag(class_type, flag_id))
  1578. {
  1579. // Create/Update group/level
  1580. GlobalGroupFlag *g_flag = level_list.AddGroup(class_type, atoi(level_id));
  1581. if (g_flag)
  1582. {
  1583. g_flag->SetFlag(flag_id, true);
  1584. }
  1585. }
  1586. }
  1587. }
  1588. }
  1589. // Get clients for this server
  1590. // Ridiculous search that brings back too many rows but ultimately
  1591. // is faster than doing all the seperate selects per client
  1592. // to do the same thing :(
  1593. OutputHelpText(GREEN_CHAT, player_ptr, "SQL server version [%s]", mani_mysql->GetServerVersion());
  1594. OutputHelpText(GREEN_CHAT, player_ptr, "Major [%i] Minor [%i] Issue [%i]", mani_mysql->GetMajor(), mani_mysql->GetMinor(), mani_mysql->Ge

Large files files are truncated, but you can click here to view the full file