Files
mcde/lib/libDtChat/conv.c
T
lotherk b330b3b60a dtchat: add the generic account editor (T2.4)
ui_accounts.c builds the account list and a settings form from the adapter schema: text, secret, int, bool, choice, info and button. Secrets are collected into a separate JSON object and never shown; a modifyVerify callback keeps the masked text while the field stores the real value (this Motif has no XmNechoChar). Add/Delete create or remove accounts through dc_model_remove_account, Save validates required fields and numbers and updates the account label. Wired to File/Accounts... with a demo IRC schema. Standalone build clean; dialog opens on Xvfb, password masked, Save shows 'saved (demo)'; test_conv and ASan/UBSan green. make mcde-host could not run: another agent's release build holds build/.build.lock (pid 175328).
2026-11-16 18:42:58 +00:00

467 lines
8.9 KiB
C

/*
* conv.c -- dtchat's accounts, conversations and message ring buffers
*
* License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree.
*
* The UI reads this model; the adapters feed it. Every conversation keeps
* the last 2000 messages in a ring buffer; index 0 is always the oldest
* message still in the ring. Strings are copied into the model.
*/
#include "dc_internal.h"
#include <stdlib.h>
#include <string.h>
#define DC_CONV_MAX_MESSAGES 2000
#define DC_CONV_INITIAL_MESSAGES 64
struct DcConv {
char *id;
char *title;
char *topic;
DcConvKind kind;
DcPresence presence;
int unread;
DcMessage *messages;
size_t cap;
size_t count;
size_t head;
};
struct DcAccount {
char *id;
char *protocol;
char *label;
DcConv **convs;
size_t nconvs;
size_t capconvs;
};
struct DcModel {
DcAccount **accounts;
size_t naccounts;
size_t capaccounts;
};
static char *
dup_string(const char *s)
{
return strdup(s != NULL ? s : "");
}
static int
ensure_ptrs(void ***array, size_t *cap, size_t need)
{
void **next;
size_t ncap;
if (need <= *cap)
return 0;
ncap = *cap != 0 ? *cap * 2 : 8;
while (ncap < need)
ncap *= 2;
next = realloc(*array, ncap * sizeof(*next));
if (next == NULL)
return -1;
*array = next;
*cap = ncap;
return 0;
}
DcModel *
dc_model_new(void)
{
return calloc(1, sizeof(DcModel));
}
static void
conv_free(DcConv *conv)
{
size_t i;
if (conv == NULL)
return;
for (i = 0; i < conv->count; i++) {
DcMessage *msg = &conv->messages[(conv->head + i) % conv->cap];
free(msg->from);
free(msg->text);
}
free(conv->messages);
free(conv->id);
free(conv->title);
free(conv->topic);
free(conv);
}
static void
account_free(DcAccount *account)
{
size_t i;
if (account == NULL)
return;
for (i = 0; i < account->nconvs; i++)
conv_free(account->convs[i]);
free(account->convs);
free(account->id);
free(account->protocol);
free(account->label);
free(account);
}
void
dc_model_free(DcModel *model)
{
size_t i;
if (model == NULL)
return;
for (i = 0; i < model->naccounts; i++)
account_free(model->accounts[i]);
free(model->accounts);
free(model);
}
size_t
dc_model_account_count(const DcModel *model)
{
return model ? model->naccounts : 0;
}
DcAccount *
dc_model_account_at(const DcModel *model, size_t index)
{
if (model == NULL || index >= model->naccounts)
return NULL;
return model->accounts[index];
}
DcAccount *
dc_model_find_account(const DcModel *model, const char *id)
{
size_t i;
if (model == NULL || id == NULL)
return NULL;
for (i = 0; i < model->naccounts; i++)
if (strcmp(model->accounts[i]->id, id) == 0)
return model->accounts[i];
return NULL;
}
DcAccount *
dc_model_add_account(DcModel *model, const char *id, const char *protocol,
const char *label)
{
DcAccount *account;
char *p, *l;
if (model == NULL || id == NULL || *id == '\0')
return NULL;
account = dc_model_find_account(model, id);
if (account != NULL) {
p = dup_string(protocol);
l = dup_string(label);
if (p == NULL || l == NULL) {
free(p);
free(l);
return NULL;
}
free(account->protocol);
free(account->label);
account->protocol = p;
account->label = l;
return account;
}
account = calloc(1, sizeof(*account));
if (account == NULL)
return NULL;
account->id = strdup(id);
account->protocol = dup_string(protocol);
account->label = dup_string(label);
if (account->id == NULL || account->protocol == NULL ||
account->label == NULL) {
account_free(account);
return NULL;
}
if (ensure_ptrs((void ***)&model->accounts, &model->capaccounts,
model->naccounts + 1) != 0) {
account_free(account);
return NULL;
}
model->accounts[model->naccounts++] = account;
return account;
}
int
dc_model_remove_account(DcModel *model, const char *id)
{
size_t i;
if (model == NULL || id == NULL)
return -1;
for (i = 0; i < model->naccounts; i++) {
if (strcmp(model->accounts[i]->id, id) == 0) {
account_free(model->accounts[i]);
memmove(&model->accounts[i], &model->accounts[i + 1],
(model->naccounts - i - 1) *
sizeof(*model->accounts));
model->naccounts--;
return 0;
}
}
return -1;
}
const char *
dc_account_id(const DcAccount *account)
{
return account ? account->id : NULL;
}
const char *
dc_account_protocol(const DcAccount *account)
{
return account ? account->protocol : NULL;
}
const char *
dc_account_label(const DcAccount *account)
{
return account ? account->label : NULL;
}
size_t
dc_account_conv_count(const DcAccount *account)
{
return account ? account->nconvs : 0;
}
DcConv *
dc_account_conv_at(const DcAccount *account, size_t index)
{
if (account == NULL || index >= account->nconvs)
return NULL;
return account->convs[index];
}
DcConv *
dc_model_find_conv(const DcModel *model, const char *account_id,
const char *conv_id)
{
DcAccount *account;
size_t i;
if (conv_id == NULL)
return NULL;
account = dc_model_find_account(model, account_id);
if (account == NULL)
return NULL;
for (i = 0; i < account->nconvs; i++)
if (strcmp(account->convs[i]->id, conv_id) == 0)
return account->convs[i];
return NULL;
}
static DcConv *
account_find_conv(DcAccount *account, const char *conv_id)
{
size_t i;
for (i = 0; i < account->nconvs; i++)
if (strcmp(account->convs[i]->id, conv_id) == 0)
return account->convs[i];
return NULL;
}
DcConv *
dc_model_add_conv(DcModel *model, const char *account_id, const char *conv_id,
DcConvKind kind, const char *title)
{
DcAccount *account;
DcConv *conv;
char *t;
if (conv_id == NULL || *conv_id == '\0')
return NULL;
account = dc_model_find_account(model, account_id);
if (account == NULL)
return NULL;
conv = account_find_conv(account, conv_id);
if (conv != NULL) {
conv->kind = kind;
if (title != NULL) {
t = strdup(title);
if (t == NULL)
return NULL;
free(conv->title);
conv->title = t;
}
return conv;
}
conv = calloc(1, sizeof(*conv));
if (conv == NULL)
return NULL;
conv->id = strdup(conv_id);
conv->title = dup_string(title);
if (conv->id == NULL || conv->title == NULL) {
conv_free(conv);
return NULL;
}
conv->kind = kind;
if (ensure_ptrs((void ***)&account->convs, &account->capconvs,
account->nconvs + 1) != 0) {
conv_free(conv);
return NULL;
}
account->convs[account->nconvs++] = conv;
return conv;
}
const char *
dc_conv_id(const DcConv *conv)
{
return conv ? conv->id : NULL;
}
const char *
dc_conv_title(const DcConv *conv)
{
return conv ? conv->title : NULL;
}
const char *
dc_conv_topic(const DcConv *conv)
{
return conv ? conv->topic : NULL;
}
DcConvKind
dc_conv_kind(const DcConv *conv)
{
return conv ? conv->kind : DC_CONV_DIRECT;
}
void
dc_conv_set_presence(DcConv *conv, DcPresence presence)
{
if (conv != NULL)
conv->presence = presence;
}
DcPresence
dc_conv_presence(const DcConv *conv)
{
return conv ? conv->presence : DC_PRESENCE_OFFLINE;
}
void
dc_conv_set_topic(DcConv *conv, const char *topic)
{
char *t;
if (conv == NULL)
return;
t = dup_string(topic);
if (t == NULL)
return;
free(conv->topic);
conv->topic = t;
}
static int
conv_grow(DcConv *conv)
{
DcMessage *next;
size_t newcap;
size_t i;
if (conv->cap >= DC_CONV_MAX_MESSAGES)
return -1;
newcap = conv->cap != 0 ? conv->cap * 2 : DC_CONV_INITIAL_MESSAGES;
if (newcap > DC_CONV_MAX_MESSAGES)
newcap = DC_CONV_MAX_MESSAGES;
next = calloc(newcap, sizeof(*next));
if (next == NULL)
return -1;
for (i = 0; i < conv->count; i++)
next[i] = conv->messages[(conv->head + i) % conv->cap];
free(conv->messages);
conv->messages = next;
conv->cap = newcap;
conv->head = 0;
return 0;
}
int
dc_conv_add_message(DcConv *conv, const char *from, const char *text,
long long ts, int flags)
{
DcMessage msg;
DcMessage *slot;
if (conv == NULL)
return -1;
msg.from = dup_string(from);
msg.text = dup_string(text);
msg.ts = ts;
msg.flags = flags;
if (msg.from == NULL || msg.text == NULL) {
free(msg.from);
free(msg.text);
return -1;
}
if (conv->cap == 0 || conv->count == conv->cap) {
if (conv->cap < DC_CONV_MAX_MESSAGES) {
if (conv_grow(conv) != 0) {
free(msg.from);
free(msg.text);
return -1;
}
}
}
if (conv->count == conv->cap) {
/* the ring is full: drop the oldest slot in place */
slot = &conv->messages[conv->head];
free(slot->from);
free(slot->text);
*slot = msg;
conv->head = (conv->head + 1) % conv->cap;
} else {
slot = &conv->messages[(conv->head + conv->count) % conv->cap];
*slot = msg;
conv->count++;
}
if ((flags & (DTCHAT_MSG_SELF | DTCHAT_MSG_SYSTEM)) == 0)
conv->unread++;
return 0;
}
size_t
dc_conv_message_count(const DcConv *conv)
{
return conv ? conv->count : 0;
}
const DcMessage *
dc_conv_message_at(const DcConv *conv, size_t index)
{
if (conv == NULL || index >= conv->count || conv->cap == 0)
return NULL;
return &conv->messages[(conv->head + index) % conv->cap];
}
int
dc_conv_unread(const DcConv *conv)
{
return conv ? conv->unread : 0;
}
void
dc_conv_mark_read(DcConv *conv)
{
if (conv != NULL)
conv->unread = 0;
}