mirror of
https://github.com/matrix-org/mjolnir.git
synced 2024-10-01 05:36:06 +00:00
Add a sync command
This commit is contained in:
parent
bd663f292d
commit
063bac77e5
@ -63,10 +63,19 @@ export class Mjolnir {
|
|||||||
return this.client.start();
|
return this.client.start();
|
||||||
}
|
}
|
||||||
|
|
||||||
private async handleEvent(roomId: string, event: any) {
|
public async syncLists() {
|
||||||
if (!event['state_key']) return; // we also don't do anything with state events that have no state key
|
for (const list of this.banLists) {
|
||||||
|
await list.updateList();
|
||||||
|
}
|
||||||
|
|
||||||
if (ALL_RULE_TYPES.includes(event['type'])) {
|
let errors = await applyServerAcls(this.banLists, Object.keys(this.protectedRooms), this.client);
|
||||||
|
await this.printActionResult(errors);
|
||||||
|
|
||||||
|
errors = await applyUserBans(this.banLists, Object.keys(this.protectedRooms), this.client);
|
||||||
|
await this.printActionResult(errors);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async syncListForRoom(roomId: string) {
|
||||||
let updated = false;
|
let updated = false;
|
||||||
for (const list of this.banLists) {
|
for (const list of this.banLists) {
|
||||||
if (list.roomId !== roomId) continue;
|
if (list.roomId !== roomId) continue;
|
||||||
@ -80,6 +89,13 @@ export class Mjolnir {
|
|||||||
|
|
||||||
errors = await applyUserBans(this.banLists, Object.keys(this.protectedRooms), this.client);
|
errors = await applyUserBans(this.banLists, Object.keys(this.protectedRooms), this.client);
|
||||||
await this.printActionResult(errors);
|
await this.printActionResult(errors);
|
||||||
|
}
|
||||||
|
|
||||||
|
private async handleEvent(roomId: string, event: any) {
|
||||||
|
if (!event['state_key']) return; // we also don't do anything with state events that have no state key
|
||||||
|
|
||||||
|
if (ALL_RULE_TYPES.includes(event['type'])) {
|
||||||
|
await this.syncListForRoom(roomId);
|
||||||
} else if (event['type'] === "m.room.member") {
|
} else if (event['type'] === "m.room.member") {
|
||||||
const errors = await applyUserBans(this.banLists, Object.keys(this.protectedRooms), this.client);
|
const errors = await applyUserBans(this.banLists, Object.keys(this.protectedRooms), this.client);
|
||||||
await this.printActionResult(errors);
|
await this.printActionResult(errors);
|
||||||
|
@ -18,8 +18,9 @@ import { Mjolnir } from "../Mjolnir";
|
|||||||
import { execStatusCommand } from "./StatusCommand";
|
import { execStatusCommand } from "./StatusCommand";
|
||||||
import { execBanCommand, execUnbanCommand } from "./UnbanBanCommand";
|
import { execBanCommand, execUnbanCommand } from "./UnbanBanCommand";
|
||||||
import { execDumpRulesCommand } from "./DumpRulesCommand";
|
import { execDumpRulesCommand } from "./DumpRulesCommand";
|
||||||
import { RichReply } from "matrix-bot-sdk";
|
import { LogService, RichReply } from "matrix-bot-sdk";
|
||||||
import * as htmlEscape from "escape-html";
|
import * as htmlEscape from "escape-html";
|
||||||
|
import { execSyncCommand } from "./SyncCommand";
|
||||||
|
|
||||||
export const COMMAND_PREFIX = "!mjolnir";
|
export const COMMAND_PREFIX = "!mjolnir";
|
||||||
|
|
||||||
@ -27,6 +28,7 @@ export function handleCommand(roomId: string, event: any, mjolnir: Mjolnir) {
|
|||||||
const cmd = event['content']['body'];
|
const cmd = event['content']['body'];
|
||||||
const parts = cmd.trim().split(' ');
|
const parts = cmd.trim().split(' ');
|
||||||
|
|
||||||
|
try {
|
||||||
if (parts.length === 1 || parts[1] === 'status') {
|
if (parts.length === 1 || parts[1] === 'status') {
|
||||||
return execStatusCommand(roomId, event, mjolnir);
|
return execStatusCommand(roomId, event, mjolnir);
|
||||||
} else if (parts[1] === 'ban' && parts.length > 3) {
|
} else if (parts[1] === 'ban' && parts.length > 3) {
|
||||||
@ -35,6 +37,8 @@ export function handleCommand(roomId: string, event: any, mjolnir: Mjolnir) {
|
|||||||
return execUnbanCommand(roomId, event, mjolnir, parts);
|
return execUnbanCommand(roomId, event, mjolnir, parts);
|
||||||
} else if (parts[1] === 'rules') {
|
} else if (parts[1] === 'rules') {
|
||||||
return execDumpRulesCommand(roomId, event, mjolnir);
|
return execDumpRulesCommand(roomId, event, mjolnir);
|
||||||
|
} else if (parts[1] === 'sync') {
|
||||||
|
return execSyncCommand(roomId, event, mjolnir);
|
||||||
} else {
|
} else {
|
||||||
// Help menu
|
// Help menu
|
||||||
const menu = "" +
|
const menu = "" +
|
||||||
@ -43,6 +47,7 @@ export function handleCommand(roomId: string, event: any, mjolnir: Mjolnir) {
|
|||||||
"!mjolnir ban <user|room|server> <glob> [reason] - Adds an entity to the ban list\n" +
|
"!mjolnir ban <user|room|server> <glob> [reason] - Adds an entity to the ban list\n" +
|
||||||
"!mjolnir unban <user|room|server> <glob> - Removes an entity from the ban list\n" +
|
"!mjolnir unban <user|room|server> <glob> - Removes an entity from the ban list\n" +
|
||||||
"!mjolnir rules - Lists the rules currently in use by Mjolnir\n" +
|
"!mjolnir rules - Lists the rules currently in use by Mjolnir\n" +
|
||||||
|
"!mjolnir sync - Force updates of all lists and re-apply rules\n" +
|
||||||
"!mjolnir help - This menu\n";
|
"!mjolnir help - This menu\n";
|
||||||
const html = `<b>Mjolnir help:</b><br><pre><code>${htmlEscape(menu)}</code></pre>`;
|
const html = `<b>Mjolnir help:</b><br><pre><code>${htmlEscape(menu)}</code></pre>`;
|
||||||
const text = `Mjolnir help:\n${menu}`;
|
const text = `Mjolnir help:\n${menu}`;
|
||||||
@ -50,4 +55,11 @@ export function handleCommand(roomId: string, event: any, mjolnir: Mjolnir) {
|
|||||||
reply["msgtype"] = "m.notice";
|
reply["msgtype"] = "m.notice";
|
||||||
return mjolnir.client.sendMessage(roomId, reply);
|
return mjolnir.client.sendMessage(roomId, reply);
|
||||||
}
|
}
|
||||||
|
} catch (e) {
|
||||||
|
LogService.error("CommandHandler", e);
|
||||||
|
const text = "There was an error processing your command - see console/log for details";
|
||||||
|
const reply = RichReply.createFor(roomId, event, text, text);
|
||||||
|
reply["msgtype"] = "m.notice";
|
||||||
|
return mjolnir.client.sendMessage(roomId, reply);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
23
src/commands/SyncCommand.ts
Normal file
23
src/commands/SyncCommand.ts
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
/*
|
||||||
|
Copyright 2019 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";
|
||||||
|
|
||||||
|
// !mjolnir sync
|
||||||
|
export async function execSyncCommand(roomId: string, event: any, mjolnir: Mjolnir) {
|
||||||
|
await mjolnir.syncLists();
|
||||||
|
return mjolnir.client.unstableApis.addReactionToEvent(roomId, event['event_id'], '✅');
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user