From 453784d7efad1d4c4c739f10c519eda3c6f27f3c Mon Sep 17 00:00:00 2001 From: Travis Ralston Date: Mon, 16 Aug 2021 20:32:00 -0600 Subject: [PATCH] Add a voice message protection to easily redact unwanted messages --- src/protections/MessageIsVoice.ts | 45 +++++++++++++++++++++++++++++++ src/protections/protections.ts | 9 +++++-- 2 files changed, 52 insertions(+), 2 deletions(-) create mode 100644 src/protections/MessageIsVoice.ts diff --git a/src/protections/MessageIsVoice.ts b/src/protections/MessageIsVoice.ts new file mode 100644 index 0000000..1daeecc --- /dev/null +++ b/src/protections/MessageIsVoice.ts @@ -0,0 +1,45 @@ +/* +Copyright 2021 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 { IProtection } from "./IProtection"; +import { Mjolnir } from "../Mjolnir"; +import { LogLevel, Permalinks, UserID } from "matrix-bot-sdk"; +import { logMessage } from "../LogProxy"; +import config from "../config"; + +export class MessageIsVoice implements IProtection { + + constructor() { + } + + public get name(): string { + return 'MessageIsVoiceProtection'; + } + + public async handleEvent(mjolnir: Mjolnir, roomId: string, event: any): Promise { + if (event['type'] === 'm.room.message' && event['content']) { + if (event['content']['msgtype'] !== 'm.audio') return; + if (event['content']['org.matrix.msc3245.voice'] === undefined) return; + await logMessage(LogLevel.INFO, "MessageIsVoice", `Redacting event from ${event['sender']} for posting a voice message. ${Permalinks.forEvent(roomId, event['event_id'], [new UserID(await mjolnir.client.getUserId()).domain])}`); + // Redact the event + if (!config.noop) { + await mjolnir.client.redactEvent(roomId, event['event_id'], "Voice messages are not permitted here"); + } else { + await logMessage(LogLevel.WARN, "FirstMessageIsImage", `Tried to redact ${event['event_id']} in ${roomId} but Mjolnir is running in no-op mode`, roomId); + } + } + } +} diff --git a/src/protections/protections.ts b/src/protections/protections.ts index ba81b5d..031c203 100644 --- a/src/protections/protections.ts +++ b/src/protections/protections.ts @@ -1,5 +1,5 @@ /* -Copyright 2019 The Matrix.org Foundation C.I.C. +Copyright 2019 - 2021 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. @@ -18,6 +18,7 @@ import { FirstMessageIsImage } from "./FirstMessageIsImage"; import { IProtection } from "./IProtection"; import { BasicFlooding, MAX_PER_MINUTE } from "./BasicFlooding"; import { WordList } from "./WordList"; +import { MessageIsVoice } from "./MessageIsVoice"; export const PROTECTIONS: PossibleProtections = { [new FirstMessageIsImage().name]: { @@ -34,7 +35,11 @@ export const PROTECTIONS: PossibleProtections = { description: "If a user posts a monitored word a set amount of time after joining, they " + "will be banned from that room. This will not publish the ban to a ban list.", factory: () => new WordList(), - } + }, + [new MessageIsVoice().name]: { + description: "If a user posts a voice message, that message will be redacted. No bans are issued.", + factory: () => new MessageIsVoice(), + }, }; export interface PossibleProtections {