From 201e1167f9d95628f6dcebbead9f004a31eb4ed1 Mon Sep 17 00:00:00 2001 From: Emil Date: Thu, 3 Aug 2023 00:16:39 -0600 Subject: [PATCH] Revised main and globalization of cred --- credentials.txt | 1 - include/error.h | 14 ++++++-- include/irc.h | 4 +-- include/parse.h | 9 +++-- install.sh | 2 +- rizon_cred.txt | 4 +++ src/irc.c | 22 +++++++++---- src/main.c | 100 ++++++++++++++++++++++++++++++++++++++++++-------------- src/parse.c | 59 +++++++++++++++++++-------------- 9 files changed, 150 insertions(+), 65 deletions(-) delete mode 100644 credentials.txt create mode 100644 rizon_cred.txt diff --git a/credentials.txt b/credentials.txt deleted file mode 100644 index 6efad6d..0000000 --- a/credentials.txt +++ /dev/null @@ -1 +0,0 @@ -username=probotic \ No newline at end of file diff --git a/include/error.h b/include/error.h index 0fe16f8..8595431 100644 --- a/include/error.h +++ b/include/error.h @@ -2,9 +2,17 @@ #include -#define ERR(ret,msg) do { fputs(msg "\n", stderr); return (ret); } while (0) -#define PERROR(ret,name) do { perror(name); return (ret); } while (0) -#define ERRMSG(msg) fputs(msg "\n", stderr) +#define ERR(ret,msg) \ + do { fputs(PROGN ": " msg "\n", stderr); return (ret); } while (0) + +#define ERRFMT(ret,fmt,...) \ + do { fprintf(stderr, PROGN ": " fmt "\n", __VA_ARGS__); return (ret); } while (0) + +#define PERROR(ret) \ + do { perror(PROGN); return (ret); } while (0) + +#define ERRMSG(msg) \ + fputs(msg "\n", stderr) #define DB_ERROR 100 #define IRC_ERROR 200 diff --git a/include/irc.h b/include/irc.h index 5311f2c..deae888 100644 --- a/include/irc.h +++ b/include/irc.h @@ -7,11 +7,9 @@ extern irc_session_t * session; extern irc_callbacks_t callbacks; -extern char const * channel; - extern char * current_username; -DECL int init(creds_t const * creds, char const * server, int port); +DECL int init(void); #define IRC_H_ #endif diff --git a/include/parse.h b/include/parse.h index bf85ca4..adafb8a 100644 --- a/include/parse.h +++ b/include/parse.h @@ -4,11 +4,16 @@ typedef struct { char * username; char * password; + char * channel; + char * server; + int port; } creds_t; +extern creds_t creds; + DECL void parse_command(char * cmd); -DECL int parse_creds(creds_t * creds, char const * creds_file); -DECL void clean_creds(creds_t * creds); +DECL int parse_creds(char const * creds_file); +DECL void clean_creds(void); #define CREDS_PARSER_H #endif diff --git a/install.sh b/install.sh index 543d043..62fadf6 100644 --- a/install.sh +++ b/install.sh @@ -10,5 +10,5 @@ TARGET=${TARGET-/opt/probotic} useradd probotic -r -s /sbin/nologin -d $TARGET mkdir -p $TARGET install -g probotic -o probotic -m 744 \ - $DIR/bootstrap/probotic_data.sqlite probotic -v $TARGET + $DIR/details.cfg $DIR/bootstrap/probotic_data.sqlite probotic -v $TARGET chown probotic:probotic $TARGET -R diff --git a/rizon_cred.txt b/rizon_cred.txt new file mode 100644 index 0000000..1b2cf97 --- /dev/null +++ b/rizon_cred.txt @@ -0,0 +1,4 @@ +username=probotic +server=irc.rizon.net +port=6667 +channel=#/g/chad diff --git a/src/irc.c b/src/irc.c index eedb6d6..bae91d3 100644 --- a/src/irc.c +++ b/src/irc.c @@ -18,6 +18,7 @@ */ +#include #include #include #include @@ -32,10 +33,9 @@ irc_session_t * session; irc_callbacks_t callbacks; -const char * channel; char * current_username; -#define IRCMSG(msg) irc_cmd_msg(session, channel, msg) +#define IRCMSG(msg) irc_cmd_msg(session, creds.channel, msg) DECL char * get_username(const char * origin) @@ -63,7 +63,7 @@ ircmsg(const char* fmt, { exit(1); } puts(fmtdmsg); - irc_cmd_msg(session, channel, fmtdmsg); + IRCMSG(fmtdmsg); free(fmtdmsg); va_end(args); @@ -80,7 +80,8 @@ event_connect(irc_session_t * session, (void) origin; (void) params; (void) count; - irc_cmd_join(session, channel, 0); + /* msg ChanServ IDENTIFY? */ + irc_cmd_join(session, creds.channel, 0); } DECL void @@ -113,7 +114,7 @@ event_channel(irc_session_t * session, } int -init(creds_t const * credentials, char const * server, int port) +init(void) { if(api_init()) { ERR(DB_ERROR, "Error initializing database."); } @@ -123,6 +124,15 @@ init(creds_t const * credentials, char const * server, int port) session = irc_create_session(&callbacks); if (!session) { ERR(1, "Error creating IRC session"); } - irc_connect(session, server, port, credentials->password, credentials->username, credentials->username, credentials->username); + assert(creds.username != NULL); + assert(creds.server != NULL); + irc_connect(session, + creds.server, creds.port, creds.password, + creds.username, creds.username, creds.username); + atexit(rope); + /* 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 ea2fe0a..0134f67 100644 --- a/src/main.c +++ b/src/main.c @@ -25,38 +25,88 @@ #include "irc.h" #include "api.h" -#define CREDS_FILE "./credentials.txt" +#define CREDS_FILE "./creds.txt" -/* args: server port channel [username] - defaults to probotic */ +#define EQOP(var,val,off) \ + do \ + { \ + if (argc > 1) \ + { \ + var = val; \ + ++argv; --argc; \ + } \ + else \ + { goto help; } \ + } while (0) + +void +help(void) +{ + fprintf(stderr, + PROGN ": usage\n" + "-server SERVER\n" + "-port PORT\n" + "-username USERNAME\n" + "-password PASSW0RD\n" + "-auth FILE\n"); +} int -main(int const argc, - char const ** argv) +main (int argc, + char ** argv) { - - /* Usage */ - if (argc != 4) - { ERR(1, "server port channel"); } - /* Arguments */ - creds_t credentials = {0}; - char const * server = argv[1]; - int const port = atoi(argv[2]); - channel = argv[3]; - - if (parse_creds(&credentials, CREDS_FILE)) - { ERR(CREDS_ERROR, "Cannot parse credentials"); } - + char const * authfile = CREDS_FILE; + if (argc > 1) + { + char * arg; + while (++argv, --argc) + { + arg = *argv; + if (*arg == '-') + { + ++arg; + if (strcmp(arg, "version") == 0) + { return 0; } + else if (strcmp(arg, "help") == 0) + { goto help; } + if (argc < 2) + { goto nop; } + if (strcmp(arg, "server") == 0) + { creds.server = argv[1]; } + else if (strcmp(arg, "port") == 0) + { creds.port = atoi(argv[1]); } + else if (strcmp(arg, "channel") == 0) + { creds.channel = argv[1]; } + else if (strcmp(arg, "username") == 0) + { creds.username = argv[1]; } + else if (strcmp(arg, "password") == 0) + { creds.password = argv[1]; } + else if (strcmp(arg, "auth") == 0) + { + authfile = argv[1]; + if (parse_creds(authfile)) + { ERR(CREDS_ERROR, "Cannot parse creds"); } + } + ++argv; --argc; + } + else + { nop: ERRFMT(1, "Oprand without option '%s'", arg); } + } + } #ifndef NDEBUG - fprintf(stderr, "-- %s:%d %s username:%s pass:%s --\n", server, port, channel, credentials.username, credentials.password ? credentials.password : "NULL"); + fprintf(stderr, "-- %s:%d %s username:%s pass:%s --\n", + creds.server, creds.port, creds.channel, creds.username, + creds.password ? creds.password : "NULL"); #endif /* NDEBUG */ /* initialization (1 means bad , 0 mean good > ; ) */ - if (init(&credentials, server, port)) + if (init()) { return 1; } - atexit(rope); - /* 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; + + return init(); +help: + help(); + return 1; } + +#undef EQOP diff --git a/src/parse.c b/src/parse.c index a21d134..4299909 100644 --- a/src/parse.c +++ b/src/parse.c @@ -26,16 +26,23 @@ #define PARAMS_COUNT 2 +creds_t creds = {0}; + enum cred_param_ids_e { USERNAME, - PASSWORD + PASSWORD, + CHANNEL, + SERVER, + PORT }; -/* TODO: move = to the handler */ char const * cred_param_names_g[] = { "username", - "password" + "password", + "channel", + "server", + "port" }; DECL void @@ -54,9 +61,9 @@ parse_command(char * cmd) else if (strcmp(cmd, "next") == 0) { ircmsg("%s: No future assignments", current_username); } else if (strcmp(cmd, "dump") == 0) - { ircmsg("%s: All projects"); } + { ircmsg("%s: All projects", current_username); } else if (strcmp(cmd, "reroll") == 0) - { ircmsg("%s: No more rerolls possible.", current_username); } + { ircmsg("%s: No more rerolls possible", current_username); } } else { @@ -72,8 +79,7 @@ parse_command(char * cmd) } DECL int -parse_creds(creds_t * creds, - char const * creds_file) +parse_creds(char const * creds_file) { /* don't put declarations in loops */ FILE * stream; @@ -88,12 +94,12 @@ parse_creds(creds_t * creds, size_t column = 1; #endif /* !NDEBUG */ - creds->username = NULL; - creds->password = NULL; + creds.username = NULL; + creds.password = NULL; stream = fopen(creds_file, "r"); if (stream == NULL) - { PERROR(1,PROGN); } + { PERROR(1); } memset(values, 0, sizeof(char *) * PARAMS_COUNT); @@ -126,33 +132,38 @@ parse_creds(creds_t * creds, line = NULL; nread = 0; } - creds->username = values[USERNAME]; - creds->password = values[PASSWORD]; + creds.username = values[USERNAME]; + creds.password = values[PASSWORD]; + creds.channel = values[CHANNEL]; + creds.server = values[SERVER]; + creds.port = atoi(values[PORT]); + atexit(clean_creds); /* Check empty but required paramters */ - if (!creds->username) + if (!creds.username) { - ERRMSG("Could not parse bot login"); + ERRMSG("Could not retrieve username"); goto fail; } fclose(stream); return 0; - /* Releasing everything in cause of a failure */ + /* Everything will be released under this halting failure */ fail: fclose(stream); - clean_creds(creds); return 1; } -void -clean_creds(creds_t * creds) -{ - /* Should we memset these? */ - free(creds->username); - creds->username = NULL; - free(creds->password); - creds->password = NULL; +void +clean_creds(void) +{ +#define FULL_FREE(obj) \ + do { memset(obj, '\0', strlen(obj)); free(obj); obj = NULL; } while (0) + FULL_FREE(creds.username); + FULL_FREE(creds.password); + FULL_FREE(creds.channel); + FULL_FREE(creds.server); +#undef FULL_FREE }