mirror of
https://github.com/matrix-org/mjolnir.git
synced 2024-07-01 00:51:36 +00:00
Add a sync command
This commit is contained in:
parent
bd663f292d
commit
063bac77e5
|
@ -63,23 +63,39 @@ export class Mjolnir {
|
|||
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) {
|
||||
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'])) {
|
||||
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);
|
||||
await this.syncListForRoom(roomId);
|
||||
} else if (event['type'] === "m.room.member") {
|
||||
const errors = await applyUserBans(this.banLists, Object.keys(this.protectedRooms), this.client);
|
||||
await this.printActionResult(errors);
|
||||
|
|
|
@ -18,8 +18,9 @@ import { Mjolnir } from "../Mjolnir";
|
|||
import { execStatusCommand } from "./StatusCommand";
|
||||
import { execBanCommand, execUnbanCommand } from "./UnbanBanCommand";
|
||||
import { execDumpRulesCommand } from "./DumpRulesCommand";
|
||||
import { RichReply } from "matrix-bot-sdk";
|
||||
import { LogService, RichReply } from "matrix-bot-sdk";
|
||||
import * as htmlEscape from "escape-html";
|
||||
import { execSyncCommand } from "./SyncCommand";
|
||||
|
||||
export const COMMAND_PREFIX = "!mjolnir";
|
||||
|
||||
|
@ -27,26 +28,37 @@ export function handleCommand(roomId: string, event: any, mjolnir: Mjolnir) {
|
|||
const cmd = event['content']['body'];
|
||||
const parts = cmd.trim().split(' ');
|
||||
|
||||
if (parts.length === 1 || parts[1] === 'status') {
|
||||
return execStatusCommand(roomId, event, mjolnir);
|
||||
} else if (parts[1] === 'ban' && parts.length > 3) {
|
||||
return execBanCommand(roomId, event, mjolnir, parts);
|
||||
} else if (parts[1] === 'unban' && parts.length > 3) {
|
||||
return execUnbanCommand(roomId, event, mjolnir, parts);
|
||||
} else if (parts[1] === 'rules') {
|
||||
return execDumpRulesCommand(roomId, event, mjolnir);
|
||||
} else {
|
||||
// Help menu
|
||||
const menu = "" +
|
||||
"!mjolnir - Print status information\n" +
|
||||
"!mjolnir status - Print status information\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 rules - Lists the rules currently in use by Mjolnir\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);
|
||||
try {
|
||||
if (parts.length === 1 || parts[1] === 'status') {
|
||||
return execStatusCommand(roomId, event, mjolnir);
|
||||
} else if (parts[1] === 'ban' && parts.length > 3) {
|
||||
return execBanCommand(roomId, event, mjolnir, parts);
|
||||
} else if (parts[1] === 'unban' && parts.length > 3) {
|
||||
return execUnbanCommand(roomId, event, mjolnir, parts);
|
||||
} else if (parts[1] === 'rules') {
|
||||
return execDumpRulesCommand(roomId, event, mjolnir);
|
||||
} else if (parts[1] === 'sync') {
|
||||
return execSyncCommand(roomId, event, mjolnir);
|
||||
} else {
|
||||
// Help menu
|
||||
const menu = "" +
|
||||
"!mjolnir - Print status information\n" +
|
||||
"!mjolnir status - Print status information\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 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";
|
||||
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";
|
||||
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