-
Notifications
You must be signed in to change notification settings - Fork 4
/
sbpp_discord.sp
357 lines (264 loc) · 10.2 KB
/
sbpp_discord.sp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
#pragma semicolon 1
#define PLUGIN_AUTHOR "RumbleFrog, SourceBans++ Dev Team"
#define PLUGIN_VERSION "1.1.0"
#include <sourcemod>
#include <sourcebanspp>
#include <sourcecomms>
#include <SteamWorks>
#include <smjansson>
#pragma newdecls required
enum
{
Ban,
Report,
Comms,
Type_Count,
Type_Unknown,
};
int EmbedColors[Type_Count] = {
0xDA1D87, // Ban
0xF9D942, // Report
0x4362FA, // Comms
};
ConVar Convars[Type_Count],
Username,
ProfilePictureURL,
WebsiteBaseURL;
char sEndpoints[Type_Count][256]
, sHostname[64]
, sHost[64];
public Plugin myinfo =
{
name = "SourceBans++ Discord Plugin",
author = PLUGIN_AUTHOR,
description = "Listens for ban & report forward and sends it to webhook endpoints",
version = PLUGIN_VERSION,
url = "https://sbpp.github.io"
};
public void OnPluginStart()
{
CreateConVar("sbpp_discord_version", PLUGIN_VERSION, "SBPP Discord Version", FCVAR_REPLICATED | FCVAR_SPONLY | FCVAR_DONTRECORD | FCVAR_NOTIFY);
Convars[Ban] = CreateConVar("sbpp_discord_banhook", "", "Discord web hook endpoint for ban forward", FCVAR_PROTECTED);
Convars[Report] = CreateConVar("sbpp_discord_reporthook", "", "Discord web hook endpoint for report forward. If left empty, the ban endpoint will be used instead", FCVAR_PROTECTED);
Convars[Comms] = CreateConVar("sbpp_discord_commshook", "", "Discord web hook endpoint for comms forward. If left empty, the ban endpoint will be used instead", FCVAR_PROTECTED);
WebsiteBaseURL = CreateConVar("sbpp_website_url", "", "The base url of your website. Leave empty to disable");
Username = CreateConVar("sbpp_discord_username", "Sourcebans++", "The username of the webhook");
ProfilePictureURL = CreateConVar("sbpp_discord_pp_url", "https://sbpp.github.io/img/favicons/android-chrome-512x512.png", "A URL pointing to the profile picture for the webhook.");
AutoExecConfig(true,"sbpp_discord");
Convars[Ban].AddChangeHook(OnConvarChanged);
Convars[Report].AddChangeHook(OnConvarChanged);
Convars[Comms].AddChangeHook(OnConvarChanged);
}
public void OnConfigsExecuted()
{
FindConVar("hostname").GetString(sHostname, sizeof sHostname);
int ip[4];
SteamWorks_GetPublicIP(ip);
if (SteamWorks_GetPublicIP(ip))
{
Format(sHost, sizeof sHost, "%d.%d.%d.%d:%d", ip[0], ip[1], ip[2], ip[3], FindConVar("hostport").IntValue);
} else
{
int iIPB = FindConVar("hostip").IntValue;
Format(sHost, sizeof sHost, "%d.%d.%d.%d:%d", iIPB >> 24 & 0x000000FF, iIPB >> 16 & 0x000000FF, iIPB >> 8 & 0x000000FF, iIPB & 0x000000FF, FindConVar("hostport").IntValue);
}
Convars[Ban].GetString(sEndpoints[Ban], sizeof sEndpoints[]);
Convars[Report].GetString(sEndpoints[Report], sizeof sEndpoints[]);
Convars[Comms].GetString(sEndpoints[Comms], sizeof sEndpoints[]);
}
public void SBPP_OnBanPlayer(int iAdmin, int iTarget, int iTime, const char[] sReason)
{
SendReport(iAdmin, iTarget, sReason, Ban, iTime);
}
public void SourceComms_OnBlockAdded(int iAdmin, int iTarget, int iTime, int iCommType, char[] sReason)
{
SendReport(iAdmin, iTarget, sReason, Comms, iTime, iCommType);
}
public void SBPP_OnReportPlayer(int iReporter, int iTarget, const char[] sReason)
{
SendReport(iReporter, iTarget, sReason, Report);
}
void SendReport(int iClient, int iTarget, const char[] sReason, int iType = Ban, int iTime = -1, any extra = 0)
{
if (iTarget != -1 && !IsValidClient(iTarget))
return;
if (StrEqual(sEndpoints[Ban], ""))
{
LogError("Missing ban hook endpoint");
return;
}
char sAuthor[MAX_NAME_LENGTH],
sTarget[MAX_NAME_LENGTH],
sAuthorID[32],
sTargetID64[32],
sTargetID[32],
sJson[2048],
sBuffer[256],
szUsername[128],
szProfilePictureURL[256];
GetConVarString(ProfilePictureURL, szProfilePictureURL, sizeof szProfilePictureURL);
GetConVarString(Username, szUsername, sizeof szUsername);
if (IsValidClient(iClient))
{
GetClientName(iClient, sAuthor, sizeof sAuthor);
GetClientAuthId(iClient, AuthId_Steam2, sAuthorID, sizeof sAuthorID);
} else
{
Format(sAuthor, sizeof sAuthor, "Console");
Format(sAuthorID, sizeof sAuthorID, "N/A");
}
GetClientAuthId(iTarget, AuthId_SteamID64, sTargetID64, sizeof sTargetID64);
GetClientName(iTarget, sTarget, sizeof sTarget);
GetClientAuthId(iTarget, AuthId_Steam2, sTargetID, sizeof sTargetID);
Handle jRequest = json_object();
Handle jEmbeds = json_array();
Handle jContent = json_object();
json_object_set(jContent, "color", json_integer(GetEmbedColor(iType)));
char szURLBuffer[512];
GetConVarString(WebsiteBaseURL, szURLBuffer, sizeof(szURLBuffer));
if(!(StrEqual(szURLBuffer, "")))
{
json_object_set(jContent, "title", json_string("View on Sourcebans"));
if(iType == Comms)
Format(sBuffer, sizeof sBuffer, "%s/index.php?p=commslist&searchText=%s", szURLBuffer, sTargetID);
else if (iType == Ban)
Format(sBuffer, sizeof sBuffer, "%s/index.php?p=banlist&searchText=%s", szURLBuffer, sTargetID);
else if (iType == Report)
Format(sBuffer, sizeof sBuffer, "%s/index.php?p=admin&c=bans#^2", szURLBuffer);
json_object_set(jContent, "url", json_string(sBuffer));
}
Handle jContentAuthor = json_object();
json_object_set_new(jContentAuthor, "name", json_string(sTarget));
Format(sBuffer, sizeof sBuffer, "https://steamcommunity.com/profiles/%s", sTargetID64);
json_object_set_new(jContentAuthor, "url", json_string(sBuffer));
json_object_set_new(jContentAuthor, "icon_url", json_string(szProfilePictureURL));
json_object_set_new(jContent, "author", jContentAuthor);
Handle jContentFooter = json_object();
Format(sBuffer, sizeof sBuffer, "%s (%s)", sHostname, sHost);
json_object_set_new(jContentFooter, "text", json_string(sBuffer));
json_object_set_new(jContentFooter, "icon_url", json_string(szProfilePictureURL));
json_object_set_new(jContent, "footer", jContentFooter);
Handle jFields = json_array();
Handle jFieldAuthor = json_object();
json_object_set_new(jFieldAuthor, "name", json_string("Author"));
Format(sBuffer, sizeof sBuffer, "%s (%s)", sAuthor, sAuthorID);
json_object_set_new(jFieldAuthor, "value", json_string(sBuffer));
json_object_set_new(jFieldAuthor, "inline", json_boolean(true));
Handle jFieldTarget = json_object();
json_object_set_new(jFieldTarget, "name", json_string("Target"));
Format(sBuffer, sizeof sBuffer, "%s (%s)", sTarget, sTargetID);
json_object_set_new(jFieldTarget, "value", json_string(sBuffer));
json_object_set_new(jFieldTarget, "inline", json_boolean(true));
Handle jFieldReason = json_object();
json_object_set_new(jFieldReason, "name", json_string("Reason"));
json_object_set_new(jFieldReason, "value", json_string(sReason));
json_array_append_new(jFields, jFieldAuthor);
json_array_append_new(jFields, jFieldTarget);
if (iType == Ban || iType == Comms)
{
Handle jFieldDuration = json_object();
json_object_set_new(jFieldDuration, "name", json_string("Duration"));
if (iTime > 0)
Format(sBuffer, sizeof sBuffer, "%d Minutes", iTime);
else if (iTime < 0)
Format(sBuffer, sizeof sBuffer, "Session");
else
Format(sBuffer, sizeof sBuffer, "Permanent");
json_object_set_new(jFieldDuration, "value", json_string(sBuffer));
json_array_append_new(jFields, jFieldDuration);
}
if (iType == Comms)
{
Handle jFieldCommType = json_object();
json_object_set_new(jFieldCommType, "name", json_string("Comm Type"));
char cType[32];
GetCommType(cType, sizeof cType, extra);
json_object_set_new(jFieldCommType, "value", json_string(cType));
json_array_append_new(jFields, jFieldCommType);
}
json_array_append_new(jFields, jFieldReason);
json_object_set_new(jContent, "fields", jFields);
json_array_append_new(jEmbeds, jContent);
json_object_set_new(jRequest, "username", json_string(szUsername));
json_object_set_new(jRequest, "avatar_url", json_string(szProfilePictureURL));
json_object_set_new(jRequest, "embeds", jEmbeds);
json_dump(jRequest, sJson, sizeof sJson, 0, false, false, true);
#if defined DEBUG
PrintToServer(sJson);
#endif
CloseHandle(jRequest);
char sEndpoint[256];
GetEndpoint(sEndpoint, sizeof sEndpoint, iType);
Handle hRequest = SteamWorks_CreateHTTPRequest(k_EHTTPMethodPOST, sEndpoint);
SteamWorks_SetHTTPRequestContextValue(hRequest, iClient, iTarget);
SteamWorks_SetHTTPRequestGetOrPostParameter(hRequest, "payload_json", sJson);
SteamWorks_SetHTTPCallbacks(hRequest, OnHTTPRequestComplete);
if (!SteamWorks_SendHTTPRequest(hRequest))
LogError("HTTP request failed for %s against %s", sAuthor, sTarget);
}
public int OnHTTPRequestComplete(Handle hRequest, bool bFailure, bool bRequestSuccessful, EHTTPStatusCode eStatusCode, int iClient, int iTarget)
{
if (!bRequestSuccessful || eStatusCode != k_EHTTPStatusCode204NoContent)
{
LogError("HTTP request failed for %N against %N", iClient, iTarget);
#if defined DEBUG
int iSize;
SteamWorks_GetHTTPResponseBodySize(hRequest, iSize);
char[] sBody = new char[iSize];
SteamWorks_GetHTTPResponseBodyData(hRequest, sBody, iSize);
PrintToServer(sBody);
PrintToServer("Status Code: %d", eStatusCode);
PrintToServer("SteamWorks_IsLoaded: %d", SteamWorks_IsLoaded());
#endif
}
CloseHandle(hRequest);
}
public void OnConvarChanged(ConVar convar, const char[] oldValue, const char[] newValue)
{
if (convar == Convars[Ban])
Convars[Ban].GetString(sEndpoints[Ban], sizeof sEndpoints[]);
else if (convar == Convars[Report])
Convars[Report].GetString(sEndpoints[Report], sizeof sEndpoints[]);
else if (convar == Convars[Comms])
Convars[Comms].GetString(sEndpoints[Comms], sizeof sEndpoints[]);
}
int GetEmbedColor(int iType)
{
if (iType != Type_Unknown)
return EmbedColors[iType];
return EmbedColors[Ban];
}
void GetEndpoint(char[] sBuffer, int iBufferSize, int iType)
{
if (!StrEqual(sEndpoints[iType], ""))
{
strcopy(sBuffer, iBufferSize, sEndpoints[iType]);
return;
}
strcopy(sBuffer, iBufferSize, sEndpoints[Ban]);
}
void GetCommType(char[] sBuffer, int iBufferSize, int iType)
{
switch (iType)
{
case TYPE_MUTE:
strcopy(sBuffer, iBufferSize, "Mute");
case TYPE_GAG:
strcopy(sBuffer, iBufferSize, "Gag");
case TYPE_SILENCE:
strcopy(sBuffer, iBufferSize, "Silence");
}
}
stock bool IsValidClient(int iClient, bool bAlive = false)
{
if (iClient >= 1 &&
iClient <= MaxClients &&
IsClientConnected(iClient) &&
IsClientInGame(iClient) &&
!IsFakeClient(iClient) &&
(bAlive == false || IsPlayerAlive(iClient)))
{
return true;
}
return false;
}