@stage-labs/metro 0.1.0-beta.21 → 0.1.0-beta.210
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/dist/api.js +9 -101
- package/dist/claude-settings.js +40 -0
- package/dist/claude.js +152 -0
- package/dist/cli.js +52 -109
- package/dist/control.js +47 -75
- package/dist/hold.js +210 -0
- package/dist/local.js +43 -0
- package/dist/mcp-config.js +25 -0
- package/dist/node-name.js +71 -0
- package/dist/onboarding.js +44 -0
- package/dist/provider-flags.js +9 -0
- package/dist/route.js +58 -0
- package/dist/runtime-install.js +96 -0
- package/dist/runtime.js +29 -52
- package/dist/serve.js +157 -0
- package/dist/service.js +304 -0
- package/dist/tail.js +119 -0
- package/dist/update.js +13 -1
- package/package.json +2 -23
- package/runtime/marketplace/.claude-plugin/marketplace.json +12 -0
- package/runtime/marketplace/plugin/.claude-plugin/plugin.json +17 -0
- package/runtime/marketplace/plugin/.mcp.json +1 -0
- package/runtime/marketplace/plugin/bin/guard.mjs +71 -0
- package/runtime/marketplace/plugin/bin/metro-plugin.mjs +30 -0
- package/runtime/marketplace/plugin/bin/policy.mjs +97 -0
- package/runtime/marketplace/plugin/bin/session-start.mjs +31 -0
- package/runtime/marketplace/plugin/hooks/hooks.json +25 -0
- package/runtime/marketplace/plugin/orchestrator.md +26 -0
- package/runtime/node_modules/@metro-labs/core/package.json +47 -0
- package/runtime/node_modules/@metro-labs/core/src/endpoints.ts +7 -0
- package/runtime/node_modules/@metro-labs/{mcp/src/daemon → core/src}/events.ts +11 -7
- package/runtime/node_modules/@metro-labs/{mcp/src/db → core/src}/ids.ts +5 -0
- package/runtime/node_modules/@metro-labs/core/src/is-record.ts +2 -0
- package/runtime/node_modules/@metro-labs/{mcp/src/stations → core/src}/lines.ts +24 -28
- package/runtime/node_modules/@metro-labs/{mcp/src/daemon → core/src}/protocol.ts +7 -4
- package/runtime/node_modules/@metro-labs/{mcp/src/daemon → core/src}/secure-fs.ts +17 -0
- package/runtime/node_modules/@metro-labs/core/src/station-names.ts +13 -0
- package/runtime/node_modules/@metro-labs/core/src/stations/account-files.ts +41 -0
- package/runtime/node_modules/@metro-labs/core/src/stations/account-store.ts +83 -0
- package/runtime/node_modules/@metro-labs/{mcp → core}/src/stations/attachments.ts +0 -1
- package/runtime/node_modules/@metro-labs/core/src/stations/messaging-normalize.ts +16 -0
- package/runtime/node_modules/@metro-labs/core/src/stations/profile.ts +58 -0
- package/runtime/node_modules/@metro-labs/core/src/stations/sender-profile.ts +64 -0
- package/runtime/node_modules/@metro-labs/{mcp → core}/src/stations/station-runtime.ts +6 -11
- package/runtime/node_modules/@metro-labs/core/src/stations/train-boot.ts +42 -0
- package/runtime/node_modules/@metro-labs/core/src/stations/train-events.ts +84 -0
- package/runtime/node_modules/@metro-labs/{mcp → core}/src/stations/types.ts +18 -1
- package/runtime/node_modules/@metro-labs/core/src/tickets.ts +37 -0
- package/runtime/node_modules/@metro-labs/core/src/version.ts +2 -0
- package/runtime/node_modules/@metro-labs/daemon/package.json +48 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agent-user/agent-fs.ts +134 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agent-user/files-api.ts +12 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agent-user/files.ts +89 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agent-user/home-fs.ts +113 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agent-user/job-env.ts +51 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agent-user/provision.ts +80 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agent-user/schedule-detail.ts +99 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agent-user/schedules-api.ts +27 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agent-user/schedules.ts +144 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agent-user/unit-files.ts +37 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agent-user/user.ts +89 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agent-user/view.ts +87 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/account-attach.ts +19 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/account-lookups.ts +55 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/account-routes.ts +76 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/accounts-api.ts +357 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/admin.ts +30 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/allowlist.ts +59 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/api.ts +131 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/bundle.ts +132 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/file-admin.ts +305 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/files.ts +142 -0
- package/runtime/node_modules/@metro-labs/{mcp/src/db/key-map.ts → daemon/src/agents/keys.ts} +1 -11
- package/runtime/node_modules/@metro-labs/{mcp/src/db/agent-map.ts → daemon/src/agents/map.ts} +58 -1
- package/runtime/node_modules/@metro-labs/daemon/src/agents/policy-route.ts +27 -0
- package/runtime/node_modules/@metro-labs/{mcp/src/db/agent-scope.ts → daemon/src/agents/scope.ts} +2 -2
- package/runtime/node_modules/@metro-labs/daemon/src/agents/sender-cards.ts +94 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/senders.ts +38 -0
- package/runtime/node_modules/@metro-labs/daemon/src/approvals/api.ts +55 -0
- package/runtime/node_modules/@metro-labs/daemon/src/approvals/pending.ts +150 -0
- package/runtime/node_modules/@metro-labs/daemon/src/approvals/preview.ts +66 -0
- package/runtime/node_modules/@metro-labs/daemon/src/boot/boot.ts +218 -0
- package/runtime/node_modules/@metro-labs/daemon/src/boot/connect-hint.ts +36 -0
- package/runtime/node_modules/@metro-labs/{mcp/src/daemon → daemon/src/boot}/crash-guard.ts +1 -1
- package/runtime/node_modules/@metro-labs/daemon/src/boot/local-owner.ts +21 -0
- package/runtime/node_modules/@metro-labs/{mcp/src/daemon → daemon/src/boot}/paths.ts +6 -34
- package/runtime/node_modules/@metro-labs/daemon/src/channels/addressed.ts +26 -0
- package/runtime/node_modules/@metro-labs/{mcp → daemon}/src/channels/inbound.ts +70 -45
- package/runtime/node_modules/@metro-labs/{mcp → daemon}/src/channels/media-note.ts +11 -7
- package/runtime/node_modules/@metro-labs/{mcp → daemon}/src/channels/pending.ts +4 -1
- package/runtime/node_modules/@metro-labs/{mcp → daemon}/src/channels/relay.ts +2 -2
- package/runtime/node_modules/@metro-labs/daemon/src/claude/api.ts +355 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/files.ts +359 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/login.ts +208 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/memory.ts +32 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/onboarding.ts +60 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/plugin-install.ts +149 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/session-files.ts +75 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/session.ts +284 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/settings.ts +109 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/setup.ts +273 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/skills.ts +120 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/version.ts +113 -0
- package/runtime/node_modules/@metro-labs/{mcp/src/daemon/connector-api.ts → daemon/src/connectors/api.ts} +79 -113
- package/runtime/node_modules/@metro-labs/{mcp/src/db/connector-config.ts → daemon/src/connectors/config.ts} +67 -41
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/gates.ts +72 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/health.ts +18 -0
- package/runtime/node_modules/@metro-labs/{mcp/src/daemon → daemon/src/connectors}/oauth-client.ts +14 -17
- package/runtime/node_modules/@metro-labs/{mcp/src/daemon → daemon/src/connectors}/oauth-discovery.ts +33 -20
- package/runtime/node_modules/@metro-labs/{mcp/src/daemon/connector-oauth-routes.ts → daemon/src/connectors/oauth-routes.ts} +30 -32
- package/runtime/node_modules/@metro-labs/{mcp/src/daemon/connector-oauth.ts → daemon/src/connectors/oauth.ts} +61 -22
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/plugin-sync.ts +131 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/reach.ts +27 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/relay-policy.ts +90 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/relay.ts +347 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/store.ts +385 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/tools.ts +81 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/url.ts +37 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/verify.ts +106 -0
- package/runtime/node_modules/@metro-labs/{mcp/src/daemon → daemon/src/files}/attach-grant.ts +4 -4
- package/runtime/node_modules/@metro-labs/{mcp/src/daemon → daemon/src/files}/attach-owner.ts +3 -3
- package/runtime/node_modules/@metro-labs/daemon/src/files/attach-reaper.ts +174 -0
- package/runtime/node_modules/@metro-labs/{mcp/src/daemon → daemon/src/files}/attach-serve.ts +12 -13
- package/runtime/node_modules/@metro-labs/{mcp/src/daemon → daemon/src/files}/upload-api.ts +10 -27
- package/runtime/node_modules/@metro-labs/{mcp/src/daemon → daemon/src/files}/upload-store.ts +4 -4
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/bedrock.ts +233 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/codex-auth.ts +208 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/codex-device.ts +91 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/codex-stream.ts +173 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/codex-translate.ts +195 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/codex-version.ts +44 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/codex.ts +128 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/effort.ts +77 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/eventstream.ts +123 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/forward.ts +237 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/frames.ts +118 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/gateway.ts +274 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/gemini-auth.ts +160 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/gemini-client.ts +46 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/gemini-setup.ts +150 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/gemini-stream.ts +139 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/gemini-translate.ts +224 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/gemini.ts +208 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/model-api.ts +199 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/model-config.ts +278 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/model-signin.ts +193 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/model-store.ts +70 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/openrouter.ts +90 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/pkce.ts +36 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/provider-models.ts +66 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/served.ts +18 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/subscription.ts +120 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/text.ts +14 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/usage.ts +244 -0
- package/runtime/node_modules/@metro-labs/{mcp → daemon}/src/mcp/accounts.ts +28 -17
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/attachment-schema.ts +91 -0
- package/runtime/node_modules/@metro-labs/{mcp → daemon}/src/mcp/call-tools.ts +49 -20
- package/runtime/node_modules/@metro-labs/{mcp → daemon}/src/mcp/ctx.ts +4 -4
- package/runtime/node_modules/@metro-labs/{mcp → daemon}/src/mcp/event-store.ts +15 -16
- package/runtime/node_modules/@metro-labs/{mcp → daemon}/src/mcp/group-tools.ts +2 -2
- package/runtime/node_modules/@metro-labs/{mcp → daemon}/src/mcp/index.ts +24 -79
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/instructions.ts +19 -0
- package/runtime/node_modules/@metro-labs/{mcp → daemon}/src/mcp/member-tools.ts +2 -2
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/permission-prompt.ts +87 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/permission-relay.ts +79 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/policy-gate.ts +82 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/policy-snapshot.ts +79 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/profile-lookup.ts +60 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/profile-tool.ts +96 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/read-tool.ts +87 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/request-identity.ts +46 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/session-slot.ts +48 -0
- package/runtime/node_modules/@metro-labs/{mcp → daemon}/src/mcp/session.ts +33 -42
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/tool-catalog.ts +88 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/tool-def.ts +9 -0
- package/runtime/node_modules/@metro-labs/{mcp → daemon}/src/mcp/tool-dispatch.ts +53 -40
- package/runtime/node_modules/@metro-labs/{mcp → daemon}/src/mcp/tool-schemas.ts +68 -119
- package/runtime/node_modules/@metro-labs/{mcp → daemon}/src/mcp/upload-tool.ts +6 -11
- package/runtime/node_modules/@metro-labs/daemon/src/metro-user/helper-script.ts +126 -0
- package/runtime/node_modules/@metro-labs/daemon/src/metro-user/install.ts +37 -0
- package/runtime/node_modules/@metro-labs/daemon/src/metro-user/privilege.ts +48 -0
- package/runtime/node_modules/@metro-labs/daemon/src/monitor/api.ts +132 -0
- package/runtime/node_modules/@metro-labs/daemon/src/net/tunnel.ts +316 -0
- package/runtime/node_modules/@metro-labs/daemon/src/policy/policy.ts +118 -0
- package/runtime/node_modules/@metro-labs/daemon/src/routes/bearer.ts +35 -0
- package/runtime/node_modules/@metro-labs/daemon/src/routes/body.ts +24 -0
- package/runtime/node_modules/@metro-labs/{mcp/src/daemon → daemon/src/routes}/http.ts +59 -51
- package/runtime/node_modules/@metro-labs/daemon/src/routes/local-mode.ts +205 -0
- package/runtime/node_modules/@metro-labs/daemon/src/routes/session-apis.ts +66 -0
- package/runtime/node_modules/@metro-labs/daemon/src/routes/session.ts +29 -0
- package/runtime/node_modules/@metro-labs/daemon/src/routes/threema-callback.ts +103 -0
- package/runtime/node_modules/@metro-labs/daemon/src/server/control.ts +39 -0
- package/runtime/node_modules/@metro-labs/daemon/src/server/machine.ts +58 -0
- package/runtime/node_modules/@metro-labs/daemon/src/server/owner.ts +26 -0
- package/runtime/node_modules/@metro-labs/daemon/src/server/update.ts +90 -0
- package/runtime/node_modules/@metro-labs/daemon/src/server.ts +2 -0
- package/runtime/node_modules/@metro-labs/daemon/src/stations/attach-driver.ts +42 -0
- package/runtime/node_modules/@metro-labs/{mcp → daemon}/src/stations/attach-inline.ts +29 -1
- package/runtime/node_modules/@metro-labs/{mcp → daemon}/src/stations/attach-interactive.ts +9 -36
- package/runtime/node_modules/@metro-labs/daemon/src/stations/attach-outlook.ts +63 -0
- package/runtime/node_modules/@metro-labs/{mcp → daemon}/src/stations/attach-resolve.ts +38 -13
- package/runtime/node_modules/@metro-labs/{mcp/src/daemon → daemon/src/stations}/attach-session.ts +32 -22
- package/runtime/node_modules/@metro-labs/{mcp → daemon}/src/stations/attach-xmtp.ts +8 -9
- package/runtime/node_modules/@metro-labs/{mcp → daemon}/src/stations/attach.ts +75 -9
- package/runtime/node_modules/@metro-labs/daemon/src/stations/materialize.ts +265 -0
- package/runtime/node_modules/@metro-labs/{mcp → daemon}/src/stations/registry.ts +22 -6
- package/runtime/node_modules/@metro-labs/daemon/src/stations/runtime-deps.ts +82 -0
- package/runtime/node_modules/@metro-labs/{mcp/src/daemon → daemon/src/stations}/supervisor-io.ts +3 -3
- package/runtime/node_modules/@metro-labs/{mcp/src/daemon → daemon/src/stations}/supervisor.ts +6 -9
- package/runtime/node_modules/@metro-labs/daemon/src/stations/threema-callbacks.ts +32 -0
- package/runtime/node_modules/@metro-labs/{mcp/src/daemon → daemon/src/stations}/train-call.ts +1 -1
- package/runtime/node_modules/@metro-labs/daemon/src/stations/webhook-endpoints.ts +51 -0
- package/runtime/node_modules/@metro-labs/daemon/src/terminal/api.ts +84 -0
- package/runtime/node_modules/@metro-labs/daemon/src/terminal/socket.ts +124 -0
- package/runtime/node_modules/@metro-labs/daemon/src/terminal/tickets.ts +15 -0
- package/runtime/node_modules/@metro-labs/daemon/src/vault/api.ts +45 -0
- package/runtime/node_modules/@metro-labs/daemon/src/vault/config.ts +51 -0
- package/runtime/node_modules/@metro-labs/daemon/src/vault/firewall.ts +9 -0
- package/runtime/node_modules/@metro-labs/daemon/src/vault/index.ts +96 -0
- package/runtime/node_modules/@metro-labs/daemon/src/vault/install.ts +56 -0
- package/runtime/node_modules/@metro-labs/daemon/src/vault/paths.ts +26 -0
- package/runtime/node_modules/@metro-labs/daemon/src/vault/proxy.ts +147 -0
- package/runtime/node_modules/@metro-labs/daemon/src/vault/store.ts +129 -0
- package/runtime/node_modules/@metro-labs/daemon/src/vault/trust.ts +44 -0
- package/runtime/node_modules/@metro-labs/{discord → discord-bot}/package.json +3 -5
- package/runtime/node_modules/@metro-labs/{discord → discord-bot}/src/accounts.ts +8 -28
- package/runtime/node_modules/@metro-labs/{discord → discord-bot}/src/actions.ts +19 -187
- package/runtime/node_modules/@metro-labs/{discord → discord-bot}/src/attachments.ts +3 -3
- package/runtime/node_modules/@metro-labs/{discord → discord-bot}/src/format.ts +31 -72
- package/runtime/node_modules/@metro-labs/{discord → discord-bot}/src/group-actions.ts +2 -2
- package/runtime/node_modules/@metro-labs/{discord → discord-bot}/src/group.ts +2 -2
- package/runtime/node_modules/@metro-labs/{discord → discord-bot}/src/index.ts +15 -21
- package/runtime/node_modules/@metro-labs/{discord → discord-bot}/src/members.ts +2 -2
- package/runtime/node_modules/@metro-labs/discord-bot/src/profile.ts +68 -0
- package/runtime/node_modules/@metro-labs/{discord → discord-bot}/src/send-files.ts +12 -13
- package/runtime/node_modules/@metro-labs/discord-bot/src/station.ts +23 -0
- package/runtime/node_modules/@metro-labs/http/package.json +29 -0
- package/runtime/node_modules/@metro-labs/http/src/api-http.ts +135 -0
- package/runtime/node_modules/@metro-labs/{mcp/src/daemon/session-api.ts → http/src/mode-api.ts} +13 -9
- package/runtime/node_modules/@metro-labs/http/src/return-to.ts +21 -0
- package/runtime/node_modules/@metro-labs/http/src/workos-token.ts +149 -0
- package/runtime/node_modules/@metro-labs/{telegram-user → outlook}/package.json +2 -5
- package/runtime/node_modules/@metro-labs/outlook/src/accounts.ts +93 -0
- package/runtime/node_modules/@metro-labs/outlook/src/actions.ts +14 -0
- package/runtime/node_modules/@metro-labs/outlook/src/attachments.ts +97 -0
- package/runtime/node_modules/@metro-labs/outlook/src/auth.ts +150 -0
- package/runtime/node_modules/@metro-labs/outlook/src/browser.ts +53 -0
- package/runtime/node_modules/@metro-labs/outlook/src/config.ts +36 -0
- package/runtime/node_modules/@metro-labs/outlook/src/format.ts +140 -0
- package/runtime/node_modules/@metro-labs/outlook/src/graph.ts +60 -0
- package/runtime/node_modules/@metro-labs/outlook/src/html.ts +72 -0
- package/runtime/node_modules/@metro-labs/outlook/src/inbound.ts +104 -0
- package/runtime/node_modules/@metro-labs/outlook/src/index.ts +12 -0
- package/runtime/node_modules/@metro-labs/outlook/src/login.ts +150 -0
- package/runtime/node_modules/@metro-labs/outlook/src/me.ts +41 -0
- package/runtime/node_modules/@metro-labs/outlook/src/outbound.ts +116 -0
- package/runtime/node_modules/@metro-labs/outlook/src/query.ts +119 -0
- package/runtime/node_modules/@metro-labs/outlook/src/state.ts +43 -0
- package/runtime/node_modules/@metro-labs/outlook/src/station.ts +15 -0
- package/runtime/node_modules/@metro-labs/outlook/src/trust.ts +70 -0
- package/runtime/node_modules/@metro-labs/telegram/package.json +3 -2
- package/runtime/node_modules/@metro-labs/telegram/src/accounts.ts +38 -96
- package/runtime/node_modules/@metro-labs/telegram/src/actions.ts +214 -253
- package/runtime/node_modules/@metro-labs/telegram/src/attachments.ts +67 -108
- package/runtime/node_modules/@metro-labs/{telegram-user → telegram}/src/client.ts +6 -6
- package/runtime/node_modules/@metro-labs/telegram/src/format.ts +101 -147
- package/runtime/node_modules/@metro-labs/{telegram-user → telegram}/src/group.ts +4 -4
- package/runtime/node_modules/@metro-labs/{telegram-user → telegram}/src/history.ts +1 -1
- package/runtime/node_modules/@metro-labs/telegram/src/inbound.ts +57 -0
- package/runtime/node_modules/@metro-labs/telegram/src/index.ts +6 -102
- package/runtime/node_modules/@metro-labs/{telegram-user → telegram}/src/login.ts +20 -20
- package/runtime/node_modules/@metro-labs/telegram/src/media-actions.ts +64 -151
- package/runtime/node_modules/@metro-labs/telegram/src/members.ts +61 -33
- package/runtime/node_modules/@metro-labs/telegram/src/profile.ts +70 -0
- package/runtime/node_modules/@metro-labs/{telegram-user → telegram}/src/reactions.ts +3 -3
- package/runtime/node_modules/@metro-labs/telegram/src/station.ts +11 -1
- package/runtime/node_modules/@metro-labs/telegram/src/types.ts +5 -47
- package/runtime/node_modules/@metro-labs/telegram-bot/package.json +25 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/accounts.ts +86 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/actions.ts +240 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/attachments.ts +128 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/format.ts +170 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/index.ts +103 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/media-actions.ts +90 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/members.ts +47 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/profile.ts +85 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/station.ts +21 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/types.ts +56 -0
- package/runtime/node_modules/@metro-labs/threema/package.json +26 -0
- package/runtime/node_modules/@metro-labs/threema/src/accounts.ts +95 -0
- package/runtime/node_modules/@metro-labs/threema/src/actions.ts +124 -0
- package/runtime/node_modules/@metro-labs/threema/src/api.ts +152 -0
- package/runtime/node_modules/@metro-labs/threema/src/crypto.ts +100 -0
- package/runtime/node_modules/@metro-labs/threema/src/files.ts +47 -0
- package/runtime/node_modules/@metro-labs/threema/src/format.ts +164 -0
- package/runtime/node_modules/@metro-labs/threema/src/groups.ts +96 -0
- package/runtime/node_modules/@metro-labs/threema/src/ids.ts +21 -0
- package/runtime/node_modules/@metro-labs/threema/src/inbound.ts +89 -0
- package/runtime/node_modules/@metro-labs/threema/src/index.ts +24 -0
- package/runtime/node_modules/@metro-labs/threema/src/messages.ts +264 -0
- package/runtime/node_modules/@metro-labs/threema/src/outbound.ts +160 -0
- package/runtime/node_modules/@metro-labs/threema/src/station.ts +13 -0
- package/runtime/node_modules/@metro-labs/threema/src/verify.ts +117 -0
- package/runtime/node_modules/@metro-labs/webhook/package.json +1 -1
- package/runtime/node_modules/@metro-labs/webhook/src/station.ts +5 -8
- package/runtime/node_modules/@metro-labs/whatsapp/package.json +2 -5
- package/runtime/node_modules/@metro-labs/whatsapp/src/accounts.ts +6 -28
- package/runtime/node_modules/@metro-labs/whatsapp/src/ack.ts +1 -1
- package/runtime/node_modules/@metro-labs/whatsapp/src/actions.ts +54 -11
- package/runtime/node_modules/@metro-labs/whatsapp/src/attachments.ts +2 -2
- package/runtime/node_modules/@metro-labs/whatsapp/src/auth-state.ts +1 -1
- package/runtime/node_modules/@metro-labs/whatsapp/src/client.ts +86 -4
- package/runtime/node_modules/@metro-labs/whatsapp/src/format.ts +7 -58
- package/runtime/node_modules/@metro-labs/whatsapp/src/inbound.ts +11 -28
- package/runtime/node_modules/@metro-labs/whatsapp/src/index.ts +4 -37
- package/runtime/node_modules/@metro-labs/whatsapp/src/keys.ts +1 -1
- package/runtime/node_modules/@metro-labs/whatsapp/src/login.ts +1 -1
- package/runtime/node_modules/@metro-labs/whatsapp/src/media.ts +1 -1
- package/runtime/node_modules/@metro-labs/whatsapp/src/names.ts +75 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/parse.ts +44 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/resolve.ts +52 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/station.ts +16 -1
- package/runtime/node_modules/@metro-labs/whatsapp/src/token-store.ts +5 -9
- package/runtime/node_modules/@metro-labs/whatsapp/src/types.ts +0 -1
- package/runtime/node_modules/@metro-labs/xmtp/package.json +3 -2
- package/runtime/node_modules/@metro-labs/xmtp/src/accounts.ts +10 -20
- package/runtime/node_modules/@metro-labs/xmtp/src/actions-close.ts +2 -2
- package/runtime/node_modules/@metro-labs/xmtp/src/actions-conv.ts +15 -204
- package/runtime/node_modules/@metro-labs/xmtp/src/actions-meta.ts +4 -43
- package/runtime/node_modules/@metro-labs/xmtp/src/actions.ts +41 -219
- package/runtime/node_modules/@metro-labs/xmtp/src/attachments.ts +3 -3
- package/runtime/node_modules/@metro-labs/xmtp/src/conv-helpers.ts +3 -3
- package/runtime/node_modules/@metro-labs/xmtp/src/emit-payloads.ts +7 -24
- package/runtime/node_modules/@metro-labs/xmtp/src/emit.ts +8 -18
- package/runtime/node_modules/@metro-labs/xmtp/src/group-members.ts +1 -1
- package/runtime/node_modules/@metro-labs/xmtp/src/group.ts +2 -2
- package/runtime/node_modules/@metro-labs/xmtp/src/identity.ts +30 -16
- package/runtime/node_modules/@metro-labs/xmtp/src/index.ts +12 -20
- package/runtime/node_modules/@metro-labs/xmtp/src/labels.ts +1 -19
- package/runtime/node_modules/@metro-labs/xmtp/src/member-args.ts +1 -1
- package/runtime/node_modules/@metro-labs/xmtp/src/names.ts +61 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/profile.ts +153 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/sender.ts +112 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/smart.ts +72 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/station.ts +5 -1
- package/runtime/node_modules/@metro-labs/xmtp/src/tools-handlers.ts +4 -51
- package/runtime/node_modules/@metro-labs/xmtp/src/tools.ts +30 -44
- package/runtime/node_modules/@metro-labs/xmtp/src/transcribe.ts +4 -6
- package/runtime/node_modules/@metro-labs/xmtp/src/verify.ts +19 -5
- package/runtime/node_modules/@metro-labs/xmtp/src/wire.ts +14 -4
- package/runtime/runtime.json +1 -1
- package/runtime/server.ts +1 -0
- package/runtime/stations.json +37 -0
- package/dist/prompt.js +0 -10
- package/dist/store.js +0 -70
- package/runtime/node_modules/@metro-labs/discord/src/station.ts +0 -19
- package/runtime/node_modules/@metro-labs/discord/src/voice-speak.ts +0 -195
- package/runtime/node_modules/@metro-labs/discord/src/voice-transcribe.ts +0 -292
- package/runtime/node_modules/@metro-labs/discord/src/voice.ts +0 -197
- package/runtime/node_modules/@metro-labs/discord/src/wire.ts +0 -5
- package/runtime/node_modules/@metro-labs/mcp/package.json +0 -66
- package/runtime/node_modules/@metro-labs/mcp/src/channels/dedupe.ts +0 -24
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/account-api.ts +0 -311
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/agent-api.ts +0 -335
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/api-http.ts +0 -129
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/boot.ts +0 -322
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/cli-pair-api.ts +0 -111
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/cli-pair.ts +0 -20
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/collection-api.ts +0 -202
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/connector-json.ts +0 -31
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/connector-tools.ts +0 -100
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/connector-url.ts +0 -57
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/connector-verify.ts +0 -333
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/google-auth.ts +0 -164
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/google-oauth.ts +0 -208
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/identity.ts +0 -79
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/oauth-pending.ts +0 -65
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/pair-codes.ts +0 -58
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/project-api.ts +0 -214
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/run-api.ts +0 -125
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/run-pair.ts +0 -18
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/runtime-source.ts +0 -105
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/session-apis.ts +0 -41
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/session.ts +0 -218
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/tunnel.ts +0 -169
- package/runtime/node_modules/@metro-labs/mcp/src/db/account-attach.ts +0 -91
- package/runtime/node_modules/@metro-labs/mcp/src/db/agent-admin.ts +0 -215
- package/runtime/node_modules/@metro-labs/mcp/src/db/client.ts +0 -37
- package/runtime/node_modules/@metro-labs/mcp/src/db/connector-collections.ts +0 -218
- package/runtime/node_modules/@metro-labs/mcp/src/db/connectors.ts +0 -329
- package/runtime/node_modules/@metro-labs/mcp/src/db/materialize.ts +0 -364
- package/runtime/node_modules/@metro-labs/mcp/src/db/projects.ts +0 -300
- package/runtime/node_modules/@metro-labs/mcp/src/db/runtime-admin.ts +0 -31
- package/runtime/node_modules/@metro-labs/mcp/src/db/runtimes.ts +0 -105
- package/runtime/node_modules/@metro-labs/mcp/src/db/schema.ts +0 -131
- package/runtime/node_modules/@metro-labs/mcp/src/db/token-carry.ts +0 -45
- package/runtime/node_modules/@metro-labs/mcp/src/db/users.ts +0 -57
- package/runtime/node_modules/@metro-labs/mcp/src/db/whatsapp-login.ts +0 -18
- package/runtime/node_modules/@metro-labs/mcp/src/mcp/channel-owner.ts +0 -29
- package/runtime/node_modules/@metro-labs/mcp/src/mcp/frame-scope.ts +0 -21
- package/runtime/node_modules/@metro-labs/mcp/src/mcp/permission-relay.ts +0 -66
- package/runtime/node_modules/@metro-labs/mcp/src/mcp/request-identity.ts +0 -73
- package/runtime/node_modules/@metro-labs/mcp/src/mcp/session-registry.ts +0 -172
- package/runtime/node_modules/@metro-labs/mcp/src/mcp/session-route.ts +0 -43
- package/runtime/node_modules/@metro-labs/mcp/src/monitor/api.ts +0 -240
- package/runtime/node_modules/@metro-labs/mcp/src/server.ts +0 -2
- package/runtime/node_modules/@metro-labs/mcp/src/stations/account-store.ts +0 -80
- package/runtime/node_modules/@metro-labs/mcp/src/stations/messaging-normalize.ts +0 -88
- package/runtime/node_modules/@metro-labs/telegram/src/wire.ts +0 -3
- package/runtime/node_modules/@metro-labs/telegram-user/src/accounts.ts +0 -81
- package/runtime/node_modules/@metro-labs/telegram-user/src/actions.ts +0 -254
- package/runtime/node_modules/@metro-labs/telegram-user/src/attachments.ts +0 -87
- package/runtime/node_modules/@metro-labs/telegram-user/src/format.ts +0 -141
- package/runtime/node_modules/@metro-labs/telegram-user/src/inbound.ts +0 -78
- package/runtime/node_modules/@metro-labs/telegram-user/src/index.ts +0 -46
- package/runtime/node_modules/@metro-labs/telegram-user/src/media-actions.ts +0 -80
- package/runtime/node_modules/@metro-labs/telegram-user/src/members.ts +0 -75
- package/runtime/node_modules/@metro-labs/telegram-user/src/normalize.ts +0 -22
- package/runtime/node_modules/@metro-labs/telegram-user/src/station.ts +0 -24
- package/runtime/node_modules/@metro-labs/telegram-user/src/types.ts +0 -7
- package/runtime/node_modules/@metro-labs/telegram-user/src/wire.ts +0 -1
- package/runtime/node_modules/@metro-labs/whatsapp/src/normalize.ts +0 -22
- package/runtime/node_modules/@metro-labs/whatsapp/src/wire.ts +0 -5
- package/runtime/node_modules/@metro-labs/xmtp/src/actions-push.ts +0 -87
- package/runtime/node_modules/@metro-labs/xmtp/src/emit-core.ts +0 -55
- package/runtime/node_modules/@metro-labs/xmtp/src/push.ts +0 -349
- /package/runtime/node_modules/@metro-labs/{mcp/src/daemon → core/src}/log.ts +0 -0
- /package/runtime/node_modules/@metro-labs/{mcp/src/mcp → core/src}/str.ts +0 -0
- /package/runtime/node_modules/@metro-labs/{mcp/src/daemon → core/src}/train-error.ts +0 -0
- /package/runtime/node_modules/@metro-labs/{mcp → daemon}/src/channels/webhook-note.ts +0 -0
- /package/runtime/node_modules/@metro-labs/{mcp → daemon}/src/mcp/raw-get-stream.ts +0 -0
- /package/runtime/node_modules/@metro-labs/{discord → discord-bot}/src/api-base.ts +0 -0
- /package/runtime/node_modules/@metro-labs/{discord → discord-bot}/src/verify.ts +0 -0
- /package/runtime/node_modules/@metro-labs/{mcp/src/daemon → http/src}/api-error.ts +0 -0
- /package/runtime/node_modules/@metro-labs/{mcp/src/daemon → http/src}/cors.ts +0 -0
- /package/runtime/node_modules/@metro-labs/{telegram → telegram-bot}/src/verify.ts +0 -0
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import { TrainError } from '@metro-labs/core/train-error';
|
|
2
|
+
import {
|
|
3
|
+
makeStation,
|
|
4
|
+
respond,
|
|
5
|
+
type CallMsg,
|
|
6
|
+
} from '@metro-labs/core/stations/station-runtime';
|
|
7
|
+
import { messagingAliases } from '@metro-labs/core/stations/messaging-normalize';
|
|
8
|
+
import { accountFor, accounts, publicKeyFor, targetOf, type Account } from './accounts.js';
|
|
9
|
+
import { hexToBytes, macMatches, open } from './crypto.js';
|
|
10
|
+
import { groupLineOf, type InboundMeta } from './format.js';
|
|
11
|
+
import { groupKey } from './groups.js';
|
|
12
|
+
import { deliver } from './inbound.js';
|
|
13
|
+
import { isThreemaId, normalizeThreemaId } from './ids.js';
|
|
14
|
+
import { decode } from './messages.js';
|
|
15
|
+
import { react, send } from './outbound.js';
|
|
16
|
+
|
|
17
|
+
export type { CallMsg };
|
|
18
|
+
export { MAX_TEXT_BYTES } from './outbound.js';
|
|
19
|
+
|
|
20
|
+
type Args = Record<string, unknown>;
|
|
21
|
+
|
|
22
|
+
const WEB_CHAT = 'https://web.threema.com/#!/messenger/conversation/contact/';
|
|
23
|
+
|
|
24
|
+
export const chatUrl = (gatewayId: string): string => `${WEB_CHAT}${encodeURIComponent(gatewayId)}`;
|
|
25
|
+
|
|
26
|
+
function listAccounts(id: string): void {
|
|
27
|
+
const list = [...accounts.values()].map((a) => ({
|
|
28
|
+
id: a.cfg.id,
|
|
29
|
+
handle: a.cfg.gatewayId,
|
|
30
|
+
url: chatUrl(a.cfg.gatewayId),
|
|
31
|
+
gatewayId: a.cfg.gatewayId,
|
|
32
|
+
groups: a.groups.list().map((g) => ({ line: groupLineOf(a.cfg.id, g), name: g.name, members: g.members.length })),
|
|
33
|
+
...(a.cfg.callbackId && a.cfg.callbackToken
|
|
34
|
+
? { callbackId: a.cfg.callbackId, callbackToken: a.cfg.callbackToken }
|
|
35
|
+
: {}),
|
|
36
|
+
}));
|
|
37
|
+
respond(id, { result: { accounts: list } });
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function listMembers(id: string, args: Args): void {
|
|
41
|
+
const line = typeof args.line === 'string' ? args.line : '';
|
|
42
|
+
const { acct, target } = targetOf(line, typeof args.account === 'string' ? args.account : undefined);
|
|
43
|
+
if (target.kind !== 'group') {
|
|
44
|
+
respond(id, { result: { members: [], capability: { supported: false, complete: false, reason: 'a Threema 1:1 chat has no roster' } } });
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
const roster = acct.groups.get(target.group);
|
|
48
|
+
if (roster === undefined) {
|
|
49
|
+
respond(id, { result: { members: [], capability: { supported: true, complete: false, reason: `no member list yet for ${groupKey(target.group)}` } } });
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
const members = roster.members.map((m) => ({ id: m, name: m, is_admin: m === roster.creator, is_bot: m === acct.cfg.gatewayId }));
|
|
53
|
+
respond(id, { result: { members, capability: { supported: true, complete: true, total: members.length } } });
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
interface CallbackArgs {
|
|
57
|
+
account: string;
|
|
58
|
+
from: string;
|
|
59
|
+
to: string;
|
|
60
|
+
messageId: string;
|
|
61
|
+
date: string;
|
|
62
|
+
nonce: string;
|
|
63
|
+
box: string;
|
|
64
|
+
mac: string;
|
|
65
|
+
nickname?: string;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const str = (v: unknown): string => (typeof v === 'string' ? v : '');
|
|
69
|
+
|
|
70
|
+
function parseCallback(args: Args): CallbackArgs {
|
|
71
|
+
const fields = {
|
|
72
|
+
account: str(args.account),
|
|
73
|
+
from: str(args.from),
|
|
74
|
+
to: str(args.to),
|
|
75
|
+
messageId: str(args.messageId),
|
|
76
|
+
date: str(args.date),
|
|
77
|
+
nonce: str(args.nonce),
|
|
78
|
+
box: str(args.box),
|
|
79
|
+
mac: str(args.mac),
|
|
80
|
+
};
|
|
81
|
+
for (const [key, value] of Object.entries(fields))
|
|
82
|
+
if (value === '')
|
|
83
|
+
throw new TrainError('threema_bad_callback', `callback is missing ${key}`, {
|
|
84
|
+
retryable: false,
|
|
85
|
+
});
|
|
86
|
+
const nickname = str(args.nickname);
|
|
87
|
+
return nickname === '' ? fields : { ...fields, nickname };
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function checkedSender(acct: Account, cb: CallbackArgs): string {
|
|
91
|
+
if (!macMatches(acct.cfg.secret, cb, cb.mac))
|
|
92
|
+
throw new TrainError('threema_bad_mac', 'the callback MAC does not match this account API secret', { retryable: false });
|
|
93
|
+
const from = normalizeThreemaId(cb.from);
|
|
94
|
+
if (!isThreemaId(from))
|
|
95
|
+
throw new TrainError('threema_bad_callback', `'${cb.from}' is not a Threema ID`, { retryable: false });
|
|
96
|
+
if (normalizeThreemaId(cb.to) !== acct.cfg.gatewayId)
|
|
97
|
+
throw new TrainError('threema_wrong_recipient', `callback addressed to ${cb.to}, not to ${acct.cfg.gatewayId}`, { retryable: false });
|
|
98
|
+
return from;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
async function callback(id: string, args: Args): Promise<void> {
|
|
102
|
+
const cb = parseCallback(args);
|
|
103
|
+
const acct = accountFor(cb.account);
|
|
104
|
+
const from = checkedSender(acct, cb);
|
|
105
|
+
const plain = open(
|
|
106
|
+
hexToBytes(cb.box, 'box'),
|
|
107
|
+
hexToBytes(cb.nonce, 'nonce'),
|
|
108
|
+
await publicKeyFor(acct, from),
|
|
109
|
+
acct.keys,
|
|
110
|
+
);
|
|
111
|
+
if (plain === null)
|
|
112
|
+
throw new TrainError(
|
|
113
|
+
'threema_undecryptable',
|
|
114
|
+
`could not decrypt a message from ${from}; the private key may not match the Gateway ID`,
|
|
115
|
+
{ retryable: false },
|
|
116
|
+
);
|
|
117
|
+
const meta: InboundMeta = { ...cb, from, messageId: cb.messageId.toLowerCase() };
|
|
118
|
+
respond(id, { result: { ok: true, kind: deliver(acct, meta, decode(plain)) } });
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
export const handleCall = makeStation({
|
|
122
|
+
handlers: { accounts: listAccounts, send, react, callback, listMembers },
|
|
123
|
+
normalize: messagingAliases({ action: 'removed' }),
|
|
124
|
+
});
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
import { errMsg } from '@metro-labs/core/log';
|
|
2
|
+
import { TrainError } from '@metro-labs/core/train-error';
|
|
3
|
+
import { GATEWAY_API, MESSAGE_ID_RE } from './ids.js';
|
|
4
|
+
|
|
5
|
+
const TIMEOUT_MS = 15_000;
|
|
6
|
+
const PUBLIC_KEY_RE = /^[0-9a-f]{64}$/;
|
|
7
|
+
|
|
8
|
+
export interface GatewayCreds {
|
|
9
|
+
gatewayId: string;
|
|
10
|
+
secret: string;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
async function call(
|
|
14
|
+
path: string,
|
|
15
|
+
init: RequestInit,
|
|
16
|
+
label: string,
|
|
17
|
+
): Promise<Response> {
|
|
18
|
+
try {
|
|
19
|
+
return await fetch(`${GATEWAY_API}${path}`, {
|
|
20
|
+
signal: AbortSignal.timeout(TIMEOUT_MS),
|
|
21
|
+
...init,
|
|
22
|
+
});
|
|
23
|
+
} catch (err) {
|
|
24
|
+
throw new TrainError(
|
|
25
|
+
'threema_unreachable',
|
|
26
|
+
`${label}: could not reach the Threema Gateway (${errMsg(err)})`,
|
|
27
|
+
{ retryable: true },
|
|
28
|
+
);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const authQuery = (c: GatewayCreds): string =>
|
|
33
|
+
new URLSearchParams({ from: c.gatewayId, secret: c.secret }).toString();
|
|
34
|
+
|
|
35
|
+
function refusal(status: number, what: string): TrainError {
|
|
36
|
+
if (status === 401)
|
|
37
|
+
return new TrainError(
|
|
38
|
+
'threema_unauthorized',
|
|
39
|
+
'the Threema Gateway rejected this account credentials; check the Gateway ID and API secret',
|
|
40
|
+
{ retryable: false },
|
|
41
|
+
);
|
|
42
|
+
if (status === 402)
|
|
43
|
+
return new TrainError(
|
|
44
|
+
'threema_no_credits',
|
|
45
|
+
'this Threema Gateway ID has no credits left; top it up at gateway.threema.ch',
|
|
46
|
+
{ retryable: false },
|
|
47
|
+
);
|
|
48
|
+
if (status === 404)
|
|
49
|
+
return new TrainError('threema_unknown_id', `Threema does not know the ID ${what}`, {
|
|
50
|
+
retryable: false,
|
|
51
|
+
});
|
|
52
|
+
if (status === 413)
|
|
53
|
+
return new TrainError(
|
|
54
|
+
'threema_message_too_long',
|
|
55
|
+
'Threema refused the message as too long',
|
|
56
|
+
{ retryable: false },
|
|
57
|
+
);
|
|
58
|
+
if (status === 400)
|
|
59
|
+
return new TrainError('threema_bad_recipient', `Threema refused the recipient ${what}`, {
|
|
60
|
+
retryable: false,
|
|
61
|
+
});
|
|
62
|
+
return new TrainError(
|
|
63
|
+
'threema_gateway_error',
|
|
64
|
+
`the Threema Gateway answered ${status}`,
|
|
65
|
+
{ retryable: status >= 500 },
|
|
66
|
+
);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export async function fetchPublicKey(
|
|
70
|
+
c: GatewayCreds,
|
|
71
|
+
threemaId: string,
|
|
72
|
+
): Promise<string> {
|
|
73
|
+
const res = await call(
|
|
74
|
+
`/pubkeys/${encodeURIComponent(threemaId)}?${authQuery(c)}`,
|
|
75
|
+
{},
|
|
76
|
+
'pubkeys',
|
|
77
|
+
);
|
|
78
|
+
if (!res.ok) throw refusal(res.status, threemaId);
|
|
79
|
+
const hex = (await res.text()).trim().toLowerCase();
|
|
80
|
+
if (!PUBLIC_KEY_RE.test(hex))
|
|
81
|
+
throw new TrainError(
|
|
82
|
+
'threema_gateway_error',
|
|
83
|
+
`pubkeys: the Threema Gateway gave an unexpected answer for ${threemaId}`,
|
|
84
|
+
);
|
|
85
|
+
return hex;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export async function fetchCredits(c: GatewayCreds): Promise<number> {
|
|
89
|
+
const res = await call(`/credits?${authQuery(c)}`, {}, 'credits');
|
|
90
|
+
if (!res.ok) throw refusal(res.status, c.gatewayId);
|
|
91
|
+
const credits = Number((await res.text()).trim());
|
|
92
|
+
if (!Number.isInteger(credits))
|
|
93
|
+
throw new TrainError(
|
|
94
|
+
'threema_gateway_error',
|
|
95
|
+
'credits: the Threema Gateway gave an unexpected answer',
|
|
96
|
+
);
|
|
97
|
+
return credits;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
export async function sendE2E(
|
|
101
|
+
c: GatewayCreds,
|
|
102
|
+
to: string,
|
|
103
|
+
nonceHex: string,
|
|
104
|
+
boxHex: string,
|
|
105
|
+
group = false,
|
|
106
|
+
): Promise<string> {
|
|
107
|
+
const body = new URLSearchParams({
|
|
108
|
+
from: c.gatewayId,
|
|
109
|
+
to,
|
|
110
|
+
nonce: nonceHex,
|
|
111
|
+
box: boxHex,
|
|
112
|
+
secret: c.secret,
|
|
113
|
+
...(group ? { group: '1' } : {}),
|
|
114
|
+
});
|
|
115
|
+
const res = await call(
|
|
116
|
+
'/send_e2e',
|
|
117
|
+
{
|
|
118
|
+
method: 'POST',
|
|
119
|
+
headers: { 'content-type': 'application/x-www-form-urlencoded' },
|
|
120
|
+
body: body.toString(),
|
|
121
|
+
},
|
|
122
|
+
'send_e2e',
|
|
123
|
+
);
|
|
124
|
+
if (!res.ok) throw refusal(res.status, to);
|
|
125
|
+
const messageId = (await res.text()).trim().toLowerCase();
|
|
126
|
+
if (!MESSAGE_ID_RE.test(messageId))
|
|
127
|
+
throw new TrainError(
|
|
128
|
+
'threema_gateway_error',
|
|
129
|
+
'send_e2e: the Threema Gateway answered without a message id',
|
|
130
|
+
);
|
|
131
|
+
return messageId;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
const BLOB_ID_RE = /^[0-9a-f]{32}$/;
|
|
135
|
+
const BLOB_TIMEOUT_MS = 120_000;
|
|
136
|
+
|
|
137
|
+
export async function uploadBlob(c: GatewayCreds, data: Uint8Array): Promise<string> {
|
|
138
|
+
const form = new FormData();
|
|
139
|
+
form.append('blob', new Blob([Buffer.from(data)]), 'blob');
|
|
140
|
+
const res = await call(`/upload_blob?${authQuery(c)}`, { method: 'POST', body: form, signal: AbortSignal.timeout(BLOB_TIMEOUT_MS) }, 'upload_blob');
|
|
141
|
+
if (!res.ok) throw refusal(res.status, 'blob');
|
|
142
|
+
const blobId = (await res.text()).trim().toLowerCase();
|
|
143
|
+
if (!BLOB_ID_RE.test(blobId)) throw new TrainError('threema_gateway_error', 'upload_blob: the Threema Gateway answered without a blob id');
|
|
144
|
+
return blobId;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
export async function downloadBlob(c: GatewayCreds, blobId: string): Promise<Uint8Array> {
|
|
148
|
+
if (!BLOB_ID_RE.test(blobId)) throw new TrainError('threema_bad_blob', `'${blobId}' is not a blob id`, { retryable: false });
|
|
149
|
+
const res = await call(`/blobs/${blobId}?${authQuery(c)}`, { signal: AbortSignal.timeout(BLOB_TIMEOUT_MS) }, 'blob download');
|
|
150
|
+
if (!res.ok) throw refusal(res.status, `blob ${blobId}`);
|
|
151
|
+
return new Uint8Array(await res.arrayBuffer());
|
|
152
|
+
}
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import { createHmac, randomInt, timingSafeEqual } from 'node:crypto';
|
|
2
|
+
import nacl from 'tweetnacl';
|
|
3
|
+
|
|
4
|
+
const HEX_RE = /^(?:[0-9a-fA-F]{2})*$/;
|
|
5
|
+
|
|
6
|
+
export interface KeyPair {
|
|
7
|
+
publicKey: Uint8Array;
|
|
8
|
+
secretKey: Uint8Array;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export function hexToBytes(hex: string, label: string): Uint8Array {
|
|
12
|
+
if (!HEX_RE.test(hex)) throw new Error(`${label} is not hex`);
|
|
13
|
+
return new Uint8Array(Buffer.from(hex, 'hex'));
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export const bytesToHex = (bytes: Uint8Array): string =>
|
|
17
|
+
Buffer.from(bytes).toString('hex');
|
|
18
|
+
|
|
19
|
+
export const keyPairFrom = (privateKeyHex: string): KeyPair =>
|
|
20
|
+
nacl.box.keyPair.fromSecretKey(hexToBytes(privateKeyHex, 'private key'));
|
|
21
|
+
|
|
22
|
+
function pad(plain: Uint8Array): Uint8Array {
|
|
23
|
+
const n = randomInt(1, 256);
|
|
24
|
+
const out = new Uint8Array(plain.length + n);
|
|
25
|
+
out.set(plain);
|
|
26
|
+
out.fill(n, plain.length);
|
|
27
|
+
return out;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function unpad(data: Uint8Array): Uint8Array | null {
|
|
31
|
+
const n = data[data.length - 1];
|
|
32
|
+
if (n === undefined || n < 1 || n > data.length) return null;
|
|
33
|
+
return data.subarray(0, data.length - n);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export function seal(
|
|
37
|
+
plain: Uint8Array,
|
|
38
|
+
theirPublicKey: Uint8Array,
|
|
39
|
+
mine: KeyPair,
|
|
40
|
+
): { nonce: Uint8Array; box: Uint8Array } {
|
|
41
|
+
const nonce = nacl.randomBytes(nacl.box.nonceLength);
|
|
42
|
+
return {
|
|
43
|
+
nonce,
|
|
44
|
+
box: nacl.box(pad(plain), nonce, theirPublicKey, mine.secretKey),
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export function open(
|
|
49
|
+
box: Uint8Array,
|
|
50
|
+
nonce: Uint8Array,
|
|
51
|
+
theirPublicKey: Uint8Array,
|
|
52
|
+
mine: KeyPair,
|
|
53
|
+
): Uint8Array | null {
|
|
54
|
+
const padded = nacl.box.open(box, nonce, theirPublicKey, mine.secretKey);
|
|
55
|
+
return padded === null ? null : unpad(padded);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export interface CallbackFields {
|
|
59
|
+
from: string;
|
|
60
|
+
to: string;
|
|
61
|
+
messageId: string;
|
|
62
|
+
date: string;
|
|
63
|
+
nonce: string;
|
|
64
|
+
box: string;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export const callbackMac = (secret: string, f: CallbackFields): string =>
|
|
68
|
+
createHmac('sha256', secret)
|
|
69
|
+
.update(f.from + f.to + f.messageId + f.date + f.nonce + f.box)
|
|
70
|
+
.digest('hex');
|
|
71
|
+
|
|
72
|
+
export function macMatches(
|
|
73
|
+
secret: string,
|
|
74
|
+
fields: CallbackFields,
|
|
75
|
+
mac: string,
|
|
76
|
+
): boolean {
|
|
77
|
+
const want = Buffer.from(callbackMac(secret, fields), 'hex');
|
|
78
|
+
const got = Buffer.from(mac.toLowerCase(), 'hex');
|
|
79
|
+
return want.length === got.length && timingSafeEqual(want, got);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
const QUOTE_RE = /^> quote #([0-9a-f]{16})\n\n([\s\S]*)$/;
|
|
83
|
+
|
|
84
|
+
export const quoted = (replyTo: string, text: string): string =>
|
|
85
|
+
`> quote #${replyTo}\n\n${text}`;
|
|
86
|
+
|
|
87
|
+
export function unquote(text: string): { replyTo?: string; text: string } {
|
|
88
|
+
const m = QUOTE_RE.exec(text);
|
|
89
|
+
const replyTo = m?.[1];
|
|
90
|
+
const body = m?.[2];
|
|
91
|
+
return replyTo === undefined || body === undefined ? { text } : { replyTo, text: body };
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
const FILE_NONCE = new Uint8Array([...new Array<number>(23).fill(0), 1]);
|
|
95
|
+
|
|
96
|
+
export const newBlobKey = (): Uint8Array => nacl.randomBytes(nacl.secretbox.keyLength);
|
|
97
|
+
|
|
98
|
+
export const sealBlob = (data: Uint8Array, key: Uint8Array): Uint8Array => nacl.secretbox(data, FILE_NONCE, key);
|
|
99
|
+
|
|
100
|
+
export const openBlob = (data: Uint8Array, key: Uint8Array): Uint8Array | null => nacl.secretbox.open(data, FILE_NONCE, key);
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { readFile } from 'node:fs/promises';
|
|
2
|
+
import { emitInbound, reportAttachment, type SavedFile } from '@metro-labs/core/stations/train-events';
|
|
3
|
+
import { assertAttachmentSize, isImageMime, kindOf, saveBufferToCache } from '@metro-labs/core/stations/attachments';
|
|
4
|
+
import { TrainError } from '@metro-labs/core/train-error';
|
|
5
|
+
import type { Account } from './accounts.js';
|
|
6
|
+
import { downloadBlob, uploadBlob } from './api.js';
|
|
7
|
+
import { bytesToHex, hexToBytes, newBlobKey, openBlob, sealBlob } from './crypto.js';
|
|
8
|
+
import { textEnvelope, type InboundMeta, type Room } from './format.js';
|
|
9
|
+
import { encodeFile, encodeGroupFile, type FileData, type GroupRef } from './messages.js';
|
|
10
|
+
|
|
11
|
+
export const MAX_BLOB_BYTES = 50 * 1024 * 1024;
|
|
12
|
+
|
|
13
|
+
export interface OutgoingFile {
|
|
14
|
+
path: string;
|
|
15
|
+
mime: string;
|
|
16
|
+
name: string;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export async function packFile(acct: Account, file: OutgoingFile, caption: string | null): Promise<FileData> {
|
|
20
|
+
const data = await readFile(file.path);
|
|
21
|
+
assertAttachmentSize(data.length);
|
|
22
|
+
if (data.length > MAX_BLOB_BYTES) throw new TrainError('threema_file_too_big', `Threema carries files up to 50 MB; ${file.name} is bigger`, { retryable: false });
|
|
23
|
+
const key = newBlobKey();
|
|
24
|
+
const blobId = await uploadBlob(acct.cfg, sealBlob(new Uint8Array(data), key));
|
|
25
|
+
return { blobId, key: bytesToHex(key), mime: file.mime, name: file.name, size: data.length, caption, media: isImageMime(file.mime) };
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export const encodeFileFor = (group: GroupRef | null, file: FileData): Uint8Array => (group === null ? encodeFile(file) : encodeGroupFile(group, file));
|
|
29
|
+
|
|
30
|
+
export const fileLabel = (file: OutgoingFile): string => kindOf(file.mime, file.path);
|
|
31
|
+
|
|
32
|
+
async function fetchFile(acct: Account, m: InboundMeta, file: FileData): Promise<SavedFile> {
|
|
33
|
+
const sealed = await downloadBlob(acct.cfg, file.blobId);
|
|
34
|
+
const plain = openBlob(sealed, hexToBytes(file.key, 'blob key'));
|
|
35
|
+
if (plain === null) throw new TrainError('threema_bad_blob', 'the file did not decrypt with the key in the message', { retryable: false });
|
|
36
|
+
return saveBufferToCache(plain, m.messageId, 0, { mime: file.mime, name: file.name });
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export function deliverFile(acct: Account, m: InboundMeta, file: FileData, room: Room, sentByUs: (id: string) => boolean): void {
|
|
40
|
+
const env = textEnvelope(acct.cfg.id, m, file.caption ?? '', sentByUs, room);
|
|
41
|
+
const line = String(env.line);
|
|
42
|
+
const forId = String(env.id);
|
|
43
|
+
const payload = { ...(env.payload as Record<string, unknown>), attachments: [{ kind: kindOf(file.mime, file.name), name: file.name, mime: file.mime, ...(file.size === null ? {} : { size: file.size }) }] };
|
|
44
|
+
emitInbound(acct.cfg.id, { ...env, payload });
|
|
45
|
+
const kind = kindOf(file.mime, file.name);
|
|
46
|
+
reportAttachment(fetchFile(acct, m, file), { station: 'threema', account: acct.cfg.id, line, forId, index: 0 }, { saved: { kind }, failed: { kind } });
|
|
47
|
+
}
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
import { emit, mintId } from '@metro-labs/core/stations/station-runtime';
|
|
2
|
+
import { selfUri } from '@metro-labs/core/stations/train-events';
|
|
3
|
+
import { unquote } from './crypto.js';
|
|
4
|
+
import { groupKey } from './groups.js';
|
|
5
|
+
import { RECEIPT_ACK, RECEIPT_DECLINE, type GroupRef } from './messages.js';
|
|
6
|
+
|
|
7
|
+
const REACTION_OF: Record<number, string> = {
|
|
8
|
+
[RECEIPT_ACK]: '👍',
|
|
9
|
+
[RECEIPT_DECLINE]: '👎',
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
export const lineOf = (accountId: string, threemaId: string): string =>
|
|
13
|
+
`metro://threema/${accountId}/${threemaId}`;
|
|
14
|
+
|
|
15
|
+
export const groupLineOf = (accountId: string, group: GroupRef): string =>
|
|
16
|
+
`metro://threema/${accountId}/${groupKey(group)}`;
|
|
17
|
+
|
|
18
|
+
const userOf = (accountId: string, threemaId: string): string =>
|
|
19
|
+
`metro://threema/${accountId}/user/${threemaId}`;
|
|
20
|
+
|
|
21
|
+
export interface InboundMeta {
|
|
22
|
+
from: string;
|
|
23
|
+
to: string;
|
|
24
|
+
messageId: string;
|
|
25
|
+
date: string;
|
|
26
|
+
nickname?: string;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export interface Room {
|
|
30
|
+
group: GroupRef | null;
|
|
31
|
+
name: string | null;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export const DIRECT: Room = { group: null, name: null };
|
|
35
|
+
|
|
36
|
+
const MENTION_ALL = '@@@@@@@@';
|
|
37
|
+
|
|
38
|
+
export const mentionsSelf = (text: string, self: string): boolean =>
|
|
39
|
+
text.includes(MENTION_ALL) || text.toUpperCase().includes(`@@${self.toUpperCase()}`);
|
|
40
|
+
|
|
41
|
+
function tsOf(date: string): string {
|
|
42
|
+
const seconds = Number(date);
|
|
43
|
+
return Number.isFinite(seconds) && seconds > 0
|
|
44
|
+
? new Date(seconds * 1000).toISOString()
|
|
45
|
+
: new Date().toISOString();
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function base(
|
|
49
|
+
accountId: string,
|
|
50
|
+
m: InboundMeta,
|
|
51
|
+
type: string,
|
|
52
|
+
room: Room,
|
|
53
|
+
): Record<string, unknown> {
|
|
54
|
+
const group = room.group;
|
|
55
|
+
return {
|
|
56
|
+
id: mintId(),
|
|
57
|
+
ts: tsOf(m.date),
|
|
58
|
+
station: 'threema',
|
|
59
|
+
line: group === null ? lineOf(accountId, m.from) : groupLineOf(accountId, group),
|
|
60
|
+
line_name: group === null ? (m.nickname ?? m.from) : (room.name ?? groupKey(group)),
|
|
61
|
+
from: userOf(accountId, m.from),
|
|
62
|
+
from_name: m.nickname,
|
|
63
|
+
from_display_name: m.nickname,
|
|
64
|
+
is_private: group === null,
|
|
65
|
+
payload: { from: m.from, to: m.to, nickname: m.nickname ?? null, type, ...(group === null ? {} : { group: groupKey(group) }) },
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export function textEnvelope(
|
|
70
|
+
accountId: string,
|
|
71
|
+
m: InboundMeta,
|
|
72
|
+
raw: string,
|
|
73
|
+
sentByUs: (messageId: string) => boolean,
|
|
74
|
+
room: Room = DIRECT,
|
|
75
|
+
): Record<string, unknown> {
|
|
76
|
+
const { replyTo, text } = unquote(raw);
|
|
77
|
+
return {
|
|
78
|
+
...base(accountId, m, 'text', room),
|
|
79
|
+
message_id: m.messageId,
|
|
80
|
+
text,
|
|
81
|
+
...(room.group !== null && mentionsSelf(text, m.to) ? { mentions_self: true } : {}),
|
|
82
|
+
...(replyTo === undefined
|
|
83
|
+
? {}
|
|
84
|
+
: {
|
|
85
|
+
reply_to: replyTo,
|
|
86
|
+
event: { type: 'reply', replyTo },
|
|
87
|
+
reply_to_self: sentByUs(replyTo),
|
|
88
|
+
}),
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export function reactionEnvelope(
|
|
93
|
+
accountId: string,
|
|
94
|
+
m: InboundMeta,
|
|
95
|
+
emoji: string,
|
|
96
|
+
targetId: string,
|
|
97
|
+
removed: boolean,
|
|
98
|
+
room: Room = DIRECT,
|
|
99
|
+
type = 'reaction',
|
|
100
|
+
): Record<string, unknown> {
|
|
101
|
+
return {
|
|
102
|
+
...base(accountId, m, type, room),
|
|
103
|
+
message_id: `${m.messageId}:${targetId}`,
|
|
104
|
+
text: `[react ${emoji}${removed ? ' (removed)' : ''}]`,
|
|
105
|
+
emoji,
|
|
106
|
+
event: { type: 'react', emoji, targetId, ...(removed ? { removed: true } : {}) },
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
export function receiptEnvelope(
|
|
111
|
+
accountId: string,
|
|
112
|
+
m: InboundMeta,
|
|
113
|
+
status: number,
|
|
114
|
+
targetId: string,
|
|
115
|
+
room: Room = DIRECT,
|
|
116
|
+
): Record<string, unknown> | null {
|
|
117
|
+
const emoji = REACTION_OF[status];
|
|
118
|
+
return emoji === undefined ? null : reactionEnvelope(accountId, m, emoji, targetId, false, room, 'receipt');
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
export function emitOutbound(
|
|
122
|
+
accountId: string,
|
|
123
|
+
line: string,
|
|
124
|
+
messageId: string,
|
|
125
|
+
text: string,
|
|
126
|
+
replyTo?: string,
|
|
127
|
+
): void {
|
|
128
|
+
emit({
|
|
129
|
+
id: mintId(),
|
|
130
|
+
ts: new Date().toISOString(),
|
|
131
|
+
station: 'threema',
|
|
132
|
+
line,
|
|
133
|
+
from: selfUri(),
|
|
134
|
+
to: line,
|
|
135
|
+
message_id: messageId,
|
|
136
|
+
text,
|
|
137
|
+
reply_to: replyTo,
|
|
138
|
+
...(replyTo ? { event: { type: 'reply', replyTo } } : {}),
|
|
139
|
+
payload: { account: accountId },
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
export function emitOutboundReaction(
|
|
144
|
+
accountId: string,
|
|
145
|
+
line: string,
|
|
146
|
+
messageId: string,
|
|
147
|
+
emoji: string,
|
|
148
|
+
targetId: string,
|
|
149
|
+
removed: boolean,
|
|
150
|
+
): void {
|
|
151
|
+
emit({
|
|
152
|
+
id: mintId(),
|
|
153
|
+
ts: new Date().toISOString(),
|
|
154
|
+
station: 'threema',
|
|
155
|
+
line,
|
|
156
|
+
from: selfUri(),
|
|
157
|
+
to: line,
|
|
158
|
+
message_id: messageId,
|
|
159
|
+
text: `[react ${emoji}${removed ? ' (removed)' : ''}]`,
|
|
160
|
+
emoji,
|
|
161
|
+
event: { type: 'react', emoji, targetId, ...(removed ? { removed: true } : {}) },
|
|
162
|
+
payload: { account: accountId },
|
|
163
|
+
});
|
|
164
|
+
}
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { mkdirSync, readFileSync, renameSync, writeFileSync } from 'node:fs';
|
|
2
|
+
import { dirname } from 'node:path';
|
|
3
|
+
import { accountFiles } from '@metro-labs/core/stations/account-files';
|
|
4
|
+
import type { GroupRef } from './messages.js';
|
|
5
|
+
|
|
6
|
+
export const GROUP_RE = /^([A-Z0-9]{8}|\*[A-Z0-9]{7})-([0-9a-f]{16})$/i;
|
|
7
|
+
|
|
8
|
+
export interface GroupRoster {
|
|
9
|
+
creator: string;
|
|
10
|
+
groupId: string;
|
|
11
|
+
members: string[];
|
|
12
|
+
name: string | null;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export const groupKey = (group: GroupRef): string => `${group.creator.toUpperCase()}-${group.groupId.toLowerCase()}`;
|
|
16
|
+
|
|
17
|
+
export function parseGroupKey(resource: string): GroupRef | null {
|
|
18
|
+
const m = GROUP_RE.exec(resource);
|
|
19
|
+
return m === null ? null : { creator: (m[1] ?? '').toUpperCase(), groupId: (m[2] ?? '').toLowerCase() };
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export const groupFiles = accountFiles('THREEMA_GROUPS_DIR', 'threema-groups-');
|
|
23
|
+
|
|
24
|
+
const groupsFile = (accountId: string): string => groupFiles.path(accountId);
|
|
25
|
+
|
|
26
|
+
function readRosters(accountId: string): Map<string, GroupRoster> {
|
|
27
|
+
try {
|
|
28
|
+
const raw: unknown = JSON.parse(readFileSync(groupsFile(accountId), 'utf8'));
|
|
29
|
+
const out = new Map<string, GroupRoster>();
|
|
30
|
+
if (Array.isArray(raw))
|
|
31
|
+
for (const entry of raw) {
|
|
32
|
+
const g = (entry ?? {}) as Partial<GroupRoster>;
|
|
33
|
+
if (typeof g.creator === 'string' && typeof g.groupId === 'string' && Array.isArray(g.members))
|
|
34
|
+
out.set(groupKey(g as GroupRef), { creator: g.creator, groupId: g.groupId, members: g.members.filter((m): m is string => typeof m === 'string'), name: typeof g.name === 'string' ? g.name : null });
|
|
35
|
+
}
|
|
36
|
+
return out;
|
|
37
|
+
} catch {
|
|
38
|
+
return new Map();
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export class GroupStore {
|
|
43
|
+
private readonly rosters: Map<string, GroupRoster>;
|
|
44
|
+
|
|
45
|
+
constructor(
|
|
46
|
+
private readonly accountId: string,
|
|
47
|
+
private readonly self: string,
|
|
48
|
+
) {
|
|
49
|
+
this.rosters = readRosters(accountId);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
get(group: GroupRef): GroupRoster | undefined {
|
|
53
|
+
return this.rosters.get(groupKey(group));
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
list(): GroupRoster[] {
|
|
57
|
+
return [...this.rosters.values()];
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
setup(creator: string, groupId: string, members: string[]): GroupRoster {
|
|
61
|
+
const key = groupKey({ creator, groupId });
|
|
62
|
+
const held = this.rosters.get(key);
|
|
63
|
+
const roster: GroupRoster = { creator, groupId, members: [...new Set([creator, ...members])], name: held?.name ?? null };
|
|
64
|
+
this.rosters.set(key, roster);
|
|
65
|
+
this.save();
|
|
66
|
+
return roster;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
rename(creator: string, groupId: string, name: string): void {
|
|
70
|
+
const held = this.rosters.get(groupKey({ creator, groupId }));
|
|
71
|
+
if (held === undefined) return;
|
|
72
|
+
held.name = name;
|
|
73
|
+
this.save();
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
leave(group: GroupRef, member: string): void {
|
|
77
|
+
const held = this.rosters.get(groupKey(group));
|
|
78
|
+
if (held === undefined) return;
|
|
79
|
+
if (member === this.self) this.rosters.delete(groupKey(group));
|
|
80
|
+
else held.members = held.members.filter((m) => m !== member);
|
|
81
|
+
this.save();
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
recipients(group: GroupRef): string[] | null {
|
|
85
|
+
const held = this.get(group);
|
|
86
|
+
return held === undefined ? null : held.members.filter((m) => m !== this.self);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
private save(): void {
|
|
90
|
+
const path = groupsFile(this.accountId);
|
|
91
|
+
mkdirSync(dirname(path), { recursive: true });
|
|
92
|
+
const tmp = `${path}.${String(process.pid)}.tmp`;
|
|
93
|
+
writeFileSync(tmp, JSON.stringify(this.list()), { mode: 0o600 });
|
|
94
|
+
renameSync(tmp, path);
|
|
95
|
+
}
|
|
96
|
+
}
|