diff --git a/src/commands/AddRemoveRoomFromDirectoryCommand.ts b/src/commands/AddRemoveRoomFromDirectoryCommand.ts new file mode 100644 index 0000000..fa81452 --- /dev/null +++ b/src/commands/AddRemoveRoomFromDirectoryCommand.ts @@ -0,0 +1,43 @@ +/* +Copyright 2020 The Matrix.org Foundation C.I.C. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +import { Mjolnir } from "../Mjolnir"; +import { RichReply } from "matrix-bot-sdk"; + +async function addRemoveFromDirectory(inRoomId: string, event: any, mjolnir: Mjolnir, roomRef: string, visibility: "public" | "private") { + const isAdmin = await mjolnir.isSynapseAdmin(); + if (!isAdmin) { + const message = "I am not a Synapse administrator, or the endpoint is blocked"; + const reply = RichReply.createFor(inRoomId, event, message, message); + reply['msgtype'] = "m.notice"; + return mjolnir.client.sendMessage(inRoomId, reply); + } + + const targetRoomId = await mjolnir.client.resolveRoom(roomRef); + await mjolnir.client.setDirectoryVisibility(targetRoomId, visibility); + + await mjolnir.client.unstableApis.addReactionToEvent(inRoomId, event['event_id'], '✅'); +} + +// !mjolnir directory add +export async function execAddRoomToDirectoryCommand(roomId: string, event: any, mjolnir: Mjolnir, parts: string[]) { + await addRemoveFromDirectory(roomId, event, mjolnir, parts[3], "public"); +} + +// !mjolnir directory remove +export async function execRemoveRoomFromDirectoryCommand(roomId: string, event: any, mjolnir: Mjolnir, parts: string[]) { + await addRemoveFromDirectory(roomId, event, mjolnir, parts[3], "private"); +} diff --git a/src/commands/CommandHandler.ts b/src/commands/CommandHandler.ts index 7b8316a..f1dd0c5 100644 --- a/src/commands/CommandHandler.ts +++ b/src/commands/CommandHandler.ts @@ -31,6 +31,8 @@ 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"; export const COMMAND_PREFIX = "!mjolnir"; @@ -77,6 +79,12 @@ export async function handleCommand(roomId: string, event: any, mjolnir: Mjolnir return await execRemoveProtectedRoom(roomId, event, mjolnir, parts); } else if (parts[1] === 'rooms' && parts.length === 2) { return await execListProtectedRooms(roomId, event, mjolnir); + } else if (parts[1] === 'move' && parts.length > 3) { + return await execMoveAliasCommand(roomId, event, mjolnir, parts); + } else if (parts[1] === 'directory' && parts.length > 3 && parts[2] === 'add') { + 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 { // Help menu const menu = "" + @@ -100,6 +108,9 @@ export async function handleCommand(roomId: string, event: any, mjolnir: Mjolnir "!mjolnir rooms - Lists all the protected rooms\n" + "!mjolnir rooms add - Adds a protected room (may cause high server load)\n" + "!mjolnir rooms remove - Removes a protected room\n" + + "!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 help - This menu\n"; const html = `Mjolnir help:
${htmlEscape(menu)}
`; const text = `Mjolnir help:\n${menu}`; diff --git a/src/commands/MoveAliasCommand.ts b/src/commands/MoveAliasCommand.ts new file mode 100644 index 0000000..3957e06 --- /dev/null +++ b/src/commands/MoveAliasCommand.ts @@ -0,0 +1,38 @@ +/* +Copyright 2020 The Matrix.org Foundation C.I.C. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +import { Mjolnir } from "../Mjolnir"; +import { RichReply } from "matrix-bot-sdk"; + +// !mjolnir move +export async function execMoveAliasCommand(roomId: string, event: any, mjolnir: Mjolnir, parts: string[]) { + const movingAlias = parts[2]; + const targetRoom = 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(movingAlias); + const newRoomId = await mjolnir.client.resolveRoom(targetRoom); + await mjolnir.client.createRoomAlias(movingAlias, newRoomId); + + await mjolnir.client.unstableApis.addReactionToEvent(roomId, event['event_id'], '✅'); +}