Added credentials parsing

This commit is contained in:
fall-leaf 2023-08-02 23:37:28 +03:00
parent fda9d49998
commit 4928814c9f
6 changed files with 23 additions and 12 deletions

1
credentials.txt Normal file
View File

@ -0,0 +1 @@
probotic

View File

@ -8,6 +8,7 @@
#define DB_ERROR 100
#define IRC_ERROR 200
#define CREDS_ERROR 300
#define ERROR_H_
#endif

View File

@ -2,12 +2,14 @@
#include <libircclient.h>
#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

View File

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

View File

@ -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. */

View File

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