@stage-labs/metro 0.1.0-beta.16 → 0.1.0-beta.161
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 +8 -57
- package/dist/claude-settings.js +40 -0
- package/dist/claude.js +182 -0
- package/dist/cli.js +56 -39
- package/dist/control.js +74 -0
- package/dist/hold.js +211 -0
- package/dist/local.js +77 -0
- package/dist/mcp-config.js +25 -0
- package/dist/node-name.js +71 -0
- package/dist/onboarding.js +43 -0
- package/dist/provider-flags.js +9 -0
- package/dist/route.js +50 -0
- package/dist/runtime-install.js +97 -0
- package/dist/runtime.js +74 -0
- package/dist/serve.js +155 -0
- package/dist/service.js +242 -0
- package/dist/tail.js +119 -0
- package/dist/update.js +13 -1
- package/package.json +6 -5
- package/runtime/marketplace/.claude-plugin/marketplace.json +12 -0
- package/runtime/marketplace/plugin/.claude-plugin/plugin.json +18 -0
- package/runtime/marketplace/plugin/.mcp.json +1 -0
- package/runtime/marketplace/plugin/bin/guard.mjs +55 -0
- package/runtime/marketplace/plugin/bin/metro-plugin.mjs +40 -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 +16 -0
- package/runtime/node_modules/@metro-labs/core/package.json +44 -0
- package/runtime/node_modules/@metro-labs/core/src/address.ts +6 -0
- package/runtime/node_modules/@metro-labs/core/src/endpoints.ts +7 -0
- package/runtime/node_modules/@metro-labs/core/src/events.ts +120 -0
- package/runtime/node_modules/@metro-labs/core/src/identity.ts +7 -0
- package/runtime/node_modules/@metro-labs/core/src/ids.ts +21 -0
- package/runtime/node_modules/@metro-labs/core/src/is-record.ts +2 -0
- package/runtime/node_modules/@metro-labs/core/src/lines.ts +87 -0
- package/runtime/node_modules/@metro-labs/core/src/log.ts +42 -0
- package/runtime/node_modules/@metro-labs/core/src/protocol.ts +226 -0
- package/runtime/node_modules/@metro-labs/core/src/secure-fs.ts +57 -0
- package/runtime/node_modules/@metro-labs/core/src/station-names.ts +13 -0
- package/runtime/node_modules/@metro-labs/core/src/stations/account-store.ts +60 -0
- package/runtime/node_modules/@metro-labs/core/src/stations/attachments.ts +196 -0
- package/runtime/node_modules/@metro-labs/core/src/stations/messaging-normalize.ts +88 -0
- package/runtime/node_modules/@metro-labs/core/src/stations/profile.ts +58 -0
- package/runtime/node_modules/@metro-labs/core/src/stations/station-runtime.ts +64 -0
- package/runtime/node_modules/@metro-labs/core/src/stations/types.ts +112 -0
- package/runtime/node_modules/@metro-labs/core/src/str.ts +5 -0
- package/runtime/node_modules/@metro-labs/core/src/train-error.ts +53 -0
- package/runtime/node_modules/@metro-labs/core/src/version.ts +2 -0
- package/runtime/node_modules/@metro-labs/daemon/package.json +47 -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-routes.ts +72 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/accounts-api.ts +388 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/admin.ts +39 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/allowlist.ts +37 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/api.ts +225 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/bundle.ts +122 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/file-admin.ts +364 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/files.ts +185 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/keys.ts +40 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/map.ts +95 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/scope.ts +64 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/senders.ts +38 -0
- package/runtime/node_modules/@metro-labs/daemon/src/boot/boot.ts +200 -0
- package/runtime/node_modules/@metro-labs/daemon/src/boot/connect-hint.ts +39 -0
- package/runtime/node_modules/@metro-labs/daemon/src/boot/crash-guard.ts +49 -0
- package/runtime/node_modules/@metro-labs/daemon/src/boot/local-owner.ts +30 -0
- package/runtime/node_modules/@metro-labs/daemon/src/boot/paths.ts +106 -0
- package/runtime/node_modules/@metro-labs/daemon/src/channels/addressed.ts +25 -0
- package/runtime/node_modules/@metro-labs/daemon/src/channels/dedupe.ts +24 -0
- package/runtime/node_modules/@metro-labs/daemon/src/channels/inbound.ts +380 -0
- package/runtime/node_modules/@metro-labs/daemon/src/channels/media-note.ts +85 -0
- package/runtime/node_modules/@metro-labs/daemon/src/channels/pending.ts +75 -0
- package/runtime/node_modules/@metro-labs/daemon/src/channels/relay.ts +129 -0
- package/runtime/node_modules/@metro-labs/daemon/src/channels/webhook-note.ts +51 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/api.ts +354 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/files.ts +358 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/login.ts +169 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/onboarding.ts +56 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/plugin-install.ts +140 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/session-files.ts +90 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/session.ts +253 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/settings.ts +117 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/setup.ts +263 -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 +109 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/api.ts +313 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/config.ts +157 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/health.ts +18 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/model.ts +69 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/oauth-client.ts +166 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/oauth-discovery.ts +139 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/oauth-pending.ts +65 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/oauth-routes.ts +171 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/oauth.ts +155 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/plugin-sync.ts +122 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/reach.ts +27 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/relay-target.ts +39 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/relay.ts +304 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/store.ts +299 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/tools.ts +108 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/url.ts +59 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/verify.ts +101 -0
- package/runtime/node_modules/@metro-labs/daemon/src/files/attach-grant.ts +99 -0
- package/runtime/node_modules/@metro-labs/daemon/src/files/attach-owner.ts +32 -0
- package/runtime/node_modules/@metro-labs/daemon/src/files/attach-serve.ts +121 -0
- package/runtime/node_modules/@metro-labs/daemon/src/files/upload-api.ts +294 -0
- package/runtime/node_modules/@metro-labs/daemon/src/files/upload-store.ts +181 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/bedrock.ts +259 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/codex-auth.ts +231 -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 +288 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/codex-translate.ts +203 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/codex.ts +231 -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 +187 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/gateway.ts +247 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/gemini-auth.ts +182 -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 +148 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/gemini-translate.ts +229 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/gemini.ts +283 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/model-api.ts +362 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/model-config.ts +232 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/openrouter.ts +90 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/provider-models.ts +65 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/served.ts +17 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/usage.ts +242 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/accounts.ts +151 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/call-tools.ts +216 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/channel-owner.ts +29 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/ctx.ts +62 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/event-store.ts +91 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/frame-scope.ts +21 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/group-tools.ts +129 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/index.ts +186 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/member-tools.ts +72 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/permission-relay.ts +66 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/profile-tool.ts +94 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/raw-get-stream.ts +207 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/request-identity.ts +47 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/session-registry.ts +172 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/session-route.ts +43 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/session.ts +215 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/tool-dispatch.ts +219 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/tool-schemas.ts +400 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/upload-tool.ts +66 -0
- package/runtime/node_modules/@metro-labs/daemon/src/monitor/api.ts +237 -0
- package/runtime/node_modules/@metro-labs/daemon/src/net/tunnel.ts +389 -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/daemon/src/routes/http.ts +370 -0
- package/runtime/node_modules/@metro-labs/daemon/src/routes/local-mode.ts +208 -0
- package/runtime/node_modules/@metro-labs/daemon/src/routes/session-apis.ts +58 -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 +65 -0
- package/runtime/node_modules/@metro-labs/daemon/src/server/machine.ts +80 -0
- package/runtime/node_modules/@metro-labs/daemon/src/server/owner.ts +48 -0
- package/runtime/node_modules/@metro-labs/daemon/src/server/update.ts +117 -0
- package/runtime/node_modules/@metro-labs/daemon/src/server.ts +2 -0
- package/runtime/node_modules/@metro-labs/daemon/src/stations/attach-inline.ts +100 -0
- package/runtime/node_modules/@metro-labs/daemon/src/stations/attach-interactive.ts +161 -0
- package/runtime/node_modules/@metro-labs/daemon/src/stations/attach-resolve.ts +202 -0
- package/runtime/node_modules/@metro-labs/daemon/src/stations/attach-session.ts +280 -0
- package/runtime/node_modules/@metro-labs/daemon/src/stations/attach-xmtp.ts +111 -0
- package/runtime/node_modules/@metro-labs/daemon/src/stations/attach.ts +248 -0
- package/runtime/node_modules/@metro-labs/daemon/src/stations/materialize.ts +261 -0
- package/runtime/node_modules/@metro-labs/daemon/src/stations/registry.ts +37 -0
- package/runtime/node_modules/@metro-labs/daemon/src/stations/runtime-deps.ts +82 -0
- package/runtime/node_modules/@metro-labs/daemon/src/stations/supervisor-io.ts +97 -0
- package/runtime/node_modules/@metro-labs/daemon/src/stations/supervisor.ts +320 -0
- package/runtime/node_modules/@metro-labs/daemon/src/stations/threema-callbacks.ts +32 -0
- package/runtime/node_modules/@metro-labs/daemon/src/stations/train-call.ts +22 -0
- package/runtime/node_modules/@metro-labs/daemon/src/terminal/api.ts +97 -0
- package/runtime/node_modules/@metro-labs/daemon/src/terminal/socket.ts +123 -0
- package/runtime/node_modules/@metro-labs/daemon/src/terminal/tickets.ts +40 -0
- package/runtime/node_modules/@metro-labs/discord-bot/package.json +28 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/accounts.ts +118 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/actions.ts +390 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/api-base.ts +1 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/attachments.ts +29 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/format.ts +220 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/group-actions.ts +50 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/group.ts +115 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/index.ts +127 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/members.ts +215 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/profile.ts +48 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/send-files.ts +43 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/station.ts +21 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/verify.ts +82 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/voice-speak.ts +195 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/voice-transcribe.ts +292 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/voice.ts +197 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/wire.ts +5 -0
- package/runtime/node_modules/@metro-labs/http/package.json +29 -0
- package/runtime/node_modules/@metro-labs/http/src/api-error.ts +8 -0
- package/runtime/node_modules/@metro-labs/http/src/api-http.ts +106 -0
- package/runtime/node_modules/@metro-labs/http/src/cors.ts +29 -0
- package/runtime/node_modules/@metro-labs/http/src/mode-api.ts +32 -0
- package/runtime/node_modules/@metro-labs/http/src/return-to.ts +21 -0
- package/runtime/node_modules/@metro-labs/http/src/workos-token.ts +150 -0
- package/runtime/node_modules/@metro-labs/telegram/package.json +26 -0
- package/runtime/node_modules/@metro-labs/telegram/src/accounts.ts +80 -0
- package/runtime/node_modules/@metro-labs/telegram/src/actions.ts +256 -0
- package/runtime/node_modules/@metro-labs/telegram/src/attachments.ts +87 -0
- package/runtime/node_modules/@metro-labs/telegram/src/client.ts +77 -0
- package/runtime/node_modules/@metro-labs/telegram/src/format.ts +156 -0
- package/runtime/node_modules/@metro-labs/telegram/src/group.ts +261 -0
- package/runtime/node_modules/@metro-labs/telegram/src/history.ts +52 -0
- package/runtime/node_modules/@metro-labs/telegram/src/inbound.ts +78 -0
- package/runtime/node_modules/@metro-labs/telegram/src/index.ts +46 -0
- package/runtime/node_modules/@metro-labs/telegram/src/login.ts +166 -0
- package/runtime/node_modules/@metro-labs/telegram/src/media-actions.ts +80 -0
- package/runtime/node_modules/@metro-labs/telegram/src/members.ts +75 -0
- package/runtime/node_modules/@metro-labs/telegram/src/normalize.ts +22 -0
- package/runtime/node_modules/@metro-labs/telegram/src/profile.ts +36 -0
- package/runtime/node_modules/@metro-labs/telegram/src/reactions.ts +167 -0
- package/runtime/node_modules/@metro-labs/telegram/src/station.ts +26 -0
- package/runtime/node_modules/@metro-labs/telegram/src/types.ts +7 -0
- package/runtime/node_modules/@metro-labs/telegram/src/wire.ts +1 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/package.json +25 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/accounts.ts +120 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/actions.ts +297 -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 +201 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/index.ts +109 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/media-actions.ts +166 -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 +33 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/station.ts +19 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/types.ts +58 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/verify.ts +38 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/wire.ts +3 -0
- package/runtime/node_modules/@metro-labs/threema/package.json +26 -0
- package/runtime/node_modules/@metro-labs/threema/src/accounts.ts +107 -0
- package/runtime/node_modules/@metro-labs/threema/src/actions.ts +135 -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 +93 -0
- package/runtime/node_modules/@metro-labs/threema/src/format.ts +182 -0
- package/runtime/node_modules/@metro-labs/threema/src/groups.ts +95 -0
- package/runtime/node_modules/@metro-labs/threema/src/ids.ts +21 -0
- package/runtime/node_modules/@metro-labs/threema/src/inbound.ts +90 -0
- package/runtime/node_modules/@metro-labs/threema/src/index.ts +29 -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 +10 -0
- package/runtime/node_modules/@metro-labs/threema/src/verify.ts +117 -0
- package/runtime/node_modules/@metro-labs/webhook/package.json +23 -0
- package/runtime/node_modules/@metro-labs/webhook/src/station.ts +37 -0
- package/runtime/node_modules/@metro-labs/whatsapp/package.json +27 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/accounts.ts +66 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/ack.ts +101 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/actions.ts +203 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/attachments.ts +72 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/auth-state.ts +129 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/client.ts +359 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/delivery.ts +49 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/format.ts +138 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/inbound.ts +57 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/index.ts +40 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/keys.ts +59 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/logger.ts +60 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/login.ts +176 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/media.ts +84 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/normalize.ts +22 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/outbox.ts +38 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/parse.ts +185 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/resolve.ts +52 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/station.ts +20 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/token-store.ts +98 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/types.ts +6 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/wire.ts +5 -0
- package/runtime/node_modules/@metro-labs/xmtp/package.json +35 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/accounts.ts +115 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/actions-close.ts +110 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/actions-conv.ts +299 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/actions-meta.ts +157 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/actions-push.ts +87 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/actions.ts +380 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/attachments.ts +107 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/codecs.ts +171 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/conv-helpers.ts +239 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/emit-core.ts +55 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/emit-payloads.ts +335 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/emit.ts +80 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/group-members.ts +34 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/group.ts +56 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/identity.ts +47 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/index.ts +125 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/labels.ts +157 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/member-args.ts +40 -0
- 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/push.ts +349 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/smart.ts +72 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/station.ts +16 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/tools-handlers.ts +126 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/tools.ts +212 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/transcribe.ts +90 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/verify.ts +98 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/wire.ts +53 -0
- package/runtime/runtime.json +3 -0
- package/runtime/server.ts +1 -0
- package/runtime/stations.json +39 -0
- package/runtime/trains/.keep +0 -0
- package/dist/login.js +0 -102
- package/dist/store.js +0 -63
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
import { emit, mintId } from '@metro-labs/core/stations/station-runtime';
|
|
2
|
+
import { unquote } from './crypto.js';
|
|
3
|
+
import { groupKey } from './groups.js';
|
|
4
|
+
import { RECEIPT_ACK, RECEIPT_DECLINE, type GroupRef } from './messages.js';
|
|
5
|
+
|
|
6
|
+
const SELF_URI = process.env.METRO_SELF_URI ?? '';
|
|
7
|
+
|
|
8
|
+
const REACTION_OF: Record<number, string> = {
|
|
9
|
+
[RECEIPT_ACK]: '👍',
|
|
10
|
+
[RECEIPT_DECLINE]: '👎',
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
export const lineOf = (accountId: string, threemaId: string): string =>
|
|
14
|
+
`metro://threema/${accountId}/${threemaId}`;
|
|
15
|
+
|
|
16
|
+
export const groupLineOf = (accountId: string, group: GroupRef): string =>
|
|
17
|
+
`metro://threema/${accountId}/${groupKey(group)}`;
|
|
18
|
+
|
|
19
|
+
const userOf = (accountId: string, threemaId: string): string =>
|
|
20
|
+
`metro://threema/${accountId}/user/${threemaId}`;
|
|
21
|
+
|
|
22
|
+
export interface InboundMeta {
|
|
23
|
+
from: string;
|
|
24
|
+
to: string;
|
|
25
|
+
messageId: string;
|
|
26
|
+
date: string;
|
|
27
|
+
nickname?: string;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export interface Room {
|
|
31
|
+
group: GroupRef | null;
|
|
32
|
+
name: string | null;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export const DIRECT: Room = { group: null, name: null };
|
|
36
|
+
|
|
37
|
+
const MENTION_ALL = '@@@@@@@@';
|
|
38
|
+
|
|
39
|
+
export const mentionsSelf = (text: string, self: string): boolean =>
|
|
40
|
+
text.includes(MENTION_ALL) || text.toUpperCase().includes(`@@${self.toUpperCase()}`);
|
|
41
|
+
|
|
42
|
+
function tsOf(date: string): string {
|
|
43
|
+
const seconds = Number(date);
|
|
44
|
+
return Number.isFinite(seconds) && seconds > 0
|
|
45
|
+
? new Date(seconds * 1000).toISOString()
|
|
46
|
+
: new Date().toISOString();
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function base(
|
|
50
|
+
accountId: string,
|
|
51
|
+
m: InboundMeta,
|
|
52
|
+
type: string,
|
|
53
|
+
room: Room,
|
|
54
|
+
): Record<string, unknown> {
|
|
55
|
+
const group = room.group;
|
|
56
|
+
return {
|
|
57
|
+
kind: 'inbound',
|
|
58
|
+
id: mintId(),
|
|
59
|
+
ts: tsOf(m.date),
|
|
60
|
+
station: 'threema',
|
|
61
|
+
line: group === null ? lineOf(accountId, m.from) : groupLineOf(accountId, group),
|
|
62
|
+
line_name: group === null ? (m.nickname ?? m.from) : (room.name ?? groupKey(group)),
|
|
63
|
+
from: userOf(accountId, m.from),
|
|
64
|
+
from_name: m.nickname,
|
|
65
|
+
from_display_name: m.nickname,
|
|
66
|
+
is_private: group === null,
|
|
67
|
+
payload: { from: m.from, to: m.to, nickname: m.nickname ?? null, type, ...(group === null ? {} : { group: groupKey(group) }) },
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export function textEnvelope(
|
|
72
|
+
accountId: string,
|
|
73
|
+
m: InboundMeta,
|
|
74
|
+
raw: string,
|
|
75
|
+
sentByUs: (messageId: string) => boolean,
|
|
76
|
+
room: Room = DIRECT,
|
|
77
|
+
): Record<string, unknown> {
|
|
78
|
+
const { replyTo, text } = unquote(raw);
|
|
79
|
+
return {
|
|
80
|
+
...base(accountId, m, 'text', room),
|
|
81
|
+
message_id: m.messageId,
|
|
82
|
+
text,
|
|
83
|
+
...(room.group !== null && mentionsSelf(text, m.to) ? { mentions_self: true } : {}),
|
|
84
|
+
...(replyTo === undefined
|
|
85
|
+
? {}
|
|
86
|
+
: {
|
|
87
|
+
reply_to: replyTo,
|
|
88
|
+
event: { type: 'reply', replyTo },
|
|
89
|
+
reply_to_self: sentByUs(replyTo),
|
|
90
|
+
}),
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export function reactionEnvelope(
|
|
95
|
+
accountId: string,
|
|
96
|
+
m: InboundMeta,
|
|
97
|
+
emoji: string,
|
|
98
|
+
targetId: string,
|
|
99
|
+
removed: boolean,
|
|
100
|
+
room: Room = DIRECT,
|
|
101
|
+
type = 'reaction',
|
|
102
|
+
): Record<string, unknown> {
|
|
103
|
+
return {
|
|
104
|
+
...base(accountId, m, type, room),
|
|
105
|
+
message_id: `${m.messageId}:${targetId}`,
|
|
106
|
+
text: `[react ${emoji}${removed ? ' (removed)' : ''}]`,
|
|
107
|
+
emoji,
|
|
108
|
+
event: { type: 'react', emoji, targetId, ...(removed ? { removed: true } : {}) },
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
export function receiptEnvelope(
|
|
113
|
+
accountId: string,
|
|
114
|
+
m: InboundMeta,
|
|
115
|
+
status: number,
|
|
116
|
+
targetId: string,
|
|
117
|
+
room: Room = DIRECT,
|
|
118
|
+
): Record<string, unknown> | null {
|
|
119
|
+
const emoji = REACTION_OF[status];
|
|
120
|
+
return emoji === undefined ? null : reactionEnvelope(accountId, m, emoji, targetId, false, room, 'receipt');
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
export function emitInbound(
|
|
124
|
+
accountId: string,
|
|
125
|
+
owner: string | undefined,
|
|
126
|
+
env: Record<string, unknown>,
|
|
127
|
+
): void {
|
|
128
|
+
const payload = {
|
|
129
|
+
...(env.payload as Record<string, unknown> | undefined),
|
|
130
|
+
account: accountId,
|
|
131
|
+
};
|
|
132
|
+
emit({ ...env, ...(owner ? { to: owner } : {}), account: accountId, payload });
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
export function emitOutbound(
|
|
136
|
+
accountId: string,
|
|
137
|
+
line: string,
|
|
138
|
+
messageId: string,
|
|
139
|
+
text: string,
|
|
140
|
+
replyTo?: string,
|
|
141
|
+
): void {
|
|
142
|
+
emit({
|
|
143
|
+
kind: 'outbound',
|
|
144
|
+
id: mintId(),
|
|
145
|
+
ts: new Date().toISOString(),
|
|
146
|
+
station: 'threema',
|
|
147
|
+
line,
|
|
148
|
+
from: SELF_URI,
|
|
149
|
+
to: line,
|
|
150
|
+
message_id: messageId,
|
|
151
|
+
text,
|
|
152
|
+
reply_to: replyTo,
|
|
153
|
+
...(replyTo ? { event: { type: 'reply', replyTo } } : {}),
|
|
154
|
+
account: accountId,
|
|
155
|
+
payload: { account: accountId },
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
export function emitOutboundReaction(
|
|
160
|
+
accountId: string,
|
|
161
|
+
line: string,
|
|
162
|
+
messageId: string,
|
|
163
|
+
emoji: string,
|
|
164
|
+
targetId: string,
|
|
165
|
+
removed: boolean,
|
|
166
|
+
): void {
|
|
167
|
+
emit({
|
|
168
|
+
kind: 'outbound',
|
|
169
|
+
id: mintId(),
|
|
170
|
+
ts: new Date().toISOString(),
|
|
171
|
+
station: 'threema',
|
|
172
|
+
line,
|
|
173
|
+
from: SELF_URI,
|
|
174
|
+
to: line,
|
|
175
|
+
message_id: messageId,
|
|
176
|
+
text: `[react ${emoji}${removed ? ' (removed)' : ''}]`,
|
|
177
|
+
emoji,
|
|
178
|
+
event: { type: 'react', emoji, targetId, ...(removed ? { removed: true } : {}) },
|
|
179
|
+
account: accountId,
|
|
180
|
+
payload: { account: accountId },
|
|
181
|
+
});
|
|
182
|
+
}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { mkdirSync, readFileSync, renameSync, writeFileSync } from 'node:fs';
|
|
2
|
+
import { homedir } from 'node:os';
|
|
3
|
+
import { dirname, join } from 'node:path';
|
|
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
|
+
const groupsFile = (accountId: string): string =>
|
|
23
|
+
join(process.env.THREEMA_GROUPS_DIR ?? join(homedir(), '.metro'), `threema-groups-${accountId}.json`);
|
|
24
|
+
|
|
25
|
+
function readRosters(accountId: string): Map<string, GroupRoster> {
|
|
26
|
+
try {
|
|
27
|
+
const raw: unknown = JSON.parse(readFileSync(groupsFile(accountId), 'utf8'));
|
|
28
|
+
const out = new Map<string, GroupRoster>();
|
|
29
|
+
if (Array.isArray(raw))
|
|
30
|
+
for (const entry of raw) {
|
|
31
|
+
const g = (entry ?? {}) as Partial<GroupRoster>;
|
|
32
|
+
if (typeof g.creator === 'string' && typeof g.groupId === 'string' && Array.isArray(g.members))
|
|
33
|
+
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 });
|
|
34
|
+
}
|
|
35
|
+
return out;
|
|
36
|
+
} catch {
|
|
37
|
+
return new Map();
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export class GroupStore {
|
|
42
|
+
private readonly rosters: Map<string, GroupRoster>;
|
|
43
|
+
|
|
44
|
+
constructor(
|
|
45
|
+
private readonly accountId: string,
|
|
46
|
+
private readonly self: string,
|
|
47
|
+
) {
|
|
48
|
+
this.rosters = readRosters(accountId);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
get(group: GroupRef): GroupRoster | undefined {
|
|
52
|
+
return this.rosters.get(groupKey(group));
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
list(): GroupRoster[] {
|
|
56
|
+
return [...this.rosters.values()];
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
setup(creator: string, groupId: string, members: string[]): GroupRoster {
|
|
60
|
+
const key = groupKey({ creator, groupId });
|
|
61
|
+
const held = this.rosters.get(key);
|
|
62
|
+
const roster: GroupRoster = { creator, groupId, members: [...new Set([creator, ...members])], name: held?.name ?? null };
|
|
63
|
+
this.rosters.set(key, roster);
|
|
64
|
+
this.save();
|
|
65
|
+
return roster;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
rename(creator: string, groupId: string, name: string): void {
|
|
69
|
+
const held = this.rosters.get(groupKey({ creator, groupId }));
|
|
70
|
+
if (held === undefined) return;
|
|
71
|
+
held.name = name;
|
|
72
|
+
this.save();
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
leave(group: GroupRef, member: string): void {
|
|
76
|
+
const held = this.rosters.get(groupKey(group));
|
|
77
|
+
if (held === undefined) return;
|
|
78
|
+
if (member === this.self) this.rosters.delete(groupKey(group));
|
|
79
|
+
else held.members = held.members.filter((m) => m !== member);
|
|
80
|
+
this.save();
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
recipients(group: GroupRef): string[] | null {
|
|
84
|
+
const held = this.get(group);
|
|
85
|
+
return held === undefined ? null : held.members.filter((m) => m !== this.self);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
private save(): void {
|
|
89
|
+
const path = groupsFile(this.accountId);
|
|
90
|
+
mkdirSync(dirname(path), { recursive: true });
|
|
91
|
+
const tmp = `${path}.${String(process.pid)}.tmp`;
|
|
92
|
+
writeFileSync(tmp, JSON.stringify(this.list()), { mode: 0o600 });
|
|
93
|
+
renameSync(tmp, path);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
@@ -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,90 @@
|
|
|
1
|
+
import type { Account } from './accounts.js';
|
|
2
|
+
import {
|
|
3
|
+
DIRECT,
|
|
4
|
+
emitInbound,
|
|
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, acct.cfg.owner, 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
|
+
const owner = acct.cfg.owner;
|
|
60
|
+
if (d.kind === 'text') {
|
|
61
|
+
emitInbound(acct.cfg.id, owner, textEnvelope(acct.cfg.id, m, d.text, sentByUs));
|
|
62
|
+
return 'text';
|
|
63
|
+
}
|
|
64
|
+
if (d.kind === 'group-text') {
|
|
65
|
+
askForRoster(acct, d.group);
|
|
66
|
+
emitInbound(acct.cfg.id, owner, textEnvelope(acct.cfg.id, m, d.text, sentByUs, roomOf(acct, d.group)));
|
|
67
|
+
return 'group-text';
|
|
68
|
+
}
|
|
69
|
+
if (d.kind === 'file') {
|
|
70
|
+
if (d.group !== null) askForRoster(acct, d.group);
|
|
71
|
+
deliverFile(acct, m, d.file, roomOf(acct, d.group), sentByUs);
|
|
72
|
+
return 'file';
|
|
73
|
+
}
|
|
74
|
+
if (d.kind === 'reaction') {
|
|
75
|
+
if (d.group !== null) askForRoster(acct, d.group);
|
|
76
|
+
emitInbound(acct.cfg.id, owner, reactionEnvelope(acct.cfg.id, m, d.emoji, d.messageId, d.removed, roomOf(acct, d.group)));
|
|
77
|
+
return 'reaction';
|
|
78
|
+
}
|
|
79
|
+
return null;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export function deliver(acct: Account, m: InboundMeta, d: Decoded): string {
|
|
83
|
+
const handled = control(acct, m, d) ?? chat(acct, m, d);
|
|
84
|
+
if (handled !== null) return handled;
|
|
85
|
+
if (d.kind === 'receipt') return reactions(acct, m, d);
|
|
86
|
+
if (d.kind === 'typing') return 'typing';
|
|
87
|
+
const type = d.kind === 'other' ? `0x${d.type.toString(16)}` : d.kind;
|
|
88
|
+
process.stderr.write(`threema[${acct.cfg.id}]: ignored a message of type ${type} from ${m.from}\n`);
|
|
89
|
+
return `ignored:${type}`;
|
|
90
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { errMsg } from '@metro-labs/core/log';
|
|
2
|
+
import { readCalls } from '@metro-labs/core/trains/protocol';
|
|
3
|
+
import { accounts, bootAccount, loadAccounts } from './accounts.js';
|
|
4
|
+
import { handleCall } from './actions.js';
|
|
5
|
+
import { fetchCredits } from './api.js';
|
|
6
|
+
|
|
7
|
+
readCalls('threema', handleCall);
|
|
8
|
+
|
|
9
|
+
for (const cfg of loadAccounts()) accounts.set(cfg.id, bootAccount(cfg));
|
|
10
|
+
if (accounts.size === 0) {
|
|
11
|
+
process.stderr.write('threema: no accounts booted, exiting\n');
|
|
12
|
+
process.exit(2);
|
|
13
|
+
}
|
|
14
|
+
process.stderr.write(
|
|
15
|
+
`threema train ready — ${accounts.size} account(s): ${[...accounts.keys()].join(', ')}\n`,
|
|
16
|
+
);
|
|
17
|
+
|
|
18
|
+
for (const acct of accounts.values())
|
|
19
|
+
fetchCredits(acct.cfg)
|
|
20
|
+
.then((credits) => {
|
|
21
|
+
process.stderr.write(
|
|
22
|
+
`threema[${acct.cfg.id}] ${acct.cfg.gatewayId}: ${credits} credits left\n`,
|
|
23
|
+
);
|
|
24
|
+
})
|
|
25
|
+
.catch((err: unknown) => {
|
|
26
|
+
process.stderr.write(
|
|
27
|
+
`threema[${acct.cfg.id}] credits check failed: ${errMsg(err)}\n`,
|
|
28
|
+
);
|
|
29
|
+
});
|
|
@@ -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
|
+
}
|