From fed0e5010c27fb485f3f552c722bfeff3c81f862 Mon Sep 17 00:00:00 2001 From: lotherk Date: Tue, 17 Nov 2026 09:48:58 +0000 Subject: [PATCH] dtchat: add the XMPP roster and presence (T4.2) After the bind the adapter sends and asks for the roster (iq get, jabber:iq:roster) and handles the result and pushes: every item becomes a roster event with the contact's id, name, subscription and current online state. Presence from contacts is tracked per bare JID and emitted as presence events (online with the XMPP show and status, offline, subscribe/unsubscribe); libstrophe 0.14's get_text is used for child text. The hello caps gain roster and presence, and the event shapes follow docs/reference/dtchat-adapter-protocol.txt (buddy, contact, away). tests/xmpp_fake.py now checks that the client announces itself and asks for the roster, delivers bob (name/subscription) and carol, the online-with-show/status and offline presence, and still carries messages both ways. Run against the standalone adapter: 22 checks, all ok. In-tree build pending: make mcde-host is running after the configure change. --- programs/dtchat/xmpp_adapter.c | 212 ++++++++++++++++++++++++++++++++- tests/xmpp_fake.py | 65 +++++++++- 2 files changed, 271 insertions(+), 6 deletions(-) diff --git a/programs/dtchat/xmpp_adapter.c b/programs/dtchat/xmpp_adapter.c index ddbcef49..d6c158c1 100644 --- a/programs/dtchat/xmpp_adapter.c +++ b/programs/dtchat/xmpp_adapter.c @@ -6,9 +6,10 @@ * T4.1 is the skeleton: hello with the setting schema (JID, password, * resource, optional server and port), configure, connect through * libstrophe, xmpp_run_once in the main loop, online/offline states, text - * messages in both directions and a clean disconnect. The roster, presence, - * MUC and the rest arrive with T4.2-T4.5; anything not implemented here is - * named in the hello capabilities. + * messages in both directions, the roster (jabber:iq:roster, including + * pushes), presence in both directions and a clean disconnect. MUC and the + * rest arrive with T4.4/T4.5; anything not implemented here is named in the + * hello capabilities. */ #include "dc_internal.h" @@ -35,6 +36,18 @@ static int quitting; static xmpp_ctx_t *ctx; static xmpp_conn_t *conn; +/* the contacts we know from the roster and their last presence, so a roster + * event can carry the state without asking the server again */ +#define MAX_CONTACTS 128 +typedef struct { + char jid[256]; + char show[32]; + char status[256]; + int online; +} Contact; +static Contact contacts[MAX_CONTACTS]; +static int ncontacts; + static void send_json(DcJson *json) { @@ -92,7 +105,7 @@ send_hello(void) DcJson *hello = dc_json_new_object(); DcJson *caps = dc_json_new_array(); DcJson *settings = dc_json_new_array(); - static const char *const capnames[] = { "text" }; + static const char *const capnames[] = { "text", "roster", "presence" }; for (size_t i = 0; i < sizeof(capnames) / sizeof(capnames[0]); i++) (void)dc_json_array_append(caps, @@ -138,6 +151,192 @@ bare_jid(const char *from, char *out, size_t outlen) out[i] = '\0'; } +static Contact * +contact_find(const char *jid) +{ + int i; + + for (i = 0; i < ncontacts; i++) + if (strcmp(contacts[i].jid, jid) == 0) + return &contacts[i]; + return NULL; +} + +static Contact * +contact_get(const char *jid) +{ + Contact *c = contact_find(jid); + + if (c != NULL) + return c; + if (ncontacts == MAX_CONTACTS) + return NULL; + c = &contacts[ncontacts++]; + memset(c, 0, sizeof(*c)); + snprintf(c->jid, sizeof(c->jid), "%s", jid); + return c; +} + +/* a child's text, copied (libstrophe 0.14's get_text_ptr answered NULL for a + * parsed body and is not used) */ +static void +child_text(xmpp_stanza_t *stanza, const char *name, char *out, size_t outlen) +{ + xmpp_stanza_t *child = xmpp_stanza_get_child_by_name(stanza, name); + char *text; + + out[0] = '\0'; + if (child == NULL) + return; + text = xmpp_stanza_get_text(child); + if (text == NULL) + return; + snprintf(out, outlen, "%s", text); + xmpp_free(ctx, text); +} + +static void +send_presence_event(const char *jid, const char *state, const char *away, + const char *status) +{ + DcJson *json = dc_json_new_object(); + + (void)dc_json_object_set(json, "type", dc_json_new_string("presence")); + (void)dc_json_object_set(json, "buddy", dc_json_new_string(jid)); + (void)dc_json_object_set(json, "show", dc_json_new_string(state)); + if (away != NULL && *away != '\0') + (void)dc_json_object_set(json, "away", dc_json_new_string(away)); + if (status != NULL && *status != '\0') + (void)dc_json_object_set(json, "status", + dc_json_new_string(status)); + send_json(json); +} + +static void +send_roster_item(const char *jid, const char *name, const char *subscription) +{ + DcJson *json = dc_json_new_object(); + DcJson *contact = dc_json_new_object(); + Contact *c = contact_find(jid); + + (void)dc_json_object_set(json, "type", dc_json_new_string("roster")); + (void)dc_json_object_set(contact, "id", dc_json_new_string(jid)); + (void)dc_json_object_set(contact, "name", + dc_json_new_string(name != NULL && *name != '\0' ? name : jid)); + (void)dc_json_object_set(contact, "subscription", + dc_json_new_string(subscription != NULL ? subscription : "none")); + (void)dc_json_object_set(contact, "online", + dc_json_new_bool(c != NULL && c->online)); + (void)dc_json_object_set(json, "contact", contact); + send_json(json); +} + +static int +presence_cb(xmpp_conn_t *c, xmpp_stanza_t *stanza, void *userdata) +{ + const char *from = xmpp_stanza_get_attribute(stanza, "from"); + const char *type = xmpp_stanza_get_attribute(stanza, "type"); + char conv[256]; + char show[32]; + char status[256]; + Contact *contact; + + (void)c; + (void)userdata; + if (from == NULL) + return 1; + bare_jid(from, conv, sizeof(conv)); + contact = contact_get(conv); + if (type == NULL) { + child_text(stanza, "show", show, sizeof(show)); + child_text(stanza, "status", status, sizeof(status)); + if (contact != NULL) { + contact->online = 1; + snprintf(contact->show, sizeof(contact->show), "%s", + show); + snprintf(contact->status, sizeof(contact->status), + "%s", status); + } + send_presence_event(conv, "online", show, status); + } else if (strcmp(type, "unavailable") == 0) { + if (contact != NULL) + contact->online = 0; + send_presence_event(conv, "offline", NULL, NULL); + } else if (strcmp(type, "subscribe") == 0) { + send_presence_event(conv, "subscribe", NULL, NULL); + } else if (strcmp(type, "unsubscribe") == 0 || + strcmp(type, "unsubscribed") == 0) { + if (contact != NULL) + contact->online = 0; + send_presence_event(conv, type, NULL, NULL); + } + return 1; +} + +static int +iq_cb(xmpp_conn_t *c, xmpp_stanza_t *stanza, void *userdata) +{ + xmpp_stanza_t *query; + xmpp_stanza_t *item; + + (void)c; + (void)userdata; + query = xmpp_stanza_get_child_by_name_and_ns(stanza, "query", + "jabber:iq:roster"); + if (query == NULL) + return 1; + for (item = xmpp_stanza_get_children(query); item != NULL; + item = xmpp_stanza_get_next(item)) { + const char *name = xmpp_stanza_get_name(item); + const char *jid = xmpp_stanza_get_attribute(item, "jid"); + const char *nick = xmpp_stanza_get_attribute(item, "name"); + const char *subscription = xmpp_stanza_get_attribute(item, + "subscription"); + + if (name == NULL || strcmp(name, "item") != 0 || jid == NULL) + continue; + send_roster_item(jid, nick, subscription); + } + return 1; +} + +static void +send_presence_available(void) +{ + xmpp_stanza_t *presence; + + if (ctx == NULL || conn == NULL) + return; + presence = xmpp_stanza_new(ctx); + if (presence == NULL) + return; + xmpp_stanza_set_name(presence, "presence"); + xmpp_send(conn, presence); + xmpp_stanza_release(presence); +} + +static void +request_roster(void) +{ + xmpp_stanza_t *iq; + xmpp_stanza_t *query; + + if (ctx == NULL || conn == NULL) + return; + iq = xmpp_iq_new(ctx, "get", "roster1"); + if (iq == NULL) + return; + query = xmpp_stanza_new(ctx); + if (query != NULL) { + xmpp_stanza_set_name(query, "query"); + xmpp_stanza_set_ns(query, "jabber:iq:roster"); + xmpp_stanza_add_child(iq, query); + xmpp_stanza_release(query); + } + xmpp_send(conn, iq); + xmpp_stanza_release(iq); +} + static int message_cb(xmpp_conn_t *c, xmpp_stanza_t *stanza, void *userdata) { @@ -182,6 +381,8 @@ conn_handler(xmpp_conn_t *c, xmpp_conn_event_t event, int error, if (event == XMPP_CONN_CONNECT) { online = 1; send_state("online", jid); + send_presence_available(); + request_roster(); return; } online = 0; @@ -242,6 +443,9 @@ connect_account(void) xmpp_conn_set_flags(conn, flags); xmpp_handler_add(conn, message_cb, "jabber:client", "message", NULL, NULL); + xmpp_handler_add(conn, presence_cb, "jabber:client", "presence", NULL, + NULL); + xmpp_handler_add(conn, iq_cb, "jabber:client", "iq", NULL, NULL); send_state("connecting", host[0] != '\0' ? host : "XMPP"); if (xmpp_connect_client(conn, host[0] != '\0' ? host : NULL, (unsigned short)port, conn_handler, NULL) != 0) { diff --git a/tests/xmpp_fake.py b/tests/xmpp_fake.py index dc394299..ae2604c0 100755 --- a/tests/xmpp_fake.py +++ b/tests/xmpp_fake.py @@ -35,6 +35,8 @@ srv.listen(1) port = srv.getsockname()[1] received = [] sasl_payload = [] +roster_request_seen = [] +presence_seen = [] done = threading.Event() @@ -97,6 +99,35 @@ def server(): "xmlns='urn:ietf:params:xml:ns:xmpp-bind'>" "tester@example.org/dtchat" % iqid).encode()) + # the client announces presence and asks for the roster; collect both + # (some of it may already sit in buf behind the bind iq) + try: + data = buf + deadline = time.time() + 3 + while (b"jabber:iq:roster" not in data or b"" + b"" + b"Friends" + b"") + conn.sendall(b"" + b"awayat lunch") + conn.sendall(b"") + # an incoming message from a contact (after the client is connected) time.sleep(0.5) conn.sendall(b"