Add argc for message allocator and add tag list manager

This commit is contained in:
Alex D. 2021-03-06 22:23:55 +00:00
parent f0f0d95078
commit 7b7cd1b7cc
Signed by: caskd
GPG Key ID: F92BA85F61F4C173
8 changed files with 225 additions and 10 deletions

View File

@ -92,6 +92,9 @@ if (BUILD_TESTS)
enable_testing()
add_test(NAME "FullLoop" COMMAND "fullloop")
add_test(NAME "EarlyTrail" COMMAND "earlytrail")
add_test(NAME "OptCRLF" COMMAND "optcrlf")
add_test(NAME "ManualAssemblers" COMMAND "manassm")
add_test(NAME "Allocators" COMMAND "allocs")
endif()
add_library(uirc ${UIRC_SOURCE})

View File

@ -28,16 +28,19 @@ IRC_Tag* uirc_malloc_tag(const char* key, const char* value);
IRC_Capability* uirc_malloc_capability(const char* name);
#endif /* UIRC_FEATURE_IRCV3 */
IRC_User* uirc_malloc_user(const char* nick, const char* user, const char* real, const char* host);
IRC_Message* uirc_malloc_message(const char* command, ...);
IRC_Message* uirc_malloc_message(const char* command, int argc, ...);
IRC_Buffer* uirc_malloc_buffer(const char* name, const char* topic, const char* key);
IRC_Network* uirc_malloc_network(const char* addr, const char* svc, const char* pass);
bool uirc_list_add_buffer(IRC_Network* net, IRC_Buffer* buf);
bool uirc_list_add_user(IRC_Buffer* buf, IRC_User* usr);
bool uirc_list_add_message(IRC_Buffer* buf, IRC_Message* msg);
bool uirc_list_add_tag(IRC_Message* msg, IRC_Tag* tag);
bool uirc_list_remove_buffer(IRC_Network* net, IRC_Buffer* buf);
bool uirc_list_remove_user(IRC_Buffer* buf, IRC_User* usr);
bool uirc_list_remove_message(IRC_Buffer* buf, IRC_Message* msg);
bool uirc_list_remove_tag(IRC_Message* msg, IRC_Tag* tag);
#ifdef UIRC_FEATURE_IRCV3
void uirc_free_tag(IRC_Tag* t);

View File

@ -99,7 +99,7 @@ uirc_malloc_user(const char* nick, const char* user, const char* real, const cha
}
IRC_Message*
uirc_malloc_message(const char* command, ...)
uirc_malloc_message(const char* command, int argc, ...)
{
IRC_Message* ret;
assert(command != NULL);
@ -113,14 +113,12 @@ uirc_malloc_message(const char* command, ...)
return NULL;
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);
ret = NULL;
}
va_start(ap, argc);
for (unsigned short i = 0; i < IRC_MAXARGS && i < argc; i++) {
const char* carg = va_arg(ap, const char*);
if ((ret->args[i] = malloc_string(carg, strlen(carg))) == NULL) {
uirc_free_message(ret);
ret = NULL;
}
}
va_end(ap);

View File

@ -68,6 +68,20 @@ uirc_list_add_user(IRC_Buffer* buf, IRC_User* usr)
return 1;
}
bool
uirc_list_add_tag(IRC_Message* msg, IRC_Tag* tag)
{
assert(msg != NULL);
if (tag == NULL) return 0;
llist_t* tmp = NULL;
if ((tmp = allocate_linked_list_elem(0)) == NULL) return 0;
if (msg->tag_list != NULL) connect_linked_list_elem(msg->tag_list, tmp);
else
msg->tag_list = tmp;
tmp->content = tag;
return 1;
}
bool
uirc_list_remove_buffer(IRC_Network* net, IRC_Buffer* buf)
{
@ -118,3 +132,20 @@ uirc_list_remove_message(IRC_Buffer* buf, IRC_Message* msg)
}
return 1;
}
bool
uirc_list_remove_tag(IRC_Message* msg, IRC_Tag* tag)
{
assert(msg != NULL);
assert(tag != NULL);
for (llist_t* cur = msg->tag_list; cur != NULL;) {
if (cur->content == tag) {
if (cur == msg->tag_list) msg->tag_list = (cur->prev != NULL) ? cur->prev : cur->next;
uirc_free_tag(tag);
remove_linked_list_elem(cur);
break;
}
if ((cur = cur->next) == NULL) return 0;
}
return 1;
}

View File

