commit 649867d5a6b812cb8b6e5dc0044d44e821e8fd25 Author: anon Date: Fri Dec 15 23:45:27 2023 +0100 init diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5f6b0d0 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.gdb_history +*.o +hibot diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..56809ee --- /dev/null +++ b/Makefile @@ -0,0 +1,37 @@ +.PHONY: clean run test + +WRAP := valgrind --track-origins=yes --leak-check=full --show-leak-kinds=all +CFLAGS += -fno-builtin -I/usr/include/libircclient/ +ifeq ($(DEBUG), 1) + CFLAGS += -Wall -Wextra -Wpedantic + CFLAGS += -DDEBUG -O0 -ggdb -fno-inline +else + CFLAGS += -O3 -fno-stack-protector -fno-exceptions -fno-rtti +endif + +LDLIBS := -lircclient + +OUT := hibot + +SOURCE.d := source/ +SOURCE := main.c +SOURCE := $(addprefix ${SOURCE.d}, ${SOURCE}) +HEADER := config.inc log.h bot.h +HEADER := $(addprefix ${SOURCE.d}, ${HEADER}) + +${OUT}: ${SOURCE} ${HEADER} + ${CC} ${CFLAGS} -o $@ ${SOURCE} ${LDLIBS} + +run: + ${OUT} irc.rizon.net:6665 "#/g/test" + +test: ${OUT} + ${WRAP} ${OUT} irc.rizon.net:6665 "#/g/test" + +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 new file mode 100644 index 0000000..07b9ad9 --- /dev/null +++ b/source/bot.h @@ -0,0 +1,75 @@ +#ifndef BOT_H + +#include + +#include + +irc_session_t * session; +irc_callbacks_t callbacks; + +void ircmsg(const char * const message) { + irc_cmd_msg(session, channel, message); +} + +void event_connect(irc_session_t * session, + const char * event, + const char * origin, + const char ** params, + unsigned int count) { + (void)event; + (void)origin; + (void)params; + (void)count; + /* msg ChanServ IDENTIFY? */ + log_notice("IRC connection secured."); + irc_cmd_join(session, channel, 0); + ircmsg("TEST"); +} + +void event_channel(irc_session_t * session, + char const * event, + char const * origin, + char const ** params, + unsigned int count) { + (void)session; + (void)event; + (void)origin; + (void)count; + /* */ + char const * message = params[1]; + ircmsg(message); +} + +int connect_bot(const char * const server, const short port) { + memset(&callbacks, 0, sizeof(callbacks)); + callbacks.event_connect = event_connect; + callbacks.event_channel = event_channel; + session = irc_create_session(&callbacks); + + if (!session) { + log_error("Error creating IRC session."); + return 1; + } else { + log_notice("IRC Session initialized."); + } + + irc_connect(session, + server, + port, + password, + username, + username, + username + ); +} + +int connection_loop(void) { + if (irc_run(session) != 0) { + log_error("Error running IRC session\n" + "Possible issue: bad URL, no network connection, bad port, refused connection."); + } + return 0; +} + +#define BOT_H +#endif diff --git a/source/config.inc b/source/config.inc new file mode 100644 index 0000000..b0e95ec --- /dev/null +++ b/source/config.inc @@ -0,0 +1,7 @@ +#ifndef CONFIG_INC + +const char * const username = PROGRAM_NAME; +const char * const password = ""; + +#define CONFIG_INC +#endif diff --git a/source/log.h b/source/log.h new file mode 100644 index 0000000..d6029b1 --- /dev/null +++ b/source/log.h @@ -0,0 +1,22 @@ +#ifndef LOG_H + +#include + +static +void log(const char * const message, const char * const color) { + fputs(color, log_file); + fputs(message, log_file); + fputs("\033[0m\n", log_file); + fflush(log_file); +} + +void log_notice(const char * const message) { + log("", message); +} + +void log_error(const char * const message) { + log("\033[33m", message); +} + +#define LOG_H +#endif diff --git a/source/main.c b/source/main.c new file mode 100644 index 0000000..9522814 --- /dev/null +++ b/source/main.c @@ -0,0 +1,49 @@ +#define PROGRAM_NAME "hibot" + +#include "config.inc" + +char * channel; +char * server; +char * port; + +#include +FILE * log_file; + +#include "log.h" +//#include "xilight.h" +#include "bot.h" + +const char help_message[] = + PROGRAM_NAME " : \n" +; + +signed main(int argc, char * * argv) { + if (argc != 3) { + goto USAGE_ERROR; + } + + channel = argv[2]; + server = strdup(argv[1]); + port = strchr(server, ':'); + if (port) { + *port = '\0'; + } else { + goto USAGE_ERROR; + } + ++port; + int port_i = 0; + if(!sscanf(port, "%hd", &port_i)) { + goto USAGE_ERROR; + } + + log_file = stdout; + + connect_bot(server, 6665); + connection_loop(); + + return 0; + + USAGE_ERROR: + puts(help_message); + return 1; +}