2019-09-25 22:13:20 -04:00
|
|
|
/*
|
2020-06-12 10:03:08 -04:00
|
|
|
Copyright 2019, 2020 The Matrix.org Foundation C.I.C.
|
2019-09-25 22:13:20 -04:00
|
|
|
|
|
|
|
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 * as path from "path";
|
|
|
|
import {
|
2019-10-31 11:55:34 -04:00
|
|
|
LogLevel,
|
2019-09-25 22:13:20 -04:00
|
|
|
LogService,
|
|
|
|
MatrixClient,
|
2019-10-03 00:20:37 -04:00
|
|
|
PantalaimonClient,
|
2019-09-27 15:57:36 -04:00
|
|
|
Permalinks,
|
2019-09-25 22:13:20 -04:00
|
|
|
RichConsoleLogger,
|
|
|
|
SimpleFsStorageProvider
|
|
|
|
} from "matrix-bot-sdk";
|
|
|
|
import config from "./config";
|
2019-09-27 17:44:28 -04:00
|
|
|
import BanList from "./models/BanList";
|
2019-09-27 17:15:10 -04:00
|
|
|
import { Mjolnir } from "./Mjolnir";
|
2019-11-06 20:46:49 -05:00
|
|
|
import { logMessage } from "./LogProxy";
|
2020-01-21 15:53:02 -05:00
|
|
|
import { MembershipEvent } from "matrix-bot-sdk/lib/models/events/MembershipEvent";
|
2020-03-05 17:38:09 -05:00
|
|
|
import * as htmlEscape from "escape-html";
|
2020-06-12 10:03:08 -04:00
|
|
|
import { Healthz } from "./health/healthz";
|
2019-11-06 20:46:49 -05:00
|
|
|
|
2021-07-22 02:38:44 -04:00
|
|
|
config.RUNTIME = {};
|
2019-09-25 22:13:20 -04:00
|
|
|
|
|
|
|
LogService.setLogger(new RichConsoleLogger());
|
2019-10-31 11:55:34 -04:00
|
|
|
LogService.setLevel(LogLevel.fromString(config.logLevel, LogLevel.DEBUG));
|
|
|
|
|
|
|
|
LogService.info("index", "Starting bot...");
|
2019-09-25 22:13:20 -04:00
|
|
|
|
2020-06-12 10:03:08 -04:00
|
|
|
Healthz.isHealthy = false; // start off unhealthy
|
|
|
|
if (config.health.healthz.enabled) {
|
|
|
|
Healthz.listen();
|
|
|
|
}
|
|
|
|
|
2019-10-03 00:20:37 -04:00
|
|
|
(async function () {
|
2021-09-09 12:21:13 -04:00
|
|
|
const storagePath = path.isAbsolute(config.dataPath) ? config.dataPath : path.join(__dirname, '../', config.dataPath)
|
|
|
|
const storage = new SimpleFsStorageProvider(path.join(storagePath, "bot.json"));
|
2019-09-25 22:13:20 -04:00
|
|
|
|
2019-10-03 00:20:37 -04:00
|
|
|
let client: MatrixClient;
|
|
|
|
if (config.pantalaimon.use) {
|
|
|
|
const pantalaimon = new PantalaimonClient(config.homeserverUrl, storage);
|
|
|
|
client = await pantalaimon.createClientWithCredentials(config.pantalaimon.username, config.pantalaimon.password);
|
|
|
|
} else {
|
|
|
|
client = new MatrixClient(config.homeserverUrl, config.accessToken, storage);
|
|
|
|
}
|
|
|
|
|
2019-11-06 20:46:49 -05:00
|
|
|
config.RUNTIME.client = client;
|
|
|
|
|
2020-03-05 17:29:30 -05:00
|
|
|
client.on("room.invite", async (roomId: string, inviteEvent: any) => {
|
|
|
|
const membershipEvent = new MembershipEvent(inviteEvent);
|
|
|
|
|
2020-03-05 17:38:09 -05:00
|
|
|
const reportInvite = async () => {
|
|
|
|
if (!config.recordIgnoredInvites) return; // Nothing to do
|
|
|
|
|
|
|
|
await client.sendMessage(config.managementRoom, {
|
|
|
|
msgtype: "m.text",
|
|
|
|
body: `${membershipEvent.sender} has invited me to ${roomId} but the config prevents me from accepting the invitation. `
|
|
|
|
+ `If you would like this room protected, use "!mjolnir rooms add ${roomId}" so I can accept the invite.`,
|
|
|
|
format: "org.matrix.custom.html",
|
|
|
|
formatted_body: `${htmlEscape(membershipEvent.sender)} has invited me to ${htmlEscape(roomId)} but the config prevents me from `
|
|
|
|
+ `accepting the invitation. If you would like this room protected, use <code>!mjolnir rooms add ${htmlEscape(roomId)}</code> `
|
|
|
|
+ `so I can accept the invite.`,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2020-01-21 15:53:02 -05:00
|
|
|
if (config.autojoinOnlyIfManager) {
|
2020-03-05 17:29:30 -05:00
|
|
|
const managers = await client.getJoinedRoomMembers(config.managementRoom);
|
2020-03-05 17:38:09 -05:00
|
|
|
if (!managers.includes(membershipEvent.sender)) return reportInvite(); // ignore invite
|
2020-01-21 15:53:02 -05:00
|
|
|
} else {
|
2020-03-05 17:29:30 -05:00
|
|
|
const groupMembers = await client.unstableApis.getGroupUsers(config.acceptInvitesFromGroup);
|
|
|
|
const userIds = groupMembers.map(m => m.user_id);
|
2020-03-05 17:38:09 -05:00
|
|
|
if (!userIds.includes(membershipEvent.sender)) return reportInvite(); // ignore invite
|
2020-01-21 15:53:02 -05:00
|
|
|
}
|
2020-03-05 17:29:30 -05:00
|
|
|
|
|
|
|
return client.joinRoom(roomId);
|
|
|
|
});
|
2019-09-25 22:13:20 -04:00
|
|
|
|
2019-09-27 17:15:10 -04:00
|
|
|
const banLists: BanList[] = [];
|
2019-09-27 17:44:28 -04:00
|
|
|
const protectedRooms: { [roomId: string]: string } = {};
|
2019-09-27 15:57:36 -04:00
|
|
|
const joinedRooms = await client.getJoinedRooms();
|
2019-09-27 16:36:23 -04:00
|
|
|
// Ensure we're also joined to the rooms we're protecting
|
2019-11-07 13:06:56 -05:00
|
|
|
LogService.info("index", "Resolving protected rooms...");
|
2019-09-27 16:36:23 -04:00
|
|
|
for (const roomRef of config.protectedRooms) {
|
|
|
|
const permalink = Permalinks.parseUrl(roomRef);
|
|
|
|
if (!permalink.roomIdOrAlias) continue;
|
|
|
|
|
|
|
|
let roomId = await client.resolveRoom(permalink.roomIdOrAlias);
|
|
|
|
if (!joinedRooms.includes(roomId)) {
|
|
|
|
roomId = await client.joinRoom(permalink.roomIdOrAlias, permalink.viaServers);
|
|
|
|
}
|
|
|
|
|
|
|
|
protectedRooms[roomId] = roomRef;
|
|
|
|
}
|
|
|
|
|
2019-09-27 15:57:36 -04:00
|
|
|
// Ensure we're also in the management room
|
2019-11-07 13:06:56 -05:00
|
|
|
LogService.info("index", "Resolving management room...");
|
2019-11-07 13:05:52 -05:00
|
|
|
const managementRoomId = await client.resolveRoom(config.managementRoom);
|
|
|
|
if (!joinedRooms.includes(managementRoomId)) {
|
|
|
|
config.managementRoom = await client.joinRoom(config.managementRoom);
|
|
|
|
} else {
|
|
|
|
config.managementRoom = managementRoomId;
|
|
|
|
}
|
2019-11-06 20:46:49 -05:00
|
|
|
await logMessage(LogLevel.INFO, "index", "Mjolnir is starting up. Use !mjolnir to query status.");
|
2019-09-27 15:57:36 -04:00
|
|
|
|
2019-11-06 20:46:49 -05:00
|
|
|
const bot = new Mjolnir(client, protectedRooms, banLists);
|
2019-09-27 17:15:10 -04:00
|
|
|
await bot.start();
|
2021-06-14 12:56:02 -04:00
|
|
|
})().catch(err => {
|
|
|
|
logMessage(LogLevel.ERROR, "index", err);
|
|
|
|
process.exit(1);
|
|
|
|
});
|