Thread: Module: Curl
View Single Post
Th3822
Member
Join Date: Jan 2013
Location: Venezuela
Old 01-31-2017 , 00:28   Re: Module: Curl
Reply With Quote #25

Here is a full plugin that does use a discord's webhooks:

Compiling with amxx >= 1.9?... See here

PHP Code:
#include <amxmodx>
#include <json>
#include <curl>

new const g_szURL[] = "https://discordapp.com/api/webhooks/{webhook.id}/{webhook.token}";
new const 
g_iTimeBetweenCalls 30;

new 
g_iLastCallbool:g_bIsWorkingCURL:g_cURLHandlecurl_slist:g_cURLHeadersg_szHostname[129], g_szNetAddress[22];

public 
plugin_init()
{
    
register_plugin("[cURL] Discord !admin Webhook""1.0""Th3-822");

    
register_clcmd("say !admin""cmd_admincall");
}

public 
plugin_cfg()
{
    
// Add a delay to wait for the values for g_szHostname and g_szNetAddress
    
g_iLastCall get_systime();
    
set_task(10.0"plugin_cfg_delayed");
}

public 
plugin_cfg_delayed()
{
    
get_cvar_string("hostname"g_szHostnamecharsmax(g_szHostname));
    
get_cvar_string("net_address"g_szNetAddresscharsmax(g_szNetAddress));
}

public 
plugin_end()
{
    if (
g_cURLHandle)
    {
        
curl_easy_cleanup(g_cURLHandle);
        
g_cURLHandle CURL:0;
    }
    if (
g_cURLHeaders)
    {
        
curl_slist_free_all(g_cURLHeaders);
        
g_cURLHeaders curl_slist:0;
    }
}

// Replace MB Chars with "."
_fixName(name[])
{
    new 
0;
    while (
name[i] != 0)
    {
        if (!(
<= name[i] <= 255))
        {
            
name[i] = '.';
        }
        
i++;
    }
}

public 
cmd_admincall(id)
{
    static 
iCurTime;

    if (!
g_bIsWorking && ((iCurTime get_systime()) - g_iLastCall) > g_iTimeBetweenCalls)
    {
        
g_iLastCall iCurTime;

        static 
szName[32], szAuthId[35], szBuffer[129], JSON:jEmbeds[1], JSON:jEmbedJSON:jWebhook;
        
get_user_name(idszNamecharsmax(szName));
        
get_user_authid(idszAuthIdcharsmax(szAuthId));
        
_fixName(szName);

        
// Create array of embed objects
        
jEmbed json_create();
        
json_set_string(jEmbed"title"g_szHostname);
        
formatex(szBuffercharsmax(szBuffer), "Click to enter on the server: steam://connect/%s"g_szNetAddress);
        
json_set_string(jEmbed"description"szBuffer);
        
jEmbeds[0] = jEmbed;

        
// Create webhook request object
        
jWebhook json_create();
        
formatex(szBuffercharsmax(szBuffer), "@here ^"%s^" <%s> is calling an Admin."szNameszAuthId);
        
json_set_string(jWebhook"content"szBuffer);
        
json_set_array(jWebhook"embeds"jEmbedssizeof(jEmbeds), _JSON_Object);

        
// Send It
        
postJSON(g_szURLjWebhook);
        
json_destroy(jWebhook);
        
// json_destroy(jEmbed); // Destroyed in chain
    
}
    else 
client_print(idprint_chat" ** Admins Can Be Called Once Each %d Seconds **"g_iTimeBetweenCalls);

    return 
PLUGIN_HANDLED;
}

postJSON(const link[], JSON:jObject)
{
    if (!
g_cURLHandle)
    {
        if (!(
g_cURLHandle curl_easy_init()))
        {
            
log_amx("[Fatal Error] Cannot Init cURL Handle.");
            
pause("d");
            return;
        }
        if (!
g_cURLHeaders)
        {
            
// Init g_cURLHeaders with "Content-Type: application/json"
            
g_cURLHeaders curl_slist_append(g_cURLHeaders"Content-Type: application/json");
            
curl_slist_append(g_cURLHeaders"User-Agent: 822_AMXX_PLUGIN/1.0"); // User-Agent
            
curl_slist_append(g_cURLHeaders"Connection: Keep-Alive"); // Keep-Alive
        
}

        
// Static Options
        
curl_easy_setopt(g_cURLHandleCURLOPT_SSL_VERIFYPEER0);
        
curl_easy_setopt(g_cURLHandleCURLOPT_SSL_VERIFYHOST0);
        
curl_easy_setopt(g_cURLHandleCURLOPT_SSLVERSIONCURL_SSLVERSION_TLSv1);
        
curl_easy_setopt(g_cURLHandleCURLOPT_FAILONERROR0);
        
curl_easy_setopt(g_cURLHandleCURLOPT_FOLLOWLOCATION0);
        
curl_easy_setopt(g_cURLHandleCURLOPT_FORBID_REUSE0);
        
curl_easy_setopt(g_cURLHandleCURLOPT_FRESH_CONNECT0);
        
curl_easy_setopt(g_cURLHandleCURLOPT_CONNECTTIMEOUT10);
        
curl_easy_setopt(g_cURLHandleCURLOPT_TIMEOUT10);
        
curl_easy_setopt(g_cURLHandleCURLOPT_HTTPHEADERg_cURLHeaders);
        
curl_easy_setopt(g_cURLHandleCURLOPT_POST1);
    }

    static 
szPostdata[513];
    
json_encode(jObjectszPostdatacharsmax(szPostdata));
    
//log_amx("[DEBUG] POST: %s", szPostdata);

    
curl_easy_setopt(g_cURLHandleCURLOPT_URLlink);
    
curl_easy_setopt(g_cURLHandleCURLOPT_COPYPOSTFIELDSszPostdata);

    
g_bIsWorking true;
    
curl_easy_perform(g_cURLHandle"postJSON_done");
}

public 
postJSON_done(CURL:curlCURLcode:code)
{
    
g_bIsWorking false;
    if (
code == CURLE_OK)
    {
        static 
statusCode;
        
curl_easy_getinfo(curlCURLINFO_RESPONSE_CODEstatusCode);
        if (
statusCode >= 400)
        {
            
log_amx("[Error] HTTP Error: %d"statusCode);
        }
    }
    else
    {
        
log_amx("[Error] cURL Error: %d"code);
        
curl_easy_cleanup(g_cURLHandle);
        
g_cURLHandle CURL:0;
    }

btw, curl_easy_cleanup and curl_slist_free_all should set the pointer to a invalid value to avoid a crash.

Last edited by Th3822; 02-15-2022 at 14:45. Reason: Added link to compiling with 1.9
Th3822 is offline