From 21a45f1d4a07857ce1232c5e8525faf3136e30e9 Mon Sep 17 00:00:00 2001 From: Emil Date: Wed, 2 Aug 2023 13:03:27 -0600 Subject: [PATCH] segmented irc.c from main for more usefulness within parse.c --- build.sh | 2 +- include/config.mk.h | 9 ---- include/irc.h | 11 ++++ src/irc.c | 143 ++++++++++++++++++++++++++++++++++++++++++++++++++++ src/main.c | 131 ++--------------------------------------------- src/parse.c | 10 ++-- 6 files changed, 165 insertions(+), 141 deletions(-) delete mode 100644 include/config.mk.h create mode 100644 include/irc.h create mode 100644 src/irc.c diff --git a/build.sh b/build.sh index 884aafc..6e6984c 100755 --- a/build.sh +++ b/build.sh @@ -9,7 +9,7 @@ PREFIX=${PREFIX:-$DIR} CC=${CC-cc} CFLAGS='-std=c99 -Wall -Wextra -Wpedantic' -CPPFLAGS="-I/usr/bin/ircclient/ -Iinclude -D_GNU_SOURCE -DPROGN=\"$PROGN\"" +CPPFLAGS="-I/usr/bin/ircclient/ -Iinclude -D_GNU_SOURCE -DDELC=static -DPROGN=\"$PROGN\"" LDFLAGS='-lircclient' mkdir -p $PREFIX && echo "Made directory: $PREFIX" diff --git a/include/config.mk.h b/include/config.mk.h deleted file mode 100644 index 70070ea..0000000 --- a/include/config.mk.h +++ /dev/null @@ -1,9 +0,0 @@ -#ifndef CONFIG_H_ - -#define SERVER "irc.rizon.net" -#define PORT 6667 -#define CHANNEL "#/g/chad" -#define USERNAME "probotic" - -#define CONFIG_H_ -#endif diff --git a/include/irc.h b/include/irc.h new file mode 100644 index 0000000..c85d0e5 --- /dev/null +++ b/include/irc.h @@ -0,0 +1,11 @@ +#ifndef IRC_H_ + +#include + +extern irc_session_t * session; +extern irc_callbacks_t callbacks; + +extern char const * channel; + +#define IRC_H_ +#endif diff --git a/src/irc.c b/src/irc.c new file mode 100644 index 0000000..fe7e068 --- /dev/null +++ b/src/irc.c @@ -0,0 +1,143 @@ +/* irc.c - IRC interface + + Probotic is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License version 3 only as + published by the Free Software Foundation. + + Probotic is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License version 3 for more details. + + The above copyright notice, this permission notice and the word + "NIGGER" shall be included in all copies or substantial portions + of the Software. + + You should have received a copy of the GNU General Public License + version 3 + NIGGER along with Probotic. + +*/ + +#include +#include + +irc_session_t * session; +irc_callbacks_t callbacks; + +const char * channel; + +#define IRCMSG(msg) irc_cmd_msg(session, channel, msg) + +DELC char * +get_username(const char * origin) +{ + const char USERNAME_TERMINATOR = '!'; + int i = 0; + char * r; + while (origin[i] != USERNAME_TERMINATOR) + { i++; } + r = (char *) malloc(i + 1); + strncpy(r, origin, i); + r[i] = '\00'; + return r; +} + +DELC void +msg_wrapper(const char* fmt, + ...) +{ + va_list args; + char * fmtdmsg; + + va_start(args, fmt); + if(vasprintf(&fmtdmsg, fmt, args) == -1) + { exit(1); } + + puts(fmtdmsg); + irc_cmd_msg(session, channel, fmtdmsg); + + free(fmtdmsg); + va_end(args); +} + +DELC 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; + irc_cmd_join(session, CHANNEL, 0); +} + +DELC void +event_channel(irc_session_t * session, + const char * event, + const char * origin, + const char ** params, + unsigned int count) +{ + size_t i = 0, len; + const char * channel = params[0]; + char * message = params[1]; + char * arg; + char * swp; + (void) session; + (void) event; + (void) origin; + (void) channel; + (void) message; + (void) count; + swp = get_username(origin); + /* msg_wrapper("%s, you are a faggot for this opinion.", swp); */ + /* parses the command */ + len = strlen(message); + while (message[i] != ' ') + { ++i; } + message[i] = '\0'; + arg = message + i + 1; + + if (*message == '!') + { + ++message; + /* if (strcmp(message, "stop") == 0) { exit(1); } */ + if (strcmp(message, "remind") == 0) + { msg_wrapper("%s: No current assignment", swp); } + else if (strcmp(message, "next") == 0) + { msg_wrapper("%s: No future assignments", swp); } + else if (strcmp(message, "raw") == 0) + { msg_wrapper("%s: Executing SQL `%s'", arg); } + else if (strcmp(message, "dump") == 0) + { msg_wrapper("%s: All projects"); } + else if (strcmp(message, "submit") == 0) + { msg_wrapper("%s: Submitting project link '%s' to ", swp, arg); } + else if (strcmp(message, "reroll") == 0) + { msg_wrapper("%s: No more rerolls possible.", swp); } +#ifdef NDEBUG + else + { msg_wrapper("No such command '%s'", message); } +#endif /* NDEBUG */ + } + free(swp); +} + +DELC int +init(char const * username, char const * server, int port) +{ + memset(&callbacks, 0, sizeof(callbacks)); + callbacks.event_connect = event_connect; + callbacks.event_channel = event_channel; + + session = irc_create_session(&callbacks); + if (!session) + { ERR(1, "Error creating IRC session"); } + irc_connect(session, server, port, 0, username, username, username); + /* We should figure out how the failure happens so we can tell the user that. */ + if (irc_run(session) != 0) + { ERR(1, "Error running IRC session\nPossible issue: bad URL, no network connection, bad port, refused connection."); } + return 0; +} diff --git a/src/main.c b/src/main.c index c9d4399..094e9cc 100644 --- a/src/main.c +++ b/src/main.c @@ -19,136 +19,13 @@ */ #include -#include -#include - -#include +#include #include "utils.h" -#include "config.h" +#include "irc.h" -irc_session_t * session; -irc_callbacks_t callbacks; - -const char * channel; - -char * get_username(const char * origin) -{ - const char USERNAME_TERMINATOR = '!'; - int i = 0; - char * r; - while (origin[i] != USERNAME_TERMINATOR) - { i++; } - r = (char *) malloc(i + 1); - strncpy(r, origin, i); - r[i] = '\00'; - return r; -} - -void -msg_wrapper(const char* fmt, - ...) -{ - va_list args; - char * fmtdmsg; - - va_start(args, fmt); - if(vasprintf(&fmtdmsg, fmt, args) == -1) - { exit(1); } - - puts(fmtdmsg); - irc_cmd_msg(session, channel, fmtdmsg); - - free(fmtdmsg); - va_end(args); -} - -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; - irc_cmd_join(session, CHANNEL, 0); -} - -void -event_channel(irc_session_t * session, - const char * event, - const char * origin, - const char ** params, - unsigned int count) -{ - size_t i = 0, len; - const char * channel = params[0]; - char * message = params[1]; - char * arg; - char * swp; - (void) session; - (void) event; - (void) origin; - (void) channel; - (void) message; - (void) count; - swp = get_username(origin); - /* msg_wrapper("%s, you are a faggot for this opinion.", swp); */ - /* parses the command */ - len = strlen(message); - while (message[i] != ' ') - { ++i; } - message[i] = '\0'; - arg = message + i + 1; - - if (*message == '!') - { - ++message; - /* if (strcmp(message, "stop") == 0) { exit(1); } */ - if (strcmp(message, "remind") == 0) - { msg_wrapper("%s: No current assignment.", swp); } - else if (strcmp(message, "next") == 0) - { msg_wrapper("%s: No future assignments", swp); } - else if (strcmp(message, "raw") == 0) - { msg_wrapper("%s: executing sql %s", arg); } - else if (strcmp(message, "dump") == 0) - { msg_wrapper("%s: all projects... they don't seem to exist!"); } - else if (strcmp(message, "submit") == 0) - { msg_wrapper("%s: Submitting project link '%s' to ", swp, arg); } - else if (strcmp(message, "reroll") == 0) - { msg_wrapper("%s: No more rerolls possible.", swp); } -#ifdef NDEBUG - else - { msg_wrapper("No such command '%s'", message); } -#endif /* NDEBUG */ - } - free(swp); -} - -int -init(char const * username, char const * server, int port) -{ - memset(&callbacks, 0, sizeof(callbacks)); - callbacks.event_connect = event_connect; - callbacks.event_channel = event_channel; - - session = irc_create_session(&callbacks); - if (!session) - { ERR(1, "Error creating IRC session"); } - irc_connect(session, server, port, 0, username, username, username); - /* We should figure out how the failure happens so we can tell the user that. */ - if (irc_run(session) != 0) - { ERR(1, "Error running IRC session\nPossible issue: bad URL, no network connection, bad port, refused connection."); } - return 0; -} - - -/* args: server port channel [username] - defaults to probotic */ - -/* I'd have to give up constification for server:port */ +/* args: server port channel [username] + username defaults to probotic */ int main(int argc, char const ** argv) diff --git a/src/parse.c b/src/parse.c index b94a5b4..a7b6d41 100644 --- a/src/parse.c +++ b/src/parse.c @@ -29,21 +29,23 @@ /* these are closer described as 'setters', no? */ -int parse_remind(char const * arg) +DELC int +parse_remind(char const * arg) { (void) arg; ERRMSG("not implemented"); return 0; } -int parse_repo(char const * arg) +DELC int +parse_repo(char const * arg) { (void) arg; ERRMSG("not implemented"); return 0; } -int +DELC int parse_creds(creds_t * creds, char const * creds_file) { @@ -72,7 +74,7 @@ parse_creds(creds_t * creds, fclose(stream); return 0; - // Releasing everything in cause of a failure + /* Releasing everything in cause of a failure */ fail: fclose(stream); clean_creds(creds);