From 9e8c7cd00c4918333ca9946dad57ab0efc4bebe5 Mon Sep 17 00:00:00 2001 From: lotherk Date: Tue, 17 Nov 2026 12:06:04 +0000 Subject: [PATCH] dtchat: add the Join Channel dialog and the IRC join/part commands (T3.3/T4.4) Conversation > Join Channel opens a Motif prompt (channel on IRC or room on XMPP); the OK callback calls the new join hook with the current account. accounts_run sets the hook, sends dc_adapter_join, adds the conversation (channel for IRC, group otherwise) and shows it; the adapter's conversation event updates it later. The IRC adapter now handles the join and part commands (JOIN , PART :reason) instead of ignoring them; XMPP already implemented join. Tests: tests/irc_join.py checks JOIN #new on the wire, the self-JOIN conversation event, PART #new and a clean bye (10 checks, standalone built adapter green); the UI path was verified with the fake adapter under Xvfb (DTCHAT_TEST_JOIN=#new -> 'accounts fake: join #new' and conversation #new) and the dialog screenshot build/mcde-shots/dtchat-join-dialog.png. Harness entry added (51). In-tree build follows. --- programs/dtchat/accounts_run.c | 23 +++++ programs/dtchat/irc_adapter.c | 25 ++++++ programs/dtchat/main.c | 20 ++++- programs/dtchat/ui_main.c | 104 +++++++++++++++++++++- programs/dtchat/ui_main.h | 6 ++ tests/irc_join.py | 154 +++++++++++++++++++++++++++++++++ 6 files changed, 327 insertions(+), 5 deletions(-) create mode 100755 tests/irc_join.py diff --git a/programs/dtchat/accounts_run.c b/programs/dtchat/accounts_run.c index 43f62a11..869884e1 100644 --- a/programs/dtchat/accounts_run.c +++ b/programs/dtchat/accounts_run.c @@ -691,6 +691,27 @@ accounts_file_send(const char *peer_id, const char *path, void *arg) } } +static void +accounts_join_hook(void *arg, const char *account, const char *conv) +{ + DtAccounts *accounts = arg; + AccountRun *run = run_find(accounts, account); + + if (run == NULL || run->adapter == NULL || conv == NULL) + return; + if (dc_adapter_join(run->adapter, conv) != 0) { + set_account_status(accounts, run, "cannot join"); + return; + } + (void)dc_model_add_conv(accounts->model, run->id, conv, + strcmp(run->protocol, "irc") == 0 ? DC_CONV_CHANNEL : + DC_CONV_GROUP, conv); + dtchat_ui_refresh(accounts->ui); + set_account_status(accounts, run, "joining"); + if (debug_on()) + fprintf(stderr, "accounts %s: join %s\n", run->id, conv); +} + static void accounts_typing_hook(void *arg, const char *conv, int on) { @@ -924,6 +945,7 @@ dtchat_accounts_start(Widget shell, DtChatUi *ui, DcModel *model, dtchat_ui_set_send_hook(ui, accounts_send_hook, accounts); dtchat_ui_set_file_send(ui, accounts_file_send, accounts); dtchat_ui_set_typing_hook(ui, accounts_typing_hook, accounts); + dtchat_ui_set_join_hook(ui, accounts_join_hook, accounts); schedule_poll(accounts); return accounts; } @@ -942,6 +964,7 @@ dtchat_accounts_stop(DtAccounts *accounts) dtchat_ui_set_send_hook(accounts->ui, NULL, NULL); dtchat_ui_set_file_send(accounts->ui, NULL, NULL); dtchat_ui_set_typing_hook(accounts->ui, NULL, NULL); + dtchat_ui_set_join_hook(accounts->ui, NULL, NULL); while (accounts->incoming != NULL) incoming_free(accounts, accounts->incoming, 1); for (i = 0; i < accounts->count; i++) { diff --git a/programs/dtchat/irc_adapter.c b/programs/dtchat/irc_adapter.c index 1f7f6b44..ea0c265b 100644 --- a/programs/dtchat/irc_adapter.c +++ b/programs/dtchat/irc_adapter.c @@ -1326,6 +1326,31 @@ read_stdin(DcFrameBuf *frame, int *sockp) reconnect_at = 0; start_connection(sockp); } + } else if (strcmp(type, "join") == 0) { + const char *conv = dc_json_string( + dc_json_get(cmd, "conv")); + + if (*sockp >= 0 && conv != NULL && *conv != '\0') { + char line[300]; + + snprintf(line, sizeof(line), "JOIN %s\r\n", + conv); + queue_line(line); + } + } else if (strcmp(type, "part") == 0) { + const char *conv = dc_json_string( + dc_json_get(cmd, "conv")); + const char *reason = dc_json_string( + dc_json_get(cmd, "reason")); + + if (*sockp >= 0 && conv != NULL && *conv != '\0') { + char line[400]; + + snprintf(line, sizeof(line), + "PART %s :%s\r\n", conv, + reason != NULL ? reason : "bye"); + queue_line(line); + } } else if (strcmp(type, "disconnect") == 0) { DcJson *bye = dc_json_new_object(); diff --git a/programs/dtchat/main.c b/programs/dtchat/main.c index eecd62fd..2d5a330d 100644 --- a/programs/dtchat/main.c +++ b/programs/dtchat/main.c @@ -496,14 +496,25 @@ smoke_ui_checks(DtChatUi *ui) static void ui_send_test_cb(XtPointer client, XtIntervalId *id) { + DtChatUi *ui = (DtChatUi *)client; const char *text = getenv("DTCHAT_TEST_UI_SEND"); const char *file = getenv("DTCHAT_TEST_UI_SEND_FILE"); + const char *join = getenv("DTCHAT_TEST_JOIN"); (void)id; if (text != NULL) - (void)dtchat_ui_send_text((DtChatUi *)client, text); + (void)dtchat_ui_send_text(ui, text); if (file != NULL) - (void)dtchat_ui_send_file((DtChatUi *)client, file); + (void)dtchat_ui_send_file(ui, file); + if (join != NULL) + (void)dtchat_ui_join(ui, join); +} + +static void +ui_join_dialog_cb(XtPointer client, XtIntervalId *id) +{ + (void)id; + dtchat_ui_join_dialog((DtChatUi *)client); } static void @@ -967,8 +978,11 @@ main(int argc, char **argv) XtAppAddTimeOut(app, 5000, smoke_check, ui); } if (getenv("DTCHAT_TEST_UI_SEND") != NULL || - getenv("DTCHAT_TEST_UI_SEND_FILE") != NULL) + getenv("DTCHAT_TEST_UI_SEND_FILE") != NULL || + getenv("DTCHAT_TEST_JOIN") != NULL) XtAppAddTimeOut(app, 2500, ui_send_test_cb, ui); + if (getenv("DTCHAT_TEST_JOIN_DIALOG") != NULL) + XtAppAddTimeOut(app, 1500, ui_join_dialog_cb, ui); if (demo_request != NULL) { XMapRaised(XtDisplay(toplevel), XtWindow(toplevel)); XtAppAddTimeOut(app, 700, demo_request_cb, (XtPointer)ui); diff --git a/programs/dtchat/ui_main.c b/programs/dtchat/ui_main.c index 3842dfcd..8be3a477 100644 --- a/programs/dtchat/ui_main.c +++ b/programs/dtchat/ui_main.c @@ -33,6 +33,7 @@ #include #include #include +#include #include #include @@ -67,6 +68,8 @@ struct DtChatUi { void *send_arg; DtChatTypingFn typing_fn; void *typing_arg; + DtChatJoinFn join_fn; + void *join_arg; XtIntervalId typing_timer; int typing_on; int suppress_typing; @@ -695,6 +698,103 @@ call_cb(Widget w, XtPointer client, XtPointer call) ui->call_fn(ui->cur_peer, ui->call_arg); } +static void +join_dialog_ok(Widget widget, XtPointer client, XtPointer call) +{ + DtChatUi *ui = client; + XmSelectionBoxCallbackStruct *cbs = call; + char *value = NULL; + + (void)widget; + if (ui->join_fn == NULL) { + set_status(ui, "joining is only available for accounts"); + return; + } + if (cbs != NULL && cbs->value != NULL) + (void)XmStringGetLtoR(cbs->value, XmFONTLIST_DEFAULT_TAG, + &value); + if (value != NULL && *value != '\0' && + ui->cur_account != NULL) + ui->join_fn(ui->join_arg, ui->cur_account, value); + else + set_status(ui, "name a channel or room"); + if (value != NULL) + XtFree(value); +} + +static void +join_dialog_cancel(Widget widget, XtPointer client, XtPointer call) +{ + (void)client; + (void)call; + XtUnmanageChild(widget); +} + +static void +join_dialog_cb(Widget widget, XtPointer client, XtPointer call) +{ + DtChatUi *ui = client; + Widget dialog; + XmString title; + XmString label; + + (void)widget; + (void)call; + if (ui->cur_account == NULL) { + set_status(ui, dc_text(TX_SELECT_PEER, + "select an account first")); + return; + } + if (ui->join_fn == NULL) { + set_status(ui, "joining is only available for accounts"); + return; + } + dialog = XmCreatePromptDialog(ui->shell, "joinChannel", NULL, 0); + if (dialog == NULL) + return; + title = xs("Join Channel"); + label = xs("Channel on IRC or room on XMPP:"); + XtVaSetValues(dialog, + XmNdialogTitle, title, + XmNselectionLabelString, label, + XmNautoUnmanage, False, + XmNnoResize, True, NULL); + XmStringFree(title); + XmStringFree(label); + XtAddCallback(dialog, XmNokCallback, join_dialog_ok, ui); + XtAddCallback(dialog, XmNcancelCallback, join_dialog_cancel, NULL); + XtUnmanageChild(XmSelectionBoxGetChild(dialog, + XmDIALOG_HELP_BUTTON)); + XtManageChild(dialog); + XtPopup(XtParent(dialog), XtGrabNone); +} + +void +dtchat_ui_join_dialog(DtChatUi *ui) +{ + if (ui != NULL) + join_dialog_cb(NULL, ui, NULL); +} + +int +dtchat_ui_join(DtChatUi *ui, const char *conv) +{ + if (ui == NULL || conv == NULL || ui->join_fn == NULL || + ui->cur_account == NULL) + return -1; + ui->join_fn(ui->join_arg, ui->cur_account, conv); + return 0; +} + +void +dtchat_ui_set_join_hook(DtChatUi *ui, DtChatJoinFn fn, void *arg) +{ + if (ui == NULL) + return; + ui->join_fn = fn; + ui->join_arg = arg; +} + static void make_menu_bar(DtChatUi *ui, Widget form, Widget *bar_out) { @@ -731,8 +831,8 @@ make_menu_bar(DtChatUi *ui, Widget form, Widget *bar_out) not_impl(ui, "Timestamps")); conversation = make_menu(bar, dc_text(TX_CONVERSATION, "Conversation")); - (void)make_item(conversation, dc_text(TX_JOIN, "Join Channel"), not_implemented_cb, - not_impl(ui, "Join a channel")); + (void)make_item(conversation, dc_text(TX_JOIN, "Join Channel"), + join_dialog_cb, ui); (void)make_item(conversation, dc_text(TX_SEND_FILE, "Send File"), send_file_cb, ui); (void)make_item(conversation, dc_text(TX_CALL, "Call"), call_cb, ui); diff --git a/programs/dtchat/ui_main.h b/programs/dtchat/ui_main.h index 5612e5ae..3638f4c1 100644 --- a/programs/dtchat/ui_main.h +++ b/programs/dtchat/ui_main.h @@ -16,6 +16,8 @@ typedef struct DtChatUi DtChatUi; typedef int (*DtChatSendFn)(void *arg, const char *conv, const char *text); /* typing state of the person at the input (XEP-0085 / IRC typing) */ typedef void (*DtChatTypingFn)(void *arg, const char *conv, int on); +/* join a channel or MUC room on an account */ +typedef void (*DtChatJoinFn)(void *arg, const char *account, const char *conv); /* MODEL is borrowed; the caller keeps it alive until dtchat_ui_destroy(). */ DtChatUi *dtchat_ui_create(Widget shell, DcModel *model); @@ -64,6 +66,10 @@ int dtchat_ui_smoke(DtChatUi *ui); /* Route sends through FN (the demo/adapter path); NULL restores the local echo. */ void dtchat_ui_set_send_hook(DtChatUi *ui, DtChatSendFn fn, void *arg); void dtchat_ui_set_typing_hook(DtChatUi *ui, DtChatTypingFn fn, void *arg); +void dtchat_ui_set_join_hook(DtChatUi *ui, DtChatJoinFn fn, void *arg); +/* ask for a channel/room and join it (the dialog), or join CONV directly */ +void dtchat_ui_join_dialog(DtChatUi *ui); +int dtchat_ui_join(DtChatUi *ui, const char *conv); /* T2.8 input helpers (the key handler uses them too). */ void dtchat_ui_clear_input(DtChatUi *ui); diff --git a/tests/irc_join.py b/tests/irc_join.py new file mode 100755 index 00000000..943083f2 --- /dev/null +++ b/tests/irc_join.py @@ -0,0 +1,154 @@ +#!/usr/bin/env python3 +"""tests/irc_join.py -- the join/part commands (T3.3/T4.4). + +usage: tests/irc_join.py [path-to-dtchat-irc] + +The server answers 001, expects JOIN #new from the adapter, sends the self +JOIN back (the adapter must report the conversation) and expects PART. +""" +import json +import queue +import socket +import subprocess +import sys +import threading +import time + +ADAPTER = sys.argv[1] if len(sys.argv) > 1 else \ + "build/mcde-host/programs/dtchat/dtchat-irc" +failures = [] +joined = [] +parted = [] +done = threading.Event() + + +def check(cond, what): + print(("ok " if cond else "FAIL ") + what, flush=True) + if not cond: + failures.append(what) + + +srv = socket.socket() +srv.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) +srv.bind(("127.0.0.1", 0)) +srv.listen(1) +port = srv.getsockname()[1] + + +def server(): + conn, _ = srv.accept() + conn.settimeout(0.2) + buf = b"" + + def wait_line(pred, timeout=8): + nonlocal buf + deadline = time.time() + timeout + while time.time() < deadline: + while b"\r\n" in buf: + line, buf = buf.split(b"\r\n", 1) + text = line.decode("utf-8", "replace") + if pred(text): + return text + try: + chunk = conn.recv(4096) + except socket.timeout: + continue + if not chunk: + return None + buf += chunk + return None + + line = wait_line(lambda s: "USER " in s) + if line is None: + return + conn.sendall(b":srv 001 tester :Welcome\r\n") + line = wait_line(lambda s: s.startswith("JOIN #new")) + joined.append(line is not None) + conn.sendall(b":tester!t@h JOIN #new\r\n") + line = wait_line(lambda s: s.startswith("PART #new")) + parted.append(line is not None) + done.set() + time.sleep(0.2) + conn.close() + + +threading.Thread(target=server, daemon=True).start() +proc = subprocess.Popen([ADAPTER, "--stdio"], stdin=subprocess.PIPE, + stdout=subprocess.PIPE, stderr=subprocess.PIPE, + text=True) +q = queue.Queue() + + +def reader(): + for line in proc.stdout: + line = line.strip() + if line: + q.put(json.loads(line)) + + +threading.Thread(target=reader, daemon=True).start() + + +def send(o): + proc.stdin.write(json.dumps(o) + "\n") + proc.stdin.flush() + + +def ev(t=5.0): + try: + return q.get(timeout=t) + except queue.Empty: + return None + + +hello = ev() +check(hello is not None and hello.get("type") == "hello", "hello arrives") +send({"type": "configure", "account": "t", + "settings": {"host": "127.0.0.1", "port": port, "tls": False, + "nick": "tester", "channels": ""}, + "secrets": {}}) +send({"type": "connect"}) + +online = False +conversation = None +deadline = time.time() + 8 +while time.time() < deadline and (not online or conversation is None): + e = ev(1.0) + if e is None: + continue + if e.get("type") == "state" and e.get("state") == "online": + online = True + send({"type": "join", "conv": "#new"}) + elif e.get("type") == "conversation": + conversation = e +check(online, "online") +check(joined and joined[0], "the adapter sent JOIN #new") +check(conversation is not None and conversation.get("conv") == "#new", + "conversation event for the channel") +check(conversation is not None and conversation.get("kind") == "channel", + "the channel kind") + +send({"type": "part", "conv": "#new", "reason": "bye"}) +check(done.wait(6), "the server saw the part") +check(parted and parted[0], "the adapter sent PART #new") + +send({"type": "disconnect"}) +bye = None +deadline = time.time() + 3 +while time.time() < deadline: + e = ev(1.0) + if e and e.get("type") == "bye": + bye = e + break +check(bye is not None, "bye on disconnect") +try: + check(proc.wait(timeout=3) == 0, "exit 0") +except subprocess.TimeoutExpired: + proc.kill() + check(False, "exit 0") +check(proc.stderr.read().strip() == "", "no stderr output") + +if failures: + print("test_irc_join: FAIL (%d)" % len(failures)) + sys.exit(1) +print("test_irc_join: all ok")