dtchat: IRC messages, splitting and the send bucket (T3.5)

Incoming PRIVMSG/NOTICE become text/notice messages, CTCP ACTION becomes an action message and direct messages open a direct conversation. dc_irc_split_next() splits at UTF-8 boundaries, prefers spaces and always breaks at newlines; the adapter budgets 512 bytes per PRIVMSG line. Sends go through a 128-line queue with a 4-message burst and 2 messages/s, pumped from the poll timeout. test_irc covers the splitter (ASCII, multibyte, over-wide character, newlines, empty); the fake-server test passes 19/19 with three chunks rejoining exactly, every line <= 512 bytes and a 0.50s max gap under flood. make mcde-host still blocked by pid 1207058.
This commit is contained in:
lotherk committed 2026-11-16 19:18:28 +00:00
1 parent 5c7bbfdfc5
commit 8665cab421
4 files changed
+312 -7

No files matched your search

+10
View File
@@ -183,6 +183,16 @@ int dc_irc_parse(const char *line, size_t len, DcIrcMessage *msg, char *err,
size_t errlen);
void dc_irc_message_free(DcIrcMessage *msg);
/*
* Split TEXT at MAX_BYTES without cutting a UTF-8 sequence. *OFFSET starts
* at 0 and is advanced past each chunk; OUT receives the chunk (NUL
* terminated, at most OUTLEN-1 bytes). Spaces and newlines at the break are
* consumed, so joining the chunks reproduces the words. Returns the chunk
* length, or 0 when TEXT is exhausted.
*/
size_t dc_irc_split_next(const char *text, size_t *offset, size_t max_bytes,
char *out, size_t outlen);
/*
* Text helpers for the UI and the adapters. All return malloc'd strings
* (free with free(3)) or NULL. dc_text_clean() removes C0 controls except
+89
View File
@@ -53,6 +53,95 @@ fail(DcIrcMessage *msg, char *err, size_t errlen, const char *text)
return -1;
}
static size_t
utf8_len(unsigned char c)
{
if (c < 0x80)
return 1;
if ((c & 0xe0) == 0xc0)
return 2;
if ((c & 0xf0) == 0xe0)
return 3;
if ((c & 0xf8) == 0xf0)
return 4;
return 1;
}
size_t
dc_irc_split_next(const char *text, size_t *offset, size_t max_bytes,
char *out, size_t outlen)
{
size_t start, end, i, cap, len;
int cut = 0;
int hard_break = 0;
size_t j;
if (text == NULL || offset == NULL || out == NULL || outlen == 0)
return 0;
start = *offset;
while (text[start] == ' ' || text[start] == '\n')
start++;
if (text[start] == '\0') {
*offset = start;
out[0] = '\0';
return 0;
}
cap = max_bytes < outlen - 1 ? max_bytes : outlen - 1;
i = start;
while (text[i] != '\0' && i - start < cap) {
size_t clen = utf8_len((unsigned char)text[i]);
if (i - start + clen > cap)
break;
i += clen;
}
if (i == start) {
/* One character is wider than the budget: take it whole. */
i = start + utf8_len((unsigned char)text[start]);
}
end = i;
for (j = start; j < end; j++) {
if (text[j] == '\n') {
end = j;
hard_break = 1;
break;
}
}
if (hard_break) {
cut = 1;
} else if (text[end] != '\0') {
cut = 1;
for (j = end; j > start; j--) {
if (text[j - 1] == ' ' || text[j - 1] == '\n') {
end = j - 1;
break;
}
}
if (end == start) {
/* no separator: hard cut, keep the UTF-8 boundary */
end = i;
}
}
len = end - start;
/* trim trailing spaces at the chunk end */
while (len > 0 && (text[start + len - 1] == ' ' ||
text[start + len - 1] == '\n'))
len--;
if (len >= outlen)
len = outlen - 1;
memcpy(out, text + start, len);
out[len] = '\0';
if (cut) {
size_t next = end;
if (text[next] == ' ' || text[next] == '\n')
next++;
*offset = next;
} else {
*offset = end;
}
return len;
}
int
dc_irc_parse(const char *line, size_t len, DcIrcMessage *msg, char *err,
size_t errlen)
+59
View File
@@ -192,6 +192,64 @@ test_errors(void)
}
}
static void
test_split(void)
{
char out[256];
char joined[4096];
size_t off, n;
const char *text = "the quick brown fox jumps over the lazy dog";
off = 0;
joined[0] = '\0';
while ((n = dc_irc_split_next(text, &off, 10, out, sizeof(out))) > 0) {
CHECK(n <= 10);
CHECK(dc_text_valid_utf8(out));
if (joined[0] != '\0')
strcat(joined, " ");
strcat(joined, out);
}
CHECK(strcmp(joined, text) == 0);
/* multibyte characters are never cut */
{
int chunks = 0;
off = 0;
while ((n = dc_irc_split_next("\u00e4\u00e4 \u00e4\u00e4 \u00e4\u00e4",
&off, 5, out, sizeof(out))) > 0) {
CHECK(dc_text_valid_utf8(out));
CHECK(n <= 5);
chunks++;
}
CHECK(chunks == 3);
}
/* one character wider than the budget still makes progress */
off = 0;
n = dc_irc_split_next("\u00e4\u00e4", &off, 1, out, sizeof(out));
CHECK(n == 2);
CHECK(strcmp(out, "\u00e4") == 0);
n = dc_irc_split_next("\u00e4\u00e4", &off, 1, out, sizeof(out));
CHECK(n == 2);
n = dc_irc_split_next("\u00e4\u00e4", &off, 1, out, sizeof(out));
CHECK(n == 0);
/* newlines separate messages */
off = 0;
CHECK(dc_irc_split_next("one\ntwo", &off, 100, out, sizeof(out)) == 3);
CHECK(strcmp(out, "one") == 0);
CHECK(dc_irc_split_next("one\ntwo", &off, 100, out, sizeof(out)) == 3);
CHECK(strcmp(out, "two") == 0);
CHECK(dc_irc_split_next("one\ntwo", &off, 100, out, sizeof(out)) == 0);
/* empty and spaces only */
off = 0;
CHECK(dc_irc_split_next(" ", &off, 10, out, sizeof(out)) == 0);
off = 0;
CHECK(dc_irc_split_next("", &off, 10, out, sizeof(out)) == 0);
}
int
main(void)
{
@@ -200,6 +258,7 @@ main(void)
test_numerics();
test_ping_mode_names();
test_errors();
test_split();
if (fails != 0) {
fprintf(stderr, "%d test(s) failed\n", fails);
return 1;
+154 -7
View File
@@ -250,6 +250,65 @@ send_all(int fd, const char *text)
return 0;
}
#define IRC_OUTQ_MAX 128
#define IRC_OUTQ_LINE 600
static char outq[IRC_OUTQ_MAX][IRC_OUTQ_LINE];
static size_t outq_head;
static size_t outq_tail;
static double outq_tokens = 4.0;
static long long outq_last_ms;
static long long
now_ms(void)
{
struct timespec ts;
(void)clock_gettime(CLOCK_MONOTONIC, &ts);
return (long long)ts.tv_sec * 1000 + ts.tv_nsec / 1000000;
}
static void
queue_line(const char *line)
{
size_t next = (outq_tail + 1) % IRC_OUTQ_MAX;
if (next == outq_head)
return; /* queue full: drop */
snprintf(outq[outq_tail], IRC_OUTQ_LINE, "%s", line);
outq_tail = next;
}
/*
* Send queued lines with a 4-message burst and 2 messages/s afterwards.
* Returns the milliseconds until the next token is due, or -1 when idle.
*/
static long long
pump_outq(int fd)
{
long long now;
double need;
if (fd < 0)
return -1;
if (outq_last_ms == 0)
outq_last_ms = now_ms();
now = now_ms();
outq_tokens += (double)(now - outq_last_ms) * 0.002;
if (outq_tokens > 4.0)
outq_tokens = 4.0;
outq_last_ms = now;
while (outq_head != outq_tail && outq_tokens >= 1.0) {
(void)send_all(fd, outq[outq_head]);
outq_head = (outq_head + 1) % IRC_OUTQ_MAX;
outq_tokens -= 1.0;
}
if (outq_head == outq_tail)
return -1;
need = 1.0 - outq_tokens;
return (long long)(need / 0.002) + 1;
}
static void
handshake(int fd)
{
@@ -284,6 +343,23 @@ typedef struct {
static IrcChannel irc_channels[IRC_MAX_CHANNELS];
static size_t irc_nchannels;
static char direct_convs[32][64];
static size_t ndirect;
static int
direct_seen(const char *name)
{
size_t i;
for (i = 0; i < ndirect; i++)
if (strcmp(direct_convs[i], name) == 0)
return 1;
if (ndirect == 32)
return 1;
snprintf(direct_convs[ndirect++], sizeof(direct_convs[0]), "%s", name);
return 0;
}
static IrcChannel *
channel_find(const char *name)
{
@@ -378,14 +454,15 @@ send_kind_message(const char *conv, const char *from, const char *kind,
}
static void
send_conversation(const char *conv, const char *topic)
send_conversation_kind(const char *conv, const char *kind,
const char *topic)
{
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("channel"));
(void)dc_json_object_set(json, "kind", dc_json_new_string(kind));
(void)dc_json_object_set(json, "title", dc_json_new_string(conv));
if (topic != NULL && *topic != '\0')
(void)dc_json_object_set(json, "topic",
@@ -393,6 +470,12 @@ send_conversation(const char *conv, const char *topic)
send_json(json);
}
static void
send_conversation(const char *conv, const char *topic)
{
send_conversation_kind(conv, "channel", topic);
}
/* "nick!user@host" or "nick" -> "nick" in OUT. */
static void
prefix_nick(const char *prefix, char *out, size_t outlen)
@@ -711,6 +794,50 @@ handle_line(int fd, const char *line)
handle_mode(ch, &msg);
}
} else if (strcmp(msg.command, "PRIVMSG") == 0 ||
strcmp(msg.command, "NOTICE") == 0) {
const char *target = msg.nparams > 0 ? msg.params[0] : NULL;
const char *text = msg.trailing != NULL ? msg.trailing : "";
char from[64];
char conv[128];
const char *body = text;
const char *kind = strcmp(msg.command, "NOTICE") == 0 ?
"notice" : "text";
size_t blen = strlen(text);
prefix_nick(msg.prefix, from, sizeof(from));
if (target == NULL || *target == '\0' ||
strcmp(from, "system") == 0) {
dc_irc_message_free(&msg);
return;
}
if (*target == '#') {
snprintf(conv, sizeof(conv), "%s", target);
} else {
snprintf(conv, sizeof(conv), "%s", from);
if (!direct_seen(conv))
send_conversation_kind(conv, "direct", NULL);
}
/* CTCP ACTION */
if (blen >= 9 && strncmp(body, "\001" "ACTION ", 8) == 0 &&
body[blen - 1] == '\001') {
kind = "action";
body += 8;
blen -= 9;
{
char *cut = strndup(body, blen);
if (cut != NULL) {
send_kind_message(conv, from, kind, cut);
free(cut);
}
}
} else if (blen >= 2 && body[0] == '\001' &&
body[blen - 1] == '\001') {
/* other CTCP (VERSION, TIME, ...): ignore in T3.5 */
} else {
send_kind_message(conv, from, kind, body);
}
} else if (strcmp(msg.command, "ERROR") == 0) {
send_error("protocol",
msg.trailing != NULL ? msg.trailing : "server error", 0);
@@ -810,13 +937,28 @@ read_stdin(DcFrameBuf *frame, int *sockp)
dc_json_get(cmd, "conv"));
const char *text = dc_json_string(
dc_json_get(cmd, "text"));
char line[1024];
char chunk[512];
char line[600];
size_t off = 0;
size_t budget;
size_t n;
if (conv != NULL && text != NULL) {
if (conv == NULL)
conv = "";
if (text == NULL)
text = "";
budget = 512 > strlen(conv) + 12 ?
512 - strlen(conv) - 12 : 1;
if (budget > sizeof(chunk) - 1)
budget = sizeof(chunk) - 1;
while ((n = dc_irc_split_next(text, &off,
budget, chunk, sizeof(chunk))) > 0) {
snprintf(line, sizeof(line),
"PRIVMSG %s :%s\r\n", conv, text);
(void)send_all(*sockp, line);
"PRIVMSG %s :%s\r\n", conv,
chunk);
queue_line(line);
}
(void)n;
}
}
dc_json_free(cmd);
@@ -852,7 +994,12 @@ main(int argc, char **argv)
fds[nfds].revents = 0;
nfds++;
}
ret = poll(fds, (nfds_t)nfds, -1);
{
long long wait = pump_outq(sock);
ret = poll(fds, (nfds_t)nfds,
wait < 0 ? -1 : (int)wait);
}
if (ret < 0) {
if (errno == EINTR)
continue;