Add a sync command

This commit is contained in:
Travis Ralston 2019-09-27 20:02:03 -06:00
parent bd663f292d
commit 063bac77e5
3 changed files with 85 additions and 34 deletions

View File

@ -63,23 +63,39 @@ export class Mjolnir {
return this.client.start(); return this.client.start();
} }
public async syncLists() {
for (const list of this.banLists) {
await list.updateList();
}
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;
for (const list of this.banLists) {
if (list.roomId !== roomId) continue;
await list.updateList();
updated = true;
}
if (!updated) return;
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);
}
private async handleEvent(roomId: string, event: any) { 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 (!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'])) { if (ALL_RULE_TYPES.includes(event['type'])) {
let updated = false; await this.syncListForRoom(roomId);
for (const list of this.banLists) {
if (list.roomId !== roomId) continue;
await list.updateList();
updated = true;
}
if (!updated) return;
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);
} 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);

View File

@ -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,26 +28,37 @@ 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(' ');
if (parts.length === 1 || parts[1] === 'status') { try {
return execStatusCommand(roomId, event, mjolnir); if (parts.length === 1 || parts[1] === 'status') {
} else if (parts[1] === 'ban' && parts.length > 3) { return execStatusCommand(roomId, event, mjolnir);
return execBanCommand(roomId, event, mjolnir, parts); } else if (parts[1] === 'ban' && parts.length > 3) {
} else if (parts[1] === 'unban' && parts.length > 3) { return execBanCommand(roomId, event, mjolnir, parts);
return execUnbanCommand(roomId, event, mjolnir, parts); } else if (parts[1] === 'unban' && parts.length > 3) {
} else if (parts[1] === 'rules') { return execUnbanCommand(roomId, event, mjolnir, parts);
return execDumpRulesCommand(roomId, event, mjolnir); } else if (parts[1] === 'rules') {
} else { return execDumpRulesCommand(roomId, event, mjolnir);
// Help menu } else if (parts[1] === 'sync') {
const menu = "" + return execSyncCommand(roomId, event, mjolnir);
"!mjolnir - Print status information\n" + } else {
"!mjolnir status - Print status information\n" + // Help menu
"!mjolnir ban <user|room|server> <glob> [reason] - Adds an entity to the ban list\n" + const menu = "" +
"!mjolnir unban <user|room|server> <glob> - Removes an entity from the ban list\n" + "!mjolnir - Print status information\n" +
"!mjolnir rules - Lists the rules currently in use by Mjolnir\n" + "!mjolnir status - Print status information\n" +
"!mjolnir help - This menu\n"; "!mjolnir ban <user|room|server> <glob> [reason] - Adds an entity to the ban list\n" +
const html = `<b>Mjolnir help:</b><br><pre><code>${htmlEscape(menu)}</code></pre>`; "!mjolnir unban <user|room|server> <glob> - Removes an entity from the ban list\n" +
const text = `Mjolnir help:\n${menu}`; "!mjolnir rules - Lists the rules currently in use by Mjolnir\n" +
const reply = RichReply.createFor(roomId, event, text, html); "!mjolnir sync - Force updates of all lists and re-apply rules\n" +
"!mjolnir help - This menu\n";
const html = `<b>Mjolnir help:</b><br><pre><code>${htmlEscape(menu)}</code></pre>`;
const text = `Mjolnir help:\n${menu}`;
const reply = RichReply.createFor(roomId, event, text, html);
reply["msgtype"] = "m.notice";
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"; reply["msgtype"] = "m.notice";
return mjolnir.client.sendMessage(roomId, reply); return mjolnir.client.sendMessage(roomId, reply);
} }

View 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'], '✅');
}