From 68a65cffdad75e8f4af6870ffd68fc2a1e8eeec5 Mon Sep 17 00:00:00 2001 From: anon Date: Sun, 17 Dec 2023 02:38:41 +0100 Subject: [PATCH] werks --- Makefile | 7 +------ source/bot.h | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++--------- source/main.c | 17 ++++++++++++++++- 3 files changed, 69 insertions(+), 16 deletions(-) diff --git a/Makefile b/Makefile index 56809ee..21e4b85 100644 --- a/Makefile +++ b/Makefile @@ -22,7 +22,7 @@ HEADER := $(addprefix ${SOURCE.d}, ${HEADER}) ${OUT}: ${SOURCE} ${HEADER} ${CC} ${CFLAGS} -o $@ ${SOURCE} ${LDLIBS} -run: +run: ${OUT} ${OUT} irc.rizon.net:6665 "#/g/test" test: ${OUT} @@ -30,8 +30,3 @@ test: ${OUT} clean: -rm ${OUT} - -docs: - for i in documentation/*.md; do \ - kramdown-man "$$i" -o documentation/manual/$$(basename -s .md $$i) ; \ - done diff --git a/source/bot.h b/source/bot.h index 82b2eff..1b002fd 100644 --- a/source/bot.h +++ b/source/bot.h @@ -3,6 +3,9 @@ #include #include +#include + +extern syntax_setter_t syntax_functions[]; irc_session_t * session; irc_callbacks_t callbacks; @@ -11,6 +14,17 @@ void ircmsg(const char * const message) { irc_cmd_msg(session, channel, message); } +static +language_t translate_language(const char * const language) { + if (!strcmp(language, "C") || !strcmp(language, "C++")) { + return C; + } else if (!strcmp(language, "ADA")) { + return ADA; + } + return -1; +} + +// XXX: msg ChanServ IDENTIFY? void event_connect(irc_session_t * session, const char * event, const char * origin, @@ -21,7 +35,6 @@ void event_connect(irc_session_t * session, (void)params; (void)count; - // XXX: msg ChanServ IDENTIFY? log_notice("IRC connection secured."); irc_cmd_join(session, channel, 0); char * buffer; @@ -39,15 +52,45 @@ void event_privmsg(irc_session_t * session, (void)event; (void)count; - char * buffer = (char *)origin; - while (*(buffer++) != '!') { ; } - asprintf(&buffer, "From %.*s:", (int)(buffer - origin)-1, origin); - ircmsg(buffer); - free(buffer); + char * const message_guard = strdup(params[1]); + char * message = message_guard; + char * terminator; + int is_code = 1; - char * message = strdup(params[1]); - ircmsg(syntax_highlight(message)); - free(message); + if (*message == '!') { + terminator = message; + while (*terminator != ' ') { + if (*terminator == '\0') { + is_code = 0; + break; + } + ++terminator; + } + *terminator = '\0'; + for (char * s = message + 1; *s != '\0'; s++) { + *s = toupper(*s); + } + int l = translate_language(message + 1); + message = terminator + 1; + if (l != -1) { + language = l; + syntax_functions[language](); + } + } + + if (is_code) { + char * buffer = (char *)origin; + while (*(buffer++) != '!') { ; } + asprintf(&buffer, "From %.*s:", (int)(buffer - origin)-1, origin); + ircmsg(buffer); + free(buffer); + + ircmsg(syntax_highlight(message)); + + ircmsg("--"); + } + + free(message_guard); } int connect_bot(const char * const server, const short port) { diff --git a/source/main.c b/source/main.c index 10e095b..876f1c4 100644 --- a/source/main.c +++ b/source/main.c @@ -7,6 +7,15 @@ char * channel; char * server; char * port; +typedef enum { + C, + ADA, +} language_t; + +language_t language = ADA; + +typedef void (*syntax_setter_t)(void); + #include FILE * log_file; @@ -14,6 +23,12 @@ FILE * log_file; #include "syntax.h" #include "bot.h" +syntax_setter_t syntax_functions[] = { + [C] = &syntax_c, + [ADA] = &syntax_ada, +}; + + const char help_message[] = PROGRAM_NAME " : \n" ; @@ -39,7 +54,7 @@ signed main(int argc, char * * argv) { log_file = stdout; - syntax_c(); + syntax_functions[language](); connect_bot(server, port_i); connection_loop();