-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.c
196 lines (147 loc) · 3.83 KB
/
util.c
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*
This file is part of toot.
toot 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.
toot 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 toot. 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"
#include <wordexp.h>
static const char *get_config_filename(void)
{
static char *ret = NULL;
if(ret)
return ret;
ret = getenv("TOOTRC");
if(!ret) {
ret = "~/.config/.tootrc";
}
return ret;
}
int
config_exists(void)
{
const char *ret = get_config_filename();
int fileRes = -1;
wordexp_t result;
wordexp( ret, &result, 0 );
fileRes = access(result.we_wordv[0], R_OK | W_OK);
wordfree(&result);
return fileRes;
}
int
store_config(const struct config *config)
{
const char *ret = get_config_filename();
wordexp_t result;
wordexp( ret, &result, 0 );
FILE *fp = fopen(result.we_wordv[0], "w+");
wordfree(&result);
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)
{
int i;
char line[256];
const char *ret = get_config_filename();
wordexp_t result;
wordexp( ret, &result, 0 );
FILE *fp = fopen(result.we_wordv[0], "r");
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);
wordfree(&result);
return -1;
}
key = line;
*value = 0;
value++;
/* remove trailing newline */
if (strlen(value)>0)
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);
wordfree(&result);
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;
}
/* like puts() but writes to stderr */
void
eputs(const char *s)
{
fputs(s,stderr);
}