aboutsummaryrefslogtreecommitdiffstats
path: root/src/util.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/util.c')
-rw-r--r--src/util.c128
1 files changed, 107 insertions, 21 deletions
diff --git a/src/util.c b/src/util.c
index 04c8d25..15e1440 100644
--- a/src/util.c
+++ b/src/util.c
@@ -18,39 +18,125 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
+#include <unistd.h>
+#include <errno.h>
+#include "util.h"
-struct memory
+static const char *
+get_config_filename(void)
{
- char *response;
- size_t size;
-};
+ static const char *ret = NULL;
-/* This function fucking sucks and should use something else. I'll
- * have to rewrite this shit function some day. But for now, it will
- * do the job.
- * TODO: Check for errors
- */
+ if(ret)
+ return ret;
-/* Be aware with buffer overflows, fscanf() */
+ ret = getenv("DEMIURGERC");
+
+ if(!ret)
+ ret = ".demiurgerc";
+
+ return ret;
+}
+
+int config_exists(void)
+{
+ return access(get_config_filename(), R_OK | W_OK);
+}
int
-get_tokens_from_file(char *filename,
- char *instance,
- char *client_id,
- char *client_secret,
- char *access_token)
+store_config(const struct config *config)
{
- FILE *fp = fopen(filename, "r");
- if(fp == NULL)
+ FILE *fp = fopen(get_config_filename(), "w+");
+
+ if (!fp)
return -1;
- fscanf(fp, "instance=%s\n", instance);
- fscanf(fp, "client_id=%s\n", client_id);
- fscanf(fp, "client_secret=%s\n", client_secret);
- fscanf(fp, "access_token=%s\n", access_token);
+
+ fprintf(fp, "instance=%s\n", config->instance);
+ fprintf(fp, "client_id=%s\n", config->client_id);
+ fprintf(fp, "client_secret=%s\n", config->client_secret);
+ fprintf(fp, "access_token=%s\n", config->access_token);
+ fclose(fp);
return 0;
}
+static int
+validate_config(const struct config *config)
+{
+ int ret = 0;
+
+ if(!config->instance[0]) {
+ fprintf(stderr, "Error: missing instance key\n");
+ ret = -1;
+ }
+
+ if(!config->client_id[0]) {
+ fprintf(stderr, "Error: missing client_id key\n");
+ ret = -1;
+ }
+
+ if(!config->client_secret[0]) {
+ fprintf(stderr, "Error: missing client_secret key\n");
+ ret = -1;
+ }
+
+ if(!config->access_token[0]) {
+ fprintf(stderr, "Error: missing access_token key\n");
+ ret = -1;
+ }
+
+ return ret;
+}
+
+int
+load_config(struct config *config)
+{
+ FILE *fp = fopen(get_config_filename(), "r");
+ int i;
+ char line[256];
+
+ memset(config, 0, sizeof(*config));
+
+ for(i = 1; fgets(line, sizeof(line), fp); i++)
+ {
+ char *key, *value;
+
+ /* TODO: cleanup all whitespaces! */
+
+ value = strchr(line, '=');
+
+ if(!value) {
+ fprintf(stderr, "Parse error at %i\n", i);
+ fclose(fp);
+ return -1;
+ }
+
+ key = line;
+ *value = 0;
+ value++;
+
+ /* remove trailing newline */
+ value[strlen(value) - 1] = 0;
+
+ if(!strcmp(key, "instance")) {
+ dm_strncpy(config->instance, value, sizeof(config->instance));
+ } else if(!strcmp(key, "client_id")) {
+ dm_strncpy(config->client_id, value, sizeof(config->client_id));
+ } else if(!strcmp(key, "client_secret")) {
+ dm_strncpy(config->client_secret, value, sizeof(config->client_secret));
+ } else if(!strcmp(key, "access_token")) {
+ dm_strncpy(config->access_token, value, sizeof(config->access_token));
+ } else {
+ fprintf(stderr, "Unknown key %s at %i\n", key, i);
+ continue;
+ }
+ }
+
+ fclose(fp);
+
+ return validate_config(config);
+}
+
size_t
cb(void *data, size_t size, size_t nmemb, void *userp)
{