@stage-labs/metro 0.1.0-beta.21 → 0.1.0-beta.211
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 +313 -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,21 @@
|
|
|
1
|
+
export const GATEWAY_API = (
|
|
2
|
+
process.env.THREEMA_GATEWAY_URL ?? 'https://msgapi.threema.ch'
|
|
3
|
+
).replace(/\/+$/, '');
|
|
4
|
+
|
|
5
|
+
const THREEMA_ID_RE = /^(?:[A-Z0-9]{8}|\*[A-Z0-9]{7})$/;
|
|
6
|
+
const KEY_HEX_RE = /^[0-9a-f]{64}$/;
|
|
7
|
+
|
|
8
|
+
export const MESSAGE_ID_RE = /^[0-9a-f]{16}$/;
|
|
9
|
+
|
|
10
|
+
export const isThreemaId = (raw: string): boolean => THREEMA_ID_RE.test(raw);
|
|
11
|
+
|
|
12
|
+
export const isGatewayId = (raw: string): boolean =>
|
|
13
|
+
raw.startsWith('*') && isThreemaId(raw);
|
|
14
|
+
|
|
15
|
+
export const normalizeThreemaId = (raw: string): string =>
|
|
16
|
+
raw.trim().toUpperCase();
|
|
17
|
+
|
|
18
|
+
export function parsePrivateKey(raw: string): string | null {
|
|
19
|
+
const text = raw.trim().replace(/^private:/i, '').trim().toLowerCase();
|
|
20
|
+
return KEY_HEX_RE.test(text) ? text : null;
|
|
21
|
+
}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import type { Account } from './accounts.js';
|
|
2
|
+
import { emitInbound } from '@metro-labs/core/stations/train-events';
|
|
3
|
+
import {
|
|
4
|
+
DIRECT,
|
|
5
|
+
reactionEnvelope,
|
|
6
|
+
receiptEnvelope,
|
|
7
|
+
textEnvelope,
|
|
8
|
+
type InboundMeta,
|
|
9
|
+
type Room,
|
|
10
|
+
} from './format.js';
|
|
11
|
+
import { deliverFile } from './files.js';
|
|
12
|
+
import type { Decoded, GroupRef } from './messages.js';
|
|
13
|
+
import { requestSync, sentByUs } from './outbound.js';
|
|
14
|
+
|
|
15
|
+
const synced = new Set<string>();
|
|
16
|
+
|
|
17
|
+
function roomOf(acct: Account, group: GroupRef | null): Room {
|
|
18
|
+
if (group === null) return DIRECT;
|
|
19
|
+
return { group, name: acct.groups.get(group)?.name ?? null };
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function askForRoster(acct: Account, group: GroupRef): void {
|
|
23
|
+
const key = `${acct.cfg.id}:${group.creator}-${group.groupId}`;
|
|
24
|
+
if (acct.groups.get(group) !== undefined || synced.has(key)) return;
|
|
25
|
+
synced.add(key);
|
|
26
|
+
requestSync(acct, group).catch((err: unknown) => {
|
|
27
|
+
process.stderr.write(`threema[${acct.cfg.id}]: could not ask ${group.creator} for the group roster: ${err instanceof Error ? err.message : String(err)}\n`);
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function reactions(acct: Account, m: InboundMeta, d: Extract<Decoded, { kind: 'receipt' }>): string {
|
|
32
|
+
let count = 0;
|
|
33
|
+
for (const target of d.messageIds) {
|
|
34
|
+
const env = receiptEnvelope(acct.cfg.id, m, d.status, target, roomOf(acct, d.group));
|
|
35
|
+
if (env === null) continue;
|
|
36
|
+
emitInbound(acct.cfg.id, env);
|
|
37
|
+
count += 1;
|
|
38
|
+
}
|
|
39
|
+
return count > 0 ? 'reaction' : `receipt:${String(d.status)}`;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function control(acct: Account, m: InboundMeta, d: Decoded): string | null {
|
|
43
|
+
if (d.kind === 'group-setup') {
|
|
44
|
+
acct.groups.setup(m.from, d.groupId, d.members);
|
|
45
|
+
return 'group-setup';
|
|
46
|
+
}
|
|
47
|
+
if (d.kind === 'group-rename') {
|
|
48
|
+
acct.groups.rename(m.from, d.groupId, d.name);
|
|
49
|
+
return 'group-rename';
|
|
50
|
+
}
|
|
51
|
+
if (d.kind === 'group-leave') {
|
|
52
|
+
acct.groups.leave(d.group, m.from);
|
|
53
|
+
return 'group-leave';
|
|
54
|
+
}
|
|
55
|
+
return null;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function chat(acct: Account, m: InboundMeta, d: Decoded): string | null {
|
|
59
|
+
if (d.kind === 'text') {
|
|
60
|
+
emitInbound(acct.cfg.id, textEnvelope(acct.cfg.id, m, d.text, sentByUs));
|
|
61
|
+
return 'text';
|
|
62
|
+
}
|
|
63
|
+
if (d.kind === 'group-text') {
|
|
64
|
+
askForRoster(acct, d.group);
|
|
65
|
+
emitInbound(acct.cfg.id, textEnvelope(acct.cfg.id, m, d.text, sentByUs, roomOf(acct, d.group)));
|
|
66
|
+
return 'group-text';
|
|
67
|
+
}
|
|
68
|
+
if (d.kind === 'file') {
|
|
69
|
+
if (d.group !== null) askForRoster(acct, d.group);
|
|
70
|
+
deliverFile(acct, m, d.file, roomOf(acct, d.group), sentByUs);
|
|
71
|
+
return 'file';
|
|
72
|
+
}
|
|
73
|
+
if (d.kind === 'reaction') {
|
|
74
|
+
if (d.group !== null) askForRoster(acct, d.group);
|
|
75
|
+
emitInbound(acct.cfg.id, reactionEnvelope(acct.cfg.id, m, d.emoji, d.messageId, d.removed, roomOf(acct, d.group)));
|
|
76
|
+
return 'reaction';
|
|
77
|
+
}
|
|
78
|
+
return null;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export function deliver(acct: Account, m: InboundMeta, d: Decoded): string {
|
|
82
|
+
const handled = control(acct, m, d) ?? chat(acct, m, d);
|
|
83
|
+
if (handled !== null) return handled;
|
|
84
|
+
if (d.kind === 'receipt') return reactions(acct, m, d);
|
|
85
|
+
if (d.kind === 'typing') return 'typing';
|
|
86
|
+
const type = d.kind === 'other' ? `0x${d.type.toString(16)}` : d.kind;
|
|
87
|
+
process.stderr.write(`threema[${acct.cfg.id}]: ignored a message of type ${type} from ${m.from}\n`);
|
|
88
|
+
return `ignored:${type}`;
|
|
89
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { errMsg } from '@metro-labs/core/log';
|
|
2
|
+
import { readCalls } from '@metro-labs/core/trains/protocol';
|
|
3
|
+
import { announceAccounts } from '@metro-labs/core/stations/train-boot';
|
|
4
|
+
import { accounts, bootAccount, loadAccounts } from './accounts.js';
|
|
5
|
+
import { handleCall } from './actions.js';
|
|
6
|
+
import { fetchCredits } from './api.js';
|
|
7
|
+
|
|
8
|
+
readCalls('threema', handleCall);
|
|
9
|
+
|
|
10
|
+
for (const cfg of loadAccounts()) accounts.set(cfg.id, bootAccount(cfg));
|
|
11
|
+
announceAccounts('threema', accounts.keys());
|
|
12
|
+
|
|
13
|
+
for (const acct of accounts.values())
|
|
14
|
+
fetchCredits(acct.cfg)
|
|
15
|
+
.then((credits) => {
|
|
16
|
+
process.stderr.write(
|
|
17
|
+
`threema[${acct.cfg.id}] ${acct.cfg.gatewayId}: ${credits} credits left\n`,
|
|
18
|
+
);
|
|
19
|
+
})
|
|
20
|
+
.catch((err: unknown) => {
|
|
21
|
+
process.stderr.write(
|
|
22
|
+
`threema[${acct.cfg.id}] credits check failed: ${errMsg(err)}\n`,
|
|
23
|
+
);
|
|
24
|
+
});
|
|
@@ -0,0 +1,264 @@
|
|
|
1
|
+
import { bytesToHex, hexToBytes } from './crypto.js';
|
|
2
|
+
|
|
3
|
+
export const MSG_TEXT = 0x01;
|
|
4
|
+
export const MSG_FILE = 0x17;
|
|
5
|
+
export const MSG_GROUP_TEXT = 0x41;
|
|
6
|
+
export const MSG_GROUP_FILE = 0x46;
|
|
7
|
+
export const MSG_GROUP_SETUP = 0x4a;
|
|
8
|
+
export const MSG_GROUP_RENAME = 0x4b;
|
|
9
|
+
export const MSG_GROUP_LEAVE = 0x4c;
|
|
10
|
+
export const MSG_GROUP_REQUEST_SYNC = 0x51;
|
|
11
|
+
export const MSG_DELIVERY_RECEIPT = 0x80;
|
|
12
|
+
export const MSG_GROUP_DELIVERY_RECEIPT = 0x81;
|
|
13
|
+
export const MSG_REACTION = 0x82;
|
|
14
|
+
export const MSG_GROUP_REACTION = 0x83;
|
|
15
|
+
export const MSG_TYPING = 0x90;
|
|
16
|
+
|
|
17
|
+
export const RECEIPT_ACK = 0x03;
|
|
18
|
+
export const RECEIPT_DECLINE = 0x04;
|
|
19
|
+
|
|
20
|
+
export const ID_BYTES = 8;
|
|
21
|
+
const MESSAGE_ID_BYTES = 8;
|
|
22
|
+
const PROTO_MESSAGE_ID = 0x09;
|
|
23
|
+
const PROTO_APPLY = 0x12;
|
|
24
|
+
const PROTO_WITHDRAW = 0x1a;
|
|
25
|
+
|
|
26
|
+
export interface GroupRef {
|
|
27
|
+
creator: string;
|
|
28
|
+
groupId: string;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export interface FileData {
|
|
32
|
+
blobId: string;
|
|
33
|
+
key: string;
|
|
34
|
+
mime: string;
|
|
35
|
+
name: string;
|
|
36
|
+
size: number | null;
|
|
37
|
+
caption: string | null;
|
|
38
|
+
media: boolean;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export type Decoded =
|
|
42
|
+
| { kind: 'text'; text: string }
|
|
43
|
+
| { kind: 'file'; group: GroupRef | null; file: FileData }
|
|
44
|
+
| { kind: 'group-text'; group: GroupRef; text: string }
|
|
45
|
+
| { kind: 'group-setup'; groupId: string; members: string[] }
|
|
46
|
+
| { kind: 'group-rename'; groupId: string; name: string }
|
|
47
|
+
| { kind: 'group-leave'; group: GroupRef }
|
|
48
|
+
| { kind: 'receipt'; group: GroupRef | null; status: number; messageIds: string[] }
|
|
49
|
+
| { kind: 'reaction'; group: GroupRef | null; messageId: string; emoji: string; removed: boolean }
|
|
50
|
+
| { kind: 'typing' }
|
|
51
|
+
| { kind: 'other'; type: number };
|
|
52
|
+
|
|
53
|
+
const ascii = (bytes: Uint8Array): string => Buffer.from(bytes).toString('ascii');
|
|
54
|
+
const utf8 = (bytes: Uint8Array): string => Buffer.from(bytes).toString('utf8');
|
|
55
|
+
|
|
56
|
+
const groupHeader = (group: GroupRef): Uint8Array =>
|
|
57
|
+
new Uint8Array([...Buffer.from(group.creator, 'ascii'), ...hexToBytes(group.groupId, 'group id')]);
|
|
58
|
+
|
|
59
|
+
const readGroup = (body: Uint8Array): GroupRef | null =>
|
|
60
|
+
body.length < ID_BYTES * 2 ? null : { creator: ascii(body.subarray(0, ID_BYTES)), groupId: bytesToHex(body.subarray(ID_BYTES, ID_BYTES * 2)) };
|
|
61
|
+
|
|
62
|
+
function ids(body: Uint8Array, from: number): string[] {
|
|
63
|
+
const out: string[] = [];
|
|
64
|
+
for (let at = from; at + MESSAGE_ID_BYTES <= body.length; at += MESSAGE_ID_BYTES) out.push(bytesToHex(body.subarray(at, at + MESSAGE_ID_BYTES)));
|
|
65
|
+
return out;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const members = (body: Uint8Array, from: number): string[] => {
|
|
69
|
+
const out: string[] = [];
|
|
70
|
+
for (let at = from; at + ID_BYTES <= body.length; at += ID_BYTES) out.push(ascii(body.subarray(at, at + ID_BYTES)));
|
|
71
|
+
return out;
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
function concat(...parts: Uint8Array[]): Uint8Array {
|
|
75
|
+
const out = new Uint8Array(parts.reduce((n, p) => n + p.length, 0));
|
|
76
|
+
let at = 0;
|
|
77
|
+
for (const p of parts) {
|
|
78
|
+
out.set(p, at);
|
|
79
|
+
at += p.length;
|
|
80
|
+
}
|
|
81
|
+
return out;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
const typed = (type: number, ...parts: Uint8Array[]): Uint8Array => concat(new Uint8Array([type]), ...parts);
|
|
85
|
+
|
|
86
|
+
export const encodeText = (text: string): Uint8Array => typed(MSG_TEXT, Buffer.from(text, 'utf8'));
|
|
87
|
+
|
|
88
|
+
export const encodeGroupText = (group: GroupRef, text: string): Uint8Array => typed(MSG_GROUP_TEXT, groupHeader(group), Buffer.from(text, 'utf8'));
|
|
89
|
+
|
|
90
|
+
const fileJson = (f: FileData): Uint8Array =>
|
|
91
|
+
Buffer.from(
|
|
92
|
+
JSON.stringify({
|
|
93
|
+
b: f.blobId,
|
|
94
|
+
k: f.key,
|
|
95
|
+
m: f.mime,
|
|
96
|
+
n: f.name,
|
|
97
|
+
...(f.size === null ? {} : { s: f.size }),
|
|
98
|
+
i: f.media ? 1 : 0,
|
|
99
|
+
j: f.media ? 1 : 0,
|
|
100
|
+
...(f.caption === null ? {} : { d: f.caption }),
|
|
101
|
+
}),
|
|
102
|
+
'utf8',
|
|
103
|
+
);
|
|
104
|
+
|
|
105
|
+
export const encodeFile = (f: FileData): Uint8Array => typed(MSG_FILE, fileJson(f));
|
|
106
|
+
|
|
107
|
+
export const encodeGroupFile = (group: GroupRef, f: FileData): Uint8Array => typed(MSG_GROUP_FILE, groupHeader(group), fileJson(f));
|
|
108
|
+
|
|
109
|
+
const HEX_RE = /^[0-9a-f]+$/i;
|
|
110
|
+
|
|
111
|
+
const str = (v: unknown, fallback: string): string => (typeof v === 'string' && v !== '' ? v : fallback);
|
|
112
|
+
const num = (v: unknown): number | null => (typeof v === 'number' ? v : null);
|
|
113
|
+
const hexOf = (v: unknown, length: number): string | null => {
|
|
114
|
+
const text = typeof v === 'string' ? v.toLowerCase() : '';
|
|
115
|
+
return text.length === length && HEX_RE.test(text) ? text : null;
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
function parseJson(raw: Uint8Array): Record<string, unknown> | null {
|
|
119
|
+
try {
|
|
120
|
+
const parsed: unknown = JSON.parse(utf8(raw));
|
|
121
|
+
return typeof parsed === 'object' && parsed !== null ? (parsed as Record<string, unknown>) : null;
|
|
122
|
+
} catch {
|
|
123
|
+
return null;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
export function decodeFileJson(raw: Uint8Array): FileData | null {
|
|
128
|
+
const o = parseJson(raw);
|
|
129
|
+
if (o === null) return null;
|
|
130
|
+
const blobId = hexOf(o.b, 32);
|
|
131
|
+
const key = hexOf(o.k, 64);
|
|
132
|
+
if (blobId === null || key === null) return null;
|
|
133
|
+
const rendering = num(o.j) ?? num(o.i) ?? 0;
|
|
134
|
+
return {
|
|
135
|
+
blobId,
|
|
136
|
+
key,
|
|
137
|
+
mime: str(o.m, 'application/octet-stream'),
|
|
138
|
+
name: str(o.n, 'file'),
|
|
139
|
+
size: num(o.s),
|
|
140
|
+
caption: typeof o.d === 'string' && o.d !== '' ? o.d : null,
|
|
141
|
+
media: rendering === 1,
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
export const encodeGroupSyncRequest = (groupId: string): Uint8Array => typed(MSG_GROUP_REQUEST_SYNC, hexToBytes(groupId, 'group id'));
|
|
146
|
+
|
|
147
|
+
function varint(n: number): Uint8Array {
|
|
148
|
+
const out: number[] = [];
|
|
149
|
+
let rest = n;
|
|
150
|
+
while (rest >= 0x80) {
|
|
151
|
+
out.push((rest & 0x7f) | 0x80);
|
|
152
|
+
rest = Math.floor(rest / 0x80);
|
|
153
|
+
}
|
|
154
|
+
out.push(rest);
|
|
155
|
+
return new Uint8Array(out);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
export function encodeReactionProto(messageId: string, emoji: string, removed: boolean): Uint8Array {
|
|
159
|
+
const emojiBytes = Buffer.from(emoji, 'utf8');
|
|
160
|
+
return concat(
|
|
161
|
+
new Uint8Array([PROTO_MESSAGE_ID]),
|
|
162
|
+
hexToBytes(messageId, 'message id'),
|
|
163
|
+
new Uint8Array([removed ? PROTO_WITHDRAW : PROTO_APPLY]),
|
|
164
|
+
varint(emojiBytes.length),
|
|
165
|
+
emojiBytes,
|
|
166
|
+
);
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
export const encodeReaction = (messageId: string, emoji: string, removed: boolean): Uint8Array =>
|
|
170
|
+
typed(MSG_REACTION, encodeReactionProto(messageId, emoji, removed));
|
|
171
|
+
|
|
172
|
+
export const encodeGroupReaction = (group: GroupRef, messageId: string, emoji: string, removed: boolean): Uint8Array =>
|
|
173
|
+
typed(MSG_GROUP_REACTION, groupHeader(group), encodeReactionProto(messageId, emoji, removed));
|
|
174
|
+
|
|
175
|
+
function readVarint(body: Uint8Array, at: number): { value: number; next: number } | null {
|
|
176
|
+
let value = 0;
|
|
177
|
+
let shift = 1;
|
|
178
|
+
for (let i = at; i < body.length; i++) {
|
|
179
|
+
const byte = body[i] ?? 0;
|
|
180
|
+
value += (byte & 0x7f) * shift;
|
|
181
|
+
if (byte < 0x80) return { value, next: i + 1 };
|
|
182
|
+
shift *= 0x80;
|
|
183
|
+
}
|
|
184
|
+
return null;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
interface ProtoField {
|
|
188
|
+
tag: number;
|
|
189
|
+
bytes: Uint8Array;
|
|
190
|
+
next: number;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
function readField(body: Uint8Array, at: number): ProtoField | null {
|
|
194
|
+
const tag = body[at] ?? 0;
|
|
195
|
+
if (tag === PROTO_MESSAGE_ID)
|
|
196
|
+
return at + 1 + MESSAGE_ID_BYTES > body.length ? null : { tag, bytes: body.subarray(at + 1, at + 1 + MESSAGE_ID_BYTES), next: at + 1 + MESSAGE_ID_BYTES };
|
|
197
|
+
if (tag !== PROTO_APPLY && tag !== PROTO_WITHDRAW) return null;
|
|
198
|
+
const len = readVarint(body, at + 1);
|
|
199
|
+
if (len === null || len.next + len.value > body.length) return null;
|
|
200
|
+
return { tag, bytes: body.subarray(len.next, len.next + len.value), next: len.next + len.value };
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
export function decodeReactionProto(body: Uint8Array): { messageId: string; emoji: string; removed: boolean } | null {
|
|
204
|
+
let messageId: string | null = null;
|
|
205
|
+
let emoji = '';
|
|
206
|
+
let removed = false;
|
|
207
|
+
for (let at = 0; at < body.length; ) {
|
|
208
|
+
const field = readField(body, at);
|
|
209
|
+
if (field === null) return null;
|
|
210
|
+
if (field.tag === PROTO_MESSAGE_ID) messageId = bytesToHex(field.bytes);
|
|
211
|
+
else {
|
|
212
|
+
emoji = utf8(field.bytes);
|
|
213
|
+
removed = field.tag === PROTO_WITHDRAW;
|
|
214
|
+
}
|
|
215
|
+
at = field.next;
|
|
216
|
+
}
|
|
217
|
+
return messageId === null || emoji === '' ? null : { messageId, emoji, removed };
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
function decodeGroup(type: number, body: Uint8Array): Decoded {
|
|
221
|
+
const group = readGroup(body);
|
|
222
|
+
if (group === null) return { kind: 'other', type };
|
|
223
|
+
const rest = body.subarray(ID_BYTES * 2);
|
|
224
|
+
if (type === MSG_GROUP_TEXT) return { kind: 'group-text', group, text: utf8(rest) };
|
|
225
|
+
if (type === MSG_GROUP_FILE) {
|
|
226
|
+
const file = decodeFileJson(rest);
|
|
227
|
+
return file === null ? { kind: 'other', type } : { kind: 'file', group, file };
|
|
228
|
+
}
|
|
229
|
+
if (type === MSG_GROUP_LEAVE) return { kind: 'group-leave', group };
|
|
230
|
+
if (type === MSG_GROUP_DELIVERY_RECEIPT) return { kind: 'receipt', group, status: rest[0] ?? 0, messageIds: ids(rest, 1) };
|
|
231
|
+
const reaction = decodeReactionProto(rest);
|
|
232
|
+
return reaction === null ? { kind: 'other', type } : { kind: 'reaction', group, ...reaction };
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
const GROUP_TYPES = new Set([MSG_GROUP_TEXT, MSG_GROUP_FILE, MSG_GROUP_LEAVE, MSG_GROUP_DELIVERY_RECEIPT, MSG_GROUP_REACTION]);
|
|
236
|
+
const DIRECT_TYPES = new Set([MSG_TEXT, MSG_FILE, MSG_TYPING, MSG_DELIVERY_RECEIPT, MSG_REACTION]);
|
|
237
|
+
|
|
238
|
+
function decodeControl(type: number, body: Uint8Array): Decoded {
|
|
239
|
+
if (body.length < ID_BYTES) return { kind: 'other', type };
|
|
240
|
+
const groupId = bytesToHex(body.subarray(0, ID_BYTES));
|
|
241
|
+
if (type === MSG_GROUP_SETUP) return { kind: 'group-setup', groupId, members: members(body, ID_BYTES) };
|
|
242
|
+
return { kind: 'group-rename', groupId, name: utf8(body.subarray(ID_BYTES)) };
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
function decodeDirect(type: number, body: Uint8Array): Decoded {
|
|
246
|
+
if (type === MSG_TEXT) return { kind: 'text', text: utf8(body) };
|
|
247
|
+
if (type === MSG_FILE) {
|
|
248
|
+
const file = decodeFileJson(body);
|
|
249
|
+
return file === null ? { kind: 'other', type } : { kind: 'file', group: null, file };
|
|
250
|
+
}
|
|
251
|
+
if (type === MSG_TYPING) return { kind: 'typing' };
|
|
252
|
+
if (type === MSG_DELIVERY_RECEIPT) return { kind: 'receipt', group: null, status: body[0] ?? 0, messageIds: ids(body, 1) };
|
|
253
|
+
const reaction = decodeReactionProto(body);
|
|
254
|
+
return reaction === null ? { kind: 'other', type } : { kind: 'reaction', group: null, ...reaction };
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
export function decode(plain: Uint8Array): Decoded {
|
|
258
|
+
const type = plain[0] ?? -1;
|
|
259
|
+
const body = plain.subarray(1);
|
|
260
|
+
if (DIRECT_TYPES.has(type)) return decodeDirect(type, body);
|
|
261
|
+
if (type === MSG_GROUP_SETUP || type === MSG_GROUP_RENAME) return decodeControl(type, body);
|
|
262
|
+
if (GROUP_TYPES.has(type)) return decodeGroup(type, body);
|
|
263
|
+
return { kind: 'other', type };
|
|
264
|
+
}
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
import { TrainError } from '@metro-labs/core/train-error';
|
|
2
|
+
import { respond } from '@metro-labs/core/stations/station-runtime';
|
|
3
|
+
import { publicKeyFor, targetOf, type Account, type Target } from './accounts.js';
|
|
4
|
+
import { sendE2E } from './api.js';
|
|
5
|
+
import { bytesToHex, quoted, seal } from './crypto.js';
|
|
6
|
+
import { encodeFileFor, fileLabel, packFile, type OutgoingFile } from './files.js';
|
|
7
|
+
import { emitOutbound, emitOutboundReaction } from './format.js';
|
|
8
|
+
import { MESSAGE_ID_RE } from './ids.js';
|
|
9
|
+
import {
|
|
10
|
+
encodeGroupReaction,
|
|
11
|
+
encodeGroupSyncRequest,
|
|
12
|
+
encodeGroupText,
|
|
13
|
+
encodeReaction,
|
|
14
|
+
encodeText,
|
|
15
|
+
type GroupRef,
|
|
16
|
+
} from './messages.js';
|
|
17
|
+
|
|
18
|
+
export const MAX_TEXT_BYTES = 3500;
|
|
19
|
+
const SENT_MAX = 2000;
|
|
20
|
+
|
|
21
|
+
type Args = Record<string, unknown>;
|
|
22
|
+
|
|
23
|
+
const sentIds = new Set<string>();
|
|
24
|
+
|
|
25
|
+
export function noteSent(messageId: string): void {
|
|
26
|
+
sentIds.add(messageId);
|
|
27
|
+
while (sentIds.size > SENT_MAX) {
|
|
28
|
+
const oldest = sentIds.values().next();
|
|
29
|
+
if (oldest.done) break;
|
|
30
|
+
sentIds.delete(oldest.value);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export const sentByUs = (messageId: string): boolean => sentIds.has(messageId);
|
|
35
|
+
|
|
36
|
+
async function deliver(acct: Account, to: string, plain: Uint8Array, group: boolean): Promise<string> {
|
|
37
|
+
const { nonce, box } = seal(plain, await publicKeyFor(acct, to), acct.keys);
|
|
38
|
+
const messageId = await sendE2E(acct.cfg, to, bytesToHex(nonce), bytesToHex(box), group);
|
|
39
|
+
noteSent(messageId);
|
|
40
|
+
return messageId;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function recipientsOf(acct: Account, group: GroupRef): string[] {
|
|
44
|
+
const members = acct.groups.recipients(group);
|
|
45
|
+
if (members === null)
|
|
46
|
+
throw new TrainError(
|
|
47
|
+
'threema_unknown_group',
|
|
48
|
+
'metro has not received this group\'s member list yet; it asked the group creator for it, try again in a moment',
|
|
49
|
+
{ retryable: true },
|
|
50
|
+
);
|
|
51
|
+
return members;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export async function requestSync(acct: Account, group: GroupRef): Promise<void> {
|
|
55
|
+
await deliver(acct, group.creator, encodeGroupSyncRequest(group.groupId), true);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
async function fanOut(acct: Account, target: Target, direct: Uint8Array, grouped: (g: GroupRef) => Uint8Array): Promise<string[]> {
|
|
59
|
+
if (target.kind === 'user') return [await deliver(acct, target.id, direct, false)];
|
|
60
|
+
const ids: string[] = [];
|
|
61
|
+
for (const member of recipientsOf(acct, target.group)) ids.push(await deliver(acct, member, grouped(target.group), true));
|
|
62
|
+
if (ids.length === 0) throw new TrainError('threema_empty_group', 'nobody else is in this group', { retryable: false });
|
|
63
|
+
return ids;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
interface SendArgs {
|
|
67
|
+
line: string;
|
|
68
|
+
text?: unknown;
|
|
69
|
+
replyTo?: unknown;
|
|
70
|
+
account?: string;
|
|
71
|
+
attachments?: unknown;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const filesOf = (raw: unknown): OutgoingFile[] =>
|
|
75
|
+
(Array.isArray(raw) ? raw : [])
|
|
76
|
+
.map((a) => (a ?? {}) as Record<string, unknown>)
|
|
77
|
+
.filter((a) => typeof a.path === 'string' && a.path !== '')
|
|
78
|
+
.map((a) => ({ path: String(a.path), mime: typeof a.mime === 'string' && a.mime !== '' ? a.mime : 'application/octet-stream', name: typeof a.name === 'string' && a.name !== '' ? a.name : 'file' }));
|
|
79
|
+
|
|
80
|
+
function textOf(a: SendArgs, files: OutgoingFile[]): string {
|
|
81
|
+
const text = typeof a.text === 'string' ? a.text : '';
|
|
82
|
+
if (text === '' && files.length === 0)
|
|
83
|
+
throw new TrainError('threema_text_required', 'give some text or a file to send', { retryable: false });
|
|
84
|
+
return text;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
async function sendFiles(acct: Account, target: Target, files: OutgoingFile[], caption: string | null): Promise<{ ids: string[]; labels: string[] }> {
|
|
88
|
+
const ids: string[] = [];
|
|
89
|
+
const labels: string[] = [];
|
|
90
|
+
for (const [i, file] of files.entries()) {
|
|
91
|
+
const packed = await packFile(acct, file, i === 0 ? caption : null);
|
|
92
|
+
const sent = await fanOut(acct, target, encodeFileFor(null, packed), (g) => encodeFileFor(g, packed));
|
|
93
|
+
ids.push(...sent);
|
|
94
|
+
labels.push(fileLabel(file));
|
|
95
|
+
}
|
|
96
|
+
return { ids, labels };
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function messageIdOf(raw: unknown, field: string): string {
|
|
100
|
+
const id = typeof raw === 'string' ? raw.toLowerCase() : '';
|
|
101
|
+
if (!MESSAGE_ID_RE.test(id))
|
|
102
|
+
throw new TrainError('threema_bad_message_id', `${field} must be a Threema message id, 16 hex characters`, { retryable: false });
|
|
103
|
+
return id;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
const replyTargetOf = (raw: unknown): string | undefined => (raw === undefined || raw === null || raw === '' ? undefined : messageIdOf(raw, 'replyTo'));
|
|
107
|
+
|
|
108
|
+
function bodyOf(text: string, replyTo: string | undefined): string {
|
|
109
|
+
const body = replyTo === undefined ? text : quoted(replyTo, text);
|
|
110
|
+
if (Buffer.byteLength(body, 'utf8') > MAX_TEXT_BYTES)
|
|
111
|
+
throw new TrainError('threema_message_too_long', `Threema carries at most ${MAX_TEXT_BYTES} bytes of text per message; split it up`, { retryable: false });
|
|
112
|
+
return body;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
interface Sent {
|
|
116
|
+
ids: string[];
|
|
117
|
+
labels: string[];
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
const resultOf = (acct: Account, sent: Sent): Record<string, unknown> => ({
|
|
121
|
+
messageId: sent.ids[0] ?? '',
|
|
122
|
+
account: acct.cfg.id,
|
|
123
|
+
...(sent.ids.length > 1 ? { messageIds: sent.ids } : {}),
|
|
124
|
+
...(sent.labels.length > 0 ? { attachments: sent.labels } : {}),
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
export async function send(id: string, args: Args): Promise<void> {
|
|
128
|
+
const a = args as unknown as SendArgs;
|
|
129
|
+
const files = filesOf(a.attachments);
|
|
130
|
+
const text = textOf(a, files);
|
|
131
|
+
const replyTo = replyTargetOf(a.replyTo);
|
|
132
|
+
const { acct, target } = targetOf(a.line, a.account);
|
|
133
|
+
const body = bodyOf(text, replyTo);
|
|
134
|
+
const sent: Sent =
|
|
135
|
+
files.length === 0
|
|
136
|
+
? { ids: await fanOut(acct, target, encodeText(body), (g) => encodeGroupText(g, body)), labels: [] }
|
|
137
|
+
: await sendFiles(acct, target, files, body === '' ? null : body);
|
|
138
|
+
emitOutbound(acct.cfg.id, a.line, sent.ids[0] ?? '', text === '' ? `[${sent.labels.join(', ')}]` : text, replyTo);
|
|
139
|
+
respond(id, { result: resultOf(acct, sent) });
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
interface ReactArgs {
|
|
143
|
+
line: string;
|
|
144
|
+
messageId?: unknown;
|
|
145
|
+
emoji?: unknown;
|
|
146
|
+
account?: string;
|
|
147
|
+
action?: unknown;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
export async function react(id: string, args: Args): Promise<void> {
|
|
151
|
+
const a = args as unknown as ReactArgs;
|
|
152
|
+
const emoji = typeof a.emoji === 'string' ? a.emoji.trim() : '';
|
|
153
|
+
if (emoji === '') throw new TrainError('threema_emoji_required', 'react needs an emoji', { retryable: false });
|
|
154
|
+
const messageId = messageIdOf(a.messageId, 'messageId');
|
|
155
|
+
const removed = a.action === 'removed';
|
|
156
|
+
const { acct, target } = targetOf(a.line, a.account);
|
|
157
|
+
const ids = await fanOut(acct, target, encodeReaction(messageId, emoji, removed), (g) => encodeGroupReaction(g, messageId, emoji, removed));
|
|
158
|
+
emitOutboundReaction(acct.cfg.id, a.line, ids[0] ?? '', emoji, messageId, removed);
|
|
159
|
+
respond(id, { result: { messageId: ids[0] ?? '', account: acct.cfg.id, emoji, removed } });
|
|
160
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { Station, Verb } from '@metro-labs/core/stations/types';
|
|
2
|
+
import { groupFiles } from './groups.js';
|
|
3
|
+
|
|
4
|
+
export const threemaStation: Station = {
|
|
5
|
+
name: 'threema',
|
|
6
|
+
hasAccounts: true,
|
|
7
|
+
hasTrain: true,
|
|
8
|
+
messageVerbs: new Set<Verb>(['send', 'reply', 'react', 'unreact']),
|
|
9
|
+
attachmentMode: 'canonical',
|
|
10
|
+
forget: groupFiles.forget,
|
|
11
|
+
forgetExcept: groupFiles.forgetExcept,
|
|
12
|
+
tools: [],
|
|
13
|
+
};
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import { createPrivateKey, createPublicKey } from 'node:crypto';
|
|
2
|
+
import {
|
|
3
|
+
GATEWAY_API,
|
|
4
|
+
isGatewayId,
|
|
5
|
+
normalizeThreemaId,
|
|
6
|
+
parsePrivateKey,
|
|
7
|
+
} from './ids.js';
|
|
8
|
+
|
|
9
|
+
export { parsePrivateKey } from './ids.js';
|
|
10
|
+
|
|
11
|
+
export class ThreemaGatewayError extends Error {}
|
|
12
|
+
|
|
13
|
+
export interface ThreemaCredentials {
|
|
14
|
+
gatewayId: string;
|
|
15
|
+
secret: string;
|
|
16
|
+
privateKey: string;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export interface ThreemaGatewayIdentity {
|
|
20
|
+
gatewayId: string;
|
|
21
|
+
publicKey: string;
|
|
22
|
+
credits: number;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const PKCS8_X25519_PREFIX = Buffer.from(
|
|
26
|
+
'302e020100300506032b656e04220420',
|
|
27
|
+
'hex',
|
|
28
|
+
);
|
|
29
|
+
const TIMEOUT_MS = 10_000;
|
|
30
|
+
const PUBLIC_KEY_RE = /^[0-9a-f]{64}$/;
|
|
31
|
+
|
|
32
|
+
export function publicKeyOf(privateKeyHex: string): string {
|
|
33
|
+
const der = Buffer.concat([
|
|
34
|
+
PKCS8_X25519_PREFIX,
|
|
35
|
+
Buffer.from(privateKeyHex, 'hex'),
|
|
36
|
+
]);
|
|
37
|
+
const key = createPrivateKey({ key: der, format: 'der', type: 'pkcs8' });
|
|
38
|
+
const jwk = createPublicKey(key).export({ format: 'jwk' });
|
|
39
|
+
return Buffer.from(String(jwk.x), 'base64url').toString('hex');
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
async function gatewayGet(
|
|
43
|
+
path: string,
|
|
44
|
+
gatewayId: string,
|
|
45
|
+
secret: string,
|
|
46
|
+
): Promise<Response> {
|
|
47
|
+
const query = new URLSearchParams({ from: gatewayId, secret });
|
|
48
|
+
try {
|
|
49
|
+
return await fetch(`${GATEWAY_API}${path}?${query.toString()}`, {
|
|
50
|
+
signal: AbortSignal.timeout(TIMEOUT_MS),
|
|
51
|
+
});
|
|
52
|
+
} catch {
|
|
53
|
+
throw new ThreemaGatewayError(
|
|
54
|
+
'could not reach the Threema Gateway to check those credentials',
|
|
55
|
+
);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function refused(res: Response, what: string): ThreemaGatewayError {
|
|
60
|
+
if (res.status === 401)
|
|
61
|
+
return new ThreemaGatewayError(
|
|
62
|
+
'Threema rejected that Gateway ID or API secret',
|
|
63
|
+
);
|
|
64
|
+
if (res.status === 404)
|
|
65
|
+
return new ThreemaGatewayError(`Threema does not know ${what}`);
|
|
66
|
+
return new ThreemaGatewayError(
|
|
67
|
+
`the Threema Gateway answered ${res.status} while checking ${what}`,
|
|
68
|
+
);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
async function fetchCredits(gatewayId: string, secret: string): Promise<number> {
|
|
72
|
+
const res = await gatewayGet('/credits', gatewayId, secret);
|
|
73
|
+
if (!res.ok) throw refused(res, 'the credit balance');
|
|
74
|
+
const credits = Number((await res.text()).trim());
|
|
75
|
+
if (!Number.isInteger(credits))
|
|
76
|
+
throw new ThreemaGatewayError(
|
|
77
|
+
'the Threema Gateway gave an unexpected answer for the credit balance',
|
|
78
|
+
);
|
|
79
|
+
return credits;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
async function fetchPublicKey(gatewayId: string, secret: string): Promise<string> {
|
|
83
|
+
const res = await gatewayGet(
|
|
84
|
+
`/pubkeys/${encodeURIComponent(gatewayId)}`,
|
|
85
|
+
gatewayId,
|
|
86
|
+
secret,
|
|
87
|
+
);
|
|
88
|
+
if (!res.ok) throw refused(res, `the public key of ${gatewayId}`);
|
|
89
|
+
const hex = (await res.text()).trim().toLowerCase();
|
|
90
|
+
if (!PUBLIC_KEY_RE.test(hex))
|
|
91
|
+
throw new ThreemaGatewayError(
|
|
92
|
+
'the Threema Gateway gave an unexpected answer for the public key',
|
|
93
|
+
);
|
|
94
|
+
return hex;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export async function verifyThreemaGateway(
|
|
98
|
+
input: ThreemaCredentials,
|
|
99
|
+
): Promise<ThreemaGatewayIdentity> {
|
|
100
|
+
const gatewayId = normalizeThreemaId(input.gatewayId);
|
|
101
|
+
if (!isGatewayId(gatewayId))
|
|
102
|
+
throw new ThreemaGatewayError(
|
|
103
|
+
'a Threema Gateway ID is a * followed by 7 letters or digits, as in *ABCDEFG',
|
|
104
|
+
);
|
|
105
|
+
const privateKey = parsePrivateKey(input.privateKey);
|
|
106
|
+
if (privateKey === null)
|
|
107
|
+
throw new ThreemaGatewayError(
|
|
108
|
+
'the private key is the 64 hex characters from the key file Threema gave you, with or without its private: prefix',
|
|
109
|
+
);
|
|
110
|
+
const credits = await fetchCredits(gatewayId, input.secret);
|
|
111
|
+
const registered = await fetchPublicKey(gatewayId, input.secret);
|
|
112
|
+
if (registered !== publicKeyOf(privateKey))
|
|
113
|
+
throw new ThreemaGatewayError(
|
|
114
|
+
`that private key does not belong to ${gatewayId}: the Gateway holds a different public key for it`,
|
|
115
|
+
);
|
|
116
|
+
return { gatewayId, publicKey: registered, credits };
|
|
117
|
+
}
|