Skip to content

Commit

Permalink
Added WebLink plugin for opening MOTD panels
Browse files Browse the repository at this point in the history
  • Loading branch information
WheteThunger committed Jul 28, 2017
1 parent 8e2b0d5 commit a88186f
Show file tree
Hide file tree
Showing 5 changed files with 368 additions and 14 deletions.
7 changes: 4 additions & 3 deletions enhancements/10x/weaponlist.sp
@@ -1,13 +1,14 @@
#include <sourcemod>
#include <sdktools>
#include <rxgtfcommon>
#include <weblink>

public Plugin myinfo =
{
name = "MOTD Weapon List",
author = "Roker",
description = "Opens MOTD with Mayhem Weapon list",
version = "1.1.1",
version = "1.2.0",
url = "http://www.reflex-gamers.com"
}
public OnPluginStart()
Expand All @@ -22,7 +23,7 @@ public OnPluginStart()

//Displays WEAPON LIST
public Action viewList(client,args) {
ShowMOTDPanel(client, "WeaponList", "https://reflex-gamers.com/weaponlist/pages/home-game", MOTDPANEL_TYPE_URL);
WEBLINK_OpenUrl(client, "https://reflex-gamers.com/weaponlist/pages/home-game");
return Plugin_Handled;
}

Expand All @@ -33,6 +34,6 @@ public Action viewLoadout(client,args) {

char url[128];
Format(url, sizeof(url), "https://reflex-gamers.com/weaponlist/pages/view-weapons/%i/%i/%i/%i", loadout[0], loadout[1] , loadout[2], loadout[3]);
ShowMOTDPanel(client, "Loadout", url , MOTDPANEL_TYPE_URL);
WEBLINK_OpenUrl(client, url);
return Plugin_Handled;
}
313 changes: 313 additions & 0 deletions enhancements/weblink.sp
@@ -0,0 +1,313 @@

#include <sourcemod>
#include <rxgstore>
#include <dbrelay>
#include <rxgcommon>

#pragma semicolon 1
#pragma newdecls required

//-----------------------------------------------------------------------------
public Plugin myinfo = {
name = "RXG Web Links",
author = "WhiteThunder",
description = "",
version = "0.1",
url = "www.reflex-gamers.com"
};

//-----------------------------------------------------------------------------

// weblink config
KeyValues kv_config;

char g_database[65];
char g_url[256];

// the ip for this server (hostip converted into ipv4 format)
char c_ip[32];

int g_session_id[MAXPLAYERS];
int g_session_token[MAXPLAYERS];

//-----------------------------------------------------------------------------
int GAME;

#define GAME_CSGO 0
#define GAME_TF2 1

//-----------------------------------------------------------------------------
public APLRes AskPluginLoad2( Handle myself, bool late, char[] error, int err_max ) {
CreateNative( "WEBLINK_OpenUrl", Native_OpenUrl );
RegPluginLibrary("weblink");
}

//-----------------------------------------------------------------------------
public void OnClientDisconnect( int client ) {
g_session_id[client] = 0;
g_session_token[client] = 0;
}

//-----------------------------------------------------------------------------
public void OnPluginStart() {

LoadTranslations( "common.phrases" );

RegConsoleCmd( "sm_web", Command_Web );

char gamedir[8];
GetGameFolderName( gamedir, sizeof gamedir );
if( StrEqual( gamedir, "csgo", false )) {
GAME = GAME_CSGO;
} else {
GAME = GAME_TF2;
}

GetIPv4( c_ip, sizeof c_ip );
LoadConfigFile();
}

//-----------------------------------------------------------------------------
void LoadConfigFile() {

kv_config = CreateKeyValues( "weblink" );

char filepath[256];
BuildPath( Path_SM, filepath, sizeof(filepath), "configs/weblink.txt" );

if( !FileExists( filepath ) ) {
SetFailState( "weblink.txt not found" );
return;
}

if( !kv_config.ImportFromFile( filepath ) ) {
SetFailState( "Error loading config file." );
return;
}

kv_config.GetString( "database", g_database, sizeof g_database );
kv_config.GetString( "url", g_url, sizeof g_url );

delete kv_config;
}

