summaryrefslogtreecommitdiffstats
path: root/src/daemon/settings.c
blob: 822397d5b5043c9e972e15481143d1f0f0836fb6 (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
/*
 * Usurpation – server daemon settings handling
 *
 * Copyright (C) 2019 Gediminas Jakutis
 * Copyright (C) 2019 Ramūnas Mažeikis
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; version 2.1
 * of the License.
 *
 * This program 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
 */

#define _GNU_SOURCE

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include "settings.h"
#include "settings_private.h"

/* TODO: should be, in this order: looking at options provided via command
	 * line arguments, looking at at /etc/conf.d/usurpation.conf or
	 * using default. Right now just uses defaults.
	 */
void settings_init(void)
{
	atexit(free_strings);
	unset_flag(flag_daemonize);
	settings.port = 6996;
	settings.verboselevel = USURP_VERBOSITY;
	settings.progname = strdup(program_invocation_short_name);
	settings.im_user = strdup("user");
	settings.im_password = strdup("password");
	settings.im_proto = strdup("prpl-irc");
#define macro2str(a) _macro2str(a)
#define _macro2str(a) #a
	setting_readconf("/" macro2str(SYSCONFDIR) "/usurpation.conf");
#undef macro2str
#undef _macro2str
}

int setting_detach(void)
{
	return test_flag(flag_daemonize);
}

int setting_verbose(void)
{
	return settings.verboselevel;
}

unsigned short int setting_port(void)
{
	return settings.port;
}

char *setting_progname(void)
{
	return strdup(settings.progname);
}

char *setting_im_user(void)
{
	return strdup(settings.im_user);
}

char *setting_im_password(void)
{
	return strdup(settings.im_password);
}

char *setting_im_proto(void)
{
	return strdup(settings.im_proto);
}

/* could be a one-liner, but let's make the logic more obvious */
static int test_flag(unsigned int flag)
{
	int ret;

	ret = settings.flags & flag;
	ret = !!ret;

	return ret;
}

static void set_flag(unsigned int flag)
{
	settings.flags |= flag;
}

static void unset_flag(unsigned int flag)
{
	settings.flags &= ~flag;
}

/* TODO: semi-stub! */
static int setting_readconf(const char * const path)
{
	struct stat filestat;
	char *buf;
	char *line;
	char *entry;
	char *value;
	char *line_state;
	char *token_state;
	int fd;


	if ((fd = open(path, O_RDONLY)) == -1) {
		if (USURP_VERBOSITY >= 2) {
			perror("error while opening the configuration file");
			return 1;
		}
	}

	fstat(fd, &filestat);
	buf = malloc(filestat.st_size);

	if (read(fd, buf, filestat.st_size) != filestat.st_size) {
		if (USURP_VERBOSITY >= 2) {
			perror("partial read of configuration file detected");
			free(buf);
			return 1;
		}
	}

	line = strtok_r(buf, "\n", &line_state);

	while (line) {
		if (line[0] != '#') {
			entry = strtok_r(line, "=", &token_state);
			value = strtok_r(NULL, "=", &token_state);
			setting_handle_config_entry(entry, value);
		}

		line = strtok_r(NULL, "\n", &line_state);
	}

	free(buf);

	return 0;
}

static int setting_handle_config_entry(const char * const entry, const char * const value)
{
	size_t i;

	for (i = 0; i < sizeof(ent_table)/sizeof(*ent_table); ++i) {
		if (!(strcmp(entry, ent_table[i].name))) {
			ent_table[i].set(value);
			return 0;
		}
	}

	return 1;
}

static void set_daemonize(const void * const arg)
{
	const char * const a = arg;

	atoi(a) ? set_flag(flag_daemonize) : unset_flag(flag_daemonize);
}

static void set_port(const void * const arg)
{
	const char * const a = arg;

	settings.port = strtoul(a, NULL, 0);
}

static void set_verbosity(const void * const arg)
{
	const char * const a = arg;

	settings.verboselevel = strtol(a, NULL, 0);
}

static void set_progname(const void * const arg)
{
	const char * const a = arg;
	free(settings.progname);
	settings.progname = strdup(a);
}

static void set_im_user(const void * const arg)
{
	const char * const a = arg;
	free(settings.im_user);
	settings.im_user = strdup(a);
}

static void set_im_password(const void * const arg)
{
	const char * const a = arg;
	free(settings.im_password);
	settings.im_password = strdup(a);
}

static void set_im_proto(const void * const arg)
{
	const char * const a = arg;
	free(settings.im_proto);
	settings.im_proto = strdup(a);
}

static void free_strings(void)
{
	free(settings.progname);
	free(settings.im_user);
	free(settings.im_password);
	free(settings.im_proto);
}