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.
252 lines
5.5 KiB
C
252 lines
5.5 KiB
C
/*
|
|
* irc.c -- the IRC line parser
|
|
*
|
|
* License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree.
|
|
*
|
|
* RFC 1459/2812 plus the IRCv3 message tags. The parser copies what it
|
|
* keeps, never modifies the input and never trusts a length: a NUL inside
|
|
* LEN, a missing command or a line longer than DC_IRC_MAX_LINE is an error,
|
|
* not a crash.
|
|
*/
|
|
#include "dc_internal.h"
|
|
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
static char *
|
|
copy_range(const char *start, const char *end)
|
|
{
|
|
size_t len = (size_t)(end - start);
|
|
char *out = malloc(len + 1);
|
|
|
|
if (out == NULL)
|
|
return NULL;
|
|
memcpy(out, start, len);
|
|
out[len] = '\0';
|
|
return out;
|
|
}
|
|
|
|
void
|
|
dc_irc_message_free(DcIrcMessage *msg)
|
|
{
|
|
size_t i;
|
|
|
|
if (msg == NULL)
|
|
return;
|
|
free(msg->tags);
|
|
free(msg->prefix);
|
|
free(msg->command);
|
|
for (i = 0; i < msg->nparams; i++)
|
|
free(msg->params[i]);
|
|
free(msg->params);
|
|
free(msg->trailing);
|
|
memset(msg, 0, sizeof(*msg));
|
|
}
|
|
|
|
static int
|
|
fail(DcIrcMessage *msg, char *err, size_t errlen, const char *text)
|
|
{
|
|
if (err != NULL && errlen > 0)
|
|
snprintf(err, errlen, "%s", text);
|
|
dc_irc_message_free(msg);
|
|
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)
|
|
{
|
|
const char *p;
|
|
const char *end;
|
|
char *cmd;
|
|
size_t cap = 0;
|
|
|
|
if (msg == NULL)
|
|
return -1;
|
|
memset(msg, 0, sizeof(*msg));
|
|
if (err != NULL && errlen > 0)
|
|
err[0] = '\0';
|
|
if (line == NULL)
|
|
return fail(msg, err, errlen, "no line");
|
|
if (len > DC_IRC_MAX_LINE)
|
|
return fail(msg, err, errlen, "line too long");
|
|
if (memchr(line, '\0', len) != NULL)
|
|
return fail(msg, err, errlen, "NUL in line");
|
|
p = line;
|
|
end = line + len;
|
|
|
|
/* IRCv3 tags: @key=value;key2 ... */
|
|
if (p < end && *p == '@') {
|
|
const char *space = memchr(p, ' ', (size_t)(end - p));
|
|
|
|
if (space == NULL)
|
|
return fail(msg, err, errlen, "tags without a command");
|
|
msg->tags = copy_range(p + 1, space);
|
|
if (msg->tags == NULL)
|
|
return fail(msg, err, errlen, "out of memory");
|
|
p = space;
|
|
while (p < end && *p == ' ')
|
|
p++;
|
|
}
|
|
/* Prefix: :nick!user@host or :server */
|
|
if (p < end && *p == ':') {
|
|
const char *space = memchr(p, ' ', (size_t)(end - p));
|
|
|
|
if (space == NULL)
|
|
return fail(msg, err, errlen, "prefix without a command");
|
|
msg->prefix = copy_range(p + 1, space);
|
|
if (msg->prefix == NULL)
|
|
return fail(msg, err, errlen, "out of memory");
|
|
p = space;
|
|
}
|
|
while (p < end && *p == ' ')
|
|
p++;
|
|
if (p >= end)
|
|
return fail(msg, err, errlen, "no command");
|
|
{
|
|
const char *space = memchr(p, ' ', (size_t)(end - p));
|
|
const char *cmd_end = space != NULL ? space : end;
|
|
|
|
cmd = copy_range(p, cmd_end);
|
|
if (cmd == NULL)
|
|
return fail(msg, err, errlen, "out of memory");
|
|
msg->command = cmd;
|
|
p = cmd_end;
|
|
}
|
|
|
|
/* Parameters; the last one may be a trailing ":" string. */
|
|
for (;;) {
|
|
while (p < end && *p == ' ')
|
|
p++;
|
|
if (p >= end)
|
|
break;
|
|
if (*p == ':') {
|
|
msg->trailing = copy_range(p + 1, end);
|
|
if (msg->trailing == NULL)
|
|
return fail(msg, err, errlen, "out of memory");
|
|
break;
|
|
}
|
|
{
|
|
const char *space = memchr(p, ' ',
|
|
(size_t)(end - p));
|
|
const char *arg_end = space != NULL ? space : end;
|
|
char **next;
|
|
char *arg;
|
|
|
|
if (msg->nparams == 15)
|
|
return fail(msg, err, errlen,
|
|
"more than 15 parameters");
|
|
arg = copy_range(p, arg_end);
|
|
if (arg == NULL)
|
|
return fail(msg, err, errlen,
|
|
"out of memory");
|
|
if (msg->nparams == cap) {
|
|
size_t ncap = cap != 0 ? cap * 2 : 4;
|
|
|
|
next = realloc(msg->params,
|
|
ncap * sizeof(*next));
|
|
if (next == NULL) {
|
|
free(arg);
|
|
return fail(msg, err, errlen,
|
|
"out of memory");
|
|
}
|
|
msg->params = next;
|
|
cap = ncap;
|
|
}
|
|
msg->params[msg->nparams++] = arg;
|
|
p = arg_end;
|
|
}
|
|
}
|
|
return 0;
|
|
}
|