//-----------------------------------------------------------------------------
public int Native_OpenUrl( Handle plugin, int numParams ) {

int userid = GetNativeCell(1);

int len;
GetNativeStringLength( 2, len );

char[] url = new char[len + 1];
GetNativeString( 2, url, len + 1 );

OpenUrl( userid, url );
}

//-----------------------------------------------------------------------------
public Action Command_Web( int client, int args ) {
char url[256];
GetCmdArg( 1, url, sizeof url );
OpenUrl( client, url );
return Plugin_Handled;
}

//-----------------------------------------------------------------------------
public void OpenUrl( int client, const char[] url ) {

DataPack pack = new DataPack();
pack.WriteString( url );

QueryClientConVar( client, "cl_disablehtmlmotd", ConVar_QueryClient, pack );
}

//-----------------------------------------------------------------------------
public void ConVar_QueryClient( QueryCookie cookie, int client,
ConVarQueryResult result,
const char[] cvarName,
const char[] cvarValue,
DataPack data ) {

data.Reset();
char url[256];
data.ReadString( url, sizeof url );
delete data;

if( cookie == QUERYCOOKIE_FAILED ) {
return;
}

if( StringToInt(cvarValue) == 1 ) {
PrintToChat( client,
"\x01You have web pages blocked. Please unblock web pages by entering \x04cl_disablehtmlmotd 0 \x01in console." );

return;
}

ProcessUrlRequest( client, url );
}

//-----------------------------------------------------------------------------
public void ProcessUrlRequest( int client, const char[] url ) {
int userid = GetClientUserId(client);

if( g_session_id[client] ) {
Session_AddUrl( userid, url );
} else {
Session_Create( userid, url );
}
}

//-----------------------------------------------------------------------------
public void Session_Create( int userid, const char[] url ) {

int client = GetClientOfUserId(userid);

if( !DBRELAY_IsConnected() ) {
PrintToChat( client, "Please try again later" );
return;
}

int token = GetRandomInt( 10000, 100000 );

// save token until client disconnect
g_session_token[client] = token;

char query[512];
FormatEx( query, sizeof query,
"INSERT INTO %s.weblink_session (user_id, token, server) VALUES (%d, %d, '%s')",
g_database,
GetSteamAccountID(client),
token,
c_ip );

DataPack pack = new DataPack();
pack.WriteCell( userid );
pack.WriteString( url );

// SourceMod guarantees these callbacks will be called in order,
// so we don't have to worry about race conditions with other users
DBRELAY_TQuery( IgnoredSQLResult, query );
DBRELAY_TQuery( Session_OnCreated, "SELECT LAST_INSERT_ID()", pack );
}

//-----------------------------------------------------------------------------
public void Session_OnCreated( Handle owner, Handle hndl, const char[] error,
DataPack data ) {

if( !hndl ) {
delete data;
LogError( "SQL error fetching WebLink Session ID ::: %s", error );
return;
}

int session_id;

if( SQL_FetchRow( hndl ) ) {
session_id = SQL_FetchInt( hndl, 0 );
}

data.Reset();
int userid = data.ReadCell();
char url[256];
data.ReadString( url, sizeof url );
delete data;

int client = GetClientOfUserId( userid );
if( client == 0 ) return; // disconnected

// save session until client disconnect
g_session_id[client] = session_id;

Session_AddUrl( userid, url );
}

