This repository has been archived on 2021-04-17. You can view files and clone it, but cannot push or open issues or pull requests.
uIRC/src/assemblers/message.c

113 lines
2.9 KiB
C

/*
* 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/>.
*/
#include "../errors.h" // ERR_UIRC_*
#include "../types.h" // IRC_Message
#include "assemblers.h" // Assm_mesg() Assm_tags() Assm_user()
#include <assert.h> // assert()
#include <stdbool.h> // false
#include <stdio.h> // NULL, snprintf()
#include <string.h> // strlen()
#include <sys/types.h> // size_t
signed long
uirc_assembler_message(char* buf, IRC_Message* m, size_t len)
{
assert(buf != NULL);
assert(m != NULL);
char* const sv = buf;
signed long ret;
#ifdef UIRC_FEATURE_IRCV3
if (m->tag_list != NULL && m->tag_list->content != NULL) {
for (llist_t* l = m->tag_list; l != NULL && l->content != NULL && ((IRC_Tag*) l->content)->key != NULL;) {
if (m->tag_list == l) {
if (len > 1) {
*(buf++) = '@';
len--;
} else
return UIRC_ERR_BUFFER_FULL;
} else {
if (len > 1) {
*(buf++) = ';';
len--;
} else
return UIRC_ERR_BUFFER_FULL;
}
if ((ret = uirc_assembler_tag(buf, l->content, len)) >= 0) {
buf += (size_t) ret;
len -= (size_t) ret;
} else
return ret;
if ((l = l->next) == NULL) {
if (len > 1) {
*(buf++) = ' ';
len--;
} else
return UIRC_ERR_BUFFER_FULL;
}
}
}
#endif /* UIRC_FEATURE_IRCV3 */
if (m->source != NULL && (m->source->nick != NULL || m->source->host != NULL)) {
if (len > 1) {
*(buf++) = ':';
len--;
} else
return UIRC_ERR_BUFFER_FULL;
if ((ret = uirc_assembler_user(buf, m->source, len)) >= 0) {
buf += (size_t) ret;
len -= (size_t) ret;
} else
return ret;
if (len > 1) {
*(buf++) = ' ';
len--;
} else
return UIRC_ERR_BUFFER_FULL;
}
if (m->command != NULL) {
if (len < 2) return UIRC_ERR_BUFFER_FULL;
if ((ret = snprintf(buf, len, "%s ", m->command)) >= 0) {
buf += (size_t) ret;
len -= (size_t) ret;
} else
return UIRC_ERR_BUFFER_ERR;
} else
return UIRC_ERR_INVALID_FORMAT;
for (unsigned int i = 0; i < IRC_MAXARGS && m->args[i] != NULL; i++) {
if (len < 2) return UIRC_ERR_BUFFER_FULL;
if ((ret = snprintf(buf, len, (i + 1 == IRC_MAXARGS || (m->args[i + 1] == NULL && m->trailing)) ? ":%s" : "%s ", m->args[i])) >= 0) {
buf += (size_t) ret;
len -= (size_t) ret;
} else
return UIRC_ERR_BUFFER_ERR;
}
return (buf += ret) - sv;
}