Files
lotherk e645e8160d dtchat: add the core API, JSON reader and line framing (T1.1)
include/DtChat/DtChat.h and Ipc.h define the UI-facing core API and the adapter contract. lib/libDtChat builds libDtChatIpc (strict JSON parser/writer, 1 MiB line framing) and the libDtChat core stub; test_json and test_frame cover escapes, UTF-8, depth 32, duplicate keys, overlong lines, CRLF, NUL and the write path. Wired into configure.ac, lib/Makefile.am and the installed headers. Standalone tests pass, ASan/UBSan clean.
2026-11-16 16:35:48 +00:00

109 lines
3.5 KiB
C

/*
* DtChat.h -- dtchat's public core API
*
* License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree.
*
* The UI and the headless tests use only this header. No Xt, no protocol
* library, no audio/video type appears here. The core owns the event queue;
* the caller drains it with dtchat_event_next() and frees each event.
*/
#ifndef _DtChat_DtChat_h
#define _DtChat_DtChat_h
#include <stddef.h>
#ifdef __cplusplus
extern "C" {
#endif
typedef struct DtChat DtChat;
typedef enum {
DTCHAT_EV_STATE, /* an account or a local peer changed state */
DTCHAT_EV_CONV, /* a conversation was added, changed or removed */
DTCHAT_EV_MESSAGE, /* a message, incoming or echoed */
DTCHAT_EV_PRESENCE, /* a buddy or a local peer changed presence */
DTCHAT_EV_TYPING, /* someone started or stopped typing */
DTCHAT_EV_REQUEST, /* the adapter needs an answer: code, password, QR */
DTCHAT_EV_CALL, /* call state changed */
DTCHAT_EV_ERROR /* an account or the local peer failed */
} DtChatEventType;
/* DtChatEvent.flags for DTCHAT_EV_MESSAGE */
#define DTCHAT_MSG_SELF 0x01 /* our own message, echoed */
#define DTCHAT_MSG_ACTION 0x02 /* /me */
#define DTCHAT_MSG_SYSTEM 0x04 /* join, part, topic, ... */
#define DTCHAT_MSG_NOTICE 0x08
/* dtchat_call() media bits */
#define DTCHAT_MEDIA_AUDIO 0x01
#define DTCHAT_MEDIA_VIDEO 0x02
/*
* One event. The strings are owned by the event; dtchat_event_free()
* releases them. "account" is NULL for a local peer, "conv" is the
* conversation id inside that account, "from" is the sender's display
* name, "text" is UTF-8. request_id is only set for DTCHAT_EV_REQUEST;
* data is reserved for the event-specific payload (a call object, later).
*/
typedef struct {
DtChatEventType type;
char *account;
char *conv;
char *from;
char *text;
long long ts; /* seconds since the epoch, 0 if unknown */
int flags;
int request_id;
void *data;
} DtChatEvent;
/*
* Open the core with its state directory. NULL means ~/.dt/dtchat.
* The call does not create the directory; the store does that when the
* first account is saved (T1.3). Returns NULL on out of memory.
*/
DtChat *dtchat_open(const char *config_dir);
/* Stop adapters, close the local listener and free the core. */
void dtchat_close(DtChat *chat);
/*
* A file descriptor that becomes readable when events are queued, for
* XtAppAddInput() or poll(). Returns -1 when the core has no wakeup pipe
* yet (the T1.1 stub).
*/
int dtchat_fd(const DtChat *chat);
/*
* Run the core's event loop once. timeout_ms < 0 waits without a timeout,
* 0 returns immediately. Returns the number of ready descriptors, 0 on a
* timeout, -1 on error.
*/
int dtchat_poll(DtChat *chat, int timeout_ms);
/* Return the next queued event, or NULL when the queue is empty. */
DtChatEvent *dtchat_event_next(DtChat *chat);
/* Free one event returned by dtchat_event_next(). NULL is allowed. */
void dtchat_event_free(DtChatEvent *event);
/* Send UTF-8 text to CONV. Returns 0 when it was queued, -1 on error. */
int dtchat_send(DtChat *chat, const char *conv, const char *text);
/* Join a channel/room; the adapter decides what that means. */
int dtchat_join(DtChat *chat, const char *account, const char *conv);
/* Answer a DTCHAT_EV_REQUEST; VALUE may be NULL for "cancel". */
int dtchat_answer(DtChat *chat, const char *account, int request_id,
const char *value);
/* Start a call to a local peer; MEDIA is DTCHAT_MEDIA_AUDIO/VIDEO. */
int dtchat_call(DtChat *chat, const char *peer, int media);
#ifdef __cplusplus
}
#endif
#endif /* _DtChat_DtChat_h */