Files
lotherk a1403f5177 dtchat: do not disconnect a NULL XMPP connection (T4.6)
The adapter's exit path called xmpp_disconnect(conn) unconditionally; when the process ended before connecting (hello only, stdin closed), libstrophe crashed on the NULL connection. The call is now inside the existing conn != NULL branch, so the installed dtchat-xmpp answers hello and exits cleanly.
2026-11-17 12:48:01 +00:00

1572 lines
40 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, MUC rooms, file transfer over IBB (XEP-0047, with an
* explicit accept) and a clean disconnect; 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 <openssl/evp.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", "files" };
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;
}
/* ---------------------------- files over IBB ----------------------------- */
#define IBB_FILE_MAX (32 * 1024 * 1024)
#define IBB_BLOCK 4096
#define MAX_FILES 4
#define FILE_TIMEOUT 45
#define IBB_NS "http://jabber.org/protocol/ibb"
#define FILE_NS "urn:dtchat:file:0"
typedef struct {
int used;
char id[64];
char conv[256];
char name[256];
char sha256[65];
FILE *fp;
unsigned long long size;
unsigned long long sent;
size_t next_len;
int seq;
int state; /* 1 open, 2 data, 3 close */
char iqid[64];
time_t last;
} OutFile;
typedef struct {
int used;
char sid[64];
char from[256];
char iqid[64];
char name[256];
char sha256[65];
unsigned long long size;
unsigned long long got;
int state; /* 1 offered, 2 streaming */
time_t last;
} InFile;
static OutFile outfiles[MAX_FILES];
static InFile infiles[MAX_FILES];
static unsigned int file_iq_seq;
static void
reset_out(OutFile *o)
{
if (o->fp != NULL)
fclose(o->fp);
memset(o, 0, sizeof(*o));
}
static void
reset_in(InFile *in)
{
memset(in, 0, sizeof(*in));
}
static void
file_error(const char *id, const char *text)
{
DcJson *json = dc_json_new_object();
(void)dc_json_object_set(json, "type", dc_json_new_string("file-error"));
(void)dc_json_object_set(json, "id", dc_json_new_string(id));
(void)dc_json_object_set(json, "text", dc_json_new_string(text));
send_json(json);
}
static void
file_reject_event(const char *id, const char *reason)
{
DcJson *json = dc_json_new_object();
(void)dc_json_object_set(json, "type",
dc_json_new_string("file-reject"));
(void)dc_json_object_set(json, "id", dc_json_new_string(id));
(void)dc_json_object_set(json, "reason",
dc_json_new_string(reason != NULL ? reason : ""));
send_json(json);
}
static OutFile *
out_by_id(const char *id)
{
int i;
for (i = 0; i < MAX_FILES; i++)
if (outfiles[i].used && strcmp(outfiles[i].id, id) == 0)
return &outfiles[i];
return NULL;
}
static OutFile *
out_by_iqid(const char *id)
{
int i;
if (id == NULL)
return NULL;
for (i = 0; i < MAX_FILES; i++)
if (outfiles[i].used && strcmp(outfiles[i].iqid, id) == 0)
return &outfiles[i];
return NULL;
}
static InFile *
in_by_sid(const char *sid)
{
int i;
if (sid == NULL)
return NULL;
for (i = 0; i < MAX_FILES; i++)
if (infiles[i].used && strcmp(infiles[i].sid, sid) == 0)
return &infiles[i];
return NULL;
}
static void
send_iq_result(const char *to, const char *id)
{
xmpp_stanza_t *iq = xmpp_iq_new(ctx, "result", id);
if (iq == NULL)
return;
if (to != NULL)
xmpp_stanza_set_attribute(iq, "to", to);
xmpp_send(conn, iq);
xmpp_stanza_release(iq);
}
static void
send_iq_error(const char *to, const char *id, const char *condition)
{
xmpp_stanza_t *iq = xmpp_iq_new(ctx, "error", id);
xmpp_stanza_t *error;
xmpp_stanza_t *child;
if (iq == NULL)
return;
if (to != NULL)
xmpp_stanza_set_attribute(iq, "to", to);
error = xmpp_stanza_new(ctx);
if (error != NULL) {
xmpp_stanza_set_name(error, "error");
xmpp_stanza_set_attribute(error, "type", "cancel");
child = xmpp_stanza_new(ctx);
if (child != NULL) {
xmpp_stanza_set_name(child, condition);
xmpp_stanza_set_ns(child,
"urn:ietf:params:xml:ns:xmpp-stanzas");
xmpp_stanza_add_child(error, child);
xmpp_stanza_release(child);
}
xmpp_stanza_add_child(iq, error);
xmpp_stanza_release(error);
}
xmpp_send(conn, iq);
xmpp_stanza_release(iq);
}
static void out_send_next(OutFile *o);
static void out_send_close(OutFile *o);
static void
out_send_close(OutFile *o)
{
xmpp_stanza_t *iq;
xmpp_stanza_t *close;
snprintf(o->iqid, sizeof(o->iqid), "ibb%u", ++file_iq_seq);
iq = xmpp_iq_new(ctx, "set", o->iqid);
if (iq == NULL)
return;
xmpp_stanza_set_attribute(iq, "to", o->conv);
close = xmpp_stanza_new(ctx);
if (close != NULL) {
xmpp_stanza_set_name(close, "close");
xmpp_stanza_set_ns(close, IBB_NS);
xmpp_stanza_set_attribute(close, "sid", o->id);
xmpp_stanza_add_child(iq, close);
xmpp_stanza_release(close);
}
xmpp_send(conn, iq);
xmpp_stanza_release(iq);
o->state = 3;
o->last = time(NULL);
}
static void
out_send_next(OutFile *o)
{
unsigned char data[IBB_BLOCK];
char b64[4 * ((IBB_BLOCK + 2) / 3) + 1];
xmpp_stanza_t *iq;
xmpp_stanza_t *datastanza;
xmpp_stanza_t *child;
size_t n;
char seq[16];
n = fread(data, 1, sizeof(data), o->fp);
if (n == 0) {
file_error(o->id, "cannot read the file");
reset_out(o);
return;
}
EVP_EncodeBlock((unsigned char *)b64, data, (int)n);
snprintf(o->iqid, sizeof(o->iqid), "ibb%u", ++file_iq_seq);
iq = xmpp_iq_new(ctx, "set", o->iqid);
if (iq == NULL)
return;
xmpp_stanza_set_attribute(iq, "to", o->conv);
datastanza = xmpp_stanza_new(ctx);
if (datastanza == NULL) {
xmpp_stanza_release(iq);
return;
}
xmpp_stanza_set_name(datastanza, "data");
xmpp_stanza_set_ns(datastanza, IBB_NS);
xmpp_stanza_set_attribute(datastanza, "sid", o->id);
snprintf(seq, sizeof(seq), "%d", o->seq);
xmpp_stanza_set_attribute(datastanza, "seq", seq);
child = xmpp_stanza_new(ctx);
if (child != NULL) {
/* the tag and its text are separate stanzas in libstrophe */
xmpp_stanza_t *text = xmpp_stanza_new(ctx);
xmpp_stanza_set_name(child, "base64");
if (text != NULL) {
xmpp_stanza_set_text(text, b64);
xmpp_stanza_add_child(child, text);
xmpp_stanza_release(text);
}
xmpp_stanza_add_child(datastanza, child);
xmpp_stanza_release(child);
}
xmpp_stanza_add_child(iq, datastanza);
xmpp_stanza_release(datastanza);
xmpp_send(conn, iq);
xmpp_stanza_release(iq);
o->next_len = n;
o->state = 2;
o->last = time(NULL);
}
static void
out_open(OutFile *o)
{
xmpp_stanza_t *iq;
xmpp_stanza_t *open;
xmpp_stanza_t *file;
char block[16];
char size[32];
snprintf(o->iqid, sizeof(o->iqid), "ibb%u", ++file_iq_seq);
iq = xmpp_iq_new(ctx, "set", o->iqid);
if (iq == NULL)
return;
xmpp_stanza_set_attribute(iq, "to", o->conv);
open = xmpp_stanza_new(ctx);
if (open == NULL) {
xmpp_stanza_release(iq);
return;
}
xmpp_stanza_set_name(open, "open");
xmpp_stanza_set_ns(open, IBB_NS);
xmpp_stanza_set_attribute(open, "sid", o->id);
snprintf(block, sizeof(block), "%d", IBB_BLOCK);
xmpp_stanza_set_attribute(open, "block-size", block);
file = xmpp_stanza_new(ctx);
if (file != NULL) {
snprintf(size, sizeof(size), "%llu", o->size);
xmpp_stanza_set_name(file, "file");
xmpp_stanza_set_ns(file, FILE_NS);
xmpp_stanza_set_attribute(file, "name", o->name);
xmpp_stanza_set_attribute(file, "size", size);
xmpp_stanza_set_attribute(file, "sha256", o->sha256);
xmpp_stanza_add_child(open, file);
xmpp_stanza_release(file);
}
xmpp_stanza_add_child(iq, open);
xmpp_stanza_release(open);
xmpp_send(conn, iq);
xmpp_stanza_release(iq);
o->state = 1;
o->last = time(NULL);
}
static void
file_send_start(const DcJson *cmd)
{
const char *id = dc_json_string(dc_json_get(cmd, "id"));
const char *conv = dc_json_string(dc_json_get(cmd, "conv"));
const char *path = dc_json_string(dc_json_get(cmd, "path"));
const char *name = dc_json_string(dc_json_get(cmd, "name"));
OutFile *o = NULL;
long long hashed_size = 0;
long size;
int i;
if (id == NULL || conv == NULL || path == NULL) {
file_error(id != NULL ? id : "?", "file-send needs conv and path");
return;
}
if (out_by_id(id) != NULL) {
file_error(id, "a transfer with this id is running");
return;
}
for (i = 0; i < MAX_FILES; i++)
if (!outfiles[i].used) {
o = &outfiles[i];
break;
}
if (o == NULL) {
file_error(id, "too many transfers");
return;
}
memset(o, 0, sizeof(*o));
if (dc_file_sha256(path, o->sha256, &hashed_size) != 0) {
file_error(id, "cannot read the file");
return;
}
if (hashed_size > IBB_FILE_MAX) {
file_error(id, "the file is larger than 32 MiB");
return;
}
o->fp = fopen(path, "rb");
if (o->fp == NULL) {
file_error(id, "cannot open the file");
return;
}
size = (long)hashed_size;
o->used = 1;
snprintf(o->id, sizeof(o->id), "%s", id);
snprintf(o->conv, sizeof(o->conv), "%s", conv);
snprintf(o->name, sizeof(o->name), "%s", name != NULL ? name :
(strrchr(path, '/') != NULL ? strrchr(path, '/') + 1 : path));
o->size = (unsigned long long)size;
o->sent = 0;
o->seq = 0;
out_open(o);
}
static void
out_iq_result(OutFile *o, int error)
{
if (error) {
file_reject_event(o->id, "the peer rejected the transfer");
reset_out(o);
return;
}
if (o->state == 1) {
out_send_next(o);
} else if (o->state == 2) {
o->sent += o->next_len;
o->seq++;
if (o->sent < o->size) {
out_send_next(o);
} else {
DcJson *json = dc_json_new_object();
out_send_close(o);
(void)dc_json_object_set(json, "type",
dc_json_new_string("file-sent"));
(void)dc_json_object_set(json, "id",
dc_json_new_string(o->id));
(void)dc_json_object_set(json, "size",
dc_json_new_int((long long)o->size));
(void)dc_json_object_set(json, "sha256",
dc_json_new_string(o->sha256));
send_json(json);
/* the close result only acknowledges; the transfer
* itself is done */
reset_out(o);
}
} else {
reset_out(o);
}
}
static void
in_offer(xmpp_stanza_t *stanza, xmpp_stanza_t *open)
{
const char *sid = xmpp_stanza_get_attribute(open, "sid");
const char *from = xmpp_stanza_get_attribute(stanza, "from");
const char *iqid = xmpp_stanza_get_attribute(stanza, "id");
xmpp_stanza_t *file;
const char *name;
const char *size;
const char *sha256;
InFile *in = NULL;
int i;
if (sid == NULL || from == NULL || iqid == NULL)
return;
if (strlen(sid) >= sizeof(infiles[0].sid)) {
send_iq_error(from, iqid, "bad-request");
return;
}
for (i = 0; i < MAX_FILES; i++)
if (!infiles[i].used) {
in = &infiles[i];
break;
}
if (in == NULL) {
send_iq_error(from, iqid, "resource-constraint");
return;
}
memset(in, 0, sizeof(*in));
in->used = 1;
in->state = 1;
snprintf(in->sid, sizeof(in->sid), "%s", sid);
snprintf(in->from, sizeof(in->from), "%s", from);
snprintf(in->iqid, sizeof(in->iqid), "%s", iqid);
file = xmpp_stanza_get_child_by_name_and_ns(open, "file", FILE_NS);
name = file != NULL ? xmpp_stanza_get_attribute(file, "name") : NULL;
size = file != NULL ? xmpp_stanza_get_attribute(file, "size") : NULL;
sha256 = file != NULL ? xmpp_stanza_get_attribute(file, "sha256") :
NULL;
snprintf(in->name, sizeof(in->name), "%s",
name != NULL ? name : sid);
in->size = size != NULL ? strtoull(size, NULL, 10) : 0;
snprintf(in->sha256, sizeof(in->sha256), "%s",
sha256 != NULL ? sha256 : "");
if (in->size > IBB_FILE_MAX) {
send_iq_error(from, iqid, "resource-constraint");
file_reject_event(sid, "the file is larger than 32 MiB");
reset_in(in);
return;
}
in->last = time(NULL);
{
char conv[256];
DcJson *json = dc_json_new_object();
bare_jid(from, conv, sizeof(conv));
(void)dc_json_object_set(json, "type",
dc_json_new_string("file-offer"));
(void)dc_json_object_set(json, "conv",
dc_json_new_string(conv));
(void)dc_json_object_set(json, "id",
dc_json_new_string(sid));
(void)dc_json_object_set(json, "name",
dc_json_new_string(in->name));
(void)dc_json_object_set(json, "size",
dc_json_new_int((long long)in->size));
if (in->sha256[0] != '\0')
(void)dc_json_object_set(json, "sha256",
dc_json_new_string(in->sha256));
send_json(json);
}
}
static void
in_data(xmpp_stanza_t *stanza, xmpp_stanza_t *data)
{
const char *sid = xmpp_stanza_get_attribute(data, "sid");
const char *from = xmpp_stanza_get_attribute(stanza, "from");
const char *iqid = xmpp_stanza_get_attribute(stanza, "id");
xmpp_stanza_t *child;
char *text;
size_t len;
unsigned long long n;
InFile *in;
(void)from;
in = in_by_sid(sid);
if (in == NULL || in->state != 2) {
send_iq_error(from, iqid, "item-not-found");
return;
}
child = xmpp_stanza_get_child_by_name(data, "base64");
if (child == NULL) {
send_iq_error(from, iqid, "bad-request");
return;
}
text = xmpp_stanza_get_text(child);
if (text == NULL) {
send_iq_error(from, iqid, "bad-request");
return;
}
len = strlen(text);
n = (unsigned long long)(len / 4) * 3;
if (len > 0 && text[len - 1] == '=')
n--;
if (len > 1 && text[len - 2] == '=')
n--;
if (in->got + n > IBB_FILE_MAX) {
send_iq_error(from, iqid, "resource-constraint");
file_error(in->sid, "the transfer is larger than 32 MiB");
reset_in(in);
xmpp_free(ctx, text);
return;
}
in->got += n;
in->last = time(NULL);
send_iq_result(from, iqid);
{
DcJson *json = dc_json_new_object();
(void)dc_json_object_set(json, "type",
dc_json_new_string("file-data"));
(void)dc_json_object_set(json, "id",
dc_json_new_string(in->sid));
(void)dc_json_object_set(json, "data",
dc_json_new_string(text));
(void)dc_json_object_set(json, "size",
dc_json_new_int((long long)n));
send_json(json);
}
xmpp_free(ctx, text);
}
static void
in_close(xmpp_stanza_t *stanza, xmpp_stanza_t *close)
{
const char *sid = xmpp_stanza_get_attribute(close, "sid");
const char *from = xmpp_stanza_get_attribute(stanza, "from");
const char *iqid = xmpp_stanza_get_attribute(stanza, "id");
InFile *in = in_by_sid(sid);
(void)from;
if (in != NULL) {
DcJson *json = dc_json_new_object();
send_iq_result(from, iqid);
(void)dc_json_object_set(json, "type",
dc_json_new_string("file-end"));
(void)dc_json_object_set(json, "id",
dc_json_new_string(in->sid));
(void)dc_json_object_set(json, "size",
dc_json_new_int((long long)in->got));
send_json(json);
reset_in(in);
} else {
send_iq_error(from, iqid, "item-not-found");
}
}
static void
file_accept_cmd(const char *sid)
{
InFile *in = in_by_sid(sid);
if (in == NULL || in->state != 1)
return;
send_iq_result(in->from, in->iqid);
in->state = 2;
in->last = time(NULL);
}
static void
file_reject_cmd(const char *sid, const char *reason)
{
InFile *in = in_by_sid(sid);
if (in == NULL)
return;
send_iq_error(in->from, in->iqid, "not-acceptable");
file_reject_event(sid, reason);
reset_in(in);
}
static void
file_cancel_cmd(const char *id)
{
OutFile *o = out_by_id(id);
InFile *in = in_by_sid(id);
if (o != NULL) {
if (o->state != 3)
out_send_close(o);
file_reject_event(id, "cancelled");
reset_out(o);
}
if (in != NULL) {
send_iq_error(in->from, in->iqid, "not-acceptable");
file_reject_event(id, "cancelled");
reset_in(in);
}
}
/* an unanswered offer or a stalled transfer ends after FILE_TIMEOUT */
static void
file_tick(void)
{
time_t now = time(NULL);
int i;
for (i = 0; i < MAX_FILES; i++) {
OutFile *o = &outfiles[i];
InFile *in = &infiles[i];
if (o->used && now - o->last > FILE_TIMEOUT) {
file_error(o->id, "the transfer timed out");
reset_out(o);
}
if (in->used && now - in->last > FILE_TIMEOUT) {
if (in->state == 1)
send_iq_error(in->from, in->iqid,
"not-acceptable");
else
send_iq_error(in->from, in->iqid,
"resource-constraint");
file_reject_event(in->sid, "the transfer timed out");
reset_in(in);
}
}
}
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;
{
const char *iqtype = xmpp_stanza_get_attribute(stanza, "type");
const char *iqid = xmpp_stanza_get_attribute(stanza, "id");
xmpp_stanza_t *child;
if (iqtype != NULL && iqid != NULL &&
(strcmp(iqtype, "result") == 0 ||
strcmp(iqtype, "error") == 0)) {
OutFile *o = out_by_iqid(iqid);
if (o != NULL) {
out_iq_result(o,
strcmp(iqtype, "error") == 0);
return 1;
}
}
if (iqtype != NULL && strcmp(iqtype, "set") == 0) {
child = xmpp_stanza_get_child_by_name_and_ns(stanza,
"open", IBB_NS);
if (child != NULL) {
in_offer(stanza, child);
return 1;
}
child = xmpp_stanza_get_child_by_name_and_ns(stanza,
"data", IBB_NS);
if (child != NULL) {
in_data(stanza, child);
return 1;
}
child = xmpp_stanza_get_child_by_name_and_ns(stanza,
"close", IBB_NS);
if (child != NULL) {
in_close(stanza, child);
return 1;
}
}
}
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);
/*
* An unexpected end of the stream is not a goodbye: exit non-zero
* without a bye, and the core (DcAdapter) restarts the process with
* its backoff and sends hello/configure/connect again. A requested
* disconnect leaves through handle_command with quitting set.
*/
_exit(1);
}
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, "file-send") == 0) {
if (conn != NULL && online)
file_send_start(cmd);
} else if (strcmp(type, "file-accept") == 0) {
const char *id = dc_json_string(dc_json_get(cmd, "id"));
if (conn != NULL && online && id != NULL)
file_accept_cmd(id);
} else if (strcmp(type, "file-reject") == 0) {
const char *id = dc_json_string(dc_json_get(cmd, "id"));
const char *reason = dc_json_string(dc_json_get(cmd,
"reason"));
if (conn != NULL && online && id != NULL)
file_reject_cmd(id, reason);
} else if (strcmp(type, "file-cancel") == 0) {
const char *id = dc_json_string(dc_json_get(cmd, "id"));
if (conn != NULL && online && id != NULL)
file_cancel_cmd(id);
} 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 (conn != NULL && online)
file_tick();
}
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:
if (conn != NULL) {
xmpp_disconnect(conn);
xmpp_conn_release(conn);
}
if (ctx != NULL)
xmpp_ctx_free(ctx);
xmpp_shutdown();
dc_frame_free(&frame);
return 0;
}