A bit of cleanup and adding joinpart

This commit is contained in:
Alex D. 2020-10-29 21:12:34 +01:00
parent e639f82818
commit 10c2b6f966
Signed by: caskd
GPG Key ID: F92BA85F61F4C173
5 changed files with 90 additions and 21 deletions

View File

@ -17,7 +17,12 @@ endif()
find_library(UIRC_PATH NAMES uirc libuirc REQUIRED)
add_executable(formatirc formatirc.c)
target_link_libraries(formatirc ${UIRC_PATH})
set_property(TARGET formatirc PROPERTY C_STANDARD 99)
install(TARGETS formatirc RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
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})
endmacro()
buildtool(formatirc)
buildtool(joinpart)

21
common.c Normal file
View File

@ -0,0 +1,21 @@
#include <stdio.h>
#include <string.h>
#include "common.h"
#define UIRC_IRCV3
#include "uirc/functions.h"
#include "uirc/types.h"
ssize_t get_buffer_line(char* buf, IRC_Message* parsed)
{
if (buf == NULL || parsed == NULL) return -1;
char* ppoi;
if ((ppoi = strchr(buf, '\n')) != NULL) {
*ppoi = '\0';
if (ppoi > buf && *(ppoi - 1) == '\r') *(ppoi - 1) = '\0';
if (Tok_mesg(buf, parsed) == 1) return ++ppoi - buf;
fprintf(stderr, "warning: received invalid message.\n");
return -1;
}
return 0;
}

7
common.h Normal file
View File

@ -0,0 +1,7 @@
#include <stdio.h>
#define UIRC_IRCV3
#include "uirc/types.h"
ssize_t get_buffer_line(char* buf, IRC_Message* parsed);

View File

@ -3,6 +3,7 @@
#include <string.h>
#include <unistd.h>
#include "common.h"
#define UIRC_IRCV3
#include "uirc/functions.h"
#include "uirc/mappings.h"
@ -15,9 +16,6 @@
#define ANSI_COLOR_GRAY "\x1b[90m"
#define ANSI_COLOR_RESET "\x1b[0m"
int main(void);
ssize_t get_buffer_line(char* buf, IRC_Message* parsed);
int main(void)
{
ssize_t bread = 1, tok;
@ -51,17 +49,3 @@ int main(void)
}
return 0;
}
ssize_t get_buffer_line(char* buf, IRC_Message* parsed)
{
if (buf == NULL || parsed == NULL) return -1;
char* ppoi;
if ((ppoi = strchr(buf, '\n')) != NULL) {
*ppoi = '\0';
if (ppoi > buf && *(ppoi - 1) == '\r') *(ppoi - 1) = '\0';
if (Tok_mesg(buf, parsed) == 1) return ++ppoi - buf;
fprintf(stderr, "warning: received invalid message.\n");
return -1;
}
return 0;
}

52
joinpart.c Normal file
View File

@ -0,0 +1,52 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "common.h"
#define UIRC_IRCV3
#include "uirc/functions.h"
#include "uirc/mappings.h"
#include "uirc/types.h"
#define ANSI_COLOR_RED "\x1b[31m"
#define ANSI_COLOR_GREEN "\x1b[32m"
#define ANSI_COLOR_YELLOW "\x1b[33m"
#define ANSI_COLOR_BLUE "\x1b[34m"
#define ANSI_COLOR_GRAY "\x1b[90m"
#define ANSI_COLOR_RESET "\x1b[0m"
int main(void)
{
ssize_t bread = 1, tok;
size_t pos = 0;
char buffer[513];
IRC_Message out;
for (; bread > 0;) {
if ((bread = read(0, buffer + pos, sizeof(buffer) - 1 - pos)) > 0) {
pos += (size_t)bread;
buffer[pos] = '\0';
if ((tok = get_buffer_line(buffer, &out)) > 0) {
if (out.cmd == JOIN || out.cmd == PART) {
printf((out.cmd == JOIN)
? ANSI_COLOR_GREEN "+++" ANSI_COLOR_RESET " %s " ANSI_COLOR_GREEN "joined" ANSI_COLOR_RESET
" " ANSI_COLOR_BLUE "%s" ANSI_COLOR_RESET
: ANSI_COLOR_YELLOW "---" ANSI_COLOR_RESET " %s " ANSI_COLOR_YELLOW "left" ANSI_COLOR_RESET
" " ANSI_COLOR_BLUE "%s" ANSI_COLOR_RESET,
out.name.nick, out.args[0]);
if (out.tags.time.value != NULL) printf(" at %s", out.tags.time.value);
putchar('\n');
} else if (out.cmd == QUIT) {
printf(ANSI_COLOR_RED "***" ANSI_COLOR_RESET " %s " ANSI_COLOR_RED "quit" ANSI_COLOR_RESET, out.name.nick);
if (out.tags.time.value != NULL) printf(" at %s", out.tags.time.value);
putchar('\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));
}
}
}
return 0;
}