mjolnir/src/commands/ImportCommand.ts

91 lines
3.8 KiB
TypeScript
Raw Normal View History

2019-10-10 06:30:52 -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.
*/
import { Mjolnir } from "../Mjolnir";
import { RichReply } from "matrix-bot-sdk";
import { RECOMMENDATION_BAN, recommendationToStable } from "../models/ListRule";
import { RULE_SERVER, RULE_USER, ruleTypeToStable } from "../models/BanList";
2019-11-06 20:46:49 -05:00
import config from "../config";
2019-10-10 06:30:52 -04:00
// !mjolnir import <room ID> <shortcode>
export async function execImportCommand(roomId: string, event: any, mjolnir: Mjolnir, parts: string[]) {
const importRoomId = await mjolnir.client.resolveRoom(parts[2]);
const list = mjolnir.lists.find(b => b.listShortcode === parts[3]);
if (!list) {
2019-10-23 15:25:32 -04:00
const errMessage = "Unable to find list - check your shortcode.";
const errReply = RichReply.createFor(roomId, event, errMessage, errMessage);
errReply["msgtype"] = "m.notice";
2021-07-22 02:24:12 -04:00
mjolnir.client.sendMessage(roomId, errReply);
return;
2019-10-10 06:30:52 -04:00
}
let importedRules = 0;
const state = await mjolnir.client.getRoomState(importRoomId);
for (const stateEvent of state) {
const content = stateEvent['content'] || {};
if (!content || Object.keys(content).length === 0) continue;
if (stateEvent['type'] === 'm.room.member' && stateEvent['state_key'] !== '') {
// Member event - check for ban
if (content['membership'] === 'ban') {
const reason = content['reason'] || '<no reason>';
2019-11-06 20:46:49 -05:00
await mjolnir.client.sendNotice(config.managementRoom, `Adding user ${stateEvent['state_key']} to ban list`);
2019-10-10 06:30:52 -04:00
const recommendation = recommendationToStable(RECOMMENDATION_BAN);
const ruleContent = {
entity: stateEvent['state_key'],
recommendation,
reason: reason,
};
const stateKey = `rule:${ruleContent.entity}`;
2021-07-22 02:38:44 -04:00
let stableRule = ruleTypeToStable(RULE_USER);
if (stableRule) {
await mjolnir.client.sendStateEvent(list.roomId, stableRule, stateKey, ruleContent);
}
2019-10-10 06:30:52 -04:00
importedRules++;
}
} else if (stateEvent['type'] === 'm.room.server_acl' && stateEvent['state_key'] === '') {
// ACL event - ban denied servers
if (!content['deny']) continue;
for (const server of content['deny']) {
const reason = "<no reason>";
2019-11-06 20:46:49 -05:00
await mjolnir.client.sendNotice(config.managementRoom, `Adding server ${server} to ban list`);
2019-10-10 06:30:52 -04:00
const recommendation = recommendationToStable(RECOMMENDATION_BAN);
const ruleContent = {
entity: server,
recommendation,
reason: reason,
};
const stateKey = `rule:${ruleContent.entity}`;
2021-07-22 02:38:44 -04:00
let stableRule = ruleTypeToStable(RULE_SERVER);
if (stableRule) {
await mjolnir.client.sendStateEvent(list.roomId, stableRule, stateKey, ruleContent);
}
2019-10-10 06:30:52 -04:00
importedRules++;
}
}
}
const message = `Imported ${importedRules} rules to ban list`;
const reply = RichReply.createFor(roomId, event, message, message);
reply['msgtype'] = "m.notice";
await mjolnir.client.sendMessage(roomId, reply);
}