Skip to content

Commit

Permalink
Merge pull request #2 from BalaM314/patch-1
Browse files Browse the repository at this point in the history
Sanitize incoming messages
  • Loading branch information
Brandons404 authored Nov 4, 2022
2 parents 3f24d66 + 1b8618b commit d9a6269
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions scripts/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,20 @@ const sendMessage = (msg) => {
}
};

const cleanMessage = message => {
const lastCharCode = message.codePointAt(message.length - 1);
const secondLastCharCode = message.codePointAt(message.length - 2);

if (lastCharCode >= 0xf80 && lastCharCode <= 0x107f && secondLastCharCode >= 0xf80 && secondLastCharCode <= 0x107f) {
//If the last two characters are both in the range U+0F80 to U+0x107F, then they were generated by foo's client and should not be displayed
message = message.slice(0, -2);//Remove them
}

//If the message contains any of the characters \, <, @, or >, escape the character and put a zero width space after it to avoid pings
//also remove newlines
return message.replace(/([\\<@>])/g, "\\$1\u200B").replace(/[\r\n]/g, "");
}

Colors.put("accent", Color.white);
Colors.put("unlaunched", Color.white);
Colors.put("stat", Color.white);
Expand Down Expand Up @@ -64,13 +78,10 @@ Events.on(PlayerChatEvent, (e) => {
if (text[0] === '/') return;

const formattedName = Strings.stripColors(player.name);
const lastChar1 = text[text.length - 1];

if (lastChar1 >= 0xf80 && lastChar1 <= 0x107f) {
text = text.slice(0, -2);
}

const cleanedMessage = cleanMessage(text);

const msg = '**' + formattedName + '**' + ': ' + text;
const msg = '**' + formattedName + '**' + ': ' + cleanedMessage;

sendMessage(msg);
});
Expand Down

0 comments on commit d9a6269

Please sign in to comment.