forked from asterisk/asterisk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunc_sayfiles.c
410 lines (363 loc) · 14.2 KB
/
func_sayfiles.c
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
/*
* Asterisk -- An open source telephony toolkit.
*
* Copyright (C) 2021, Naveen Albert <asterisk@phreaknet.org>
*
* 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 Returns files played by Say applications
*
* \author Naveen Albert <asterisk@phreaknet.org>
* \ingroup functions
*/
/*** MODULEINFO
<support_level>extended</support_level>
***/
#include "asterisk.h"
#include "asterisk/pbx.h"
#include "asterisk/file.h"
#include "asterisk/channel.h"
#include "asterisk/say.h"
#include "asterisk/lock.h"
#include "asterisk/localtime.h"
#include "asterisk/utils.h"
#include "asterisk/app.h"
#include "asterisk/test.h"
#include "asterisk/module.h"
#include "asterisk/conversions.h"
/*** DOCUMENTATION
<function name="SAYFILES" language="en_US">
<since>
<version>16.21.0</version>
<version>18.7.0</version>
<version>19.0.0</version>
</since>
<synopsis>
Returns the ampersand-delimited file names that would be played by the Say applications (e.g. SayAlpha, SayDigits).
</synopsis>
<syntax>
<parameter name="value" required="true">
<para>The value to be translated to filenames.</para>
</parameter>
<parameter name="type">
<para>Say application type.</para>
<enumlist>
<enum name="alpha">
<para>Files played by SayAlpha(). Default if none is specified.</para>
</enum>
<enum name="digits">
<para>Files played by SayDigits().</para>
</enum>
<enum name="money">
<para>Files played by SayMoney(). Currently supported for English and US dollars only.</para>
</enum>
<enum name="number">
<para>Files played by SayNumber(). Currently supported for English only.</para>
</enum>
<enum name="ordinal">
<para>Files played by SayOrdinal(). Currently supported for English only.</para>
</enum>
<enum name="phonetic">
<para>Files played by SayPhonetic().</para>
</enum>
</enumlist>
</parameter>
</syntax>
<description>
<para>Returns the files that would be played by a Say application. These filenames could then be
passed directly into Playback, BackGround, Read, Queue, or any application which supports
playback of multiple ampersand-delimited files.</para>
<example title="Read using the number 123">
same => n,Read(response,${SAYFILES(123,number)})
</example>
</description>
<see-also>
<ref type="application">SayAlpha</ref>
<ref type="application">SayDigits</ref>
<ref type="application">SayMoney</ref>
<ref type="application">SayNumber</ref>
<ref type="application">SayOrdinal</ref>
<ref type="application">SayPhonetic</ref>
</see-also>
</function>
***/
static int sayfile_exec(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
{
char *value, *type, *files;
const char *lang;
struct ast_str *filenames = NULL;
AST_DECLARE_APP_ARGS(args,
AST_APP_ARG(value);
AST_APP_ARG(type);
);
if (ast_strlen_zero(data)) {
ast_log(LOG_WARNING, "SAYFILES requires an argument\n");
return 0;
}
AST_STANDARD_APP_ARGS(args, data);
value = args.value;
type = (ast_strlen_zero(args.type) ? "alpha" : args.type);
lang = (chan ? ast_channel_language(chan) : "en"); /* No chan for unit tests */
if (!strcmp(type, "alpha")) {
filenames = ast_get_character_str(value, lang, AST_SAY_CASE_NONE);
} else if (!strcmp(type, "phonetic")) {
filenames = ast_get_phonetic_str(value, lang);
} else if (!strcmp(type, "digits")) {
filenames = ast_get_digit_str(value, lang);
} else if (!strcmp(type, "number")) {
int num;
if (ast_str_to_int(value, &num)) {
ast_log(LOG_WARNING, "Invalid numeric argument: %s\n", value);
} else {
filenames = ast_get_number_str(num, lang);
}
} else if (!strcmp(type, "ordinal")) {
int num;
if (ast_str_to_int(value, &num)) {
ast_log(LOG_WARNING, "Invalid numeric argument: %s\n", value);
} else {
filenames = ast_get_ordinal_str(num, lang);
}
} else if (!strcmp(type, "money")) {
filenames = ast_get_money_str(value, lang);
} else {
ast_log(LOG_WARNING, "Invalid say type specified: %s\n", type);
}
if (!filenames) {
return -1;
}
files = ast_str_buffer(filenames);
snprintf(buf, len, "%s", files);
ast_free(filenames);
return 0;
}
static struct ast_custom_function sayfiles = {
.name = "SAYFILES",
.read = sayfile_exec,
};
#ifdef TEST_FRAMEWORK
AST_TEST_DEFINE(test_SAYFILES_function)
{
enum ast_test_result_state res = AST_TEST_PASS;
struct ast_str *expr, *result;
switch (cmd) {
case TEST_INIT:
info->name = "test_SAYFILES_function";
info->category = "/funcs/func_sayfiles/";
info->summary = "Test SAYFILES function substitution";
info->description =
"Executes a series of variable substitutions using the SAYFILES function and ensures that the expected results are received.";
return AST_TEST_NOT_RUN;
case TEST_EXECUTE:
break;
}
ast_test_status_update(test, "Testing SAYFILES() substitution ...\n");
if (!(expr = ast_str_create(16))) {
return AST_TEST_FAIL;
}
if (!(result = ast_str_create(16))) {
ast_free(expr);
return AST_TEST_FAIL;
}
ast_str_set(&expr, 0, "${SAYFILES(hi Th3re,alpha)}");
ast_str_substitute_variables(&result, 0, NULL, ast_str_buffer(expr));
if (strcmp(ast_str_buffer(result), "letters/h&letters/i&letters/space&letters/t&letters/h&digits/3&letters/r&letters/e") != 0) {
ast_test_status_update(test, "SAYFILES(hi Th3re,alpha) test failed ('%s')\n",
ast_str_buffer(result));
res = AST_TEST_FAIL;
}
ast_str_set(&expr, 0, "${SAYFILES(phreak,phonetic)}");
ast_str_substitute_variables(&result, 0, NULL, ast_str_buffer(expr));
if (strcmp(ast_str_buffer(result), "phonetic/p_p&phonetic/h_p&phonetic/r_p&phonetic/e_p&phonetic/a_p&phonetic/k_p") != 0) {
ast_test_status_update(test, "SAYFILES(phreak,phonetic) test failed ('%s')\n",
ast_str_buffer(result));
res = AST_TEST_FAIL;
}
ast_str_set(&expr, 0, "${SAYFILES(35,digits)}");
ast_str_substitute_variables(&result, 0, NULL, ast_str_buffer(expr));
if (strcmp(ast_str_buffer(result), "digits/3&digits/5") != 0) {
ast_test_status_update(test, "SAYFILES(35,digits) test failed ('%s')\n",
ast_str_buffer(result));
res = AST_TEST_FAIL;
}
/* + should be ignored and there should not be a leading & */
ast_str_set(&expr, 0, "${SAYFILES(+18005551212,digits)}");
ast_str_substitute_variables(&result, 0, NULL, ast_str_buffer(expr));
if (strcmp(ast_str_buffer(result), "digits/1&digits/8&digits/0&digits/0&digits/5&digits/5&digits/5&digits/1&digits/2&digits/1&digits/2") != 0) {
ast_test_status_update(test, "SAYFILES(+18005551212,digits) test failed ('%s')\n",
ast_str_buffer(result));
res = AST_TEST_FAIL;
}
ast_str_set(&expr, 0, "${SAYFILES(35,number)}");
ast_str_substitute_variables(&result, 0, NULL, ast_str_buffer(expr));
if (strcmp(ast_str_buffer(result), "digits/30&digits/5") != 0) {
ast_test_status_update(test, "SAYFILES(35,number) test failed ('%s')\n",
ast_str_buffer(result));
res = AST_TEST_FAIL;
}
ast_str_set(&expr, 0, "${SAYFILES(747,number)}");
ast_str_substitute_variables(&result, 0, NULL, ast_str_buffer(expr));
if (strcmp(ast_str_buffer(result), "digits/7&digits/hundred&digits/40&digits/7") != 0) {
ast_test_status_update(test, "SAYFILES(747,number) test failed ('%s')\n",
ast_str_buffer(result));
res = AST_TEST_FAIL;
}
ast_str_set(&expr, 0, "${SAYFILES(1042,number)}");
ast_str_substitute_variables(&result, 0, NULL, ast_str_buffer(expr));
if (strcmp(ast_str_buffer(result), "digits/1&digits/thousand&digits/40&digits/2") != 0) {
ast_test_status_update(test, "SAYFILES(1042,number) test failed ('%s')\n",
ast_str_buffer(result));
res = AST_TEST_FAIL;
}
ast_str_set(&expr, 0, "${SAYFILES(0,number)}");
ast_str_substitute_variables(&result, 0, NULL, ast_str_buffer(expr));
if (strcmp(ast_str_buffer(result), "digits/0") != 0) {
ast_test_status_update(test, "SAYFILES(0,digits) test failed ('%s')\n",
ast_str_buffer(result));
res = AST_TEST_FAIL;
}
ast_str_set(&expr, 0, "${SAYFILES(2001000001,number)}");
ast_str_substitute_variables(&result, 0, NULL, ast_str_buffer(expr));
if (strcmp(ast_str_buffer(result), "digits/2&digits/billion&digits/1&digits/million&digits/1") != 0) {
ast_test_status_update(test, "SAYFILES(2001000001,number) test failed ('%s')\n",
ast_str_buffer(result));
res = AST_TEST_FAIL;
}
ast_str_set(&expr, 0, "${SAYFILES(7,ordinal)}");
ast_str_substitute_variables(&result, 0, NULL, ast_str_buffer(expr));
if (strcmp(ast_str_buffer(result), "digits/h-7") != 0) {
ast_test_status_update(test, "SAYFILES(7,ordinal) test failed ('%s')\n",
ast_str_buffer(result));
res = AST_TEST_FAIL;
}
ast_str_set(&expr, 0, "${SAYFILES(35,ordinal)}");
ast_str_substitute_variables(&result, 0, NULL, ast_str_buffer(expr));
if (strcmp(ast_str_buffer(result), "digits/30&digits/h-5") != 0) {
ast_test_status_update(test, "SAYFILES(35,ordinal) test failed ('%s')\n",
ast_str_buffer(result));
res = AST_TEST_FAIL;
}
ast_str_set(&expr, 0, "${SAYFILES(1042,ordinal)}");
ast_str_substitute_variables(&result, 0, NULL, ast_str_buffer(expr));
if (strcmp(ast_str_buffer(result), "digits/1&digits/thousand&digits/40&digits/h-2") != 0) {
ast_test_status_update(test, "SAYFILES(1042,ordinal) test failed ('%s')\n",
ast_str_buffer(result));
res = AST_TEST_FAIL;
}
ast_str_set(&expr, 0, "${SAYFILES(11042,ordinal)}");
ast_str_substitute_variables(&result, 0, NULL, ast_str_buffer(expr));
if (strcmp(ast_str_buffer(result), "digits/11&digits/thousand&digits/40&digits/h-2") != 0) {
ast_test_status_update(test, "SAYFILES(11042,ordinal) test failed ('%s')\n",
ast_str_buffer(result));
res = AST_TEST_FAIL;
}
ast_str_set(&expr, 0, "${SAYFILES(40000,ordinal)}");
ast_str_substitute_variables(&result, 0, NULL, ast_str_buffer(expr));
if (strcmp(ast_str_buffer(result), "digits/40&digits/h-thousand") != 0) {
ast_test_status_update(test, "SAYFILES(40000,ordinal) test failed ('%s')\n",
ast_str_buffer(result));
res = AST_TEST_FAIL;
}
ast_str_set(&expr, 0, "${SAYFILES(43638,ordinal)}");
ast_str_substitute_variables(&result, 0, NULL, ast_str_buffer(expr));
if (strcmp(ast_str_buffer(result), "digits/40&digits/3&digits/thousand&digits/6&digits/hundred&digits/30&digits/h-8") != 0) {
ast_test_status_update(test, "SAYFILES(43638,ordinal) test failed ('%s')\n",
ast_str_buffer(result));
res = AST_TEST_FAIL;
}
ast_str_set(&expr, 0, "${SAYFILES(1000000,ordinal)}");
ast_str_substitute_variables(&result, 0, NULL, ast_str_buffer(expr));
if (strcmp(ast_str_buffer(result), "digits/1&digits/h-million") != 0) {
ast_test_status_update(test, "SAYFILES(1000000,ordinal) test failed ('%s')\n",
ast_str_buffer(result));
res = AST_TEST_FAIL;
}
ast_str_set(&expr, 0, "${SAYFILES(1000001,ordinal)}");
ast_str_substitute_variables(&result, 0, NULL, ast_str_buffer(expr));
if (strcmp(ast_str_buffer(result), "digits/1&digits/million&digits/h-1") != 0) {
ast_test_status_update(test, "SAYFILES(1000001,ordinal) test failed ('%s')\n",
ast_str_buffer(result));
res = AST_TEST_FAIL;
}
ast_str_set(&expr, 0, "${SAYFILES(2001000001,ordinal)}");
ast_str_substitute_variables(&result, 0, NULL, ast_str_buffer(expr));
if (strcmp(ast_str_buffer(result), "digits/2&digits/billion&digits/1&digits/million&digits/h-1") != 0) {
ast_test_status_update(test, "SAYFILES(2001000001,ordinal) test failed ('%s')\n",
ast_str_buffer(result));
res = AST_TEST_FAIL;
}
ast_str_set(&expr, 0, "${SAYFILES(0,money)}");
ast_str_substitute_variables(&result, 0, NULL, ast_str_buffer(expr));
if (strcmp(ast_str_buffer(result), "digits/0¢s") != 0) {
ast_test_status_update(test, "SAYFILES(0,money) test failed ('%s')\n",
ast_str_buffer(result));
res = AST_TEST_FAIL;
}
ast_str_set(&expr, 0, "${SAYFILES(0.01,money)}");
ast_str_substitute_variables(&result, 0, NULL, ast_str_buffer(expr));
if (strcmp(ast_str_buffer(result), "digits/1¢") != 0) {
ast_test_status_update(test, "SAYFILES(0.01,money) test failed ('%s')\n",
ast_str_buffer(result));
res = AST_TEST_FAIL;
}
ast_str_set(&expr, 0, "${SAYFILES(0.42,money)}");
ast_str_substitute_variables(&result, 0, NULL, ast_str_buffer(expr));
if (strcmp(ast_str_buffer(result), "digits/40&digits/2¢s") != 0) {
ast_test_status_update(test, "SAYFILES(0.42,money) test failed ('%s')\n",
ast_str_buffer(result));
res = AST_TEST_FAIL;
}
ast_str_set(&expr, 0, "${SAYFILES(1.00,money)}");
ast_str_substitute_variables(&result, 0, NULL, ast_str_buffer(expr));
if (strcmp(ast_str_buffer(result), "digits/1&letters/dollar") != 0) {
ast_test_status_update(test, "SAYFILES(1.00,money) test failed ('%s')\n",
ast_str_buffer(result));
res = AST_TEST_FAIL;
}
ast_str_set(&expr, 0, "${SAYFILES(1.42,money)}");
ast_str_substitute_variables(&result, 0, NULL, ast_str_buffer(expr));
if (strcmp(ast_str_buffer(result), "digits/1&letters/dollar_&and&digits/40&digits/2¢s") != 0) {
ast_test_status_update(test, "SAYFILES(1.42,money) test failed ('%s')\n",
ast_str_buffer(result));
res = AST_TEST_FAIL;
}
ast_str_set(&expr, 0, "${SAYFILES(2.00,money)}");
ast_str_substitute_variables(&result, 0, NULL, ast_str_buffer(expr));
if (strcmp(ast_str_buffer(result), "digits/2&dollars") != 0) {
ast_test_status_update(test, "SAYFILES(2.00,money) test failed ('%s')\n",
ast_str_buffer(result));
res = AST_TEST_FAIL;
}
ast_str_set(&expr, 0, "${SAYFILES(2.42,money)}");
ast_str_substitute_variables(&result, 0, NULL, ast_str_buffer(expr));
if (strcmp(ast_str_buffer(result), "digits/2&dollars&and&digits/40&digits/2¢s") != 0) {
ast_test_status_update(test, "SAYFILES(2.42,money) test failed ('%s')\n",
ast_str_buffer(result));
res = AST_TEST_FAIL;
}
ast_free(expr);
ast_free(result);
return res;
}
#endif
static int unload_module(void)
{
AST_TEST_UNREGISTER(test_SAYFILES_function);
return ast_custom_function_unregister(&sayfiles);
}
static int load_module(void)
{
AST_TEST_REGISTER(test_SAYFILES_function);
return ast_custom_function_register(&sayfiles);
}
AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Say application files");