This commit is contained in:
anon 2023-12-15 23:45:27 +01:00
commit 649867d5a6
6 changed files with 193 additions and 0 deletions

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
.gdb_history
*.o
hibot

37
Makefile Normal file
View File

@ -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

75
source/bot.h Normal file
View File

@ -0,0 +1,75 @@
#ifndef BOT_H
#include <libircclient.h>
#include <string.h>
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

7
source/config.inc Normal file
View File

@ -0,0 +1,7 @@
#ifndef CONFIG_INC
const char * const username = PROGRAM_NAME;
const char * const password = "";
#define CONFIG_INC
#endif

22
source/log.h Normal file
View File

@ -0,0 +1,22 @@
#ifndef LOG_H
#include <stdio.h>
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

49
source/main.c Normal file
View File

@ -0,0 +1,49 @@
#define PROGRAM_NAME "hibot"
#include "config.inc"
char * channel;
char * server;
char * port;
#include <stdio.h>
FILE * log_file;
#include "log.h"
//#include "xilight.h"
#include "bot.h"
const char help_message[] =
PROGRAM_NAME " <server>:<port> <channel>\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;
}