Commit a0a14e64 authored by Red Bot's avatar Red Bot

Merge branch 'discord-fixes' into 'master'

Discord fixes

See merge request !114
parents 4d84e202 968bd505
......@@ -27,6 +27,45 @@ std::string escape_json(const std::string &s) {
return o.str();
}
std::string unescape_json(const std::string &s) {
std::ostringstream o;
for (auto c = s.cbegin(); c != s.cend(); ++c) {
if (*c == '\\')
{
++c;
switch (*c)
{
case 't': { o << "\t"; break; }
case 'b': { o << "\b"; break; }
case 'f': { o << "\f"; break; }
case 'n': { o << "\n"; break; }
case 'r': { o << "\r"; break; }
case '\\': { o << "\\"; break; }
case '/': { o << "/"; break; }
case '"': { o << "\""; break; }
default:
;
}
}
else {
o << *c;
}
}
return o.str();
}
// Escapes special discord characters
std::string escape_discord(const std::string &s) {
std::ostringstream o;
for (auto c : s)
{
if (c == '`' || c == '*' || c == '_' || c == '~')
o << "\\";
o << c;
}
return o.str();
}
// Real simple client, all it does is invoke a callback when a message is received
class CallbackDiscordClient : public SleepyDiscord::DiscordClient {
public:
......@@ -51,7 +90,9 @@ void DiscordThread(std::string token, std::string channelId, std::string control
{
CallbackDiscordClient client(token, [&](SleepyDiscord::Message & msg) {
if (msg.author.ID == controlUserId && msg.channelID == channelId)
fromDiscord.enqueue(msg.content);
{
fromDiscord.enqueue(unescape_json(msg.content));
}
});
fromDiscord.enqueue(connectMsg);
......@@ -66,7 +107,7 @@ void DiscordThread(std::string token, std::string channelId, std::string control
std::string msg, combinedMsg;
while (combinedMsg.length() <= 1800 && toDiscord.tryDequeue(msg))
{
combinedMsg += msg + "\n";
combinedMsg += escape_discord(msg) + "\n";
}
if (combinedMsg != "")
......
#include "DiscordClient.h"
#include <nlohmann\json.hpp>
#include <fstream>
#include <regex>
using nlohmann::json;
......@@ -340,10 +341,16 @@ PLUGIN_API VOID OnPulse(VOID)
void EnqueueIfMatch(const PCHAR Line)
{
char buffer[MAX_STRING] = { 0 };
strcpy_s(buffer, Line);
// Remove any colours
StripMQChat(Line, buffer);
// Remove any item links
std::regex re("\x12[A-F0-9]{56}([^\x12]*)\x12");
strcpy_s(buffer, std::regex_replace(buffer, re, "$1").c_str());
if (bRun.load() && blechAllow.Feed(buffer) && !blechBlock.Feed(buffer))
toDiscord.enqueue(Line);
toDiscord.enqueue(buffer);
}
PLUGIN_API DWORD OnWriteChatColor(PCHAR Line, DWORD Color, DWORD Filter)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment