2016-02-10 06:08:58 +00:00
|
|
|
/*
|
|
|
|
* charybdis - an advanced ircd.
|
|
|
|
* Copyright (c) 2016 William Pitcock <nenolod@dereferenced.org>.
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice is present in all copies.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
|
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
|
|
|
|
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
|
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
|
|
|
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
|
|
|
|
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "stdinc.h"
|
|
|
|
#include "ircd_defs.h"
|
|
|
|
#include "msgbuf.h"
|
2016-02-20 17:23:44 +00:00
|
|
|
#include "client.h"
|
|
|
|
#include "ircd.h"
|
2016-02-10 06:08:58 +00:00
|
|
|
|
2017-06-28 20:24:10 +00:00
|
|
|
static const char tag_escape_table[256] = {
|
|
|
|
/* x0 x1 x2 x3 x4 x5 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,
|
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
|
|
|
msgbuf_unescape_value(char *value)
|
|
|
|
{
|
|
|
|
char *in = value;
|
|
|
|
char *out = value;
|
|
|
|
|
|
|
|
if (value == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
while (*in != '\0') {
|
|
|
|
if (*in == '\\') {
|
|
|
|
const char unescape = tag_unescape_table[(unsigned char)*++in];
|
|
|
|
|
|
|
|
/* "\\\0" is unescaped to the character itself, "\0" */
|
|
|
|
if (*in == '\0')
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (unescape) {
|
|
|
|
*out++ = unescape;
|
2017-07-26 20:06:49 +00:00
|
|
|
in++;
|
2017-06-28 20:24:10 +00:00
|
|
|
} else {
|
|
|
|
*out++ = *in++;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
*out++ = *in++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* copy final '\0' */
|
|
|
|
*out = *in;
|
|
|
|
}
|
|
|
|
|
2016-02-10 06:08:58 +00:00
|
|
|
/*
|
|
|
|
* parse a message into a MsgBuf.
|
|
|
|
* returns 0 on success, 1 on error.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
msgbuf_parse(struct MsgBuf *msgbuf, char *line)
|
|
|
|
{
|
2017-06-28 20:24:10 +00:00
|
|
|
char *ch = line;
|
2016-02-10 06:08:58 +00:00
|
|
|
|
|
|
|
msgbuf_init(msgbuf);
|
|
|
|
|
2017-06-28 20:24:10 +00:00
|
|
|
if (*ch == '@') {
|
2016-02-11 03:16:28 +00:00
|
|
|
char *t = ch + 1;
|
2016-02-10 06:08:58 +00:00
|
|
|
|
|
|
|
ch = strchr(ch, ' ');
|
2017-06-28 20:24:10 +00:00
|
|
|
|
|
|
|
/* truncate tags if they're too long */
|
|
|
|
if ((ch != NULL && (ch - line) + 1 > TAGSLEN) || (ch == NULL && strlen(line) >= TAGSLEN)) {
|
|
|
|
ch = &line[TAGSLEN - 1];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ch != NULL) {
|
|
|
|
/* NULL terminate the tags string */
|
|
|
|
*ch++ = '\0';
|
|
|
|
|
|
|
|
while (1) {
|
2016-02-10 06:08:58 +00:00
|
|
|
char *next = strchr(t, ';');
|
|
|
|
char *eq = strchr(t, '=');
|
|
|
|
|
2017-06-28 20:24:10 +00:00
|
|
|
if (next != NULL) {
|
2016-02-10 06:08:58 +00:00
|
|
|
*next = '\0';
|
|
|
|
|
2016-02-11 03:16:28 +00:00
|
|
|
if (eq > next)
|
|
|
|
eq = NULL;
|
|
|
|
}
|
2016-02-10 06:08:58 +00:00
|
|
|
|
|
|
|
if (eq != NULL)
|
2016-02-11 03:16:28 +00:00
|
|
|
*eq++ = '\0';
|
2016-02-10 06:08:58 +00:00
|
|
|
|
2017-06-28 20:24:10 +00:00
|
|
|
if (*t != '\0') {
|
|
|
|
msgbuf_unescape_value(eq);
|
2016-02-13 06:19:23 +00:00
|
|
|
msgbuf_append_tag(msgbuf, t, eq, 0);
|
2017-06-28 20:24:10 +00:00
|
|
|
}
|
2016-02-10 06:46:32 +00:00
|
|
|
|
2017-06-28 20:24:10 +00:00
|
|
|
if (next != NULL) {
|
2016-02-10 06:46:32 +00:00
|
|
|
t = next + 1;
|
2017-06-28 20:24:10 +00:00
|
|
|
} else {
|
2016-02-10 06:46:32 +00:00
|
|
|
break;
|
2017-06-28 20:24:10 +00:00
|
|
|
}
|
2016-02-10 06:08:58 +00:00
|
|
|
}
|
2017-06-28 20:24:10 +00:00
|
|
|
} else {
|
|
|
|
return 1;
|
2016-02-10 06:08:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-28 20:24:10 +00:00
|
|
|
/* truncate message if it's too long */
|
|
|
|
if (strlen(ch) > DATALEN) {
|
|
|
|
ch[DATALEN] = '\0';
|
|
|
|
}
|
2016-02-10 06:08:58 +00:00
|
|
|
|
2017-06-28 20:24:10 +00:00
|
|
|
if (*ch == ':') {
|
2016-02-10 06:08:58 +00:00
|
|
|
ch++;
|
|
|
|
msgbuf->origin = ch;
|
|
|
|
|
|
|
|
char *end = strchr(ch, ' ');
|
|
|
|
if (end == NULL)
|
2017-06-28 20:24:10 +00:00
|
|
|
return 4;
|
2016-02-10 06:08:58 +00:00
|
|
|
|
|
|
|
*end = '\0';
|
2017-06-28 20:24:10 +00:00
|
|
|
ch = end + 1;
|
2016-02-10 06:08:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (*ch == '\0')
|
2017-06-28 20:24:10 +00:00
|
|
|
return 2;
|
2016-02-10 06:08:58 +00:00
|
|
|
|
2017-07-28 20:50:16 +00:00
|
|
|
msgbuf->n_para = rb_string_to_array(ch, (char **)msgbuf->para, MAXPARA);
|
|
|
|
if (msgbuf->n_para == 0)
|
2017-06-28 20:24:10 +00:00
|
|
|
return 3;
|
2016-02-10 06:08:58 +00:00
|
|
|
|
2017-07-28 20:50:16 +00:00
|
|
|
msgbuf->cmd = msgbuf->para[0];
|
2016-02-10 06:08:58 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2016-02-20 17:21:12 +00:00
|
|
|
|
2017-06-28 20:24:10 +00:00
|
|
|
/*
|
|
|
|
* Unparse msgbuf tags into a buffer
|
|
|
|
* returns the length of the tags written
|
|
|
|
*/
|
|
|
|
static size_t
|
|
|
|
msgbuf_unparse_tags(char *buf, size_t buflen, const struct MsgBuf *msgbuf, unsigned int capmask)
|
2016-02-20 17:21:12 +00:00
|
|
|
{
|
2016-11-25 16:02:11 +00:00
|
|
|
bool has_tags = false;
|
2017-06-28 20:24:10 +00:00
|
|
|
char *commit = buf;
|
|
|
|
char *output = buf;
|
|
|
|
const char * const end = &buf[buflen - 2]; /* this is where the final ' ' goes */
|
|
|
|
|
|
|
|
for (size_t i = 0; i < msgbuf->n_tags; i++) {
|
|
|
|
size_t len;
|
2016-02-20 17:21:12 +00:00
|
|
|
|
|
|
|
if ((msgbuf->tags[i].capmask & capmask) == 0)
|
|
|
|
continue;
|
|
|
|
|
2016-11-25 16:02:11 +00:00
|
|
|
if (has_tags) {
|
2017-06-28 20:24:10 +00:00
|
|
|
if (output >= end)
|
|
|
|
break;
|
|
|
|
*output++ = ';';
|
2016-11-25 16:02:11 +00:00
|
|
|
} else {
|
2017-06-28 20:24:10 +00:00
|
|
|
if (output >= end)
|
|
|
|
break;
|
|
|
|
*output++ = '@';
|
2016-11-25 16:02:11 +00:00
|
|
|
}
|
2016-02-20 17:21:12 +00:00
|
|
|
|
2017-06-28 20:24:10 +00:00
|
|
|
if (msgbuf->tags[i].key == NULL)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
len = strlen(msgbuf->tags[i].key);
|
|
|
|
if (len == 0)
|
|
|
|
continue;
|
2016-02-20 17:21:12 +00:00
|
|
|
|
2017-06-28 20:24:10 +00:00
|
|
|
if (output + len > end)
|
|
|
|
break;
|
|
|
|
strcat(output, msgbuf->tags[i].key);
|
|
|
|
output += len;
|
|
|
|
|
|
|
|
if (msgbuf->tags[i].value != NULL) {
|
|
|
|
if (output >= end)
|
|
|
|
break;
|
|
|
|
*output++ = '=';
|
|
|
|
|
|
|
|
len = strlen(msgbuf->tags[i].value);
|
|
|
|
/* this only checks the unescaped length,
|
|
|
|
* but the escaped length could be longer
|
|
|
|
*/
|
|
|
|
if (output + len > end)
|
|
|
|
break;
|
|
|
|
|
|
|
|
for (size_t n = 0; n < len; n++) {
|
|
|
|
const unsigned char c = msgbuf->tags[i].value[n];
|
|
|
|
const char escape = tag_escape_table[c];
|
|
|
|
|
|
|
|
if (escape) {
|
|
|
|
if (output + 2 > end)
|
|
|
|
break;
|
|
|
|
|
|
|
|
*output++ = '\\';
|
|
|
|
*output++ = escape;
|
|
|
|
} else {
|
|
|
|
if (output >= end)
|
|
|
|
break;
|
|
|
|
|
|
|
|
*output++ = c;
|
|
|
|
}
|
|
|
|
}
|
2016-02-20 17:21:12 +00:00
|
|
|
}
|
2017-06-28 20:24:10 +00:00
|
|
|
|
|
|
|
has_tags = true;
|
|
|
|
commit = output;
|
2016-02-20 17:21:12 +00:00
|
|
|
}
|
|
|
|
|
2016-11-25 16:02:11 +00:00
|
|
|
if (has_tags)
|
2017-06-28 20:24:10 +00:00
|
|
|
*commit++ = ' ';
|
|
|
|
|
|
|
|
*commit = 0;
|
|
|
|
return commit - buf;
|
2016-02-20 17:21:12 +00:00
|
|
|
}
|
|
|
|
|
2017-06-28 20:24:10 +00:00
|
|
|
/*
|
|
|
|
* unparse a MsgBuf and message prefix into a buffer
|
|
|
|
* if origin is NULL, me.name will be used.
|
|
|
|
* cmd should not be NULL.
|
|
|
|
* updates buflen to correctly allow remaining message data to be added
|
|
|
|
*/
|
2016-02-20 20:46:09 +00:00
|
|
|
void
|
2017-06-28 20:24:10 +00:00
|
|
|
msgbuf_unparse_prefix(char *buf, size_t *buflen, const struct MsgBuf *msgbuf, unsigned int capmask)
|
2016-02-20 17:21:12 +00:00
|
|
|
{
|
2017-06-28 20:24:10 +00:00
|
|
|
size_t tags_buflen;
|
|
|
|
size_t tags_used = 0;
|
|
|
|
|
|
|
|
memset(buf, 0, *buflen);
|
|
|
|
|
|
|
|
tags_buflen = *buflen;
|
|
|
|
if (tags_buflen > TAGSLEN + 1)
|
|
|
|
tags_buflen = TAGSLEN + 1;
|
2016-02-20 17:21:12 +00:00
|
|
|
|
|
|
|
if (msgbuf->n_tags > 0)
|
2017-06-28 20:24:10 +00:00
|
|
|
tags_used = msgbuf_unparse_tags(buf, tags_buflen, msgbuf, capmask);
|
|
|
|
|
|
|
|
const size_t data_bufmax = (tags_used + DATALEN + 1);
|
|
|
|
if (*buflen > data_bufmax)
|
|
|
|
*buflen = data_bufmax;
|
2016-02-20 17:21:12 +00:00
|
|
|
|
2017-06-28 20:24:10 +00:00
|
|
|
rb_snprintf_append(buf, *buflen, ":%s ", msgbuf->origin != NULL ? msgbuf->origin : me.name);
|
2016-02-20 22:37:19 +00:00
|
|
|
|
|
|
|
if (msgbuf->cmd != NULL)
|
2017-06-28 20:24:10 +00:00
|
|
|
rb_snprintf_append(buf, *buflen, "%s ", msgbuf->cmd);
|
2016-02-20 23:59:00 +00:00
|
|
|
|
|
|
|
if (msgbuf->target != NULL)
|
2017-06-28 20:24:10 +00:00
|
|
|
rb_snprintf_append(buf, *buflen, "%s ", msgbuf->target);
|
2016-02-20 17:21:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* unparse a pure MsgBuf into a buffer.
|
|
|
|
* if origin is NULL, me.name will be used.
|
2017-06-28 20:24:10 +00:00
|
|
|
* cmd should not be NULL.
|
2016-02-20 17:21:12 +00:00
|
|
|
* returns 0 on success, 1 on error.
|
|
|
|
*/
|
|
|
|
int
|
2017-06-28 20:24:10 +00:00
|
|
|
msgbuf_unparse(char *buf, size_t buflen, const struct MsgBuf *msgbuf, unsigned int capmask)
|
2016-02-20 17:21:12 +00:00
|
|
|
{
|
2017-06-28 20:24:10 +00:00
|
|
|
size_t buflen_copy = buflen;
|
|
|
|
|
|
|
|
msgbuf_unparse_prefix(buf, &buflen_copy, msgbuf, capmask);
|
|
|
|
|
|
|
|
for (size_t i = 0; i < msgbuf->n_para; i++) {
|
|
|
|
const char *fmt;
|
|
|
|
|
|
|
|
if (i == (msgbuf->n_para - 1) && strchr(msgbuf->para[i], ' ') != NULL) {
|
|
|
|
fmt = (i == 0) ? ":%s" : " :%s";
|
|
|
|
} else {
|
|
|
|
fmt = (i == 0) ? "%s" : " %s";
|
2016-02-20 17:21:12 +00:00
|
|
|
}
|
2017-06-28 20:24:10 +00:00
|
|
|
|
|
|
|
rb_snprintf_append(buf, buflen_copy, fmt, msgbuf->para[i]);
|
2016-02-20 17:21:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2016-02-20 17:38:00 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* unparse a MsgBuf stem + format string into a buffer
|
|
|
|
* if origin is NULL, me.name will be used.
|
|
|
|
* cmd may not be NULL.
|
|
|
|
* returns 0 on success, 1 on error.
|
|
|
|
*/
|
|
|
|
int
|
2017-06-28 20:24:10 +00:00
|
|
|
msgbuf_vunparse_fmt(char *buf, size_t buflen, const struct MsgBuf *head, unsigned int capmask, const char *fmt, va_list va)
|
2016-02-20 17:38:00 +00:00
|
|
|
{
|
2017-06-28 20:24:10 +00:00
|
|
|
size_t buflen_copy = buflen;
|
2016-02-20 17:38:00 +00:00
|
|
|
char *ws;
|
|
|
|
size_t prefixlen;
|
|
|
|
|
2017-06-28 20:24:10 +00:00
|
|
|
msgbuf_unparse_prefix(buf, &buflen_copy, head, capmask);
|
2016-02-20 17:38:00 +00:00
|
|
|
prefixlen = strlen(buf);
|
|
|
|
|
|
|
|
ws = buf + prefixlen;
|
2017-06-28 20:24:10 +00:00
|
|
|
vsnprintf(ws, buflen_copy - prefixlen, fmt, va);
|
2016-02-20 17:38:00 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* unparse a MsgBuf stem + format string into a buffer (with va_list handling)
|
|
|
|
* if origin is NULL, me.name will be used.
|
|
|
|
* cmd may not be NULL.
|
|
|
|
* returns 0 on success, 1 on error.
|
|
|
|
*/
|
|
|
|
int
|
2017-06-28 20:24:10 +00:00
|
|
|
msgbuf_unparse_fmt(char *buf, size_t buflen, const struct MsgBuf *head, unsigned int capmask, const char *fmt, ...)
|
2016-02-20 17:38:00 +00:00
|
|
|
{
|
|
|
|
va_list va;
|
|
|
|
int res;
|
|
|
|
|
|
|
|
va_start(va, fmt);
|
|
|
|
res = msgbuf_vunparse_fmt(buf, buflen, head, capmask, fmt, va);
|
|
|
|
va_end(va);
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|