Make escaping utils global and add to ddp bridge

This commit is contained in:
Lamp 2018-11-09 13:10:20 -08:00
parent 7ece8e4416
commit d12b882dfc
No known key found for this signature in database
GPG Key ID: 0F1F8704BEDE369E
3 changed files with 20 additions and 21 deletions

View File

@ -21,11 +21,11 @@ var wasConnected = false;
if (chatmsg.type == "message") { if (chatmsg.type == "message") {
//if (chatmsg.id != myId) //if (chatmsg.id != myId)
if (!chatmsg.content.startsWith('\u034f')) if (!chatmsg.content.startsWith('\u034f'))
send2discord(`**${chatmsg.user.nick}:** ${chatmsg.content}`); send2discord(`**${sanitizeName(chatmsg.user.nick)}:** ${escapeDiscordMentions(chatmsg.content)}`);
} else if (chatmsg.type == "join") { } else if (chatmsg.type == "join") {
send2discord(`__***${chatmsg.nick || chatmsg.id} joined.***__`); send2discord(`__***${sanitizeName(chatmsg.nick || chatmsg.id)} joined.***__`);
} else if (chatmsg.type == "leave") { } else if (chatmsg.type == "leave") {
send2discord(`__***${chatmsg.nick || chatmsg.id} left.***__`); send2discord(`__***${sanitizeName(chatmsg.nick || chatmsg.id)} left.***__`);
} }
} /*else if (transmission.type == 'load') { } /*else if (transmission.type == 'load') {
myId = transmission.id; myId = transmission.id;

View File

@ -130,9 +130,9 @@ global.createMPPbridge = function createMPPbridge(room, DiscordChannelID, site =
gClient.on('a', async msg => { gClient.on('a', async msg => {
if (msg.p._id == gClient.getOwnParticipant()._id) return; if (msg.p._id == gClient.getOwnParticipant()._id) return;
var id = msg.p._id.substr(0,6); var id = msg.p._id.substr(0,6);
var name = msg.p.name
var name = sanitizeName(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); 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; if (message.channel.id !== DiscordChannelID || message.author.bot || message.content.startsWith('!')) return;
var str = message.cleanContent; var str = message.cleanContent;
var aname = `${message.member.displayName}#${message.member.user.discriminator}`; var aname = `${message.member.displayName}#${message.member.user.discriminator}`;
var arr = [];
if (str.startsWith('/') || str.startsWith('\\')) if (str.startsWith('/') || str.startsWith('\\'))
msgQueue.push(`${aname}`); msgQueue.push(`${aname}`);
else else
@ -288,18 +287,3 @@ commands.chown = require('./commands/chown');
commands.list = require('./commands/list'); commands.list = require('./commands/list');
commands.ban = require('./commands/ban'); 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;
}

View File

@ -1,3 +1,18 @@
Array.prototype.random = function () { Array.prototype.random = function () {
return this[Math.floor(Math.random() * this.length)] 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;
} }