Files
mcde/programs/dtchat
lotherk 2f3511ebf2 dtchat: run the configured accounts as adapters in the UI
accounts_run.c loads accounts.json (id, protocol, label, adapter, settings, enabled) and secrets.json (per account id) and starts one DcAdapter per enabled account, pumping the DcLoop from an Xt timeout. Its events go into the model and the panel: state into the status line, conversation (direct/channel/group) and roster entries into the model (roster sets presence), messages into the transcript via dtchat_ui_message_added, presence/typing/errors into the status/dialog, request events into the existing request dialog with the answer routed to that adapter. The UI send hook resolves the selected account (dtchat_ui_current_account) or the account that owns the conversation and calls dc_adapter_send_text; dtchat_accounts_join/_typing expose join and XEP-0085 for menus and tests. Normal GUI runs (not --demo/--local/--perf/--smoke) start with an empty model and the configured accounts; the demo peers are gone in that mode. dtchat-irc is installed next to dtchat and listed in the PLIST; dtchat-xmpp and the demo adapter remain build artifacts. Verified with the fake adapter under Xvfb (accounts fake: starting/online/sent alice/message alice), including the real UI send path (DTCHAT_TEST_UI_SEND), screenshot build/mcde-shots/dtchat-accounts.png; --smoke-test still passes.
2026-11-17 10:22:27 +00:00
..

dtchat adapters -- a guide for authors
======================================

An adapter is a separate process that speaks one network protocol for dtchat.
It reads JSON lines on stdin, writes JSON lines on stdout and may write
anything to stderr (dtchat drains and discards it).  The core and the UI never
link a protocol library; an adapter can be written in C, C++, Go, Python or
anything that can read and write lines.

The normative contract is docs/reference/dtchat-adapter-protocol.txt (v1,
ipc=1).  This file is the short version and a skeleton.

1. Start and handshake
----------------------

dtchat starts the process as

    PATH --stdio

with three pipes.  Within 10 seconds the adapter must send one line:

    {"type":"hello","ipc":1,"protocol":"irc","version":"1.0",
     "caps":["text","channels","typing","sasl","tls"],
     "settings":[ ... ]}

`ipc` must be 1 and `protocol` is the protocol name.  `settings` describes the
account form; each entry has `key`, `label`, `type` (`text`, `secret`, `bool`,
`int`, `choice`, `info`, `button`), optional `default`, `required`, `help`,
`choices` and `action`.  `secret` values are stored in secrets.json, never in
the account file, and the UI shows only `*`.

Then dtchat sends:

    {"type":"configure","account":"name","settings":{...},"secrets":{...}}
    {"type":"connect"}

Answer with a state and then events:

    {"type":"state","state":"connecting"}
    {"type":"state","state":"online","detail":"server"}

`online` resets the restart backoff.

2. Commands (core -> adapter)
-----------------------------

    {"type":"send","conv":"#cde","text":"hello","client_id":"c1"}
    {"type":"join","conv":"#cde"}
    {"type":"answer","id":7,"value":"12345"}
    {"type":"disconnect"}
    {"type":"set_presence","show":"online","status":"..."}   (optional)
    {"type":"typing","conv":"#cde","on":true}                (optional)

`answer` replies to a request; `value` is a string or null for cancel.
`disconnect` means: say goodbye and exit.

3. Events (adapter -> core)
---------------------------

    {"type":"conversation","conv":"#cde","kind":"channel","title":"#cde"}
    {"type":"message","conv":"#cde","id":"m1","ts":...,
     "from":"alice","self":false,"kind":"text","text":"hi"}
    {"type":"sent","conv":"#cde","client_id":"c1","ts":...}
    {"type":"presence","buddy":"alice","show":"online","status":"brb"}
    {"type":"roster","conv":"#cde","contact":{"id":"alice","name":"Alice",
     "mode":"@"}}
    {"type":"typing","conv":"#cde","who":"alice","on":true}
    {"type":"log","level":"warn","text":"..."}
    {"type":"error","code":"auth","text":"Password rejected","fatal":false}
    {"type":"bye","reason":"requested"}

`kind` in a message is `text`, `action`, `notice` or `system`.  `fatal:true`
and `bye` mean: do not restart.  Any other exit is a crash; dtchat restarts
after 2, 4, 8 ... 60 seconds (reset by `online`).

4. Asking the person
--------------------

    {"type":"request","id":7,"kind":"code","title":"Telegram",
     "prompt":"Code from the app","secret":true}
    {"type":"request","id":8,"kind":"qr","title":"WhatsApp","hint":"...",
     "qr":{"width":33,"height":33,"data":"<base64, 1 byte per module>"}}

`kind` is `code`, `password`, `notice`, `qr` or `confirm`.  The answer comes
back as `{"type":"answer","id":7,"value":"..."}` (null = cancel).

5. Security
-----------

- Secrets arrive only in `configure`; never write them to stdout, stderr or
  a file of your own.
- Never run a shell from the adapter for a user-controlled string.
- dtchat starts adapters only from its adapter directory (plus an explicit
  path in tests); the adapter is a separate process and may crash without
  taking the UI down.
- A line over 1 MiB, a NUL or invalid JSON ends the process; keep stdout to
  one object per line.

6. Testing
----------

- lib/libDtChat/fake_adapter.c is a minimal adapter (`normal`, `crash`,
  `garbage` modes).
- lib/libDtChat/test_adapter.c starts it with dc_adapter_start() and checks
  the handshake, send/message, join/answer, stop/bye and the restart backoff.
- `tools/mcde-tests.sh` runs the tests; `make mcde-host` builds them.
- For a new adapter, add a `--selftest` that feeds hello/configure/connect
  through pipes and checks the answers without a network, then a fake server
  test for the protocol itself.

7. Skeleton (Python, for a first probe)
---------------------------------------

    #!/usr/bin/env python3
    import json, sys

    def send(obj):
        sys.stdout.write(json.dumps(obj) + "\n")
        sys.stdout.flush()

    send({"type":"hello","ipc":1,"protocol":"demo","version":"0.1",
          "caps":["text"],
          "settings":[{"key":"nick","label":"Nickname","type":"text",
                       "default":"you"}]})
    nick = "you"
    for line in sys.stdin:
        cmd = json.loads(line)
        t = cmd.get("type")
        if t == "configure":
            nick = (cmd.get("settings") or {}).get("nick", nick)
        elif t == "connect":
            send({"type":"state","state":"online"})
            send({"type":"conversation","conv":"lobby","kind":"channel",
                  "title":"Lobby"})
        elif t == "send":
            send({"type":"sent","conv":cmd["conv"],
                  "client_id":cmd.get("client_id")})
            send({"type":"message","conv":cmd["conv"],"from":"echo",
                  "kind":"text","text":cmd["text"]})
        elif t == "disconnect":
            send({"type":"bye","reason":"requested"})
            sys.exit(0)

Save it, `chmod +x`, and start it with a test adapter path; the UI and the
headless mode will show the lobby and echo your messages.

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