diff --git a/src/matrixroomhandler.ts b/src/matrixroomhandler.ts index 5c8f9d1f78b95b1849903d839626e0bda9d74335..2c04db5216054d99d05bbf72dd3008b32377d998 100644 --- a/src/matrixroomhandler.ts +++ b/src/matrixroomhandler.ts @@ -93,21 +93,44 @@ export class MatrixRoomHandler { } else if (event.type === "m.room.redaction" && context.rooms.remote) { return this.discord.ProcessMatrixRedact(event); } else if (event.type === "m.room.message") { - log.verbose("MatrixRoomHandler", "Got m.room.message event"); - if (event.content.body && event.content.body.startsWith("!discord")) { - return this.ProcessCommand(event, context); - } else if (context.rooms.remote) { - const srvChanPair = context.rooms.remote.roomId.substr("_discord".length).split("_", ROOM_NAME_PARTS); - return this.discord.ProcessMatrixMsgEvent(event, srvChanPair[0], srvChanPair[1]).catch((err) => { - log.warn("MatrixRoomHandler", "There was an error sending a matrix event", err); + log.verbose("MatrixRoomHandler", "Got m.room.message event"); + if (event.content.body && event.content.body.startsWith("!discord")) { + return this.ProcessCommand(event, context); + } else if (context.rooms.remote) { + const srvChanPair = context.rooms.remote.roomId.substr("_discord".length).split("_", ROOM_NAME_PARTS); + return this.discord.ProcessMatrixMsgEvent(event, srvChanPair[0], srvChanPair[1]).catch((err) => { + log.warn("MatrixRoomHandler", "There was an error sending a matrix event", err); + }); + } + } else if (event.type === "m.room.encryption" && context.rooms.remote) { + return this.HandleEncryptionWarning(event.room_id).catch((err) => { + return Promise.reject(`Failed to handle encrypted room, ${err}`); }); - } } else { log.verbose("MatrixRoomHandler", "Got non m.room.message event"); } return Promise.reject("Event not processed by bridge"); } + public async HandleEncryptionWarning(roomId: string): Promise<void> { + const intent = this.bridge.getIntent(); + log.info("MatrixRoomHandler", `User has turned on encryption in ${roomId}, so leaving.`); + /* N.B 'status' is not specced but https://github.com/matrix-org/matrix-doc/pull/828 + has been open for over a year with no resolution. */ + const sendPromise = intent.sendMessage(roomId, { + msgtype: "m.notice", + status: "critical", + body: "You have turned on encryption in this room, so the service will not bridge any new messages.", + }); + const channel = await this.discord.GetChannelFromRoomId(roomId); + await (channel as Discord.TextChannel).send( + "Someone on Matrix has turned on encryption in this room, so the service will not bridge any new messages", + ); + await sendPromise; + await intent.leave(roomId); + await this.bridge.getRoomStore().removeEntriesByMatrixRoomId(roomId); + } + public HandleInvite(event: any) { log.info("MatrixRoomHandler", "Received invite for " + event.state_key + " in room " + event.room_id); if (event.state_key === this.botUserId) { diff --git a/test/mocks/channel.ts b/test/mocks/channel.ts index d9794702e3d4fe8177ee3ae628037e3d47588440..3af74eaf26a66f4899bd96de4f7fe8513055ce6c 100644 --- a/test/mocks/channel.ts +++ b/test/mocks/channel.ts @@ -5,4 +5,7 @@ import {MockCollection} from "./collection"; export class MockChannel { public members = new MockCollection<string, MockMember>(); constructor (public id: string = "", public guild: any = null) { } + public send(data: any): Promise<any> { + return Promise.resolve(data); + } } diff --git a/test/test_matrixroomhandler.ts b/test/test_matrixroomhandler.ts index 36a0e7001b7aabc78fb6f9f2d736cc2502358783..fb8d5ccb38e0485878f032dede65e25db1261421 100644 --- a/test/test_matrixroomhandler.ts +++ b/test/test_matrixroomhandler.ts @@ -102,11 +102,20 @@ function createRH(opts: any = {}) { }; const handler = new MatrixRoomHandler(bot as any, config, "@botuser:localhost", provisioner as any); handler.setBridge({ - getIntent: () => { return { - sendMessage: (roomId, content) => Promise.resolve(content), - getClient: () => mxClient, - }; }, - }); + getIntent: () => { + return { + sendMessage: (roomId, content) => Promise.resolve(content), + leave: (roomId) => Promise.resolve(roomId), + getClient: () => mxClient, + }; + }, + getRoomStore: () => { + return { + removeEntriesByMatrixRoomId: (roomId) => Promise.resolve(roomId), + }; + }, + }, + ); return handler; } @@ -195,6 +204,18 @@ describe("MatrixRoomHandler", () => { return expect(handler.OnEvent(buildRequest({ type: "m.room.message", content: {body: "abc"}}), context)).to.eventually.equal("processed"); }); + it("should alert if encryption is turned on", () => { + const handler = createRH(); + const context = { + rooms: { + remote: { + roomId: "_discord_123_456", + }, + }, + }; + return expect(handler.OnEvent(buildRequest({ + type: "m.room.encryption", room_id: "!accept:localhost"}), context)).to.eventually.be.fulfilled; + }); it("should process !discord commands", () => { const handler = createRH(); handler.ProcessCommand = (ev) => Promise.resolve("processedcmd");