diff --git a/package.json b/package.json index d3167842..55b841b9 100644 --- a/package.json +++ b/package.json @@ -63,8 +63,8 @@ "js-yaml": "^4.1.0", "jsdom": "^24.0.0", "matrix-appservice-bridge": "^9.0.1", - "matrix-protection-suite": "npm:@gnuxie/matrix-protection-suite@0.15.0", - "matrix-protection-suite-for-matrix-bot-sdk": "npm:@gnuxie/matrix-protection-suite-for-matrix-bot-sdk@0.15.0", + "matrix-protection-suite": "npm:@gnuxie/matrix-protection-suite@0.16.0", + "matrix-protection-suite-for-matrix-bot-sdk": "npm:@gnuxie/matrix-protection-suite-for-matrix-bot-sdk@0.16.1", "parse-duration": "^1.0.2", "pg": "^8.8.0", "shell-quote": "^1.7.3", diff --git a/src/draupnirfactory/DraupnirFactory.ts b/src/draupnirfactory/DraupnirFactory.ts index 26422df2..94d4763b 100644 --- a/src/draupnirfactory/DraupnirFactory.ts +++ b/src/draupnirfactory/DraupnirFactory.ts @@ -39,7 +39,8 @@ export class DraupnirFactory { roomMembershipManager, client, clientPlatform, - clientUserID + clientUserID, + config ); if (isError(protectedRoomsSet)) { return protectedRoomsSet; diff --git a/src/draupnirfactory/DraupnirProtectedRoomsSet.ts b/src/draupnirfactory/DraupnirProtectedRoomsSet.ts index c8ab55ed..2a6e6139 100644 --- a/src/draupnirfactory/DraupnirProtectedRoomsSet.ts +++ b/src/draupnirfactory/DraupnirProtectedRoomsSet.ts @@ -25,10 +25,14 @@ limitations under the License. * are NOT distributed, contributed, committed, or licensed under the Apache License. */ -import { ActionResult, ClientPlatform, MJOLNIR_PROTECTED_ROOMS_EVENT_TYPE, MJOLNIR_WATCHED_POLICY_ROOMS_EVENT_TYPE, MatrixRoomID, MjolnirEnabledProtectionsEvent, MjolnirEnabledProtectionsEventType, MjolnirPolicyRoomsConfig, MjolnirProtectedRoomsConfig, MjolnirProtectedRoomsEvent, MjolnirProtectionSettingsEventType, MjolnirProtectionsConfig, MjolnirWatchedPolicyRoomsEvent, Ok, PolicyListConfig, PolicyRoomManager, ProtectedRoomsConfig, ProtectedRoomsSet, ProtectionsConfig, RoomJoiner, RoomMembershipManager, RoomStateManager, SetMembership, SetRoomState, StandardProtectedRoomsSet, StandardSetMembership, StandardSetRoomState, StringUserID, isError } from "matrix-protection-suite"; +import { ActionResult, ClientPlatform, Logger, MJOLNIR_PROTECTED_ROOMS_EVENT_TYPE, MJOLNIR_WATCHED_POLICY_ROOMS_EVENT_TYPE, MatrixRoomID, MissingProtectionCB, MjolnirEnabledProtectionsEvent, MjolnirEnabledProtectionsEventType, MjolnirPolicyRoomsConfig, MjolnirProtectedRoomsConfig, MjolnirProtectedRoomsEvent, MjolnirProtectionSettingsEventType, MjolnirProtectionsConfig, MjolnirWatchedPolicyRoomsEvent, Ok, PolicyListConfig, PolicyRoomManager, ProtectedRoomsConfig, ProtectedRoomsSet, ProtectionsManager, RoomJoiner, RoomMembershipManager, RoomStateManager, SetMembership, SetRoomState, StandardProtectedRoomsSet, StandardProtectionsManager, StandardSetMembership, StandardSetRoomState, StringUserID, isError } from "matrix-protection-suite"; import { BotSDKMatrixAccountData, BotSDKMatrixStateData, MatrixSendClient } from "matrix-protection-suite-for-matrix-bot-sdk"; import { DefaultEnabledProtectionsMigration } from "../protections/DefaultEnabledProtectionsMigration"; import '../protections/DraupnirProtectionsIndex'; +import { IConfig } from "../config"; +import { runProtectionConfigHooks } from "../protections/ConfigHooks"; + +const log = new Logger('DraupnirProtectedRoomsSet'); async function makePolicyListConfig( client: MatrixSendClient, @@ -81,30 +85,55 @@ async function makeSetRoomState( ); } -async function makeProtectionConfig( +function missingProtectionCB(protectionName: string): void { + log.warn(`Unable to find a protection description for the protection named`, protectionName); +} + +// FIXME: https://github.com/the-draupnir-project/Draupnir/issues/338 +function makeMissingProtectionCB(): MissingProtectionCB { + return missingProtectionCB +} + +async function makeProtectionsManager( client: MatrixSendClient, roomStateManager: RoomStateManager, - managementRoom: MatrixRoomID -): Promise> { + managementRoom: MatrixRoomID, + config: IConfig +): Promise> { const result = await roomStateManager.getRoomStateRevisionIssuer( managementRoom ); if (isError(result)) { return result; } - return Ok(new MjolnirProtectionsConfig( + const protectionsConfigResult = await MjolnirProtectionsConfig.create( new BotSDKMatrixAccountData( MjolnirEnabledProtectionsEventType, MjolnirEnabledProtectionsEvent, client ), - new BotSDKMatrixStateData( - MjolnirProtectionSettingsEventType, - result.ok, - client - ), - DefaultEnabledProtectionsMigration, - )); + { + migrationHandler: DefaultEnabledProtectionsMigration, + missingProtectionCB: makeMissingProtectionCB() + } + ); + if (isError(protectionsConfigResult)) { + return protectionsConfigResult; + } + const hookResult = await runProtectionConfigHooks(config, protectionsConfigResult.ok); + if (isError(hookResult)) { + return hookResult; + } + return Ok( + new StandardProtectionsManager( + protectionsConfigResult.ok, + new BotSDKMatrixStateData( + MjolnirProtectionSettingsEventType, + result.ok, + client + ) + ) + ); } @@ -115,7 +144,8 @@ export async function makeProtectedRoomsSet( roomMembershipManager: RoomMembershipManager, client: MatrixSendClient, clientPlatform: ClientPlatform, - userID: StringUserID + userID: StringUserID, + config: IConfig, ): Promise> { const protectedRoomsConfig = await makeProtectedRoomsConfig(client, clientPlatform.toRoomJoiner()) if (isError(protectedRoomsConfig)) { @@ -139,10 +169,11 @@ export async function makeProtectedRoomsSet( if (isError(policyListConfig)) { return policyListConfig; } - const protectionsConfig = await makeProtectionConfig( + const protectionsConfig = await makeProtectionsManager( client, roomStateManager, - managementRoom + managementRoom, + config ); if (isError(protectionsConfig)) { return protectionsConfig; diff --git a/src/protections/ConfigHooks.ts b/src/protections/ConfigHooks.ts new file mode 100644 index 00000000..3e2b376f --- /dev/null +++ b/src/protections/ConfigHooks.ts @@ -0,0 +1,33 @@ +// Copyright 2024 Gnuxie +// +// SPDX-License-Identifier: AFL-3.0 + +import { ActionResult, Ok, ProtectionsConfig, ServerBanSynchronisationProtection, isError } from "matrix-protection-suite"; +import { IConfig } from "../config"; + +type ConfigHook = (config: IConfig, protectionsConfig: ProtectionsConfig) => Promise>; + +const hooks: ConfigHook[] = [ + async function disableServerACL (config, protectionsConfig) { + if (config.disableServerACL) { + return await protectionsConfig.disableProtection(ServerBanSynchronisationProtection.name); + } else { + return Ok(undefined); + } + } +] + +/** + * Introduced to allow the legacy option `config.disableServerACL` to map onto + * MPS's ServerBanSynchronisationProtection. I think we need to deprecate the + * option and offer something else. + */ +export async function runProtectionConfigHooks(config: IConfig, protectionsConfig: ProtectionsConfig): Promise> { + for (const hook of hooks) { + const result = await hook(config, protectionsConfig); + if (isError(result)) { + return result; + } + } + return Ok(undefined); +} diff --git a/yarn.lock b/yarn.lock index b7ab0b25..de58ecab 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2505,15 +2505,15 @@ matrix-appservice@^2.0.0: request-promise "^4.2.6" sanitize-html "^2.8.0" -"matrix-protection-suite-for-matrix-bot-sdk@npm:@gnuxie/matrix-protection-suite-for-matrix-bot-sdk@0.15.0": - version "0.15.0" - resolved "https://registry.yarnpkg.com/@gnuxie/matrix-protection-suite-for-matrix-bot-sdk/-/matrix-protection-suite-for-matrix-bot-sdk-0.15.0.tgz#d59c51af79b3d198791a065fed16bd65b52dac33" - integrity sha512-U4CqeTBBwuI/kQFjT5muFwIoNkCopb5yG4TXvEEJqWy+Vt2NrAHV3UGwr5t/fcBYzzC+GLDYlRQkG5B9x0n3YA== +"matrix-protection-suite-for-matrix-bot-sdk@npm:@gnuxie/matrix-protection-suite-for-matrix-bot-sdk@0.16.1": + version "0.16.1" + resolved "https://registry.yarnpkg.com/@gnuxie/matrix-protection-suite-for-matrix-bot-sdk/-/matrix-protection-suite-for-matrix-bot-sdk-0.16.1.tgz#322d6cb88cb158140f432e1f853a69e8697cd6a5" + integrity sha512-fipZknx+QNuwNHV+er6LWdnUJ8/6NhuIpqP9O8zdMVC8Wn+/vNcAzTKCO3k9ulDNYtTgA8M+dyOlg0nOJVCJAQ== -"matrix-protection-suite@npm:@gnuxie/matrix-protection-suite@0.15.0": - version "0.15.0" - resolved "https://registry.yarnpkg.com/@gnuxie/matrix-protection-suite/-/matrix-protection-suite-0.15.0.tgz#7a876e4f670ac8d513e23eb4d4919d1f67f19d1c" - integrity sha512-2E75q9KEj1nz9V/5I7dOLRDTh1czDEftppbcr4DvPnJeXiJsLJ5eiSqd/hbr0AN0/PS+rICPgOqGOEbXXsk3uQ== +"matrix-protection-suite@npm:@gnuxie/matrix-protection-suite@0.16.0": + version "0.16.0" + resolved "https://registry.yarnpkg.com/@gnuxie/matrix-protection-suite/-/matrix-protection-suite-0.16.0.tgz#380367f76721373e13cc04399ddffa0b9b1ee9c0" + integrity sha512-XNAaECsv3CUYZN2mBiwVrG+qOs+KP0NDIw353/KtZFOAoAKwCFltXpPoQe/TT+Lc0eSVxfKtW6+rofWBccjfVQ== dependencies: await-lock "^2.2.2" crypto-js "^4.1.1"