Support multiline commands in PM

This is a huge hack... :[
This commit is contained in:
Guangcong Luo 2019-04-09 00:21:30 +08:00
parent df4d16cc20
commit f286746d9f
2 changed files with 21 additions and 3 deletions

View File

@ -106,7 +106,7 @@ class PatternTester {
/**
* @param {string} text
*/
test(text) {
testCommand(text) {
const spaceIndex = text.indexOf(' ');
if (this.fastElements.has(spaceIndex >= 0 ? text.slice(0, spaceIndex) : text)) {
return true;
@ -114,6 +114,23 @@ class PatternTester {
if (!this.regexp) return false;
return this.regexp.test(text);
}
/**
* @param {string} text
*/
test(text) {
if (!text.includes('\n')) return null;
if (this.testCommand(text)) return text;
// The PM matching is a huge mess, and really needs to be replaced with
// the new multiline command system soon.
const pmMatches = /^(\/(?:pm|w|whisper|msg) [^,]*, ?)(.*)/i.exec(text);
if (pmMatches && this.testCommand(pmMatches[2])) {
if (text.split('\n').every(line => line.startsWith(pmMatches[1]))) {
return text.replace(/\n\/(?:pm|w|whisper|msg) [^,]*, ?/g, '\n');
}
return text;
}
return null;
}
}
Chat.multiLinePattern = new PatternTester();

View File

@ -1653,8 +1653,9 @@ function socketReceive(worker, workerid, socketid, message) {
const room = Rooms(roomId);
if (!room) return;
if (Chat.multiLinePattern.test(message)) {
user.chat(message, room, connection);
const multilineMessage = Chat.multiLinePattern.test(message);
if (multilineMessage) {
user.chat(multilineMessage, room, connection);
return;
}