summaryrefslogtreecommitdiff
path: root/src/sms_config.c
blob: 2d44e18213f53ca3d62353471c48acd9202d62c5 (plain)
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <termios.h>
#include <linux/limits.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <errno.h>

#include "global.h"

#if HAVE_LIBYAML
#include <yaml.h>
#endif

#include "atcmd.h"
#include "utils.h"
#include "log.h"

#if HAVE_LIBYAML

struct config_info {
	yaml_parser_t parser;
	char *filename;
	FILE *file;

	yaml_event_t event;

	int map_level;
	char *section;
	char *key;
	char *value;

	int complete;
};

static int boolean_value(const char *str)
{
	return !strcmp(str, "true") ? true : false;
}

static int config_close(struct config_info *config)
{
	yaml_parser_delete(&config->parser);
	fclose(config->file);
	free(config->filename);
	free(config->section);
	free(config->key);
	free(config->value);

	return 0;
}

static int config_open(struct config_info *config, const char *filename)
{
	int err;

	memset(config, 0, sizeof(*config));

	config->filename = strdup(filename);
	if (!config->filename) {
		log_error("out of memory");
	}

	config->file = fopen(config->filename, "r");
	if (!config->file) {
		log_error("open config %s: %m", config->filename);
		return -1;
	}

	err = yaml_parser_initialize(&config->parser);
	if (!err) {
		log_error("yaml_parser_initialize");
		fclose(config->file);
		return -1;
	}

	yaml_parser_set_input_file(&config->parser, config->file);

	config->complete = false;

	return 0;
}

static int config_next_event(struct config_info *config)
{
	int err;

	err = yaml_parser_parse(&config->parser, &config->event);
	if (!err) {
		log_error("yaml parse error");
		return -1;
	}

	return 0;
}

static void config_delete_event(struct config_info *config)
{
	yaml_event_delete(&config->event);
}

static int config_set_core_value(const char *key, const char *value)
{
	log_debug("try setting core.%s to %s", key, value);

	if (!strcmp("verbose", key)) {
		Global.core.verbose = boolean_value(value);
	} else if (!strcmp("interactive", key)) {
		Global.core.interactive = boolean_value(value);
	} else if (!strcmp("sms-init", key)) {
		Global.core.sms_init = boolean_value(value);
	} else if (!strcmp("baud-rate", key)) {
		Global.core.baud_rate = atoi(value);
		Global.core.baud_rate = value_to_baud(Global.core.baud_rate);
	} else if (!strcmp("read-timeout", key)) {
		Global.core.read_timeout = atoi(value);
	} else if (!strcmp("device", key)) {
		free(Global.core.device);
		Global.core.device = strdup(value);
	} else if (!strcmp("msg-store-read", key)) {
		free(Global.core.msg_store_read);
		Global.core.msg_store_read = strdup(value);
	} else if (!strcmp("msg-store-send", key)) {
		free(Global.core.msg_store_send);
		Global.core.msg_store_send = strdup(value);
	} else if (!strcmp("msg-store-new", key)) {
		free(Global.core.msg_store_new);
		Global.core.msg_store_new = strdup(value);
	} else if (!strcmp("pb-store", key)) {
		free(Global.core.pb_store);
		Global.core.pb_store = strdup(value);
	} else if (!strcmp("editor", key)) {
		free(Global.core.editor);
		Global.core.editor = strdup(value);
	} else if (!strcmp("edit-file", key)) {
		free(Global.core.edit_file);
		Global.core.edit_file = strdup(value);
	}

	return 0;
}

static int config_set_smtp_value(const char *key, const char *value)
{
	log_debug("try setting smtp.%s to '%s'", key, value);

	if (!strcmp("server", key)) {
		free(Global.smtp.server);
		Global.smtp.server = strdup(value);
	} else if (!strcmp("port", key)) {
		Global.smtp.port = atoi(value);
	} else if (!strcmp("user", key)) {
		free(Global.smtp.user);
		Global.smtp.user = strdup(value);
	} else if (!strcmp("passwd", key)) {
		free(Global.smtp.passwd);
		Global.smtp.passwd = strdup(value);
	} else if (!strcmp("encryption", key)) {
		free(Global.smtp.encryption);
		Global.smtp.encryption = strdup(value);
	}

	return 0;
}

