This repository has been archived on 2021-02-08. You can view files and clone it, but cannot push or open issues or pull requests.
uIRCd/src/channels.c

94 lines
2.7 KiB
C

/*
* This file is part of uIRCd. (https://git.redxen.eu/caskd/uIRCd)
* Copyright (c) 2019, 2020 Alex-David Denes
*
* uIRCd 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.
*
* uIRCd 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 uIRCd. If not, see <https://www.gnu.org/licenses/>.
*/
#include "channels.h"
#include "logging.h" // LOG()
#include "memory.h" // allocate_copy()
#include <errno.h> // errno
#include <stdbool.h> // bool
#include <stdio.h> // snprintf()
#include <stdlib.h> // malloc() realloc()
#include <string.h> // strerror()
#include <sys/socket.h> // sockaddr connect()
#include <sys/types.h> // size_t ssize_t socklen_t
int
init_chanarray(Channel** chans)
{
if (chans != NULL && *chans == NULL) {
// Initialise dynamic-sized array with last element as NULL pointer
if ((*chans = malloc(sizeof(Channel))) != NULL) {
memset(*chans, '\0', sizeof(Channel));
} else {
LOG(LOG_WARN, "Could not initialise channel array. " ERRNOFMT, strerror(errno), errno);
return 0;
}
}
return 1;
}
signed int
resize_chanarray(Channel** chans)
{
unsigned int i = get_channelindex(NULL, *chans);
Channel* tmp = NULL;
LOG(LOG_DEBUG, "Found %i existing channels.", i);
if ((tmp = realloc(*chans, (i + 2) * sizeof(Channel))) != NULL) {
*chans = tmp;
tmp[i + 1].name = NULL;
tmp[i + 1].key = NULL;
} else {
LOG(LOG_WARN, "Could not allocate channel struct. " ERRNOFMT, strerror(errno), errno);
return -1;
}
return (signed int) i;
}
int
set_channel(Channel* chan, const char* name, const char* key, bool joined)
{
LOG(LOG_DEBUG, "Setting channel %s", (name == NULL) ? chan->name : name);
if (name != NULL) {
if (!allocate_copy(&chan->name, name)) {
LOG(LOG_WARN, "Couldn't allocate memory for the channel name %s. " ERRNOFMT, name, strerror(errno), errno);
return 0;
}
}
if (key != NULL) {
if (!allocate_copy(&chan->key, key)) {
LOG(LOG_WARN, "Couldn't allocate memory for the channel %s key. " ERRNOFMT, (name == NULL) ? chan->name : name, strerror(errno), errno);
return 0;
}
}
chan->joined = joined;
return 1;
}
unsigned int
get_channelindex(const char* name, Channel* chans)
{
unsigned int i;
for (i = 0; chans != NULL && chans[i].name != NULL; i++) {
if (name != NULL && strcmp(chans[i].name, name) == 0) break;
}
return i;
}