191 lines
4.7 KiB
C
191 lines
4.7 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 "memory.h"
|
|
#include "types.h" // IRC_*
|
|
|
|
#include <assert.h> // assert()
|
|
#include <corelibs/llist.h> // llist_t remove_linked_list_elem()
|
|
#include <corelibs/stringext.h> // malloc_string()
|
|
#include <stdarg.h> // va_*() va_list
|
|
#include <stdlib.h> // free()
|
|
#include <string.h> // strcpy()
|
|
|
|
IRC_Tag*
|
|
uirc_malloc_tag(const char* key, const char* value)
|
|
{
|
|
IRC_Tag* ret;
|
|
assert(key != NULL);
|
|
if ((ret = malloc(sizeof(IRC_Tag))) != NULL) {
|
|
memset(ret, 0, sizeof(IRC_Tag));
|
|
if ((ret->key = malloc_string(key, strlen(key))) == NULL) {
|
|
uirc_free_tag(ret);
|
|
return NULL;
|
|
}
|
|
if (value != NULL) {
|
|
if ((ret->value = malloc_string(value, strlen(value))) == NULL) {
|
|
uirc_free_tag(ret);
|
|
return NULL;
|
|
}
|
|
}
|
|
return ret;
|
|
}
|
|
return NULL;
|
|
}
|
|
|
|
IRC_Capability*
|
|
uirc_malloc_capability(const char* name)
|
|
{
|
|
IRC_Capability* ret;
|
|
assert(name != NULL);
|
|
if ((ret = malloc(sizeof(IRC_Capability))) != NULL) {
|
|
memset(ret, 0, sizeof(IRC_Capability));
|
|
if ((ret->name = malloc_string(name, strlen(name))) == NULL) {
|
|
uirc_free_capability(ret);
|
|
return NULL;
|
|
}
|
|
return ret;
|
|
}
|
|
return NULL;
|
|
}
|
|
|
|
IRC_User*
|
|
uirc_malloc_user(const char* nick, const char* user, const char* real, const char* host)
|
|
{
|
|
IRC_User* ret;
|
|
if ((ret = malloc(sizeof(IRC_User))) != NULL) {
|
|
memset(ret, 0, sizeof(IRC_User));
|
|
if (nick != NULL) {
|
|
if ((ret->nick = malloc_string(nick, strlen(nick))) == NULL) {
|
|
uirc_free_user(ret);
|
|
return NULL;
|
|
}
|
|
}
|
|
if (user != NULL) {
|
|
if ((ret->user = malloc_string(user, strlen(user))) == NULL) {
|
|
uirc_free_user(ret);
|
|
return NULL;
|
|
}
|
|
}
|
|
if (real != NULL) {
|
|
if ((ret->real = malloc_string(real, strlen(real))) == NULL) {
|
|
uirc_free_user(ret);
|
|
return NULL;
|
|
}
|
|
}
|
|
if (host != NULL) {
|
|
if ((ret->host = malloc_string(host, strlen(host))) == NULL) {
|
|
uirc_free_user(ret);
|
|
return NULL;
|
|
}
|
|
}
|
|
return ret;
|
|
}
|
|
return NULL;
|
|
}
|
|
|
|
IRC_Message*
|
|
uirc_malloc_message(const char* command, ...)
|
|
{
|
|
IRC_Message* ret;
|
|
assert(command != NULL);
|
|
if ((ret = malloc(sizeof(IRC_Capability))) != NULL) {
|
|
memset(ret, 0, sizeof(IRC_Capability));
|
|
if ((ret->command = malloc_string(command, strlen(command))) == NULL) {
|
|
uirc_free_message(ret);
|
|
return NULL;
|
|
}
|
|
return ret;
|
|
}
|
|
va_list ap;
|
|
va_start(ap, command);
|
|
for (unsigned short i = 0; i < IRC_MAXARGS; i++) {
|
|
const char* carg = NULL;
|
|
if ((carg = va_arg(ap, const char*)) != NULL) {
|
|
if ((ret->args[i] = malloc_string(carg, strlen(carg))) == NULL) {
|
|
uirc_free_message(ret);
|
|
return NULL;
|
|
}
|
|
}
|
|
}
|
|
va_end(ap);
|
|
return NULL;
|
|
}
|
|
|
|
IRC_Buffer*
|
|
uirc_malloc_buffer(const char* name, const char* topic, const char* key)
|
|
{
|
|
IRC_Buffer* ret;
|
|
assert(name != NULL);
|
|
if ((ret = malloc(sizeof(IRC_Buffer))) != NULL) {
|
|
memset(ret, 0, sizeof(IRC_Buffer));
|
|
if ((ret->name = malloc_string(name, strlen(name))) == NULL) {
|
|
uirc_free_buffer(ret);
|
|
return NULL;
|
|
}
|
|
if (topic != NULL) {
|
|
if ((ret->topic = malloc_string(topic, strlen(topic))) == NULL) {
|
|
uirc_free_buffer(ret);
|
|
return NULL;
|
|
}
|
|
}
|
|
if (key != NULL) {
|
|
if ((ret->key = malloc_string(key, strlen(key))) == NULL) {
|
|
uirc_free_buffer(ret);
|
|
return NULL;
|
|
}
|
|
}
|
|
return ret;
|
|
}
|
|
return NULL;
|
|
}
|
|
|
|
IRC_Network*
|
|
uirc_malloc_network(const char* addr, const char* svc, const char* pass, const char* quitmsg)
|
|
{
|
|
IRC_Network* ret;
|
|
assert(addr != NULL);
|
|
assert(svc != NULL);
|
|
if ((ret = malloc(sizeof(IRC_Network))) != NULL) {
|
|
memset(ret, 0, sizeof(IRC_Network));
|
|
if ((ret->addr = malloc_string(addr, strlen(addr))) == NULL) {
|
|
uirc_free_network(ret);
|
|
return NULL;
|
|
}
|
|
if ((ret->svc = malloc_string(svc, strlen(svc))) == NULL) {
|
|
uirc_free_network(ret);
|
|
return NULL;
|
|
}
|
|
if (pass != NULL) {
|
|
if ((ret->pass = malloc_string(pass, strlen(svc))) == NULL) {
|
|
uirc_free_network(ret);
|
|
return NULL;
|
|
}
|
|
}
|
|
if (quitmsg != NULL) {
|
|
if ((ret->quitmsg = malloc_string(quitmsg, strlen(quitmsg))) == NULL) {
|
|
uirc_free_network(ret);
|
|
return NULL;
|
|
}
|
|
}
|
|
return ret;
|
|
}
|
|
return NULL;
|
|
}
|
|
|