static int config_set_send_email_value(const char *key, const char *value)
{
	log_debug("try setting send-email.%s to %s", key, value);

	if (!strcmp("domain", key)) {
		free(Global.send_email.domain);
		Global.send_email.domain = strdup(value);
	}

	return 0;
}

static int config_set_user_value(const char *key, const char *value)
{
	log_debug("try setting user.%s to %s", key, value);

	if (!strcmp("name", key)) {
		free(Global.user.name);
		Global.user.name = strdup(value);
	} else if (!strcmp("email", key)) {
		free(Global.user.email);
		Global.user.email = strdup(value);
	}

	return 0;
}

static int config_handle_event(struct config_info *config)
{
	switch (config->event.type) {
	case YAML_STREAM_START_EVENT:
		return 0;
	case YAML_STREAM_END_EVENT:
		return 0;
	case YAML_DOCUMENT_START_EVENT:
		return 0;
	case YAML_DOCUMENT_END_EVENT:
		config->complete = true;
		return 0;

	case YAML_SCALAR_EVENT:
		if (config->map_level == 1) {
			config->section = strdup((char *) config->event.data.scalar.value);
			if (!config->section) {
				log_error("out of memory");
				return -1;
			}
			log_debug("section: %s", config->section);

			return 0;
		} else if (config->map_level != 2) {
			log_debug("bad map level: %d", config->map_level);
			return -1;
		}

		if (!config->key) {
			config->key = strdup((char *) config->event.data.scalar.value);
			if (!config->key) {
				log_error("out of memory");
				return -1;
			}

			return 0;
		} else if (!config->value) {
			config->value = strdup((char *) config->event.data.scalar.value);
			if (!config->value) {
				log_error("out of memory");
				return -1;
			}
		}

		if (config->key && config->value) {
			if (!strcmp(config->section, "core")) {
				config_set_core_value(config->key, config->value);
			} else if (!strcmp(config->section, "smtp")) {
				config_set_smtp_value(config->key, config->value);
			} else if (!strcmp(config->section, "send-email")) {
				config_set_send_email_value(config->key, config->value);
			} else if (!strcmp(config->section, "user")) {
				config_set_user_value(config->key, config->value);
			}

			free(config->key);
			config->key = NULL;

			free(config->value);
			config->value = NULL;
		}

		return 0;

	case YAML_MAPPING_START_EVENT:
		config->map_level++;

		return 0;

	case YAML_MAPPING_END_EVENT:
		free(config->section);
		config->section = NULL;

		free(config->key);
		config->key = NULL;

		free(config->value);
		config->value = NULL;

		config->map_level--;

		return 0;

	default:
		log_error("event type: %d", config->event.type);
		return -1;
	}
}

static int _sms_config_load(const char *filename)
{
	int err;
	struct config_info config;
	struct stat sbuf;

	err = stat(filename, &sbuf);
	if (err < 0) {
		if (errno == ENOENT) {
			log_debug("sms config file missing");
			return 0;
		}

		return -1;
	}

	err = config_open(&config, filename);
	if (err < 0) {
		log_error("open config failed");
		return -1;
	}

	while (1) {
		err = config_next_event(&config);
		if (err < 0) {
			log_error("handle event failed");
			break;
		}

		err = config_handle_event(&config);
		if (err < 0) {
			log_error("handle event failed");
			config_delete_event(&config);
			break;
		}
		config_delete_event(&config);

		if (config.complete) {
			break;
		}
	}

	int complete = config.complete;

	err = config_close(&config);
	if (err < 0) {
		log_error("close config failed");
		return -1;
	}

	if (!complete) {
		log_error("config load did not complete");
		return -1;
	}

	log_debug("config loaded");

	return 0;
}

#define SMS_CONFIG_FILE		".smsconfig"

int sms_config_load(void)
{
	int err;
	char *cp;
	char prev[PATH_MAX];
	char *home;
	char *config;

	config = getenv("SMS_CONFIG");
	if (config) {
		return _sms_config_load(config);
	}

	home = getenv("HOME");
	if (!home) {
		log_error("HOME is not set");
		return -1;
	}

	cp = getcwd(prev, sizeof(prev));
	if (!cp) {
		log_error("getcwd %m");
		return -1;
	}

	chdir(home);

	err = _sms_config_load(SMS_CONFIG_FILE);

	chdir(prev);

	return err;
}
#else
int sms_config_load(void)
{
	return 0;
}
#endif