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/tokenizer/message.c

143 lines
3.6 KiB
C

/*
* This file is part of uIRC. (https://git.redxen.eu/caskd/uIRC)
* Copyright (c) 2019-2021 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 "uirc/error.h" // uirc_errno
#include "uirc/memory.h" // uirc_memory_*()
#include "uirc/tokenizer.h" // uirc_tokenizer_*()
#include "uirc/type.h" // IRC_Message
#include <assert.h> // assert()
#include <corelibs/stringext.h> // stringext_strmalloc() stringext_strtok_mr()
#include <ctype.h> // isalnum()
#include <stdio.h> // NULL
#include <stdlib.h> // malloc()
#include <string.h> // strlen()
static void skip_spaces(char** addr);
IRC_Message*
uirc_tokenizer_message(const char* str)
{
assert(str != NULL);
IRC_Message* m = NULL;
char* ws = NULL;
if ((m = malloc(sizeof(*m))) == NULL) {
uirc_errno = UIRC_ERR_SYSERR;
goto cleanup;
}
memset(m, 0, sizeof(*m));
size_t len = strlen(str);
// Ignore CRLF at the end, irrelevant for parsing
if (str[len - 1] == '\n') {
len--;
if (str[len - 1] == '\r') len--;
}
if ((ws = stringext_strmalloc(str, len)) == NULL) {
uirc_errno = UIRC_ERR_SYSERR;
goto cleanup;
}
char* p = ws;
#ifdef UIRC_FEATURE_IRCV3
if (*p == '@') {
p++;
const char* tmp = NULL;
if ((tmp = stringext_strtok_mr(&p, " ")) == NULL) {
uirc_errno = UIRC_ERR_INVFMT;
goto cleanup;
}
if ((m->tag_list = uirc_tokenizer_tag_list(tmp)) == NULL) goto cleanup;
}
skip_spaces(&p);
#endif /* UIRC_FEATURE_IRCV3 */
if (*p == ':') {
const char* tmp = NULL;
p++;
if ((tmp = stringext_strtok_mr(&p, " ")) == NULL) {
uirc_errno = UIRC_ERR_INVFMT;
goto cleanup;
}
if ((m->source = uirc_tokenizer_user(tmp)) == NULL) goto cleanup;
skip_spaces(&p);
}
if (isalnum(*p)) {
const char* tmp = NULL;
if ((tmp = stringext_strtok_mr(&p, " ")) == NULL) {
uirc_errno = UIRC_ERR_INVFMT;
goto cleanup;
}
if ((m->command = stringext_strmalloc(tmp, strlen(tmp))) == NULL) {
uirc_errno = UIRC_ERR_SYSERR;
goto cleanup;
}
} else {
uirc_errno = UIRC_ERR_INVFMT;
goto cleanup;
}
skip_spaces(&p);
short i;
for (i = 0; i < IRC_MAXARGS && *p;) {
if (i == IRC_MAXARGS - 1 || *p == ':') {
if (*p == ':') {
p++;
// trailing is supposed to represent a early ending of spaced arguments, not if there's any trailing arguments at all
m->trailing = true;
}
if ((m->args[i++] = stringext_strmalloc(p, strlen(p))) == NULL) {
uirc_errno = UIRC_ERR_SYSERR;
goto cleanup;
}
break;
}
const char* tmp = NULL;
if ((tmp = stringext_strtok_mr(&p, " ")) == NULL) {
uirc_errno = UIRC_ERR_INVFMT;
goto cleanup;
}
if ((m->args[i++] = stringext_strmalloc(tmp, strlen(tmp))) == NULL) {
uirc_errno = UIRC_ERR_SYSERR;
goto cleanup;
}
skip_spaces(&p);
if (i < IRC_MAXARGS) m->args[i] = NULL;
}
free(ws);
return m;
cleanup:
if (m != NULL) uirc_struct_free(m, IRC_STRUCT_MESSAGE);
free(m);
free(ws);
return NULL;
}
static void
skip_spaces(char** addr)
{
for (; **addr == ' '; (*addr)++)
;
}