dtchat: run the configured accounts as adapters in the UI
accounts_run.c loads accounts.json (id, protocol, label, adapter, settings, enabled) and secrets.json (per account id) and starts one DcAdapter per enabled account, pumping the DcLoop from an Xt timeout. Its events go into the model and the panel: state into the status line, conversation (direct/channel/group) and roster entries into the model (roster sets presence), messages into the transcript via dtchat_ui_message_added, presence/typing/errors into the status/dialog, request events into the existing request dialog with the answer routed to that adapter. The UI send hook resolves the selected account (dtchat_ui_current_account) or the account that owns the conversation and calls dc_adapter_send_text; dtchat_accounts_join/_typing expose join and XEP-0085 for menus and tests. Normal GUI runs (not --demo/--local/--perf/--smoke) start with an empty model and the configured accounts; the demo peers are gone in that mode. dtchat-irc is installed next to dtchat and listed in the PLIST; dtchat-xmpp and the demo adapter remain build artifacts. Verified with the fake adapter under Xvfb (accounts fake: starting/online/sent alice/message alice), including the real UI send path (DTCHAT_TEST_UI_SEND), screenshot build/mcde-shots/dtchat-accounts.png; --smoke-test still passes.
This commit is contained in:
1 parent
f0e8c668d6
commit
2f3511ebf2
8 files changed
+680
-7
No files matched your search
@@ -2551,6 +2551,7 @@ ${PLIST.appbuilder}dt/bin/dtbuilder
|
||||
dt/bin/dtcalc
|
||||
dt/bin/dtchat
|
||||
dt/bin/dtchat-headless
|
||||
dt/bin/dtchat-irc
|
||||
dt/bin/dtchooser
|
||||
dt/bin/dtcm
|
||||
dt/bin/dtcm_delete
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
MAINTAINERCLEANFILES = Makefile.in
|
||||
|
||||
bin_PROGRAMS = dtchat dtchat-headless
|
||||
noinst_PROGRAMS = dtchat-demo dtchat-irc
|
||||
bin_PROGRAMS = dtchat dtchat-headless dtchat-irc
|
||||
noinst_PROGRAMS = dtchat-demo
|
||||
|
||||
man_MANS = dtchat.1
|
||||
|
||||
@@ -16,8 +16,9 @@ dtchat_LDADD = $(top_builddir)/lib/libDtChat/libDtChat.la \
|
||||
$(top_builddir)/lib/McdeSys/libMcdeSys.la $(XTOOLLIB)
|
||||
|
||||
dtchat_SOURCES = main.c ui_main.c ui_accounts.c ui_request.c ui_peer.c \
|
||||
ui_file.c localui.c ui_call.c demo.c ui_main.h ui_accounts.h \
|
||||
ui_request.h ui_peer.h ui_file.h localui.h ui_call.h demo.h
|
||||
ui_file.c localui.c ui_call.c demo.c accounts_run.c ui_main.h \
|
||||
ui_accounts.h ui_request.h ui_peer.h ui_file.h localui.h ui_call.h \
|
||||
demo.h accounts_run.h
|
||||
|
||||
|
||||
dtchat_demo_SOURCES = demo_adapter.c
|
||||
|
||||
@@ -0,0 +1,583 @@
|
||||
/*
|
||||
* accounts_run.c -- one adapter process per configured account
|
||||
*
|
||||
* License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree.
|
||||
*
|
||||
* accounts.json names the accounts:
|
||||
*
|
||||
* {"accounts":[{"id":"libera","protocol":"irc","label":"Libera",
|
||||
* "adapter":"/usr/mdt/bin/dtchat-irc","enabled":true,
|
||||
* "settings":{"host":"irc.libera.chat","port":6697}}]}
|
||||
*
|
||||
* secrets.json keeps the secrets apart, under the account's id:
|
||||
*
|
||||
* {"accounts":{"libera":{"password":"..."}}}
|
||||
*
|
||||
* Every enabled account gets a DcAdapter (a child process speaking the v1
|
||||
* protocol); its state, conversations, messages, roster, presence, typing,
|
||||
* requests and errors become model and panel entries. The DcLoop is pumped
|
||||
* from an Xt timeout every 40 ms, as in the demo.
|
||||
*/
|
||||
#define _POSIX_C_SOURCE 200809L
|
||||
|
||||
#include "accounts_run.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
|
||||
#include "ui_request.h"
|
||||
|
||||
#define MAX_ACCOUNTS 16
|
||||
|
||||
typedef struct AccountRun AccountRun;
|
||||
struct AccountRun {
|
||||
DtAccounts *accounts;
|
||||
char id[64];
|
||||
char protocol[32];
|
||||
char label[128];
|
||||
char path[512];
|
||||
DcAdapter *adapter;
|
||||
int online;
|
||||
};
|
||||
|
||||
struct DtAccounts {
|
||||
Widget shell;
|
||||
DtChatUi *ui;
|
||||
DcModel *model;
|
||||
DcStore *store;
|
||||
DcLoop *loop;
|
||||
XtIntervalId timer;
|
||||
AccountRun run[MAX_ACCOUNTS];
|
||||
size_t count;
|
||||
int client_seq;
|
||||
int test_sent;
|
||||
};
|
||||
|
||||
static void poll_cb(XtPointer client, XtIntervalId *id);
|
||||
|
||||
static int
|
||||
debug_on(void)
|
||||
{
|
||||
return getenv("DTCHAT_ACCOUNTS_DEBUG") != NULL;
|
||||
}
|
||||
|
||||
static void
|
||||
schedule_poll(DtAccounts *accounts)
|
||||
{
|
||||
accounts->timer = XtAppAddTimeOut(
|
||||
XtWidgetToApplicationContext(accounts->shell), 40, poll_cb,
|
||||
accounts);
|
||||
}
|
||||
|
||||
static DcConvKind
|
||||
conv_kind(const char *kind)
|
||||
{
|
||||
if (kind == NULL)
|
||||
return DC_CONV_DIRECT;
|
||||
if (strcmp(kind, "channel") == 0)
|
||||
return DC_CONV_CHANNEL;
|
||||
if (strcmp(kind, "group") == 0 || strcmp(kind, "muc") == 0)
|
||||
return DC_CONV_GROUP;
|
||||
if (strcmp(kind, "nearby") == 0)
|
||||
return DC_CONV_NEARBY;
|
||||
return DC_CONV_DIRECT;
|
||||
}
|
||||
|
||||
static AccountRun *
|
||||
run_find(DtAccounts *accounts, const char *id)
|
||||
{
|
||||
size_t i;
|
||||
|
||||
if (id == NULL)
|
||||
return NULL;
|
||||
for (i = 0; i < accounts->count; i++)
|
||||
if (strcmp(accounts->run[i].id, id) == 0)
|
||||
return &accounts->run[i];
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* the account the UI is showing; falls back to the one that owns CONV */
|
||||
static AccountRun *
|
||||
run_current(DtAccounts *accounts, const char *conv)
|
||||
{
|
||||
const char *id = dtchat_ui_current_account(accounts->ui);
|
||||
AccountRun *run = run_find(accounts, id);
|
||||
size_t i;
|
||||
|
||||
if (run != NULL)
|
||||
return run;
|
||||
if (conv != NULL)
|
||||
for (i = 0; i < accounts->count; i++)
|
||||
if (dc_model_find_conv(accounts->model,
|
||||
accounts->run[i].id, conv) != NULL)
|
||||
return &accounts->run[i];
|
||||
return accounts->count > 0 ? &accounts->run[0] : NULL;
|
||||
}
|
||||
|
||||
static void
|
||||
set_account_status(DtAccounts *accounts, AccountRun *run, const char *state)
|
||||
{
|
||||
char line[256];
|
||||
|
||||
snprintf(line, sizeof(line), "%s: %s", run->label[0] != '\0' ?
|
||||
run->label : run->id, state != NULL ? state : "");
|
||||
dtchat_ui_set_status(accounts->ui, line);
|
||||
if (debug_on())
|
||||
fprintf(stderr, "accounts %s: %s\n", run->id,
|
||||
state != NULL ? state : "");
|
||||
}
|
||||
|
||||
/* ------------------------------- events --------------------------------- */
|
||||
|
||||
typedef struct {
|
||||
AccountRun *run;
|
||||
int id;
|
||||
} RequestCtx;
|
||||
|
||||
static void
|
||||
request_answer(const char *value, void *arg)
|
||||
{
|
||||
RequestCtx *ctx = arg;
|
||||
|
||||
if (ctx->run->adapter != NULL)
|
||||
(void)dc_adapter_answer(ctx->run->adapter, ctx->id, value);
|
||||
free(ctx);
|
||||
}
|
||||
|
||||
static void
|
||||
handle_message(DtAccounts *accounts, AccountRun *run, const DcJson *event)
|
||||
{
|
||||
const char *conv = dc_json_string(dc_json_get(event, "conv"));
|
||||
const char *from = dc_json_string(dc_json_get(event, "from"));
|
||||
const char *kind = dc_json_string(dc_json_get(event, "kind"));
|
||||
const char *text = dc_json_string(dc_json_get(event, "text"));
|
||||
int self = dc_json_bool(dc_json_get(event, "self"));
|
||||
long long ts = dc_json_int(dc_json_get(event, "ts"));
|
||||
DcConv *convp;
|
||||
int flags = 0;
|
||||
|
||||
if (conv == NULL || text == NULL)
|
||||
return;
|
||||
convp = dc_model_find_conv(accounts->model, run->id, conv);
|
||||
if (convp == NULL)
|
||||
convp = dc_model_add_conv(accounts->model, run->id, conv,
|
||||
DC_CONV_DIRECT, conv);
|
||||
if (convp == NULL)
|
||||
return;
|
||||
if (kind != NULL && strcmp(kind, "action") == 0)
|
||||
flags |= DTCHAT_MSG_ACTION;
|
||||
else if (kind != NULL && strcmp(kind, "system") == 0)
|
||||
flags |= DTCHAT_MSG_SYSTEM;
|
||||
else if (kind != NULL && strcmp(kind, "notice") == 0)
|
||||
flags |= DTCHAT_MSG_NOTICE;
|
||||
if (ts <= 0)
|
||||
ts = (long long)time(NULL);
|
||||
(void)dc_conv_add_message(convp, self ? "me" :
|
||||
(from != NULL ? from : "?"), text, ts, flags);
|
||||
dtchat_ui_message_added(accounts->ui, run->id, conv);
|
||||
if (debug_on())
|
||||
fprintf(stderr, "accounts %s: message %s from %s\n", run->id,
|
||||
conv, self ? "me" : (from != NULL ? from : "?"));
|
||||
}
|
||||
|
||||
static void
|
||||
handle_event(DcAdapter *adapter, const DcJson *event, void *arg)
|
||||
{
|
||||
AccountRun *run = arg;
|
||||
DtAccounts *accounts = run->accounts;
|
||||
const char *type = dc_json_string(dc_json_get(event, "type"));
|
||||
|
||||
(void)adapter;
|
||||
if (type == NULL)
|
||||
return;
|
||||
if (strcmp(type, "state") == 0) {
|
||||
const char *state = dc_json_string(dc_json_get(event, "state"));
|
||||
const char *detail = dc_json_string(dc_json_get(event,
|
||||
"detail"));
|
||||
char line[256];
|
||||
|
||||
if (state == NULL)
|
||||
return;
|
||||
run->online = strcmp(state, "online") == 0;
|
||||
snprintf(line, sizeof(line), "%s%s%s", state,
|
||||
detail != NULL && *detail != '\0' ? " - " : "",
|
||||
detail != NULL ? detail : "");
|
||||
set_account_status(accounts, run, line);
|
||||
if (run->online && !accounts->test_sent) {
|
||||
const char *send = getenv("DTCHAT_TEST_ACCOUNTS_SEND");
|
||||
|
||||
if (send != NULL) {
|
||||
char *copy = strdup(send);
|
||||
char *sep;
|
||||
|
||||
if (copy != NULL &&
|
||||
(sep = strchr(copy, '|')) != NULL) {
|
||||
*sep++ = '\0';
|
||||
accounts->test_sent = 1;
|
||||
(void)dtchat_accounts_send(accounts,
|
||||
run->id, copy, sep);
|
||||
}
|
||||
free(copy);
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (strcmp(type, "conversation") == 0) {
|
||||
const char *conv = dc_json_string(dc_json_get(event, "conv"));
|
||||
const char *kind = dc_json_string(dc_json_get(event, "kind"));
|
||||
const char *title = dc_json_string(dc_json_get(event, "title"));
|
||||
|
||||
if (conv == NULL)
|
||||
return;
|
||||
(void)dc_model_add_conv(accounts->model, run->id, conv,
|
||||
conv_kind(kind), title != NULL ? title : conv);
|
||||
dtchat_ui_refresh(accounts->ui);
|
||||
if (debug_on())
|
||||
fprintf(stderr, "accounts %s: conversation %s\n",
|
||||
run->id, conv);
|
||||
return;
|
||||
}
|
||||
if (strcmp(type, "message") == 0) {
|
||||
handle_message(accounts, run, event);
|
||||
return;
|
||||
}
|
||||
if (strcmp(type, "roster") == 0) {
|
||||
const DcJson *contact = dc_json_get(event, "contact");
|
||||
const char *id = dc_json_string(dc_json_get(contact, "id"));
|
||||
const char *name = dc_json_string(dc_json_get(contact, "name"));
|
||||
int online = dc_json_bool(dc_json_get(contact, "online"));
|
||||
|
||||
if (id == NULL)
|
||||
return;
|
||||
{
|
||||
DcConv *convp = dc_model_add_conv(accounts->model,
|
||||
run->id, id, DC_CONV_DIRECT,
|
||||
name != NULL ? name : id);
|
||||
|
||||
if (convp != NULL)
|
||||
dc_conv_set_presence(convp, online ?
|
||||
DC_PRESENCE_ONLINE : DC_PRESENCE_OFFLINE);
|
||||
}
|
||||
dtchat_ui_refresh(accounts->ui);
|
||||
if (debug_on())
|
||||
fprintf(stderr, "accounts %s: roster %s\n", run->id,
|
||||
id);
|
||||
return;
|
||||
}
|
||||
if (strcmp(type, "presence") == 0) {
|
||||
const char *buddy = dc_json_string(dc_json_get(event, "buddy"));
|
||||
const char *show = dc_json_string(dc_json_get(event, "show"));
|
||||
const char *status = dc_json_string(dc_json_get(event,
|
||||
"status"));
|
||||
DcConv *convp;
|
||||
char line[300];
|
||||
|
||||
if (buddy == NULL || show == NULL)
|
||||
return;
|
||||
convp = dc_model_find_conv(accounts->model, run->id, buddy);
|
||||
if (convp != NULL) {
|
||||
if (strcmp(show, "online") == 0)
|
||||
dc_conv_set_presence(convp,
|
||||
DC_PRESENCE_ONLINE);
|
||||
else
|
||||
dc_conv_set_presence(convp,
|
||||
DC_PRESENCE_OFFLINE);
|
||||
}
|
||||
snprintf(line, sizeof(line), "%s is %s%s%s", buddy, show,
|
||||
status != NULL && *status != '\0' ? " - " : "",
|
||||
status != NULL ? status : "");
|
||||
dtchat_ui_set_status(accounts->ui, line);
|
||||
if (debug_on())
|
||||
fprintf(stderr, "accounts %s: presence %s %s\n",
|
||||
run->id, buddy, show);
|
||||
return;
|
||||
}
|
||||
if (strcmp(type, "typing") == 0) {
|
||||
const char *conv = dc_json_string(dc_json_get(event, "conv"));
|
||||
int on = dc_json_bool(dc_json_get(event, "on"));
|
||||
char line[256];
|
||||
|
||||
snprintf(line, sizeof(line), "%s is %s", conv != NULL ? conv :
|
||||
"someone", on ? "typing" : "not typing");
|
||||
dtchat_ui_set_status(accounts->ui, line);
|
||||
return;
|
||||
}
|
||||
if (strcmp(type, "request") == 0) {
|
||||
RequestCtx *ctx = calloc(1, sizeof(*ctx));
|
||||
|
||||
if (ctx == NULL)
|
||||
return;
|
||||
ctx->run = run;
|
||||
ctx->id = (int)dc_json_int(dc_json_get(event, "id"));
|
||||
if (dtchat_request_dialog_create(accounts->shell, event, 0,
|
||||
request_answer, ctx) == NULL) {
|
||||
(void)dc_adapter_answer(run->adapter, ctx->id, NULL);
|
||||
free(ctx);
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (strcmp(type, "error") == 0) {
|
||||
const char *text = dc_json_string(dc_json_get(event, "text"));
|
||||
|
||||
dtchat_error_dialog(accounts->shell, run->label[0] != '\0' ?
|
||||
run->label : run->id, text != NULL ? text :
|
||||
"The adapter reported an error.");
|
||||
set_account_status(accounts, run, "adapter error");
|
||||
return;
|
||||
}
|
||||
if (strcmp(type, "bye") == 0) {
|
||||
run->online = 0;
|
||||
set_account_status(accounts, run, "adapter said goodbye");
|
||||
}
|
||||
}
|
||||
|
||||
/* ------------------------------- sending -------------------------------- */
|
||||
|
||||
static int
|
||||
accounts_send_hook(void *arg, const char *conv, const char *text)
|
||||
{
|
||||
DtAccounts *accounts = arg;
|
||||
AccountRun *run = run_current(accounts, conv);
|
||||
|
||||
if (run == NULL || run->adapter == NULL || conv == NULL ||
|
||||
text == NULL)
|
||||
return -1;
|
||||
return dtchat_accounts_send(accounts, run->id, conv, text);
|
||||
}
|
||||
|
||||
int
|
||||
dtchat_accounts_send(DtAccounts *accounts, const char *account,
|
||||
const char *conv, const char *text)
|
||||
{
|
||||
AccountRun *run;
|
||||
char client_id[32];
|
||||
|
||||
if (accounts == NULL || conv == NULL || text == NULL)
|
||||
return -1;
|
||||
run = run_find(accounts, account);
|
||||
if (run == NULL || run->adapter == NULL)
|
||||
return -1;
|
||||
snprintf(client_id, sizeof(client_id), "c%d", ++accounts->client_seq);
|
||||
if (dc_adapter_send_text(run->adapter, conv, text, client_id) != 0)
|
||||
return -1;
|
||||
if (debug_on())
|
||||
fprintf(stderr, "accounts %s: sent %s\n", run->id, conv);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
dtchat_accounts_join(DtAccounts *accounts, const char *account,
|
||||
const char *conv)
|
||||
{
|
||||
AccountRun *run;
|
||||
|
||||
if (accounts == NULL || conv == NULL)
|
||||
return -1;
|
||||
run = run_find(accounts, account);
|
||||
if (run == NULL || run->adapter == NULL)
|
||||
return -1;
|
||||
return dc_adapter_join(run->adapter, conv);
|
||||
}
|
||||
|
||||
int
|
||||
dtchat_accounts_typing(DtAccounts *accounts, const char *account,
|
||||
const char *conv, int on)
|
||||
{
|
||||
AccountRun *run;
|
||||
|
||||
if (accounts == NULL || conv == NULL)
|
||||
return -1;
|
||||
run = run_find(accounts, account);
|
||||
if (run == NULL || run->adapter == NULL)
|
||||
return -1;
|
||||
return dc_adapter_send_typing(run->adapter, conv, on);
|
||||
}
|
||||
|
||||
size_t
|
||||
dtchat_accounts_count(const DtAccounts *accounts)
|
||||
{
|
||||
return accounts != NULL ? accounts->count : 0;
|
||||
}
|
||||
|
||||
int
|
||||
dtchat_accounts_online(const DtAccounts *accounts)
|
||||
{
|
||||
size_t i;
|
||||
int n = 0;
|
||||
|
||||
if (accounts == NULL)
|
||||
return 0;
|
||||
for (i = 0; i < accounts->count; i++)
|
||||
if (accounts->run[i].online)
|
||||
n++;
|
||||
return n;
|
||||
}
|
||||
|
||||
/* -------------------------------- start --------------------------------- */
|
||||
|
||||
static void
|
||||
poll_cb(XtPointer client, XtIntervalId *id)
|
||||
{
|
||||
DtAccounts *accounts = client;
|
||||
|
||||
(void)id;
|
||||
accounts->timer = 0;
|
||||
if (accounts->loop != NULL)
|
||||
(void)dc_loop_run(accounts->loop, 0);
|
||||
schedule_poll(accounts);
|
||||
}
|
||||
|
||||
static const DcJson *
|
||||
secrets_for(const DcJson *secrets, const char *id)
|
||||
{
|
||||
const DcJson *map = dc_json_get(secrets, "accounts");
|
||||
const DcJson *entry;
|
||||
|
||||
entry = dc_json_get(map, id);
|
||||
if (entry == NULL)
|
||||
entry = dc_json_get(secrets, id);
|
||||
return entry;
|
||||
}
|
||||
|
||||
static void
|
||||
account_path(const char *adapter_dir, const char *adapter,
|
||||
const char *protocol, char *out, size_t outlen)
|
||||
{
|
||||
if (adapter != NULL && *adapter != '\0') {
|
||||
if (adapter[0] == '/')
|
||||
snprintf(out, outlen, "%s", adapter);
|
||||
else
|
||||
snprintf(out, outlen, "%s/%s",
|
||||
adapter_dir != NULL ? adapter_dir : ".", adapter);
|
||||
return;
|
||||
}
|
||||
snprintf(out, outlen, "%s/dtchat-%s",
|
||||
adapter_dir != NULL ? adapter_dir : ".", protocol);
|
||||
}
|
||||
|
||||
DtAccounts *
|
||||
dtchat_accounts_start(Widget shell, DtChatUi *ui, DcModel *model,
|
||||
const char *config_dir, const char *adapter_dir)
|
||||
{
|
||||
DtAccounts *accounts;
|
||||
DcJson *json;
|
||||
DcJson *secrets;
|
||||
DcJson *list;
|
||||
size_t i;
|
||||
|
||||
if (shell == NULL || ui == NULL || model == NULL)
|
||||
return NULL;
|
||||
accounts = calloc(1, sizeof(*accounts));
|
||||
if (accounts == NULL)
|
||||
return NULL;
|
||||
accounts->shell = shell;
|
||||
accounts->ui = ui;
|
||||
accounts->model = model;
|
||||
accounts->store = dc_store_open(config_dir);
|
||||
if (accounts->store == NULL) {
|
||||
free(accounts);
|
||||
return NULL;
|
||||
}
|
||||
accounts->loop = dc_loop_new();
|
||||
if (accounts->loop == NULL) {
|
||||
dc_store_close(accounts->store);
|
||||
free(accounts);
|
||||
return NULL;
|
||||
}
|
||||
json = dc_store_load_accounts(accounts->store);
|
||||
secrets = dc_store_load_secrets(accounts->store);
|
||||
list = dc_json_get(json, "accounts");
|
||||
for (i = 0; list != NULL && i < dc_json_array_size(list) &&
|
||||
accounts->count < MAX_ACCOUNTS; i++) {
|
||||
const DcJson *entry = dc_json_array_get(list, i);
|
||||
const DcJson *settings;
|
||||
DcJson *secret;
|
||||
DcJson *secret_empty = NULL;
|
||||
const DcJson *enabled = dc_json_get(entry, "enabled");
|
||||
const char *id = dc_json_string(dc_json_get(entry, "id"));
|
||||
const char *protocol = dc_json_string(dc_json_get(entry,
|
||||
"protocol"));
|
||||
const char *label = dc_json_string(dc_json_get(entry, "label"));
|
||||
const char *adapter = dc_json_string(dc_json_get(entry,
|
||||
"adapter"));
|
||||
DcJson *empty;
|
||||
AccountRun *run;
|
||||
|
||||
if (id == NULL || *id == '\0')
|
||||
continue;
|
||||
if (enabled != NULL && !dc_json_bool(enabled))
|
||||
continue;
|
||||
run = &accounts->run[accounts->count];
|
||||
memset(run, 0, sizeof(*run));
|
||||
run->accounts = accounts;
|
||||
snprintf(run->id, sizeof(run->id), "%s", id);
|
||||
snprintf(run->protocol, sizeof(run->protocol), "%s",
|
||||
protocol != NULL ? protocol : "unknown");
|
||||
snprintf(run->label, sizeof(run->label), "%s",
|
||||
label != NULL ? label : id);
|
||||
account_path(adapter_dir, adapter, run->protocol, run->path,
|
||||
sizeof(run->path));
|
||||
settings = dc_json_get(entry, "settings");
|
||||
secret = (DcJson *)secrets_for(secrets, id);
|
||||
if (secret == NULL) {
|
||||
secret = secret_empty = dc_json_new_object();
|
||||
}
|
||||
if (settings == NULL) {
|
||||
empty = dc_json_new_object();
|
||||
settings = empty;
|
||||
} else {
|
||||
empty = NULL;
|
||||
}
|
||||
run->adapter = dc_adapter_start(accounts->loop, run->path,
|
||||
run->id, settings, secret, handle_event, run);
|
||||
dc_json_free(empty);
|
||||
dc_json_free(secret_empty);
|
||||
if (run->adapter == NULL) {
|
||||
if (debug_on())
|
||||
fprintf(stderr,
|
||||
"accounts %s: cannot start %s\n", id,
|
||||
run->path);
|
||||
continue;
|
||||
}
|
||||
(void)dc_model_add_account(model, run->id, run->protocol,
|
||||
run->label);
|
||||
accounts->count++;
|
||||
if (debug_on())
|
||||
fprintf(stderr, "accounts %s: started %s\n", run->id,
|
||||
run->path);
|
||||
}
|
||||
dc_json_free(json);
|
||||
dc_json_free(secrets);
|
||||
dtchat_ui_refresh(ui);
|
||||
dtchat_ui_set_send_hook(ui, accounts_send_hook, accounts);
|
||||
schedule_poll(accounts);
|
||||
return accounts;
|
||||
}
|
||||
|
||||
void
|
||||
dtchat_accounts_stop(DtAccounts *accounts)
|
||||
{
|
||||
size_t i;
|
||||
|
||||
if (accounts == NULL)
|
||||
return;
|
||||
if (accounts->timer != 0) {
|
||||
(void)XtRemoveTimeOut(accounts->timer);
|
||||
accounts->timer = 0;
|
||||
}
|
||||
dtchat_ui_set_send_hook(accounts->ui, NULL, NULL);
|
||||
for (i = 0; i < accounts->count; i++) {
|
||||
if (accounts->run[i].adapter != NULL) {
|
||||
dc_adapter_stop(accounts->run[i].adapter);
|
||||
dc_adapter_free(accounts->run[i].adapter);
|
||||
accounts->run[i].adapter = NULL;
|
||||
}
|
||||
}
|
||||
if (accounts->loop != NULL)
|
||||
dc_loop_free(accounts->loop);
|
||||
if (accounts->store != NULL)
|
||||
dc_store_close(accounts->store);
|
||||
free(accounts);
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
/*
|
||||
* accounts_run.h -- the configured accounts, each as its adapter process
|
||||
*
|
||||
* License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree.
|
||||
*/
|
||||
#ifndef _Dtchat_accounts_run_h
|
||||
#define _Dtchat_accounts_run_h
|
||||
|
||||
#include <X11/Intrinsic.h>
|
||||
|
||||
#include "dc_internal.h"
|
||||
#include "ui_main.h"
|
||||
|
||||
typedef struct DtAccounts DtAccounts;
|
||||
|
||||
/*
|
||||
* Start one adapter per enabled account in accounts.json (CONFIG_DIR, or
|
||||
* ~/.dt/dtchat when NULL). An account names its "adapter" (absolute, or
|
||||
* relative to ADAPTER_DIR), "protocol", "label" and "settings"; the secrets
|
||||
* come from secrets.json under the same id. Events are routed into MODEL
|
||||
* and the UI, and the send hook of the UI goes through the adapter of the
|
||||
* selected account.
|
||||
*/
|
||||
DtAccounts *dtchat_accounts_start(Widget shell, DtChatUi *ui, DcModel *model,
|
||||
const char *config_dir, const char *adapter_dir);
|
||||
void dtchat_accounts_stop(DtAccounts *accounts);
|
||||
|
||||
size_t dtchat_accounts_count(const DtAccounts *accounts);
|
||||
int dtchat_accounts_online(const DtAccounts *accounts);
|
||||
|
||||
/* the explicit calls behind the UI hooks, for a caller that knows the
|
||||
* account (a menu, a test) */
|
||||
int dtchat_accounts_send(DtAccounts *accounts, const char *account,
|
||||
const char *conv, const char *text);
|
||||
int dtchat_accounts_join(DtAccounts *accounts, const char *account,
|
||||
const char *conv);
|
||||
int dtchat_accounts_typing(DtAccounts *accounts, const char *account,
|
||||
const char *conv, int on);
|
||||
|
||||
#endif /* _Dtchat_accounts_run_h */
|
||||
@@ -109,6 +109,14 @@ changes the identity; a copy is the backup.
|
||||
.TP
|
||||
.I ~/.dt/dtchat/peers.json
|
||||
The pinned peers and their blocked state.
|
||||
.TP
|
||||
.I ~/.dt/dtchat/accounts.json
|
||||
The accounts: id, protocol, label, adapter path, settings and whether the
|
||||
account is enabled. Every enabled account runs as its adapter process
|
||||
(dtchat-irc is installed next to dtchat).
|
||||
.TP
|
||||
.I ~/.dt/dtchat/secrets.json
|
||||
The passwords, tokens and codes of the accounts, by account id (0600).
|
||||
.SH EXAMPLES
|
||||
Run the demo without a network:
|
||||
.PP
|
||||
|
||||
+34
-3
@@ -26,6 +26,7 @@
|
||||
#include "ui_peer.h"
|
||||
#include "ui_file.h"
|
||||
#include "localui.h"
|
||||
#include "accounts_run.h"
|
||||
#include "ui_call.h"
|
||||
|
||||
static XtAppContext app;
|
||||
@@ -492,6 +493,16 @@ smoke_ui_checks(DtChatUi *ui)
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void
|
||||
ui_send_test_cb(XtPointer client, XtIntervalId *id)
|
||||
{
|
||||
const char *text = getenv("DTCHAT_TEST_UI_SEND");
|
||||
|
||||
(void)id;
|
||||
if (text != NULL)
|
||||
(void)dtchat_ui_send_text((DtChatUi *)client, text);
|
||||
}
|
||||
|
||||
static void
|
||||
smoke_check(XtPointer client, XtIntervalId *id)
|
||||
{
|
||||
@@ -737,7 +748,9 @@ main(int argc, char **argv)
|
||||
int demo_flag = 0;
|
||||
int perf_flag = 0;
|
||||
int smoke = 0;
|
||||
int account_mode;
|
||||
int i;
|
||||
DtAccounts *accounts_rt = NULL;
|
||||
|
||||
/*
|
||||
* Xt does not know --smoke-test; remove it before XtVaAppInitialize
|
||||
@@ -840,10 +853,12 @@ main(int argc, char **argv)
|
||||
return 2;
|
||||
}
|
||||
(void)clock_gettime(CLOCK_MONOTONIC, &perf_start);
|
||||
account_mode = !local_mode && !demo_flag && !perf_flag && !smoke;
|
||||
demo_model = perf_flag ? make_perf_model() :
|
||||
(local_mode || demo_flag) ? dc_model_new() : make_demo_model();
|
||||
demo_peers = (local_mode || perf_flag) ? dc_peers_new() :
|
||||
make_demo_peers();
|
||||
(local_mode || demo_flag || account_mode) ? dc_model_new() :
|
||||
make_demo_model();
|
||||
demo_peers = (local_mode || perf_flag || account_mode) ?
|
||||
dc_peers_new() : make_demo_peers();
|
||||
if (demo_model == NULL || demo_peers == NULL) {
|
||||
fprintf(stderr, "dtchat: out of memory\n");
|
||||
return 1;
|
||||
@@ -906,6 +921,19 @@ main(int argc, char **argv)
|
||||
}
|
||||
}
|
||||
|
||||
if (account_mode) {
|
||||
char bindir[512];
|
||||
const char *slash = strrchr(argv[0], '/');
|
||||
|
||||
if (slash != NULL && slash != argv[0])
|
||||
snprintf(bindir, sizeof(bindir), "%.*s",
|
||||
(int)(slash - argv[0]), argv[0]);
|
||||
else
|
||||
snprintf(bindir, sizeof(bindir), ".");
|
||||
accounts_rt = dtchat_accounts_start(toplevel, ui, demo_model,
|
||||
local_config, bindir);
|
||||
}
|
||||
|
||||
if (demo_flag) {
|
||||
adapter_path = adapter_opt != NULL ? strdup(adapter_opt) :
|
||||
default_adapter_path(argv[0]);
|
||||
@@ -935,6 +963,8 @@ main(int argc, char **argv)
|
||||
XMapRaised(XtDisplay(toplevel), XtWindow(toplevel));
|
||||
XtAppAddTimeOut(app, 5000, smoke_check, ui);
|
||||
}
|
||||
if (getenv("DTCHAT_TEST_UI_SEND") != NULL)
|
||||
XtAppAddTimeOut(app, 2500, ui_send_test_cb, ui);
|
||||
if (demo_request != NULL) {
|
||||
XMapRaised(XtDisplay(toplevel), XtWindow(toplevel));
|
||||
XtAppAddTimeOut(app, 700, demo_request_cb, (XtPointer)ui);
|
||||
@@ -942,6 +972,7 @@ main(int argc, char **argv)
|
||||
XtAppMainLoop(app);
|
||||
dtchat_call_window_destroy(demo_call);
|
||||
dtchat_local_stop(local_mgr);
|
||||
dtchat_accounts_stop(accounts_rt);
|
||||
dtchat_demo_stop(echo);
|
||||
dtchat_ui_destroy(ui);
|
||||
dc_model_free(demo_model);
|
||||
|
||||
@@ -1158,6 +1158,12 @@ dtchat_ui_shell(const DtChatUi *ui)
|
||||
return ui != NULL ? ui->shell : NULL;
|
||||
}
|
||||
|
||||
const char *
|
||||
dtchat_ui_current_account(const DtChatUi *ui)
|
||||
{
|
||||
return ui != NULL ? ui->cur_account : NULL;
|
||||
}
|
||||
|
||||
int
|
||||
dtchat_ui_is_viewable(DtChatUi *ui)
|
||||
{
|
||||
|
||||
@@ -22,6 +22,9 @@ void dtchat_ui_destroy(DtChatUi *ui);
|
||||
/* The toplevel shell, for dialog parents. */
|
||||
Widget dtchat_ui_shell(const DtChatUi *ui);
|
||||
|
||||
/* The account of the conversation in the window, for the send hook. */
|
||||
const char *dtchat_ui_current_account(const DtChatUi *ui);
|
||||
|
||||
/* True when the shell is realized and its map state is IsViewable. */
|
||||
int dtchat_ui_is_viewable(DtChatUi *ui);
|
||||
|
||||
|
||||
Reference in new issue
Block a user