Guest User

calladmin_steam_jb.spNEW

a guest
Oct 24th, 2018
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.12 KB | None | 0 0
  1. #include <sourcemod>
  2. #include "include/autoexecconfig"
  3. #include "include/messagebot"
  4. #include "include/calladmin"
  5. #include "include/system2"
  6. #include <regex>
  7.  
  8. #undef REQUIRE_PLUGIN
  9. #include "include/updater"
  10. #pragma semicolon 1
  11. #pragma newdecls required
  12.  
  13. #pragma dynamic 32768
  14.  
  15. #define MAX_ITEMS 150
  16.  
  17. ConVar g_hVersion;
  18.  
  19. ConVar g_hSteamUsername;
  20. char g_sSteamUsername[128];
  21.  
  22. ConVar g_hSteamPassword;
  23. char g_sSteamPassword[128];
  24.  
  25. Regex g_hSteamID2Regex;
  26. Regex g_hSteamID3Regex;
  27. Regex g_hCommunityIDRegex;
  28.  
  29. char g_sSteamIDConfigFile[PLATFORM_MAX_PATH];
  30. char g_sGroupIDConfigFile[PLATFORM_MAX_PATH];
  31.  
  32. int g_iLastReportID;
  33.  
  34. enum AuthStringType
  35. {
  36. AuthString_SteamID2,
  37. AuthString_SteamID3,
  38. AuthString_CommunityID,
  39. AuthString_Unknown
  40. }
  41.  
  42. ArrayList g_hRecipientAdt;
  43.  
  44. #define UPDATER_URL "http://plugins.gugyclan.eu/calladmin/calladmin_steam.txt"
  45.  
  46. public Plugin myinfo =
  47. {
  48. name = "CallAdmin: Steam module",
  49. author = "Impact, dordnung",
  50. description = "The steammodule for CallAdmin",
  51. version = CALLADMIN_VERSION,
  52. url = "http://gugyclan.eu"
  53. }
  54.  
  55. public void OnPluginStart()
  56. {
  57. BuildPath(Path_SM, g_sSteamIDConfigFile, sizeof(g_sSteamIDConfigFile), "configs/calladmin_steam_steamidlist.cfg");
  58.  
  59. if (!FileExists(g_sSteamIDConfigFile))
  60. {
  61. CreateSteamIDList();
  62. }
  63.  
  64. BuildPath(Path_SM, g_sGroupIDConfigFile, sizeof(g_sGroupIDConfigFile), "configs/calladmin_steam_groupidlist.cfg");
  65.  
  66. if (!FileExists(g_sGroupIDConfigFile))
  67. {
  68. CreateGroupIDList();
  69. }
  70.  
  71. g_hSteamID2Regex = new Regex("^STEAM_[0-1]{1}:[0-1]{1}:[0-9]+$");
  72. g_hSteamID3Regex = new Regex("^\\[U:1:[0-9]{3,11}+\\]$");
  73. g_hCommunityIDRegex = new Regex("^[0-9]{4,17}+$");
  74.  
  75. g_hRecipientAdt = new ArrayList(ByteCountToCells(21));
  76.  
  77. MessageBot_ClearRecipients();
  78.  
  79. ParseSteamIDList();
  80.  
  81. ParseGroupIDList();
  82.  
  83. RegConsoleCmd("sm_calladmin_steam_reload", Command_Reload);
  84. RegConsoleCmd("sm_calladmin_steam_listrecipients", Command_ListRecipients);
  85.  
  86. AutoExecConfig_SetFile("plugin.calladmin_steam");
  87.  
  88. g_hVersion = AutoExecConfig_CreateConVar("sm_calladmin_steam_version", CALLADMIN_VERSION, "Plugin version", FCVAR_NOTIFY|FCVAR_DONTRECORD);
  89. g_hSteamUsername = AutoExecConfig_CreateConVar("sm_calladmin_steam_username", "", "Your steam username", FCVAR_PROTECTED);
  90. g_hSteamPassword = AutoExecConfig_CreateConVar("sm_calladmin_steam_password", "", "Your steam password", FCVAR_PROTECTED);
  91.  
  92. AutoExecConfig(true, "plugin.calladmin_steam");
  93. AutoExecConfig_CleanFile();
  94.  
  95. g_hVersion.SetString(CALLADMIN_VERSION, false, false);
  96. g_hVersion.AddChangeHook(OnCvarChanged);
  97.  
  98. g_hSteamUsername.GetString(g_sSteamUsername, sizeof(g_sSteamUsername));
  99. g_hSteamUsername.AddChangeHook(OnCvarChanged);
  100.  
  101. g_hSteamPassword.GetString(g_sSteamPassword, sizeof(g_sSteamPassword));
  102. g_hSteamPassword.AddChangeHook(OnCvarChanged);
  103. }
  104.  
  105. public void OnMessageResultReceived(MessageBotResult result, MessageBotError error)
  106. {
  107. static char resultString[][] = {"No error", "Error while trying to login", "Operation timed out",
  108. "No recipients were setup prior to sending a message", "Couldn't send to any recipient"};
  109.  
  110.  
  111. if (result != RESULT_NO_ERROR)
  112. {
  113. CallAdmin_LogMessage("Failed to send steam message: (result: %d [%s] | error: %d)", result, resultString[result], error);
  114. }
  115. }
  116.  
  117. void CreateSteamIDList()
  118. {
  119. File hFile;
  120. hFile = OpenFile(g_sSteamIDConfigFile, "w");
  121.  
  122. if (hFile == null)
  123. {
  124. CallAdmin_LogMessage("Failed to open configfile 'calladmin_steam_steamidlist.cfg' for writing");
  125. SetFailState("Failed to open configfile 'calladmin_steam_steamidlist.cfg' for writing");
  126. }
  127.  
  128. hFile.WriteLine("// List of steamids or communityids, seperated by a new line");
  129. hFile.WriteLine("// STEAM_0:0:1");
  130. hFile.WriteLine("// 76561197960265730");
  131.  
  132. delete hFile;
  133. }
  134.  
  135. void ParseSteamIDList()
  136. {
  137. File hFile;
  138.  
  139. hFile = OpenFile(g_sSteamIDConfigFile, "r");
  140.  
  141. if (hFile == null)
  142. {
  143. CallAdmin_LogMessage("Failed to open configfile 'calladmin_steam_steamidlist.cfg' for reading");
  144. SetFailState("Failed to open configfile 'calladmin_steam_steamidlist.cfg' for reading");
  145. }
  146.  
  147. char sReadBuffer[128];
  148.  
  149.  
  150. int len;
  151. while (!hFile.EndOfFile() && hFile.ReadLine(sReadBuffer, sizeof(sReadBuffer)))
  152. {
  153. if (sReadBuffer[0] == '/' || IsCharSpace(sReadBuffer[0]))
  154. {
  155. continue;
  156. }
  157.  
  158. ReplaceString(sReadBuffer, sizeof(sReadBuffer), "\n", "");
  159. ReplaceString(sReadBuffer, sizeof(sReadBuffer), "\r", "");
  160. ReplaceString(sReadBuffer, sizeof(sReadBuffer), "\t", "");
  161. ReplaceString(sReadBuffer, sizeof(sReadBuffer), " ", "");
  162.  
  163. len = strlen(sReadBuffer);
  164. for (int i; i < len; i++)
  165. {
  166. if (sReadBuffer[i] == ' ' || sReadBuffer[i] == '/')
  167. {
  168. sReadBuffer[i] = '\0';
  169.  
  170. break;
  171. }
  172. }
  173.  
  174. AuthStringType type = GetAuthIDType(sReadBuffer);
  175.  
  176. if (type == AuthString_SteamID2)
  177. {
  178. g_hSteamID2Regex.GetSubString(1, sReadBuffer, sizeof(sReadBuffer));
  179. }
  180. else if (type == AuthString_SteamID3)
  181. {
  182. g_hSteamID3Regex.GetSubString(1, sReadBuffer, sizeof(sReadBuffer));
  183.  
  184. SteamID3ToSteamId2(sReadBuffer, sReadBuffer, sizeof(sReadBuffer));
  185. }
  186. else if (type == AuthString_CommunityID)
  187. {
  188. g_hCommunityIDRegex.GetSubString(1, sReadBuffer, sizeof(sReadBuffer));
  189. }
  190. else
  191. {
  192. continue;
  193. }
  194.  
  195. MessageBot_AddRecipient(sReadBuffer);
  196. g_hRecipientAdt.PushString(sReadBuffer);
  197. }
  198.  
  199. hFile.Close();
  200. }
  201.  
  202. void CreateGroupIDList()
  203. {
  204. File hFile;
  205. hFile = OpenFile(g_sGroupIDConfigFile, "w");
  206.  
  207. if (hFile == null)
  208. {
  209. CallAdmin_LogMessage("Failed to open configfile 'calladmin_steam_groupidlist.cfg' for writing");
  210. SetFailState("Failed to open configfile 'calladmin_steam_groupidlist.cfg' for writing");
  211. }
  212.  
  213. hFile.WriteLine("// List of group names (custom group name), separated by a new line");
  214. hFile.WriteLine("// So for example if your community link is: http://steamcommunity.com/groups/Valve then write in a new line: Valve");
  215. hFile.WriteLine("// YourGroupName");
  216.  
  217. hFile.Close();
  218. }
  219.  
  220. void ParseGroupIDList()
  221. {
  222. File hFile;
  223.  
  224. hFile = OpenFile(g_sGroupIDConfigFile, "r");
  225.  
  226. if (hFile == null)
  227. {
  228. CallAdmin_LogMessage("Failed to open configfile 'calladmin_steam_groupidlist.cfg' for reading");
  229. SetFailState("Failed to open configfile 'calladmin_steam_groupidlist.cfg' for reading");
  230. }
  231.  
  232. char sReadBuffer[128];
  233.  
  234. int len;
  235. while (! hFile.EndOfFile() && hFile.ReadLine(sReadBuffer, sizeof(sReadBuffer)))
  236. {
  237. if (sReadBuffer[0] == '/' || IsCharSpace(sReadBuffer[0]))
  238. {
  239. continue;
  240. }
  241.  
  242. ReplaceString(sReadBuffer, sizeof(sReadBuffer), "\n", "");
  243. ReplaceString(sReadBuffer, sizeof(sReadBuffer), "\r", "");
  244. ReplaceString(sReadBuffer, sizeof(sReadBuffer), "\t", "");
  245. ReplaceString(sReadBuffer, sizeof(sReadBuffer), " ", "");
  246.  
  247. len = strlen(sReadBuffer);
  248. for (int i; i < len; i++)
  249. {
  250. if (sReadBuffer[i] == ' ' || sReadBuffer[i] == '/')
  251. {
  252. sReadBuffer[i] = '\0';
  253.  
  254. len = strlen(sReadBuffer);
  255.  
  256. break;
  257. }
  258. }
  259.  
  260. if (len < 3 || len > 64)
  261. {
  262. continue;
  263. }
  264.  
  265. FetchGroupMembers(sReadBuffer);
  266. }
  267.  
  268. hFile.Close();
  269. }
  270.  
  271. public void OnCvarChanged(Handle cvar, const char[] oldValue, const char[] newValue)
  272. {
  273. if (cvar == g_hVersion)
  274. {
  275. g_hVersion.SetString(CALLADMIN_VERSION, false, false);
  276. }
  277. else if (cvar == g_hSteamUsername)
  278. {
  279. g_hSteamUsername.GetString(g_sSteamUsername, sizeof(g_sSteamUsername));
  280. }
  281. else if (cvar == g_hSteamPassword)
  282. {
  283. g_hSteamPassword.GetString(g_sSteamPassword, sizeof(g_sSteamPassword));
  284. }
  285. }
  286.  
  287. public Action Command_Reload(int client, int args)
  288. {
  289. if (!CheckCommandAccess(client, "sm_calladmin_admin", ADMFLAG_BAN, false))
  290. {
  291. ReplyToCommand(client, " \x10[HR] \x0E%t", "CallAdmin_NoAdmin");
  292.  
  293. return Plugin_Handled;
  294. }
  295.  
  296. MessageBot_ClearRecipients();
  297. g_hRecipientAdt.Clear();
  298.  
  299. ParseSteamIDList();
  300.  
  301. ParseGroupIDList();
  302.  
  303. return Plugin_Handled;
  304. }
  305.  
  306. public Action Command_ListRecipients(int client, int args)
  307. {
  308. if (!CheckCommandAccess(client, "sm_calladmin_admin", ADMFLAG_BAN, false))
  309. {
  310. ReplyToCommand(client, " \x10[HR] \x0E%t", "CallAdmin_NoAdmin");
  311.  
  312. return Plugin_Handled;
  313. }
  314.  
  315. int count = g_hRecipientAdt.Length;
  316. char sRecipientBuffer[21];
  317.  
  318. if (count)
  319. {
  320. for (int i; i < count; i++)
  321. {
  322. g_hRecipientAdt.GetString(i, sRecipientBuffer, sizeof(sRecipientBuffer));
  323.  
  324. ReplyToCommand(client, "Recipient %d: %s%s", i + 1, sRecipientBuffer, MessageBot_IsRecipient(sRecipientBuffer) ? "" : " (Not In Messagebot's list)");
  325. }
  326. }
  327. else
  328. {
  329. ReplyToCommand(client, "Recipient list is empty");
  330. }
  331.  
  332. return Plugin_Handled;
  333. }
  334.  
  335. public void OnAllPluginsLoaded()
  336. {
  337. if (LibraryExists("updater"))
  338. {
  339. Updater_AddPlugin(UPDATER_URL);
  340. }
  341. }
  342.  
  343. public void OnLibraryAdded(const char[] name)
  344. {
  345. if (StrEqual(name, "updater"))
  346. {
  347. Updater_AddPlugin(UPDATER_URL);
  348. }
  349. }
  350.  
  351. public void CallAdmin_OnReportPost(int client, int target, const char[] reason)
  352. {
  353. MessageBot_SetLoginData(g_sSteamUsername, g_sSteamPassword);
  354.  
  355. char sClientName[MAX_NAME_LENGTH];
  356. char sClientID[21];
  357.  
  358. char sTargetName[MAX_NAME_LENGTH];
  359. char sTargetID[21];
  360.  
  361. char sServerIP[16];
  362. int serverPort;
  363. char sServerName[128];
  364.  
  365. CallAdmin_GetHostIP(sServerIP, sizeof(sServerIP));
  366. serverPort = CallAdmin_GetHostPort();
  367. CallAdmin_GetHostName(sServerName, sizeof(sServerName));
  368.  
  369. if (client == REPORTER_CONSOLE)
  370. {
  371. strcopy(sClientName, sizeof(sClientName), "Server/Console");
  372. strcopy(sClientID, sizeof(sClientID), "Server/Console");
  373. }
  374. else
  375. {
  376. GetClientName(client, sClientName, sizeof(sClientName));
  377.  
  378. if (!GetClientAuthId(client, AuthId_Steam2, sClientID, sizeof(sClientID)))
  379. {
  380. CallAdmin_LogMessage("Failed to get authentication for client %d (%s)", client, sClientName);
  381.  
  382. return;
  383. }
  384. }
  385.  
  386. GetClientName(target, sTargetName, sizeof(sTargetName));
  387.  
  388. if (!GetClientAuthId(target, AuthId_Steam2, sTargetID, sizeof(sTargetID)))
  389. {
  390. CallAdmin_LogMessage("Failed to get authentication for client %d (%s)", client, sTargetName);
  391.  
  392. return;
  393. }
  394.  
  395. g_iLastReportID = CallAdmin_GetReportID();
  396.  
  397. char sMessage[4096];
  398. Format(sMessage, sizeof(sMessage), "-\nÚj jelentés a szerveren: Herbál Reality | JailBreak\nJelentésID: %d\nJelentő: %s\nGyanúsított: %s\nIndok: %s\nHa játékban vagy, írd be /handle %d a chat ablakba, hogy kezeld ezt a jelentést!", g_iLastReportID, sClientName, sTargetName, reason, g_iLastReportID);
  399.  
  400. MessageBot_SendMessage(OnMessageResultReceived, sMessage);
  401. }
  402.  
  403. public void CallAdmin_OnReportHandled(int client, int id)
  404. {
  405. if (id != g_iLastReportID)
  406. {
  407. return;
  408. }
  409.  
  410. char sMessage[1024];
  411. Format(sMessage, sizeof(sMessage), "\nAz utolsó jelentést (%d) kezelte: %N", g_iLastReportID, client);
  412.  
  413. MessageBot_SendMessage(OnMessageResultReceived, sMessage);
  414. }
  415.  
  416. void FetchGroupMembers(const char[] groupID)
  417. {
  418. char sGroupID[64 * 4];
  419. System2_URLEncode(sGroupID, sizeof(sGroupID), groupID);
  420.  
  421. System2HTTPRequest httpRequest = new System2HTTPRequest(OnHTTPReceive, "https://steamcommunity.com/groups/%s/memberslistxml?xml=1", sGroupID);
  422. httpRequest.Timeout = 10;
  423.  
  424. httpRequest.GET();
  425.  
  426. delete httpRequest;
  427. }
  428.  
  429. public void OnHTTPReceive(bool success, const char[] error, System2HTTPRequest request, System2HTTPResponse response, HTTPRequestMethod method)
  430. {
  431. if (!success)
  432. {
  433. CallAdmin_LogMessage("Error on fetching group members: %s", error);
  434. return;
  435. }
  436.  
  437. if (response.StatusCode != 200)
  438. {
  439. CallAdmin_LogMessage("Error on fetching group members: HTTP status code %d", response.StatusCode);
  440. return;
  441. }
  442.  
  443. char[] data = new char[response.ContentLength + 1];
  444. response.GetContent(data, response.ContentLength + 1);
  445.  
  446. static int SPLITSIZE1 = MAX_ITEMS + 50;
  447. static int SPLITSIZE2 = 64;
  448.  
  449. char[][] Split = new char[SPLITSIZE1][SPLITSIZE2];
  450. char sTempID[21];
  451.  
  452. int startindex = 0;
  453. if ( (startindex = StrContains(data, "<members>", true)) == -1)
  454. {
  455. startindex = 0;
  456. }
  457.  
  458. int endindex = strlen(data);
  459. if ( (endindex = StrContains(data, "</members>", true)) != -1)
  460. {
  461. data[endindex] = '\0';
  462. }
  463.  
  464. ExplodeString(data[startindex], "<steamID64>", Split, SPLITSIZE1, SPLITSIZE2);
  465.  
  466. int splitsize = SPLITSIZE1;
  467. int index;
  468. for (int i; i < splitsize; i++)
  469. {
  470. if (strlen(Split[i]) > 0)
  471. {
  472. if ( (index = StrContains(Split[i], "</steamID64>", true)) != -1)
  473. {
  474. Split[i][index] = '\0';
  475. }
  476.  
  477. if (GetAuthIDType(Split[i]) != AuthString_CommunityID)
  478. {
  479. continue;
  480. }
  481.  
  482. strcopy(sTempID, sizeof(sTempID), Split[i]);
  483.  
  484. MessageBot_AddRecipient(sTempID);
  485. g_hRecipientAdt.PushString(sTempID);
  486. }
  487. }
  488. }
  489.  
  490. stock AuthStringType GetAuthIDType(const char[] auth)
  491. {
  492. if (g_hSteamID2Regex.Match(auth) == 1)
  493. {
  494. return AuthString_SteamID2;
  495. }
  496. else if (g_hSteamID3Regex.Match(auth) == 1)
  497. {
  498. return AuthString_SteamID3;
  499. }
  500. else if (g_hCommunityIDRegex.Match(auth) == 1)
  501. {
  502. return AuthString_CommunityID;
  503. }
  504.  
  505. return AuthString_Unknown;
  506. }
  507.  
  508. stock void SteamID3ToSteamId2(const char[] steamID3, char[] dest, int max_len)
  509. {
  510. char sTemp[21];
  511. strcopy(sTemp, sizeof(sTemp), steamID3);
  512.  
  513. sTemp[strlen(sTemp)] = '\0';
  514.  
  515. int temp = StringToInt(sTemp[5]);
  516.  
  517. Format(dest, max_len, "STEAM_0:%d:%d", temp & 1, temp >> 1);
  518. }
Add Comment
Please, Sign In to add comment