# dtchat -- architecture decision

Decided 2026-11-16 by Konrad.  This record belongs to `ext/src/mcde`; the
work is tracked in tasksd scope `mcde/dtchat` (T0.1 = `t0320`).

## What dtchat must do

A CDE/Motif chat program that speaks IRC, XMPP, Telegram and WhatsApp,
finds other dtchat instances on the local network by itself, and exchanges
text, files, speech and video with them -- locally and, where the protocol
allows it, through the protocol.

## Decision

dtchat is built from

- a Motif UI (`programs/dtchat`),
- a core library without Xt (`lib/libDtChat`: event loop, storage, chat
  model, local peer protocol, media),
- one adapter process per network account (`programs/dtchat-irc`,
  `programs/dtchat-xmpp`, optional `dtchat-telegram` and `dtchat-whatsapp`).

The UI and the core link no protocol library.  Adapters speak a JSON-lines
protocol on stdin/stdout (`include/DtChat/Ipc.h`,
`docs/reference/dtchat-adapter-protocol.txt`).  Local discovery, TLS, file
transfer, calls and media live in the core, because they share the event
loop and the audio/video state with the UI.

## Why not libpurple

libpurple is the fastest route to IRC and XMPP, but it is not the base of
this program:

- it pulls GLib, libxml2 and libidn into the process
  (`pkgsrc/chat/libpurple/Makefile`);
- its Telegram plug-in in this tree is `telegram-purple-1.3.1` from 2018
  (`pkgsrc/chat/telegram-purple/Makefile`);
- this pkgsrc tree has no WhatsApp plug-in at all;
- it has no local peer protocol, no speech and no video (Pidgin's old
  Farsight path is gone);
- libpurple 2.x is in maintenance, and Pidgin 3 is not a schedule.

A `dtchat-purple` adapter (libpurple in its own process, GLib only there)
remains the fallback if the native XMPP adapter overruns.  It would not
change the architecture, only one adapter.

## Why not Matrix as the base

Matrix is a server protocol.  IRC, Telegram and WhatsApp would only arrive
through bridges, which need a homeserver and a bridge deployment.  A C
client would still need E2EE (vodozemac, Rust) and VoIP (WebRTC); this
pkgsrc tree has `chat/matrix-synapse` and `chat/py-matrix-nio`, no
maintained C client SDK.  A text-only `dtchat-matrix` adapter via a helper
is possible later; it is not the base.

## The exception to the MCDE philosophy

`ext/src/mcde/AGENTS.md` (philosophy point 2) allows "nothing beyond POSIX
and the X/Motif libraries".  TLS, XMPP, speech and video cannot be written
that way.  Decided exception, recorded in `AGENTS.md` the same day:

| Library | Source | Used for | Required |
|---|---|---|---|
| libtls | `security/libretls` (both systems) | TLS for IRC, XMPP, local peers | yes |
| libcrypto | base OpenSSL (`src/crypto/external/bsd/openssl`) / with libretls | X25519, Ed25519, HKDF, ChaCha20-Poly1305 | yes |
| libstrophe | `chat/libstrophe` 0.12.3 | XMPP | yes for XMPP |
| libopus | `audio/libopus` | speech | yes for speech |
| libvpx | `multimedia/libvpx` | video | optional at build time |

Each library sits behind exactly one module (`dc_tls`, `dc_xmpp`,
`dc_audio`, `dc_video`).  No GUI toolkit and no scripting runtime is
added; the codec libraries are optional at build time.  The Telegram
adapter may use TDLib (C++) and the WhatsApp adapter whatsmeow (Go), but
only in their own packages and processes, never linked into the UI or the
core.

Note: the NetBSD base carries OpenSSL, not libtls.  Measured 2026-11-16:
`src/crypto/external/bsd/openssl/` exists, `src/lib/libtls` does not.
libtls therefore comes from pkgsrc on both systems.

## Process model

