Replace old creds parser with pair parser

This commit is contained in:
Emil 2023-08-03 20:36:49 -06:00
parent 8f5db62a49
commit 76f6c41e4b
7 changed files with 110 additions and 145 deletions

View File

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

33
include/cred_data.h Normal file
View File

@ -0,0 +1,33 @@
#ifndef CRED_DATA_H_
#define PARAMS_COUNT 5
enum cred_param_ids_e
{
USERNAME,
PASSWORD,
CHANNEL,
SERVER,
PORT
};
char const * cred_names[] =
{
"username",
"password",
"channel",
"server",
"port"
};
size_t const cred_names_len[] =
{
8,
8,
7,
6,
4
};
#define CRED_DATA_H_
#endif

View File

@ -14,7 +14,8 @@ extern creds_t creds;
DECL char * slurp(const char * fn);
DECL int parse_pair(char const * buf, size_t const len);
DECL void parse_command(char * cmd);
DECL void clean_creds(void);
DECL void creds_free_password(void);
DECL void creds_free_rest(void);
#define CREDS_PARSER_H
#endif

View File

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

View File

@ -123,14 +123,21 @@ init(void)
callbacks.event_channel = event_channel;
session = irc_create_session(&callbacks);
if (!session)
{ ERR(1, "Error creating IRC session"); }
{
ERRMSG("Error creating IRC session");
goto fail;
}
assert(creds.username != NULL);
assert(creds.server != NULL);
irc_connect(session,
creds.server, creds.port, creds.password,
creds.username, creds.username, creds.username);
creds_free_password();
atexit(rope);
return 0;
fail:
creds_free_password();
return 1;
}
DECL int

View File

@ -91,10 +91,14 @@ main (int argc,
fprintf(stderr, "file: %s\n", authfile);
PERROR(1);
}
authfile = argv[1];
if (parse_pair(buf, strlen(buf)))
{ ERR(CREDS_ERROR, "Cannot parse creds"); }
{
free(buf);
creds_free_rest();
ERR(CREDS_ERROR, "Cannot parse creds");
}
free(buf);
atexit(creds_free_rest);
}
++argv; --argc;
}
@ -103,12 +107,11 @@ main (int argc,
}
}
#ifndef NDEBUG
fprintf(stderr, "-- %s:%d %s username:%s pass:%s --\n",
fprintf(stderr, "-- server:'%s:%d' channel:'%s' username:'%s' pass:%s --\n",
creds.server, creds.port, creds.channel, creds.username,
creds.password ? creds.password : "NULL");
creds.password);
#endif /* NDEBUG */
/* initialization (1 means bad , 0 mean good > ; ) */
if (init())
{ return 1; }

View File

