summaryrefslogtreecommitdiffstats
path: root/src/call_script.cpp
blob: 571efe166cf989c4f782281fa4e3b7ac2a7c36b5 (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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
/*
    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, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

#include <cstdio>
#include <cstring>
#include <iostream>
#include <unistd.h>
#include "call_script.h"
#include "log.h"
#include "userintf.h"
#include "util.h"

// Maximum length of the reason value
#define MAX_LEN_REASON		50

// Script result fields
#define SCR_ACTION		"action"
#define SCR_REASON		"reason"
#define SCR_CONTACT		"contact"
#define SCR_CALLER_NAME		"caller_name"
#define SCR_RINGTONE		"ringtone"
#define SCR_DISPLAY_MSG		"display_msg"
#define SCR_INTERNAL_ERROR	"internal_error"

// Script triggers
#define SCR_TRIGGER_IN_CALL		"in_call"
#define SCR_TRIGGER_IN_CALL_ANSWERED	"in_call_answered"
#define SCR_TRIGGER_IN_CALL_FAILED	"in_call_failed"
#define SCR_TRIGGER_OUT_CALL		"out_call"
#define SCR_TRIGGER_OUT_CALL_ANSWERED	"out_call_answered"
#define SCR_TRIGGER_OUT_CALL_FAILED	"out_call_failed"
#define SCR_TRIGGER_LOCAL_RELEASE	"local_release"
#define SCR_TRIGGER_REMOTE_RELEASE	"remote_release"

/////////////////////////
// class t_script_result
/////////////////////////

t_script_result::t_script_result() {
	clear();
}

t_script_result::t_action t_script_result::str2action(const string action_string) {
	string s = tolower(action_string);
	
	t_action result;	
	if (s == "continue") {
		result = ACTION_CONTINUE;
	} else if (s == "reject") {
		result = ACTION_REJECT;
	} else if (s == "dnd") {
		result = ACTION_DND;
	} else if (s == "redirect") {
		result = ACTION_REDIRECT;
	} else if (s == "autoanswer") {
		result = ACTION_AUTOANSWER;
	} else {
		// Unknown action
		result = ACTION_ERROR;
	}
	
	return result;
}

void t_script_result::clear(void) {
	action = ACTION_CONTINUE;
	reason.clear();
	contact.clear();
	caller_name.clear();
	ringtone.clear();
	display_msgs.clear();
}

void t_script_result::set_parameter(const string &parameter, const string &value) {
	if (parameter == SCR_ACTION) {
		action = str2action(value);
	} else if (parameter == SCR_REASON) {
		if (value.size() <= MAX_LEN_REASON) {
			reason = value;
		} else {
			reason = value.substr(0, MAX_LEN_REASON);
		}
	} else if (parameter == SCR_CONTACT) {
		contact = value;
	} else if (parameter == SCR_CALLER_NAME) {
		caller_name = value;
	} else if (parameter == SCR_RINGTONE) {
		ringtone = value;
	} else if (parameter == SCR_DISPLAY_MSG) {
		display_msgs.push_back(value);
	}
	// Unknown parameters are ignored
}

/////////////////////////
// class t_call_script
/////////////////////////

string t_call_script::trigger2str(t_trigger t) const {
	switch (t) {
	case TRIGGER_IN_CALL:
		return SCR_TRIGGER_IN_CALL;
	case TRIGGER_IN_CALL_ANSWERED:
		return SCR_TRIGGER_IN_CALL_ANSWERED;
	case TRIGGER_IN_CALL_FAILED:
		return SCR_TRIGGER_IN_CALL_FAILED;
	case TRIGGER_OUT_CALL:
		return SCR_TRIGGER_OUT_CALL;
	case TRIGGER_OUT_CALL_ANSWERED:
		return SCR_TRIGGER_OUT_CALL_ANSWERED;
	case TRIGGER_OUT_CALL_FAILED:
		return SCR_TRIGGER_OUT_CALL_FAILED;
	case TRIGGER_LOCAL_RELEASE:
		return SCR_TRIGGER_LOCAL_RELEASE;
	case TRIGGER_REMOTE_RELEASE:
		return SCR_TRIGGER_REMOTE_RELEASE;
	default:
		return "unknown";
	}
}

char **t_call_script::create_env(t_sip_message *m) const {
		string var_twinkle;

		// Number of existing environment variables
		int environ_size = 0;
		for (int i = 0; environ[i] != NULL; i++) {
			environ_size++;
		}
		
		// Number of SIP environment variables
		int start_sip_env = environ_size; // Position of SIP variables
		list<string> l = m->encode_env();
		
		var_twinkle = "SIP_FROM_USER=";
		var_twinkle += m->hdr_from.uri.get_user();
		l.push_back(var_twinkle);
		
		var_twinkle = "SIP_FROM_HOST=";
		var_twinkle += m->hdr_from.uri.get_host();
		l.push_back(var_twinkle);
		
		var_twinkle = "SIP_TO_USER=";
		var_twinkle += m->hdr_to.uri.get_user();
		l.push_back(var_twinkle);
		
		var_twinkle = "SIP_TO_HOST=";
		var_twinkle += m->hdr_to.uri.get_host();
		l.push_back(var_twinkle);
		
		environ_size += l.size();
		
		// Number of Twinkle environment variables
		int start_twinkle_env = environ_size; // Position of Twinkle variables
		environ_size += 3;
		
		// MEMMAN not called on purpose
		char **env = new char *[environ_size + 1];
		
		// Copy current environment to child
		for (int i = 0; environ[i] != NULL; i++) {
			env[i] = strdup(environ[i]);
		}
		
		// Add environment variables for SIP request
		int j = start_sip_env;
		for (list<string>::iterator i = l.begin(); i != l.end(); i++, j++) {
			env[j] = strdup(i->c_str());
		}
		
		// Add Twinkle specific environment variables
		var_twinkle = "TWINKLE_USER_PROFILE=";
		var_twinkle += user_config->get_profile_name();
		env[start_twinkle_env] = strdup(var_twinkle.c_str());
		
		var_twinkle = "TWINKLE_TRIGGER=";
		var_twinkle += trigger2str(trigger);
		env[start_twinkle_env + 1] = strdup(var_twinkle.c_str());
		
		var_twinkle = "TWINKLE_LINE=";
		var_twinkle += ulong2str(line_number);
		env[start_twinkle_env + 2] = strdup(var_twinkle.c_str());
		
		// Terminate array with NULL
		env[environ_size] = NULL;
		
		return env;
}

char **t_call_script::create_argv(void) const {
		// Determine script agument list
		vector<string> arg_list = split_ws(script_command, true);
		
		// MEMMAN not called on purpose
		char **argv = new char *[arg_list.size() + 1];
		
		int idx = 0;
		for (vector<string>::iterator i = arg_list.begin(); 
		     i != arg_list.end(); i++, idx++) 
		{
			argv[idx] = strdup(i->c_str());
		}
		argv[arg_list.size()] = NULL;
		
		return argv;
}

t_call_script::t_call_script(t_user *_user_config, t_trigger _trigger, uint16 _line_number) :
	user_config(_user_config),
	trigger(_trigger),
	line_number(_line_number)
{
	switch (trigger) {
	case TRIGGER_IN_CALL:
		script_command = user_config->get_script_incoming_call();
		break;
	case TRIGGER_IN_CALL_ANSWERED:
		script_command = user_config->get_script_in_call_answered();
		break;
	case TRIGGER_IN_CALL_FAILED:
		script_command = user_config->get_script_in_call_failed();
		break;
	case TRIGGER_OUT_CALL:
		script_command = user_config->get_script_outgoing_call();
		break;
	case TRIGGER_OUT_CALL_ANSWERED:
		script_command = user_config->get_script_out_call_answered();
		break;
	case TRIGGER_OUT_CALL_FAILED:
		script_command = user_config->get_script_out_call_failed();
		break;
	case TRIGGER_LOCAL_RELEASE:
		script_command = user_config->get_script_local_release();
		break;
	case TRIGGER_REMOTE_RELEASE:
		script_command = user_config->get_script_remote_release();
		break;
	default:
		script_command.clear();
		break;
	}
}

void t_call_script::exec_action(t_script_result &result, t_sip_message *m) const 
{
	result.clear();
	
	if (script_command.empty()) return;
	
	log_file->write_header("t_call_script::exec_action");
	log_file->write_raw("Execute script: ");
	log_file->write_raw(script_command);
	log_file->write_raw("\nTrigger: ");
	log_file->write_raw(trigger2str(trigger));
	log_file->write_raw("\nLine: ");
	log_file->write_raw(line_number);
	log_file->write_endl();
	log_file->write_footer();
	
	// Create pipe for communication with child process
	int fds[2];
	if (pipe(fds) == -1) {
		// Failed to create pipe
		log_file->write_header("t_call_script::exec_action",
			LOG_NORMAL, LOG_WARNING);
		log_file->write_raw("Failed to create pipe: ");
		log_file->write_raw(get_error_str(errno));
		log_file->write_endl();
		log_file->write_footer();
		return;
	}
	
	// Fork child process
	pid_t pid = fork();
	if (pid == -1) {
		// Failed to fork child process
		log_file->write_header("t_call_script::exec_action",
			LOG_NORMAL, LOG_WARNING);
		log_file->write_raw("Failed to fork child process: ");
		log_file->write_raw(get_error_str(errno));
		log_file->write_endl();
		log_file->write_footer();
		
		close(fds[0]);
		close(fds[1]);
		return;
	} else if (pid == 0) {
		// Child process
		
		// Close the read end of the pipe
		close(fds[0]);
		
		// Redirect stdout to the write end of the pipe
		dup2(fds[1], STDOUT_FILENO);
		
		// NOTE: MEMMAN audits are not called as all pointers will be deleted
		//       automatically when the child process dies
		//	 Also, the child process has a copy of the MEMMAN object
		char **argv = create_argv();
		
		// Determine environment
		char **env = create_env(m);
		
		// Replace the child process by the script
		if (execve(argv[0], argv, env) == -1) {
			// Failed to execute script. Report error to parent.
			string err_msg;
			err_msg = get_error_str(errno);
			err_msg += ": ";
			err_msg += argv[0];
			cout << SCR_INTERNAL_ERROR << '=' << err_msg << endl;
			exit(0);
		}
	} else {
		// Parent process
		log_file->write_header("t_call_script::exec_action");
		log_file->write_raw("Child process spawned, pid = ");
		log_file->write_raw((int)pid);
		log_file->write_endl();
		log_file->write_footer();
		
		// Close the write end of the pipe
		close(fds[1]);
		
		// Read the script results
		FILE *fp_result = fdopen(fds[0], "r");
		if (!fp_result) {
			log_file->write_header("t_call_script::exec_action",
				LOG_NORMAL, LOG_WARNING);
			log_file->write_raw("Failed to open pipe to child: ");
			log_file->write_raw(get_error_str(errno));
			log_file->write_endl();
			log_file->write_footer();
			
			// Child will be cleaned up by phone_sigwait

			close(fds[0]);
			return;
		}
		
		char *line_buf = NULL;
		size_t line_buf_len = 0;
		ssize_t num_read;
		
		// Read and parse script results.
		while ((num_read = getline(&line_buf, &line_buf_len, fp_result)) != -1) {
			// Strip newline if present
			if (line_buf[num_read - 1] == '\n') {
				line_buf[num_read - 1] = 0;
			}

			// Convert the read line to a C++ string
			string line(line_buf);	
			line = trim(line);
			
			// Stop reading on end command
			if (line == "end") break;
	
			// Skip empty lines
			if (line.empty()) continue;
	
			// Skip comment lines
			if (line[0] == '#') continue;
	
			vector<string> v = split_on_first(line, '=');
			
			// SKip invalid lines
			if (v.size() != 2) continue;
	
			string parameter = trim(v[0]);
			string value = trim(v[1]);
			
			if (parameter == SCR_INTERNAL_ERROR) {
				log_file->write_report(value,
					"t_call_script::exec_action",
					LOG_NORMAL, LOG_WARNING);
				ui->cb_display_msg(value, MSG_WARNING);
				result.clear();
				break;
			}
			
			result.set_parameter(parameter, value);
		}
		
		if (line_buf) free(line_buf);
		fclose(fp_result);
		close(fds[0]);
		
		// Child will be cleaned up by phone_sigwait
	}
}

void t_call_script::exec_notify(t_sip_message *m) const 
{
	if (script_command.empty()) return;
	
	log_file->write_header("t_call_script::exec_notify");
	log_file->write_raw("Execute script: ");
	log_file->write_raw(script_command);
	log_file->write_raw("\nTrigger: ");
	log_file->write_raw(trigger2str(trigger));
	log_file->write_endl();
	log_file->write_footer();
	
	// Fork child process
	pid_t pid = fork();
	if (pid == -1) {
		// Failed to fork child process
		log_file->write_header("t_call_script::exec_notify",
			LOG_NORMAL, LOG_WARNING);
		log_file->write_raw("Failed to fork child process: ");
		log_file->write_raw(get_error_str(errno));
		log_file->write_endl();
		log_file->write_footer();

		return;
	} else if (pid == 0) {
		// Child process
			
		// NOTE: MEMMAN audits are not called as all pointers will be deleted
		//       automatically when the child process dies
		//	 Also, the child process has a copy of the MEMMAN object
		char **argv = create_argv();
		
		// Determine environment
		char **env = create_env(m);
		
		// Replace the child process by the script
		if (execve(argv[0], argv, env) == -1) {
			// Failed to execute script.
			exit(0);
		}
	} else {
		// Parent process
		log_file->write_header("t_call_script::exec_notify");
		log_file->write_raw("Child process spawned, pid = ");
		log_file->write_raw((int)pid);
		log_file->write_endl();
		log_file->write_footer();
		
		// No interaction with child needed.
		// Child will be cleaned up by phone_sigwait
	}
}