demo_adapter.c is a real child process speaking protocol v1 (hello with a nick schema, connect opens lobby, send echoes as 'echo', /code asks for a code, answer reports it, disconnect says bye). demo.c owns the DcLoop, pumps it from a 40 ms Xt timeout, applies state/conversation/message/request/error/bye to the model and the UI, and routes sends through dtchat_ui_set_send_hook. --demo uses an empty model plus the adapter; --adapter overrides the path; --demo --smoke-test checks online, selection, a real send round trip and the echoed text. The transcript now word-wraps: XmText's XmNscrollHorizontal is a secondary resource and must be set in the creation arg list, and the text is fitted to the viewport on ConfigureNotify (the horizontal bar is unmanaged). Standalone plain and demo smoke PASS; /code answered with 4242 shows '-demo- answered: 4242'; screenshot build/mcde-shots/dtchat-t26.png. make mcde-host blocked again: build/.build.lock is held by another agent's distfiles fetch (pid 815951).
239 lines
6.5 KiB
C
239 lines
6.5 KiB
C
/*
|
|
* demo_adapter.c -- dtchat's echo adapter for --demo
|
|
*
|
|
* License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree.
|
|
*
|
|
* Speaks the v1 JSON-lines protocol on stdin/stdout and never touches the
|
|
* network. connect opens "lobby" and greets; every send is echoed back as a
|
|
* message from "echo"; "/code" opens a code request so the request dialogs can
|
|
* be tried by hand.
|
|
*/
|
|
#include "dc_internal.h"
|
|
|
|
#include <errno.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <time.h>
|
|
#include <unistd.h>
|
|
|
|
static char nick[64] = "you";
|
|
static int request_id;
|
|
|
|
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_message(const char *conv, const char *from, const char *kind,
|
|
const char *text)
|
|
{
|
|
DcJson *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(from));
|
|
(void)dc_json_object_set(json, "kind", dc_json_new_string(kind));
|
|
(void)dc_json_object_set(json, "text", dc_json_new_string(text));
|
|
(void)dc_json_object_set(json, "ts",
|
|
dc_json_new_int((long long)time(NULL)));
|
|
send_json(json);
|
|
}
|
|
|
|
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));
|
|
send_json(json);
|
|
}
|
|
|
|
static void
|
|
send_state(const char *state)
|
|
{
|
|
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));
|
|
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();
|
|
DcJson *nickfield = dc_json_new_object();
|
|
|
|
(void)dc_json_array_append(caps, dc_json_new_string("text"));
|
|
(void)dc_json_array_append(caps, dc_json_new_string("channels"));
|
|
(void)dc_json_array_append(caps, dc_json_new_string("request"));
|
|
(void)dc_json_object_set(nickfield, "key", dc_json_new_string("nick"));
|
|
(void)dc_json_object_set(nickfield, "label",
|
|
dc_json_new_string("Nickname"));
|
|
(void)dc_json_object_set(nickfield, "type", dc_json_new_string("text"));
|
|
(void)dc_json_object_set(nickfield, "default",
|
|
dc_json_new_string("you"));
|
|
(void)dc_json_array_append(settings, nickfield);
|
|
(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("demo"));
|
|
(void)dc_json_object_set(hello, "version",
|
|
dc_json_new_string("1.0"));
|
|
(void)dc_json_object_set(hello, "caps", caps);
|
|
(void)dc_json_object_set(hello, "settings", settings);
|
|
send_json(hello);
|
|
}
|
|
|
|
static void
|
|
handle(DcJson *cmd)
|
|
{
|
|
const char *type = dc_json_string(dc_json_get(cmd, "type"));
|
|
const char *conv;
|
|
const char *text;
|
|
const char *client_id;
|
|
|
|
if (type == NULL)
|
|
return;
|
|
if (strcmp(type, "configure") == 0) {
|
|
const char *n = dc_json_string(dc_json_get(
|
|
dc_json_get(cmd, "settings"), "nick"));
|
|
|
|
if (n != NULL && *n != '\0')
|
|
snprintf(nick, sizeof(nick), "%s", n);
|
|
return;
|
|
}
|
|
if (strcmp(type, "connect") == 0) {
|
|
send_state("online");
|
|
send_conversation("lobby", "channel", "Lobby");
|
|
send_message("lobby", "demo", "text",
|
|
"Welcome to the dtchat demo. Everything you type is echoed. "
|
|
"Try /code for a request dialog.");
|
|
return;
|
|
}
|
|
if (strcmp(type, "join") == 0) {
|
|
conv = dc_json_string(dc_json_get(cmd, "conv"));
|
|
if (conv != NULL)
|
|
send_conversation(conv, "channel", conv);
|
|
return;
|
|
}
|
|
if (strcmp(type, "send") == 0) {
|
|
DcJson *sent = dc_json_new_object();
|
|
char line[1024];
|
|
|
|
conv = dc_json_string(dc_json_get(cmd, "conv"));
|
|
text = dc_json_string(dc_json_get(cmd, "text"));
|
|
client_id = dc_json_string(dc_json_get(cmd, "client_id"));
|
|
if (conv == NULL)
|
|
conv = "lobby";
|
|
if (text == NULL)
|
|
text = "";
|
|
(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));
|
|
(void)dc_json_object_set(sent, "ts",
|
|
dc_json_new_int((long long)time(NULL)));
|
|
send_json(sent);
|
|
if (strcmp(text, "/code") == 0) {
|
|
DcJson *request = dc_json_new_object();
|
|
|
|
request_id++;
|
|
(void)dc_json_object_set(request, "type",
|
|
dc_json_new_string("request"));
|
|
(void)dc_json_object_set(request, "id",
|
|
dc_json_new_int(request_id));
|
|
(void)dc_json_object_set(request, "kind",
|
|
dc_json_new_string("code"));
|
|
(void)dc_json_object_set(request, "title",
|
|
dc_json_new_string("Demo"));
|
|
(void)dc_json_object_set(request, "prompt",
|
|
dc_json_new_string("Any code will do"));
|
|
send_json(request);
|
|
return;
|
|
}
|
|
snprintf(line, sizeof(line), "%s", text);
|
|
send_message(conv, "echo", "text", line);
|
|
return;
|
|
}
|
|
if (strcmp(type, "answer") == 0) {
|
|
const char *value = dc_json_string(dc_json_get(cmd, "value"));
|
|
char line[256];
|
|
|
|
snprintf(line, sizeof(line), "answered: %s",
|
|
value != NULL ? value : "(cancelled)");
|
|
send_message("lobby", "demo", "notice", line);
|
|
return;
|
|
}
|
|
if (strcmp(type, "disconnect") == 0) {
|
|
DcJson *bye = dc_json_new_object();
|
|
|
|
(void)dc_json_object_set(bye, "type", dc_json_new_string("bye"));
|
|
(void)dc_json_object_set(bye, "reason",
|
|
dc_json_new_string("requested"));
|
|
send_json(bye);
|
|
_exit(0);
|
|
}
|
|
}
|
|
|
|
int
|
|
main(void)
|
|
{
|
|
DcFrameBuf frame;
|
|
char buf[4096];
|
|
|
|
dc_frame_init(&frame);
|
|
send_hello();
|
|
for (;;) {
|
|
ssize_t n = read(STDIN_FILENO, buf, sizeof(buf));
|
|
|
|
if (n < 0) {
|
|
if (errno == EINTR)
|
|
continue;
|
|
break;
|
|
}
|
|
if (n == 0)
|
|
break;
|
|
if (dc_frame_feed(&frame, buf, (size_t)n) != 0)
|
|
break;
|
|
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(cmd);
|
|
dc_json_free(cmd);
|
|
}
|
|
}
|
|
}
|
|
dc_frame_free(&frame);
|
|
return 0;
|
|
}
|