@ -21,33 +21,14 @@
#include <stdlib.h>
#include <string.h>
#include "cred_data.h"
#include "parse.h"
#include "error.h"
#define PARAMS_COUNT 2
#define ARRAY_SIZE(x) ((size_t) (sizeof x) / (size_t) (sizeof *x))
creds_t creds = {0};
enum cred_param_ids_e
{
USERNAME,
PASSWORD,
CHANNEL,
SERVER,
PORT
};
char const * cred_names[] =
{
"username",
"password",
"channel",
"server",
"port"
};
DECL char *
slurp(const char * fn)
{
@ -59,10 +40,12 @@ slurp(const char * fn)
fseek(fp, 0, SEEK_END);
len = ftell(fp);
rewind(fp);
b = malloc(len);
b = malloc(len+2);
if (b)
{ fread(b, 1, len, fp); }
fclose(fp);
b[len+1] = '\0';
return b;
}
else
@ -82,6 +65,8 @@ parse_command(char * cmd)
if (cmd[i] == '\0')
{
/* no arguments */
if (strcmp(cmd, "kill") == 0)
{ exit(1); }
if (strcmp(cmd, "remind") == 0)
{ ircmsg("%s: No current assignment", current_username); }
else if (strcmp(cmd, "next") == 0)
@ -105,137 +90,77 @@ parse_command(char * cmd)
}
DECL int
parse_pair(char const * buf, size_t const len)
parse_pair(char const * buf, size_t len)
{
size_t i, f, x;
for (i = 0; i < len; ++i)
/* fprintf(stderr, "ENT len:%ld buf:%sEOF\n", len, buf); */
for (i = 0; buf[i] &&
i < len; ++i)
{
if (buf[i] == '=')
{
/* X macro for handling this data may be better */
for (f = 0; f < ARRAY_SIZE(cred_names[i]); ++i)
++i;
for (f = 0, x = 0; f < PARAMS_COUNT; ++f)
{
fprintf(stderr, "ARRAY_SIZE(cred_names[i]) = %d\n",
ARRAY_SIZE(cred_names[i]));
if (strncmp(buf, cred_names[i], ARRAY_SIZE(cred_names[i])) == 0)
/* fprintf(stderr, "x%ld, i%ld, %s\n", x, i, buf); */
/* X macro for handling this data may be better */
if (strncmp(buf, cred_names[f], cred_names_len[f]) == 0)
{
/* fprintf(stderr, "f%ld:len%ld:%s\n", f, cred_names_len[f], */
/* cred_names[f]); fflush(stderr); */
buf += i;
x = i;
while (i != '\0' ||
i != '\n')
{ ++i; }
x = i - x;
switch (i)
while (buf[x] != '\0')
{
case USERNAME: creds.username = strndup(buf,x); break;
case PASSWORD: creds.password = strndup(buf,x); break;
case CHANNEL: creds.channel = strndup(buf,x); break;
case SERVER: creds.server = strndup(buf,x); break;
case PORT: creds.port = atoi(buf); break;
if (buf[x] == '\n')
{
len -= i; i = 0; break;
}
++x;
}
while (i != '\0' ||
i != '\n');
switch (f)
{
case USERNAME: creds.username = strndup(buf,x); break;
case PASSWORD: creds.password = strndup(buf,x); break;
case CHANNEL: creds.channel = strndup(buf,x); break;
case SERVER: creds.server = strndup(buf,x); break;
case PORT: creds.port = atoi(buf); break;
}
if (x + 2 < len)
{ buf += x + 1; }
else
{ return 1; }
goto next;
}
#ifndef NDEBUG
else
{ fprintf(stderr, "DISCARDED\n"); }
#endif /* !NDEBUG */
}
}
next:;
}
return 0;
}
#if 0
DECL int
parse_creds(char const * creds_file)
{
/* don't put declarations in loops */
FILE * stream;
char * values[PARAMS_COUNT];
char * line = NULL;
char const * val;
size_t nread = 0;
size_t param_len;
size_t val_len;
size_t i;
#ifndef NDEBUG
size_t column = 1;
#endif /* !NDEBUG */
creds.username = NULL;
creds.password = NULL;
stream = fopen(creds_file, "r");
if (stream == NULL)
{ PERROR(1); }
memset(values, 0, sizeof(char *) * PARAMS_COUNT);
while (getline(&line, &nread, stream) > 0)
{
for (i = 0; i < PARAMS_COUNT; i++)
{
/* Ideally this should be optimized out as the literals are known constants */
param_len = strlen(cred_param_names_g[i]);
/* TODO lookahead for a = b notation? */
if (!strncmp(cred_param_names_g[i], line, param_len) &&
line[param_len] == '=')
{
/* Starts with the current parameter specifier */
val = line + param_len + 1;
val_len = strlen(val);
/* Duplicates and gets rid of a newline */
values[i] = strndup(val, val[val_len - 1] == '\n' ? val_len - 1 : val_len);
break;
}
#ifndef NDEBUG
else
{ fprintf(stderr, "line %ld '%s' DISCARDED\n", column, line); }
++column;
#endif /* !NDEBUG */
}
free(line);
line = NULL;
nread = 0;
}
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)
{
ERRMSG("Could not retrieve username");
goto fail;
}
fclose(stream);
return 0;
/* Everything will be released under this halting failure */
fail:
fclose(stream);
return 1;
}
#endif /* 0 */
#define FULL_FREE(obj) \
do \
{ \
if ((obj)) \
{ \
memset((obj), '\0', strlen((obj))); \
free(obj); \
(obj) = NULL; \
} \
} while (0)
void
clean_creds(void)
creds_free_password(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);
}
void
creds_free_rest(void)
{
FULL_FREE(creds.username);
/* FULL_FREE(creds.password); */
FULL_FREE(creds.channel);
FULL_FREE(creds.server);
#undef FULL_FREE
}
#undef FULL_FREE