aboutsummaryrefslogtreecommitdiffstats
path: root/src/util.c
blob: 15e1440e35d601cfcd60c0e3c64499f4b9bc8f7f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
  This file is part of demiurge.

  demiurge is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  demiurge is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with demiurge.  If not, see <https://www.gnu.org/licenses/>.
*/

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include "util.h"

static const char *
get_config_filename(void)
{
	static const char *ret = NULL;

	if(ret)
		return ret;

	ret = getenv("DEMIURGERC");

	if(!ret)
		ret = ".demiurgerc";

	return ret;
}

int config_exists(void)
{
	return access(get_config_filename(), R_OK | W_OK);
}

int
store_config(const struct config *config)
{
	FILE *fp = fopen(get_config_filename(), "w+");

	if (!fp)
		return -1;

	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)
{
	size_t realsize = size * nmemb;
	struct memory *mem = (struct memory *)userp;

	char *ptr = realloc(mem->response, mem->size + realsize + 1);
	if(ptr == NULL)
		return 0; /* out of memory! */

	mem->response = ptr;
	memcpy(&(mem->response[mem->size]), data, realsize);
	mem->size += realsize;
	mem->response[mem->size] = 0;

	return realsize;
}

size_t
write_data(void *buffer, size_t size, size_t nmemb, void *userp)
{

	memcpy(userp, buffer, nmemb * size);
	return 0;
}

/* like puts() but writes to stderr */

void
eputs(const char *s)
{
	fprintf(stderr, "%s\n", s);
}