dtchat: add XMPP carbons and chat states (T4.3)

After the bind the adapter enables XEP-0280 carbons and unwraps sent/received forwarded messages: a message another of our resources sent becomes a normal message event with self=true and the contact as conv, a received one self=false. XEP-0085 chat states are handled before the body: composing emits typing on, paused/active/inactive/gone off. The typing command sends composing or paused to the conversation, and dc_adapter_send_typing() puts it on the core side. tests/xmpp_fake.py checks carbons were enabled, the forwarded message (self, conv bob, text), typing on/off from bob, and that a typing command reaches the server as <composing> addressed to bob. Standalone adapter: all checks ok. The UI does not send typing for adapter accounts yet (adapter account wiring is still open), which the commit documents.
This commit is contained in:
lotherk committed 2026-11-17 09:56:18 +00:00
1 parent fed0e5010c
commit a2c4a0e4c4
4 files changed
+200 -11

No files matched your search

+19
View File
@@ -738,6 +738,25 @@ dc_adapter_send_text(DcAdapter *adapter, const char *conv, const char *text,
return rc;
}
int
dc_adapter_send_typing(DcAdapter *adapter, const char *conv, int on)
{
DcJson *cmd;
int rc;
if (adapter == NULL || conv == NULL)
return -1;
cmd = dc_json_new_object();
if (cmd == NULL)
return -1;
(void)dc_json_object_set(cmd, "type", dc_json_new_string("typing"));
(void)dc_json_object_set(cmd, "conv", dc_json_new_string(conv));
(void)dc_json_object_set(cmd, "on", dc_json_new_bool(on));
rc = dc_adapter_send_json(adapter, cmd);
dc_json_free(cmd);
return rc;
}
int
dc_adapter_join(DcAdapter *adapter, const char *conv)
{
+2
View File
@@ -700,6 +700,8 @@ 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);
/* XEP-0085 / IRC typing: tell CONV that the person is typing or stopped */
int dc_adapter_send_typing(DcAdapter *adapter, const char *conv, int on);
int dc_adapter_join(DcAdapter *adapter, const char *conv);
int dc_adapter_answer(DcAdapter *adapter, int request_id, const char *value);
+134 -9
View File
@@ -6,10 +6,11 @@
* 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, 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.
* messages in both directions, message carbons (XEP-0280), chat states
* (XEP-0085), 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"
@@ -105,7 +106,8 @@ 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", "roster", "presence" };
static const char *const capnames[] = { "text", "roster", "presence",
"typing" };
for (size_t i = 0; i < sizeof(capnames) / sizeof(capnames[0]); i++)
(void)dc_json_array_append(caps,
@@ -300,6 +302,56 @@ iq_cb(xmpp_conn_t *c, xmpp_stanza_t *stanza, void *userdata)
return 1;
}
/* XEP-0280: a message another of our resources sent or received arrives
* wrapped in <sent>/<received><forwarded>; the inner message is the one the
* UI cares about, and MINE says which direction it went */
static xmpp_stanza_t *
carbon_inner(xmpp_stanza_t *stanza, int *mine)
{
xmpp_stanza_t *sent = xmpp_stanza_get_child_by_name_and_ns(stanza,
"sent", "urn:xmpp:carbons:2");
xmpp_stanza_t *wrapper = sent;
xmpp_stanza_t *forwarded;
xmpp_stanza_t *inner;
if (wrapper == NULL)
wrapper = xmpp_stanza_get_child_by_name_and_ns(stanza,
"received", "urn:xmpp:carbons:2");
if (wrapper == NULL)
return NULL;
forwarded = xmpp_stanza_get_child_by_name_and_ns(wrapper,
"forwarded", "urn:xmpp:forward:0");
if (forwarded == NULL)
return NULL;
inner = xmpp_stanza_get_child_by_name(forwarded, "message");
if (inner == NULL)
return NULL;
*mine = sent != NULL;
return inner;
}
static void
enable_carbons(void)
{
xmpp_stanza_t *iq;
xmpp_stanza_t *enable;
if (ctx == NULL || conn == NULL)
return;
iq = xmpp_iq_new(ctx, "set", "carbons1");
if (iq == NULL)
return;
enable = xmpp_stanza_new(ctx);
if (enable != NULL) {
xmpp_stanza_set_name(enable, "enable");
xmpp_stanza_set_ns(enable, "urn:xmpp:carbons:2");
xmpp_stanza_add_child(iq, enable);
xmpp_stanza_release(enable);
}
xmpp_send(conn, iq);
xmpp_stanza_release(iq);
}
static void
send_presence_available(void)
{
@@ -340,16 +392,65 @@ request_roster(void)
static int
message_cb(xmpp_conn_t *c, xmpp_stanza_t *stanza, void *userdata)
{
const char *from = xmpp_stanza_get_attribute(stanza, "from");
xmpp_stanza_t *body = xmpp_stanza_get_child_by_name(stanza, "body");
xmpp_stanza_t *inner;
xmpp_stanza_t *body;
xmpp_stanza_t *state;
const char *from;
const char *to;
const char *text;
const char *chatstate_ns = "http://jabber.org/protocol/chatstates";
char *allocated = NULL;
char conv[256];
int mine = 0;
DcJson *json;
(void)c;
(void)userdata;
if (from == NULL || body == NULL)
inner = carbon_inner(stanza, &mine);
if (inner != NULL)
stanza = inner;
from = xmpp_stanza_get_attribute(stanza, "from");
to = xmpp_stanza_get_attribute(stanza, "to");
if (from == NULL)
return 1;
/* XEP-0085: a message with a chat state instead of a body */
state = xmpp_stanza_get_child_by_name_and_ns(stanza, "composing",
chatstate_ns);
if (state != NULL) {
bare_jid(from, conv, sizeof(conv));
json = dc_json_new_object();
(void)dc_json_object_set(json, "type",
dc_json_new_string("typing"));
(void)dc_json_object_set(json, "conv",
dc_json_new_string(conv));
(void)dc_json_object_set(json, "on", dc_json_new_bool(1));
send_json(json);
return 1;
}
state = xmpp_stanza_get_child_by_name_and_ns(stanza, "paused",
chatstate_ns);
if (state == NULL)
state = xmpp_stanza_get_child_by_name_and_ns(stanza, "active",
chatstate_ns);
if (state == NULL)
state = xmpp_stanza_get_child_by_name_and_ns(stanza, "gone",
chatstate_ns);
if (state == NULL)
state = xmpp_stanza_get_child_by_name_and_ns(stanza, "inactive",
chatstate_ns);
if (state != NULL) {
bare_jid(from, conv, sizeof(conv));
json = dc_json_new_object();
(void)dc_json_object_set(json, "type",
dc_json_new_string("typing"));
(void)dc_json_object_set(json, "conv",
dc_json_new_string(conv));
(void)dc_json_object_set(json, "on", dc_json_new_bool(0));
send_json(json);
return 1;
}
body = xmpp_stanza_get_child_by_name(stanza, "body");
if (body == NULL)
return 1;
/*
* libstrophe 0.14's get_text_ptr() answered NULL for a parsed body
@@ -359,13 +460,14 @@ message_cb(xmpp_conn_t *c, xmpp_stanza_t *stanza, void *userdata)
allocated = (char *)text;
if (text == NULL)
return 1;
bare_jid(from, conv, sizeof(conv));
bare_jid(mine ? to : from, conv, sizeof(conv));
json = dc_json_new_object();
(void)dc_json_object_set(json, "type", dc_json_new_string("message"));
(void)dc_json_object_set(json, "conv", dc_json_new_string(conv));
(void)dc_json_object_set(json, "from", dc_json_new_string(conv));
(void)dc_json_object_set(json, "kind", dc_json_new_string("text"));
(void)dc_json_object_set(json, "text", dc_json_new_string(text));
(void)dc_json_object_set(json, "self", dc_json_new_bool(mine));
send_json(json);
if (allocated != NULL)
xmpp_free(ctx, allocated);
@@ -383,6 +485,7 @@ conn_handler(xmpp_conn_t *c, xmpp_conn_event_t event, int error,
send_state("online", jid);
send_presence_available();
request_roster();
enable_carbons();
return;
}
online = 0;
@@ -495,6 +598,28 @@ handle_command(DcJson *cmd)
return;
}
(void)connect_account();
} else if (strcmp(type, "typing") == 0) {
const char *conv = dc_json_string(dc_json_get(cmd, "conv"));
int on = dc_json_bool(dc_json_get(cmd, "on"));
xmpp_stanza_t *msg;
xmpp_stanza_t *child;
if (conn == NULL || !online || conv == NULL)
return;
msg = xmpp_message_new(ctx, "chat", conv, NULL);
if (msg == NULL)
return;
child = xmpp_stanza_new(ctx);
if (child != NULL) {
xmpp_stanza_set_name(child,
on ? "composing" : "paused");
xmpp_stanza_set_ns(child,
"http://jabber.org/protocol/chatstates");
xmpp_stanza_add_child(msg, child);
xmpp_stanza_release(child);
}
xmpp_send(conn, msg);
xmpp_stanza_release(msg);
} else if (strcmp(type, "send") == 0) {
const char *conv = dc_json_string(dc_json_get(cmd, "conv"));
const char *text = dc_json_string(dc_json_get(cmd, "text"));
+45 -2
View File
@@ -37,6 +37,8 @@ received = []
sasl_payload = []
roster_request_seen = []
presence_seen = []
carbons_seen = []
typing_seen = []
done = threading.Event()
@@ -112,6 +114,7 @@ def server():
data += chunk
roster_request_seen.append(b"jabber:iq:roster" in data)
presence_seen.append(b"<presence" in data)
carbons_seen.append(b"urn:xmpp:carbons:2" in data)
buf = b""
except socket.timeout:
pass
@@ -128,15 +131,37 @@ def server():
conn.sendall(b"<presence from='bob@example.org/laptop' "
b"type='unavailable'/>")
# another resource sent a message (XEP-0280) ...
conn.sendall(b"<message from='tester@example.org/phone' "
b"to='bob@example.org' type='chat'>"
b"<sent xmlns='urn:xmpp:carbons:2'>"
b"<forwarded xmlns='urn:xmpp:forward:0'>"
b"<message from='tester@example.org/phone' "
b"to='bob@example.org' type='chat'>"
b"<body>from my phone</body></message>"
b"</forwarded></sent></message>")
# ... and bob started and stopped typing (XEP-0085)
conn.sendall(b"<message from='bob@example.org/laptop' "
b"to='tester@example.org/dtchat' type='chat'>"
b"<composing "
b"xmlns='http://jabber.org/protocol/chatstates'/></message>")
conn.sendall(b"<message from='bob@example.org/laptop' "
b"to='tester@example.org/dtchat' type='chat'>"
b"<paused "
b"xmlns='http://jabber.org/protocol/chatstates'/></message>")
# an incoming message from a contact (after the client is connected)
time.sleep(0.5)
conn.sendall(b"<message from='bob@example.org/laptop' type='chat' "
b"to='tester@example.org/dtchat'>"
b"<body>hello back</body></message>")
# wait for our outgoing message
# wait for our outgoing message, then for the typing command
try:
if fill_until(b"</message>"):
received.append(take(b"</message>").decode("utf-8", "replace"))
if fill_until(b"</message>"):
typing_seen.append(take(b"</message>").decode("utf-8",
"replace"))
except socket.timeout:
pass
done.set()
@@ -190,6 +215,8 @@ online = False
message = None
roster = {}
presence = []
typing = []
carbon_msg = None
deadline = time.time() + 10
while time.time() < deadline:
e = ev(1.0)
@@ -207,6 +234,10 @@ while time.time() < deadline:
roster[contact.get("id")] = contact
elif e.get("type") == "presence":
presence.append(e)
elif e.get("type") == "typing":
typing.append(e)
if e.get("type") == "message" and e.get("self"):
carbon_msg = e
check(online, "online after SASL and bind")
check(roster_request_seen and roster_request_seen[0],
"the client asked for the roster")
@@ -228,6 +259,14 @@ check(online_ev and online_ev[0].get("status") == "at lunch",
check(any(p.get("show") == "offline" and
p.get("buddy") == "bob@example.org" for p in presence),
"presence offline")
check(carbons_seen and carbons_seen[0],
"the client enabled message carbons")
check(carbon_msg is not None and
carbon_msg.get("text") == "from my phone", "carbons message event")
check(carbon_msg is not None and carbon_msg.get("conv") == "bob@example.org",
"carbons message is filed under the contact")
check(any(t.get("on") for t in typing), "incoming typing on")
check(any(not t.get("on") for t in typing), "incoming typing off")
check(message is not None, "incoming message event")
if message is not None:
check(message.get("conv") == "bob@example.org",
@@ -245,7 +284,11 @@ while time.time() < deadline and sent is None:
check(sent is not None, "sent event")
check(sasl_payload and sasl_payload[0].startswith(b"\0tester\0"),
"SASL PLAIN carries the user and password")
check(done.wait(5), "server saw the outgoing message")
send({"type": "typing", "conv": "bob@example.org", "on": True})
check(done.wait(5), "server saw the outgoing message and typing")
check(typing_seen and "composing" in typing_seen[-1] and
"bob@example.org" in typing_seen[-1],
"outgoing typing reached the server")
joined = "\n".join(received)
check("hello xmpp" in joined, "outgoing stanza reached the server")
check("to='bob@example.org'" in joined or 'to="bob@example.org"' in joined,