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/user.c

77 lines
2.0 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 "error.h" // uirc_errno
#include "memory.h" // uirc_memory_*()
#include "tokenizer.h" // uirc_tokenizer_*()
#include "type.h" // IRC_Message
#include <assert.h> // assert()
#include <corelibs/stringext.h> // stringext_strmalloc()
#include <stdbool.h> // bool
#include <stdlib.h> // malloc()
#include <string.h> // strchr()
IRC_User*
uirc_tokenizer_user(const char* str)
{
assert(str != NULL);
IRC_User* u = NULL;
char* ws = NULL;
if ((u = malloc(sizeof(*u))) == NULL) {
uirc_errno = UIRC_ERR_SYSERR;
goto cleanup;
}
memset(u, 0, sizeof(*u));
if ((ws = stringext_strmalloc(str, strlen(str))) == NULL) {
uirc_errno = UIRC_ERR_SYSERR;
goto cleanup;
}
char* tmp;
if ((tmp = strchr(ws, '@')) != NULL) {
*(tmp++) = '\0';
if ((u->host = stringext_strmalloc(tmp, strlen(tmp))) == NULL) {
uirc_errno = UIRC_ERR_SYSERR;
goto cleanup;
}
}
if ((tmp = strchr(ws, '!')) != NULL) {
*(tmp++) = '\0';
if ((u->user = stringext_strmalloc(tmp, strlen(tmp))) == NULL) {
uirc_errno = UIRC_ERR_SYSERR;
goto cleanup;
}
}
if ((u->nick = stringext_strmalloc(ws, strlen(ws))) == NULL) {
uirc_errno = UIRC_ERR_SYSERR;
goto cleanup;
}
free(ws);
return u;
cleanup:
if (u != NULL) uirc_struct_free(u, IRC_STRUCT_USER);
free(u);
free(ws);
return NULL;
}