Files
mcde/lib/libDtChat/dc_internal.h
T
lotherk f46a914c73 dtchat: add the message catalog and the German UI (T10.4)
i18n.c opens catopen("dtchat", NL_CAT_LOCALE) and dc_text(id, fallback) reads set 1; the program calls dc_i18n_init after setlocale. The visible labels of the main window, the account editor, the request/peer/file/pair dialogs and the local status texts use ui_text.h ids; dtchat.msg (English) has 75 entries and programs/localized/de_DE.UTF-8/msg/dtchat.msg the German copy (ASCII transliteration, core font). test_i18n builds both catalogs with gencat and checks File/Datei, Send/Senden and the fallback; the German run under Xvfb shows Datei/Bearbeiten/Ansicht/Konversation/Hilfe, In der Naehe and Senden (screenshot build/mcde-shots/dtchat-t104-de.png) and the smoke stays green. Dynamic formatted statuses are partly still English. The in-tree build is pending: a foreign release chain holds the lock (pid 141722).
2026-11-17 07:06:55 +00:00

438 lines
18 KiB
C

/*
* dc_internal.h -- private types of libDtChat
*
* License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree.
*
* Nothing here is installed. The public surface is include/DtChat/.
*/
#ifndef _DtChat_dc_internal_h
#define _DtChat_dc_internal_h
#include "DtChat/DtChat.h"
#include "DtChat/Ipc.h"
#include <poll.h>
/*
* The core's event loop: one poll(2) per run, monotonic one-shot timers and
* a wakeup pipe for the UI and for adapter threads. No Xt, no threads
* required. All functions are private to libDtChat.
*/
typedef struct DcLoop DcLoop;
typedef void (*DcLoopFdFn)(int fd, short revents, void *arg);
typedef void (*DcLoopTimerFn)(void *arg);
DcLoop *dc_loop_new(void);
void dc_loop_free(DcLoop *loop);
/* milliseconds from CLOCK_MONOTONIC; never goes backwards */
long long dc_loop_now_ms(void);
/*
* Watch FD for EVENTS (POLLIN/POLLOUT). Returns an id for
* dc_loop_del_fd(), or -1. The id is unique among fds and timers.
*/
int dc_loop_add_fd(DcLoop *loop, int fd, short events, DcLoopFdFn fn,
void *arg);
int dc_loop_del_fd(DcLoop *loop, int id);
/*
* One-shot timer: call FN(ARG) after DELAY_MS. DELAY_MS must be >= 0.
* Returns an id for dc_loop_del_timer(), or -1. Equal deadlines fire in
* the order they were added; a callback may add or delete timers.
*/
int dc_loop_add_timer(DcLoop *loop, long long delay_ms, DcLoopTimerFn fn,
void *arg);
int dc_loop_del_timer(DcLoop *loop, int id);
/* Wake a blocking dc_loop_run() from another thread/signal handler. */
int dc_loop_wake(DcLoop *loop);
/* Read end of the wakeup pipe, for XtAppAddInput() or poll(). */
int dc_loop_wakeup_fd(const DcLoop *loop);
/*
* Poll once and dispatch ready fds and due timers. timeout_ms < 0 waits
* until an fd, the wakeup pipe or the next timer; 0 does not wait.
* Returns the number of dispatched events (a wakeup counts as one), 0 on
* timeout, -1 on a poll() error other than EINTR (which returns 0).
*/
int dc_loop_run(DcLoop *loop, int timeout_ms);
/*
* The store: ~/.dt/dtchat with accounts.json, secrets.json, identity.json.
* The directory is 0700, every file 0600. Writes are atomic (write a
* temporary file, fsync, rename, fsync the directory). A file that is not
* valid JSON is renamed to NAME.bad and reported as missing/corrupt; it is
* never silently parsed or overwritten.
*/
typedef struct DcStore DcStore;
DcStore *dc_store_open(const char *dir); /* NULL: $HOME/.dt/dtchat */
void dc_store_close(DcStore *store);
const char *dc_store_dir(const DcStore *store);
/*
* Load/save one named file. NAME is a base name (no '/', no ".."). On a
* missing file dc_store_load() returns NULL and sets *MISSING to 1; on a
* corrupt file it sets *MISSING to 0, writes a message to ERR and moves the
* file to NAME.bad. ERR may be NULL.
*/
DcJson *dc_store_load(DcStore *store, const char *name, int *missing,
char *err, size_t errlen);
int dc_store_save(DcStore *store, const char *name, const DcJson *json);
/* Typed wrappers with their empty defaults. */
DcJson *dc_store_load_accounts(DcStore *store); /* {"accounts":[]} */
int dc_store_save_accounts(DcStore *store, const DcJson *json);
DcJson *dc_store_load_secrets(DcStore *store); /* {} */
int dc_store_save_secrets(DcStore *store, const DcJson *json);
DcJson *dc_store_load_identity(DcStore *store); /* no default: NULL */
int dc_store_save_identity(DcStore *store, const DcJson *json);
/*
* The chat model: accounts, conversations and a 2000-message ring buffer
* per conversation. Strings are copied; the model owns them. Message
* index 0 is the oldest one still in the ring; dc_conv_message_at()
* returns NULL past the end. Unread counts ignore our own and system
* messages.
*/
typedef struct DcModel DcModel;
typedef struct DcAccount DcAccount;
typedef struct DcConv DcConv;
typedef enum {
DC_CONV_DIRECT,
DC_CONV_CHANNEL,
DC_CONV_GROUP,
DC_CONV_NEARBY
} DcConvKind;
typedef enum {
DC_PRESENCE_OFFLINE,
DC_PRESENCE_ONLINE,
DC_PRESENCE_AWAY,
DC_PRESENCE_DND
} DcPresence;
typedef struct {
char *from;
char *text;
long long ts;
int flags; /* DTCHAT_MSG_* from DtChat.h */
} DcMessage;
DcModel *dc_model_new(void);
void dc_model_free(DcModel *model);
size_t dc_model_account_count(const DcModel *model);
DcAccount *dc_model_account_at(const DcModel *model, size_t index);
DcAccount *dc_model_find_account(const DcModel *model, const char *id);
/* Idempotent: an existing id updates protocol/label and is returned. */
DcAccount *dc_model_add_account(DcModel *model, const char *id,
const char *protocol, const char *label);
int dc_model_remove_account(DcModel *model, const char *id);
const char *dc_account_id(const DcAccount *account);
const char *dc_account_protocol(const DcAccount *account);
const char *dc_account_label(const DcAccount *account);
size_t dc_account_conv_count(const DcAccount *account);
DcConv *dc_account_conv_at(const DcAccount *account, size_t index);
DcConv *dc_model_find_conv(const DcModel *model, const char *account_id,
const char *conv_id);
/* Idempotent: an existing conversation gets the new kind/title. */
DcConv *dc_model_add_conv(DcModel *model, const char *account_id,
const char *conv_id, DcConvKind kind, const char *title);
const char *dc_conv_id(const DcConv *conv);
const char *dc_conv_title(const DcConv *conv);
const char *dc_conv_topic(const DcConv *conv);
DcConvKind dc_conv_kind(const DcConv *conv);
void dc_conv_set_topic(DcConv *conv, const char *topic);
void dc_conv_set_presence(DcConv *conv, DcPresence presence);
DcPresence dc_conv_presence(const DcConv *conv);
int dc_conv_add_message(DcConv *conv, const char *from, const char *text,
long long ts, int flags);
size_t dc_conv_message_count(const DcConv *conv);
const DcMessage *dc_conv_message_at(const DcConv *conv, size_t index);
int dc_conv_unread(const DcConv *conv);
void dc_conv_mark_read(DcConv *conv);
/*
* The local identity: one Ed25519 key, a self-signed X.509 certificate and
* the SHA-256 pin of the certificate's DER form. identity.json in DIR is
* 0600 (DIR 0700) and written atomically through the store. A corrupt file
* is moved aside and load_or_create() fails instead of silently replacing it;
* a missing one is generated and saved. The strings belong to the identity.
*/
typedef struct DcIdentity DcIdentity;
DcIdentity *dc_identity_load_or_create(const char *dir);
int dc_identity_save(const DcIdentity *id, const char *dir);
void dc_identity_free(DcIdentity *id);
const char *dc_identity_pin(const DcIdentity *id);
const char *dc_identity_public_b64(const DcIdentity *id);
const char *dc_identity_cert_pem(const DcIdentity *id);
const char *dc_identity_key_pem(const DcIdentity *id);
/*
* Local peers. One peer per identity id; dc_peers_announce() takes the
* service's TXT record ("v=1;id=...;name=...;pin=...;port=...", keys joined
* with ';'), merges it by id and marks the peer online. A service that goes
* away is dc_peers_offline(); a manual peer is not touched by that. The
* block flag belongs to the peer and survives announcements. Strings belong
* to the peer.
*/
typedef struct DcPeer DcPeer;
typedef struct DcPeers DcPeers;
DcPeers *dc_peers_new(void);
void dc_peers_free(DcPeers *peers);
size_t dc_peers_count(const DcPeers *peers);
DcPeer *dc_peers_at(const DcPeers *peers, size_t index);
DcPeer *dc_peers_find(const DcPeers *peers, const char *id);
int dc_peers_announce(DcPeers *peers, const char *txt, const char *host,
int port);
int dc_peers_offline(DcPeers *peers, const char *id);
DcPeer *dc_peers_add_manual(DcPeers *peers, const char *id, const char *name,
const char *host, int port, const char *pin);
void dc_peer_set_blocked(DcPeer *peer, int blocked);
const char *dc_peer_id(const DcPeer *peer);
const char *dc_peer_name(const DcPeer *peer);
const char *dc_peer_host(const DcPeer *peer);
const char *dc_peer_pin(const DcPeer *peer);
int dc_peer_port(const DcPeer *peer);
int dc_peer_online(const DcPeer *peer);
int dc_peer_blocked(const DcPeer *peer);
int dc_peer_manual(const DcPeer *peer);
/*
* The local peer listener. dc_local_server_new() binds HOST (NULL or
* "0.0.0.0" = all interfaces) and PORT (0 = ephemeral); the identity is the
* TLS server certificate. A peer sends {"type":"hello","id":...,"pin":...};
* ID and PIN are checked against EXPECT_ID/EXPECT_PIN (either may be NULL)
* and a hello-ack answers ok true/false. A wrong PIN closes the connection.
* FN is called once with the accepted hello (owned by the connection and
* valid only during the call). Everything runs from DcLoop.
*/
typedef struct DcLocalServer DcLocalServer;
typedef struct DcLocalConn DcLocalConn;
typedef struct DcLocalPeer DcLocalPeer;
typedef void (*DcLocalHelloFn)(DcLocalConn *conn, const DcJson *hello,
void *arg);
typedef void (*DcLocalLineFn)(DcLocalConn *conn, const DcJson *json,
void *arg);
typedef void (*DcLocalReadyFn)(DcLocalConn *conn, int ok, void *arg);
typedef void (*DcLocalCloseFn)(DcLocalConn *conn, void *arg);
typedef void (*DcLocalPeerLineFn)(DcLocalPeer *peer, const DcJson *json,
void *arg);
typedef void (*DcLocalPeerStateFn)(DcLocalPeer *peer, int online, void *arg);
DcLocalServer *dc_local_server_new(DcLoop *loop, const DcIdentity *identity,
const char *host, int port, const char *expect_id, const char *expect_pin,
DcLocalHelloFn hello, DcLocalLineFn line, DcLocalCloseFn closed,
void *arg);
int dc_local_server_port(const DcLocalServer *server);
void dc_local_server_free(DcLocalServer *server);
DcLocalConn *dc_local_connect(DcLoop *loop, const DcIdentity *identity,
const char *host, int port, const char *expect_pin,
DcLocalLineFn line, DcLocalReadyFn ready, DcLocalCloseFn closed,
void *arg);
int dc_local_conn_send_json(DcLocalConn *conn, const DcJson *json);
int dc_local_conn_busy(const DcLocalConn *conn);
int dc_local_send_presence(DcLocalConn *conn, const char *show,
const char *status);
int dc_local_send_msg(DcLocalConn *conn, const char *text,
const char *client_id);
int dc_local_send_msg_ack(DcLocalConn *conn, const char *client_id);
int dc_local_send_typing(DcLocalConn *conn, int on);
void dc_local_conn_close(DcLocalConn *conn);
const char *dc_local_conn_peer_id(const DcLocalConn *conn);
const char *dc_local_conn_peer_pin(const DcLocalConn *conn);
/*
* A client link with reconnect: 2, 4, 8 ... 60 s backoff, an online/offline
* callback and the line callback for the ready connection. Send fails while
* offline; the peer does not queue messages.
*/
DcLocalPeer *dc_local_peer_new(DcLoop *loop, const DcIdentity *identity,
const char *host, int port, const char *expect_pin,
DcLocalPeerLineFn line, DcLocalPeerStateFn state_fn, void *arg);
int dc_local_peer_start(DcLocalPeer *peer);
void dc_local_peer_stop(DcLocalPeer *peer);
void dc_local_peer_free(DcLocalPeer *peer);
int dc_local_peer_send(DcLocalPeer *peer, const DcJson *json);
int dc_local_peer_online(const DcLocalPeer *peer);
DcLocalConn *dc_local_peer_conn(const DcLocalPeer *peer);
/*
* Local file transfer. A sender offers name/size/sha256, sends 64 KiB
* chunks as base64 file-data lines and a file-end; the receiver writes
* NAME.part, enforces 512 MiB, verifies the hash and renames to a sanitized
* name (no overwrite). dc_file_send_step() sends one chunk; 0 means call
* again (the connection may be busy), 1 means the file-end is out.
*/
#define DC_FILE_MAX (512LL * 1024 * 1024)
#define DC_FILE_CHUNK (64 * 1024)
typedef struct DcFileSend DcFileSend;
typedef struct DcFileRecv DcFileRecv;
char *dc_file_safe_name(const char *name);
int dc_file_sha256(const char *path, char out[65], long long *size);
DcFileSend *dc_file_send_new(DcLocalConn *conn, const char *id,
const char *path, char *err, size_t errlen);
void dc_file_send_start(DcFileSend *send);
int dc_file_send_step(DcFileSend *send);
void dc_file_send_cancel(DcFileSend *send, const char *reason);
void dc_file_send_free(DcFileSend *send);
DcFileRecv *dc_file_recv_new(const char *dir, const DcJson *offer, char *err,
size_t errlen);
int dc_file_recv_data(DcFileRecv *recv, const char *b64, char *err,
size_t errlen);
int dc_file_recv_end(DcFileRecv *recv, char *err, size_t errlen);
void dc_file_recv_cancel(DcFileRecv *recv);
const char *dc_file_recv_path(const DcFileRecv *recv);
long long dc_file_recv_got(const DcFileRecv *recv);
/*
* Pairing and TOFU. dc_pair_sas() derives the 6-digit code from both
* certificate pins (order independent). peers.json keeps id -> pin and the
* blocked flag (0600 through the store); a changed pin answers -1 in
* dc_pins_check() and stays blocked until dc_pins_confirm() stores the new
* pin after the person compared a new SAS.
*/
char *dc_pair_sas(const char *pin_a, const char *pin_b);
typedef struct DcPins DcPins;
DcPins *dc_pins_load(const char *dir);
int dc_pins_save(DcPins *pins, const char *dir);
void dc_pins_free(DcPins *pins);
int dc_pins_check(const DcPins *pins, const char *id, const char *pin);
int dc_pins_confirm(DcPins *pins, const char *id, const char *pin);
int dc_pins_set_blocked(DcPins *pins, const char *id, int blocked);
int dc_pins_is_blocked(const DcPins *pins, const char *id);
size_t dc_pins_count(const DcPins *pins);
const char *dc_pins_id_at(const DcPins *pins, size_t index);
const char *dc_pins_pin_at(const DcPins *pins, size_t index);
/*
* Message catalog "dtchat" (set 1). The program calls setlocale() and
* dc_i18n_init() once; dc_text() returns the translation for ID or FALLBACK
* when no catalog is available. The catalog is built from dtchat.msg
* (C and each locale under programs/localized/<locale>/msg).
*/
void dc_i18n_init(void);
void dc_i18n_close(void);
const char *dc_text(int id, const char *fallback);
/*
* IRC lines (RFC 1459/2812 + IRCv3 tags). dc_irc_parse() copies the tags,
* the prefix, the command, up to 15 parameters and the trailing text;
* dc_irc_message_free() releases them. LEN must not include CR/LF. A NUL
* inside LEN, a missing command, more than 15 parameters or a line over
* DC_IRC_MAX_LINE bytes is an error.
*/
#define DC_IRC_MAX_LINE (1024 * 1024)
typedef struct {
char *tags; /* without '@', or NULL */
char *prefix; /* without ':', or NULL */
char *command; /* never NULL after a successful parse */
char **params;
size_t nparams;
char *trailing; /* without ':', or NULL */
} DcIrcMessage;
int dc_irc_parse(const char *line, size_t len, DcIrcMessage *msg, char *err,
size_t errlen);
void dc_irc_message_free(DcIrcMessage *msg);
/*
* Split TEXT at MAX_BYTES without cutting a UTF-8 sequence. *OFFSET starts
* at 0 and is advanced past each chunk; OUT receives the chunk (NUL
* terminated, at most OUTLEN-1 bytes). Spaces and newlines at the break are
* consumed, so joining the chunks reproduces the words. Returns the chunk
* length, or 0 when TEXT is exhausted.
*/
size_t dc_irc_split_next(const char *text, size_t *offset, size_t max_bytes,
char *out, size_t outlen);
/*
* Text helpers for the UI and the adapters. All return malloc'd strings
* (free with free(3)) or NULL. dc_text_clean() removes C0 controls except
* newline/tab and normalises CR/CRLF; dc_text_irc_strip() removes the mIRC
* formatting codes but keeps UTF-8; dc_text_to_display() converts UTF-8 to
* CHARSET with iconv, falls back to ASCII//TRANSLIT and then to '?'.
*/
int dc_text_valid_utf8(const char *s);
char *dc_text_clean(const char *s);
char *dc_text_irc_strip(const char *s);
char *dc_text_to_display(const char *s, const char *charset);
/*
* Adapter processes. dc_adapter_start() forks/execs PATH with "--stdio",
* waits for its "hello" (ipc 1), sends "configure" and "connect", and then
* routes every JSON object from the adapter to FN. The event is owned by
* the adapter and valid only during the callback.
*
* If the child dies without saying "bye", the manager restarts it after
* 2, 4, 8 ... 60 seconds. dc_adapter_stop() sends "disconnect" and starts
* the 2 s shutdown; dc_adapter_free() may be called after the state is
* STOPPED (it also kills a still-running child). Settings and secrets are
* deep-copied; the caller keeps its objects.
*/
typedef struct DcAdapter DcAdapter;
typedef enum {
DC_ADAPTER_STARTING,
DC_ADAPTER_CONFIGURED,
DC_ADAPTER_ONLINE,
DC_ADAPTER_BACKOFF,
DC_ADAPTER_STOPPED
} DcAdapterState;
typedef void (*DcAdapterEventFn)(DcAdapter *adapter, const DcJson *event,
void *arg);
DcAdapter *dc_adapter_start(DcLoop *loop, const char *path,
const char *account, const DcJson *settings, const DcJson *secrets,
DcAdapterEventFn fn, void *arg);
void dc_adapter_stop(DcAdapter *adapter);
void dc_adapter_free(DcAdapter *adapter);
DcAdapterState dc_adapter_state(const DcAdapter *adapter);
int dc_adapter_restarts(const DcAdapter *adapter);
int dc_adapter_send_json(DcAdapter *adapter, const DcJson *json);
int dc_adapter_send_text(DcAdapter *adapter, const char *conv,
const char *text, const char *client_id);
int dc_adapter_join(DcAdapter *adapter, const char *conv);
int dc_adapter_answer(DcAdapter *adapter, int request_id, const char *value);
/*
* One dtchat per user: DIR/run/dtchat.lock is flock()ed and
* DIR/run/dtchat.sock is the control socket. The first process is the
* primary and gets OPEN_CB for every "open" request; a second process
* gets an instance with dc_instance_is_primary()==0, calls
* dc_instance_notify() and exits. The lock is released and the socket
* removed by dc_instance_close().
*/
typedef struct DcInstance DcInstance;
typedef void (*DcInstanceOpenFn)(const char *conv, void *arg);
DcInstance *dc_instance_acquire(DcLoop *loop, const char *dir,
DcInstanceOpenFn open_cb, void *arg);
int dc_instance_is_primary(const DcInstance *instance);
const char *dc_instance_socket(const DcInstance *instance);
void dc_instance_close(DcInstance *instance);
/* Connect to a running primary and send {"cmd":"open","conv":...}. */
int dc_instance_notify(const char *dir, const char *conv);
#endif /* _DtChat_dc_internal_h */