@gakr-gakr/matrix 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +285 -0
- package/SPEC-SUPPORT.md +116 -0
- package/api.ts +38 -0
- package/auth-presence.ts +56 -0
- package/autobot.plugin.json +28 -0
- package/channel-plugin-api.ts +3 -0
- package/cli-metadata.ts +11 -0
- package/contract-api.ts +17 -0
- package/doctor-contract-api.ts +1 -0
- package/helper-api.ts +3 -0
- package/index.ts +55 -0
- package/package.json +101 -0
- package/plugin-entry.handlers.runtime.ts +1 -0
- package/runtime-api.ts +72 -0
- package/runtime-heavy-api.ts +1 -0
- package/runtime-setter-api.ts +3 -0
- package/secret-contract-api.ts +5 -0
- package/setup-entry.ts +17 -0
- package/setup-plugin-api.ts +3 -0
- package/src/account-selection.ts +223 -0
- package/src/actions.ts +346 -0
- package/src/approval-auth.ts +25 -0
- package/src/approval-handler.runtime.ts +595 -0
- package/src/approval-ids.ts +6 -0
- package/src/approval-native.ts +348 -0
- package/src/approval-reaction-auth.ts +45 -0
- package/src/approval-reactions.ts +313 -0
- package/src/auth-precedence.ts +61 -0
- package/src/channel-account-paths.ts +97 -0
- package/src/channel.runtime.ts +17 -0
- package/src/channel.setup.ts +48 -0
- package/src/channel.ts +667 -0
- package/src/cli-metadata.ts +19 -0
- package/src/cli.ts +2298 -0
- package/src/config-adapter.ts +41 -0
- package/src/config-schema.ts +159 -0
- package/src/config-ui-hints.ts +56 -0
- package/src/directory-live.ts +238 -0
- package/src/doctor-contract.ts +287 -0
- package/src/doctor.ts +262 -0
- package/src/env-vars.ts +92 -0
- package/src/exec-approval-resolver.ts +23 -0
- package/src/exec-approvals.ts +293 -0
- package/src/group-mentions.ts +41 -0
- package/src/legacy-crypto-inspector-availability.ts +60 -0
- package/src/legacy-crypto.ts +531 -0
- package/src/legacy-state.ts +156 -0
- package/src/matrix/account-config.ts +175 -0
- package/src/matrix/accounts.ts +194 -0
- package/src/matrix/actions/client.ts +31 -0
- package/src/matrix/actions/devices.ts +34 -0
- package/src/matrix/actions/limits.ts +6 -0
- package/src/matrix/actions/messages.ts +129 -0
- package/src/matrix/actions/pins.ts +63 -0
- package/src/matrix/actions/polls.ts +109 -0
- package/src/matrix/actions/profile.ts +37 -0
- package/src/matrix/actions/reactions.ts +59 -0
- package/src/matrix/actions/room.ts +71 -0
- package/src/matrix/actions/summary.ts +88 -0
- package/src/matrix/actions/types.ts +63 -0
- package/src/matrix/actions/verification.ts +589 -0
- package/src/matrix/actions.ts +37 -0
- package/src/matrix/active-client.ts +26 -0
- package/src/matrix/async-lock.ts +18 -0
- package/src/matrix/backup-health.ts +124 -0
- package/src/matrix/client/config-runtime-api.ts +9 -0
- package/src/matrix/client/config-secret-input.runtime.ts +1 -0
- package/src/matrix/client/config.ts +853 -0
- package/src/matrix/client/create-client.ts +105 -0
- package/src/matrix/client/env-auth.ts +95 -0
- package/src/matrix/client/file-sync-store.ts +289 -0
- package/src/matrix/client/logging.ts +140 -0
- package/src/matrix/client/migration-snapshot.runtime.ts +1 -0
- package/src/matrix/client/private-network-host.ts +1 -0
- package/src/matrix/client/runtime.ts +4 -0
- package/src/matrix/client/shared.ts +316 -0
- package/src/matrix/client/storage.ts +543 -0
- package/src/matrix/client/types.ts +50 -0
- package/src/matrix/client/url-validation.ts +76 -0
- package/src/matrix/client-bootstrap.ts +173 -0
- package/src/matrix/client.ts +23 -0
- package/src/matrix/config-paths.ts +31 -0
- package/src/matrix/config-update.ts +292 -0
- package/src/matrix/credentials-read.ts +207 -0
- package/src/matrix/credentials-write.runtime.ts +35 -0
- package/src/matrix/credentials.ts +95 -0
- package/src/matrix/deps.ts +309 -0
- package/src/matrix/device-health.ts +31 -0
- package/src/matrix/direct-management.ts +349 -0
- package/src/matrix/direct-room.ts +128 -0
- package/src/matrix/draft-stream.ts +225 -0
- package/src/matrix/encryption-guidance.ts +24 -0
- package/src/matrix/errors.ts +21 -0
- package/src/matrix/format.ts +426 -0
- package/src/matrix/legacy-crypto-inspector.ts +95 -0
- package/src/matrix/media-errors.ts +20 -0
- package/src/matrix/media-text.ts +162 -0
- package/src/matrix/monitor/access-state.ts +145 -0
- package/src/matrix/monitor/ack-config.ts +27 -0
- package/src/matrix/monitor/allowlist.ts +92 -0
- package/src/matrix/monitor/auto-join.ts +86 -0
- package/src/matrix/monitor/config.ts +569 -0
- package/src/matrix/monitor/context-summary.ts +43 -0
- package/src/matrix/monitor/direct.ts +296 -0
- package/src/matrix/monitor/events.ts +397 -0
- package/src/matrix/monitor/handler.ts +2271 -0
- package/src/matrix/monitor/inbound-dedupe.ts +267 -0
- package/src/matrix/monitor/index.ts +540 -0
- package/src/matrix/monitor/legacy-crypto-restore.ts +139 -0
- package/src/matrix/monitor/location.ts +108 -0
- package/src/matrix/monitor/media.ts +119 -0
- package/src/matrix/monitor/mentions.ts +256 -0
- package/src/matrix/monitor/reaction-events.ts +197 -0
- package/src/matrix/monitor/recent-invite.ts +30 -0
- package/src/matrix/monitor/replies.ts +136 -0
- package/src/matrix/monitor/reply-context.ts +92 -0
- package/src/matrix/monitor/room-history.ts +301 -0
- package/src/matrix/monitor/room-info.ts +126 -0
- package/src/matrix/monitor/rooms.ts +52 -0
- package/src/matrix/monitor/route.ts +179 -0
- package/src/matrix/monitor/runtime-api.ts +28 -0
- package/src/matrix/monitor/startup-verification.ts +237 -0
- package/src/matrix/monitor/startup.ts +218 -0
- package/src/matrix/monitor/status.ts +120 -0
- package/src/matrix/monitor/sync-lifecycle.ts +91 -0
- package/src/matrix/monitor/task-runner.ts +38 -0
- package/src/matrix/monitor/test-events.ts +21 -0
- package/src/matrix/monitor/thread-context.ts +108 -0
- package/src/matrix/monitor/threads.ts +85 -0
- package/src/matrix/monitor/types.ts +30 -0
- package/src/matrix/monitor/verification-events.ts +643 -0
- package/src/matrix/monitor/verification-utils.ts +46 -0
- package/src/matrix/outbound-media-runtime.ts +1 -0
- package/src/matrix/poll-summary.ts +110 -0
- package/src/matrix/poll-types.ts +429 -0
- package/src/matrix/probe.runtime.ts +4 -0
- package/src/matrix/probe.ts +97 -0
- package/src/matrix/profile.ts +184 -0
- package/src/matrix/reaction-common.ts +147 -0
- package/src/matrix/sdk/crypto-bootstrap.ts +438 -0
- package/src/matrix/sdk/crypto-facade.ts +242 -0
- package/src/matrix/sdk/crypto-node.runtime.ts +17 -0
- package/src/matrix/sdk/crypto-runtime.ts +14 -0
- package/src/matrix/sdk/decrypt-bridge.ts +410 -0
- package/src/matrix/sdk/event-helpers.ts +83 -0
- package/src/matrix/sdk/http-client.ts +87 -0
- package/src/matrix/sdk/idb-persistence-lock.ts +51 -0
- package/src/matrix/sdk/idb-persistence.ts +286 -0
- package/src/matrix/sdk/logger.ts +108 -0
- package/src/matrix/sdk/read-response-with-limit.ts +19 -0
- package/src/matrix/sdk/recovery-key-store.ts +453 -0
- package/src/matrix/sdk/timeout-abort-signal.ts +1 -0
- package/src/matrix/sdk/transport-runtime-api.ts +18 -0
- package/src/matrix/sdk/transport.ts +352 -0
- package/src/matrix/sdk/types.ts +245 -0
- package/src/matrix/sdk/verification-manager.ts +795 -0
- package/src/matrix/sdk/verification-status.ts +23 -0
- package/src/matrix/sdk.ts +2152 -0
- package/src/matrix/send/client.ts +93 -0
- package/src/matrix/send/formatting.ts +189 -0
- package/src/matrix/send/media.ts +244 -0
- package/src/matrix/send/targets.ts +104 -0
- package/src/matrix/send/types.ts +131 -0
- package/src/matrix/send.ts +660 -0
- package/src/matrix/session-store-metadata.ts +108 -0
- package/src/matrix/startup-abort.ts +44 -0
- package/src/matrix/subagent-hooks.ts +308 -0
- package/src/matrix/sync-state.ts +27 -0
- package/src/matrix/target-ids.ts +79 -0
- package/src/matrix/thread-bindings-shared.ts +206 -0
- package/src/matrix/thread-bindings.ts +580 -0
- package/src/matrix-migration.runtime.ts +9 -0
- package/src/migration-config.ts +243 -0
- package/src/migration-snapshot-backup.ts +116 -0
- package/src/migration-snapshot.ts +53 -0
- package/src/onboarding.ts +775 -0
- package/src/outbound.ts +248 -0
- package/src/plugin-entry.runtime.js +115 -0
- package/src/plugin-entry.runtime.ts +70 -0
- package/src/profile-update.ts +71 -0
- package/src/record-shared.ts +3 -0
- package/src/resolve-targets.ts +175 -0
- package/src/resolver.runtime.ts +5 -0
- package/src/resolver.ts +21 -0
- package/src/runtime-api.ts +106 -0
- package/src/runtime.ts +13 -0
- package/src/secret-contract.ts +174 -0
- package/src/session-route.ts +126 -0
- package/src/setup-bootstrap.ts +102 -0
- package/src/setup-config.ts +222 -0
- package/src/setup-contract.ts +90 -0
- package/src/setup-core.ts +146 -0
- package/src/setup-dm-policy.ts +15 -0
- package/src/setup-surface.ts +4 -0
- package/src/startup-maintenance.ts +114 -0
- package/src/storage-paths.ts +92 -0
- package/src/thread-binding-api.ts +23 -0
- package/src/tool-actions.runtime.ts +1 -0
- package/src/tool-actions.ts +498 -0
- package/src/types.ts +257 -0
- package/subagent-hooks-api.ts +31 -0
- package/test-api.ts +21 -0
- package/thread-binding-api.ts +4 -0
- package/thread-bindings-runtime.ts +4 -0
- package/tsconfig.json +16 -0
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
import type {
|
|
3
|
+
MatrixMessageAttachmentKind,
|
|
4
|
+
MatrixMessageAttachmentSummary,
|
|
5
|
+
} from "./actions/types.js";
|
|
6
|
+
|
|
7
|
+
const MATRIX_MEDIA_KINDS: Record<string, MatrixMessageAttachmentKind> = {
|
|
8
|
+
"m.audio": "audio",
|
|
9
|
+
"m.file": "file",
|
|
10
|
+
"m.image": "image",
|
|
11
|
+
"m.sticker": "sticker",
|
|
12
|
+
"m.video": "video",
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
function resolveMatrixMediaKind(msgtype: string | undefined): MatrixMessageAttachmentKind | null {
|
|
16
|
+
return MATRIX_MEDIA_KINDS[msgtype ?? ""] ?? null;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function resolveMatrixMediaLabel(
|
|
20
|
+
kind: MatrixMessageAttachmentKind | undefined,
|
|
21
|
+
fallback = "media",
|
|
22
|
+
): string {
|
|
23
|
+
return `${kind ?? fallback} attachment`;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function formatMatrixAttachmentMarker(params: {
|
|
27
|
+
kind?: MatrixMessageAttachmentKind;
|
|
28
|
+
tooLarge?: boolean;
|
|
29
|
+
unavailable?: boolean;
|
|
30
|
+
}): string {
|
|
31
|
+
const label = resolveMatrixMediaLabel(params.kind);
|
|
32
|
+
if (params.tooLarge) {
|
|
33
|
+
return `[matrix ${label} too large]`;
|
|
34
|
+
}
|
|
35
|
+
return params.unavailable ? `[matrix ${label} unavailable]` : `[matrix ${label}]`;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function isLikelyBareFilename(text: string): boolean {
|
|
39
|
+
const trimmed = text.trim();
|
|
40
|
+
if (!trimmed || trimmed.includes("\n") || /\s/.test(trimmed)) {
|
|
41
|
+
return false;
|
|
42
|
+
}
|
|
43
|
+
if (path.basename(trimmed) !== trimmed) {
|
|
44
|
+
return false;
|
|
45
|
+
}
|
|
46
|
+
return path.extname(trimmed).length > 1;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function resolveCaptionOrFilename(params: { body?: string; filename?: string }): {
|
|
50
|
+
caption?: string;
|
|
51
|
+
filename?: string;
|
|
52
|
+
} {
|
|
53
|
+
const body = params.body?.trim() ?? "";
|
|
54
|
+
const filename = params.filename?.trim() ?? "";
|
|
55
|
+
if (filename) {
|
|
56
|
+
if (!body || body === filename) {
|
|
57
|
+
return { filename };
|
|
58
|
+
}
|
|
59
|
+
return { caption: body, filename };
|
|
60
|
+
}
|
|
61
|
+
if (!body) {
|
|
62
|
+
return {};
|
|
63
|
+
}
|
|
64
|
+
if (isLikelyBareFilename(body)) {
|
|
65
|
+
return { filename: body };
|
|
66
|
+
}
|
|
67
|
+
return { caption: body };
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export function resolveMatrixMessageAttachment(params: {
|
|
71
|
+
body?: string;
|
|
72
|
+
filename?: string;
|
|
73
|
+
msgtype?: string;
|
|
74
|
+
}): MatrixMessageAttachmentSummary | undefined {
|
|
75
|
+
const kind = resolveMatrixMediaKind(params.msgtype);
|
|
76
|
+
if (!kind) {
|
|
77
|
+
return undefined;
|
|
78
|
+
}
|
|
79
|
+
const resolved = resolveCaptionOrFilename(params);
|
|
80
|
+
return {
|
|
81
|
+
kind,
|
|
82
|
+
caption: resolved.caption,
|
|
83
|
+
filename: resolved.filename,
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export function resolveMatrixMessageBody(params: {
|
|
88
|
+
body?: string;
|
|
89
|
+
filename?: string;
|
|
90
|
+
msgtype?: string;
|
|
91
|
+
}): string | undefined {
|
|
92
|
+
const attachment = resolveMatrixMessageAttachment(params);
|
|
93
|
+
if (!attachment) {
|
|
94
|
+
const body = params.body?.trim() ?? "";
|
|
95
|
+
return body || undefined;
|
|
96
|
+
}
|
|
97
|
+
return attachment.caption;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function formatMatrixAttachmentText(params: {
|
|
101
|
+
attachment?: MatrixMessageAttachmentSummary;
|
|
102
|
+
tooLarge?: boolean;
|
|
103
|
+
unavailable?: boolean;
|
|
104
|
+
}): string | undefined {
|
|
105
|
+
if (!params.attachment) {
|
|
106
|
+
return undefined;
|
|
107
|
+
}
|
|
108
|
+
return formatMatrixAttachmentMarker({
|
|
109
|
+
kind: params.attachment.kind,
|
|
110
|
+
tooLarge: params.tooLarge,
|
|
111
|
+
unavailable: params.unavailable,
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
export function formatMatrixMessageText(params: {
|
|
116
|
+
body?: string;
|
|
117
|
+
attachment?: MatrixMessageAttachmentSummary;
|
|
118
|
+
tooLarge?: boolean;
|
|
119
|
+
unavailable?: boolean;
|
|
120
|
+
}): string | undefined {
|
|
121
|
+
const body = params.body?.trim() ?? "";
|
|
122
|
+
const marker = formatMatrixAttachmentText({
|
|
123
|
+
attachment: params.attachment,
|
|
124
|
+
tooLarge: params.tooLarge,
|
|
125
|
+
unavailable: params.unavailable,
|
|
126
|
+
});
|
|
127
|
+
if (!marker) {
|
|
128
|
+
return body || undefined;
|
|
129
|
+
}
|
|
130
|
+
if (!body) {
|
|
131
|
+
return marker;
|
|
132
|
+
}
|
|
133
|
+
return `${body}\n\n${marker}`;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
export function formatMatrixMediaUnavailableText(params: {
|
|
137
|
+
body?: string;
|
|
138
|
+
filename?: string;
|
|
139
|
+
msgtype?: string;
|
|
140
|
+
}): string {
|
|
141
|
+
return (
|
|
142
|
+
formatMatrixMessageText({
|
|
143
|
+
body: resolveMatrixMessageBody(params),
|
|
144
|
+
attachment: resolveMatrixMessageAttachment(params),
|
|
145
|
+
unavailable: true,
|
|
146
|
+
}) ?? ""
|
|
147
|
+
);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
export function formatMatrixMediaTooLargeText(params: {
|
|
151
|
+
body?: string;
|
|
152
|
+
filename?: string;
|
|
153
|
+
msgtype?: string;
|
|
154
|
+
}): string {
|
|
155
|
+
return (
|
|
156
|
+
formatMatrixMessageText({
|
|
157
|
+
body: resolveMatrixMessageBody(params),
|
|
158
|
+
attachment: resolveMatrixMessageAttachment(params),
|
|
159
|
+
tooLarge: true,
|
|
160
|
+
}) ?? ""
|
|
161
|
+
);
|
|
162
|
+
}
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createChannelIngressResolver,
|
|
3
|
+
defineStableChannelIngressIdentity,
|
|
4
|
+
type ResolvedChannelMessageIngress,
|
|
5
|
+
} from "autobot/plugin-sdk/channel-ingress-runtime";
|
|
6
|
+
import { normalizeMatrixAllowList, resolveMatrixAllowListMatch } from "./allowlist.js";
|
|
7
|
+
|
|
8
|
+
type MatrixMonitorAccessState = {
|
|
9
|
+
effectiveGroupAllowFrom: string[];
|
|
10
|
+
effectiveRoomUsers: string[];
|
|
11
|
+
messageIngress: ResolvedChannelMessageIngress;
|
|
12
|
+
accountId: string;
|
|
13
|
+
senderId: string;
|
|
14
|
+
isRoom: boolean;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
function normalizeMatrixEntry(raw?: string | null): string | null {
|
|
18
|
+
return normalizeMatrixAllowList([raw ?? ""])[0] ?? null;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const matrixIngressIdentity = defineStableChannelIngressIdentity({
|
|
22
|
+
key: "sender-id",
|
|
23
|
+
normalize: normalizeMatrixEntry,
|
|
24
|
+
matchEntry({ subject, entry }) {
|
|
25
|
+
const senderId = subject.identifiers[0]?.value;
|
|
26
|
+
return (
|
|
27
|
+
entry.value === "*" ||
|
|
28
|
+
resolveMatrixAllowListMatch({
|
|
29
|
+
allowList: [entry.value],
|
|
30
|
+
userId: senderId ?? "",
|
|
31
|
+
}).allowed
|
|
32
|
+
);
|
|
33
|
+
},
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
function resolveMatrixGroupIngress(params: {
|
|
37
|
+
groupPolicy: "open" | "allowlist" | "disabled";
|
|
38
|
+
effectiveGroupAllowFrom: string[];
|
|
39
|
+
effectiveRoomUsers: string[];
|
|
40
|
+
}): { groupPolicy: "open" | "allowlist" | "disabled"; groupAllowFrom: string[] } {
|
|
41
|
+
if (params.groupPolicy === "disabled") {
|
|
42
|
+
return { groupPolicy: "disabled", groupAllowFrom: [] };
|
|
43
|
+
}
|
|
44
|
+
if (params.effectiveRoomUsers.length > 0) {
|
|
45
|
+
return { groupPolicy: "allowlist", groupAllowFrom: params.effectiveRoomUsers };
|
|
46
|
+
}
|
|
47
|
+
if (params.groupPolicy === "allowlist" && params.effectiveGroupAllowFrom.length > 0) {
|
|
48
|
+
return { groupPolicy: "allowlist", groupAllowFrom: params.effectiveGroupAllowFrom };
|
|
49
|
+
}
|
|
50
|
+
return { groupPolicy: "open", groupAllowFrom: [] };
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export async function resolveMatrixMonitorAccessState(params: {
|
|
54
|
+
allowFrom: Array<string | number>;
|
|
55
|
+
storeAllowFrom: Array<string | number>;
|
|
56
|
+
dmPolicy?: "open" | "pairing" | "allowlist" | "disabled";
|
|
57
|
+
groupPolicy?: "open" | "allowlist" | "disabled";
|
|
58
|
+
groupAllowFrom: Array<string | number>;
|
|
59
|
+
roomUsers: Array<string | number>;
|
|
60
|
+
senderId: string;
|
|
61
|
+
isRoom: boolean;
|
|
62
|
+
accountId?: string;
|
|
63
|
+
eventKind?: "message" | "reaction";
|
|
64
|
+
}): Promise<MatrixMonitorAccessState> {
|
|
65
|
+
const dmPolicy = params.dmPolicy ?? "pairing";
|
|
66
|
+
const groupPolicy = params.groupPolicy ?? "open";
|
|
67
|
+
const effectiveGroupAllowFrom = normalizeMatrixAllowList(params.groupAllowFrom);
|
|
68
|
+
const effectiveRoomUsers = normalizeMatrixAllowList(params.roomUsers);
|
|
69
|
+
const groupIngress = resolveMatrixGroupIngress({
|
|
70
|
+
groupPolicy,
|
|
71
|
+
effectiveGroupAllowFrom,
|
|
72
|
+
effectiveRoomUsers,
|
|
73
|
+
});
|
|
74
|
+
const accountId = params.accountId ?? "default";
|
|
75
|
+
const eventKind = params.eventKind ?? "message";
|
|
76
|
+
const ingress = createChannelIngressResolver({
|
|
77
|
+
channelId: "matrix",
|
|
78
|
+
accountId,
|
|
79
|
+
identity: matrixIngressIdentity,
|
|
80
|
+
readStoreAllowFrom: async () => params.storeAllowFrom,
|
|
81
|
+
});
|
|
82
|
+
const resolved = await ingress.message({
|
|
83
|
+
subject: { stableId: params.senderId },
|
|
84
|
+
conversation: {
|
|
85
|
+
kind: params.isRoom ? "group" : "direct",
|
|
86
|
+
id: params.isRoom ? "matrix-room" : "matrix-dm",
|
|
87
|
+
},
|
|
88
|
+
event: {
|
|
89
|
+
kind: eventKind,
|
|
90
|
+
authMode: "inbound" as const,
|
|
91
|
+
mayPair: params.isRoom ? false : eventKind === "message",
|
|
92
|
+
},
|
|
93
|
+
dmPolicy,
|
|
94
|
+
groupPolicy: params.isRoom ? groupIngress.groupPolicy : "disabled",
|
|
95
|
+
policy: { groupAllowFromFallbackToAllowFrom: false },
|
|
96
|
+
allowFrom: params.allowFrom,
|
|
97
|
+
...(params.isRoom ? { groupAllowFrom: groupIngress.groupAllowFrom } : {}),
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
return {
|
|
101
|
+
effectiveGroupAllowFrom,
|
|
102
|
+
effectiveRoomUsers,
|
|
103
|
+
messageIngress: resolved,
|
|
104
|
+
accountId,
|
|
105
|
+
senderId: params.senderId,
|
|
106
|
+
isRoom: params.isRoom,
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
export async function resolveMatrixMonitorCommandAccess(
|
|
111
|
+
state: MatrixMonitorAccessState,
|
|
112
|
+
params: {
|
|
113
|
+
useAccessGroups: boolean;
|
|
114
|
+
allowTextCommands: boolean;
|
|
115
|
+
hasControlCommand: boolean;
|
|
116
|
+
},
|
|
117
|
+
) {
|
|
118
|
+
const commandAllowFrom = state.isRoom ? [] : state.messageIngress.senderAccess.effectiveAllowFrom;
|
|
119
|
+
const commandGroupAllowFrom =
|
|
120
|
+
state.effectiveRoomUsers.length > 0 ? state.effectiveRoomUsers : state.effectiveGroupAllowFrom;
|
|
121
|
+
const resolved = await createChannelIngressResolver({
|
|
122
|
+
channelId: "matrix",
|
|
123
|
+
accountId: state.accountId,
|
|
124
|
+
identity: matrixIngressIdentity,
|
|
125
|
+
}).command({
|
|
126
|
+
subject: { stableId: state.senderId },
|
|
127
|
+
conversation: {
|
|
128
|
+
kind: state.isRoom ? "group" : "direct",
|
|
129
|
+
id: state.isRoom ? "matrix-room" : "matrix-dm",
|
|
130
|
+
},
|
|
131
|
+
dmPolicy: "allowlist",
|
|
132
|
+
groupPolicy: "allowlist",
|
|
133
|
+
policy: { groupAllowFromFallbackToAllowFrom: false },
|
|
134
|
+
allowFrom: commandAllowFrom,
|
|
135
|
+
groupAllowFrom: commandGroupAllowFrom,
|
|
136
|
+
command: {
|
|
137
|
+
useAccessGroups: params.useAccessGroups,
|
|
138
|
+
allowTextCommands: params.allowTextCommands,
|
|
139
|
+
hasControlCommand: params.hasControlCommand,
|
|
140
|
+
groupOwnerAllowFrom: "none",
|
|
141
|
+
commandGroupAllowFromFallbackToAllowFrom: false,
|
|
142
|
+
},
|
|
143
|
+
});
|
|
144
|
+
return resolved.commandAccess;
|
|
145
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { resolveAckReaction } from "autobot/plugin-sdk/channel-feedback";
|
|
2
|
+
import type { AutoBotConfig } from "autobot/plugin-sdk/config-contracts";
|
|
3
|
+
import type { CoreConfig } from "../../types.js";
|
|
4
|
+
import { resolveMatrixAccountConfig } from "../account-config.js";
|
|
5
|
+
|
|
6
|
+
type MatrixAckReactionScope = "group-mentions" | "group-all" | "direct" | "all" | "none" | "off";
|
|
7
|
+
|
|
8
|
+
export function resolveMatrixAckReactionConfig(params: {
|
|
9
|
+
cfg: AutoBotConfig;
|
|
10
|
+
agentId: string;
|
|
11
|
+
accountId?: string | null;
|
|
12
|
+
}): { ackReaction: string; ackReactionScope: MatrixAckReactionScope } {
|
|
13
|
+
const matrixConfig = params.cfg.channels?.matrix;
|
|
14
|
+
const accountConfig = resolveMatrixAccountConfig({
|
|
15
|
+
cfg: params.cfg as CoreConfig,
|
|
16
|
+
accountId: params.accountId,
|
|
17
|
+
});
|
|
18
|
+
const ackReaction = resolveAckReaction(params.cfg, params.agentId, {
|
|
19
|
+
channel: "matrix",
|
|
20
|
+
accountId: params.accountId ?? undefined,
|
|
21
|
+
}).trim();
|
|
22
|
+
const ackReactionScope = (accountConfig.ackReactionScope ??
|
|
23
|
+
matrixConfig?.ackReactionScope ??
|
|
24
|
+
params.cfg.messages?.ackReactionScope ??
|
|
25
|
+
"group-mentions") as MatrixAckReactionScope;
|
|
26
|
+
return { ackReaction, ackReactionScope };
|
|
27
|
+
}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import {
|
|
2
|
+
resolveAllowlistMatchByCandidates,
|
|
3
|
+
type AllowlistMatch,
|
|
4
|
+
} from "autobot/plugin-sdk/allow-from";
|
|
5
|
+
import { normalizeLowercaseStringOrEmpty } from "autobot/plugin-sdk/string-coerce-runtime";
|
|
6
|
+
import { normalizeStringEntries } from "autobot/plugin-sdk/string-normalization-runtime";
|
|
7
|
+
|
|
8
|
+
function normalizeAllowList(list?: Array<string | number>) {
|
|
9
|
+
return normalizeStringEntries(list);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
function normalizeMatrixUser(raw?: string | null): string {
|
|
13
|
+
const value = (raw ?? "").trim();
|
|
14
|
+
if (!value) {
|
|
15
|
+
return "";
|
|
16
|
+
}
|
|
17
|
+
if (!value.startsWith("@") || !value.includes(":")) {
|
|
18
|
+
return normalizeLowercaseStringOrEmpty(value);
|
|
19
|
+
}
|
|
20
|
+
const withoutAt = value.slice(1);
|
|
21
|
+
const splitIndex = withoutAt.indexOf(":");
|
|
22
|
+
if (splitIndex === -1) {
|
|
23
|
+
return normalizeLowercaseStringOrEmpty(value);
|
|
24
|
+
}
|
|
25
|
+
const localpart = normalizeLowercaseStringOrEmpty(withoutAt.slice(0, splitIndex));
|
|
26
|
+
const server = normalizeLowercaseStringOrEmpty(withoutAt.slice(splitIndex + 1));
|
|
27
|
+
if (!server) {
|
|
28
|
+
return normalizeLowercaseStringOrEmpty(value);
|
|
29
|
+
}
|
|
30
|
+
return `@${localpart}:${server}`;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export function normalizeMatrixUserId(raw?: string | null): string {
|
|
34
|
+
const trimmed = (raw ?? "").trim();
|
|
35
|
+
if (!trimmed) {
|
|
36
|
+
return "";
|
|
37
|
+
}
|
|
38
|
+
const lowered = normalizeLowercaseStringOrEmpty(trimmed);
|
|
39
|
+
if (lowered.startsWith("matrix:")) {
|
|
40
|
+
return normalizeMatrixUser(trimmed.slice("matrix:".length));
|
|
41
|
+
}
|
|
42
|
+
if (lowered.startsWith("user:")) {
|
|
43
|
+
return normalizeMatrixUser(trimmed.slice("user:".length));
|
|
44
|
+
}
|
|
45
|
+
return normalizeMatrixUser(trimmed);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function normalizeMatrixAllowListEntry(raw: string): string {
|
|
49
|
+
const trimmed = raw.trim();
|
|
50
|
+
if (!trimmed) {
|
|
51
|
+
return "";
|
|
52
|
+
}
|
|
53
|
+
if (trimmed === "*") {
|
|
54
|
+
return trimmed;
|
|
55
|
+
}
|
|
56
|
+
const lowered = normalizeLowercaseStringOrEmpty(trimmed);
|
|
57
|
+
if (lowered.startsWith("matrix:")) {
|
|
58
|
+
return `matrix:${normalizeMatrixUser(trimmed.slice("matrix:".length))}`;
|
|
59
|
+
}
|
|
60
|
+
if (lowered.startsWith("user:")) {
|
|
61
|
+
return `user:${normalizeMatrixUser(trimmed.slice("user:".length))}`;
|
|
62
|
+
}
|
|
63
|
+
return normalizeMatrixUser(trimmed);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export function normalizeMatrixAllowList(list?: Array<string | number>) {
|
|
67
|
+
return normalizeAllowList(list).map((entry) => normalizeMatrixAllowListEntry(entry));
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
type MatrixAllowListMatch = AllowlistMatch<"wildcard" | "id" | "prefixed-id" | "prefixed-user">;
|
|
71
|
+
|
|
72
|
+
type MatrixAllowListMatchSource = NonNullable<MatrixAllowListMatch["matchSource"]>;
|
|
73
|
+
|
|
74
|
+
export function resolveMatrixAllowListMatch(params: {
|
|
75
|
+
allowList: string[];
|
|
76
|
+
userId?: string;
|
|
77
|
+
}): MatrixAllowListMatch {
|
|
78
|
+
const allowList = params.allowList;
|
|
79
|
+
if (allowList.length === 0) {
|
|
80
|
+
return { allowed: false };
|
|
81
|
+
}
|
|
82
|
+
if (allowList.includes("*")) {
|
|
83
|
+
return { allowed: true, matchKey: "*", matchSource: "wildcard" };
|
|
84
|
+
}
|
|
85
|
+
const userId = normalizeMatrixUser(params.userId);
|
|
86
|
+
const candidates: Array<{ value?: string; source: MatrixAllowListMatchSource }> = [
|
|
87
|
+
{ value: userId, source: "id" },
|
|
88
|
+
{ value: userId ? `matrix:${userId}` : "", source: "prefixed-id" },
|
|
89
|
+
{ value: userId ? `user:${userId}` : "", source: "prefixed-user" },
|
|
90
|
+
];
|
|
91
|
+
return resolveAllowlistMatchByCandidates<MatrixAllowListMatchSource>({ allowList, candidates });
|
|
92
|
+
}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { normalizeStringifiedOptionalString } from "autobot/plugin-sdk/string-coerce-runtime";
|
|
2
|
+
import { getMatrixRuntime } from "../../runtime.js";
|
|
3
|
+
import type { MatrixConfig } from "../../types.js";
|
|
4
|
+
import type { MatrixClient } from "../sdk.js";
|
|
5
|
+
import type { RuntimeEnv } from "./runtime-api.js";
|
|
6
|
+
|
|
7
|
+
export function registerMatrixAutoJoin(params: {
|
|
8
|
+
client: MatrixClient;
|
|
9
|
+
accountConfig: Pick<MatrixConfig, "autoJoin" | "autoJoinAllowlist">;
|
|
10
|
+
runtime: RuntimeEnv;
|
|
11
|
+
}) {
|
|
12
|
+
const { client, accountConfig, runtime } = params;
|
|
13
|
+
const core = getMatrixRuntime();
|
|
14
|
+
const logVerbose = (message: string) => {
|
|
15
|
+
if (!core.logging.shouldLogVerbose()) {
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
runtime.log?.(message);
|
|
19
|
+
};
|
|
20
|
+
const autoJoin = accountConfig.autoJoin ?? "off";
|
|
21
|
+
const rawAllowlist = (accountConfig.autoJoinAllowlist ?? [])
|
|
22
|
+
.map((entry) => normalizeStringifiedOptionalString(entry))
|
|
23
|
+
.filter((entry): entry is string => Boolean(entry));
|
|
24
|
+
const autoJoinAllowlist = new Set(rawAllowlist);
|
|
25
|
+
const allowedRoomIds = new Set(rawAllowlist.filter((entry) => entry.startsWith("!")));
|
|
26
|
+
const allowedAliases = rawAllowlist.filter((entry) => entry.startsWith("#"));
|
|
27
|
+
const resolvedAliasRoomIds = new Map<string, string>();
|
|
28
|
+
|
|
29
|
+
if (autoJoin === "off") {
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
if (autoJoin === "always") {
|
|
34
|
+
logVerbose("matrix: auto-join enabled for all invites");
|
|
35
|
+
} else {
|
|
36
|
+
logVerbose("matrix: auto-join enabled for allowlist invites");
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const resolveAllowedAliasRoomId = async (alias: string): Promise<string | null> => {
|
|
40
|
+
if (resolvedAliasRoomIds.has(alias)) {
|
|
41
|
+
return resolvedAliasRoomIds.get(alias) ?? null;
|
|
42
|
+
}
|
|
43
|
+
const resolved = await params.client.resolveRoom(alias);
|
|
44
|
+
if (resolved) {
|
|
45
|
+
resolvedAliasRoomIds.set(alias, resolved);
|
|
46
|
+
}
|
|
47
|
+
return resolved;
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
const resolveAllowedAliasRoomIds = async (): Promise<string[]> => {
|
|
51
|
+
const resolved = await Promise.all(
|
|
52
|
+
allowedAliases.map(async (alias) => {
|
|
53
|
+
try {
|
|
54
|
+
return await resolveAllowedAliasRoomId(alias);
|
|
55
|
+
} catch (err) {
|
|
56
|
+
runtime.error?.(`matrix: failed resolving allowlisted alias ${alias}: ${String(err)}`);
|
|
57
|
+
return null;
|
|
58
|
+
}
|
|
59
|
+
}),
|
|
60
|
+
);
|
|
61
|
+
return resolved.filter((roomId): roomId is string => Boolean(roomId));
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
// Handle invites directly so both "always" and "allowlist" modes share the same path.
|
|
65
|
+
client.on("room.invite", async (roomId: string, _inviteEvent: unknown) => {
|
|
66
|
+
if (autoJoin === "allowlist") {
|
|
67
|
+
const allowedAliasRoomIds = await resolveAllowedAliasRoomIds();
|
|
68
|
+
const allowed =
|
|
69
|
+
autoJoinAllowlist.has("*") ||
|
|
70
|
+
allowedRoomIds.has(roomId) ||
|
|
71
|
+
allowedAliasRoomIds.some((resolvedRoomId) => resolvedRoomId === roomId);
|
|
72
|
+
|
|
73
|
+
if (!allowed) {
|
|
74
|
+
logVerbose(`matrix: invite ignored (not in allowlist) room=${roomId}`);
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
try {
|
|
80
|
+
await client.joinRoom(roomId);
|
|
81
|
+
logVerbose(`matrix: joined room ${roomId}`);
|
|
82
|
+
} catch (err) {
|
|
83
|
+
runtime.error?.(`matrix: failed to join room ${roomId}: ${String(err)}`);
|
|
84
|
+
}
|
|
85
|
+
});
|
|
86
|
+
}
|