//-----------------------------------------------------------------------------
void Session_AddUrl( int userid, const char[] url ) {

int client = GetClientOfUserId(userid);

// could be disconnected if called directly
if( !DBRELAY_IsConnected() ) {
PrintToChat( client, "Please try again later" );
return;
}

Handle db;
DBRELAY_GetDatabase( db );

char url_escaped[256];
SQL_EscapeString( db, url, url_escaped, sizeof url_escaped );

char query[512];
FormatEx( query, sizeof query,
"INSERT INTO %s.weblink_url (weblink_session_id, url) VALUES (%d, '%s')",
g_database,
g_session_id[client],
url_escaped );

DataPack pack = new DataPack();
pack.WriteCell( userid );
pack.WriteString( url );

DBRELAY_TQuery( Session_OnUrlAdded, query, pack );
}

//-----------------------------------------------------------------------------
public void Session_OnUrlAdded( Handle owner, Handle hndl, const char[] error,
DataPack data ) {

if( !hndl ) {
delete data;
LogError( "SQL error fetching WebLink Session URL ID ::: %s", error );
return;
}

char url[256];

data.Reset();
int userid = data.ReadCell();
data.ReadString( url, sizeof url );
delete data;

int client = GetClientOfUserId(userid);
if( client == 0 ) return; //disconnected

ShowUrl( client );
}

//-----------------------------------------------------------------------------
public void ShowUrl( int client ) {

char game_abbr[13];

if( GAME == GAME_CSGO ) {
game_abbr = "csgo";
} else if( GAME == GAME_TF2 ) {
game_abbr = "tf2";
} else {
game_abbr = "unknown";
}

char url[512];
FormatEx( url, sizeof url,
"%s?id=%d&token=%d&game=%s",
g_url,
g_session_id[client],
g_session_token[client],
game_abbr );

KeyValues kv = new KeyValues( "motd" );
kv.SetString( "title", "" );
kv.SetNum( "type", MOTDPANEL_TYPE_URL );
kv.SetString( "msg", url );
ShowVGUIPanel( client, "info", kv, true );
delete kv;

//ShowMOTDPanel( client, "", url, MOTDPANEL_TYPE_URL );
}

//-----------------------------------------------------------------------------
public void IgnoredSQLResult( Handle owner, Handle hndl, const char[] error,
any data ) {}
13 changes: 13 additions & 0 deletions include/rxgcommon.inc
Expand Up @@ -107,3 +107,16 @@ stock GetIPv4( char[] buffer, int buffer_size ) {
FormatEx( buffer, buffer_size, "%d.%d.%d.%d",
pieces[0], pieces[1], pieces[2], pieces[3] );
}

//-----------------------------------------------------------------------------
stock URLEncode( const char[] input_str, char[] output_str, int len ) {
strcopy( output_str, len, input_str );

// Make sure % is first to avoid collisions.
char ReplaceThis[20][] = {"%", " ", "!", "*", "'", "(", ")", ";", ":", "@", "&", "=", "+", "$", ",", "/", "?", "#", "[", "]"};
char ReplaceWith[20][] = {"%25", "%20", "%21", "%2A", "%27", "%28", "%29", "%3B", "%3A", "%40", "%26", "%3D", "%2B", "%24", "%2C", "%2F", "%3F", "%23", "%5B", "%5D"};

for( new i = 0; i < 20; i++ ) {
ReplaceString( output_str, len, ReplaceThis[i], ReplaceWith[i] );
}
}
29 changes: 29 additions & 0 deletions include/weblink.inc
@@ -0,0 +1,29 @@
#if defined _weblink_included
#endinput
#endif

#define _weblink_included

//-----------------------------------------------------------------------------
// Opens the specified link
//
native bool WEBLINK_OpenUrl( int client, const char[] link );

//-----------------------------------------------------------------------------
public SharedPlugin __pl_weblink =
{
name = "weblink",
file = "weblink.smx",
#if defined REQUIRE_PLUGIN
required = 1,
#else
required = 0,
#endif
};

#if !defined REQUIRE_PLUGIN
public __pl_weblink_SetNTVOptional()
{
MarkNativeAsOptional("WEBLINK_OpenUrl");
}
#endif

0 comments on commit a88186f

Please sign in to comment.