From 01d3f2f119599d537209a360c5b60e3fd73c1dd4 Mon Sep 17 00:00:00 2001 From: Matthew Hodgson Date: Wed, 16 Sep 2015 01:09:32 +0100 Subject: [PATCH] implement /part, /j, and error rather than pass-through unrecognised commands --- src/SlashCommands.js | 61 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/src/SlashCommands.js b/src/SlashCommands.js index 907aa26d6b..b47b953a12 100644 --- a/src/SlashCommands.js +++ b/src/SlashCommands.js @@ -91,6 +91,14 @@ var commands = { var matches = args.match(/^(\S+)$/); if (matches) { var room_alias = matches[1]; + if (room_alias[0] !== '#') { + return reject("Usage: /join #alias:domain"); + } + if (!room_alias.match(/:/)) { + var domain = MatrixClientPeg.get().credentials.userId.replace(/^.*:/, ''); + room_alias += ':' + domain; + } + // Try to find a room with this alias var rooms = MatrixClientPeg.get().getRooms(); var roomId; @@ -134,6 +142,52 @@ var commands = { return reject("Usage: /join "); }, + part: function(room_id, args) { + var targetRoomId; + if (args) { + var matches = args.match(/^(\S+)$/); + if (matches) { + var room_alias = matches[1]; + if (room_alias[0] !== '#') { + return reject("Usage: /part [#alias:domain]"); + } + if (!room_alias.match(/:/)) { + var domain = MatrixClientPeg.get().credentials.userId.replace(/^.*:/, ''); + room_alias += ':' + domain; + } + + // Try to find a room with this alias + var rooms = MatrixClientPeg.get().getRooms(); + for (var i = 0; i < rooms.length; i++) { + var aliasEvents = rooms[i].currentState.getStateEvents( + "m.room.aliases" + ); + for (var j = 0; j < aliasEvents.length; j++) { + var aliases = aliasEvents[j].getContent().aliases || []; + for (var k = 0; k < aliases.length; k++) { + if (aliases[k] === room_alias) { + targetRoomId = rooms[i].roomId; + break; + } + } + if (targetRoomId) { break; } + } + if (targetRoomId) { break; } + } + } + if (!targetRoomId) { + return reject("Unrecognised room alias: " + room_alias); + } + } + if (!targetRoomId) targetRoomId = room_id; + return success( + MatrixClientPeg.get().leave(targetRoomId).then( + function() { + dis.dispatch({action: 'view_next_room'}); + }) + ); + }, + // Kick a user from the room with an optional reason kick: function(room_id, args) { if (args) { @@ -227,6 +281,9 @@ var commands = { } }; +// helpful aliases +commands.j = commands.join; + module.exports = { /** * Process the given text for /commands and perform them. @@ -244,9 +301,13 @@ module.exports = { var bits = input.match(/^(\S+?)( +(.*))?$/); var cmd = bits[1].substring(1).toLowerCase(); var args = bits[3]; + if (cmd === "me") return null; if (commands[cmd]) { return commands[cmd](roomId, args); } + else { + return reject("Unrecognised command: " + input); + } } return null; // not a command }