Add a command to dump the rules the bot is using

This commit is contained in:
Travis Ralston 2019-09-27 15:57:39 -06:00
parent 39b59dbee1
commit fa1ad260f1
6 changed files with 72 additions and 3 deletions

View File

@ -19,6 +19,7 @@
},
"dependencies": {
"config": "^3.2.2",
"escape-html": "^1.0.3",
"js-yaml": "^3.13.1",
"matrix-bot-sdk": "^0.4.0-beta.5"
}

View File

@ -17,6 +17,7 @@ limitations under the License.
import { Mjolnir } from "../Mjolnir";
import { execStatusCommand } from "./StatusCommand";
import { execBanCommand, execUnbanCommand } from "./UnbanBanCommand";
import { execDumpRulesCommand } from "./DumpRulesCommand";
export const COMMAND_PREFIX = "!mjolnir";
@ -30,6 +31,8 @@ export function handleCommand(roomId: string, event: any, mjolnir: Mjolnir) {
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 {
// TODO: Help menu
}

View File

@ -0,0 +1,66 @@
/*
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";
import { RichReply } from "matrix-bot-sdk";
import * as htmlEscape from "escape-html";
// !mjolnir rules
export async function execDumpRulesCommand(roomId: string, event: any, mjolnir: Mjolnir) {
let html = "<b>Rules currently in use:</b><br/>";
let text = "Rules currently in use:\n";
let hasLists = false;
for (const list of mjolnir.banLists) {
hasLists = true;
let hasRules = false;
html += `<a href="${list.roomRef}">${list.roomId}</a>:<br/><ul>`;
text += `${list.roomRef}:\n`;
for (const rule of list.serverRules) {
hasRules = true;
html += `<li>server (<code>${rule.recommendation}</code>): <code>${htmlEscape(rule.entity)}</code> (${htmlEscape(rule.reason)})</li>`;
text += `* server (${rule.recommendation}): ${rule.entity} (${rule.reason})\n`;
}
for (const rule of list.userRules) {
hasRules = true;
html += `<li>user (<code>${rule.recommendation}</code>): <code>${htmlEscape(rule.entity)}</code> (${htmlEscape(rule.reason)})</li>`;
text += `* user (${rule.recommendation}): ${rule.entity} (${rule.reason})\n`;
}
for (const rule of list.roomRules) {
hasRules = true;
html += `<li>room (<code>${rule.recommendation}</code>): <code>${htmlEscape(rule.entity)}</code> (${htmlEscape(rule.reason)})</li>`;
text += `* room (${rule.recommendation}): ${rule.entity} (${rule.reason})\n`;
}
if (!hasRules) {
html += "<li><i>No rules</i>";
text += "* No rules\n";
}
}
if (!hasLists) {
html = "No ban lists configured";
text = "No ban lists configured";
}
const reply = RichReply.createFor(roomId, event, text, html);
reply["msgtype"] = "m.notice";
return mjolnir.client.sendMessage(roomId, reply);
}

View File

@ -33,7 +33,7 @@ export async function execStatusCommand(roomId: string, event: any, mjolnir: Mjo
for (const list of mjolnir.banLists) {
const ruleInfo = `rules: ${list.serverRules.length} servers, ${list.userRules.length} users, ${list.roomRules.length} rooms`;
html += `<li><a href="${list.roomRef}">${list.roomId}</a> (${ruleInfo})</li>`;
text += `${list.roomRef} (${ruleInfo})\n`;
text += `* ${list.roomRef} (${ruleInfo})\n`;
}
html += "</ul>";

View File

@ -62,7 +62,6 @@ export async function execBanCommand(roomId: string, event: any, mjolnir: Mjolni
await mjolnir.client.unstableApis.addReactionToEvent(roomId, event['event_id'], '✅');
}
// !mjolnir unban <user|server|room> <glob>
export async function execUnbanCommand(roomId: string, event: any, mjolnir: Mjolnir, parts: string[]) {
const bits = parseBits(parts);

View File

@ -327,7 +327,7 @@ entities@^2.0.0:
resolved "https://registry.yarnpkg.com/entities/-/entities-2.0.0.tgz#68d6084cab1b079767540d80e56a39b423e4abf4"
integrity sha512-D9f7V0JSRwIxlRI2mjMqufDrRDnx8p+eEOz7aUM9SuvF8gsBzra0/6tbjl1m8eQHrZlYj6PxqE00hZ1SAIKPLw==
escape-html@~1.0.3:
escape-html@^1.0.3, escape-html@~1.0.3:
version "1.0.3"
resolved "https://registry.yarnpkg.com/escape-html/-/escape-html-1.0.3.tgz#0258eae4d3d0c0974de1c169188ef0051d1d1988"
integrity sha1-Aljq5NPQwJdN4cFpGI7wBR0dGYg=