dtchat: add the XMPP roster and presence (T4.2)
After the bind the adapter sends <presence/> 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.
This commit is contained in:
1 parent
f59563a84a
commit
fed0e5010c
2 files changed
+271
-6
No files matched your search
@@ -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) {
|
||||
|
||||
+63
-2
@@ -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'>"
|
||||
"<jid>tester@example.org/dtchat</jid></bind></iq>" %
|
||||
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"<presence" not in data) \
|
||||
and time.time() < deadline:
|
||||
chunk = conn.recv(4096)
|
||||
if not chunk:
|
||||
break
|
||||
data += chunk
|
||||
roster_request_seen.append(b"jabber:iq:roster" in data)
|
||||
presence_seen.append(b"<presence" in data)
|
||||
buf = b""
|
||||
except socket.timeout:
|
||||
pass
|
||||
|
||||
# the roster answer and two presence changes for bob
|
||||
conn.sendall(b"<iq type='result' id='roster1'>"
|
||||
b"<query xmlns='jabber:iq:roster'>"
|
||||
b"<item jid='bob@example.org' name='Bob' "
|
||||
b"subscription='both'><group>Friends</group></item>"
|
||||
b"<item jid='carol@example.org' "
|
||||
b"subscription='none'/></query></iq>")
|
||||
conn.sendall(b"<presence from='bob@example.org/laptop'>"
|
||||
b"<show>away</show><status>at lunch</status></presence>")
|
||||
conn.sendall(b"<presence from='bob@example.org/laptop' "
|
||||
b"type='unavailable'/>")
|
||||
|
||||
# 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' "
|
||||
@@ -157,16 +188,46 @@ send({"type": "connect"})
|
||||
|
||||
online = False
|
||||
message = None
|
||||
deadline = time.time() + 8
|
||||
while time.time() < deadline and not (online and message):
|
||||
roster = {}
|
||||
presence = []
|
||||
deadline = time.time() + 10
|
||||
while time.time() < deadline:
|
||||
e = ev(1.0)
|
||||
if e is None:
|
||||
if roster.get("bob@example.org") and roster.get("carol@example.org") \
|
||||
and any(p.get("show") == "offline" for p in presence):
|
||||
break
|
||||
continue
|
||||
if e.get("type") == "state" and e.get("state") == "online":
|
||||
online = True
|
||||
elif e.get("type") == "message":
|
||||
message = e
|
||||
elif e.get("type") == "roster":
|
||||
contact = e.get("contact", {})
|
||||
roster[contact.get("id")] = contact
|
||||
elif e.get("type") == "presence":
|
||||
presence.append(e)
|
||||
check(online, "online after SASL and bind")
|
||||
check(roster_request_seen and roster_request_seen[0],
|
||||
"the client asked for the roster")
|
||||
check(presence_seen and presence_seen[0],
|
||||
"the client announced itself")
|
||||
check(roster.get("bob@example.org", {}).get("name") == "Bob",
|
||||
"roster item with its name")
|
||||
check(roster.get("bob@example.org", {}).get("subscription") == "both",
|
||||
"roster item with its subscription")
|
||||
check(roster.get("carol@example.org", {}).get("subscription") == "none",
|
||||
"second roster item")
|
||||
check(roster.get("bob@example.org", {}).get("online") is False,
|
||||
"roster starts offline")
|
||||
online_ev = [p for p in presence
|
||||
if p.get("show") == "online" and p.get("away") == "away"]
|
||||
check(bool(online_ev), "presence online with show")
|
||||
check(online_ev and online_ev[0].get("status") == "at lunch",
|
||||
"presence status")
|
||||
check(any(p.get("show") == "offline" and
|
||||
p.get("buddy") == "bob@example.org" for p in presence),
|
||||
"presence offline")
|
||||
check(message is not None, "incoming message event")
|
||||
if message is not None:
|
||||
check(message.get("conv") == "bob@example.org",
|
||||
|
||||
Reference in new issue
Block a user