2021-02-15 11:39:25 +00:00
|
|
|
/*
|
|
|
|
* This file is part of uIRC. (https://git.redxen.eu/caskd/uIRC)
|
2021-03-11 22:52:30 +00:00
|
|
|
* Copyright (c) 2019-2021 Alex-David Denes
|
2021-02-15 11:39:25 +00:00
|
|
|
*
|
|
|
|
* 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-04-07 21:45:44 +00:00
|
|
|
#include "error.h" // uirc_errno
|
|
|
|
#include "memory.h" // Free_IRC_Tag()
|
|
|
|
#include "tokenizer.h" // uirc_tokenizer_tags()
|
|
|
|
#include "type.h" // IRC_Tag
|
2021-02-15 11:39:25 +00:00
|
|
|
|
|
|
|
#include <assert.h> // assert()
|
2021-03-12 23:26:19 +00:00
|
|
|
#include <corelibs/llist.h> // llist_elem_alloc() llist_elem_conn() llist_elem_rm()
|
|
|
|
#include <corelibs/stringext.h> // stringext_strmalloc()
|
2021-02-15 11:39:25 +00:00
|
|
|
#include <stdbool.h> // true
|
|
|
|
#include <stdlib.h> // free()
|
|
|
|
#include <string.h> // strchr()
|
|
|
|
|
|
|
|
llist_t*
|
2021-02-15 15:57:29 +00:00
|
|
|
uirc_tokenizer_tag_list(const char* str)
|
2021-02-15 11:39:25 +00:00
|
|
|
{
|
2021-02-15 15:57:29 +00:00
|
|
|
assert(str != NULL);
|
2021-02-15 11:39:25 +00:00
|
|
|
|
2021-03-30 13:24:16 +00:00
|
|
|
llist_t* l = NULL;
|
|
|
|
char* ws = NULL;
|
|
|
|
|
|
|
|
if ((ws = stringext_strmalloc(str, strlen(str))) == NULL) {
|
|
|
|
uirc_errno = UIRC_ERR_SYSERR;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2021-02-15 11:39:25 +00:00
|
|
|
|
2021-03-30 13:24:16 +00:00
|
|
|
char * p = ws, *tmp;
|
|
|
|
llist_t* pl = NULL;
|
2021-03-12 23:26:19 +00:00
|
|
|
while ((tmp = stringext_strtok_mr(&p, ";")) != NULL) {
|
2021-02-15 11:39:25 +00:00
|
|
|
llist_t* cl;
|
2021-03-12 23:26:19 +00:00
|
|
|
if ((cl = llist_elem_alloc(0)) == NULL) {
|
2021-03-30 13:24:16 +00:00
|
|
|
uirc_errno = UIRC_ERR_SYSERR;
|
|
|
|
goto cleanup;
|
2021-02-15 11:39:25 +00:00
|
|
|
}
|
2021-03-30 13:24:16 +00:00
|
|
|
if ((cl->content = uirc_tokenizer_tag(tmp)) == NULL) goto cleanup;
|
2021-02-15 11:39:25 +00:00
|
|
|
|
|
|
|
if (l == NULL) l = cl;
|
|
|
|
else
|
2021-03-12 23:26:19 +00:00
|
|
|
llist_elem_conn(pl, cl);
|
2021-02-15 11:39:25 +00:00
|
|
|
pl = cl;
|
|
|
|
}
|
2021-02-15 16:10:31 +00:00
|
|
|
|
2021-02-15 15:57:29 +00:00
|
|
|
free(ws);
|
2021-02-15 11:39:25 +00:00
|
|
|
return l;
|
2021-03-30 13:24:16 +00:00
|
|
|
cleanup:
|
|
|
|
if (l != NULL) uirc_list_free(l, IRC_STRUCT_TAG);
|
|
|
|
return NULL;
|
2021-02-15 11:39:25 +00:00
|
|
|
}
|
|
|
|
|
2021-02-15 15:57:29 +00:00
|
|
|
IRC_Tag*
|
|
|
|
uirc_tokenizer_tag(const char* str)
|
|
|
|
{
|
|
|
|
assert(str != NULL);
|
|
|
|
|
2021-03-30 13:24:16 +00:00
|
|
|
char* ws = NULL;
|
|
|
|
IRC_Tag* t = NULL;
|
2021-02-15 15:57:29 +00:00
|
|
|
|
2021-03-30 13:24:16 +00:00
|
|
|
if ((ws = stringext_strmalloc(str, strlen(str))) == NULL) {
|
|
|
|
uirc_errno = UIRC_ERR_SYSERR;
|
|
|
|
goto cleanup;
|
2021-02-15 15:57:29 +00:00
|
|
|
}
|
2021-02-15 16:10:31 +00:00
|
|
|
|
2021-03-30 13:24:16 +00:00
|
|
|
if ((t = malloc(sizeof(*t))) == NULL) {
|
|
|
|
uirc_errno = UIRC_ERR_SYSERR;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
memset(t, 0, sizeof(*t));
|
2021-02-15 16:10:31 +00:00
|
|
|
|
2021-03-30 13:24:16 +00:00
|
|
|
char* ckey = ws;
|
2021-02-15 15:57:29 +00:00
|
|
|
if (*ws == '+') {
|
|
|
|
ckey++;
|
|
|
|
t->clientbound = true;
|
|
|
|
}
|
|
|
|
char* cval = strchr(ckey, '=');
|
|
|
|
if (cval != NULL) {
|
|
|
|
*(cval++) = '\0';
|
2021-03-12 23:26:19 +00:00
|
|
|
if ((t->value = stringext_strmalloc(cval, strlen(cval))) == NULL) {
|
2021-03-30 13:24:16 +00:00
|
|
|
uirc_errno = UIRC_ERR_SYSERR;
|
|
|
|
goto cleanup;
|
2021-02-15 15:57:29 +00:00
|
|
|
}
|
|
|
|
}
|
2021-03-12 23:26:19 +00:00
|
|
|
if ((t->key = stringext_strmalloc(ckey, strlen(ckey))) == NULL) {
|
2021-03-30 13:24:16 +00:00
|
|
|
uirc_errno = UIRC_ERR_SYSERR;
|
|
|
|
goto cleanup;
|
2021-02-15 15:57:29 +00:00
|
|
|
}
|
2021-02-15 16:10:31 +00:00
|
|
|
|
2021-02-15 15:57:29 +00:00
|
|
|
free(ws);
|
|
|
|
return t;
|
2021-03-30 13:24:16 +00:00
|
|
|
cleanup:
|
|
|
|
if (t != NULL) uirc_struct_free(t, IRC_STRUCT_TAG);
|
2021-02-15 15:57:29 +00:00
|
|
|
free(t);
|
2021-03-30 13:24:16 +00:00
|
|
|
free(ws);
|
|
|
|
return NULL;
|
2021-02-15 11:39:25 +00:00
|
|
|
}
|