dtchat: add the IRC adapter skeleton (T3.1)

dtchat-irc speaks protocol v1 on stdio: hello with the six-field schema (host, port, tls, nick, password, channels), configure, connect, non-blocking getaddrinfo/connect, NICK/USER, PING/PONG, state connecting/online/offline and PRIVMSG on send. TLS, CAP, SASL, channels and the full parser are T3.2-T3.6; tls:true gets a clear non-fatal error instead of a silent plain connection. Verified against a local fake IRC server: 13/13 checks (schema, connecting, online after prefixed 001, NICK/USER, PONG, PRIVMSG, bye, exit 0, empty stderr). Built as noinst dtchat-irc; make mcde-host still waits for the lock.
This commit is contained in:
lotherk committed 2026-11-16 19:09:25 +00:00
1 parent 1519328081
commit ee43f07e4c
2 files changed
+499 -1

No files matched your search

+6 -1
View File
@@ -1,7 +1,7 @@
MAINTAINERCLEANFILES = Makefile.in
bin_PROGRAMS = dtchat
noinst_PROGRAMS = dtchat-demo
noinst_PROGRAMS = dtchat-demo dtchat-irc
dtchat_CPPFLAGS = -I$(top_srcdir)/include \
-I$(top_srcdir)/lib/libDtChat -I$(top_builddir)/lib/libDtChat \
@@ -19,6 +19,11 @@ dtchat_demo_CPPFLAGS = -I$(top_srcdir)/include \
-I$(top_srcdir)/lib/libDtChat -I$(top_builddir)/lib/libDtChat
dtchat_demo_LDADD = $(top_builddir)/lib/libDtChat/libDtChatIpc.la
dtchat_irc_SOURCES = irc_adapter.c
dtchat_irc_CPPFLAGS = -I$(top_srcdir)/include \
-I$(top_srcdir)/lib/libDtChat -I$(top_builddir)/lib/libDtChat
dtchat_irc_LDADD = $(top_builddir)/lib/libDtChat/libDtChatIpc.la
appdefaultsdir = $(CDE_INSTALLATION_TOP)/app-defaults/C
dist_appdefaults_DATA = Dtchat
+493
View File
@@ -0,0 +1,493 @@
/*
* irc_adapter.c -- dtchat's IRC adapter, protocol v1 on stdio
*
* License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree.
*
* T3.1 is the skeleton: hello with the setting schema, configure, connect,
* a non-blocking TCP connection, the minimal RFC 1459 handshake (NICK/USER),
* PING/PONG and the online state. TLS, CAP, SASL, the full parser, channels
* and messages arrive with T3.2-T3.6; until then a "tls":true account gets a
* clear, non-fatal error instead of a silent plain connection.
*/
#include "dc_internal.h"
#include <errno.h>
#include <fcntl.h>
#include <netdb.h>
#include <poll.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <time.h>
#include <unistd.h>
static char host[256] = "irc.libera.chat";
static int port = 6697;
static int use_tls;
static char nick[64] = "";
static char password[256] = "";
static char channels[512] = "";
static int configured;
static int connect_pending;
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_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", "channels", "typing" };
size_t i;
for (i = 0; i < sizeof(capnames) / sizeof(capnames[0]); i++)
(void)dc_json_array_append(caps,
dc_json_new_string(capnames[i]));
{
DcJson *f = dc_json_new_object();
(void)dc_json_object_set(f, "key", dc_json_new_string("host"));
(void)dc_json_object_set(f, "label",
dc_json_new_string("Server"));
(void)dc_json_object_set(f, "type",
dc_json_new_string("text"));
(void)dc_json_object_set(f, "required",
dc_json_new_bool(1));
(void)dc_json_object_set(f, "default",
dc_json_new_string("irc.libera.chat"));
(void)dc_json_array_append(settings, f);
}
{
DcJson *f = dc_json_new_object();
(void)dc_json_object_set(f, "key", dc_json_new_string("port"));
(void)dc_json_object_set(f, "label",
dc_json_new_string("Port"));
(void)dc_json_object_set(f, "type", dc_json_new_string("int"));
(void)dc_json_object_set(f, "default", dc_json_new_int(6697));
(void)dc_json_array_append(settings, f);
}
{
DcJson *f = dc_json_new_object();
(void)dc_json_object_set(f, "key", dc_json_new_string("tls"));
(void)dc_json_object_set(f, "label", dc_json_new_string("TLS"));
(void)dc_json_object_set(f, "type", dc_json_new_string("bool"));
(void)dc_json_object_set(f, "default", dc_json_new_bool(1));
(void)dc_json_array_append(settings, f);
}
{
DcJson *f = dc_json_new_object();
(void)dc_json_object_set(f, "key", dc_json_new_string("nick"));
(void)dc_json_object_set(f, "label",
dc_json_new_string("Nickname"));
(void)dc_json_object_set(f, "type",
dc_json_new_string("text"));
(void)dc_json_object_set(f, "required",
dc_json_new_bool(1));
(void)dc_json_array_append(settings, f);
}
{
DcJson *f = dc_json_new_object();
(void)dc_json_object_set(f, "key",
dc_json_new_string("password"));
(void)dc_json_object_set(f, "label",
dc_json_new_string("Password"));
(void)dc_json_object_set(f, "type",
dc_json_new_string("secret"));
(void)dc_json_array_append(settings, f);
}
{
DcJson *f = dc_json_new_object();
(void)dc_json_object_set(f, "key",
dc_json_new_string("channels"));
(void)dc_json_object_set(f, "label",
dc_json_new_string("Channels"));
(void)dc_json_object_set(f, "type",
dc_json_new_string("text"));
(void)dc_json_object_set(f, "help",
dc_json_new_string("comma separated"));
(void)dc_json_array_append(settings, f);
}
(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("irc"));
(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);
}
static int
set_nonblock(int fd)
{
int fl = fcntl(fd, F_GETFL, 0);
if (fl < 0)
return -1;
return fcntl(fd, F_SETFL, fl | O_NONBLOCK);
}
/* Open the TCP connection; returns the fd or -1. */
static int
open_socket(void)
{
struct addrinfo hints;
struct addrinfo *res = NULL;
struct addrinfo *ai;
char service[16];
int fd = -1;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
snprintf(service, sizeof(service), "%d", port);
if (getaddrinfo(host, service, &hints, &res) != 0) {
send_error("network", "cannot resolve the IRC server", 0);
send_state("offline", NULL);
return -1;
}
for (ai = res; ai != NULL; ai = ai->ai_next) {
fd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
if (fd < 0)
continue;
(void)set_nonblock(fd);
if (connect(fd, ai->ai_addr, ai->ai_addrlen) == 0) {
connect_pending = 0;
break;
}
if (errno == EINPROGRESS) {
connect_pending = 1;
break;
}
close(fd);
fd = -1;
}
freeaddrinfo(res);
if (fd < 0) {
send_error("network", "cannot connect to the IRC server", 0);
send_state("offline", NULL);
}
return fd;
}
static int
send_all(int fd, const char *text)
{
size_t left = strlen(text);
while (left > 0) {
ssize_t n = write(fd, text, left);
if (n < 0) {
if (errno == EINTR)
continue;
if (errno == EAGAIN) {
struct pollfd p;
p.fd = fd;
p.events = POLLOUT;
p.revents = 0;
if (poll(&p, 1, 2000) <= 0)
return -1;
continue;
}
return -1;
}
text += n;
left -= (size_t)n;
}
return 0;
}
static void
handshake(int fd)
{
char line[512];
if (password[0] != '\0') {
snprintf(line, sizeof(line), "PASS %s\r\n", password);
(void)send_all(fd, line);
}
snprintf(line, sizeof(line), "NICK %s\r\n", nick);
(void)send_all(fd, line);
snprintf(line, sizeof(line), "USER %s 0 * :%s\r\n", nick, nick);
(void)send_all(fd, line);
}
static void
handle_line(int fd, const char *line)
{
const char *body = line;
if (*body == ':') {
const char *space = strchr(body, ' ');
if (space != NULL)
body = space + 1;
}
if (strncmp(body, "PING ", 5) == 0) {
char reply[512];
snprintf(reply, sizeof(reply), "PONG %s\r\n", body + 5);
(void)send_all(fd, reply);
return;
}
if (strncmp(body, "001 ", 4) == 0) {
send_state("online", host);
return;
}
if (strncmp(body, "ERROR ", 6) == 0) {
send_error("protocol", body + 6, 0);
send_state("offline", NULL);
return;
}
/* CAP, JOIN, PRIVMSG and the rest arrive with T3.2-T3.5. */
}
static void
read_stdin(DcFrameBuf *frame, int *sockp)
{
char buf[4096];
ssize_t n = read(STDIN_FILENO, buf, sizeof(buf));
if (n <= 0)
return;
if (dc_frame_feed(frame, buf, (size_t)n) != 0)
return;
for (;;) {
size_t len = 0;
char *line = dc_frame_next(frame, &len);
DcJson *cmd;
const char *type;
if (line == NULL)
break;
cmd = dc_json_parse(line, len, NULL, 0);
free(line);
if (cmd == NULL) {
send_error("protocol", "invalid JSON command", 0);
continue;
}
type = dc_json_string(dc_json_get(cmd, "type"));
if (type == NULL) {
dc_json_free(cmd);
continue;
}
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, "host"));
if (v != NULL && *v != '\0')
snprintf(host, sizeof(host), "%s", v);
v = dc_json_string(dc_json_get(settings, "nick"));
if (v != NULL)
snprintf(nick, sizeof(nick), "%s", v);
v = dc_json_string(dc_json_get(settings, "channels"));
if (v != NULL)
snprintf(channels, sizeof(channels), "%s", v);
if (dc_json_get(settings, "port") != NULL)
port = (int)dc_json_int(dc_json_get(settings,
"port"));
use_tls = dc_json_bool(dc_json_get(settings, "tls"));
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) {
int fd;
if (!configured || nick[0] == '\0') {
send_error("config", "nickname is required", 0);
send_state("offline", NULL);
} else if (use_tls) {
send_error("tls",
"TLS arrives with T3.3; turn TLS off for now",
0);
send_state("offline", NULL);
} else {
send_state("connecting", host);
fd = open_socket();
if (fd >= 0) {
*sockp = fd;
if (!connect_pending)
handshake(fd);
}
}
} else 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);
} else if (strcmp(type, "send") == 0) {
if (*sockp >= 0) {
const char *conv = dc_json_string(
dc_json_get(cmd, "conv"));
const char *text = dc_json_string(
dc_json_get(cmd, "text"));
char line[1024];
if (conv != NULL && text != NULL) {
snprintf(line, sizeof(line),
"PRIVMSG %s :%s\r\n", conv, text);
(void)send_all(*sockp, line);
}
}
}
dc_json_free(cmd);
}
}
int
main(int argc, char **argv)
{
DcFrameBuf frame;
char inbuf[8192];
size_t inlen = 0;
int sock = -1;
(void)argc;
(void)argv;
signal(SIGPIPE, SIG_IGN);
dc_frame_init(&frame);
send_hello();
for (;;) {
struct pollfd fds[2];
int nfds = 0;
int ret;
fds[nfds].fd = STDIN_FILENO;
fds[nfds].events = POLLIN;
fds[nfds].revents = 0;
nfds++;
if (sock >= 0) {
fds[nfds].fd = sock;
fds[nfds].events = POLLIN |
(connect_pending ? POLLOUT : 0);
fds[nfds].revents = 0;
nfds++;
}
ret = poll(fds, (nfds_t)nfds, -1);
if (ret < 0) {
if (errno == EINTR)
continue;
break;
}
if (fds[0].revents & (POLLIN | POLLHUP))
read_stdin(&frame, &sock);
if (nfds > 1 && fds[1].revents != 0) {
if (connect_pending && (fds[1].revents & POLLOUT)) {
int err = 0;
socklen_t len = sizeof(err);
(void)getsockopt(sock, SOL_SOCKET, SO_ERROR,
&err, &len);
connect_pending = 0;
if (err != 0) {
send_error("network",
"cannot connect to the IRC server",
0);
send_state("offline", NULL);
close(sock);
sock = -1;
} else {
handshake(sock);
}
} else if (fds[1].revents & (POLLIN | POLLHUP |
POLLERR)) {
char buf[4096];
ssize_t n = read(sock, buf, sizeof(buf));
if (n <= 0) {
send_state("offline", NULL);
close(sock);
sock = -1;
inlen = 0;
} else {
char *start;
char *nl;
size_t used;
if (inlen + (size_t)n >=
sizeof(inbuf)) {
inlen = 0;
}
memcpy(inbuf + inlen, buf, (size_t)n);
inlen += (size_t)n;
inbuf[inlen] = '\0';
start = inbuf;
while ((nl = strstr(start,
"\r\n")) != NULL) {
*nl = '\0';
handle_line(sock, start);
start = nl + 2;
}
used = (size_t)(start - inbuf);
memmove(inbuf, start, inlen - used);
inlen -= used;
inbuf[inlen] = '\0';
}
}
}
}
if (sock >= 0)
close(sock);
dc_frame_free(&frame);
return 0;
}