2019-09-27 17:15:10 -04:00
|
|
|
/*
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2019-10-04 23:22:34 -04:00
|
|
|
import { LogService, MatrixClient } from "matrix-bot-sdk";
|
2019-09-27 17:15:10 -04:00
|
|
|
import BanList, { ALL_RULE_TYPES } from "./models/BanList";
|
|
|
|
import { applyServerAcls } from "./actions/ApplyAcl";
|
|
|
|
import { RoomUpdateError } from "./models/RoomUpdateError";
|
|
|
|
import { COMMAND_PREFIX, handleCommand } from "./commands/CommandHandler";
|
2019-09-27 21:54:13 -04:00
|
|
|
import { applyUserBans } from "./actions/ApplyBan";
|
2019-10-04 23:02:37 -04:00
|
|
|
import config from "./config";
|
2019-09-27 17:15:10 -04:00
|
|
|
|
2019-10-04 23:22:18 -04:00
|
|
|
export const STATE_NOT_STARTED = "not_started";
|
|
|
|
export const STATE_CHECKING_PERMISSIONS = "checking_permissions";
|
|
|
|
export const STATE_SYNCING = "syncing";
|
|
|
|
export const STATE_RUNNING = "running";
|
|
|
|
|
2019-09-27 17:15:10 -04:00
|
|
|
export class Mjolnir {
|
2019-09-27 18:04:08 -04:00
|
|
|
|
|
|
|
private displayName: string;
|
|
|
|
private localpart: string;
|
2019-10-04 23:22:18 -04:00
|
|
|
private currentState: string = STATE_NOT_STARTED;
|
2019-09-27 18:04:08 -04:00
|
|
|
|
2019-09-27 17:15:10 -04:00
|
|
|
constructor(
|
|
|
|
public readonly client: MatrixClient,
|
2019-09-27 17:44:28 -04:00
|
|
|
public readonly managementRoomId: string,
|
2019-09-27 17:15:10 -04:00
|
|
|
public readonly protectedRooms: { [roomId: string]: string },
|
|
|
|
public readonly banLists: BanList[],
|
|
|
|
) {
|
|
|
|
client.on("room.event", this.handleEvent.bind(this));
|
|
|
|
|
|
|
|
client.on("room.message", async (roomId, event) => {
|
|
|
|
if (roomId !== managementRoomId) return;
|
|
|
|
if (!event['content']) return;
|
|
|
|
|
|
|
|
const content = event['content'];
|
2019-09-27 18:04:08 -04:00
|
|
|
if (content['msgtype'] === "m.text" && content['body']) {
|
|
|
|
const prefixes = [COMMAND_PREFIX, this.localpart + ":", this.displayName + ":", await client.getUserId() + ":"];
|
2019-09-27 22:07:16 -04:00
|
|
|
const prefixUsed = prefixes.find(p => content['body'].startsWith(p));
|
|
|
|
if (!prefixUsed) return;
|
|
|
|
|
|
|
|
// rewrite the event body to make the prefix uniform (in case the bot has spaces in its display name)
|
|
|
|
event['content']['body'] = COMMAND_PREFIX + content['body'].substring(prefixUsed.length);
|
2019-09-27 18:04:08 -04:00
|
|
|
|
2019-09-27 17:15:10 -04:00
|
|
|
await client.sendReadReceipt(roomId, event['event_id']);
|
|
|
|
return handleCommand(roomId, event, this);
|
|
|
|
}
|
|
|
|
});
|
2019-09-27 18:04:08 -04:00
|
|
|
|
|
|
|
client.getUserId().then(userId => {
|
|
|
|
this.localpart = userId.split(':')[0].substring(1);
|
|
|
|
return client.getUserProfile(userId);
|
|
|
|
}).then(profile => {
|
|
|
|
if (profile['displayname']) {
|
|
|
|
this.displayName = profile['displayname'];
|
|
|
|
}
|
|
|
|
})
|
2019-09-27 17:15:10 -04:00
|
|
|
}
|
|
|
|
|
2019-10-04 23:22:18 -04:00
|
|
|
public get state(): string {
|
|
|
|
return this.currentState;
|
|
|
|
}
|
|
|
|
|
2019-09-27 17:15:10 -04:00
|
|
|
public start() {
|
2019-10-04 23:22:34 -04:00
|
|
|
return this.client.start().then(async () => {
|
|
|
|
this.currentState = STATE_CHECKING_PERMISSIONS;
|
|
|
|
if (config.verifyPermissionsOnStartup) {
|
2019-10-04 23:38:50 -04:00
|
|
|
if (config.verboseLogging) {
|
|
|
|
await this.client.sendNotice(this.managementRoomId, "Checking permissions...");
|
|
|
|
}
|
|
|
|
await this.verifyPermissions(config.verboseLogging);
|
2019-10-04 23:22:34 -04:00
|
|
|
}
|
|
|
|
}).then(async () => {
|
|
|
|
this.currentState = STATE_SYNCING;
|
2019-10-04 23:02:37 -04:00
|
|
|
if (config.syncOnStartup) {
|
2019-10-04 23:38:50 -04:00
|
|
|
if (config.verboseLogging) {
|
|
|
|
await this.client.sendNotice(this.managementRoomId, "Syncing lists...");
|
|
|
|
}
|
|
|
|
await this.syncLists(config.verboseLogging);
|
2019-10-04 23:02:37 -04:00
|
|
|
}
|
2019-10-04 23:22:34 -04:00
|
|
|
}).then(async () => {
|
|
|
|
this.currentState = STATE_RUNNING;
|
2019-10-04 23:38:50 -04:00
|
|
|
if (config.verboseLogging) {
|
|
|
|
await this.client.sendNotice(this.managementRoomId, "Startup complete.");
|
|
|
|
}
|
2019-10-04 23:02:37 -04:00
|
|
|
});
|
2019-09-27 17:15:10 -04:00
|
|
|
}
|
|
|
|
|
2019-10-04 23:38:50 -04:00
|
|
|
public async verifyPermissions(verbose: boolean = true) {
|
2019-10-04 23:22:34 -04:00
|
|
|
const errors: RoomUpdateError[] = [];
|
|
|
|
for (const roomId of Object.keys(this.protectedRooms)) {
|
2019-10-04 23:36:19 -04:00
|
|
|
errors.push(...(await this.verifyPermissionsIn(roomId)));
|
2019-10-04 23:22:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
const hadErrors = await this.printActionResult(errors, "Permission errors in protected rooms:");
|
2019-10-04 23:38:50 -04:00
|
|
|
if (!hadErrors && verbose) {
|
2019-10-04 23:22:34 -04:00
|
|
|
const html = `<font color="#00cc00">All permissions look OK.</font>`;
|
|
|
|
const text = "All permissions look OK.";
|
|
|
|
await this.client.sendMessage(this.managementRoomId, {
|
|
|
|
msgtype: "m.notice",
|
|
|
|
body: text,
|
|
|
|
format: "org.matrix.custom.html",
|
|
|
|
formatted_body: html,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-04 23:36:19 -04:00
|
|
|
private async verifyPermissionsIn(roomId: string): Promise<RoomUpdateError[]> {
|
|
|
|
const errors: RoomUpdateError[] = [];
|
|
|
|
|
|
|
|
try {
|
|
|
|
const ownUserId = await this.client.getUserId();
|
|
|
|
|
|
|
|
const powerLevels = await this.client.getRoomStateEvent(roomId, "m.room.power_levels", "");
|
|
|
|
if (!powerLevels) {
|
|
|
|
// noinspection ExceptionCaughtLocallyJS
|
|
|
|
throw new Error("Missing power levels state event");
|
|
|
|
}
|
|
|
|
|
|
|
|
function plDefault(val: number | undefined | null, def: number): number {
|
|
|
|
if (!val && val !== 0) return def;
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
|
|
|
const users = powerLevels['users'] || {};
|
|
|
|
const events = powerLevels['events'] || {};
|
|
|
|
const usersDefault = plDefault(powerLevels['users_default'], 0);
|
|
|
|
const stateDefault = plDefault(powerLevels['state_default'], 50);
|
|
|
|
const ban = plDefault(powerLevels['ban'], 50);
|
|
|
|
const kick = plDefault(powerLevels['kick'], 50);
|
|
|
|
const redact = plDefault(powerLevels['redact'], 50);
|
|
|
|
|
|
|
|
const userLevel = plDefault(users[ownUserId], usersDefault);
|
|
|
|
const aclLevel = plDefault(events["m.room.server_acl"], stateDefault);
|
|
|
|
|
|
|
|
// Wants: ban, kick, redact, m.room.server_acl
|
|
|
|
|
|
|
|
if (userLevel < ban) {
|
|
|
|
errors.push({roomId, errorMessage: `Missing power level for bans: ${userLevel} < ${ban}`});
|
|
|
|
}
|
|
|
|
if (userLevel < kick) {
|
|
|
|
errors.push({roomId, errorMessage: `Missing power level for kicks: ${userLevel} < ${kick}`});
|
|
|
|
}
|
|
|
|
if (userLevel < redact) {
|
|
|
|
errors.push({roomId, errorMessage: `Missing power level for redactions: ${userLevel} < ${redact}`});
|
|
|
|
}
|
|
|
|
if (userLevel < aclLevel) {
|
|
|
|
errors.push({roomId, errorMessage: `Missing power level for server ACLs: ${userLevel} < ${aclLevel}`});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise OK
|
|
|
|
} catch (e) {
|
|
|
|
LogService.error("Mjolnir", e);
|
|
|
|
errors.push({roomId, errorMessage: e.message || (e.body ? e.body.error : '<no message>')});
|
|
|
|
}
|
|
|
|
|
|
|
|
return errors;
|
|
|
|
}
|
|
|
|
|
2019-10-04 23:38:50 -04:00
|
|
|
public async syncLists(verbose: boolean = true) {
|
2019-09-27 22:02:03 -04:00
|
|
|
for (const list of this.banLists) {
|
|
|
|
await list.updateList();
|
|
|
|
}
|
|
|
|
|
2019-10-04 22:59:30 -04:00
|
|
|
let hadErrors = false;
|
|
|
|
|
|
|
|
const aclErrors = await applyServerAcls(this.banLists, Object.keys(this.protectedRooms), this);
|
|
|
|
const banErrors = await applyUserBans(this.banLists, Object.keys(this.protectedRooms), this);
|
|
|
|
hadErrors = hadErrors || await this.printActionResult(aclErrors, "Errors updating server ACLs:");
|
|
|
|
hadErrors = hadErrors || await this.printActionResult(banErrors, "Errors updating member bans:");
|
|
|
|
|
2019-10-04 23:38:50 -04:00
|
|
|
if (!hadErrors && verbose) {
|
2019-10-04 22:59:30 -04:00
|
|
|
const html = `<font color="#00cc00">Done updating rooms - no errors</font>`;
|
2019-10-04 23:38:50 -04:00
|
|
|
const text = "Done updating rooms - no errors";
|
2019-10-04 22:59:30 -04:00
|
|
|
await this.client.sendMessage(this.managementRoomId, {
|
|
|
|
msgtype: "m.notice",
|
|
|
|
body: text,
|
|
|
|
format: "org.matrix.custom.html",
|
|
|
|
formatted_body: html,
|
|
|
|
});
|
|
|
|
}
|
2019-09-27 22:02:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
2019-10-04 22:59:30 -04:00
|
|
|
let hadErrors = false;
|
|
|
|
|
|
|
|
const aclErrors = await applyServerAcls(this.banLists, Object.keys(this.protectedRooms), this);
|
|
|
|
const banErrors = await applyUserBans(this.banLists, Object.keys(this.protectedRooms), this);
|
|
|
|
hadErrors = hadErrors || await this.printActionResult(aclErrors, "Errors updating server ACLs:");
|
|
|
|
hadErrors = hadErrors || await this.printActionResult(banErrors, "Errors updating member bans:");
|
|
|
|
|
|
|
|
if (!hadErrors) {
|
|
|
|
const html = `<font color="#00cc00"><b>Done updating rooms - no errors</b></font>`;
|
|
|
|
const text = "Done updating rooms - no errors";
|
|
|
|
await this.client.sendMessage(this.managementRoomId, {
|
|
|
|
msgtype: "m.notice",
|
|
|
|
body: text,
|
|
|
|
format: "org.matrix.custom.html",
|
|
|
|
formatted_body: html,
|
|
|
|
});
|
|
|
|
}
|
2019-09-27 22:02:03 -04:00
|
|
|
}
|
|
|
|
|
2019-09-27 17:15:10 -04:00
|
|
|
private async handleEvent(roomId: string, event: any) {
|
2019-10-08 12:58:37 -04:00
|
|
|
if (!Object.keys(this.protectedRooms).includes(roomId)) return;
|
|
|
|
|
|
|
|
if (event['type'] === 'm.room.power_levels' && event['state_key'] === '') {
|
2019-10-04 23:36:19 -04:00
|
|
|
// power levels were updated - recheck permissions
|
|
|
|
const url = this.protectedRooms[roomId];
|
|
|
|
const html = `Power levels changed in <a href="${url}">${roomId}</a> - checking permissions...`;
|
|
|
|
const text = `Power levels changed in ${url} - checking permissions...`;
|
|
|
|
await this.client.sendMessage(this.managementRoomId, {
|
|
|
|
msgtype: "m.notice",
|
|
|
|
body: text,
|
|
|
|
format: "org.matrix.custom.html",
|
|
|
|
formatted_body: html,
|
|
|
|
});
|
|
|
|
const errors = await this.verifyPermissionsIn(roomId);
|
|
|
|
const hadErrors = await this.printActionResult(errors);
|
|
|
|
if (!hadErrors) {
|
|
|
|
const html = `<font color="#00cc00">All permissions look OK.</font>`;
|
|
|
|
const text = "All permissions look OK.";
|
|
|
|
await this.client.sendMessage(this.managementRoomId, {
|
|
|
|
msgtype: "m.notice",
|
|
|
|
body: text,
|
|
|
|
format: "org.matrix.custom.html",
|
|
|
|
formatted_body: html,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!event['state_key']) return; // from here we don't do anything with events missing/empty state key
|
2019-09-27 17:15:10 -04:00
|
|
|
|
|
|
|
if (ALL_RULE_TYPES.includes(event['type'])) {
|
2019-09-27 22:02:03 -04:00
|
|
|
await this.syncListForRoom(roomId);
|
2019-09-27 17:15:10 -04:00
|
|
|
} else if (event['type'] === "m.room.member") {
|
2019-10-04 22:59:30 -04:00
|
|
|
const errors = await applyUserBans(this.banLists, Object.keys(this.protectedRooms), this);
|
|
|
|
const hadErrors = await this.printActionResult(errors);
|
|
|
|
|
|
|
|
if (!hadErrors) {
|
|
|
|
const html = `<font color="#00cc00"><b>Done updating rooms - no errors</b></font>`;
|
|
|
|
const text = "Done updating rooms - no errors";
|
|
|
|
await this.client.sendMessage(this.managementRoomId, {
|
|
|
|
msgtype: "m.notice",
|
|
|
|
body: text,
|
|
|
|
format: "org.matrix.custom.html",
|
|
|
|
formatted_body: html,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
} else return; // Not processed
|
2019-09-27 17:15:10 -04:00
|
|
|
}
|
|
|
|
|
2019-10-04 23:36:19 -04:00
|
|
|
private async printActionResult(errors: RoomUpdateError[], title: string = null) {
|
2019-10-04 22:59:30 -04:00
|
|
|
if (errors.length <= 0) return false;
|
2019-09-27 21:54:13 -04:00
|
|
|
|
2019-09-27 17:15:10 -04:00
|
|
|
let html = "";
|
|
|
|
let text = "";
|
|
|
|
|
2019-10-04 22:59:30 -04:00
|
|
|
const htmlTitle = title ? `${title}<br />` : '';
|
|
|
|
const textTitle = title ? `${title}\n` : '';
|
|
|
|
|
|
|
|
html += `<font color="#ff0000"><b>${htmlTitle}${errors.length} errors updating protected rooms!</b></font><br /><ul>`;
|
|
|
|
text += `${textTitle}${errors.length} errors updating protected rooms!\n`;
|
2019-09-27 21:54:13 -04:00
|
|
|
for (const error of errors) {
|
|
|
|
const url = this.protectedRooms[error.roomId] ? this.protectedRooms[error.roomId] : `https://matrix.to/#/${error.roomId}`;
|
|
|
|
html += `<li><a href="${url}">${error.roomId}</a> - ${error.errorMessage}</li>`;
|
|
|
|
text += `${url} - ${error.errorMessage}\n`;
|
2019-09-27 17:15:10 -04:00
|
|
|
}
|
2019-09-27 21:54:13 -04:00
|
|
|
html += "</ul>";
|
2019-09-27 17:15:10 -04:00
|
|
|
|
|
|
|
const message = {
|
|
|
|
msgtype: "m.notice",
|
|
|
|
body: text,
|
|
|
|
format: "org.matrix.custom.html",
|
|
|
|
formatted_body: html,
|
|
|
|
};
|
2019-10-04 22:59:30 -04:00
|
|
|
await this.client.sendMessage(this.managementRoomId, message);
|
|
|
|
return true;
|
2019-09-27 17:15:10 -04:00
|
|
|
}
|
|
|
|
}
|