Revised main and globalization of cred

This commit is contained in:
Emil 2023-08-03 00:16:39 -06:00
parent 71038cee03
commit 201e1167f9
9 changed files with 150 additions and 65 deletions

View File

@ -1 +0,0 @@
username=probotic

View File

@ -2,9 +2,17 @@
#include <stdio.h>
#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

View File

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

View File

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

View File

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

4
rizon_cred.txt Normal file
View File

@ -0,0 +1,4 @@
username=probotic
server=irc.rizon.net
port=6667
channel=#/g/chad

View File

@ -18,6 +18,7 @@
*/
#include <assert.h>
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
@ -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;
}

View File

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

View File

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