From 4928814c9f2b18d1833e918ab152a822a979c8f8 Mon Sep 17 00:00:00 2001 From: fall-leaf Date: Wed, 2 Aug 2023 23:37:28 +0300 Subject: [PATCH] Added credentials parsing --- credentials.txt | 1 + include/error.h | 1 + include/irc.h | 4 +++- src/irc.c | 4 ++-- src/main.c | 16 +++++++++------- src/parse.c | 9 +++++++-- 6 files changed, 23 insertions(+), 12 deletions(-) create mode 100644 credentials.txt diff --git a/credentials.txt b/credentials.txt new file mode 100644 index 0000000..575c4db --- /dev/null +++ b/credentials.txt @@ -0,0 +1 @@ +probotic \ No newline at end of file diff --git a/include/error.h b/include/error.h index 2ee5c4b..a436e8b 100644 --- a/include/error.h +++ b/include/error.h @@ -8,6 +8,7 @@ #define DB_ERROR 100 #define IRC_ERROR 200 +#define CREDS_ERROR 300 #define ERROR_H_ #endif diff --git a/include/irc.h b/include/irc.h index 0f7a0b0..ab8557d 100644 --- a/include/irc.h +++ b/include/irc.h @@ -2,12 +2,14 @@ #include +#include "parse.h" + extern irc_session_t * session; extern irc_callbacks_t callbacks; extern char const * channel; -DELC int init(char const * username, char const * server, int port); +DELC int init(creds_t const * creds, char const * server, int port); #define IRC_H_ #endif diff --git a/src/irc.c b/src/irc.c index 140ae5e..f6833a9 100644 --- a/src/irc.c +++ b/src/irc.c @@ -131,7 +131,7 @@ event_channel(irc_session_t * session, } int -init(char const * username, char const * server, int port) +init(creds_t const * credentials, char const * server, int port) { if(api_init()) { ERR(DB_ERROR, "Error initializing database."); } @@ -141,6 +141,6 @@ init(char const * username, char const * server, int port) session = irc_create_session(&callbacks); if (!session) { ERR(1, "Error creating IRC session"); } - irc_connect(session, server, port, 0, username, username, username); + irc_connect(session, server, port, credentials->password, credentials->username, credentials->username, credentials->username); return 0; } diff --git a/src/main.c b/src/main.c index 5001258..88b4e6a 100644 --- a/src/main.c +++ b/src/main.c @@ -25,6 +25,8 @@ #include "irc.h" #include "api.h" +#define CREDS_FILE "./credentials.txt" + /* args: server port channel [username] - defaults to probotic */ int @@ -33,23 +35,23 @@ main(int argc, { /* Usage */ - if (argc > 5 || argc < 4) - { ERR(1, "server port channel [username]"); } + if (argc != 4) + { ERR(1, "server port channel"); } /* Arguments */ - char const * username = "probotic"; + creds_t credentials; char const * server = argv[1]; int const port = atoi(argv[2]); channel = argv[3]; - if (argc > 4) - { username = argv[4]; } + if (parse_creds(&credentials, CREDS_FILE)) + { ERR(CREDS_ERROR, "Cannot parse credentials"); } #ifdef NDEBUG - fprintf(stderr, "-- %s:%d %s %s --\n", server, port, channel, username); + fprintf(stderr, "-- %s:%d %s %s %s --\n", server, port, channel, credentials.username, credentials.password ? credentials.password : "NULL"); #endif /* NDEBUG */ /* initialization (1 means bad , 0 mean good > ; ) */ - if (init(username, server, port)) + if (init(&credentials, server, port)) { return 1; } atexit(rope); /* We should figure out how the failure happens so we can tell the user that. */ diff --git a/src/parse.c b/src/parse.c index 41c9781..e1e7682 100644 --- a/src/parse.c +++ b/src/parse.c @@ -46,8 +46,13 @@ parse_creds(creds_t * creds, if (getline(&(creds->password), &nread, stream) < 1) { - ERRMSG("Cannot get password"); - goto fail; + /* Bot credentials file with an empty password is a valid case. + * Considering irc_connect api the pointer to the password should be NULL in that case. + */ + + /* Theoretically it can be allocated with an empty string */ + free(creds->password); + creds->password = NULL; } fclose(stream);