Files
mcde/programs/dtchat/xmpp_adapter.c
T
lotherk f0e8c668d6 dtchat: add XMPP MUC rooms (T4.4)
join sends a presence to room/nick with the http://jabber.org/protocol/muc <x/>; the room's self-presence is answered with a conversation event (kind group, title room), other occupants are reported as presence events with buddy=room/nick, groupchat messages are filed under the room with the sender's nick in from, and part sends an unavailable presence. The nick is the JID's local part. The hello caps gain muc. tests/xmpp_muc.py drives a minimal server through stream, SASL PLAIN, bind, join (checks the MUC element), self-presence, alice's presence, a groupchat message and the part; all 17 checks pass, exit 0, no stderr. UI wiring for adapter accounts is still open, as is the live test.
2026-11-17 10:02:33 +00:00

895 lines
24 KiB
C

/*
* xmpp_adapter.c -- dtchat's XMPP adapter, protocol v1 on stdio
*
* License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree.
*
* 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, 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"
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <strophe.h>
static char jid[256] = "";
static char password[256] = "";
static char resource[64] = "dtchat";
static char host[256] = "";
static int port;
static int configured;
static int online;
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;
/* the MUC rooms we joined, so a self-presence can be answered with the
* conversation event once */
#define MAX_ROOMS 16
typedef struct {
char room[256];
int announced;
} Room;
static Room rooms[MAX_ROOMS];
static int nrooms;
static void muc_nick(char *out, size_t outlen);
static Room *room_find(const char *room);
static void send_conversation(const char *conv, const char *kind,
const char *title);
static void
send_json(DcJson *json)
{
size_t len = 0;
char *text;
if (json == NULL)
return;
text = dc_json_write(json, &len);
if (text != NULL) {
(void)dc_frame_send(STDOUT_FILENO, text, len);
free(text);
}
dc_json_free(json);
}
static void
send_state(const char *state, const char *detail)
{
DcJson *json = dc_json_new_object();
(void)dc_json_object_set(json, "type", dc_json_new_string("state"));
(void)dc_json_object_set(json, "state", dc_json_new_string(state));
if (detail != NULL && *detail != '\0')
(void)dc_json_object_set(json, "detail",
dc_json_new_string(detail));
send_json(json);
}
static void
send_error(const char *code, const char *text, int fatal)
{
DcJson *json = dc_json_new_object();
(void)dc_json_object_set(json, "type", dc_json_new_string("error"));
(void)dc_json_object_set(json, "code", dc_json_new_string(code));
(void)dc_json_object_set(json, "text", dc_json_new_string(text));
(void)dc_json_object_set(json, "fatal", dc_json_new_bool(fatal));
send_json(json);
}
static void
send_bye(const char *reason)
{
DcJson *json = dc_json_new_object();
(void)dc_json_object_set(json, "type", dc_json_new_string("bye"));
(void)dc_json_object_set(json, "reason", dc_json_new_string(reason));
send_json(json);
}
static void
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",
"typing", "muc" };
for (size_t i = 0; i < sizeof(capnames) / sizeof(capnames[0]); i++)
(void)dc_json_array_append(caps,
dc_json_new_string(capnames[i]));
#define FIELD(key, label, type) do { \
DcJson *f = dc_json_new_object(); \
(void)dc_json_object_set(f, "key", dc_json_new_string(key)); \
(void)dc_json_object_set(f, "label", dc_json_new_string(label)); \
(void)dc_json_object_set(f, "type", dc_json_new_string(type)); \
(void)dc_json_array_append(settings, f); \
} while (0)
FIELD("jid", "Jabber ID", "text");
FIELD("password", "Password", "secret");
FIELD("resource", "Resource", "text");
FIELD("host", "Server", "text");
FIELD("port", "Port", "int");
(void)dc_json_object_set(hello, "type", dc_json_new_string("hello"));
(void)dc_json_object_set(hello, "ipc", dc_json_new_int(DC_IPC_VERSION));
(void)dc_json_object_set(hello, "protocol",
dc_json_new_string("xmpp"));
(void)dc_json_object_set(hello, "version",
dc_json_new_string("0.1"));
(void)dc_json_object_set(hello, "caps", caps);
(void)dc_json_object_set(hello, "settings", settings);
send_json(hello);
#undef FIELD
}
/* "user@example.org/resource" -> "user@example.org" */
static void
bare_jid(const char *from, char *out, size_t outlen)
{
size_t i = 0;
if (from == NULL)
from = "";
while (from[i] != '\0' && from[i] != '/' && i + 1 < outlen) {
out[i] = from[i];
i++;
}
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));
/* a room's presence: our own announces the conversation, occupants
* are shown as room/nick */
if (strchr(from, '/') != NULL) {
Room *room = room_find(conv);
if (room != NULL) {
const char *res = strchr(from, '/') + 1;
char nick[64];
muc_nick(nick, sizeof(nick));
if (type == NULL) {
if (!room->announced &&
strcmp(res, nick) == 0) {
room->announced = 1;
send_conversation(conv, "group", conv);
} else if (strcmp(res, nick) != 0) {
send_presence_event(from, "online", NULL,
NULL);
}
} else if (strcmp(type, "unavailable") == 0 &&
strcmp(res, nick) != 0) {
send_presence_event(from, "offline", NULL, NULL);
}
return 1;
}
}
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;
}
/* 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);
}
/* the nickname in a room: the local part of the JID */
static void
muc_nick(char *out, size_t outlen)
{
size_t i = 0;
while (jid[i] != '\0' && jid[i] != '@' && i + 1 < outlen) {
out[i] = jid[i];
i++;
}
out[i] = '\0';
if (i == 0)
snprintf(out, outlen, "dtchat");
}
static Room *
room_find(const char *room)
{
int i;
for (i = 0; i < nrooms; i++)
if (strcmp(rooms[i].room, room) == 0)
return &rooms[i];
return NULL;
}
static Room *
room_add(const char *room)
{
Room *r = room_find(room);
if (r != NULL || nrooms == MAX_ROOMS)
return r;
r = &rooms[nrooms++];
memset(r, 0, sizeof(*r));
snprintf(r->room, sizeof(r->room), "%s", room);
return r;
}
static void
send_conversation(const char *conv, const char *kind, const char *title)
{
DcJson *json = dc_json_new_object();
(void)dc_json_object_set(json, "type",
dc_json_new_string("conversation"));
(void)dc_json_object_set(json, "conv", dc_json_new_string(conv));
(void)dc_json_object_set(json, "kind", dc_json_new_string(kind));
(void)dc_json_object_set(json, "title",
dc_json_new_string(title != NULL ? title : conv));
send_json(json);
}
static void
room_presence(const char *room, const char *type)
{
xmpp_stanza_t *presence;
char nick[64];
char full[512];
if (ctx == NULL || conn == NULL || room == NULL)
return;
muc_nick(nick, sizeof(nick));
snprintf(full, sizeof(full), "%s/%s", room, nick);
presence = xmpp_stanza_new(ctx);
if (presence == NULL)
return;
xmpp_stanza_set_name(presence, "presence");
xmpp_stanza_set_attribute(presence, "to", full);
if (type != NULL)
xmpp_stanza_set_attribute(presence, "type", type);
if (type == NULL) {
xmpp_stanza_t *x = xmpp_stanza_new(ctx);
if (x != NULL) {
xmpp_stanza_set_name(x, "x");
xmpp_stanza_set_ns(x, "http://jabber.org/protocol/muc");
xmpp_stanza_add_child(presence, x);
xmpp_stanza_release(x);
}
}
xmpp_send(conn, presence);
xmpp_stanza_release(presence);
}
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)
{
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;
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;
if (xmpp_stanza_get_attribute(stanza, "type") != NULL &&
strcmp(xmpp_stanza_get_attribute(stanza, "type"), "groupchat") == 0) {
xmpp_stanza_t *gbody;
char room[256];
const char *sender = "?";
char *gtext;
bare_jid(from, room, sizeof(room));
if (strchr(from, '/') != NULL)
sender = strchr(from, '/') + 1;
gbody = xmpp_stanza_get_child_by_name(stanza, "body");
if (gbody == NULL)
return 1;
gtext = xmpp_stanza_get_text(gbody);
if (gtext == NULL)
return 1;
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(room));
(void)dc_json_object_set(json, "from",
dc_json_new_string(sender));
(void)dc_json_object_set(json, "kind",
dc_json_new_string("text"));
(void)dc_json_object_set(json, "text",
dc_json_new_string(gtext));
send_json(json);
xmpp_free(ctx, gtext);
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
* (measured against the fake server); get_text() copies it.
*/
text = xmpp_stanza_get_text(body);
allocated = (char *)text;
if (text == NULL)
return 1;
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);
return 1;
}
static void
conn_handler(xmpp_conn_t *c, xmpp_conn_event_t event, int error,
xmpp_stream_error_t *stream_error, void *userdata)
{
(void)c;
(void)userdata;
if (event == XMPP_CONN_CONNECT) {
online = 1;
send_state("online", jid);
send_presence_available();
request_roster();
enable_carbons();
return;
}
online = 0;
if (quitting)
return;
if (event == XMPP_CONN_FAIL) {
char text[512];
snprintf(text, sizeof(text), "cannot connect to %s",
host[0] != '\0' ? host : jid);
send_error("network", text, 0);
} else if (stream_error != NULL && stream_error->text != NULL) {
send_error("stream", stream_error->text, 0);
} else if (error != 0) {
send_error("network", "the XMPP connection failed", 0);
}
send_state("offline", NULL);
send_bye(error != 0 ? "failed" : "disconnected");
_exit(0);
}
static int
connect_account(void)
{
long flags = 0;
if (jid[0] == '\0') {
send_error("config", "the Jabber ID is required", 0);
send_state("offline", NULL);
return -1;
}
xmpp_initialize();
ctx = xmpp_ctx_new(NULL, getenv("DTCHAT_XMPP_DEBUG") != NULL ?
xmpp_get_default_logger(XMPP_LEVEL_DEBUG) : NULL);
if (ctx == NULL) {
send_error("config", "out of memory", 0);
return -1;
}
conn = xmpp_conn_new(ctx);
if (conn == NULL) {
send_error("config", "out of memory", 0);
return -1;
}
{
char full[sizeof(jid) + sizeof(resource)];
if (strchr(jid, '/') != NULL)
snprintf(full, sizeof(full), "%s", jid);
else
snprintf(full, sizeof(full), "%s/%s", jid, resource);
xmpp_conn_set_jid(conn, full);
}
xmpp_conn_set_pass(conn, password);
if (getenv("DTCHAT_XMPP_NO_TLS") != NULL)
flags |= XMPP_CONN_FLAG_DISABLE_TLS;
if (getenv("DTCHAT_XMPP_TRUST_TLS") != NULL)
flags |= XMPP_CONN_FLAG_TRUST_TLS;
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) {
send_error("network", "cannot start the XMPP connection", 0);
send_state("offline", NULL);
return -1;
}
return 0;
}
static void
handle_command(DcJson *cmd)
{
const char *type = dc_json_string(dc_json_get(cmd, "type"));
if (type == NULL)
return;
if (strcmp(type, "configure") == 0) {
DcJson *settings = dc_json_get(cmd, "settings");
DcJson *secrets = dc_json_get(cmd, "secrets");
const char *v;
v = dc_json_string(dc_json_get(settings, "jid"));
if (v != NULL)
snprintf(jid, sizeof(jid), "%s", v);
v = dc_json_string(dc_json_get(settings, "resource"));
if (v != NULL && *v != '\0')
snprintf(resource, sizeof(resource), "%s", v);
v = dc_json_string(dc_json_get(settings, "host"));
if (v != NULL)
snprintf(host, sizeof(host), "%s", v);
if (dc_json_get(settings, "port") != NULL) {
long p = (long)dc_json_int(dc_json_get(settings,
"port"));
if (p > 0 && p <= 65535)
port = (int)p;
}
v = dc_json_string(dc_json_get(secrets, "password"));
if (v != NULL)
snprintf(password, sizeof(password), "%s", v);
configured = 1;
} else if (strcmp(type, "connect") == 0) {
if (!configured) {
send_error("config", "configure first", 0);
send_state("offline", NULL);
return;
}
(void)connect_account();
} else if (strcmp(type, "join") == 0) {
const char *conv = dc_json_string(dc_json_get(cmd, "conv"));
if (conn == NULL || !online || conv == NULL)
return;
if (room_add(conv) != NULL)
room_presence(conv, NULL);
} else if (strcmp(type, "part") == 0) {
const char *conv = dc_json_string(dc_json_get(cmd, "conv"));
Room *room = conv != NULL ? room_find(conv) : NULL;
if (conn == NULL || conv == NULL || room == NULL)
return;
room_presence(conv, "unavailable");
room->announced = 0;
} 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"));
const char *client_id = dc_json_string(dc_json_get(cmd,
"client_id"));
xmpp_stanza_t *msg;
if (conn == NULL || !online || conv == NULL || text == NULL)
return;
msg = xmpp_message_new(ctx, "chat", conv, NULL);
if (msg == NULL)
return;
(void)xmpp_message_set_body(msg, text);
xmpp_send(conn, msg);
xmpp_stanza_release(msg);
{
DcJson *sent = dc_json_new_object();
(void)dc_json_object_set(sent, "type",
dc_json_new_string("sent"));
(void)dc_json_object_set(sent, "conv",
dc_json_new_string(conv));
if (client_id != NULL)
(void)dc_json_object_set(sent, "client_id",
dc_json_new_string(client_id));
send_json(sent);
}
} else if (strcmp(type, "disconnect") == 0) {
quitting = 1;
if (conn != NULL)
xmpp_disconnect(conn);
send_bye("requested");
_exit(0);
}
}
int
main(int argc, char **argv)
{
DcFrameBuf frame;
char buf[4096];
int fl;
(void)argc;
(void)argv;
signal(SIGPIPE, SIG_IGN);
dc_frame_init(&frame);
send_hello();
fl = fcntl(STDIN_FILENO, F_GETFL, 0);
if (fl >= 0)
(void)fcntl(STDIN_FILENO, F_SETFL, fl | O_NONBLOCK);
for (;;) {
ssize_t n;
if (ctx != NULL)
xmpp_run_once(ctx, 50);
if (ctx == NULL) {
struct timespec pause;
pause.tv_sec = 0;
pause.tv_nsec = 20 * 1000 * 1000;
(void)nanosleep(&pause, NULL);
}
for (;;) {
n = read(STDIN_FILENO, buf, sizeof(buf));
if (n < 0) {
if (errno == EINTR)
continue;
break;
}
if (n == 0)
goto out;
if (dc_frame_feed(&frame, buf, (size_t)n) != 0)
goto out;
for (;;) {
size_t len = 0;
char *line = dc_frame_next(&frame, &len);
DcJson *cmd;
if (line == NULL)
break;
cmd = dc_json_parse(line, len, NULL, 0);
free(line);
if (cmd != NULL) {
handle_command(cmd);
dc_json_free(cmd);
}
}
}
}
out:
xmpp_disconnect(conn);
if (conn != NULL)
xmpp_conn_release(conn);
if (ctx != NULL)
xmpp_ctx_free(ctx);
xmpp_shutdown();
dc_frame_free(&frame);
return 0;
}