Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor command-line handling #335

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"start:dev": "yarn build && node --async-stack-traces lib/index.js",
"test": "ts-mocha --project ./tsconfig.json test/commands/**/*.ts",
"test:integration": "NODE_ENV=harness ts-mocha --async-stack-traces --require test/integration/fixtures.ts --timeout 300000 --project ./tsconfig.json \"test/integration/**/*Test.ts\"",
"test:YORIC": "NODE_ENV=harness ts-mocha --async-stack-traces --require test/integration/fixtures.ts --timeout 300000 --project ./tsconfig.json \"test/integration/roomMembersTest.ts\"",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oopsie

"test:manual": "NODE_ENV=harness ts-node test/integration/manualLaunchScript.ts",
"version": "sed -i '/# version automated/s/[0-9][0-9]*\\.[0-9][0-9]*\\.[0-9][0-9]*/'$npm_package_version'/' synapse_antispam/setup.py && git add synapse_antispam/setup.py && cat synapse_antispam/setup.py"
},
Expand Down Expand Up @@ -45,7 +46,7 @@
"jsdom": "^16.6.0",
"matrix-bot-sdk": "^0.5.19",
"parse-duration": "^1.0.2",
"shell-quote": "^1.7.3"
"tokenizr": "^1.6.7"
},
"engines": {
"node": ">=16.0.0"
Expand Down
53 changes: 17 additions & 36 deletions src/Mjolnir.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import {
import BanList, { ALL_RULE_TYPES as ALL_BAN_LIST_RULE_TYPES, ListRuleChange, RULE_ROOM, RULE_SERVER, RULE_USER } from "./models/BanList";
import { applyServerAcls } from "./actions/ApplyAcl";
import { RoomUpdateError } from "./models/RoomUpdateError";
import { COMMAND_PREFIX, handleCommand } from "./commands/CommandHandler";
import { applyUserBans } from "./actions/ApplyBan";
import config from "./config";
import ErrorCache, { ERROR_KIND_FATAL, ERROR_KIND_PERMISSION } from "./ErrorCache";
Expand All @@ -50,6 +49,7 @@ import RuleServer from "./models/RuleServer";
import { RoomMemberManager } from "./RoomMembers";
import { ProtectedRoomActivityTracker } from "./queues/ProtectedRoomActivityTracker";
import { ThrottlingQueue } from "./queues/ThrottlingQueue";
import { CommandManager } from "./commands/Command";

const levelToFn = {
[LogLevel.DEBUG.toString()]: LogService.debug,
Expand Down Expand Up @@ -79,6 +79,7 @@ export class Mjolnir {
private localpart: string;
private currentState: string = STATE_NOT_STARTED;
public readonly roomJoins: RoomMemberManager;
public readonly commandManager: CommandManager;
public protections = new Map<string /* protection name */, Protection>();
/**
* This is for users who are not listed on a watchlist,
Expand Down Expand Up @@ -202,45 +203,12 @@ export class Mjolnir {
this.automaticRedactionReasons.push(new MatrixGlob(reason.toLowerCase()));
}

this.commandManager = new CommandManager(managementRoomId, this);

// Setup bot.

client.on("room.event", this.handleEvent.bind(this));

client.on("room.message", async (roomId, event) => {
if (roomId !== this.managementRoomId) return;
if (!event['content']) return;

const content = event['content'];
if (content['msgtype'] === "m.text" && content['body']) {
const prefixes = [
COMMAND_PREFIX,
this.localpart + ":",
this.displayName + ":",
await client.getUserId() + ":",
this.localpart + " ",
this.displayName + " ",
await client.getUserId() + " ",
...config.commands.additionalPrefixes.map(p => `!${p}`),
...config.commands.additionalPrefixes.map(p => `${p}:`),
...config.commands.additionalPrefixes.map(p => `${p} `),
...config.commands.additionalPrefixes,
];
if (config.commands.allowNoPrefix) prefixes.push("!");

const prefixUsed = prefixes.find(p => content['body'].toLowerCase().startsWith(p.toLowerCase()));
if (!prefixUsed) return;

// rewrite the event body to make the prefix uniform (in case the bot has spaces in its display name)
let restOfBody = content['body'].substring(prefixUsed.length);
if (!restOfBody.startsWith(" ")) restOfBody = ` ${restOfBody}`;
event['content']['body'] = COMMAND_PREFIX + restOfBody;
LogService.info("Mjolnir", `Command being run by ${event['sender']}: ${event['content']['body']}`);

await client.sendReadReceipt(roomId, event['event_id']);
return handleCommand(roomId, event, this);
}
});

client.on("room.join", (roomId: string, event: any) => {
LogService.info("Mjolnir", `Joined ${roomId}`);
return this.resyncJoinedRooms();
Expand All @@ -257,6 +225,19 @@ export class Mjolnir {
if (profile['displayname']) {
this.displayName = profile['displayname'];
}
}).then(() => {
const prefixes = [
"mjolnir",
this.localpart,
];
if (this.displayName) {
prefixes.push(this.displayName);
}
prefixes.push(...config.commands.additionalPrefixes);
if (config.commands.allowNoPrefix) {
prefixes.push("!");
}
this.commandManager.init(prefixes);
});

// Setup room activity watcher
Expand Down
Loading