aboutsummaryrefslogtreecommitdiffstats
path: root/funcs/func_redirecting.c
blob: 62c6d7bc997f41b861c6ed8661cc755fd63e3e19 (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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
/*
 * Asterisk -- An open source telephony toolkit.
 *
 * Copyright (C) 2008 Digium, Inc.
 *
 * Richard Mudgett <rmudgett@digium.com>
 *
 * See http://www.asterisk.org for more information about
 * the Asterisk project. Please do not directly contact
 * any of the maintainers of this project for assistance;
 * the project provides a web site, mailing lists and IRC
 * channels for your use.
 *
 * This program is free software, distributed under the terms of
 * the GNU General Public License Version 2. See the LICENSE file
 * at the top of the source tree.
 */

/*!
 * \file
 * \brief Redirecting data dialplan function
 * \ingroup functions
 *
 * \author Richard Mudgett <rmudgett@digium.com>
 *
 * See Also:
 * \arg \ref AstCREDITS
 */


#include "asterisk.h"

ASTERISK_FILE_VERSION(__FILE__, "$Revision$")

/* ------------------------------------------------------------------- */


#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>

#include "asterisk/module.h"
#include "asterisk/channel.h"
#include "asterisk/pbx.h"
#include "asterisk/logger.h"
#include "asterisk/utils.h"
#include "asterisk/app.h"
#include "asterisk/options.h"
#include "asterisk/callerid.h"

/*
 * Do not document the REDIRECTING(pres) datatype.
 * It has turned out that the from-pres and to-pres values must be kept
 * separate.  They represent two different parties and there is a case when
 * they are active at the same time.  The plain pres option will simply
 * live on as a historical relic.
 */
/*** DOCUMENTATION
	<function name="REDIRECTING" language="en_US">
		<synopsis>
			Gets or sets Redirecting data on the channel.
		</synopsis>
		<syntax>
			<parameter name="datatype" required="true">
				<para>The allowable datatypes are:</para>
				<enumlist>
					<enum name = "from-all" />
					<enum name = "from-num" />
					<enum name = "from-name" />
					<enum name = "from-ton" />
					<enum name = "from-pres" />
					<enum name = "to-all" />
					<enum name = "to-num" />
					<enum name = "to-name" />
					<enum name = "to-ton" />
					<enum name = "to-pres" />
					<enum name = "reason" />
					<enum name = "count" />
				</enumlist>
			</parameter>
			<parameter name="i">
				<para>If set, this will prevent the channel from sending out protocol
				messages because of the value being set</para>
			</parameter>
		</syntax>
		<description>
			<para>Gets or sets Redirecting data on the channel. The allowable values
			for the <replaceable>reason</replaceable> field are the following:</para>
			<enumlist>
				<enum name = "unknown"><para>Unknown</para></enum>
				<enum name = "cfb"><para>Call Forwarding Busy</para></enum>
				<enum name = "cfnr"><para>Call Forwarding No Reply</para></enum>
				<enum name = "unavailable"><para>Callee is Unavailable</para></enum>
				<enum name = "time_of_day"><para>Time of Day</para></enum>
				<enum name = "dnd"><para>Do Not Disturb</para></enum>
				<enum name = "deflection"><para>Call Deflection</para></enum>
				<enum name = "follow_me"><para>Follow Me</para></enum>
				<enum name = "out_of_order"><para>Called DTE Out-Of-Order</para></enum>
				<enum name = "away"><para>Callee is Away</para></enum>
				<enum name = "cf_dte"><para>Call Forwarding By The Called DTE</para></enum>
				<enum name = "cfu"><para>Call Forwarding Unconditional</para></enum>
			</enumlist>
		</description>
	</function>
 ***/

enum ID_FIELD_STATUS {
	ID_FIELD_VALID,
	ID_FIELD_INVALID,
	ID_FIELD_UNKNOWN
};




/* ******************************************************************* */
/*!
 * \internal
 * \brief Read values from the party id struct.
 *
 * \param buf Buffer to fill with read value.
 * \param len Length of the buffer
 * \param data Remaining function datatype string
 *
 * \retval ID_FIELD_VALID on success.
 * \retval ID_FIELD_UNKNOWN on unknown field name.
 */
static enum ID_FIELD_STATUS redirecting_id_read(char *buf, size_t len, char *data, const struct ast_party_id *id)
{
	enum ID_FIELD_STATUS status;

	status = ID_FIELD_VALID;

	if (!strncasecmp("all", data, 3)) {
		snprintf(buf, len, "\"%s\" <%s>",
			 S_OR(id->name, ""),
			 S_OR(id->number, ""));
	} else if (!strncasecmp("name", data, 4)) {
		if (id->name) {
			ast_copy_string(buf, id->name, len);
		}
	} else if (!strncasecmp("num", data, 3)) {
		if (id->number) {
			ast_copy_string(buf, id->number, len);
		}
	} else if (!strncasecmp("ton", data, 3)) {
		snprintf(buf, len, "%d", id->number_type);
	} else if (!strncasecmp("pres", data, 4)) {
		ast_copy_string(buf, ast_named_caller_presentation(id->number_presentation), len);
	} else {
		status = ID_FIELD_UNKNOWN;
	}

	return status;
}




/* ******************************************************************* */
/*!
 * \internal
 * \brief Read values from the redirecting information struct.
 *
 * \param chan Asterisk channel to read
 * \param cmd Not used
 * \param data Redirecting function datatype string
 * \param buf Buffer to fill with read value.
 * \param len Length of the buffer
 *
 * \retval 0 on success.
 * \retval -1 on error.
 */
static int redirecting_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
{
	/* Ensure that the buffer is empty */
	*buf = 0;

	if (!chan)
		return -1;

	ast_channel_lock(chan);

	if (!strncasecmp("from-", data, 5)) {
		struct ast_party_id from_id;

		from_id = chan->redirecting.from;
		from_id.number = chan->cid.cid_rdnis;
		switch (redirecting_id_read(buf, len, data + 5, &from_id)) {
		case ID_FIELD_VALID:
		case ID_FIELD_INVALID:
			break;

		default:
			ast_log(LOG_ERROR, "Unknown redirecting data type '%s'.\n", data);
			break;
		}
	} else if (!strncasecmp("to-", data, 3)) {
		switch (redirecting_id_read(buf, len, data + 3, &chan->redirecting.to)) {
		case ID_FIELD_VALID:
		case ID_FIELD_INVALID:
			break;

		default:
			ast_log(LOG_ERROR, "Unknown redirecting data type '%s'.\n", data);
			break;
		}
	} else if (!strncasecmp("pres", data, 4)) {
		ast_copy_string(buf, ast_named_caller_presentation(chan->redirecting.from.number_presentation), len);
	} else if (!strncasecmp("reason", data, 6)) {
		ast_copy_string(buf, ast_redirecting_reason_name(chan->redirecting.reason), len);
	} else if (!strncasecmp("count", data, 5)) {
		snprintf(buf, len, "%d", chan->redirecting.count);
	} else {
		ast_log(LOG_ERROR, "Unknown redirecting data type '%s'.\n", data);
	}

	ast_channel_unlock(chan);

	return 0;
}




/* ******************************************************************* */
/*!
 * \internal
 * \brief Write new values to the party id struct
 *
 * \param id Party ID struct to write values
 * \param data Remaining function datatype string
 * \param value Value to assign to the party id.
 *
 * \retval ID_FIELD_VALID on success.
 * \retval ID_FIELD_INVALID on error with field value.
 * \retval ID_FIELD_UNKNOWN on unknown field name.
 */
static enum ID_FIELD_STATUS redirecting_id_write(struct ast_party_id *id, char *data, const char *value)
{
	char *val;
	enum ID_FIELD_STATUS status;

	status = ID_FIELD_VALID;

	if (!strncasecmp("all", data, 3)) {
		char name[256];
		char num[256];

		ast_callerid_split(value, name, sizeof(name), num, sizeof(num));
		if (!(id->name = ast_strdup(name))) {
			return ID_FIELD_INVALID;
		}
		if (!(id->number = ast_strdup(num))) {
			return ID_FIELD_INVALID;
		}
	} else if (!strncasecmp("name", data, 4)) {
		id->name = ast_strdup(value);
		ast_trim_blanks(id->name);
	} else if (!strncasecmp("num", data, 3)) {
		id->number = ast_strdup(value);
		ast_trim_blanks(id->number);
	} else if (!strncasecmp("ton", data, 3)) {
		val = ast_strdupa(value);
		ast_trim_blanks(val);

		if (('0' <= val[0]) && (val[0] <= '9')) {
			id->number_type = atoi(val);
		} else {
			ast_log(LOG_ERROR, "Unknown redirecting type of number '%s', value unchanged\n", val);
			status = ID_FIELD_INVALID;
		}
	} else if (!strncasecmp("pres", data, 4)) {
		int pres;

		val = ast_strdupa(value);
		ast_trim_blanks(val);

		if (('0' <= val[0]) && (val[0] <= '9')) {
			pres = atoi(val);
		} else {
			pres = ast_parse_caller_presentation(val);
		}

		if (pres < 0) {
			ast_log(LOG_ERROR, "Unknown redirecting number presentation '%s', value unchanged\n", val);
			status = ID_FIELD_INVALID;
		} else {
			id->number_presentation = pres;
		}
	} else {
		status = ID_FIELD_UNKNOWN;
	}

	return status;
}




/* ******************************************************************* */
/*!
 * \internal
 * \brief Write new values to the redirecting information struct.
 *
 * \param chan Asterisk channel to update
 * \param cmd Not used
 * \param data Redirecting function datatype string
 * \param value Value to assign to the redirecting information struct.
 *
 * \retval 0 on success.
 * \retval -1 on error.
 */
static int redirecting_write(struct ast_channel *chan, const char *cmd, char *data, const char *value)
{
	struct ast_party_redirecting redirecting;
	char *val;
	char *option;
	void (*set_it)(struct ast_channel *chan, const struct ast_party_redirecting *redirecting);

	if (!value || !chan) {
		return -1;
	}

	/* Determine if the update indication inhibit option is present */
	option = strchr(data, ',');
	if (option) {
		option = ast_skip_blanks(option + 1);
		switch (*option) {
		case 'i':
			set_it = ast_channel_set_redirecting;
			break;

		default:
			ast_log(LOG_ERROR, "Unknown redirecting option '%s'.\n", option);
			return 0;
		}
	}
	else {
		set_it = ast_channel_update_redirecting;
	}

	ast_channel_lock(chan);
	ast_party_redirecting_set_init(&redirecting, &chan->redirecting);
	ast_channel_unlock(chan);

	value = ast_skip_blanks(value);

	if (!strncasecmp("from-", data, 5)) {
		switch (redirecting_id_write(&redirecting.from, data + 5, value)) {
		case ID_FIELD_VALID:
			set_it(chan, &redirecting);
			ast_party_redirecting_free(&redirecting);
			break;

		case ID_FIELD_INVALID:
			break;

		default:
			ast_log(LOG_ERROR, "Unknown redirecting data type '%s'.\n", data);
			break;
		}
	} else if (!strncasecmp("to-", data, 3)) {
		switch (redirecting_id_write(&redirecting.to, data + 3, value)) {
		case ID_FIELD_VALID:
			set_it(chan, &redirecting);
			ast_party_redirecting_free(&redirecting);
			break;

		case ID_FIELD_INVALID:
			break;

		default:
			ast_log(LOG_ERROR, "Unknown redirecting data type '%s'.\n", data);
			break;
		}
	} else if (!strncasecmp("pres", data, 4)) {
		int pres;

		val = ast_strdupa(value);
		ast_trim_blanks(val);

		if (('0' <= val[0]) && (val[0] <= '9')) {
			pres = atoi(val);
		} else {
			pres = ast_parse_caller_presentation(val);
		}

		if (pres < 0) {
			ast_log(LOG_ERROR, "Unknown redirecting number presentation '%s', value unchanged\n", val);
		} else {
			redirecting.from.number_presentation = pres;
			redirecting.to.number_presentation = pres;
			set_it(chan, &redirecting);
		}
	} else if (!strncasecmp("reason", data, 6)) {
		int reason;

		val = ast_strdupa(value);
		ast_trim_blanks(val);

		if (('0' <= val[0]) && (val[0] <= '9')) {
			reason = atoi(val);
		} else {
			reason = ast_redirecting_reason_parse(val);
		}

		if (reason < 0) {
			ast_log(LOG_ERROR, "Unknown redirecting reason '%s', value unchanged\n", val);
		} else {
			redirecting.reason = reason;
			set_it(chan, &redirecting);
		}
	} else if (!strncasecmp("count", data, 5)) {
		val = ast_strdupa(value);
		ast_trim_blanks(val);

		if (('0' <= val[0]) && (val[0] <= '9')) {
			redirecting.count = atoi(val);
			set_it(chan, &redirecting);
		} else {
			ast_log(LOG_ERROR, "Unknown redirecting count '%s', value unchanged\n", val);
		}
	} else {
		ast_log(LOG_ERROR, "Unknown redirecting data type '%s'.\n", data);
	}

	return 0;
}




static struct ast_custom_function redirecting_function = {
	.name = "REDIRECTING",
	.read = redirecting_read,
	.write = redirecting_write,
};




/* ******************************************************************* */
/*!
 * \internal
 * \brief Unload the function module
 *
 * \retval 0 on success.
 * \retval -1 on error.
 */
static int unload_module(void)
{
	return ast_custom_function_unregister(&redirecting_function);
}




/* ******************************************************************* */
/*!
 * \internal
 * \brief Load and initialize the function module.
 *
 * \retval 0 on success.
 * \retval -1 on error.
 */
static int load_module(void)
{
	return ast_custom_function_register(&redirecting_function)
		? AST_MODULE_LOAD_DECLINE
		: AST_MODULE_LOAD_SUCCESS;
}




AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Redirecting data dialplan function");


/* ------------------------------------------------------------------- */
/* end func_redirecting.c */