@ -7,3 +7,12 @@ target_link_libraries(fullloop uirc)
add_executable(earlytrail earlytrail.c common.c)
target_link_libraries(earlytrail uirc)
add_executable(optcrlf optcrlf.c common.c)
target_link_libraries(optcrlf uirc)
add_executable(manassm manassm.c common.c)
target_link_libraries(manassm uirc)
add_executable(allocs allocs.c common.c)
target_link_libraries(allocs uirc)

65
tests/allocs.c Normal file
View File

@ -0,0 +1,65 @@
/*
* 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 "common.h" // expect() print_irc_message()
#include "uirc.h" // uirc_* IRC_*
#include <stdbool.h> // true
#include <stdio.h> // fprintf()
#include <stdlib.h> // EXIT_*
#include <string.h> // strcmp()
#define NICKN "nick"
#define USERN "someuser"
#define HOSTN "pop.com"
#define TAGKEY "time"
#define TAGVAL "2020-09-02T20:10:30.000Z"
#define CMD "NOTICE"
#define ARG1 "*"
#define ARG2 "piss off"
int
main(void)
{
IRC_User* u = uirc_malloc_user(NICKN, USERN, NULL, HOSTN);
if (strcmp(NICKN, u->nick) != 0 || strcmp(USERN, u->user) != 0 || strcmp(HOSTN, u->host) != 0) {
fprintf(stderr, "User allocator returned unexpected results\n");
return EXIT_FAILURE;
}
uirc_free_user(u);
free(u);
IRC_Tag* t = uirc_malloc_tag(TAGKEY, TAGVAL);
if (strcmp(TAGKEY, t->key) != 0 || strcmp(TAGVAL, t->value) != 0) {
fprintf(stderr, "Tag allocator returned unexpected results\n");
return EXIT_FAILURE;
}
uirc_free_tag(t);
free(t);
IRC_Message* m = uirc_malloc_message(CMD, 2, ARG1, ARG2);
m->trailing = true;
if (strcmp(m->command, CMD) != 0 || strcmp(m->args[0], ARG1) != 0 || strcmp(m->args[1], ARG2) != 0) {
fprintf(stderr, "Message allocator returned unexpected results\n");
return EXIT_FAILURE;
}
uirc_free_message(m);
free(m);
return EXIT_SUCCESS;
}

62
tests/manassm.c Normal file
View File

@ -0,0 +1,62 @@
/*
* 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 "common.h" // expect() print_irc_message()
#include "uirc.h" // uirc_* IRC_*
#include <stdio.h> // fprintf()
#include <stdlib.h> // EXIT_*
#include <string.h> // strcmp()
#define TAGKEY "wow"
#define TAGKEY2 "wowcum"
#define TAGVAL "wow2"
#define TAGVAL2 "wow3rd"
int
main(void)
{
IRC_Message* m = uirc_malloc_message("NONE", 0);
uirc_list_add_tag(m, uirc_malloc_tag(TAGKEY, TAGVAL));
uirc_list_add_tag(m, uirc_malloc_tag(TAGKEY2, TAGVAL2));
IRC_Tag* t = m->tag_list->next->content;
if (strcmp(TAGKEY2, t->key) != 0 || strcmp(TAGVAL2, t->value) != 0) {
fprintf(stderr, "List allocator didn't allocate things right\n");
return EXIT_FAILURE;
}
t = m->tag_list->content;
if (strcmp(TAGKEY, t->key) != 0 || strcmp(TAGVAL, t->value) != 0) {
fprintf(stderr, "List allocator didn't allocate things right\n");
return EXIT_FAILURE;
}
uirc_list_remove_tag(m, m->tag_list->content);
t = m->tag_list->content;
if (strcmp(TAGKEY2, t->key) != 0 || strcmp(TAGVAL2, t->value) != 0) {
fprintf(stderr, "List remover fucked up\n");
return EXIT_FAILURE;
}
uirc_free_message(m);
free(m);
return EXIT_SUCCESS;
}

44
tests/optcrlf.c Normal file
View File

@ -0,0 +1,44 @@
/*
* 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 "common.h" // expect() print_irc_message()
#include "uirc.h" // uirc_* IRC_*
#include <assert.h> // assert()
#include <stdio.h> // printf()
#include <stdlib.h> // EXIT_SUCCESS
#include <string.h> // strlen()
int
main(void)
{
const char* str =
"NOTICE #public :yo does someone want to watch Redo of Healer with me?"; // This should parse just fine as CRLF is a delimiter and nothing
// important for the message itself
IRC_Message* m = NULL;
if ((m = uirc_tokenizer_message(str)) == NULL) {
fprintf(stderr, "Failed to convert string to IRC_Message struct.\n");
return EXIT_FAILURE;
}
print_irc_message(m);
uirc_free_message(m);
free(m);
return EXIT_SUCCESS;
}