257 lines
6.8 KiB
C
257 lines
6.8 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 "errors.h" // uirc_errno
|
|
#include "memory.h"
|
|
#include "types.h" // IRC_*
|
|
|
|
#include <assert.h> // assert()
|
|
#include <corelibs/stringext.h> // stringext_strmalloc()
|
|
#include <stdarg.h> // va_*
|
|
#include <stdlib.h> // free()
|
|
#include <string.h> // strlen()
|
|
|
|
void
|
|
uirc_struct_free(void* obj, IRC_Struct_Type type)
|
|
{
|
|
assert(obj != NULL);
|
|
switch (type) {
|
|
#ifdef UIRC_FEATURE_IRCV3
|
|
case IRC_STRUCT_TAG: {
|
|
IRC_Tag* tmpob = obj;
|
|
free(tmpob->key);
|
|
free(tmpob->value);
|
|
break;
|
|
}
|
|
case IRC_STRUCT_CAPABILITY: {
|
|
IRC_Capability* tmpob = obj;
|
|
free(tmpob->name);
|
|
break;
|
|
}
|
|
#endif /* UIRC_FEATURE_IRCV3 */
|
|
case IRC_STRUCT_USER: {
|
|
IRC_User* tmpob = obj;
|
|
free(tmpob->nick);
|
|
free(tmpob->user);
|
|
free(tmpob->real);
|
|
free(tmpob->host);
|
|
break;
|
|
}
|
|
case IRC_STRUCT_MESSAGE: {
|
|
IRC_Message* tmpob = obj;
|
|
free(tmpob->command);
|
|
if (tmpob->source != NULL) {
|
|
uirc_struct_free(tmpob->source, IRC_STRUCT_USER);
|
|
free(tmpob->source);
|
|
}
|
|
#ifdef UIRC_FEATURE_IRCV3
|
|
if (tmpob->tag_list != NULL) uirc_list_free(tmpob->tag_list, IRC_STRUCT_TAG);
|
|
#endif /* UIRC_FEATURE_IRCV3 */
|
|
for (unsigned short i = 0; i < IRC_MAXARGS && tmpob->args[i] != NULL; i++) free(tmpob->args[i]);
|
|
break;
|
|
}
|
|
case IRC_STRUCT_BUFFER: {
|
|
IRC_Buffer* tmpob = obj;
|
|
free(tmpob->name);
|
|
free(tmpob->key);
|
|
free(tmpob->topic);
|
|
if (tmpob->message_list != NULL) uirc_list_free(tmpob->message_list, IRC_STRUCT_MESSAGE);
|
|
if (tmpob->user_list != NULL) uirc_list_free(tmpob->user_list, IRC_STRUCT_USER);
|
|
break;
|
|
}
|
|
case IRC_STRUCT_NETWORK: {
|
|
IRC_Network* tmpob = obj;
|
|
free(tmpob->addr);
|
|
free(tmpob->svc);
|
|
free(tmpob->pass);
|
|
if (tmpob->user != NULL) {
|
|
uirc_struct_free(tmpob->user, IRC_STRUCT_USER);
|
|
free(tmpob->user);
|
|
}
|
|
#ifdef UIRC_FEATURE_IRCV3
|
|
if (tmpob->cap_list != NULL) uirc_list_free(tmpob->cap_list, IRC_STRUCT_CAPABILITY);
|
|
#endif /* UIRC_FEATURE_IRCV3 */
|
|
if (tmpob->buf_list != NULL) uirc_list_free(tmpob->buf_list, IRC_STRUCT_BUFFER);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
#ifdef UIRC_FEATURE_IRCV3
|
|
IRC_Tag*
|
|
uirc_struct_assm_tag(bool clientbound, const char* key, const char* value)
|
|
{
|
|
assert(key != NULL);
|
|
IRC_Tag* t = NULL;
|
|
|
|
if ((t = malloc(sizeof(*t))) == NULL) {
|
|
uirc_errno = UIRC_ERR_SYSERR;
|
|
goto cleanup;
|
|
}
|
|
memset(t, 0, sizeof(*t));
|
|
|
|
t->clientbound = clientbound;
|
|
if ((t->key = stringext_strmalloc(key, strlen(key))) == NULL
|
|
|| (value != NULL && (t->value = stringext_strmalloc(value, strlen(value))) == NULL)) {
|
|
uirc_errno = UIRC_ERR_SYSERR;
|
|
goto cleanup;
|
|
}
|
|
return t;
|
|
cleanup:
|
|
if (t != NULL) uirc_struct_free(t, IRC_STRUCT_TAG);
|
|
free(t);
|
|
return NULL;
|
|
}
|
|
|
|
IRC_Capability*
|
|
uirc_struct_assm_capability(bool active, const char* name)
|
|
{
|
|
assert(name != NULL);
|
|
IRC_Capability* c = NULL;
|
|
|
|
if ((c = malloc(sizeof(*c))) == NULL) {
|
|
uirc_errno = UIRC_ERR_SYSERR;
|
|
goto cleanup;
|
|
}
|
|
memset(c, 0, sizeof(*c));
|
|
|
|
c->active = active;
|
|
if ((c->name = stringext_strmalloc(name, strlen(name))) == NULL) {
|
|
uirc_errno = UIRC_ERR_SYSERR;
|
|
goto cleanup;
|
|
}
|
|
return c;
|
|
cleanup:
|
|
if (c != NULL) uirc_struct_free(c, IRC_STRUCT_CAPABILITY);
|
|
free(c);
|
|
return NULL;
|
|
}
|
|
#endif /* UIRC_FEATURE_IRCV3 */
|
|
|
|
IRC_User*
|
|
uirc_struct_assm_user(bool service, IRC_Modes modes, const char* nick, const char* user, const char* real, const char* host)
|
|
{
|
|
IRC_User* u = NULL;
|
|
|
|
if ((u = malloc(sizeof(*u))) == NULL) {
|
|
uirc_errno = UIRC_ERR_SYSERR;
|
|
goto cleanup;
|
|
}
|
|
memset(u, 0, sizeof(*u));
|
|
|
|
u->is_service = service;
|
|
u->modes = modes;
|
|
if ((nick != NULL && (u->nick = stringext_strmalloc(nick, strlen(nick))) == NULL)
|
|
|| (user != NULL && (u->user = stringext_strmalloc(user, strlen(user))) == NULL)
|
|
|| (real != NULL && (u->real = stringext_strmalloc(real, strlen(real))) == NULL)
|
|
|| (host != NULL && (u->host = stringext_strmalloc(host, strlen(host))) == NULL)) {
|
|
uirc_errno = UIRC_ERR_SYSERR;
|
|
goto cleanup;
|
|
}
|
|
return u;
|
|
cleanup:
|
|
if (u != NULL) uirc_struct_free(u, IRC_STRUCT_USER);
|
|
free(u);
|
|
return NULL;
|
|
}
|
|
|
|
IRC_Message*
|
|
uirc_struct_assm_message(const char* cmd, bool trailing, unsigned int argc, ...)
|
|
{
|
|
assert(cmd != NULL);
|
|
IRC_Message* m = NULL;
|
|
|
|
if ((m = malloc(sizeof(*m))) == NULL) {
|
|
uirc_errno = UIRC_ERR_SYSERR;
|
|
goto cleanup;
|
|
}
|
|
memset(m, 0, sizeof(*m));
|
|
|
|
m->trailing = trailing;
|
|
if ((m->command = stringext_strmalloc(cmd, strlen(cmd))) == NULL) {
|
|
uirc_errno = UIRC_ERR_SYSERR;
|
|
goto cleanup;
|
|
}
|
|
va_list ap;
|
|
va_start(ap, argc);
|
|
for (unsigned short i = 0; i < IRC_MAXARGS && i < argc; i++) {
|
|
const char* carg = va_arg(ap, const char*);
|
|
if ((m->args[i] = stringext_strmalloc(carg, strlen(carg))) == NULL) {
|
|
uirc_errno = UIRC_ERR_SYSERR;
|
|
goto cleanup;
|
|
}
|
|
}
|
|
va_end(ap);
|
|
return m;
|
|
cleanup:
|
|
if (m != NULL) uirc_struct_free(m, IRC_STRUCT_MESSAGE);
|
|
free(m);
|
|
return NULL;
|
|
}
|
|
|
|
IRC_Buffer*
|
|
uirc_struct_assm_buffer(IRC_Buffer_Type type, IRC_Modes modes, bool subscribed, const char* name, const char* key, const char* topic)
|
|
{
|
|
assert(name != NULL);
|
|
IRC_Buffer* b = NULL;
|
|
|
|
if ((b = malloc(sizeof(*b))) == NULL) {
|
|
uirc_errno = UIRC_ERR_SYSERR;
|
|
goto cleanup;
|
|
}
|
|
memset(b, 0, sizeof(*b));
|
|
|
|
b->subscribed = subscribed;
|
|
b->modes = modes;
|
|
b->type = type;
|
|
if ((b->name = stringext_strmalloc(name, strlen(name))) == NULL || (key != NULL && (b->name = stringext_strmalloc(key, strlen(key))) == NULL)
|
|
|| (topic != NULL && (b->topic = stringext_strmalloc(topic, strlen(topic))) == NULL)) {
|
|
uirc_errno = UIRC_ERR_SYSERR;
|
|
goto cleanup;
|
|
}
|
|
return b;
|
|
cleanup:
|
|
if (b != NULL) uirc_struct_free(b, IRC_STRUCT_BUFFER);
|
|
free(b);
|
|
return NULL;
|
|
}
|
|
|
|
IRC_Network*
|
|
uirc_struct_assm_network(const char* addr, const char* svc, const char* pass)
|
|
{
|
|
IRC_Network* n = NULL;
|
|
|
|
if ((n = malloc(sizeof(*n))) == NULL) {
|
|
uirc_errno = UIRC_ERR_SYSERR;
|
|
goto cleanup;
|
|
}
|
|
memset(n, 0, sizeof(*n));
|
|
|
|
if ((addr != NULL && (n->addr = stringext_strmalloc(addr, strlen(addr))) == NULL)
|
|
|| (svc != NULL && (n->svc = stringext_strmalloc(svc, strlen(svc))) == NULL)
|
|
|| (pass != NULL && (n->pass = stringext_strmalloc(pass, strlen(pass))) == NULL)) {
|
|
uirc_errno = UIRC_ERR_SYSERR;
|
|
goto cleanup;
|
|
}
|
|
return n;
|
|
cleanup:
|
|
uirc_struct_free(n, IRC_STRUCT_NETWORK);
|
|
free(n);
|
|
return NULL;
|
|
}
|