From 761ecadf0aa4277f83089fc8b6b267b9857175d2 Mon Sep 17 00:00:00 2001 From: neon Date: Mon, 23 Jul 2018 10:45:59 +0200 Subject: [PATCH] initial commit --- includes/unloze.inc | 30 ++ .../scripting/unloze_ForumIntegration.sp | 339 ++++++++++++++++++ 2 files changed, 369 insertions(+) create mode 100644 includes/unloze.inc create mode 100644 unloze_ForumIntegration/scripting/unloze_ForumIntegration.sp diff --git a/includes/unloze.inc b/includes/unloze.inc new file mode 100644 index 00000000..0a4a1558 --- /dev/null +++ b/includes/unloze.inc @@ -0,0 +1,30 @@ +#if defined _unloze_ForumIntegration_Included + #endinput +#endif +#define _unloze_ForumIntegration_Included + +typeset HasSteamIDReservedSlotCallback +{ + function void (const char[] sSteam32ID, int Result); + function void (const char[] sSteam32ID, int Result, any Data); +}; + +native void HasSteamIDReservedSlot(const char[] sSteam32ID, HasSteamIDReservedSlotCallback Callback, any Data = 0); + +public SharedPlugin __pl_unloze_ForumIntegration = +{ + name = "UNLOZE Forum Integration", + file = "unloze_ForumIntegration.smx", +#if defined REQUIRE_PLUGIN + required = 1, +#else + required = 0, +#endif +}; + +#if !defined REQUIRE_PLUGIN +public __pl_UNLOZE Forum Integration_SetNTVOptional() +{ + MarkNativeAsOptional("HasSteamIDReservedSlot"); +} +#endif \ No newline at end of file diff --git a/unloze_ForumIntegration/scripting/unloze_ForumIntegration.sp b/unloze_ForumIntegration/scripting/unloze_ForumIntegration.sp new file mode 100644 index 00000000..0c3dc92d --- /dev/null +++ b/unloze_ForumIntegration/scripting/unloze_ForumIntegration.sp @@ -0,0 +1,339 @@ +//==================================================================================================== +// +// Name: UNLOZE Forum Integration. +// Author: .George & zaCade (Original by Botox) +// Description: Handles forum access ingame. +// +//==================================================================================================== +#include +#include +#include + +#pragma newdecls required + +#define APIKEY "k9Mk2lA3mF9Sk0FoaD" + +/* STRINGS */ +char G_sGroup[MAXPLAYERS+1][64]; + +/* BOOLS */ +bool G_bPreAdminChecked[MAXPLAYERS+1]; +bool G_bResponseFailed[MAXPLAYERS+1]; +bool G_bResponsePassed[MAXPLAYERS+1]; + +//---------------------------------------------------------------------------------------------------- +// Purpose: +//---------------------------------------------------------------------------------------------------- +public Plugin myinfo = +{ + name = "UNLOZE Forum Integration", + author = ".George & zaCade (Original by Botox)", + description = "Handles forum access ingame", + version = "1.2" +}; + +//---------------------------------------------------------------------------------------------------- +// Purpose: +//---------------------------------------------------------------------------------------------------- +public APLRes AskPluginLoad2(Handle myself, bool late, char[] error, int err_max) +{ + CreateNative("HasSteamIDReservedSlot", Native_HasSteamIDReservedSlot); + + RegPluginLibrary("unloze"); + + return APLRes_Success; +} + +//---------------------------------------------------------------------------------------------------- +// Purpose: +//---------------------------------------------------------------------------------------------------- +public void OnRebuildAdminCache(AdminCachePart part) +{ + if (part != AdminCache_Admins) + return; + + CreateTimer(1.0, OnRebuildAdminCachePost, INVALID_HANDLE, TIMER_FLAG_NO_MAPCHANGE); +} + +//---------------------------------------------------------------------------------------------------- +// Purpose: +//---------------------------------------------------------------------------------------------------- +public Action OnRebuildAdminCachePost(Handle hTimer) +{ + for (int client = 1; client <= MaxClients; client++) + { + if(G_bResponsePassed[client] && G_bPreAdminChecked[client]) + ApplyGroupFlags(client); + } +} + +//---------------------------------------------------------------------------------------------------- +// Purpose: +//---------------------------------------------------------------------------------------------------- +public void OnClientConnected(int client) +{ + G_bPreAdminChecked[client] = false; + G_bResponseFailed[client] = false; + G_bResponsePassed[client] = false; + + G_sGroup[client][0] = 0; +} + +//---------------------------------------------------------------------------------------------------- +// Purpose: +//---------------------------------------------------------------------------------------------------- +public void OnClientDisconnect(int client) +{ + G_bPreAdminChecked[client] = false; + G_bResponseFailed[client] = false; + G_bResponsePassed[client] = false; + + G_sGroup[client][0] = 0; +} + +//---------------------------------------------------------------------------------------------------- +// Purpose: +//---------------------------------------------------------------------------------------------------- +public void OnClientAuthorized(int client, const char[] sSteamID32) +{ + if (IsFakeClient(client)) + return; + + char sSteamID64[32]; + SteamID32toSteamID64(sSteamID32, sSteamID64, sizeof(sSteamID64)); + + int iSerial = GetClientSerial(client); + + char sRequest[256]; + FormatEx(sRequest, sizeof(sRequest), "https://unloze.com/api/private_api.php?api_key=%s&steam_id=%s", APIKEY, sSteamID64); + + Handle hRequest = SteamWorks_CreateHTTPRequest(k_EHTTPMethodGET, sRequest); + if (!hRequest || + !SteamWorks_SetHTTPCallbacks(hRequest, OnClientAuthorized_OnTransferComplete) || + !SteamWorks_SetHTTPRequestContextValue(hRequest, iSerial) || + !SteamWorks_SendHTTPRequest(hRequest)) + { + delete hRequest; + } +} + +//---------------------------------------------------------------------------------------------------- +// Purpose: +//---------------------------------------------------------------------------------------------------- +public int OnClientAuthorized_OnTransferComplete(Handle hRequest, bool bFailure, bool bSuccessful, EHTTPStatusCode eStatusCode, int iSerial) +{ + int client = GetClientFromSerial(iSerial); + + if (!client) //Player disconnected. + { + delete hRequest; + return; + } + + if (bFailure || !bSuccessful || eStatusCode != k_EHTTPStatusCode200OK) + { + G_bResponseFailed[client] = true; + + if (G_bPreAdminChecked[client]) + NotifyPostAdminCheck(client); + + delete hRequest; + return; + } + + SteamWorks_GetHTTPResponseBodyCallback(hRequest, OnClientAuthorized_OnTransferResponse, iSerial); +} + +//---------------------------------------------------------------------------------------------------- +// Purpose: +//---------------------------------------------------------------------------------------------------- +public int OnClientAuthorized_OnTransferResponse(char[] sData, int iSerial) +{ + int client = GetClientFromSerial(iSerial); + + if (!client) //Player disconnected. + return; + + TrimString(sData); + StripQuotes(sData); + + strcopy(G_sGroup[client], sizeof(G_sGroup[]), sData); + + G_bResponsePassed[client] = true; + + if (G_bPreAdminChecked[client]) + NotifyPostAdminCheck(client); +} + +//---------------------------------------------------------------------------------------------------- +// Purpose: +//---------------------------------------------------------------------------------------------------- +public Action OnClientPreAdminCheck(int client) +{ + G_bPreAdminChecked[client] = true; + + if (G_bResponsePassed[client] || G_bResponseFailed[client]) + return Plugin_Continue; + + RunAdminCacheChecks(client); + return Plugin_Handled; +} + +//---------------------------------------------------------------------------------------------------- +// Purpose: +//---------------------------------------------------------------------------------------------------- +public void OnClientPostAdminFilter(int client) +{ + ApplyGroupFlags(client); +} + +//---------------------------------------------------------------------------------------------------- +// Purpose: +//---------------------------------------------------------------------------------------------------- +stock void ApplyGroupFlags(int client) +{ + if (!G_bResponsePassed[client]) + return; + + AdminId AdmID; + GroupId GrpID; + + if ((AdmID = GetUserAdmin(client)) == INVALID_ADMIN_ID) + { + LogMessage("Creating new admin for %L", client); + + AdmID = CreateAdmin(); + SetUserAdmin(client, AdmID, true); + } + + if ((GrpID = FindAdmGroup(G_sGroup[client])) != INVALID_GROUP_ID) + { + if (AdminInheritGroup(AdmID, GrpID)) + { + LogMessage("%L added to group %s", client, G_sGroup[client]); + } + } + else + { + LogMessage("%L group not found %s", client, G_sGroup[client]); + } +} + +//---------------------------------------------------------------------------------------------------- +// Purpose: +//---------------------------------------------------------------------------------------------------- +public int Native_HasSteamIDReservedSlot(Handle plugin, int numParams) +{ + char sSteamID32[32]; + GetNativeString(1, sSteamID32, sizeof(sSteamID32)); + + HasSteamIDReservedSlotCallback callback; + callback = GetNativeCell(2); + + any data; + data = GetNativeCell(3); + + char sSteamID64[32]; + SteamID32toSteamID64(sSteamID32, sSteamID64, sizeof(sSteamID64)); + + char sRequest[256]; + FormatEx(sRequest, sizeof(sRequest), "https://unloze.com/api/private_api.php?api_key=%s&steam_id=%s", APIKEY, sSteamID64); + + DataPack hDataPack = new DataPack(); + hDataPack.WriteString(sSteamID32); + hDataPack.WriteFunction(callback); + hDataPack.WriteCell(plugin); + hDataPack.WriteCell(data); + + Handle hRequest = SteamWorks_CreateHTTPRequest(k_EHTTPMethodGET, sRequest); + if (!hRequest || + !SteamWorks_SetHTTPCallbacks(hRequest, Native_HasSteamIDReservedSlot_OnTransferComplete) || + !SteamWorks_SetHTTPRequestContextValue(hRequest, hDataPack) || + !SteamWorks_SendHTTPRequest(hRequest)) + { + delete hRequest; + } +} + +//---------------------------------------------------------------------------------------------------- +// Purpose: +//---------------------------------------------------------------------------------------------------- +public int Native_HasSteamIDReservedSlot_OnTransferComplete(Handle hRequest, bool bFailure, bool bSuccessful, EHTTPStatusCode eStatusCode, DataPack hDataPack) +{ + if (bFailure || !bSuccessful || eStatusCode != k_EHTTPStatusCode200OK) + { + char sData[32] = "NOGROUP"; + Native_HasSteamIDReservedSlot_OnTransferResponse(sData, hDataPack); + + delete hRequest; + return; + } + + SteamWorks_GetHTTPResponseBodyCallback(hRequest, Native_HasSteamIDReservedSlot_OnTransferResponse, hDataPack); +} + +//---------------------------------------------------------------------------------------------------- +// Purpose: +//---------------------------------------------------------------------------------------------------- +public int Native_HasSteamIDReservedSlot_OnTransferResponse(char[] sData, DataPack hDataPack) +{ + hDataPack.Reset(); + + char sSteamID32[32]; + hDataPack.ReadString(sSteamID32, sizeof(sSteamID32)); + + HasSteamIDReservedSlotCallback callback; + callback = view_as(hDataPack.ReadFunction()); + + Handle plugin; + plugin = hDataPack.ReadCell(); + + any data; + data = hDataPack.ReadCell(); + + TrimString(sData); + StripQuotes(sData); + + int result; + if (StrEqual(sData, "Game-Donator", false)) + result = 1; + else + result = 0; + + Call_StartFunction(plugin, callback); + Call_PushString(sSteamID32); + Call_PushCell(result); + Call_PushCell(data); + Call_Finish(); + + delete hDataPack; + return; +} + +//---------------------------------------------------------------------------------------------------- +// Purpose: +//---------------------------------------------------------------------------------------------------- +stock bool SteamID32toSteamID64(const char[] sSteamID32, char[] sSteamID64, int iSize) +{ + if (strlen(sSteamID32) < 11 || strncmp(sSteamID32[0], "STEAM_", 6)) + { + sSteamID64[0] = 0; + return false; + } + + int iUpper = 765611979; + int isSteam64ID = StringToInt(sSteamID32[10]) * 2 + 60265728 + sSteamID32[8] - 48; + + int iDiv = isSteam64ID / 100000000; + int iIdx = 9 - (iDiv ? (iDiv / 10 + 1) : 0); + + iUpper += iDiv; + + IntToString(isSteam64ID, sSteamID64[iIdx], iSize - iIdx); + iIdx = sSteamID64[9]; + + IntToString(iUpper, sSteamID64, iSize); + sSteamID64[9] = iIdx; + + return true; +} \ No newline at end of file