2021-02-15 11:39:25 +00:00
|
|
|
/*
|
|
|
|
* This file is part of uIRC. (https://git.redxen.eu/caskd/uIRC)
|
|
|
|
* Copyright (c) 2019, 2020 Alex-David Denes
|
|
|
|
*
|
|
|
|
* uIRC is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* any later version.
|
|
|
|
*
|
|
|
|
* uIRC is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with uIRC. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2021-02-15 15:22:10 +00:00
|
|
|
#include "assemblers.h" // uirc_assembler_tag_*
|
2021-02-22 17:09:41 +00:00
|
|
|
#include "types.h" // IRC_Tag
|
2021-02-15 11:39:25 +00:00
|
|
|
|
2021-02-15 15:22:10 +00:00
|
|
|
#include <assert.h> // assert()
|
|
|
|
#include <corelibs/llist.h> // llist_t
|
|
|
|
#include <stdio.h> // NULL
|
|
|
|
#include <sys/types.h> // size_t ssize_t
|
2021-02-15 11:39:25 +00:00
|
|
|
|
2021-02-15 15:22:10 +00:00
|
|
|
ssize_t
|
2021-02-15 11:39:25 +00:00
|
|
|
uirc_assembler_tag(char* buf, const IRC_Tag* t, size_t len)
|
|
|
|
{
|
|
|
|
assert(t != NULL);
|
2021-02-15 15:22:10 +00:00
|
|
|
assert(buf != NULL);
|
2021-02-15 11:39:25 +00:00
|
|
|
assert(t->key != NULL);
|
|
|
|
|
2021-02-15 15:22:10 +00:00
|
|
|
const char* const sv = buf;
|
|
|
|
int res;
|
2021-02-15 11:39:25 +00:00
|
|
|
|
|
|
|
if (t->clientbound) {
|
|
|
|
if (len > 1) {
|
|
|
|
*(buf++) = '+';
|
|
|
|
len--;
|
|
|
|
} else
|
2021-02-15 15:22:10 +00:00
|
|
|
return -1;
|
2021-02-15 11:39:25 +00:00
|
|
|
}
|
|
|
|
|
2021-02-15 15:22:10 +00:00
|
|
|
if (len < 1) return -1;
|
2021-02-15 11:39:25 +00:00
|
|
|
if ((res = snprintf(buf, len, "%s", t->key)) >= 0) {
|
|
|
|
buf += (size_t) res;
|
|
|
|
len -= (size_t) res;
|
|
|
|
} else
|
2021-02-15 15:22:10 +00:00
|
|
|
return -1;
|
2021-02-15 11:39:25 +00:00
|
|
|
|
|
|
|
if (t->value != NULL) {
|
2021-02-15 15:22:10 +00:00
|
|
|
if (len < 1) return -1;
|
2021-02-15 11:39:25 +00:00
|
|
|
if ((res = snprintf(buf, len, "=%s", t->value)) >= 0) {
|
|
|
|
buf += (size_t) res;
|
|
|
|
len -= (size_t) res;
|
|
|
|
} else
|
2021-02-15 15:22:10 +00:00
|
|
|
return -1;
|
2021-02-15 11:39:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return buf - sv;
|
|
|
|
}
|
|
|
|
|
2021-02-15 15:22:10 +00:00
|
|
|
ssize_t
|
|
|
|
uirc_assembler_tag_list(char* buf, const llist_t* tl, size_t len)
|
|
|
|
{
|
|
|
|
assert(tl != NULL);
|
|
|
|
assert(buf != NULL);
|
|
|
|
|
|
|
|
const char* const sv = buf;
|
2021-03-05 11:26:31 +00:00
|
|
|
for (; tl != NULL && tl->content != NULL && ((IRC_Tag*) tl->content)->key != NULL; tl = tl->next) {
|
|
|
|
if (buf != sv) {
|
2021-02-15 15:22:10 +00:00
|
|
|
if (len > 1) {
|
|
|
|
*(buf++) = ';';
|
|
|
|
len--;
|
|
|
|
} else
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t ret;
|
|
|
|
if ((ret = uirc_assembler_tag(buf, tl->content, len)) >= 0) {
|
|
|
|
buf += (size_t) ret;
|
|
|
|
len -= (size_t) ret;
|
|
|
|
} else
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return buf - sv;
|
|
|
|
}
|