- One `dtchat` process per user session.  `flock` on
  `~/.dt/dtchat/run/dtchat.lock`; a second start talks to the first over
  `~/.dt/dtchat/run/dtchat.sock` and exits.
- One adapter process per account, spawned only from
  `${libexecdir}/dtchat` (plus `--adapter-path` in tests), restarted with
  backoff unless it said `bye`.
- Local peers: DNS-SD `_dtchat._tcp`, TLS with a self-signed Ed25519
  certificate and JSON control channel, files over the same TLS connection.
  The first contact is an explicit pairing, not silent TOFU: both sides
  show a 6-digit SAS derived from the two certificate hashes and a nonce,
  the people compare it out of band, and only then is the pin written to
  `peers.json`.  Until pairing, a peer may send a text request only -- no
  files, no calls.  A changed pin blocks the peer until it is paired again
  (task `t0360`, Phase 5).  This keeps the TOFU convenience without
  silently trusting the first key on the LAN.
- Calls: X25519 ephemeral per call, signed with the identity key; media
  over UDP with ChaCha20-Poly1305, Opus 48 kHz mono, VP8 640x360.  LAN
  only in v1: no ICE/STUN/TURN, no relay.

## Spike results and decisions (2026-11-16)

The Phase 0 measurements are in `docs/reference/dtchat-spikes.txt`; the
short form and what it changes:

| Spike | Result | Decision |
|---|---|---|
| T0.3 libtls (Linux) | non-blocking handshake with `poll`; TLS 1.3; system CA verifies; self-signed rejected; TOFU pin matches `openssl x509 -outform der \| sha256sum` | keep libtls; NetBSD run open |
| T0.4 DNS-SD (Linux) | `_dtchat._tcp` registers and resolves on IPv4/IPv6, TXT survives, goodbye works; pkgsrc avahi has no `libdns_sd` compat | Linux `avahi-client`, NetBSD `libdns_sd`, one McdeSys API; NetBSD run open |
| T0.5 libstrophe | TLS 1.3 + SASL PLAIN + bind + message through `xmpp_run_once`; no MUC module; GCC 16 needs `-Wno-error=incompatible-pointer-types` | XMPP adapter is viable; MUC is ours; add the flag or a one-line patch |
| T0.6 audio (Linux) | 48 kHz S16_LE mono, 960-frame (20 ms) periods, capture+playback open together | PTT stays the v1 default; ring buffer/prefill is required; NetBSD run open |
| T0.7 TDLib (Linux) | 1.8.70 builds; `tdjson` starts without an account and answers `getOption("version")`; 50 MB library, 537 MB build tree | Telegram is Linux-first; NetBSD build is a release gate; no media stack, so no calls |
| T0.8 whatsmeow | builds; QR event (277 chars) and a 160 KB session DB | WhatsApp text is viable; scan/message needs a spare account; no calls |

Consequences for the order:

- Phase 1 (core) and Phase 3/4 (IRC/XMPP) proceed on Linux.  The NetBSD
  runs of T0.3, T0.4 and T0.6 are a **release gate**, not a blocker for
  the code: before the first release, repeat them on a NetBSD host and
  fix the backends there.
- The D-Bus/avahi-client backend on Linux and the libdns_sd backend on
  NetBSD are separate implementations behind the same McdeSys calls; the
  plan's hope for one underlying library was wrong.
- WhatsApp remains text-only and unofficial; the live test (scan, send,
  receive) needs a spare account and is paused.
- Video stays optional (`--disable-dtchat-video`) and is gated on a
  capture test on both systems; no promise beyond LAN.
- Telegram: TDLib has signalling, not media.  The Linux build (1.8.70)
  works, so the adapter is Phase 8 as a separate package; the NetBSD build
  is still open and is a release gate.  Either way, no Telegram call button
  in v1.

## What the product promises

