Added credentials parsing
This commit is contained in:
parent
fda9d49998
commit
4928814c9f
1
credentials.txt
Normal file
1
credentials.txt
Normal file
@ -0,0 +1 @@
|
||||
probotic
|
@ -8,6 +8,7 @@
|
||||
|
||||
#define DB_ERROR 100
|
||||
#define IRC_ERROR 200
|
||||
#define CREDS_ERROR 300
|
||||
|
||||
#define ERROR_H_
|
||||
#endif
|
||||
|
@ -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
|
||||
|
@ -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;
|
||||
}
|
||||
|
16
src/main.c
16
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. */
|
||||
|
@ -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);
|
||||
|
Reference in New Issue
Block a user