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 2374e6d..b8f637b 100644 --- a/include/irc.h +++ b/include/irc.h @@ -2,6 +2,8 @@ #include +#include "parse.h" + extern irc_session_t * session; extern irc_callbacks_t callbacks; @@ -9,7 +11,7 @@ extern char const * channel; extern char * current_username; -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/include/irccolors.h b/include/irccolors.h index ccc5de4..237f376 100644 --- a/include/irccolors.h +++ b/include/irccolors.h @@ -1,16 +1,16 @@ -#define IRC_COLOR_WHITE "\x0300" -#define IRC_COLOR_BLACK "\x0301" -#define IRC_COLOR_BLUE "\x0302" -#define IRC_COLOR_GREEN "\x0303" -#define IRC_COLOR_RED "\x0304" -#define IRC_COLOR_BROWN "\x0305" -#define IRC_COLOR_PURPLE "\x0306" -#define IRC_COLOR_ORANGE "\x0307" -#define IRC_COLOR_YELLOW "\x0308" -#define IRC_COLOR_LIGHT_GREEN "\x0309" -#define IRC_COLOR_TEAL "\x0310" -#define IRC_COLOR_LIGHT_CYAN "\x0311" -#define IRC_COLOR_LIGHT_BLUE "\x0312" -#define IRC_COLOR_PINK "\x0313" -#define IRC_COLOR_GREY "\x0314" -#define IRC_COLOR_LIGHT_GREY "\x0315" +#define IRC_COLOR_WHITE "\x03\x00" +#define IRC_COLOR_BLACK "\x03\x01" +#define IRC_COLOR_BLUE "\x03\x02" +#define IRC_COLOR_GREEN "\x03\x03" +#define IRC_COLOR_RED "\x03\x04" +#define IRC_COLOR_BROWN "\x03\x05" +#define IRC_COLOR_PURPLE "\x03\x06" +#define IRC_COLOR_ORANGE "\x03\x07" +#define IRC_COLOR_YELLOW "\x03\x08" +#define IRC_COLOR_LIGHT_GREEN "\x03\x09" +#define IRC_COLOR_TEAL "\x03\x10" +#define IRC_COLOR_LIGHT_CYAN "\x03\x11" +#define IRC_COLOR_LIGHT_BLUE "\x03\x12" +#define IRC_COLOR_PINK "\x03\x13" +#define IRC_COLOR_GREY "\x03\x14" +#define IRC_COLOR_LIGHT_GREY "\x03\x15" diff --git a/src/irc.c b/src/irc.c index 463c055..0c4bb3a 100644 --- a/src/irc.c +++ b/src/irc.c @@ -113,7 +113,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."); } @@ -123,6 +123,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 28b274d..768abd8 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 134e13b..2cf8dee 100644 --- a/src/parse.c +++ b/src/parse.c @@ -79,8 +79,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);