Revised main and globalization of cred
This commit is contained in:
parent
71038cee03
commit
201e1167f9
@ -1 +0,0 @@
|
|||||||
username=probotic
|
|
@ -2,9 +2,17 @@
|
|||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
#define ERR(ret,msg) do { fputs(msg "\n", stderr); return (ret); } while (0)
|
#define ERR(ret,msg) \
|
||||||
#define PERROR(ret,name) do { perror(name); return (ret); } while (0)
|
do { fputs(PROGN ": " msg "\n", stderr); return (ret); } while (0)
|
||||||
#define ERRMSG(msg) fputs(msg "\n", stderr)
|
|
||||||
|
#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 DB_ERROR 100
|
||||||
#define IRC_ERROR 200
|
#define IRC_ERROR 200
|
||||||
|
@ -7,11 +7,9 @@
|
|||||||
extern irc_session_t * session;
|
extern irc_session_t * session;
|
||||||
extern irc_callbacks_t callbacks;
|
extern irc_callbacks_t callbacks;
|
||||||
|
|
||||||
extern char const * channel;
|
|
||||||
|
|
||||||
extern char * current_username;
|
extern char * current_username;
|
||||||
|
|
||||||
DECL int init(creds_t const * creds, char const * server, int port);
|
DECL int init(void);
|
||||||
|
|
||||||
#define IRC_H_
|
#define IRC_H_
|
||||||
#endif
|
#endif
|
||||||
|
@ -4,11 +4,16 @@ typedef struct
|
|||||||
{
|
{
|
||||||
char * username;
|
char * username;
|
||||||
char * password;
|
char * password;
|
||||||
|
char * channel;
|
||||||
|
char * server;
|
||||||
|
int port;
|
||||||
} creds_t;
|
} creds_t;
|
||||||
|
|
||||||
|
extern creds_t creds;
|
||||||
|
|
||||||
DECL void parse_command(char * cmd);
|
DECL void parse_command(char * cmd);
|
||||||
DECL int parse_creds(creds_t * creds, char const * creds_file);
|
DECL int parse_creds(char const * creds_file);
|
||||||
DECL void clean_creds(creds_t * creds);
|
DECL void clean_creds(void);
|
||||||
|
|
||||||
#define CREDS_PARSER_H
|
#define CREDS_PARSER_H
|
||||||
#endif
|
#endif
|
||||||
|
@ -10,5 +10,5 @@ TARGET=${TARGET-/opt/probotic}
|
|||||||
useradd probotic -r -s /sbin/nologin -d $TARGET
|
useradd probotic -r -s /sbin/nologin -d $TARGET
|
||||||
mkdir -p $TARGET
|
mkdir -p $TARGET
|
||||||
install -g probotic -o probotic -m 744 \
|
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
|
chown probotic:probotic $TARGET -R
|
||||||
|
4
rizon_cred.txt
Normal file
4
rizon_cred.txt
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
username=probotic
|
||||||
|
server=irc.rizon.net
|
||||||
|
port=6667
|
||||||
|
channel=#/g/chad
|
22
src/irc.c
22
src/irc.c
@ -18,6 +18,7 @@
|
|||||||
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include <assert.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdarg.h>
|
#include <stdarg.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
@ -32,10 +33,9 @@
|
|||||||
irc_session_t * session;
|
irc_session_t * session;
|
||||||
irc_callbacks_t callbacks;
|
irc_callbacks_t callbacks;
|
||||||
|
|
||||||
const char * channel;
|
|
||||||
char * current_username;
|
char * current_username;
|
||||||
|
|
||||||
#define IRCMSG(msg) irc_cmd_msg(session, channel, msg)
|
#define IRCMSG(msg) irc_cmd_msg(session, creds.channel, msg)
|
||||||
|
|
||||||
DECL char *
|
DECL char *
|
||||||
get_username(const char * origin)
|
get_username(const char * origin)
|
||||||
@ -63,7 +63,7 @@ ircmsg(const char* fmt,
|
|||||||
{ exit(1); }
|
{ exit(1); }
|
||||||
|
|
||||||
puts(fmtdmsg);
|
puts(fmtdmsg);
|
||||||
irc_cmd_msg(session, channel, fmtdmsg);
|
IRCMSG(fmtdmsg);
|
||||||
|
|
||||||
free(fmtdmsg);
|
free(fmtdmsg);
|
||||||
va_end(args);
|
va_end(args);
|
||||||
@ -80,7 +80,8 @@ event_connect(irc_session_t * session,
|
|||||||
(void) origin;
|
(void) origin;
|
||||||
(void) params;
|
(void) params;
|
||||||
(void) count;
|
(void) count;
|
||||||
irc_cmd_join(session, channel, 0);
|
/* msg ChanServ IDENTIFY? */
|
||||||
|
irc_cmd_join(session, creds.channel, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
DECL void
|
DECL void
|
||||||
@ -113,7 +114,7 @@ event_channel(irc_session_t * session,
|
|||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
init(creds_t const * credentials, char const * server, int port)
|
init(void)
|
||||||
{
|
{
|
||||||
if(api_init())
|
if(api_init())
|
||||||
{ ERR(DB_ERROR, "Error initializing database."); }
|
{ 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);
|
session = irc_create_session(&callbacks);
|
||||||
if (!session)
|
if (!session)
|
||||||
{ ERR(1, "Error creating IRC 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;
|
return 0;
|
||||||
}
|
}
|
||||||
|
100
src/main.c
100
src/main.c
@ -25,38 +25,88 @@
|
|||||||
#include "irc.h"
|
#include "irc.h"
|
||||||
#include "api.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
|
int
|
||||||
main(int const argc,
|
main (int argc,
|
||||||
char const ** argv)
|
char ** argv)
|
||||||
{
|
{
|
||||||
|
char const * authfile = CREDS_FILE;
|
||||||
/* Usage */
|
if (argc > 1)
|
||||||
if (argc != 4)
|
{
|
||||||
{ ERR(1, "server port channel"); }
|
char * arg;
|
||||||
/* Arguments */
|
while (++argv, --argc)
|
||||||
creds_t credentials = {0};
|
{
|
||||||
char const * server = argv[1];
|
arg = *argv;
|
||||||
int const port = atoi(argv[2]);
|
if (*arg == '-')
|
||||||
channel = argv[3];
|
{
|
||||||
|
++arg;
|
||||||
if (parse_creds(&credentials, CREDS_FILE))
|
if (strcmp(arg, "version") == 0)
|
||||||
{ ERR(CREDS_ERROR, "Cannot parse credentials"); }
|
{ 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
|
#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 */
|
#endif /* NDEBUG */
|
||||||
|
|
||||||
/* initialization (1 means bad , 0 mean good > ; ) */
|
/* initialization (1 means bad , 0 mean good > ; ) */
|
||||||
if (init(&credentials, server, port))
|
if (init())
|
||||||
{ return 1; }
|
{ return 1; }
|
||||||
atexit(rope);
|
|
||||||
/* We should figure out how the failure happens so we can tell the user that. */
|
return init();
|
||||||
if (irc_run(session) != 0)
|
help:
|
||||||
{ ERR(1, "Error running IRC session\nPossible issue: bad URL,"
|
help();
|
||||||
" no network connection, bad port, refused connection."); }
|
return 1;
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#undef EQOP
|
||||||
|
59
src/parse.c
59
src/parse.c
@ -26,16 +26,23 @@
|
|||||||
|
|
||||||
#define PARAMS_COUNT 2
|
#define PARAMS_COUNT 2
|
||||||
|
|
||||||
|
creds_t creds = {0};
|
||||||
|
|
||||||
enum cred_param_ids_e
|
enum cred_param_ids_e
|
||||||
{
|
{
|
||||||
USERNAME,
|
USERNAME,
|
||||||
PASSWORD
|
PASSWORD,
|
||||||
|
CHANNEL,
|
||||||
|
SERVER,
|
||||||
|
PORT
|
||||||
};
|
};
|
||||||
|
|
||||||
/* TODO: move = to the handler */
|
|
||||||
char const * cred_param_names_g[] = {
|
char const * cred_param_names_g[] = {
|
||||||
"username",
|
"username",
|
||||||
"password"
|
"password",
|
||||||
|
"channel",
|
||||||
|
"server",
|
||||||
|
"port"
|
||||||
};
|
};
|
||||||
|
|
||||||
DECL void
|
DECL void
|
||||||
@ -54,9 +61,9 @@ parse_command(char * cmd)
|
|||||||
else if (strcmp(cmd, "next") == 0)
|
else if (strcmp(cmd, "next") == 0)
|
||||||
{ ircmsg("%s: No future assignments", current_username); }
|
{ ircmsg("%s: No future assignments", current_username); }
|
||||||
else if (strcmp(cmd, "dump") == 0)
|
else if (strcmp(cmd, "dump") == 0)
|
||||||
{ ircmsg("%s: All projects"); }
|
{ ircmsg("%s: All projects", current_username); }
|
||||||
else if (strcmp(cmd, "reroll") == 0)
|
else if (strcmp(cmd, "reroll") == 0)
|
||||||
{ ircmsg("%s: No more rerolls possible.", current_username); }
|
{ ircmsg("%s: No more rerolls possible", current_username); }
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@ -72,8 +79,7 @@ parse_command(char * cmd)
|
|||||||
}
|
}
|
||||||
|
|
||||||
DECL int
|
DECL int
|
||||||
parse_creds(creds_t * creds,
|
parse_creds(char const * creds_file)
|
||||||
char const * creds_file)
|
|
||||||
{
|
{
|
||||||
/* don't put declarations in loops */
|
/* don't put declarations in loops */
|
||||||
FILE * stream;
|
FILE * stream;
|
||||||
@ -88,12 +94,12 @@ parse_creds(creds_t * creds,
|
|||||||
size_t column = 1;
|
size_t column = 1;
|
||||||
#endif /* !NDEBUG */
|
#endif /* !NDEBUG */
|
||||||
|
|
||||||
creds->username = NULL;
|
creds.username = NULL;
|
||||||
creds->password = NULL;
|
creds.password = NULL;
|
||||||
|
|
||||||
stream = fopen(creds_file, "r");
|
stream = fopen(creds_file, "r");
|
||||||
if (stream == NULL)
|
if (stream == NULL)
|
||||||
{ PERROR(1,PROGN); }
|
{ PERROR(1); }
|
||||||
|
|
||||||
memset(values, 0, sizeof(char *) * PARAMS_COUNT);
|
memset(values, 0, sizeof(char *) * PARAMS_COUNT);
|
||||||
|
|
||||||
@ -126,33 +132,38 @@ parse_creds(creds_t * creds,
|
|||||||
line = NULL;
|
line = NULL;
|
||||||
nread = 0;
|
nread = 0;
|
||||||
}
|
}
|
||||||
creds->username = values[USERNAME];
|
creds.username = values[USERNAME];
|
||||||
creds->password = values[PASSWORD];
|
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 */
|
/* Check empty but required paramters */
|
||||||
if (!creds->username)
|
if (!creds.username)
|
||||||
{
|
{
|
||||||
ERRMSG("Could not parse bot login");
|
ERRMSG("Could not retrieve username");
|
||||||
goto fail;
|
goto fail;
|
||||||
}
|
}
|
||||||
|
|
||||||
fclose(stream);
|
fclose(stream);
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
/* Releasing everything in cause of a failure */
|
/* Everything will be released under this halting failure */
|
||||||
fail:
|
fail:
|
||||||
fclose(stream);
|
fclose(stream);
|
||||||
clean_creds(creds);
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
|
||||||
clean_creds(creds_t * creds)
|
|
||||||
{
|
|
||||||
/* Should we memset these? */
|
|
||||||
free(creds->username);
|
|
||||||
creds->username = NULL;
|
|
||||||
|
|
||||||
free(creds->password);
|
void
|
||||||
creds->password = NULL;
|
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
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user