From 3dd4274b3b054904024b00c33793734e5085d3ea Mon Sep 17 00:00:00 2001 From: Travis Ralston Date: Thu, 20 Feb 2020 13:30:48 -0700 Subject: [PATCH] Add alias add/remove commands Fixes https://github.com/matrix-org/mjolnir/issues/42 --- .../{MoveAliasCommand.ts => AliasCommands.ts} | 36 +++++++++++++++++++ src/commands/CommandHandler.ts | 8 ++++- 2 files changed, 43 insertions(+), 1 deletion(-) rename src/commands/{MoveAliasCommand.ts => AliasCommands.ts} (50%) diff --git a/src/commands/MoveAliasCommand.ts b/src/commands/AliasCommands.ts similarity index 50% rename from src/commands/MoveAliasCommand.ts rename to src/commands/AliasCommands.ts index 3957e06..c209c1c 100644 --- a/src/commands/MoveAliasCommand.ts +++ b/src/commands/AliasCommands.ts @@ -36,3 +36,39 @@ export async function execMoveAliasCommand(roomId: string, event: any, mjolnir: await mjolnir.client.unstableApis.addReactionToEvent(roomId, event['event_id'], '✅'); } + +// !mjolnir alias add +export async function execAddAliasCommand(roomId: string, event: any, mjolnir: Mjolnir, parts: string[]) { + const aliasToAdd = parts[3]; + const targetRoom = parts[4]; + + const isAdmin = await mjolnir.isSynapseAdmin(); + if (!isAdmin) { + const message = "I am not a Synapse administrator, or the endpoint is blocked"; + const reply = RichReply.createFor(roomId, event, message, message); + reply['msgtype'] = "m.notice"; + return mjolnir.client.sendMessage(roomId, reply); + } + + const newRoomId = await mjolnir.client.resolveRoom(targetRoom); + await mjolnir.client.createRoomAlias(aliasToAdd, newRoomId); + + await mjolnir.client.unstableApis.addReactionToEvent(roomId, event['event_id'], '✅'); +} + +// !mjolnir alias remove +export async function execRemoveAliasCommand(roomId: string, event: any, mjolnir: Mjolnir, parts: string[]) { + const aliasToRemove = parts[3]; + + const isAdmin = await mjolnir.isSynapseAdmin(); + if (!isAdmin) { + const message = "I am not a Synapse administrator, or the endpoint is blocked"; + const reply = RichReply.createFor(roomId, event, message, message); + reply['msgtype'] = "m.notice"; + return mjolnir.client.sendMessage(roomId, reply); + } + + await mjolnir.client.deleteRoomAlias(aliasToRemove); + + await mjolnir.client.unstableApis.addReactionToEvent(roomId, event['event_id'], '✅'); +} diff --git a/src/commands/CommandHandler.ts b/src/commands/CommandHandler.ts index 1cd3b3c..cefe4d0 100644 --- a/src/commands/CommandHandler.ts +++ b/src/commands/CommandHandler.ts @@ -31,10 +31,10 @@ import { execDeactivateCommand } from "./DeactivateCommand"; import { execDisableProtection, execEnableProtection, execListProtections } from "./ProtectionsCommands"; import { execListProtectedRooms } from "./ListProtectedRoomsCommand"; import { execAddProtectedRoom, execRemoveProtectedRoom } from "./AddRemoveProtectedRoomsCommand"; -import { execMoveAliasCommand } from "./MoveAliasCommand"; import { execAddRoomToDirectoryCommand, execRemoveRoomFromDirectoryCommand } from "./AddRemoveRoomFromDirectoryCommand"; import { execSetPowerLevelCommand } from "./SetPowerLevelCommand"; import { execShutdownRoomCommand } from "./ShutdownRoomCommand"; +import { execAddAliasCommand, execMoveAliasCommand, execRemoveAliasCommand } from "./AliasCommands"; export const COMMAND_PREFIX = "!mjolnir"; @@ -87,6 +87,10 @@ export async function handleCommand(roomId: string, event: any, mjolnir: Mjolnir return await execAddRoomToDirectoryCommand(roomId, event, mjolnir, parts); } else if (parts[1] === 'directory' && parts.length > 3 && parts[2] === 'remove') { return await execRemoveRoomFromDirectoryCommand(roomId, event, mjolnir, parts); + } else if (parts[1] === 'alias' && parts.length > 4 && parts[2] === 'add') { + return await execAddAliasCommand(roomId, event, mjolnir, parts); + } else if (parts[1] === 'alias' && parts.length > 3 && parts[2] === 'remove') { + return await execRemoveAliasCommand(roomId, event, mjolnir, parts); } else if (parts[1] === 'powerlevel' && parts.length > 3) { return await execSetPowerLevelCommand(roomId, event, mjolnir, parts); } else if (parts[1] === 'shutdown' && parts[2] === 'room' && parts.length > 3) { @@ -118,6 +122,8 @@ export async function handleCommand(roomId: string, event: any, mjolnir: Mjolnir "!mjolnir move - Moves a to a new \n" + "!mjolnir directory add - Publishes a room in the server's room directory\n" + "!mjolnir directory remove - Removes a room from the server's room directory\n" + + "!mjolnir alias add - Adds to \n" + + "!mjolnir alias remove - Deletes the room alias from whatever room it is attached to\n" + "!mjolnir shutdown room - Uses the bot's account to shut down a room, preventing access to the room on this server\n" + "!mjolnir powerlevel [room alias/ID] - Sets the power level of the user in the specified room (or all protected rooms)\n" + "!mjolnir help - This menu\n";