From d12b882dfc70f4c514a6b26df86f70878c6a56c1 Mon Sep 17 00:00:00 2001 From: Lamp Date: Fri, 9 Nov 2018 13:10:20 -0800 Subject: [PATCH] Make escaping utils global and add to ddp bridge --- src/ddpbridge.js | 6 +++--- src/mppbridger/index.js | 20 ++------------------ src/util.js | 15 +++++++++++++++ 3 files changed, 20 insertions(+), 21 deletions(-) diff --git a/src/ddpbridge.js b/src/ddpbridge.js index 98ff777..a7dd97b 100644 --- a/src/ddpbridge.js +++ b/src/ddpbridge.js @@ -21,11 +21,11 @@ var wasConnected = false; if (chatmsg.type == "message") { //if (chatmsg.id != myId) if (!chatmsg.content.startsWith('\u034f')) - send2discord(`**${chatmsg.user.nick}:** ${chatmsg.content}`); + send2discord(`**${sanitizeName(chatmsg.user.nick)}:** ${escapeDiscordMentions(chatmsg.content)}`); } else if (chatmsg.type == "join") { - send2discord(`__***${chatmsg.nick || chatmsg.id} joined.***__`); + send2discord(`__***${sanitizeName(chatmsg.nick || chatmsg.id)} joined.***__`); } else if (chatmsg.type == "leave") { - send2discord(`__***${chatmsg.nick || chatmsg.id} left.***__`); + send2discord(`__***${sanitizeName(chatmsg.nick || chatmsg.id)} left.***__`); } } /*else if (transmission.type == 'load') { myId = transmission.id; diff --git a/src/mppbridger/index.js b/src/mppbridger/index.js index cd3b3f4..d1e7a01 100755 --- a/src/mppbridger/index.js +++ b/src/mppbridger/index.js @@ -130,9 +130,9 @@ global.createMPPbridge = function createMPPbridge(room, DiscordChannelID, site = gClient.on('a', async msg => { if (msg.p._id == gClient.getOwnParticipant()._id) return; var id = msg.p._id.substr(0,6); - var name = msg.p.name var name = sanitizeName(msg.p.name); - var str = `\`${id}\` **${name}:** ${msg.a.replace(/<@/g, "<\\@")}`; + var content = escapeDiscordMentions(msg.a); + var str = `\`${id}\` **${name}:** ${content}`; dSend(str); }); @@ -143,7 +143,6 @@ global.createMPPbridge = function createMPPbridge(room, DiscordChannelID, site = if (message.channel.id !== DiscordChannelID || message.author.bot || message.content.startsWith('!')) return; var str = message.cleanContent; var aname = `${message.member.displayName}#${message.member.user.discriminator}`; - var arr = []; if (str.startsWith('/') || str.startsWith('\\')) msgQueue.push(`โคน ${aname}`); else @@ -288,18 +287,3 @@ commands.chown = require('./commands/chown'); commands.list = require('./commands/list'); commands.ban = require('./commands/ban'); - - - - - - -/* util */ - -function sanitizeName(str){ // for showing mpp names in discord - str = str.replace(/[_~*\\]/g,"\\$&"); // formatting - str = str.replace(/<@/g, "<\\@"); // mentions - str = str.replace(/discord.gg\//g, 'discord.gg\\/'); // invites - //TODO escape channel mentions too? not necessary but for consistency - return str; -} diff --git a/src/util.js b/src/util.js index a59d538..437869c 100644 --- a/src/util.js +++ b/src/util.js @@ -1,3 +1,18 @@ + Array.prototype.random = function () { return this[Math.floor(Math.random() * this.length)] +} + +global.sanitizeName = function sanitizeName(str){ // for showing names in discord + str = str.replace(/[_~*\\]/g,"\\$&"); // formatting + str = escapeDiscordMentions(str); // mentions + str = str.replace(/discord.gg\//g, 'discord.gg\\/'); // invites + str = str.replace(/(http|https):\/\//g, "$1\\://"); // urls + return str; +} + +global.escapeDiscordMentions = function escapeDiscordMentions(str) { // escape discord mentions from a string to be sent to discord + str = str.replace(/<@/g, "<\\@"); // users & roles + // escaping channel mentions is not necessary + return str; } \ No newline at end of file