Commit cef9117d authored by Grant's avatar Grant
Browse files

restrict bot addition to specific mxid

parent 044bb26e
Loading
Loading
Loading
Loading
+23 −1
Original line number Diff line number Diff line
@@ -108,6 +108,19 @@ export class Matrix {
    );
  }

  static async isUserInRoom(roomId: string, user: IUsername): Promise<boolean> {
    const req = await this.fetch<MatrixRoomMembership["content"]>(
      `/_matrix/client/v3/rooms/${roomId}/state/m.room.member/@${user}:${MATRIX_HOMESERVER}`
    );

    if ("errcode" in req.data) {
      // if(req.data.errcode === "M_NOT_FOUND")
      return false;
    } else {
      return req.data.membership === "join";
    }
  }

  /**
   * Use the main account to make sure the mentioned user is in the room, if not, invite
   * @param roomId
@@ -293,7 +306,6 @@ export class Matrix {
   * @see https://spec.matrix.org/v1.10/application-service-api/#using-sync-and-events
   */
  sync() {}

  /**
   * Check if a Discord channel has a matching Matrix room
   *
@@ -330,6 +342,16 @@ export class Matrix {
    return req;
  }

  async leaveRoom(roomId: string) {
    const req = await this.fetch(
      `/_matrix/client/v3/rooms/${roomId}/leave`,
      "POST",
      {}
    );

    return req;
  }

  /**
   * Get all local aliases for room_id
   *
+11 −3
Original line number Diff line number Diff line
@@ -89,9 +89,16 @@ class MatrixHandler_ {
        event.state_key ===
        "@_discord_bot:" + process.env.MATRIX_HOMESERVER
      ) {
        const matrixClient = await Matrix.for("_discord_bot");

        if (event.sender !== process.env.MATRIX_ADMIN) {
          // only the admin account should be able to add the bot
          await matrixClient.leaveRoom(event.room_id);
          return;
        }

        // auto accept if it's the main bot that's being invited

        const matrixClient = await Matrix.for("_discord_bot");
        await matrixClient.joinRoom(event.room_id);
      } else {
        // for another bot account
@@ -116,7 +123,8 @@ class MatrixHandler_ {

    if (event.content.membership === "leave") {
      if (event.prev_content?.membership === "leave") return; // not a membership leave
      if (event.state_key?.startsWith("@_discord_")) return;

      if (event.state_key && !event.state_key.startsWith("@_discord_"))
        await this.handleRoomLeave(event);
    }

+6 −0
Original line number Diff line number Diff line
@@ -12,6 +12,12 @@ declare global {
      MATRIX_HS_TOKEN: string;
      MATRIX_HOMESERVER: string;
      DISCORD_TOKEN: string;

      /**
       * what matrix account is able to invite the main bot
       * @example @grant:aftermath.gg
       */
      MATRIX_ADMIN: string;
    }
  }
}