/*
* 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 .
*/
#include "../memory/memory.h" // uirc_memory_*()
#include "../types.h" // IRC_Message
#include "tokenizers.h" // uirc_tokenizer_*()
#include // assert()
#include // malloc_string()
#include // bool
#include // malloc()
#include // strchr()
static void free_ctx(IRC_User* u, char* s);
IRC_User*
uirc_tokenizer_user(const char* s)
{
assert(s != NULL);
IRC_User* u = malloc(sizeof(IRC_User));
char* const ws = malloc_string(s, strlen(s));
char* tmp;
if (u == NULL || ws == NULL) {
free(u);
free(ws);
return NULL;
}
if ((tmp = strchr(ws, '@')) != NULL) {
*(tmp++) = '\0';
if ((u->host = malloc_string(tmp, strlen(tmp))) == NULL) {
free_ctx(u, ws);
return NULL;
}
}
if ((tmp = strchr(ws, '!')) != NULL) {
*(tmp++) = '\0';
if ((u->user = malloc_string(tmp, strlen(tmp))) == NULL) {
free_ctx(u, ws);
return NULL;
}
}
if ((u->nick = malloc_string(ws, strlen(ws))) == NULL) {
free_ctx(u, ws);
return NULL;
}
free(ws);
return u;
}
static void
free_ctx(IRC_User* u, char* s)
{
uirc_free_user(u);
free(u);
free(s);
}