Bot-side: Banning registration by ip, username, email

This commit is contained in:
David Teller 2021-04-15 14:52:09 +02:00
parent 47f996125c
commit fa418d0441
6 changed files with 51 additions and 22 deletions

View File

@ -15,7 +15,7 @@ limitations under the License.
*/
import { Mjolnir } from "../Mjolnir";
import BanList, { RULE_ROOM, RULE_SERVER, RULE_USER, USER_RULE_TYPES } from "../models/BanList";
import BanList, { RULE_REGISTRATION_EMAIL, RULE_REGISTRATION_IP, RULE_ROOM, RULE_SERVER, RULE_USER, USER_RULE_TYPES } from "../models/BanList";
import { LogLevel, LogService, MatrixGlob, RichReply } from "matrix-bot-sdk";
import { RECOMMENDATION_BAN, recommendationToStable } from "../models/ListRule";
import config from "../config";
@ -50,10 +50,20 @@ export async function parseArguments(roomId: string, event: any, mjolnir: Mjolni
while (argumentIndex < 7 && argumentIndex < parts.length) {
const arg = parts[argumentIndex++];
if (!arg) break;
if (["user", "room", "server"].includes(arg.toLowerCase())) {
if (arg.toLowerCase() === 'user') ruleType = RULE_USER;
if (arg.toLowerCase() === 'room') ruleType = RULE_ROOM;
if (arg.toLowerCase() === 'server') ruleType = RULE_SERVER;
if (["user", "room", "server", "registration_ip", "registration_email"].includes(arg.toLowerCase())) {
let lowercase = arg.toLowerCase();
for (let {value, type} of [
{value: "user", type: RULE_USER},
{value: "room", type: RULE_ROOM},
{value: "server", type: RULE_SERVER},
{value: "registration_ip", type: RULE_REGISTRATION_IP},
{value: "registration_email", type: RULE_REGISTRATION_EMAIL},
]) {
if (lowercase === value) {
ruleType = type;
break;
}
}
} else if (!entity && (arg[0] === '@' || arg[0] === '!' || arg[0] === '#' || arg.includes("*"))) {
entity = arg;
if (arg.startsWith("@") && !ruleType) ruleType = RULE_USER;
@ -115,7 +125,7 @@ export async function parseArguments(roomId: string, event: any, mjolnir: Mjolni
};
}
// !mjolnir ban <shortcode> <user|server|room> <glob> [reason] [--force]
// !mjolnir ban <shortcode> <user|server|room|email|ip> <glob> [reason] [--force]
export async function execBanCommand(roomId: string, event: any, mjolnir: Mjolnir, parts: string[]) {
const bits = await parseArguments(roomId, event, mjolnir, parts);
if (!bits) return; // error already handled
@ -132,7 +142,7 @@ export async function execBanCommand(roomId: string, event: any, mjolnir: Mjolni
await mjolnir.client.unstableApis.addReactionToEvent(roomId, event['event_id'], '✅');
}
// !mjolnir unban <shortcode> <user|server|room> <glob> [apply:t/f]
// !mjolnir unban <shortcode> <user|server|room|email|ip> <glob> [apply:t/f]
export async function execUnbanCommand(roomId: string, event: any, mjolnir: Mjolnir, parts: string[]) {
const bits = await parseArguments(roomId, event, mjolnir, parts);
if (!bits) return; // error already handled

View File

@ -20,18 +20,37 @@ import { ListRule } from "./ListRule";
export const RULE_USER = "m.room.rule.user";
export const RULE_ROOM = "m.room.rule.room";
export const RULE_SERVER = "m.room.rule.server";
export const RULE_REGISTRATION_EMAIL = "m.room.rule.registration.email";
export const RULE_REGISTRATION_IP = "m.room.rule.registration.ip";
export const USER_RULE_TYPES = [RULE_USER, "org.matrix.mjolnir.rule.user"];
export const ROOM_RULE_TYPES = [RULE_ROOM, "org.matrix.mjolnir.rule.room"];
export const SERVER_RULE_TYPES = [RULE_SERVER, "org.matrix.mjolnir.rule.server"];
export const ALL_RULE_TYPES = [...USER_RULE_TYPES, ...ROOM_RULE_TYPES, ...SERVER_RULE_TYPES];
export const REGISTRATION_EMAIL_RULE_TYPES = [RULE_REGISTRATION_EMAIL, "org.matrix.mjolnir.rule.registration.email"];
export const REGISTRATION_IP_RULE_TYPES = [RULE_REGISTRATION_IP, "org.matrix.mjolnir.rule.registration.ip"];
export const ALL_RULE_TYPES = [...USER_RULE_TYPES, ...ROOM_RULE_TYPES, ...SERVER_RULE_TYPES, ...REGISTRATION_EMAIL_RULE_TYPES, ...REGISTRATION_IP_RULE_TYPES];
const LABELED_RULE_TYPES = Object.freeze([
{types: USER_RULE_TYPES, key: RULE_USER},
{types: ROOM_RULE_TYPES, key: RULE_ROOM},
{types: SERVER_RULE_TYPES, key: RULE_SERVER},
{types: REGISTRATION_EMAIL_RULE_TYPES, key: RULE_REGISTRATION_EMAIL},
{types: REGISTRATION_IP_RULE_TYPES, key: RULE_REGISTRATION_IP},
]);
export const SHORTCODE_EVENT_TYPE = "org.matrix.mjolnir.shortcode";
export function ruleTypeToStable(rule: string, unstable = true): string {
if (USER_RULE_TYPES.includes(rule)) return unstable ? USER_RULE_TYPES[USER_RULE_TYPES.length - 1] : RULE_USER;
if (ROOM_RULE_TYPES.includes(rule)) return unstable ? ROOM_RULE_TYPES[ROOM_RULE_TYPES.length - 1] : RULE_ROOM;
if (SERVER_RULE_TYPES.includes(rule)) return unstable ? SERVER_RULE_TYPES[SERVER_RULE_TYPES.length - 1] : RULE_SERVER;
for (let {types, key} of LABELED_RULE_TYPES) {
if (types.includes(rule)) {
if (unstable) {
return types[types.length - 1];
} else {
return key;
}
}
}
return null;
}
@ -86,13 +105,13 @@ export default class BanList {
}
let kind: string = null;
if (USER_RULE_TYPES.includes(event['type'])) {
kind = RULE_USER;
} else if (ROOM_RULE_TYPES.includes(event['type'])) {
kind = RULE_ROOM;
} else if (SERVER_RULE_TYPES.includes(event['type'])) {
kind = RULE_SERVER;
} else {
for (let {types, key} of LABELED_RULE_TYPES) {
if (types.includes(event['type'])) {
kind = key;
break;
}
}
if (kind == null) {
continue; // invalid/unknown
}

View File

@ -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.

View File

@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
# 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.

View File

@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
# 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.

View File

@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
# 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.