summaryrefslogtreecommitdiffstats
path: root/src/service.cpp
blob: 87a9bc28a36375c652cb269a651d0d5846293ab1 (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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
/*
    Copyright (C) 2005-2009  Michel de Boer <michel@twinklephone.com>

    This program 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 2 of the License, or
    (at your option) any later version.

    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 General Public License for more details.

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

#include <cassert>
#include <sys/stat.h>
#include <unistd.h>
#include <fstream>
#include <iostream>
#include "service.h"
#include "log.h"
#include "userintf.h"
#include "util.h"

#define FLD_CF_ALWAYS			"cf_always"
#define FLD_CF_BUSY			"cf_busy"
#define FLD_CF_NOANSWER			"cf_noanswer"
#define FLD_DND				"dnd"
#define FLD_AUTO_ANSWER			"auto_answer"

void t_service::lock() {
	mtx_service.lock();
}

void t_service::unlock() {
	mtx_service.unlock();
}

t_service::t_service(t_user *user) {
	user_config = user;

	// Call redirection
	cf_always_active = false;
	cf_busy_active = false;
	cf_noanswer_active = false;

	// Do not disturb
	dnd_active = false;
	
	// Auto answer
	auto_answer_active = false;
	
	string msg;
	(void)read_config(msg);
}

bool t_service::multiple_services_active(void) {
	int num_services = 0;
	
	if (is_cf_active()) num_services++;
	if (is_dnd_active()) num_services++;
	if (is_auto_answer_active()) num_services++;
	
	if (num_services > 1) return true;
	
	return false;
}

void t_service::enable_cf(t_cf_type cf_type, const list<t_display_url> &cf_dest) {
	lock();

	switch (cf_type) {
	case CF_ALWAYS:
		cf_always_active = true;
		cf_always_dest = cf_dest;
		break;
	case CF_BUSY:
		cf_busy_active = true;
		cf_busy_dest = cf_dest;
		break;
	case CF_NOANSWER:
		cf_noanswer_active = true;
		cf_noanswer_dest = cf_dest;
		break;
	default:
		assert(false);
	}

	unlock();
	
	string msg;
	(void)write_config(msg);
}

void t_service::disable_cf(t_cf_type cf_type) {
	lock();

	switch (cf_type) {
	case CF_ALWAYS:
		cf_always_active = false;
		cf_always_dest.clear();
		break;
	case CF_BUSY:
		cf_busy_active = false;
		cf_busy_dest.clear();
		break;
	case CF_NOANSWER:
		cf_noanswer_active = false;
		cf_noanswer_dest.clear();
		break;
	default:
		assert(false);
	}

	unlock();
	
	string msg;
	(void)write_config(msg);
}

bool t_service::get_cf_active(t_cf_type cf_type, list<t_display_url> &dest) {
	bool active = false;

	lock();

	switch (cf_type) {
	case CF_ALWAYS:
		active = cf_always_active;
		dest = cf_always_dest;
		break;
	case CF_BUSY:
		active = cf_busy_active;
		dest = cf_busy_dest;
		break;
	case CF_NOANSWER:
		active = cf_noanswer_active;
		dest = cf_noanswer_dest;
		break;
	default:
		assert(false);
	}

	unlock();
	return active;
}

bool t_service::is_cf_active(void) {
	bool active = false;

	lock();
	active = cf_always_active || cf_busy_active || cf_noanswer_active;
	unlock();

	return active;
}

list<t_display_url> t_service::get_cf_dest(t_cf_type cf_type) {
	list<t_display_url> dest;

	lock();

	switch (cf_type) {
	case CF_ALWAYS:
		dest = cf_always_dest;
		break;
	case CF_BUSY:
		dest = cf_busy_dest;
		break;
	case CF_NOANSWER:
		dest = cf_noanswer_dest;
		break;
	default:
		assert(false);
	}

	unlock();
	return dest;
}

void t_service::enable_dnd(void) {
	lock();
	dnd_active = true;
	unlock();
	
	string msg;
	(void)write_config(msg);
}

void t_service::disable_dnd(void) {
	lock();
	dnd_active = false;
	unlock();
	
	string msg;
	(void)write_config(msg);
}

bool t_service::is_dnd_active(void) const {
	return dnd_active;
}

void t_service::enable_auto_answer(bool on) {
	lock();
	auto_answer_active = on;
	unlock();
	
	string msg;
	(void)write_config(msg);
}

bool t_service::is_auto_answer_active(void) const {
	return auto_answer_active;
}

bool t_service::read_config(string &error_msg) {
	struct stat stat_buf;
	
	lock();
	
	string filename = user_config->get_profile_name() + SVC_FILE_EXT;
	string f = user_config->expand_filename(filename);
	
	// Check if config file exists
	if (stat(f.c_str(), &stat_buf) != 0) {
		unlock();
		return true;
	}
	
	// Open file
	ifstream config(f.c_str());
	if (!config) {
		error_msg = "Cannot open file for reading: ";
		error_msg += f;
		log_file->write_report(error_msg, "t_service::read_config",
			LOG_NORMAL, LOG_CRITICAL);
		unlock();
		return false;
	}

	t_display_url display_url;
	cf_always_active = false;
	cf_always_dest.clear();
	cf_busy_active = false;
	cf_busy_dest.clear();
	cf_noanswer_active = false;
	cf_noanswer_dest.clear();
	
	while (!config.eof()) {
		string line;
		getline(config, line);

		// Check if read operation succeeded
		if (!config.good() && !config.eof()) {
			error_msg = "File system error while reading file ";
			error_msg += f;
			log_file->write_report(error_msg, "t_service::read_config",
				LOG_NORMAL, LOG_CRITICAL);
			unlock();
			return false;
		}

		line = trim(line);

		// Skip empty lines
		if (line.size() == 0) continue;

		// Skip comment lines
		if (line[0] == '#') continue;

		vector<string> l = split_on_first(line, '=');
		if (l.size() != 2) {
			error_msg = "Syntax error in file ";
			error_msg += f;
			error_msg += "\n";
			error_msg += line;
			log_file->write_report(error_msg, "t_service::read_config",
				LOG_NORMAL, LOG_CRITICAL);
			unlock();
			return false;
		}

		string parameter = trim(l[0]);
		string value = trim(l[1]);
		
		if (parameter == FLD_CF_ALWAYS) {
			ui->expand_destination(user_config, value, display_url);
			if (display_url.is_valid()) {
				cf_always_active = true;
				cf_always_dest.push_back(display_url);
			}
		} else if (parameter == FLD_CF_BUSY) {
			ui->expand_destination(user_config, value, display_url);
			if (display_url.is_valid()) {
				cf_busy_active = true;
				cf_busy_dest.push_back(display_url);
			}
		} else if (parameter == FLD_CF_NOANSWER) {
			ui->expand_destination(user_config, value, display_url);
			if (display_url.is_valid()) {
				cf_noanswer_active = true;
				cf_noanswer_dest.push_back(display_url);
			}
		} else if (parameter == FLD_DND) {
			dnd_active = yesno2bool(value);
		} else if (parameter == FLD_AUTO_ANSWER) {
			auto_answer_active = yesno2bool(value);
		} else {
			// Ignore unknown parameters. Only report in log file.
			log_file->write_header("t_service::read_config",
				LOG_NORMAL, LOG_WARNING);
			log_file->write_raw("Unknown parameter in service config: ");
			log_file->write_raw(parameter);
			log_file->write_endl();
			log_file->write_footer();
		}
	}
	
	unlock();
	return true;
}

bool t_service::write_config(string &error_msg) {
	struct stat stat_buf;
	
	lock();

	string filename = user_config->get_profile_name() + SVC_FILE_EXT;
	string f = user_config->expand_filename(filename);

	// Make a backup of the file if we are editing an existing file, so
	// that can be restored when writing fails.
	string f_backup = f + '~';
	if (stat(f.c_str(), &stat_buf) == 0) {
		if (rename(f.c_str(), f_backup.c_str()) != 0) {
			string err = get_error_str(errno);
			error_msg = "Failed to backup ";
			error_msg += f;
			error_msg += " to ";
			error_msg += f_backup;
			error_msg += "\n";
			error_msg += err;
			log_file->write_report(error_msg, "t_service::write_config",
				LOG_NORMAL, LOG_CRITICAL);
			unlock();
			return false;
		}
	}

	ofstream config(f.c_str());
	if (!config) {
		error_msg = "Cannot open file for writing: ";
		error_msg += f;
		log_file->write_report(error_msg, "t_user::write_config",
			LOG_NORMAL, LOG_CRITICAL);
		unlock();
		return false;
	}

	for (list<t_display_url>::iterator i = cf_always_dest.begin(); 
	     i != cf_always_dest.end(); i++)
	{
		config << FLD_CF_ALWAYS << '=' << i->encode() << endl;
	}
	
	for (list<t_display_url>::iterator i = cf_busy_dest.begin(); 
	     i != cf_busy_dest.end(); i++)
	{
		config << FLD_CF_BUSY << '=' << i->encode() << endl;
	}
	
	for (list<t_display_url>::iterator i = cf_noanswer_dest.begin(); 
	     i != cf_noanswer_dest.end(); i++)
	{
		config << FLD_CF_NOANSWER << '=' << i->encode() << endl;
	}
	
	config << FLD_DND << '=' << bool2yesno(dnd_active) << endl;
	config << FLD_AUTO_ANSWER << '=' << bool2yesno(auto_answer_active) << endl;
	
	// Check if writing succeeded
	if (!config.good()) {
		// Restore backup
		config.close();
		rename(f_backup.c_str(), f.c_str());

		error_msg = "File system error while writing file ";
		error_msg += f;
		log_file->write_report(error_msg, "t_service::write_config",
			LOG_NORMAL, LOG_CRITICAL);
		unlock();
		return false;
	}
	
	unlock();
	return true;
}