| Transport | Text | Files | Speech | Video | Status |
|---|---|---|---|---|---|
| IRC | yes | no (no DCC) | no | no | v1 |
| XMPP | yes | yes (IBB) | Jingle later | Jingle later | v1 text |
| Telegram | yes | yes | **no** | **no** | optional package |
| WhatsApp | yes | yes | **no** | **no** | optional, unofficial |
| dtchat local | yes | yes | yes | yes | v1/v2 |

TDLib exposes Telegram call signalling (`createCall`, `acceptCall`,
`sendCallSignalingData`, `discardCall` in `td_api.tl`), but not the media
stack: `callStateReady` hands over servers, config and the encryption key,
and the RTP/WebRTC side is the separate `tgcalls` library.  dtchat v1 does
not implement it, so there is no Telegram call button.  whatsmeow lists
"Calls" as not implemented, so the same holds for WhatsApp.  The UI only
shows call buttons for transports whose `caps` include `audio` or `video`.
(Corrected 2026-11-16 after reading `td_api.tl`; the first version of this
record said TDLib had no call API, which is wrong.)

## Consequences

- More than one process, and a documented IPC contract instead of direct
  function calls.  In exchange: crash isolation, optional heavy
  dependencies, and testable adapters.
- The core must be usable without X (`--headless`), which is also the test
  path for two peers on one machine.
- Telegram and WhatsApp are separate packages (`chat/mcde-telegram`,
  `chat/mcde-whatsapp`); the main MCDE package stays lean and
  cross-buildable.
- No OMEMO, no Matrix E2EE, no Telegram/WhatsApp calls, no NAT traversal
  in v1.  These are stated in the manual and in the UI, not hidden.
- The MCDE memory target (256 MB) applies to UI and core, not to the
  optional TDLib/Go helpers; their cost is documented per adapter.

## Open points (tracked as tasks, status 2026-11-16)

- T0.3: libtls -- Linux done, **NetBSD run open** (no reachable host).
- T0.4: DNS-SD -- Linux done, decision: `avahi-client` on Linux,
  `libdns_sd` on NetBSD; **NetBSD run open**.
- T0.5: libstrophe -- done (TLS/SASL/message against a fake server);
  GCC 16 flag/patch and no MUC module recorded.
- T0.6: audio -- Linux done (48 kHz/20 ms duplex), **NetBSD `audio(4)`
  open**; push-to-talk is the v1 default.
- T0.7: TDLib -- Linux build and `tdjson` start verified (1.8.70);
  **NetBSD build open**; the Telegram `api_id`/`api_hash` remain the
  user's (Telegram does not allow shipping a shared one).
- T0.8: whatsmeow -- build and QR event done; scan/message needs a spare
  account and is open; no calls.
- T0.9: this review; Telegram and video remain conditional on T0.7 and
  T7.1.

## Sources measured in this tree (2026-11-16)

- `ext/src/mcde/AGENTS.md` -- philosophy, point 2.
- `ext/src/mcde/lib/McdeSys/Makefile.am` -- `-ldns_sd` on NetBSD.
- `ext/src/mcde/lib/McdeSys/netbsd.c:1837` -- DNS-SD through mdnsd.
- `ext/src/mcde/lib/McdeSys/linux.c:1308` -- DNS-SD through avahi-browse.
- `pkgsrc/chat/libstrophe/Makefile` -- `libstrophe-0.12.3`.
- `pkgsrc/chat/libpurple/Makefile` -- GLib, libxml2, libidn.
- `pkgsrc/chat/telegram-purple/Makefile` -- `telegram-purple-1.3.1`.
- `pkgsrc/security/libretls/Makefile` -- libtls for OpenSSL.
- `pkgsrc/audio/libopus/Makefile`, `pkgsrc/multimedia/libvpx/Makefile`.
- `src/crypto/external/bsd/openssl/` -- base OpenSSL; no `src/lib/libtls`.

The detailed task list is the dtchat plan of 2026-11-16
(`.hermes/plans/2026-11-16_152710-dtchat.md` in the workspace) and the
tasks in scope `mcde/dtchat`.

<!-- License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree. -->
