/* * demo.c -- wire dtchat to the local echo adapter * * License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree. * * The adapter is a real child process speaking the v1 protocol, so --demo * exercises the same path as IRC or XMPP: hello, configure, connect, events, * send, request/answer and the restart policy. The DcLoop is pumped from an * Xt timeout every 40 ms; there is no second thread and no network. */ #define _POSIX_C_SOURCE 200809L #include "demo.h" #include #include #include #include #include "ui_request.h" struct DtDemo { Widget shell; DtChatUi *ui; DcModel *model; DcLoop *loop; DcAdapter *adapter; XtIntervalId timer; int online; int sent; int client_seq; }; static void poll_cb(XtPointer client, XtIntervalId *id); 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) return DC_CONV_GROUP; if (strcmp(kind, "nearby") == 0) return DC_CONV_NEARBY; return DC_CONV_DIRECT; } static void schedule_poll(DtDemo *demo) { demo->timer = XtAppAddTimeOut( XtWidgetToApplicationContext(demo->shell), 40, poll_cb, demo); } static void poll_cb(XtPointer client, XtIntervalId *id) { DtDemo *demo = client; (void)id; demo->timer = 0; if (demo->loop != NULL) (void)dc_loop_run(demo->loop, 0); schedule_poll(demo); } typedef struct { DtDemo *demo; int id; } RequestCtx; static void request_answer(const char *value, void *arg) { RequestCtx *ctx = arg; if (ctx->demo->adapter != NULL) (void)dc_adapter_answer(ctx->demo->adapter, ctx->id, value); free(ctx); } static void handle_event(DcAdapter *adapter, const DcJson *event, void *arg) { DtDemo *demo = arg; const char *type = dc_json_string(dc_json_get(event, "type")); const char *conv; (void)adapter; if (type == NULL) return; if (strcmp(type, "state") == 0) { const char *state = dc_json_string(dc_json_get(event, "state")); if (state != NULL && strcmp(state, "online") == 0) { demo->online = 1; dtchat_ui_set_status(demo->ui, "demo: online"); } return; } if (strcmp(type, "conversation") == 0) { const char *id = 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 (id == NULL) return; (void)dc_model_add_conv(demo->model, "demo", id, conv_kind(kind), title != NULL ? title : id); dtchat_ui_refresh(demo->ui); return; } if (strcmp(type, "message") == 0) { 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")); long long ts = dc_json_int(dc_json_get(event, "ts")); DcConv *convp; int flags = 0; conv = dc_json_string(dc_json_get(event, "conv")); if (conv == NULL || text == NULL) return; convp = dc_model_find_conv(demo->model, "demo", conv); if (convp == NULL) convp = dc_model_add_conv(demo->model, "demo", 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, from != NULL ? from : "?", text, ts, flags); dtchat_ui_message_added(demo->ui, "demo", conv); return; } if (strcmp(type, "request") == 0) { RequestCtx *ctx = calloc(1, sizeof(*ctx)); if (ctx == NULL) return; ctx->demo = demo; ctx->id = (int)dc_json_int(dc_json_get(event, "id")); if (dtchat_request_dialog_create(demo->shell, event, 0, request_answer, ctx) == NULL) { (void)dc_adapter_answer(demo->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(demo->shell, "dtchat demo", text != NULL ? text : "The adapter reported an error."); dtchat_ui_set_status(demo->ui, "demo: adapter error"); return; } if (strcmp(type, "bye") == 0) dtchat_ui_set_status(demo->ui, "demo: adapter said goodbye"); } static int demo_send(void *arg, const char *conv, const char *text) { DtDemo *demo = arg; char client_id[32]; if (demo->adapter == NULL) return -1; snprintf(client_id, sizeof(client_id), "c%d", ++demo->client_seq); if (dc_adapter_send_text(demo->adapter, conv, text, client_id) != 0) return -1; demo->sent++; dtchat_ui_set_status(demo->ui, "demo: sending"); return 0; } DtDemo * dtchat_demo_start(Widget shell, DtChatUi *ui, DcModel *model, const char *adapter_path) { DtDemo *demo; DcJson *settings; DcJson *secrets; DcJson *hello_settings; if (shell == NULL || ui == NULL || model == NULL || adapter_path == NULL) return NULL; demo = calloc(1, sizeof(*demo)); if (demo == NULL) return NULL; demo->shell = shell; demo->ui = ui; demo->model = model; demo->loop = dc_loop_new(); if (demo->loop == NULL) goto fail; (void)dc_model_add_account(model, "demo", "demo", "Demo (echo)"); dtchat_ui_refresh(ui); dtchat_ui_set_send_hook(ui, demo_send, demo); settings = dc_json_new_object(); hello_settings = dc_json_new_object(); secrets = dc_json_new_object(); if (settings == NULL || hello_settings == NULL || secrets == NULL) { dc_json_free(settings); dc_json_free(hello_settings); dc_json_free(secrets); goto fail; } (void)dc_json_object_set(hello_settings, "nick", dc_json_new_string("you")); (void)dc_json_object_set(settings, "nick", dc_json_new_string("you")); demo->adapter = dc_adapter_start(demo->loop, adapter_path, "demo", settings, secrets, handle_event, demo); dc_json_free(settings); dc_json_free(secrets); dc_json_free(hello_settings); if (demo->adapter == NULL) goto fail; schedule_poll(demo); return demo; fail: dtchat_demo_stop(demo); return NULL; } void dtchat_demo_stop(DtDemo *demo) { if (demo == NULL) return; if (demo->timer != 0) { (void)XtRemoveTimeOut(demo->timer); demo->timer = 0; } if (demo->ui != NULL) dtchat_ui_set_send_hook(demo->ui, NULL, NULL); if (demo->adapter != NULL) { dc_adapter_stop(demo->adapter); dc_adapter_free(demo->adapter); demo->adapter = NULL; } if (demo->loop != NULL) { dc_loop_free(demo->loop); demo->loop = NULL; } free(demo); } int dtchat_demo_online(const DtDemo *demo) { return demo != NULL ? demo->online : 0; } int dtchat_demo_sent(const DtDemo *demo) { return demo != NULL ? demo->sent : 0; }