Separate modes and add functions related to them

This commit is contained in:
Alex D. 2021-04-04 21:01:20 +00:00
parent 0096d57380
commit aa1d47cb27
Signed by: caskd
GPG Key ID: F92BA85F61F4C173
9 changed files with 179 additions and 15 deletions

View File

@ -22,6 +22,7 @@ set(UIRC_SOURCE
src/assemblers/message.c
src/assemblers/user.c
src/error/error.c
src/modes/modes.c
src/memory/list.c
src/memory/struct.c
src/tokenizers/message.c
@ -100,6 +101,7 @@ if (BUILD_TESTS)
add_test(NAME "Allocators" COMMAND "allocs")
add_test(NAME "ManualAssemblers" COMMAND "manassm")
add_test(NAME "ErrorNumbers" COMMAND "errno")
add_test(NAME "ModeBitmasks" COMMAND "modes")
endif()
add_library(uirc ${UIRC_SOURCE})

View File

@ -31,9 +31,9 @@ void uirc_struct_free(void* obj, IRC_Struct_Type type);
IRC_Tag* uirc_struct_assm_tag(bool clientbound, const char* key, const char* value);
IRC_Capability* uirc_struct_assm_capability(bool active, const char* name);
#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* uirc_struct_assm_user(bool service, IRC_Modes* modes, const char* nick, const char* user, const char* real, const char* host);
IRC_Message* uirc_struct_assm_message(const char* cmd, bool trailing, unsigned int argc, ...);
IRC_Buffer* uirc_struct_assm_buffer(IRC_Buffer_Type type, IRC_Modes modes, bool subscribed, const char* name, const char* key, const char* topic);
IRC_Buffer* uirc_struct_assm_buffer(IRC_Buffer_Type type, IRC_Modes* modes, bool subscribed, const char* name, const char* key, const char* topic);
IRC_Network* uirc_struct_assm_network(const char* addr, const char* svc, const char* pass);
void* uirc_list_append(llist_t* anchor, void* content);

View File

@ -16,17 +16,30 @@
* along with uIRC. If not, see <https://www.gnu.org/licenses/>.
*/
#include <inttypes.h> // int_least8_t
#include <stdbool.h> // bool
/*! \file */
#ifndef UIRC_GUARD_PUBLIC_MODES
#define UIRC_GUARD_PUBLIC_MODES
/* Mode bitmask positions */
// TODO: Add all modes in this bitmask
#define IRC_MODE_WALLOPS 1
#define IRC_MODE_INVIS 2
typedef bool IRC_Modes[(('z' - 'a')) * 2];
typedef enum {
IRC_MODE_WALLOPS = 'w',
IRC_MODE_INVISIBLE = 'i',
IRC_MODE_AWAY = 'a',
IRC_MODE_RESTRICTED = 'r',
IRC_MODE_OPERATOR = 'o',
IRC_MODE_LOCALOPERATOR = 'O',
IRC_MODE_SERVERNOTICES = 's',
} IRC_Mode;
#define MBMASK(MODEBIT) (1 << MODEBIT)
bool uirc_mode_toggle(char m, IRC_Modes ms);
bool uirc_mode_enable(char m, IRC_Modes ms);
bool uirc_mode_disable(char m, IRC_Modes ms);
void uirc_mode_copy(IRC_Modes d, IRC_Modes const s);
bool uirc_mode_fetch(char m, IRC_Modes ms);
#endif /* UIRC_GUARD_PUBLIC_MODES */

View File

@ -18,6 +18,8 @@
/*! \file */
#include "modes.h"
#include <corelibs/llist.h> // llist_t
#include <inttypes.h> // uintmax_t
#include <stdbool.h> // bool
@ -25,8 +27,7 @@
#ifndef UIRC_GUARD_PUBLIC_TYPES
#define UIRC_GUARD_PUBLIC_TYPES
typedef unsigned short IRC_Modes;
typedef uintmax_t IRC_Counter;
typedef uintmax_t IRC_Counter;
#ifdef UIRC_FEATURE_IRCV3
/*!

View File

@ -18,6 +18,7 @@
#include "errors.h" // uirc_errno
#include "memory.h"
#include "modes.h" // uirc_modes_*
#include "types.h" // IRC_*
#include <assert.h> // assert()
@ -144,7 +145,7 @@ cleanup:
#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)
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;
@ -155,7 +156,7 @@ uirc_struct_assm_user(bool service, IRC_Modes modes, const char* nick, const cha
memset(u, 0, sizeof(*u));
u->is_service = service;
u->modes = modes;
if (modes != NULL) uirc_mode_copy(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)
@ -205,7 +206,7 @@ cleanup:
}
IRC_Buffer*
uirc_struct_assm_buffer(IRC_Buffer_Type type, IRC_Modes modes, bool subscribed, const char* name, const char* key, const char* topic)
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;
@ -217,8 +218,8 @@ uirc_struct_assm_buffer(IRC_Buffer_Type type, IRC_Modes modes, bool subscribed,
memset(b, 0, sizeof(*b));
b->subscribed = subscribed;
b->modes = modes;
b->type = type;
if (modes != NULL) uirc_mode_copy(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;

84
src/modes/modes.c Normal file
View File

@ -0,0 +1,84 @@
/*
* 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 "modes.h"
#include <assert.h> // assert()
#include <ctype.h> // isalpha()
#include <inttypes.h> // int_least8_t
#include <stddef.h> //ptrdiff_t
#if ('b' - 'a' != 1 || 'B' - 'A' != 1 || 'z' - 'a' != 25)
#error "System is not US-ASCII." // This is crucial for the conversions below
#endif
static int_least8_t
uirc_mode_index(char mode)
{
assert(mode != NULL);
int_least8_t index = 0;
if (islower(mode)) {
index += mode - 'a';
} else if (isupper(mode)) {
index += ('z' - 'a'); // We offset the index by the size of the lower characters
index += mode - 'A';
} else
return -1;
return index;
}
bool
uirc_mode_toggle(char m, IRC_Modes ms)
{
int_least8_t i = uirc_mode_index(m);
if (i < 0) return false;
ms[i] = !ms[i];
return true;
}
bool
uirc_mode_enable(char m, IRC_Modes ms)
{
int_least8_t i = uirc_mode_index(m);
if (i < 0) return false;
ms[i] = true;
return true;
}
bool
uirc_mode_disable(char m, IRC_Modes ms)
{
int_least8_t i = uirc_mode_index(m);
if (i < 0) return false;
ms[i] = false;
return true;
}
void
uirc_mode_copy(IRC_Modes d, IRC_Modes const s)
{
for (unsigned long i = 0; i < sizeof(IRC_Modes) / sizeof(*s); i++) d[i] = s[i];
}
bool
uirc_mode_fetch(char m, IRC_Modes ms)
{
int_least8_t i = uirc_mode_index(m);
assert(i != -1);
return ms[i];
}

View File

@ -19,3 +19,6 @@ target_link_libraries(allocs uirc)
add_executable(errno errno.c common.c)
target_link_libraries(errno uirc)
add_executable(modes modes.c common.c)
target_link_libraries(modes uirc)

View File

@ -36,7 +36,9 @@
int
main(void)
{
IRC_User* u = uirc_struct_assm_user(false, 0, NICKN, USERN, NULL, HOSTN);
IRC_Modes modes;
uirc_mode_toggle('a', modes);
IRC_User* u = uirc_struct_assm_user(false, &modes, NICKN, USERN, NULL, HOSTN);
if (u == NULL) {
fprintf(stderr, "Memory allocation failed\n");
return EXIT_FAILURE;
@ -45,6 +47,10 @@ main(void)
fprintf(stderr, "User allocator returned unexpected results\n");
return EXIT_FAILURE;
}
if (!uirc_mode_fetch('a', u->modes)) {
fprintf(stderr, "Modes weren't copied correctly\n");
return EXIT_FAILURE;
}
uirc_struct_free(u, IRC_STRUCT_USER);
free(u);

54
src/tests/modes.c Normal file
View File

@ -0,0 +1,54 @@
/*
* 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 "modes.h" // uirc_mode_*() IRC_Modes
#include <stdio.h> // fprintf() stderr
#include <stdlib.h> // EXIT_*
int
main(void)
{
IRC_Modes modes, cpymodes;
if (!uirc_mode_enable('a', modes) || !modes[0]) {
fprintf(stderr, "Mode enable didn't work or mode was invalid\n");
return EXIT_FAILURE;
}
if (!uirc_mode_enable('A', modes) || !modes[25]) {
fprintf(stderr, "Mode enable didn't work or mode was invalid\n");
return EXIT_FAILURE;
}
uirc_mode_copy(cpymodes, modes);
if (!cpymodes[0] || !cpymodes[25]) {
fprintf(stderr, "Mode copy didn't work\n");
return EXIT_FAILURE;
}
if (!uirc_mode_toggle('a', modes) || modes[0]) {
fprintf(stderr, "Mode toggle didn't work or mode was invalid\n");
return EXIT_FAILURE;
}
if (!uirc_mode_disable('a', cpymodes) || cpymodes[0]) {
fprintf(stderr, "Mode disable didn't work or mode was invalid\n");
return EXIT_FAILURE;
}
if (!cpymodes[25]) {
fprintf(stderr, "Mode was toggled off incorrectly\n");
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}