How to use the matrix-bot-sdk.MatrixGlob function in matrix-bot-sdk

To help you get started, we’ve selected a few matrix-bot-sdk examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github matrix-org / mjolnir / src / commands / UnbanBanCommand.ts View on Github external
export async function execUnbanCommand(roomId: string, event: any, mjolnir: Mjolnir, parts: string[]) {
    const bits = await parseArguments(roomId, event, mjolnir, parts);
    if (!bits) return; // error already handled

    const ruleContent = {}; // empty == clear/unban
    const stateKey = `rule:${bits.entity}`;

    await mjolnir.client.sendStateEvent(bits.list.roomId, bits.ruleType, stateKey, ruleContent);

    if (USER_RULE_TYPES.includes(bits.ruleType) && parts.length > 5 && parts[5] === 'true') {
        const rule = new MatrixGlob(bits.entity);
        await logMessage(LogLevel.INFO, "UnbanBanCommand", "Unbanning users that match glob: " + bits.entity);
        let unbannedSomeone = false;
        for (const protectedRoomId of Object.keys(mjolnir.protectedRooms)) {
            const members = await mjolnir.client.getRoomMembers(protectedRoomId, null, ['ban'], null);
            for (const member of members) {
                const victim = member['state_key'];
                if (!member['content'] || member['content']['membership'] !== 'ban') continue;
                if (rule.test(victim)) {
                    await logMessage(LogLevel.DEBUG, "UnbanBanCommand", `Unbanning ${victim} in ${protectedRoomId}`);

                    if (!config.noop) {
                        await mjolnir.client.unbanUser(victim, protectedRoomId);
                    } else {
                        await logMessage(LogLevel.WARN, "UnbanBanCommand", `Attempted to unban ${victim} in ${protectedRoomId} but Mjolnir is running in no-op mode`);
                    }
github matrix-org / mjolnir / src / Mjolnir.ts View on Github external
constructor(
        public readonly client: MatrixClient,
        public readonly protectedRooms: { [roomId: string]: string },
        private banLists: BanList[],
    ) {
        for (const reason of config.automaticallyRedactForReasons) {
            this.automaticRedactionReasons.push(new MatrixGlob(reason.toLowerCase()));
        }

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

        client.on("room.message", async (roomId, event) => {
            if (roomId !== config.managementRoom) 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() + ":"];
                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);
github matrix-org / mjolnir / src / utils.ts View on Github external
limit: 0,
            types: [],
        },
        account_data: {
            limit: 0,
            types: [],
        },
    };

    let isGlob = true;
    if (!sender.includes("*")) {
        isGlob = false;
        filter.room.timeline['senders'] = [sender];
    }

    const matcher = new MatrixGlob(sender);

    function testUser(userId: string): boolean {
        if (isGlob) {
            return matcher.test(userId);
        } else {
            return userId === sender;
        }
    }

    function initialSync() {
        const qs = {
            filter: JSON.stringify(filter),
        };
        return client.doRequest("GET", "/_matrix/client/r0/sync", qs);
    }
github matrix-org / mjolnir / src / models / ListRule.ts View on Github external
constructor(public readonly entity: string, private action: string, public readonly reason: string, public readonly kind: string) {
        this.glob = new MatrixGlob(entity);
    }