From 619c2e412589bb8914cf9d2882eabe47936c4d9a Mon Sep 17 00:00:00 2001 From: Alex Denes Date: Thu, 7 Jan 2021 11:49:06 +0000 Subject: [PATCH] Add list and fix some stuff --- CMakeLists.txt | 69 +++++++++++++++++++++++++++-------- common.h | 6 +-- joinpart.c => irc-events.c | 4 -- irc-list.c | 35 ++++++++++++++++++ formatirc.c => irc-messages.c | 13 +++---- 5 files changed, 94 insertions(+), 33 deletions(-) rename joinpart.c => irc-events.c (93%) create mode 100644 irc-list.c rename formatirc.c => irc-messages.c (86%) diff --git a/CMakeLists.txt b/CMakeLists.txt index e0eeeef..e4bc348 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,28 +1,65 @@ cmake_minimum_required(VERSION 3.16) -project(irc-toolkit LANGUAGES C) +project( + irc-toolkit + VERSION 2021.01.07 + DESCRIPTION "IRC Protocol helpers" + LANGUAGES C +) +include(GNUInstallDirs) + +OPTION(CODE_ANALYZER "Analyze the code statically" OFF) +OPTION(CODE_COVERAGE "Build with coverage tools" OFF) + +find_library(UIRC_PATH NAMES uirc REQUIRED) + +set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -Werror") if (CMAKE_C_COMPILER_ID STREQUAL "GNU") - add_compile_options(-Wall -Wextra -Wformat-overflow=2 -Wformat-security -Winit-self -Wstrict-overflow=2 -Wstringop-overflow=2 -Walloc-zero -Wduplicated-branches -Wduplicated-cond -Wtrampolines -Wfloat-equal -Wshadow -Wunsafe-loop-optimizations -Wparentheses -pedantic -fanalyzer -fstack-check) + add_compile_options( + -Wall + -Wextra + -Wformat-overflow=2 + -Wformat-security + -Winit-self + -Wstrict-overflow=2 + -Wstringop-overflow=2 + -Walloc-zero + -Wduplicated-branches + -Wduplicated-cond + -Wtrampolines + -Wfloat-equal + -Wshadow + -Wunsafe-loop-optimizations + -Wparentheses + -fstack-check + -pedantic + ) + if (CODE_ANALYZER) + add_compile_options(-fanalyzer) + endif() elseif (CMAKE_C_COMPILER_ID STREQUAL "Clang") - add_compile_options(-Weverything -pedantic -Wno-padded -Wno-disabled-macro-expansion) + add_compile_options( + -Weverything + -Wno-padded + -Wno-disabled-macro-expansion + -pedantic + ) + if (CODE_COVERAGE) + add_compile_options(-fprofile-instr-generate -fcoverage-mapping) + endif() + if (CODE_ANALYZER) + add_compile_options(--analyze) + endif() endif() -OPTION(CMAKE_BUILD_TYPE "Debug") - -if (CMAKE_BUILD_TYPE EQUAL "Debug") - set(COMPILE_OPTIONS "${COMPILE_OPTIONS} -Og") -elseif (CMAKE_BUILD_TYPE EQUAL "Release") - set(COMPILE_OPTIONS "${COMPILE_OPTIONS} -O2 -Werror") -endif() - -find_library(UIRC_PATH NAMES uirc libuirc REQUIRED) - macro(buildtool source) add_executable(${source} ${source}.c common.c) target_link_libraries(${source} ${UIRC_PATH}) set_property(TARGET ${source} PROPERTY C_STANDARD 99) - install(TARGETS ${source} RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}) + install(TARGETS ${source} RUNTIME) endmacro() -buildtool(formatirc) -buildtool(joinpart) +buildtool(irc-list) +buildtool(irc-events) +buildtool(irc-messages) + diff --git a/common.h b/common.h index 920ea69..c50c249 100644 --- a/common.h +++ b/common.h @@ -5,8 +5,7 @@ #include #define UIRC_IRCV3 -#include "uirc/functions.h" -#include "uirc/types.h" +#include "uirc/uirc.h" #define ANSI_COLOR_RED "\x1b[31m" #define ANSI_COLOR_GREEN "\x1b[32m" @@ -15,9 +14,6 @@ #define ANSI_COLOR_GRAY "\x1b[90m" #define ANSI_COLOR_RESET "\x1b[0m" -#define UIRC_IRCV3 -#include "uirc/types.h" - extern char* mIRC_COLORS[16]; ssize_t get_buffer_line(char* buf, IRC_Message* parsed); void print_local_time(const char* irc_time); diff --git a/joinpart.c b/irc-events.c similarity index 93% rename from joinpart.c rename to irc-events.c index 2010f47..4f2f5bb 100644 --- a/joinpart.c +++ b/irc-events.c @@ -4,10 +4,6 @@ #include #include "common.h" -#define UIRC_IRCV3 -#include "uirc/functions.h" -#include "uirc/mappings.h" -#include "uirc/types.h" int main(void) { diff --git a/irc-list.c b/irc-list.c new file mode 100644 index 0000000..0459ca4 --- /dev/null +++ b/irc-list.c @@ -0,0 +1,35 @@ +#include +#include +#include +#include + +#include "common.h" + +int main(void) +{ + ssize_t bread = 1, tok; + size_t pos = 0; + char buffer[513]; + IRC_Message out; + for (; bread > 0;) { + if ((tok = get_buffer_line(buffer, &out)) > 0) { + if (out.cmd == RPL_LISTSTART || out.cmd == RPL_LIST) { + printf(ANSI_COLOR_YELLOW "%s" ANSI_COLOR_RESET "\nTopic: %s\nUsers: %s\n", out.args[1], out.args[3], out.args[2]); + print_local_time(out.tags.time.value); + putchar('\n'); + } else if (out.cmd == RPL_LISTEND) { + printf(ANSI_COLOR_RED "----- END" ANSI_COLOR_RESET "\n"); + } + for (long unsigned int x = 0; x < sizeof(buffer) && *(buffer + tok + x); x++) *(buffer + x) = *(buffer + x + tok); + pos -= (unsigned long)tok; + *(buffer + pos) = '\0'; + memset(&out, '\0', sizeof(out)); + continue; + } + if ((bread = read(0, buffer + pos, sizeof(buffer) - 1 - pos)) > 0) { + pos += (size_t)bread; + buffer[pos] = '\0'; + } + } + return 0; +} diff --git a/formatirc.c b/irc-messages.c similarity index 86% rename from formatirc.c rename to irc-messages.c index 8b17e01..e88a7be 100644 --- a/formatirc.c +++ b/irc-messages.c @@ -4,10 +4,6 @@ #include #include "common.h" -#define UIRC_IRCV3 -#include "uirc/functions.h" -#include "uirc/mappings.h" -#include "uirc/types.h" int main(void) { @@ -18,13 +14,14 @@ int main(void) for (; bread > 0;) { if ((tok = get_buffer_line(buffer, &out)) > 0) { if (out.cmd == PRIVMSG || out.cmd == NOTICE) { - printf((out.name.nick != NULL) ? ANSI_COLOR_RED "%s" ANSI_COLOR_RESET : ANSI_COLOR_GRAY "%s" ANSI_COLOR_RESET, - (out.name.nick != NULL) ? out.name.nick : "unknown"); - putchar('/'); if (out.args[0] != NULL) { - printf(ANSI_COLOR_YELLOW "%s" ANSI_COLOR_RESET, + printf(strchr("#+&!", *out.args[0]) != NULL ? ANSI_COLOR_YELLOW "%s" ANSI_COLOR_RESET + : ANSI_COLOR_GREEN "%s" ANSI_COLOR_RESET, strchr("#+&!", *out.args[0]) != NULL ? out.args[0] : "direct"); } + putchar('/'); + printf((out.name.nick != NULL) ? ANSI_COLOR_RED "%s" ANSI_COLOR_RESET : ANSI_COLOR_GRAY "%s" ANSI_COLOR_RESET, + (out.name.nick != NULL) ? out.name.nick : "unknown"); if (out.args[1] != NULL) printf(": %s", out.args[1]); print_local_time(out.tags.time.value); putchar('\n');