Asterisk - The Open Source Telephony Project GIT-master-7e7a603
main/app.c
Go to the documentation of this file.
1/*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 1999 - 2005, Digium, Inc.
5 *
6 * Mark Spencer <markster@digium.com>
7 *
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
13 *
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
17 */
18
19/*! \file
20 *
21 * \brief Convenient Application Routines
22 *
23 * \author Mark Spencer <markster@digium.com>
24 */
25
26/*!
27 * Application Skeleton is an example of creating an application for Asterisk.
28 * \example app_skel.c
29 */
30
31/*** MODULEINFO
32 <support_level>core</support_level>
33 ***/
34
35#include "asterisk.h"
36
37#ifdef HAVE_SYS_STAT_H
38#include <sys/stat.h>
39#endif
40#include <regex.h> /* for regcomp(3) */
41#include <sys/file.h> /* for flock(2) */
42#include <signal.h> /* for pthread_sigmask(3) */
43#include <stdlib.h> /* for closefrom(3) */
44#include <sys/types.h>
45#include <sys/wait.h> /* for waitpid(2) */
46#ifndef HAVE_CLOSEFROM
47#include <dirent.h> /* for opendir(3) */
48#endif
49#ifdef HAVE_CAP
50#include <sys/capability.h>
51#endif /* HAVE_CAP */
52
53#include "asterisk/paths.h" /* use ast_config_AST_DATA_DIR */
54#include "asterisk/channel.h"
55#include "asterisk/pbx.h"
56#include "asterisk/file.h"
57#include "asterisk/app.h"
58#include "asterisk/dsp.h"
59#include "asterisk/utils.h"
60#include "asterisk/lock.h"
64#include "asterisk/test.h"
65#include "asterisk/module.h"
66#include "asterisk/astobj2.h"
67#include "asterisk/stasis.h"
69#include "asterisk/json.h"
71
72AST_THREADSTORAGE_PUBLIC(ast_str_thread_global_buf);
73
75
76struct zombie {
77 pid_t pid;
79};
80
82
83#ifdef HAVE_CAP
84static cap_t child_cap;
85#endif
86/*!
87 * \brief Define \ref stasis topic objects
88 * @{
89 */
92
93/*! @} */
94
95static void *shaun_of_the_dead(void *data)
96{
97 struct zombie *cur;
98 int status;
99 for (;;) {
100 if (!AST_LIST_EMPTY(&zombies)) {
101 /* Don't allow cancellation while we have a lock. */
102 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
105 if (waitpid(cur->pid, &status, WNOHANG) != 0) {
107 ast_free(cur);
108 }
109 }
112 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
113 }
114 pthread_testcancel();
115 /* Wait for 60 seconds, without engaging in a busy loop. */
116 ast_poll(NULL, 0, AST_LIST_FIRST(&zombies) ? 5000 : 60000);
117 }
118 return NULL;
119}
120
121
122#define AST_MAX_FORMATS 10
123
125
126/*!
127 * \brief This function presents a dialtone and reads an extension into 'collect'
128 * which must be a pointer to a **pre-initialized** array of char having a
129 * size of 'size' suitable for writing to. It will collect no more than the smaller
130 * of 'maxlen' or 'size' minus the original strlen() of collect digits.
131 * \param chan struct.
132 * \param context
133 * \param collect
134 * \param size
135 * \param maxlen
136 * \param timeout timeout in milliseconds
137*/
138int ast_app_dtget(struct ast_channel *chan, const char *context, char *collect, size_t size, int maxlen, int timeout)
139{
140 struct ast_tone_zone_sound *ts;
141 int res = 0, x = 0;
142
143 if (maxlen > size) {
144 maxlen = size;
145 }
146
147 if (!timeout) {
148 if (ast_channel_pbx(chan) && ast_channel_pbx(chan)->dtimeoutms) {
149 timeout = ast_channel_pbx(chan)->dtimeoutms;
150 } else {
151 timeout = 5000;
152 }
153 }
154
155 if ((ts = ast_get_indication_tone(ast_channel_zone(chan), "dial"))) {
156 res = ast_playtones_start(chan, 0, ts->data, 0);
158 } else {
159 ast_log(LOG_NOTICE, "Huh....? no dial for indications?\n");
160 }
161
162 for (x = strlen(collect); x < maxlen; ) {
163 res = ast_waitfordigit(chan, timeout);
164 if (!ast_ignore_pattern(context, collect)) {
165 ast_playtones_stop(chan);
166 }
167 if (res < 1) {
168 break;
169 }
170 if (res == '#') {
171 break;
172 }
173 collect[x++] = res;
174 if (!ast_matchmore_extension(chan, context, collect, 1,
175 S_COR(ast_channel_caller(chan)->id.number.valid, ast_channel_caller(chan)->id.number.str, NULL))) {
176 break;
177 }
178 }
179
180 if (res >= 0) {
181 res = ast_exists_extension(chan, context, collect, 1,
182 S_COR(ast_channel_caller(chan)->id.number.valid, ast_channel_caller(chan)->id.number.str, NULL)) ? 1 : 0;
183 }
184
185 return res;
186}
187
188enum ast_getdata_result ast_app_getdata(struct ast_channel *c, const char *prompt, char *s, int maxlen, int timeout)
189{
190 return ast_app_getdata_terminator(c, prompt, s, maxlen, timeout, NULL);
191}
192
194 int maxlen, int timeout, char *terminator)
195{
196 int res = 0, to, fto;
197 char *front, *filename;
198
199 /* XXX Merge with full version? XXX */
200
201 if (maxlen)
202 s[0] = '\0';
203
204 if (!prompt)
205 prompt = "";
206
207 filename = ast_strdupa(prompt);
208 while ((front = ast_strsep(&filename, '&', AST_STRSEP_STRIP | AST_STRSEP_TRIM))) {
209 if (!ast_strlen_zero(front)) {
210 res = ast_streamfile(c, front, ast_channel_language(c));
211 if (res)
212 continue;
213 }
214 if (ast_strlen_zero(filename)) {
215 /* set timeouts for the last prompt */
216 fto = ast_channel_pbx(c) ? ast_channel_pbx(c)->rtimeoutms : 6000;
218
219 if (timeout > 0) {
220 fto = to = timeout;
221 }
222 if (timeout < 0) {
223 fto = to = 1000000000;
224 }
225 } else {
226 /* there is more than one prompt, so
227 * get rid of the long timeout between
228 * prompts, and make it 50ms */
229 fto = 50;
231 }
232 res = ast_readstring(c, s, maxlen, to, fto, (terminator ? terminator : "#"));
234 return res;
235 }
236 if (!ast_strlen_zero(s)) {
237 return res;
238 }
239 }
240
241 return res;
242}
243
244/* The lock type used by ast_lock_path() / ast_unlock_path() */
246
247int ast_app_getdata_full(struct ast_channel *c, const char *prompt, char *s, int maxlen, int timeout, int audiofd, int ctrlfd)
248{
249 int res, to = 2000, fto = 6000;
250
251 if (!ast_strlen_zero(prompt)) {
253 if (res < 0) {
254 return res;
255 }
256 }
257
258 if (timeout > 0) {
259 fto = to = timeout;
260 }
261 if (timeout < 0) {
262 fto = to = 1000000000;
263 }
264
265 res = ast_readstring_full(c, s, maxlen, to, fto, "#", audiofd, ctrlfd);
266
267 return res;
268}
269
270/* BUGBUG this is not thread safe. */
272
274{
275 app_stack_callbacks = funcs;
276}
277
278const char *ast_app_expand_sub_args(struct ast_channel *chan, const char *args)
279{
280 const struct ast_app_stack_funcs *funcs;
281 const char *new_args;
282
283 funcs = app_stack_callbacks;
284 if (!funcs || !funcs->expand_sub_args || !ast_module_running_ref(funcs->module)) {
286 "Cannot expand 'Gosub(%s)' arguments. The app_stack module is not available.\n",
287 args);
288 return NULL;
289 }
290
291 new_args = funcs->expand_sub_args(chan, args);
292 ast_module_unref(funcs->module);
293
294 return new_args;
295}
296
297int ast_app_exec_sub(struct ast_channel *autoservice_chan, struct ast_channel *sub_chan, const char *sub_args, int ignore_hangup)
298{
299 const struct ast_app_stack_funcs *funcs;
300 int res;
301
302 funcs = app_stack_callbacks;
303 if (!funcs || !funcs->run_sub || !ast_module_running_ref(funcs->module)) {
305 "Cannot run 'Gosub(%s)'. The app_stack module is not available.\n",
306 sub_args);
307 return -1;
308 }
309
310 if (autoservice_chan) {
311 ast_autoservice_start(autoservice_chan);
312 }
313
314 res = funcs->run_sub(sub_chan, sub_args, ignore_hangup);
315 ast_module_unref(funcs->module);
316
317 if (autoservice_chan) {
318 ast_autoservice_stop(autoservice_chan);
319 }
320
321 if (!ignore_hangup && ast_check_hangup_locked(sub_chan)) {
322 ast_queue_hangup(sub_chan);
323 }
324
325 return res;
326}
327
328int ast_app_run_sub(struct ast_channel *autoservice_chan, struct ast_channel *sub_chan, const char *sub_location, const char *sub_args, int ignore_hangup)
329{
330 int res;
331 char *args_str;
332 size_t args_len;
333
334 if (ast_strlen_zero(sub_args)) {
335 return ast_app_exec_sub(autoservice_chan, sub_chan, sub_location, ignore_hangup);
336 }
337
338 /* Create the Gosub application argument string. */
339 args_len = strlen(sub_location) + strlen(sub_args) + 3;
340 args_str = ast_malloc(args_len);
341 if (!args_str) {
342 return -1;
343 }
344 snprintf(args_str, args_len, "%s(%s)", sub_location, sub_args);
345
346 res = ast_app_exec_sub(autoservice_chan, sub_chan, args_str, ignore_hangup);
347 ast_free(args_str);
348 return res;
349}
350
351/*! \brief The container for the voicemail provider */
352static AO2_GLOBAL_OBJ_STATIC(vm_provider);
353
354/*! Voicemail not registered warning */
355static int vm_warnings;
356
358{
359 struct ast_vm_functions *table;
360 int is_registered;
361
362 table = ao2_global_obj_ref(vm_provider);
363 is_registered = table ? 1 : 0;
365 return is_registered;
366}
367
369{
371
372 if (!vm_table->module_name) {
373 ast_log(LOG_ERROR, "Voicemail provider missing required information.\n");
374 return -1;
375 }
377 ast_log(LOG_ERROR, "Voicemail provider '%s' has incorrect version\n",
379 return -1;
380 }
381
382 table = ao2_global_obj_ref(vm_provider);
383 if (table) {
384 ast_log(LOG_WARNING, "Voicemail provider already registered by %s.\n",
385 table->module_name);
387 }
388
390 if (!table) {
391 return -1;
392 }
393 *table = *vm_table;
394 table->module = module;
395
397 return 0;
398}
399
401{
402 struct ast_vm_functions *table;
403
404 table = ao2_global_obj_ref(vm_provider);
405 if (table && !strcmp(table->module_name, module_name)) {
406 ao2_global_obj_release(vm_provider);
407 }
409}
410
411#ifdef TEST_FRAMEWORK
412/*! \brief Holding container for the voicemail provider used while testing */
413static AO2_GLOBAL_OBJ_STATIC(vm_provider_holder);
414static int provider_is_swapped = 0;
415
416void ast_vm_test_swap_table_in(const struct ast_vm_functions *vm_table)
417{
418 RAII_VAR(struct ast_vm_functions *, holding_table, NULL, ao2_cleanup);
419 RAII_VAR(struct ast_vm_functions *, new_table, NULL, ao2_cleanup);
420
421 if (provider_is_swapped) {
422 ast_log(LOG_ERROR, "Attempted to swap in test function table without swapping out old test table.\n");
423 return;
424 }
425
426 holding_table = ao2_global_obj_ref(vm_provider);
427
428 if (holding_table) {
429 ao2_global_obj_replace_unref(vm_provider_holder, holding_table);
430 }
431
432 new_table = ao2_alloc_options(sizeof(*new_table), NULL, AO2_ALLOC_OPT_LOCK_NOLOCK);
433 if (!new_table) {
434 return;
435 }
436 *new_table = *vm_table;
437
438 ao2_global_obj_replace_unref(vm_provider, new_table);
439 provider_is_swapped = 1;
440}
441
442void ast_vm_test_swap_table_out(void)
443{
444 RAII_VAR(struct ast_vm_functions *, held_table, NULL, ao2_cleanup);
445
446 if (!provider_is_swapped) {
447 ast_log(LOG_ERROR, "Attempted to swap out test function table, but none is currently installed.\n");
448 return;
449 }
450
451 held_table = ao2_global_obj_ref(vm_provider_holder);
452 if (!held_table) {
453 return;
454 }
455
456 ao2_global_obj_replace_unref(vm_provider, held_table);
457 ao2_global_obj_release(vm_provider_holder);
458 provider_is_swapped = 0;
459}
460#endif
461
462/*! \brief The container for the voicemail greeter provider */
463static AO2_GLOBAL_OBJ_STATIC(vm_greeter_provider);
464
465/*! Voicemail greeter not registered warning */
467
469{
471 int is_registered;
472
473 table = ao2_global_obj_ref(vm_greeter_provider);
474 is_registered = table ? 1 : 0;
476 return is_registered;
477}
478
480{
482
483 if (!vm_table->module_name) {
484 ast_log(LOG_ERROR, "Voicemail greeter provider missing required information.\n");
485 return -1;
486 }
488 ast_log(LOG_ERROR, "Voicemail greeter provider '%s' has incorrect version\n",
490 return -1;
491 }
492
493 table = ao2_global_obj_ref(vm_greeter_provider);
494 if (table) {
495 ast_log(LOG_WARNING, "Voicemail greeter provider already registered by %s.\n",
496 table->module_name);
498 }
499
501 if (!table) {
502 return -1;
503 }
504 *table = *vm_table;
505 table->module = module;
506
507 ao2_global_obj_replace_unref(vm_greeter_provider, table);
508 return 0;
509}
510
512{
514
515 table = ao2_global_obj_ref(vm_greeter_provider);
516 if (table && !strcmp(table->module_name, module_name)) {
517 ao2_global_obj_release(vm_greeter_provider);
518 }
520}
521
522#ifdef TEST_FRAMEWORK
523static ast_vm_test_create_user_fn *ast_vm_test_create_user_func = NULL;
524static ast_vm_test_destroy_user_fn *ast_vm_test_destroy_user_func = NULL;
525
526void ast_install_vm_test_functions(ast_vm_test_create_user_fn *vm_test_create_user_func,
527 ast_vm_test_destroy_user_fn *vm_test_destroy_user_func)
528{
529 ast_vm_test_create_user_func = vm_test_create_user_func;
530 ast_vm_test_destroy_user_func = vm_test_destroy_user_func;
531}
532
533void ast_uninstall_vm_test_functions(void)
534{
535 ast_vm_test_create_user_func = NULL;
536 ast_vm_test_destroy_user_func = NULL;
537}
538#endif
539
540static void vm_warn_no_provider(void)
541{
542 if (vm_warnings++ % 10 == 0) {
543 ast_verb(3, "No voicemail provider registered.\n");
544 }
545}
546
547#define VM_API_CALL(res, api_call, api_parms) \
548 do { \
549 struct ast_vm_functions *table; \
550 table = ao2_global_obj_ref(vm_provider); \
551 if (!table) { \
552 vm_warn_no_provider(); \
553 } else if (table->api_call) { \
554 ast_module_ref(table->module); \
555 (res) = table->api_call api_parms; \
556 ast_module_unref(table->module); \
557 } \
558 ao2_cleanup(table); \
559 } while (0)
560
562{
563 if (vm_greeter_warnings++ % 10 == 0) {
564 ast_verb(3, "No voicemail greeter provider registered.\n");
565 }
566}
567
568#define VM_GREETER_API_CALL(res, api_call, api_parms) \
569 do { \
570 struct ast_vm_greeter_functions *table; \
571 table = ao2_global_obj_ref(vm_greeter_provider); \
572 if (!table) { \
573 vm_greeter_warn_no_provider(); \
574 } else if (table->api_call) { \
575 ast_module_ref(table->module); \
576 (res) = table->api_call api_parms; \
577 ast_module_unref(table->module); \
578 } \
579 ao2_cleanup(table); \
580 } while (0)
581
582int ast_app_has_voicemail(const char *mailboxes, const char *folder)
583{
584 int res = 0;
585
586 VM_API_CALL(res, has_voicemail, (mailboxes, folder));
587 return res;
588}
589
590/*!
591 * \internal
592 * \brief Function used as a callback for ast_copy_recording_to_vm when a real one isn't installed.
593 * \param vm_rec_data Stores crucial information about the voicemail that will basically just be used
594 * to figure out what the name of the recipient was supposed to be
595 */
597{
598 int res = -1;
599
600 VM_API_CALL(res, copy_recording_to_vm, (vm_rec_data));
601 return res;
602}
603
604int ast_app_inboxcount(const char *mailboxes, int *newmsgs, int *oldmsgs)
605{
606 int res = 0;
607
608 if (newmsgs) {
609 *newmsgs = 0;
610 }
611 if (oldmsgs) {
612 *oldmsgs = 0;
613 }
614
615 VM_API_CALL(res, inboxcount, (mailboxes, newmsgs, oldmsgs));
616 return res;
617}
618
619int ast_app_inboxcount2(const char *mailboxes, int *urgentmsgs, int *newmsgs, int *oldmsgs)
620{
621 int res = 0;
622
623 if (newmsgs) {
624 *newmsgs = 0;
625 }
626 if (oldmsgs) {
627 *oldmsgs = 0;
628 }
629 if (urgentmsgs) {
630 *urgentmsgs = 0;
631 }
632
633 VM_API_CALL(res, inboxcount2, (mailboxes, urgentmsgs, newmsgs, oldmsgs));
634 return res;
635}
636
637int ast_app_sayname(struct ast_channel *chan, const char *mailbox_id)
638{
639 int res = -1;
640
641 VM_GREETER_API_CALL(res, sayname, (chan, mailbox_id));
642 return res;
643}
644
645int ast_app_messagecount(const char *mailbox_id, const char *folder)
646{
647 int res = 0;
648
649 VM_API_CALL(res, messagecount, (mailbox_id, folder));
650 return res;
651}
652
653const char *ast_vm_index_to_foldername(int id)
654{
655 const char *res = NULL;
656
657 VM_API_CALL(res, index_to_foldername, (id));
658 return res;
659}
660
662 const char *context,
663 const char *folder,
664 int descending,
665 enum ast_vm_snapshot_sort_val sort_val,
666 int combine_INBOX_and_OLD)
667{
668 struct ast_vm_mailbox_snapshot *res = NULL;
669
670 VM_API_CALL(res, mailbox_snapshot_create, (mailbox, context, folder, descending,
671 sort_val, combine_INBOX_and_OLD));
672 return res;
673}
674
676{
677 struct ast_vm_mailbox_snapshot *res = NULL;
678
679 VM_API_CALL(res, mailbox_snapshot_destroy, (mailbox_snapshot));
680 return res;
681}
682
683int ast_vm_msg_move(const char *mailbox,
684 const char *context,
685 size_t num_msgs,
686 const char *oldfolder,
687 const char *old_msg_ids[],
688 const char *newfolder)
689{
690 int res = 0;
691
692 VM_API_CALL(res, msg_move, (mailbox, context, num_msgs, oldfolder, old_msg_ids,
693 newfolder));
694 return res;
695}
696
698 const char *context,
699 size_t num_msgs,
700 const char *folder,
701 const char *msgs[])
702{
703 int res = 0;
704
705 VM_API_CALL(res, msg_remove, (mailbox, context, num_msgs, folder, msgs));
706 return res;
707}
708
710 const char *from_context,
711 const char *from_folder,
712 const char *to_mailbox,
713 const char *to_context,
714 const char *to_folder,
715 size_t num_msgs,
716 const char *msg_ids[],
717 int delete_old)
718{
719 int res = 0;
720
721 VM_API_CALL(res, msg_forward, (from_mailbox, from_context, from_folder, to_mailbox,
722 to_context, to_folder, num_msgs, msg_ids, delete_old));
723 return res;
724}
725
727 const char *mailbox,
728 const char *context,
729 const char *folder,
730 const char *msg_num,
732{
733 int res = 0;
734
735 VM_API_CALL(res, msg_play, (chan, mailbox, context, folder, msg_num, cb));
736 return res;
737}
738
739#ifdef TEST_FRAMEWORK
740int ast_vm_test_create_user(const char *context, const char *mailbox)
741{
742 if (ast_vm_test_create_user_func) {
743 return ast_vm_test_create_user_func(context, mailbox);
744 }
745 return 0;
746}
747
748int ast_vm_test_destroy_user(const char *context, const char *mailbox)
749{
750 if (ast_vm_test_destroy_user_func) {
751 return ast_vm_test_destroy_user_func(context, mailbox);
752 }
753 return 0;
754}
755#endif
756
757static int external_sleep(struct ast_channel *chan, int ms)
758{
759 usleep(ms * 1000);
760 return 0;
761}
762
763static int sf_stream(struct ast_channel *chan, struct ast_channel *chan2, const char *digits, int frequency, int is_external)
764{
765 /* Bell System Technical Journal 39 (Nov. 1960) */
766 #define SF_ON 67
767 #define SF_OFF 33
768 #define SF_BETWEEN 600
769
770 const char *ptr;
771 int res;
772 struct ast_silence_generator *silgen = NULL, *silgen2 = NULL;
773 char *freq;
774 int (*my_sleep)(struct ast_channel *chan, int ms);
775
776 if (frequency >= 100000) {
777 ast_log(LOG_WARNING, "Frequency too large: %d\n", frequency);
778 return -1;
779 }
780
781 if (is_external) {
782 my_sleep = external_sleep;
783 } else {
784 my_sleep = ast_safe_sleep;
785 }
786
787 /* Need a quiet time before sending digits. */
790 if (chan2) {
792 }
793 }
794 if (chan2) {
796 }
797 res = my_sleep(chan, 100);
798 if (chan2) {
800 }
801 if (res) {
802 goto sf_stream_cleanup;
803 }
804
805/* len(SF_ON) + len(SF_OFF) + len(0) + maxlen(frequency) + /,/ + null terminator = 2 + 2 + 1 + 5 at most + 3 + 1 = 14 */
806#define SF_BUF_LEN 20
807 freq = ast_alloca(SF_BUF_LEN); /* min 20 to avoid compiler warning about insufficient buffer */
808 /* pauses need to send audio, so send 0 Hz */
809 snprintf(freq, SF_BUF_LEN, "%d/%d,%d/%d", frequency, SF_ON, 0, SF_OFF);
810
811 for (ptr = digits; *ptr; ptr++) {
812 if (*ptr == 'w') {
813 /* 'w' -- wait half a second */
814 if (chan2) {
816 }
817 res = my_sleep(chan, 500);
818 if (chan2) {
820 }
821 if (res) {
822 break;
823 }
824 } else if (*ptr == 'h' || *ptr == 'H') {
825 /* 'h' -- 2600 Hz for half a second, but
826 only to far end of trunk, not near end */
827 ast_playtones_start(chan, 0, "2600", 0);
828 if (chan2) {
829 ast_playtones_start(chan2, 0, "0", 0);
831 }
832 res = my_sleep(chan, 250);
834 if (chan2) {
837 }
838 if (res) {
839 break;
840 }
841 } else if (strchr("0123456789*#ABCDabcdwWfF", *ptr)) {
842 if (*ptr == 'f' || *ptr == 'F') {
843 /* ignore return values if not supported by channel */
845 } else if (*ptr == 'W') {
846 /* ignore return values if not supported by channel */
848 } else {
849 /* Character represents valid SF */
850 int beeps;
851 if (*ptr == '*') {
852 beeps = 11;
853 } else if (*ptr == '#') {
854 beeps = 12;
855 } else if (*ptr == 'D') {
856 beeps = 13;
857 } else if (*ptr == 'C') {
858 beeps = 14;
859 } else if (*ptr == 'B') {
860 beeps = 15;
861 } else if (*ptr == 'A') {
862 beeps = 16;
863 } else {
864 beeps = (*ptr == '0') ? 10 : *ptr - '0';
865 }
866 while (beeps-- > 0) {
867 ast_playtones_start(chan, 0, freq, 0);
868 if (chan2) {
869 ast_playtones_start(chan2, 0, freq, 0);
871 }
872 res = my_sleep(chan, SF_ON + SF_OFF);
874 if (chan2) {
877 }
878 if (res) {
879 break;
880 }
881 }
882 }
883 /* pause between digits */
884 ast_playtones_start(chan, 0, "0", 0);
885 if (chan2) {
886 ast_playtones_start(chan2, 0, "0", 0);
888 }
889 res = my_sleep(chan, SF_BETWEEN);
890 if (chan2) {
893 }
895 if (res) {
896 break;
897 }
898 } else {
899 ast_log(LOG_WARNING, "Illegal SF character '%c' in string. (0-9A-DwWfFhH allowed)\n", *ptr);
900 }
901 }
902
903sf_stream_cleanup:
904 if (silgen) {
906 }
907 if (silgen2) {
909 }
910
911 return res;
912}
913
914static int mf_stream(struct ast_channel *chan, struct ast_channel *chan2, const char *digits, int between, unsigned int duration,
915 unsigned int durationkp, unsigned int durationst, int is_external)
916{
917 const char *ptr;
918 int res;
919 struct ast_silence_generator *silgen = NULL, *silgen2 = NULL;
920 int (*my_sleep)(struct ast_channel *chan, int ms);
921
922 if (is_external) {
923 my_sleep = external_sleep;
924 } else {
925 my_sleep = ast_safe_sleep;
926 }
927
928 if (!between) {
929 between = 100;
930 }
931
932 /* Need a quiet time before sending digits. */
935 if (chan2) {
937 }
938 }
939 if (chan2) {
941 }
942 res = my_sleep(chan, 100);
943 if (chan2) {
945 }
946 if (res) {
947 goto mf_stream_cleanup;
948 }
949
950 for (ptr = digits; *ptr; ptr++) {
951 if (*ptr == 'w') {
952 /* 'w' -- wait half a second */
953 if (chan2) {
955 }
956 res = my_sleep(chan, 500);
957 if (chan2) {
959 }
960 if (res) {
961 break;
962 }
963 } else if (*ptr == 'h' || *ptr == 'H') {
964 /* 'h' -- 2600 Hz for half a second, but
965 only to far end of trunk, not near end */
966 ast_playtones_start(chan, 0, "2600", 0);
967 if (chan2) {
968 ast_playtones_start(chan2, 0, "0", 0);
970 }
971 res = my_sleep(chan, 250);
973 if (chan2) {
976 }
977 if (res) {
978 break;
979 }
980 } else if (strchr("0123456789*#ABCwWfF", *ptr)) {
981 if (*ptr == 'f' || *ptr == 'F') {
982 /* ignore return values if not supported by channel */
984 } else if (*ptr == 'W') {
985 /* ignore return values if not supported by channel */
987 } else {
988 /* Character represents valid MF */
989 ast_senddigit_mf(chan, *ptr, duration, durationkp, durationst, is_external);
990 if (chan2) {
991 ast_senddigit_mf(chan2, *ptr, duration, durationkp, durationst, is_external);
992 }
993 }
994 /* pause between digits */
995 /* The DSP code in Asterisk does not currently properly receive repeated tones
996 if no audio is sent in the middle. Simply sending audio (even 0 Hz)
997 works around this limitation and guarantees the correct behavior.
998 */
999 ast_playtones_start(chan, 0, "0", 0);
1000 if (chan2) {
1001 ast_playtones_start(chan2, 0, "0", 0);
1002 ast_autoservice_start(chan2);
1003 }
1004 res = my_sleep(chan, between);
1006 if (chan2) {
1007 ast_autoservice_stop(chan2);
1008 ast_senddigit_mf_end(chan2);
1009 }
1010 if (res) {
1011 break;
1012 }
1013 } else {
1014 ast_log(LOG_WARNING, "Illegal MF character '%c' in string. (0-9*#ABCwWfFhH allowed)\n", *ptr);
1015 }
1016 }
1017
1018mf_stream_cleanup:
1019 if (silgen) {
1021 }
1022 if (silgen2) {
1023 ast_channel_stop_silence_generator(chan2, silgen2);
1024 }
1025
1026 return res;
1027}
1028
1029static int dtmf_stream(struct ast_channel *chan, const char *digits, int between, unsigned int duration, int is_external)
1030{
1031 const char *ptr;
1032 int res;
1033 struct ast_silence_generator *silgen = NULL;
1034 int (*my_sleep)(struct ast_channel *chan, int ms);
1035 int (*my_senddigit)(struct ast_channel *chan, char digit, unsigned int duration);
1036
1037 if (is_external) {
1038 my_sleep = external_sleep;
1039 my_senddigit = ast_senddigit_external;
1040 } else {
1041 my_sleep = ast_safe_sleep;
1042 my_senddigit = ast_senddigit;
1043 }
1044
1045 if (!between) {
1046 between = 100;
1047 }
1048
1049 /* Need a quiet time before sending digits. */
1052 }
1053 res = my_sleep(chan, 100);
1054 if (res) {
1055 goto dtmf_stream_cleanup;
1056 }
1057
1058 for (ptr = digits; *ptr; ptr++) {
1059 if (*ptr == 'w') {
1060 /* 'w' -- wait half a second */
1061 res = my_sleep(chan, 500);
1062 if (res) {
1063 break;
1064 }
1065 } else if (*ptr == 'W') {
1066 /* 'W' -- wait a second */
1067 res = my_sleep(chan, 1000);
1068 if (res) {
1069 break;
1070 }
1071 } else if (strchr("0123456789*#abcdfABCDF", *ptr)) {
1072 if (*ptr == 'f' || *ptr == 'F') {
1073 /* ignore return values if not supported by channel */
1075 } else {
1076 /* Character represents valid DTMF */
1077 my_senddigit(chan, *ptr, duration);
1078 }
1079 /* pause between digits */
1080 res = my_sleep(chan, between);
1081 if (res) {
1082 break;
1083 }
1084 } else {
1085 ast_log(LOG_WARNING, "Illegal DTMF character '%c' in string. (0-9*#aAbBcCdD allowed)\n", *ptr);
1086 }
1087 }
1088
1089dtmf_stream_cleanup:
1090 if (silgen) {
1092 }
1093
1094 return res;
1095}
1096
1097int ast_sf_stream(struct ast_channel *chan, struct ast_channel *peer, struct ast_channel *chan2, const char *digits, int frequency, int is_external)
1098{
1099 int res;
1100 if (frequency <= 0) {
1101 frequency = 2600;
1102 }
1103 if (!is_external && !chan2 && peer && ast_autoservice_start(peer)) {
1104 return -1;
1105 }
1106 res = sf_stream(chan, chan2, digits, frequency, is_external);
1107 if (!is_external && !chan2 && peer && ast_autoservice_stop(peer)) {
1108 res = -1;
1109 }
1110 return res;
1111}
1112
1113int ast_mf_stream(struct ast_channel *chan, struct ast_channel *peer, struct ast_channel *chan2, const char *digits,
1114 int between, unsigned int duration, unsigned int durationkp, unsigned int durationst, int is_external)
1115{
1116 int res;
1117 if (!is_external && !chan2 && peer && ast_autoservice_start(peer)) {
1118 return -1;
1119 }
1120 res = mf_stream(chan, chan2, digits, between, duration, durationkp, durationst, is_external);
1121 if (!is_external && !chan2 && peer && ast_autoservice_stop(peer)) {
1122 res = -1;
1123 }
1124 return res;
1125}
1126
1127int ast_dtmf_stream(struct ast_channel *chan, struct ast_channel *peer, const char *digits, int between, unsigned int duration)
1128{
1129 int res;
1130
1131 if (peer && ast_autoservice_start(peer)) {
1132 return -1;
1133 }
1134 res = dtmf_stream(chan, digits, between, duration, 0);
1135 if (peer && ast_autoservice_stop(peer)) {
1136 res = -1;
1137 }
1138
1139 return res;
1140}
1141
1142void ast_dtmf_stream_external(struct ast_channel *chan, const char *digits, int between, unsigned int duration)
1143{
1144 dtmf_stream(chan, digits, between, duration, 1);
1145}
1146
1148 int fd;
1152};
1153
1154static void linear_release(struct ast_channel *chan, void *params)
1155{
1156 struct linear_state *ls = params;
1157
1158 if (ls->origwfmt && ast_set_write_format(chan, ls->origwfmt)) {
1159 ast_log(LOG_WARNING, "Unable to restore channel '%s' to format '%s'\n",
1161 }
1162 ao2_cleanup(ls->origwfmt);
1163
1164 if (ls->autoclose) {
1165 close(ls->fd);
1166 }
1167
1168 ast_free(params);
1169}
1170
1171static int linear_generator(struct ast_channel *chan, void *data, int len, int samples)
1172{
1173 short buf[2048 + AST_FRIENDLY_OFFSET / 2];
1174 struct linear_state *ls = data;
1175 struct ast_frame f = {
1177 .data.ptr = buf + AST_FRIENDLY_OFFSET / 2,
1178 .offset = AST_FRIENDLY_OFFSET,
1179 };
1180 int res;
1181
1183
1184 len = samples * 2;
1185 if (len > sizeof(buf) - AST_FRIENDLY_OFFSET) {
1186 ast_log(LOG_WARNING, "Can't generate %d bytes of data!\n" , len);
1187 len = sizeof(buf) - AST_FRIENDLY_OFFSET;
1188 }
1189 res = read(ls->fd, buf + AST_FRIENDLY_OFFSET/2, len);
1190 if (res > 0) {
1191 f.datalen = res;
1192 f.samples = res / 2;
1193 ast_write(chan, &f);
1194 if (res == len) {
1195 return 0;
1196 }
1197 }
1198 return -1;
1199}
1200
1201static void *linear_alloc(struct ast_channel *chan, void *params)
1202{
1203 struct linear_state *ls = params;
1204
1205 if (!params) {
1206 return NULL;
1207 }
1208
1209 /* In this case, params is already malloc'd */
1210 if (ls->allowoverride) {
1212 } else {
1214 }
1215
1217
1219 ast_log(LOG_WARNING, "Unable to set '%s' to linear format (write)\n", ast_channel_name(chan));
1220 ao2_cleanup(ls->origwfmt);
1221 ast_free(ls);
1222 ls = params = NULL;
1223 }
1224
1225 return params;
1226}
1227
1229{
1231 .release = linear_release,
1232 .generate = linear_generator,
1233};
1234
1235int ast_linear_stream(struct ast_channel *chan, const char *filename, int fd, int allowoverride)
1236{
1237 struct linear_state *lin;
1238 char tmpf[256];
1239 int autoclose = 0;
1240
1241 if (fd < 0) {
1242 if (ast_strlen_zero(filename)) {
1243 return -1;
1244 }
1245
1246 autoclose = 1;
1247
1248 if (filename[0] == '/') {
1249 ast_copy_string(tmpf, filename, sizeof(tmpf));
1250 } else {
1251 snprintf(tmpf, sizeof(tmpf), "%s/%s/%s", ast_config_AST_DATA_DIR, "sounds", filename);
1252 }
1253
1254 fd = open(tmpf, O_RDONLY);
1255 if (fd < 0) {
1256 ast_log(LOG_WARNING, "Unable to open file '%s': %s\n", tmpf, strerror(errno));
1257 return -1;
1258 }
1259 }
1260
1261 lin = ast_calloc(1, sizeof(*lin));
1262 if (!lin) {
1263 if (autoclose) {
1264 close(fd);
1265 }
1266
1267 return -1;
1268 }
1269
1270 lin->fd = fd;
1272 lin->autoclose = autoclose;
1273
1274 return ast_activate_generator(chan, &linearstream, lin);
1275}
1276
1277static int control_streamfile(struct ast_channel *chan,
1278 const char *file,
1279 const char *fwd,
1280 const char *rev,
1281 const char *stop,
1282 const char *suspend,
1283 const char *restart,
1284 int skipms,
1285 long *offsetms,
1286 const char *lang,
1288{
1289 char *breaks = NULL;
1290 char *end = NULL;
1291 int blen = 2;
1292 int res;
1293 long pause_restart_point = 0;
1294 long offset = 0;
1295 struct ast_silence_generator *silgen = NULL;
1296
1297 if (!file) {
1298 return -1;
1299 }
1300 if (offsetms) {
1301 offset = *offsetms * 8; /* XXX Assumes 8kHz */
1302 }
1303 if (lang == NULL) {
1304 lang = ast_channel_language(chan);
1305 }
1306
1307 if (stop) {
1308 blen += strlen(stop);
1309 }
1310 if (suspend) {
1311 blen += strlen(suspend);
1312 }
1313 if (restart) {
1314 blen += strlen(restart);
1315 }
1316
1317 if (blen > 2) {
1318 breaks = ast_alloca(blen + 1);
1319 breaks[0] = '\0';
1320 if (stop) {
1321 strcat(breaks, stop);
1322 }
1323 if (suspend) {
1324 strcat(breaks, suspend);
1325 }
1326 if (restart) {
1327 strcat(breaks, restart);
1328 }
1329 }
1330
1331 if ((end = strchr(file, ':'))) {
1332 if (!strcasecmp(end, ":end")) {
1333 *end = '\0';
1334 end++;
1335 } else {
1336 end = NULL;
1337 }
1338 }
1339
1340 for (;;) {
1341 ast_stopstream(chan);
1342 res = ast_streamfile(chan, file, lang);
1343 if (!res) {
1344 if (pause_restart_point) {
1345 ast_seekstream(ast_channel_stream(chan), pause_restart_point, SEEK_SET);
1346 pause_restart_point = 0;
1347 }
1348 else if (end || offset < 0) {
1349 if (offset == -8) {
1350 offset = 0;
1351 }
1352 ast_verb(3, "ControlPlayback seek to offset %ld from end\n", offset);
1353
1354 ast_seekstream(ast_channel_stream(chan), offset, SEEK_END);
1355 end = NULL;
1356 offset = 0;
1357 } else if (offset) {
1358 ast_verb(3, "ControlPlayback seek to offset %ld\n", offset);
1359 ast_seekstream(ast_channel_stream(chan), offset, SEEK_SET);
1360 offset = 0;
1361 }
1362 if (cb) {
1363 res = ast_waitstream_fr_w_cb(chan, breaks, fwd, rev, skipms, cb);
1364 } else {
1365 res = ast_waitstream_fr(chan, breaks, fwd, rev, skipms);
1366 }
1367 }
1368
1369 if (res < 1) {
1370 break;
1371 }
1372
1373 /* We go at next loop if we got the restart char */
1374 if ((restart && strchr(restart, res)) || res == AST_CONTROL_STREAM_RESTART) {
1375 ast_debug(1, "we'll restart the stream here at next loop\n");
1376 pause_restart_point = 0;
1377 ast_test_suite_event_notify("PLAYBACK","Channel: %s\r\n"
1378 "Control: %s\r\n",
1379 ast_channel_name(chan),
1380 "Restart");
1381 continue;
1382 }
1383
1384 if ((suspend && strchr(suspend, res)) || res == AST_CONTROL_STREAM_SUSPEND) {
1385 pause_restart_point = ast_tellstream(ast_channel_stream(chan));
1386
1389 }
1390 ast_test_suite_event_notify("PLAYBACK","Channel: %s\r\n"
1391 "Control: %s\r\n",
1392 ast_channel_name(chan),
1393 "Pause");
1394 for (;;) {
1395 ast_stopstream(chan);
1396 if (!(res = ast_waitfordigit(chan, 1000))) {
1397 continue;
1398 } else if (res == -1 || (suspend && strchr(suspend, res)) || (stop && strchr(stop, res))
1400 break;
1401 }
1402 }
1403 if (silgen) {
1405 silgen = NULL;
1406 }
1407
1408 if ((suspend && (res == *suspend)) || res == AST_CONTROL_STREAM_SUSPEND) {
1409 res = 0;
1410 ast_test_suite_event_notify("PLAYBACK","Channel: %s\r\n"
1411 "Control: %s\r\n",
1412 ast_channel_name(chan),
1413 "Unpause");
1414 continue;
1415 }
1416 }
1417
1418 if (res == -1) {
1419 break;
1420 }
1421
1422 /* if we get one of our stop chars, return it to the calling function */
1423 if ((stop && strchr(stop, res)) || res == AST_CONTROL_STREAM_STOP) {
1424 ast_test_suite_event_notify("PLAYBACK","Channel: %s\r\n"
1425 "Control: %s\r\n",
1426 ast_channel_name(chan),
1427 "Stop");
1428 break;
1429 }
1430 }
1431
1432 if (pause_restart_point) {
1433 offset = pause_restart_point;
1434 } else {
1435 if (ast_channel_stream(chan)) {
1436 offset = ast_tellstream(ast_channel_stream(chan));
1437 } else {
1438 offset = -8; /* indicate end of file */
1439 }
1440 }
1441
1442 if (offsetms) {
1443 *offsetms = offset / 8; /* samples --> ms ... XXX Assumes 8 kHz */
1444 }
1445
1446 ast_stopstream(chan);
1447
1448 return res;
1449}
1450
1452 const char *file,
1453 const char *fwd,
1454 const char *rev,
1455 const char *stop,
1456 const char *suspend,
1457 const char *restart,
1458 int skipms,
1459 long *offsetms,
1461{
1462 return control_streamfile(chan, file, fwd, rev, stop, suspend, restart, skipms, offsetms, NULL, cb);
1463}
1464
1465int ast_control_streamfile(struct ast_channel *chan, const char *file,
1466 const char *fwd, const char *rev,
1467 const char *stop, const char *suspend,
1468 const char *restart, int skipms, long *offsetms)
1469{
1470 return control_streamfile(chan, file, fwd, rev, stop, suspend, restart, skipms, offsetms, NULL, NULL);
1471}
1472
1473int ast_control_streamfile_lang(struct ast_channel *chan, const char *file,
1474 const char *fwd, const char *rev, const char *stop, const char *suspend,
1475 const char *restart, int skipms, const char *lang, long *offsetms)
1476{
1477 return control_streamfile(chan, file, fwd, rev, stop, suspend, restart, skipms, offsetms, lang, NULL);
1478}
1479
1484};
1485
1486static enum control_tone_frame_response_result control_tone_frame_response(struct ast_channel *chan, struct ast_frame *fr, struct ast_tone_zone_sound *ts, const char *tone, int *paused)
1487{
1488 switch (fr->subclass.integer) {
1490 ast_playtones_stop(chan);
1493 if (*paused) {
1494 *paused = 0;
1495 if (ast_playtones_start(chan, 0, ts ? ts->data : tone, 0)) {
1497 }
1498 } else {
1499 *paused = 1;
1500 ast_playtones_stop(chan);
1501 }
1504 ast_playtones_stop(chan);
1505 if (ast_playtones_start(chan, 0, ts ? ts->data : tone, 0)) {
1507 }
1510 ast_log(LOG_NOTICE, "Media control operation 'reverse' not supported for media type 'tone'\n");
1513 ast_log(LOG_NOTICE, "Media control operation 'forward' not supported for media type 'tone'\n");
1515 case AST_CONTROL_HANGUP:
1516 case AST_CONTROL_BUSY:
1519 }
1520
1522}
1523
1524static int parse_tone_uri(char *tone_parser,
1525 const char **tone_indication,
1526 const char **tone_zone)
1527{
1528 *tone_indication = strsep(&tone_parser, ";");
1529
1530 if (ast_strlen_zero(tone_parser)) {
1531 /* Only the indication is included */
1532 return 0;
1533 }
1534
1535 if (!(strncmp(tone_parser, "tonezone=", 9))) {
1536 *tone_zone = tone_parser + 9;
1537 } else {
1538 ast_log(LOG_ERROR, "Unexpected Tone URI component: %s\n", tone_parser);
1539 return -1;
1540 }
1541
1542 return 0;
1543}
1544
1545int ast_control_tone(struct ast_channel *chan, const char *tone)
1546{
1547 struct ast_tone_zone *zone = NULL;
1548 struct ast_tone_zone_sound *ts;
1549 int paused = 0;
1550 int res = 0;
1551
1552 const char *tone_indication = NULL;
1553 const char *tone_zone = NULL;
1554 char *tone_uri_parser;
1555
1556 if (ast_strlen_zero(tone)) {
1557 return -1;
1558 }
1559
1560 tone_uri_parser = ast_strdupa(tone);
1561
1562 if (parse_tone_uri(tone_uri_parser, &tone_indication, &tone_zone)) {
1563 return -1;
1564 }
1565
1566 if (tone_zone) {
1567 zone = ast_get_indication_zone(tone_zone);
1568 }
1569
1570 ts = ast_get_indication_tone(zone ? zone : ast_channel_zone(chan), tone_indication);
1571
1572 if (ast_playtones_start(chan, 0, ts ? ts->data : tone_indication, 0)) {
1573 res = -1;
1574 }
1575
1576 while (!res) {
1577 struct ast_frame *fr;
1578
1579 if (ast_waitfor(chan, -1) < 0) {
1580 res = -1;
1581 break;
1582 }
1583
1584 fr = ast_read_noaudio(chan);
1585
1586 if (!fr) {
1587 res = -1;
1588 break;
1589 }
1590
1591 if (fr->frametype != AST_FRAME_CONTROL) {
1592 continue;
1593 }
1594
1595 res = control_tone_frame_response(chan, fr, ts, tone_indication, &paused);
1596 if (res == CONTROL_TONE_RESPONSE_FINISHED) {
1597 res = 0;
1598 break;
1599 } else if (res == CONTROL_TONE_RESPONSE_FAILED) {
1600 res = -1;
1601 break;
1602 }
1603 }
1604
1605 if (ts) {
1607 }
1608
1609 if (zone) {
1610 ast_tone_zone_unref(zone);
1611 }
1612
1613 return res;
1614}
1615
1616int ast_play_and_wait(struct ast_channel *chan, const char *fn)
1617{
1618 int d = 0;
1619
1620 if ((d = ast_streamfile(chan, fn, ast_channel_language(chan)))) {
1621 return d;
1622 }
1623
1625
1626 ast_stopstream(chan);
1627
1628 return d;
1629}
1630
1631/*!
1632 * \brief Construct a silence frame of the same duration as \a orig.
1633 *
1634 * The \a orig frame must be \ref ast_format_slin.
1635 *
1636 * \param orig Frame as basis for silence to generate.
1637 * \return New frame of silence; free with ast_frfree().
1638 * \retval NULL on error.
1639 */
1640static struct ast_frame *make_silence(const struct ast_frame *orig)
1641{
1642 struct ast_frame *silence;
1643 size_t size;
1644 size_t datalen;
1645 size_t samples = 0;
1646
1647 if (!orig) {
1648 return NULL;
1649 }
1650 do {
1652 ast_log(LOG_WARNING, "Attempting to silence non-slin frame\n");
1653 return NULL;
1654 }
1655
1656 samples += orig->samples;
1657
1658 orig = AST_LIST_NEXT(orig, frame_list);
1659 } while (orig);
1660
1661 ast_verb(4, "Silencing %zu samples\n", samples);
1662
1663
1664 datalen = sizeof(short) * samples;
1665 size = sizeof(*silence) + datalen;
1666 silence = ast_calloc(1, size);
1667 if (!silence) {
1668 return NULL;
1669 }
1670
1671 silence->mallocd = AST_MALLOCD_HDR;
1672 silence->frametype = AST_FRAME_VOICE;
1673 silence->data.ptr = (void *)(silence + 1);
1674 silence->samples = samples;
1675 silence->datalen = datalen;
1676
1678
1679 return silence;
1680}
1681
1682/*!
1683 * \brief Sets a channel's read format to \ref ast_format_slin, recording
1684 * its original format.
1685 *
1686 * \param chan Channel to modify.
1687 * \param[out] orig_format Output variable to store channel's original read
1688 * format.
1689 * \return 0 on success.
1690 * \return -1 on error.
1691 */
1692static int set_read_to_slin(struct ast_channel *chan, struct ast_format **orig_format)
1693{
1694 if (!chan || !orig_format) {
1695 return -1;
1696 }
1697 *orig_format = ao2_bump(ast_channel_readformat(chan));
1699}
1700
1702static int global_maxsilence = 0;
1703
1704/*! Optionally play a sound file or a beep, then record audio and video from the channel.
1705 * \param chan Channel to playback to/record from.
1706 * \param playfile Filename of sound to play before recording begins.
1707 * \param recordfile Filename to record to.
1708 * \param maxtime Maximum length of recording (in seconds).
1709 * \param fmt Format(s) to record message in. Multiple formats may be specified by separating them with a '|'.
1710 * \param duration Where to store actual length of the recorded message (in milliseconds).
1711 * \param sound_duration Where to store the length of the recorded message (in milliseconds), minus any silence
1712 * \param beep Whether to play a beep before starting to record.
1713 * \param silencethreshold
1714 * \param maxsilence Length of silence that will end a recording (in milliseconds).
1715 * \param path Optional filesystem path to unlock.
1716 * \param prepend If true, prepend the recorded audio to an existing file and follow prepend mode recording rules
1717 * \param acceptdtmf DTMF digits that will end the recording.
1718 * \param canceldtmf DTMF digits that will cancel the recording.
1719 * \param skip_confirmation_sound If true, don't play auth-thankyou at end. Nice for custom recording prompts in apps.
1720 * \param if_exists
1721 *
1722 * \retval -1 failure or hangup
1723 * \retval 'S' Recording ended from silence timeout
1724 * \retval 't' Recording ended from the message exceeding the maximum duration, or via DTMF in prepend mode
1725 * \retval dtmfchar Recording ended via the return value's DTMF character for either cancel or accept.
1726 */
1727static int __ast_play_and_record(struct ast_channel *chan, const char *playfile, const char *recordfile, int maxtime, const char *fmt, int *duration, int *sound_duration, int beep, int silencethreshold, int maxsilence, const char *path, int prepend, const char *acceptdtmf, const char *canceldtmf, int skip_confirmation_sound, enum ast_record_if_exists if_exists)
1728{
1729 int d = 0;
1730 char *fmts;
1731 char comment[256];
1732 int x, fmtcnt = 1, res = -1, outmsg = 0;
1733 struct ast_filestream *others[AST_MAX_FORMATS];
1734 const char *sfmt[AST_MAX_FORMATS];
1735 char *stringp = NULL;
1736 time_t start, end;
1737 struct ast_dsp *sildet = NULL; /* silence detector dsp */
1738 int totalsilence = 0;
1739 int dspsilence = 0;
1740 int olddspsilence = 0;
1741 struct ast_format *rfmt = NULL;
1742 struct ast_silence_generator *silgen = NULL;
1743 char prependfile[PATH_MAX];
1744 int ioflags; /* IO flags for writing output file */
1745
1746 ioflags = O_CREAT|O_WRONLY;
1747
1748 switch (if_exists) {
1750 ioflags |= O_EXCL;
1751 break;
1753 ioflags |= O_TRUNC;
1754 break;
1756 ioflags |= O_APPEND;
1757 break;
1759 ast_assert(0);
1760 break;
1761 }
1762
1763 if (silencethreshold < 0) {
1765 }
1766
1767 if (maxsilence < 0) {
1769 }
1770
1771 /* barf if no pointer passed to store duration in */
1772 if (!duration) {
1773 ast_log(LOG_WARNING, "Error play_and_record called without duration pointer\n");
1774 return -1;
1775 }
1776
1777 ast_debug(1, "play_and_record: %s, %s, '%s'\n", playfile ? playfile : "<None>", recordfile, fmt);
1778 snprintf(comment, sizeof(comment), "Playing %s, Recording to: %s on %s\n", playfile ? playfile : "<None>", recordfile, ast_channel_name(chan));
1779
1780 if (playfile || beep) {
1781 if (!beep) {
1782 d = ast_play_and_wait(chan, playfile);
1783 }
1784 if (d > -1) {
1785 d = ast_stream_and_wait(chan, "beep", "");
1786 }
1787 if (d < 0) {
1788 return -1;
1789 }
1790 }
1791
1792 if (prepend) {
1793 ast_copy_string(prependfile, recordfile, sizeof(prependfile));
1794 strncat(prependfile, "-prepend", sizeof(prependfile) - strlen(prependfile) - 1);
1795 }
1796
1797 fmts = ast_strdupa(fmt);
1798
1799 stringp = fmts;
1800 strsep(&stringp, "|");
1801 ast_debug(1, "Recording Formats: sfmts=%s\n", fmts);
1802 sfmt[0] = ast_strdupa(fmts);
1803
1804 while ((fmt = strsep(&stringp, "|"))) {
1805 if (fmtcnt > AST_MAX_FORMATS - 1) {
1806 ast_log(LOG_WARNING, "Please increase AST_MAX_FORMATS in file.h\n");
1807 break;
1808 }
1809 /*
1810 * Storage for 'fmt' is on the stack and held by 'fmts', which is maintained for
1811 * the rest of this function. So okay to not duplicate 'fmt' here, but only keep
1812 * a pointer to it.
1813 */
1814 sfmt[fmtcnt++] = fmt;
1815 }
1816
1817 end = start = time(NULL); /* pre-initialize end to be same as start in case we never get into loop */
1818 for (x = 0; x < fmtcnt; x++) {
1819 others[x] = ast_writefile(prepend ? prependfile : recordfile, sfmt[x], comment, ioflags, 0, AST_FILE_MODE);
1820 ast_verb(3, "x=%d, open writing: %s format: %s, %p\n", x, prepend ? prependfile : recordfile, sfmt[x], others[x]);
1821
1822 if (!others[x]) {
1823 break;
1824 }
1825 }
1826
1827 if (path) {
1828 ast_unlock_path(path);
1829 }
1830
1831 if (maxsilence > 0) {
1832 sildet = ast_dsp_new(); /* Create the silence detector */
1833 if (!sildet) {
1834 ast_log(LOG_WARNING, "Unable to create silence detector :(\n");
1835 return -1;
1836 }
1838 res = set_read_to_slin(chan, &rfmt);
1839 if (res < 0) {
1840 ast_log(LOG_WARNING, "Unable to set to linear mode, giving up\n");
1841 ast_dsp_free(sildet);
1842 ao2_cleanup(rfmt);
1843 return -1;
1844 }
1845 }
1846
1847 if (!prepend) {
1848 /* Request a video update */
1850
1853 }
1854 }
1855
1856 if (x == fmtcnt) {
1857 /* Loop, writing the packets we read to the writer(s), until
1858 * we have reason to stop. */
1859 struct ast_frame *f;
1860 int paused = 0;
1861 int muted = 0;
1862 time_t pause_start = 0;
1863 int paused_secs = 0;
1864 int pausedsilence = 0;
1865
1866 for (;;) {
1867 if (!(res = ast_waitfor(chan, 2000))) {
1868 ast_debug(1, "One waitfor failed, trying another\n");
1869 /* Try one more time in case of masq */
1870 if (!(res = ast_waitfor(chan, 2000))) {
1871 ast_log(LOG_WARNING, "No audio available on %s??\n", ast_channel_name(chan));
1872 res = -1;
1873 }
1874 }
1875
1876 if (res < 0) {
1877 f = NULL;
1878 break;
1879 }
1880 if (!(f = ast_read(chan))) {
1881 break;
1882 }
1883 if (f->frametype == AST_FRAME_VOICE) {
1884 /* write each format */
1885 if (paused) {
1886 /* It's all good */
1887 res = 0;
1888 } else {
1889 struct ast_frame *silence = NULL;
1890 struct ast_frame *orig = f;
1891
1892 if (muted) {
1893 silence = make_silence(orig);
1894 if (!silence) {
1895 ast_log(LOG_WARNING, "Error creating silence\n");
1896 break;
1897 }
1898 f = silence;
1899 }
1900 for (x = 0; x < fmtcnt; x++) {
1901 if (prepend && !others[x]) {
1902 break;
1903 }
1904 res = ast_writestream(others[x], f);
1905 }
1906 ast_frame_dtor(silence);
1907 f = orig;
1908 }
1909
1910 /* Silence Detection */
1911 if (maxsilence > 0) {
1912 dspsilence = 0;
1913 ast_dsp_silence(sildet, f, &dspsilence);
1914 if (olddspsilence > dspsilence) {
1915 totalsilence += olddspsilence;
1916 }
1917 olddspsilence = dspsilence;
1918
1919 if (paused) {
1920 /* record how much silence there was while we are paused */
1921 pausedsilence = dspsilence;
1922 } else if (dspsilence > pausedsilence) {
1923 /* ignore the paused silence */
1924 dspsilence -= pausedsilence;
1925 } else {
1926 /* dspsilence has reset, reset pausedsilence */
1927 pausedsilence = 0;
1928 }
1929
1930 if (dspsilence > maxsilence) {
1931 /* Ended happily with silence */
1932 ast_verb(3, "Recording automatically stopped after a silence of %d seconds\n", dspsilence/1000);
1933 res = 'S';
1934 outmsg = 2;
1935 break;
1936 }
1937 }
1938 /* Exit on any error */
1939 if (res) {
1940 ast_log(LOG_WARNING, "Error writing frame\n");
1941 break;
1942 }
1943 } else if (f->frametype == AST_FRAME_VIDEO) {
1944 /* Write only once */
1945 ast_writestream(others[0], f);
1946 } else if (f->frametype == AST_FRAME_DTMF) {
1947 if (prepend) {
1948 /* stop recording with any digit */
1949 ast_verb(3, "User ended message by pressing %c\n", f->subclass.integer);
1950 res = 't';
1951 outmsg = 2;
1952 break;
1953 }
1954 if (strchr(acceptdtmf, f->subclass.integer)) {
1955 ast_verb(3, "User ended message by pressing %c\n", f->subclass.integer);
1956 res = f->subclass.integer;
1957 outmsg = 2;
1958 break;
1959 }
1960 if (strchr(canceldtmf, f->subclass.integer)) {
1961 ast_verb(3, "User canceled message by pressing %c\n", f->subclass.integer);
1962 res = f->subclass.integer;
1963 outmsg = 0;
1964 break;
1965 }
1966 } else if (f->frametype == AST_FRAME_CONTROL) {
1968 ast_verb(3, "Message canceled by control\n");
1969 outmsg = 0; /* cancels the recording */
1970 res = 0;
1971 break;
1972 } else if (f->subclass.integer == AST_CONTROL_RECORD_STOP) {
1973 ast_verb(3, "Message ended by control\n");
1974 res = 0;
1975 break;
1976 } else if (f->subclass.integer == AST_CONTROL_RECORD_SUSPEND) {
1977 paused = !paused;
1978 ast_verb(3, "Message %spaused by control\n",
1979 paused ? "" : "un");
1980 if (paused) {
1981 pause_start = time(NULL);
1982 } else {
1983 paused_secs += time(NULL) - pause_start;
1984 }
1985 } else if (f->subclass.integer == AST_CONTROL_RECORD_MUTE) {
1986 muted = !muted;
1987 ast_verb(3, "Message %smuted by control\n",
1988 muted ? "" : "un");
1989 /* We can only silence slin frames, so
1990 * set the mode, if we haven't already
1991 * for sildet
1992 */
1993 if (muted && !rfmt) {
1994 ast_verb(3, "Setting read format to linear mode\n");
1995 res = set_read_to_slin(chan, &rfmt);
1996 if (res < 0) {
1997 ast_log(LOG_WARNING, "Unable to set to linear mode, giving up\n");
1998 break;
1999 }
2000 }
2001 }
2002 }
2003 if (maxtime && !paused) {
2004 end = time(NULL);
2005 if (maxtime < (end - start - paused_secs)) {
2006 ast_verb(3, "Took too long, cutting it short...\n");
2007 res = 't';
2008 outmsg = 2;
2009 break;
2010 }
2011 }
2012 ast_frfree(f);
2013 }
2014 if (!f) {
2015 ast_verb(3, "User hung up\n");
2016 res = -1;
2017 outmsg = 1;
2018 } else {
2019 ast_frfree(f);
2020 }
2021 } else {
2022 ast_log(LOG_WARNING, "Error creating writestream '%s', format '%s'\n", recordfile, sfmt[x]);
2023 }
2024
2025 if (!prepend) {
2026 if (silgen) {
2028 }
2029 }
2030
2031 /*!\note
2032 * Instead of asking how much time passed (end - start), calculate the number
2033 * of seconds of audio which actually went into the file. This fixes a
2034 * problem where audio is stopped up on the network and never gets to us.
2035 *
2036 * Note that we still want to use the number of seconds passed for the max
2037 * message, otherwise we could get a situation where this stream is never
2038 * closed (which would create a resource leak).
2039 */
2040 *duration = others[0] ? ast_tellstream(others[0]) / 8000 : 0;
2041 if (sound_duration) {
2042 *sound_duration = *duration;
2043 }
2044
2045 if (!prepend) {
2046 /* Reduce duration by a total silence amount */
2047 if (olddspsilence <= dspsilence) {
2048 totalsilence += dspsilence;
2049 }
2050
2051 if (sound_duration) {
2052 if (totalsilence > 0) {
2053 *sound_duration -= (totalsilence - 200) / 1000;
2054 }
2055 if (*sound_duration < 0) {
2056 *sound_duration = 0;
2057 }
2058 }
2059
2060 if (dspsilence > 0) {
2061 *duration -= (dspsilence - 200) / 1000;
2062 }
2063
2064 if (*duration < 0) {
2065 *duration = 0;
2066 }
2067
2068 for (x = 0; x < fmtcnt; x++) {
2069 if (!others[x]) {
2070 break;
2071 }
2072 /*!\note
2073 * If we ended with silence, trim all but the first 200ms of silence
2074 * off the recording. However, if we ended with '#', we don't want
2075 * to trim ANY part of the recording.
2076 */
2077 if (res > 0 && dspsilence) {
2078 /* rewind only the trailing silence */
2079 ast_stream_rewind(others[x], dspsilence - 200);
2080 }
2081 ast_truncstream(others[x]);
2082 ast_closestream(others[x]);
2083 }
2084 } else if (prepend && outmsg) {
2085 struct ast_filestream *realfiles[AST_MAX_FORMATS];
2086 struct ast_frame *fr;
2087
2088 for (x = 0; x < fmtcnt; x++) {
2089 snprintf(comment, sizeof(comment), "Opening the real file %s.%s\n", recordfile, sfmt[x]);
2090 realfiles[x] = ast_readfile(recordfile, sfmt[x], comment, O_RDONLY, 0, 0);
2091 if (!others[x]) {
2092 break;
2093 }
2094 if (!realfiles[x]) {
2095 ast_closestream(others[x]);
2096 continue;
2097 }
2098 /*!\note Same logic as above. */
2099 if (dspsilence) {
2100 ast_stream_rewind(others[x], dspsilence - 200);
2101 }
2102 ast_truncstream(others[x]);
2103 /* add the original file too */
2104 while ((fr = ast_readframe(realfiles[x]))) {
2105 ast_writestream(others[x], fr);
2106 ast_frfree(fr);
2107 }
2108 ast_closestream(others[x]);
2109 ast_closestream(realfiles[x]);
2110 ast_filerename(prependfile, recordfile, sfmt[x]);
2111 ast_verb(4, "Recording Format: sfmts=%s, prependfile %s, recordfile %s\n", sfmt[x], prependfile, recordfile);
2112 ast_filedelete(prependfile, sfmt[x]);
2113 }
2114 } else {
2115 for (x = 0; x < fmtcnt; x++) {
2116 if (!others[x]) {
2117 break;
2118 }
2119 ast_closestream(others[x]);
2120 }
2121 }
2122
2123 if (rfmt && ast_set_read_format(chan, rfmt)) {
2124 ast_log(LOG_WARNING, "Unable to restore format %s to channel '%s'\n", ast_format_get_name(rfmt), ast_channel_name(chan));
2125 }
2126 ao2_cleanup(rfmt);
2127 if ((outmsg == 2) && (!skip_confirmation_sound)) {
2128 ast_stream_and_wait(chan, "auth-thankyou", "");
2129 }
2130 if (sildet) {
2131 ast_dsp_free(sildet);
2132 }
2133 return res;
2134}
2135
2136static const char default_acceptdtmf[] = "#";
2137static const char default_canceldtmf[] = "";
2138
2139int ast_play_and_record_full(struct ast_channel *chan, const char *playfile, const char *recordfile, int maxtime, const char *fmt, int *duration, int *sound_duration, int beep, int silencethreshold, int maxsilence, const char *path, const char *acceptdtmf, const char *canceldtmf, int skip_confirmation_sound, enum ast_record_if_exists if_exists)
2140{
2141 return __ast_play_and_record(chan, playfile, recordfile, maxtime, fmt, duration, sound_duration, beep, silencethreshold, maxsilence, path, 0, S_OR(acceptdtmf, ""), S_OR(canceldtmf, default_canceldtmf), skip_confirmation_sound, if_exists);
2142}
2143
2144int ast_play_and_record(struct ast_channel *chan, const char *playfile, const char *recordfile, int maxtime, const char *fmt, int *duration, int *sound_duration, int silencethreshold, int maxsilence, const char *path)
2145{
2146 return __ast_play_and_record(chan, playfile, recordfile, maxtime, fmt, duration, sound_duration, 0, silencethreshold, maxsilence, path, 0, default_acceptdtmf, default_canceldtmf, 0, AST_RECORD_IF_EXISTS_OVERWRITE);
2147}
2148
2149int ast_play_and_prepend(struct ast_channel *chan, char *playfile, char *recordfile, int maxtime, char *fmt, int *duration, int *sound_duration, int beep, int silencethreshold, int maxsilence)
2150{
2151 return __ast_play_and_record(chan, playfile, recordfile, maxtime, fmt, duration, sound_duration, beep, silencethreshold, maxsilence, NULL, 1, default_acceptdtmf, default_canceldtmf, 1, AST_RECORD_IF_EXISTS_OVERWRITE);
2152}
2153
2154/* Channel group core functions */
2155
2156int ast_app_group_split_group(const char *data, char *group, int group_max, char *category, int category_max)
2157{
2158 int res = 0;
2159 char tmp[256];
2160 char *grp = NULL, *cat = NULL;
2161
2162 if (!ast_strlen_zero(data)) {
2163 ast_copy_string(tmp, data, sizeof(tmp));
2164 grp = tmp;
2165 if ((cat = strchr(tmp, '@'))) {
2166 *cat++ = '\0';
2167 }
2168 }
2169
2170 if (!ast_strlen_zero(grp)) {
2171 ast_copy_string(group, grp, group_max);
2172 } else {
2173 *group = '\0';
2174 }
2175
2176 if (!ast_strlen_zero(cat)) {
2177 ast_copy_string(category, cat, category_max);
2178 }
2179
2180 return res;
2181}
2182
2183int ast_app_group_set_channel(struct ast_channel *chan, const char *data)
2184{
2185 int res = 0;
2186 char group[80] = "", category[80] = "";
2187 struct ast_group_info *gi = NULL;
2188 size_t len = 0;
2189
2190 if (ast_app_group_split_group(data, group, sizeof(group), category, sizeof(category))) {
2191 return -1;
2192 }
2193
2194 /* Calculate memory we will need if this is new */
2195 len = sizeof(*gi) + strlen(group) + 1;
2196 if (!ast_strlen_zero(category)) {
2197 len += strlen(category) + 1;
2198 }
2199
2202 if ((gi->chan == chan) && ((ast_strlen_zero(category) && ast_strlen_zero(gi->category)) || (!ast_strlen_zero(gi->category) && !strcasecmp(gi->category, category)))) {
2204 ast_free(gi);
2205 break;
2206 }
2207 }
2209
2210 if (ast_strlen_zero(group)) {
2211 /* Enable unsetting the group */
2212 } else if ((gi = ast_calloc(1, len))) {
2213 gi->chan = chan;
2214 gi->group = (char *) gi + sizeof(*gi);
2215 strcpy(gi->group, group);
2216 if (!ast_strlen_zero(category)) {
2217 gi->category = (char *) gi + sizeof(*gi) + strlen(group) + 1;
2218 strcpy(gi->category, category);
2219 }
2221 } else {
2222 res = -1;
2223 }
2224
2226
2227 return res;
2228}
2229
2230int ast_app_group_get_count(const char *group, const char *category)
2231{
2232 struct ast_group_info *gi = NULL;
2233 int count = 0;
2234
2235 if (ast_strlen_zero(group)) {
2236 return 0;
2237 }
2238
2241 if (!strcasecmp(gi->group, group) && (ast_strlen_zero(category) || (!ast_strlen_zero(gi->category) && !strcasecmp(gi->category, category)))) {
2242 count++;
2243 }
2244 }
2246
2247 return count;
2248}
2249
2250int ast_app_group_match_get_count(const char *groupmatch, const char *category)
2251{
2252 struct ast_group_info *gi = NULL;
2253 regex_t regexbuf_group;
2254 regex_t regexbuf_category;
2255 int count = 0;
2256
2257 if (ast_strlen_zero(groupmatch)) {
2258 ast_log(LOG_NOTICE, "groupmatch empty\n");
2259 return 0;
2260 }
2261
2262 /* if regex compilation fails, return zero matches */
2263 if (regcomp(&regexbuf_group, groupmatch, REG_EXTENDED | REG_NOSUB)) {
2264 ast_log(LOG_ERROR, "Regex compile failed on: %s\n", groupmatch);
2265 return 0;
2266 }
2267
2268 if (!ast_strlen_zero(category) && regcomp(&regexbuf_category, category, REG_EXTENDED | REG_NOSUB)) {
2269 ast_log(LOG_ERROR, "Regex compile failed on: %s\n", category);
2270 regfree(&regexbuf_group);
2271 return 0;
2272 }
2273
2276 if (!regexec(&regexbuf_group, gi->group, 0, NULL, 0) && (ast_strlen_zero(category) || (!ast_strlen_zero(gi->category) && !regexec(&regexbuf_category, gi->category, 0, NULL, 0)))) {
2277 count++;
2278 }
2279 }
2281
2282 regfree(&regexbuf_group);
2283 if (!ast_strlen_zero(category)) {
2284 regfree(&regexbuf_category);
2285 }
2286
2287 return count;
2288}
2289
2290int ast_app_group_update(struct ast_channel *old, struct ast_channel *new)
2291{
2292 struct ast_group_info *gi = NULL;
2293
2296 if (gi->chan == old) {
2297 gi->chan = new;
2298 } else if (gi->chan == new) {
2300 ast_free(gi);
2301 }
2302 }
2305
2306 return 0;
2307}
2308
2310{
2311 struct ast_group_info *gi = NULL;
2312
2315 if (gi->chan == chan) {
2317 ast_free(gi);
2318 }
2319 }
2322
2323 return 0;
2324}
2325
2327{
2328 return AST_RWLIST_WRLOCK(&groups);
2329}
2330
2332{
2333 return AST_RWLIST_RDLOCK(&groups);
2334}
2335
2337{
2338 return AST_RWLIST_FIRST(&groups);
2339}
2340
2342{
2343 return AST_RWLIST_UNLOCK(&groups);
2344}
2345
2346unsigned int __ast_app_separate_args(char *buf, char delim, int remove_chars, char **array, int arraylen)
2347{
2348 int argc;
2349 char *scan, *wasdelim = NULL;
2350 int paren = 0, quote = 0, bracket = 0;
2351
2352 if (!array || !arraylen) {
2353 return 0;
2354 }
2355
2356 memset(array, 0, arraylen * sizeof(*array));
2357
2358 if (!buf) {
2359 return 0;
2360 }
2361
2362 scan = buf;
2363
2364 for (argc = 0; *scan && (argc < arraylen - 1); argc++) {
2365 array[argc] = scan;
2366 for (; *scan; scan++) {
2367 if (*scan == '(') {
2368 paren++;
2369 } else if (*scan == ')') {
2370 if (paren) {
2371 paren--;
2372 }
2373 } else if (*scan == '[') {
2374 bracket++;
2375 } else if (*scan == ']') {
2376 if (bracket) {
2377 bracket--;
2378 }
2379 } else if (*scan == '"' && delim != '"') {
2380 quote = quote ? 0 : 1;
2381 if (remove_chars) {
2382 /* Remove quote character from argument */
2383 memmove(scan, scan + 1, strlen(scan));
2384 scan--;
2385 }
2386 } else if (*scan == '\\') {
2387 if (remove_chars) {
2388 /* Literal character, don't parse */
2389 memmove(scan, scan + 1, strlen(scan));
2390 } else {
2391 scan++;
2392 }
2393 } else if ((*scan == delim) && !paren && !quote && !bracket) {
2394 wasdelim = scan;
2395 *scan++ = '\0';
2396 break;
2397 }
2398 }
2399 }
2400
2401 /* If the last character in the original string was the delimiter, then
2402 * there is one additional argument. */
2403 if (*scan || (scan > buf && (scan - 1) == wasdelim)) {
2404 array[argc++] = scan;
2405 }
2406
2407 return argc;
2408}
2409
2410static enum AST_LOCK_RESULT ast_lock_path_lockfile(const char *path)
2411{
2412 char *s;
2413 char *fs;
2414 int res;
2415 int fd;
2416 int lp = strlen(path);
2417 time_t start;
2418
2419 s = ast_alloca(lp + 10);
2420 fs = ast_alloca(lp + 20);
2421
2422 snprintf(fs, strlen(path) + 19, "%s/.lock-%08lx", path, (unsigned long)ast_random());
2423 fd = open(fs, O_WRONLY | O_CREAT | O_EXCL, AST_FILE_MODE);
2424 if (fd < 0) {
2425 ast_log(LOG_ERROR, "Unable to create lock file '%s': %s\n", path, strerror(errno));
2427 }
2428 close(fd);
2429
2430 snprintf(s, strlen(path) + 9, "%s/.lock", path);
2431 start = time(NULL);
2432 while (((res = link(fs, s)) < 0) && (errno == EEXIST) && (time(NULL) - start < 5)) {
2433 sched_yield();
2434 }
2435
2436 unlink(fs);
2437
2438 if (res) {
2439 ast_log(LOG_WARNING, "Failed to lock path '%s': %s\n", path, strerror(errno));
2440 return AST_LOCK_TIMEOUT;
2441 } else {
2442 ast_debug(1, "Locked path '%s'\n", path);
2443 return AST_LOCK_SUCCESS;
2444 }
2445}
2446
2447static int ast_unlock_path_lockfile(const char *path)
2448{
2449 char *s;
2450 int res;
2451
2452 s = ast_alloca(strlen(path) + 10);
2453
2454 snprintf(s, strlen(path) + 9, "%s/%s", path, ".lock");
2455
2456 if ((res = unlink(s))) {
2457 ast_log(LOG_ERROR, "Could not unlock path '%s': %s\n", path, strerror(errno));
2458 } else {
2459 ast_debug(1, "Unlocked path '%s'\n", path);
2460 }
2461
2462 return res;
2463}
2464
2467 int fd;
2468 char *path;
2469};
2470
2472
2473static void path_lock_destroy(struct path_lock *obj)
2474{
2475 if (obj->fd >= 0) {
2476 close(obj->fd);
2477 }
2478 if (obj->path) {
2479 ast_free(obj->path);
2480 }
2481 ast_free(obj);
2482}
2483
2484static enum AST_LOCK_RESULT ast_lock_path_flock(const char *path)
2485{
2486 char *fs;
2487 int res;
2488 int fd;
2489 time_t start;
2490 struct path_lock *pl;
2491 struct stat st, ost;
2492
2493 fs = ast_alloca(strlen(path) + 20);
2494
2495 snprintf(fs, strlen(path) + 19, "%s/lock", path);
2496 if (lstat(fs, &st) == 0) {
2497 if ((st.st_mode & S_IFMT) == S_IFLNK) {
2498 ast_log(LOG_WARNING, "Unable to create lock file "
2499 "'%s': it's already a symbolic link\n",
2500 fs);
2501 return AST_LOCK_FAILURE;
2502 }
2503 if (st.st_nlink > 1) {
2504 ast_log(LOG_WARNING, "Unable to create lock file "
2505 "'%s': %u hard links exist\n",
2506 fs, (unsigned int) st.st_nlink);
2507 return AST_LOCK_FAILURE;
2508 }
2509 }
2510 if ((fd = open(fs, O_WRONLY | O_CREAT, 0600)) < 0) {
2511 ast_log(LOG_WARNING, "Unable to create lock file '%s': %s\n",
2512 fs, strerror(errno));
2514 }
2515 if (!(pl = ast_calloc(1, sizeof(*pl)))) {
2516 /* We don't unlink the lock file here, on the possibility that
2517 * someone else created it - better to leave a little mess
2518 * than create a big one by destroying someone else's lock
2519 * and causing something to be corrupted.
2520 */
2521 close(fd);
2522 return AST_LOCK_FAILURE;
2523 }
2524 pl->fd = fd;
2525 pl->path = ast_strdup(path);
2526
2527 time(&start);
2528 while (
2529 #ifdef SOLARIS
2530 ((res = fcntl(pl->fd, F_SETLK, fcntl(pl->fd, F_GETFL) | O_NONBLOCK)) < 0) &&
2531 #else
2532 ((res = flock(pl->fd, LOCK_EX | LOCK_NB)) < 0) &&
2533 #endif
2534 (errno == EWOULDBLOCK) &&
2535 (time(NULL) - start < 5))
2536 usleep(1000);
2537 if (res) {
2538 ast_log(LOG_WARNING, "Failed to lock path '%s': %s\n",
2539 path, strerror(errno));
2540 /* No unlinking of lock done, since we tried and failed to
2541 * flock() it.
2542 */
2544 return AST_LOCK_TIMEOUT;
2545 }
2546
2547 /* Check for the race where the file is recreated or deleted out from
2548 * underneath us.
2549 */
2550 if (lstat(fs, &st) != 0 && fstat(pl->fd, &ost) != 0 &&
2551 st.st_dev != ost.st_dev &&
2552 st.st_ino != ost.st_ino) {
2553 ast_log(LOG_WARNING, "Unable to create lock file '%s': "
2554 "file changed underneath us\n", fs);
2556 return AST_LOCK_FAILURE;
2557 }
2558
2559 /* Success: file created, flocked, and is the one we started with */
2563
2564 ast_debug(1, "Locked path '%s'\n", path);
2565
2566 return AST_LOCK_SUCCESS;
2567}
2568
2569static int ast_unlock_path_flock(const char *path)
2570{
2571 char *s;
2572 struct path_lock *p;
2573
2574 s = ast_alloca(strlen(path) + 20);
2575
2578 if (!strcmp(p->path, path)) {
2580 break;
2581 }
2582 }
2585
2586 if (p) {
2587 snprintf(s, strlen(path) + 19, "%s/lock", path);
2588 unlink(s);
2590 ast_debug(1, "Unlocked path '%s'\n", path);
2591 } else {
2592 ast_debug(1, "Failed to unlock path '%s': "
2593 "lock not found\n", path);
2594 }
2595
2596 return 0;
2597}
2598
2600{
2602}
2603
2605{
2607
2608 switch (ast_lock_type) {
2611 break;
2614 break;
2615 }
2616
2617 return r;
2618}
2619
2620int ast_unlock_path(const char *path)
2621{
2622 int r = 0;
2623
2624 switch (ast_lock_type) {
2627 break;
2630 break;
2631 }
2632
2633 return r;
2634}
2635
2636int ast_record_review(struct ast_channel *chan, const char *playfile, const char *recordfile, int maxtime, const char *fmt, int *duration, const char *path)
2637{
2638 int silencethreshold;
2639 int maxsilence = 0;
2640 int res = 0;
2641 int cmd = 0;
2642 int max_attempts = 3;
2643 int attempts = 0;
2644 int recorded = 0;
2645 int message_exists = 0;
2646 /* Note that urgent and private are for flagging messages as such in the future */
2647
2648 /* barf if no pointer passed to store duration in */
2649 if (!duration) {
2650 ast_log(LOG_WARNING, "Error ast_record_review called without duration pointer\n");
2651 return -1;
2652 }
2653
2654 cmd = '3'; /* Want to start by recording */
2655
2657
2658 while ((cmd >= 0) && (cmd != 't')) {
2659 switch (cmd) {
2660 case '1':
2661 if (!message_exists) {
2662 /* In this case, 1 is to record a message */
2663 cmd = '3';
2664 break;
2665 } else {
2666 ast_stream_and_wait(chan, "vm-msgsaved", "");
2667 cmd = 't';
2668 return res;
2669 }
2670 case '2':
2671 /* Review */
2672 ast_verb(3, "Reviewing the recording\n");
2673 cmd = ast_stream_and_wait(chan, recordfile, AST_DIGIT_ANY);
2674 break;
2675 case '3':
2676 message_exists = 0;
2677 /* Record */
2678 ast_verb(3, "R%secording\n", recorded == 1 ? "e-r" : "");
2679 recorded = 1;
2680 if ((cmd = ast_play_and_record(chan, playfile, recordfile, maxtime, fmt, duration, NULL, silencethreshold, maxsilence, path)) == -1) {
2681 /* User has hung up, no options to give */
2682 return cmd;
2683 }
2684 if (cmd == '0') {
2685 break;
2686 } else if (cmd == '*') {
2687 break;
2688 } else {
2689 /* If all is well, a message exists */
2690 message_exists = 1;
2691 cmd = 0;
2692 }
2693 break;
2694 case '4':
2695 case '5':
2696 case '6':
2697 case '7':
2698 case '8':
2699 case '9':
2700 case '*':
2701 case '#':
2702 cmd = ast_play_and_wait(chan, "vm-sorry");
2703 break;
2704 default:
2705 if (message_exists) {
2706 cmd = ast_play_and_wait(chan, "vm-review");
2707 } else {
2708 if (!(cmd = ast_play_and_wait(chan, "vm-torerecord"))) {
2709 cmd = ast_waitfordigit(chan, 600);
2710 }
2711 }
2712
2713 if (!cmd) {
2714 cmd = ast_waitfordigit(chan, 6000);
2715 }
2716 if (!cmd) {
2717 attempts++;
2718 }
2719 if (attempts > max_attempts) {
2720 cmd = 't';
2721 }
2722 }
2723 }
2724 if (cmd == 't') {
2725 cmd = 0;
2726 }
2727 return cmd;
2728}
2729
2730#define RES_UPONE (1 << 16)
2731#define RES_EXIT (1 << 17)
2732#define RES_REPEAT (1 << 18)
2733#define RES_RESTART ((1 << 19) | RES_REPEAT)
2734
2735static int ast_ivr_menu_run_internal(struct ast_channel *chan, struct ast_ivr_menu *menu, void *cbdata);
2736
2737static int ivr_dispatch(struct ast_channel *chan, struct ast_ivr_option *option, char *exten, void *cbdata)
2738{
2739 int res;
2740 int (*ivr_func)(struct ast_channel *, void *);
2741 char *c;
2742 char *n;
2743
2744 switch (option->action) {
2745 case AST_ACTION_UPONE:
2746 return RES_UPONE;
2747 case AST_ACTION_EXIT:
2748 return RES_EXIT | (((unsigned long)(option->adata)) & 0xffff);
2749 case AST_ACTION_REPEAT:
2750 return RES_REPEAT | (((unsigned long)(option->adata)) & 0xffff);
2751 case AST_ACTION_RESTART:
2752 return RES_RESTART ;
2753 case AST_ACTION_NOOP:
2754 return 0;
2756 res = ast_stream_and_wait(chan, (char *)option->adata, AST_DIGIT_ANY);
2757 if (res < 0) {
2758 ast_log(LOG_NOTICE, "Unable to find file '%s'!\n", (char *)option->adata);
2759 res = 0;
2760 }
2761 return res;
2763 res = ast_stream_and_wait(chan, (char *)option->adata, "");
2764 if (res < 0) {
2765 ast_log(LOG_NOTICE, "Unable to find file '%s'!\n", (char *)option->adata);
2766 res = 0;
2767 }
2768 return res;
2769 case AST_ACTION_MENU:
2770 if ((res = ast_ivr_menu_run_internal(chan, (struct ast_ivr_menu *)option->adata, cbdata)) == -2) {
2771 /* Do not pass entry errors back up, treat as though it was an "UPONE" */
2772 res = 0;
2773 }
2774 return res;
2776 if (!(res = ast_waitfordigit(chan, ast_channel_pbx(chan) ? ast_channel_pbx(chan)->rtimeoutms : 10000))) {
2777 return 't';
2778 }
2779 return res;
2781 ivr_func = option->adata;
2782 res = ivr_func(chan, cbdata);
2783 return res;
2785 res = ast_parseable_goto(chan, option->adata);
2786 return 0;
2789 res = 0;
2790 c = ast_strdupa(option->adata);
2791 while ((n = strsep(&c, ";"))) {
2792 if ((res = ast_stream_and_wait(chan, n,
2793 (option->action == AST_ACTION_BACKLIST) ? AST_DIGIT_ANY : ""))) {
2794 break;
2795 }
2796 }
2797 ast_stopstream(chan);
2798 return res;
2799 default:
2800 ast_log(LOG_NOTICE, "Unknown dispatch function %u, ignoring!\n", option->action);
2801 return 0;
2802 }
2803 return -1;
2804}
2805
2806static int option_exists(struct ast_ivr_menu *menu, char *option)
2807{
2808 int x;
2809 for (x = 0; menu->options[x].option; x++) {
2810 if (!strcasecmp(menu->options[x].option, option)) {
2811 return x;
2812 }
2813 }
2814 return -1;
2815}
2816
2817static int option_matchmore(struct ast_ivr_menu *menu, char *option)
2818{
2819 int x;
2820 for (x = 0; menu->options[x].option; x++) {
2821 if ((!strncasecmp(menu->options[x].option, option, strlen(option))) &&
2822 (menu->options[x].option[strlen(option)])) {
2823 return x;
2824 }
2825 }
2826 return -1;
2827}
2828
2829static int read_newoption(struct ast_channel *chan, struct ast_ivr_menu *menu, char *exten, int maxexten)
2830{
2831 int res = 0;
2832 int ms;
2833 while (option_matchmore(menu, exten)) {
2834 ms = ast_channel_pbx(chan) ? ast_channel_pbx(chan)->dtimeoutms : 5000;
2835 if (strlen(exten) >= maxexten - 1) {
2836 break;
2837 }
2838 if ((res = ast_waitfordigit(chan, ms)) < 1) {
2839 break;
2840 }
2841 exten[strlen(exten) + 1] = '\0';
2842 exten[strlen(exten)] = res;
2843 }
2844 return res > 0 ? 0 : res;
2845}
2846
2847static int ast_ivr_menu_run_internal(struct ast_channel *chan, struct ast_ivr_menu *menu, void *cbdata)
2848{
2849 /* Execute an IVR menu structure */
2850 int res = 0;
2851 int pos = 0;
2852 int retries = 0;
2853 char exten[AST_MAX_EXTENSION] = "s";
2854 if (option_exists(menu, "s") < 0) {
2855 strcpy(exten, "g");
2856 if (option_exists(menu, "g") < 0) {
2857 ast_log(LOG_WARNING, "No 's' nor 'g' extension in menu '%s'!\n", menu->title);
2858 return -1;
2859 }
2860 }
2861 while (!res) {
2862 while (menu->options[pos].option) {
2863 if (!strcasecmp(menu->options[pos].option, exten)) {
2864 res = ivr_dispatch(chan, menu->options + pos, exten, cbdata);
2865 ast_debug(1, "IVR Dispatch of '%s' (pos %d) yields %d\n", exten, pos, res);
2866 if (res < 0) {
2867 break;
2868 } else if (res & RES_UPONE) {
2869 return 0;
2870 } else if (res & RES_EXIT) {
2871 return res;
2872 } else if (res & RES_REPEAT) {
2873 int maxretries = res & 0xffff;
2874 if ((res & RES_RESTART) == RES_RESTART) {
2875 retries = 0;
2876 } else {
2877 retries++;
2878 }
2879 if (!maxretries) {
2880 maxretries = 3;
2881 }
2882 if ((maxretries > 0) && (retries >= maxretries)) {
2883 ast_debug(1, "Max retries %d exceeded\n", maxretries);
2884 return -2;
2885 } else {
2886 if (option_exists(menu, "g") > -1) {
2887 strcpy(exten, "g");
2888 } else if (option_exists(menu, "s") > -1) {
2889 strcpy(exten, "s");
2890 }
2891 }
2892 pos = 0;
2893 continue;
2894 } else if (res && strchr(AST_DIGIT_ANY, res)) {
2895 ast_debug(1, "Got start of extension, %c\n", res);
2896 exten[1] = '\0';
2897 exten[0] = res;
2898 if ((res = read_newoption(chan, menu, exten, sizeof(exten)))) {
2899 break;
2900 }
2901 if (option_exists(menu, exten) < 0) {
2902 if (option_exists(menu, "i")) {
2903 ast_debug(1, "Invalid extension entered, going to 'i'!\n");
2904 strcpy(exten, "i");
2905 pos = 0;
2906 continue;
2907 } else {
2908 ast_debug(1, "Aborting on invalid entry, with no 'i' option!\n");
2909 res = -2;
2910 break;
2911 }
2912 } else {
2913 ast_debug(1, "New existing extension: %s\n", exten);
2914 pos = 0;
2915 continue;
2916 }
2917 }
2918 }
2919 pos++;
2920 }
2921 ast_debug(1, "Stopping option '%s', res is %d\n", exten, res);
2922 pos = 0;
2923 if (!strcasecmp(exten, "s")) {
2924 strcpy(exten, "g");
2925 } else {
2926 break;
2927 }
2928 }
2929 return res;
2930}
2931
2932int ast_ivr_menu_run(struct ast_channel *chan, struct ast_ivr_menu *menu, void *cbdata)
2933{
2934 int res = ast_ivr_menu_run_internal(chan, menu, cbdata);
2935 /* Hide internal coding */
2936 return res > 0 ? 0 : res;
2937}
2938
2939char *ast_read_textfile(const char *filename)
2940{
2941 int fd, count = 0, res;
2942 char *output = NULL;
2943 struct stat filesize;
2944
2945 if (stat(filename, &filesize) == -1) {
2946 ast_log(LOG_WARNING, "Error can't stat %s\n", filename);
2947 return NULL;
2948 }
2949
2950 count = filesize.st_size + 1;
2951
2952 if ((fd = open(filename, O_RDONLY)) < 0) {
2953 ast_log(LOG_WARNING, "Cannot open file '%s' for reading: %s\n", filename, strerror(errno));
2954 return NULL;
2955 }
2956
2957 if ((output = ast_malloc(count))) {
2958 res = read(fd, output, count - 1);
2959 if (res == count - 1) {
2960 output[res] = '\0';
2961 } else {
2962 ast_log(LOG_WARNING, "Short read of %s (%d of %d): %s\n", filename, res, count - 1, strerror(errno));
2963 ast_free(output);
2964 output = NULL;
2965 }
2966 }
2967
2968 close(fd);
2969
2970 return output;
2971}
2972
2973static int parse_options(const struct ast_app_option *options, void *_flags, char **args, char *optstr, int flaglen)
2974{
2975 char *s, *arg;
2976 int curarg, res = 0;
2977 unsigned int argloc;
2978 struct ast_flags *flags = _flags;
2979 struct ast_flags64 *flags64 = _flags;
2980
2981 if (flaglen == 32) {
2983 } else {
2984 flags64->flags = 0;
2985 }
2986
2987 if (!optstr) {
2988 return 0;
2989 }
2990
2991 s = optstr;
2992 while (*s) {
2993 curarg = *s++ & 0x7f; /* the array (in app.h) has 128 entries */
2994 argloc = options[curarg].arg_index;
2995 if (*s == '(') {
2996 int paren = 1, quote = 0;
2997 int parsequotes = (s[1] == '"') ? 1 : 0;
2998
2999 /* Has argument */
3000 arg = ++s;
3001 for (; *s; s++) {
3002 if (*s == '(' && !quote) {
3003 paren++;
3004 } else if (*s == ')' && !quote) {
3005 /* Count parentheses, unless they're within quotes (or backslashed, below) */
3006 paren--;
3007 } else if (*s == '"' && parsequotes) {
3008 /* Leave embedded quotes alone, unless they are the first character */
3009 quote = quote ? 0 : 1;
3010 ast_copy_string(s, s + 1, INT_MAX);
3011 s--;
3012 } else if (*s == '\\') {
3013 if (!quote) {
3014 /* If a backslash is found outside of quotes, remove it */
3015 ast_copy_string(s, s + 1, INT_MAX);
3016 } else if (quote && s[1] == '"') {
3017 /* Backslash for a quote character within quotes, remove the backslash */
3018 ast_copy_string(s, s + 1, INT_MAX);
3019 } else {
3020 /* Backslash within quotes, keep both characters */
3021 s++;
3022 }
3023 }
3024
3025 if (paren == 0) {
3026 break;
3027 }
3028 }
3029 /* This will find the closing paren we found above, or none, if the string ended before we found one. */
3030 if ((s = strchr(s, ')'))) {
3031 if (argloc) {
3032 args[argloc - 1] = arg;
3033 }
3034 *s++ = '\0';
3035 } else {
3036 ast_log(LOG_WARNING, "Missing closing parenthesis for argument '%c' in string '%s'\n", curarg, arg);
3037 res = -1;
3038 break;
3039 }
3040 } else if (argloc) {
3041 args[argloc - 1] = "";
3042 }
3043 if (!options[curarg].flag) {
3044 ast_log(LOG_WARNING, "Unrecognized option: '%c'\n", curarg);
3045 }
3046 if (flaglen == 32) {
3047 ast_set_flag(flags, options[curarg].flag);
3048 } else {
3049 ast_set_flag64(flags64, options[curarg].flag);
3050 }
3051 }
3052
3053 return res;
3054}
3055
3056int ast_app_parse_options(const struct ast_app_option *options, struct ast_flags *flags, char **args, char *optstr)
3057{
3058 return parse_options(options, flags, args, optstr, 32);
3059}
3060
3061int ast_app_parse_options64(const struct ast_app_option *options, struct ast_flags64 *flags, char **args, char *optstr)
3062{
3063 return parse_options(options, flags, args, optstr, 64);
3064}
3065
3066void ast_app_options2str64(const struct ast_app_option *options, struct ast_flags64 *flags, char *buf, size_t len)
3067{
3068 unsigned int i, found = 0;
3069 for (i = 32; i < 128 && found < len; i++) {
3070 if (ast_test_flag64(flags, options[i].flag)) {
3071 buf[found++] = i;
3072 }
3073 }
3074 buf[found] = '\0';
3075}
3076
3077int ast_get_encoded_char(const char *stream, char *result, size_t *consumed)
3078{
3079 int i;
3080 *consumed = 1;
3081 *result = 0;
3082 if (ast_strlen_zero(stream)) {
3083 *consumed = 0;
3084 return -1;
3085 }
3086
3087 if (*stream == '\\') {
3088 *consumed = 2;
3089 switch (*(stream + 1)) {
3090 case 'n':
3091 *result = '\n';
3092 break;
3093 case 'r':
3094 *result = '\r';
3095 break;
3096 case 't':
3097 *result = '\t';
3098 break;
3099 case 'x':
3100 /* Hexadecimal */
3101 if (strchr("0123456789ABCDEFabcdef", *(stream + 2)) && *(stream + 2) != '\0') {
3102 *consumed = 3;
3103 if (*(stream + 2) <= '9') {
3104 *result = *(stream + 2) - '0';
3105 } else if (*(stream + 2) <= 'F') {
3106 *result = *(stream + 2) - 'A' + 10;
3107 } else {
3108 *result = *(stream + 2) - 'a' + 10;
3109 }
3110 } else {
3111 ast_log(LOG_ERROR, "Illegal character '%c' in hexadecimal string\n", *(stream + 2));
3112 return -1;
3113 }
3114
3115 if (strchr("0123456789ABCDEFabcdef", *(stream + 3)) && *(stream + 3) != '\0') {
3116 *consumed = 4;
3117 *result <<= 4;
3118 if (*(stream + 3) <= '9') {
3119 *result += *(stream + 3) - '0';
3120 } else if (*(stream + 3) <= 'F') {
3121 *result += *(stream + 3) - 'A' + 10;
3122 } else {
3123 *result += *(stream + 3) - 'a' + 10;
3124 }
3125 }
3126 break;
3127 case '0':
3128 /* Octal */
3129 *consumed = 2;
3130 for (i = 2; ; i++) {
3131 if (strchr("01234567", *(stream + i)) && *(stream + i) != '\0') {
3132 (*consumed)++;
3133 ast_debug(5, "result was %d, ", *result);
3134 *result <<= 3;
3135 *result += *(stream + i) - '0';
3136 ast_debug(5, "is now %d\n", *result);
3137 } else {
3138 break;
3139 }
3140 }
3141 break;
3142 default:
3143 *result = *(stream + 1);
3144 }
3145 } else {
3146 *result = *stream;
3147 *consumed = 1;
3148 }
3149 return 0;
3150}
3151
3152char *ast_get_encoded_str(const char *stream, char *result, size_t result_size)
3153{
3154 char *cur = result;
3155 size_t consumed;
3156
3157 while (cur < result + result_size - 1 && !ast_get_encoded_char(stream, cur, &consumed)) {
3158 cur++;
3159 stream += consumed;
3160 }
3161 *cur = '\0';
3162 return result;
3163}
3164
3165int ast_str_get_encoded_str(struct ast_str **str, int maxlen, const char *stream)
3166{
3167 char next, *buf;
3168 size_t offset = 0;
3169 size_t consumed;
3170
3171 if (strchr(stream, '\\')) {
3172 while (!ast_get_encoded_char(stream, &next, &consumed)) {
3173 if (offset + 2 > ast_str_size(*str) && maxlen > -1) {
3174 ast_str_make_space(str, maxlen > 0 ? maxlen : (ast_str_size(*str) + 48) * 2 - 48);
3175 }
3176 if (offset + 2 > ast_str_size(*str)) {
3177 break;
3178 }
3180 buf[offset++] = next;
3181 stream += consumed;
3182 }
3184 buf[offset++] = '\0';
3186 } else {
3187 ast_str_set(str, maxlen, "%s", stream);
3188 }
3189 return 0;
3190}
3191
3193{
3194 closefrom(n + 1);
3195}
3196
3197int ast_safe_fork(int stop_reaper)
3198{
3199 sigset_t signal_set, old_set;
3200 int pid;
3201
3202 /* Don't let the default signal handler for children reap our status */
3203 if (stop_reaper) {
3205 }
3206
3207 /* GCC 4.9 gives a bogus "right-hand operand of comma expression has
3208 * no effect" warning */
3209 (void) sigfillset(&signal_set);
3210 pthread_sigmask(SIG_BLOCK, &signal_set, &old_set);
3211
3212 pid = fork();
3213
3214 if (pid != 0) {
3215 /* Fork failed or parent */
3216 pthread_sigmask(SIG_SETMASK, &old_set, NULL);
3217 if (!stop_reaper && pid > 0) {
3218 struct zombie *cur = ast_calloc(1, sizeof(*cur));
3219 if (cur) {
3220 cur->pid = pid;
3226 ast_log(LOG_ERROR, "Shaun of the Dead wants to kill zombies, but can't?!!\n");
3228 }
3229 }
3230 }
3231 }
3232 return pid;
3233 } else {
3234 /* Child */
3235#ifdef HAVE_CAP
3236 cap_set_proc(child_cap);
3237#endif
3238
3239 /* Before we unblock our signals, return our trapped signals back to the defaults */
3240 signal(SIGHUP, SIG_DFL);
3241 signal(SIGCHLD, SIG_DFL);
3242 signal(SIGINT, SIG_DFL);
3243 signal(SIGURG, SIG_DFL);
3244 signal(SIGTERM, SIG_DFL);
3245 signal(SIGPIPE, SIG_DFL);
3246 signal(SIGXFSZ, SIG_DFL);
3247
3248 /* unblock important signal handlers */
3249 if (pthread_sigmask(SIG_UNBLOCK, &signal_set, NULL)) {
3250 ast_log(LOG_WARNING, "unable to unblock signals: %s\n", strerror(errno));
3251 _exit(1);
3252 }
3253
3254 return pid;
3255 }
3256}
3257
3259{
3261}
3262
3263int ast_app_parse_timelen(const char *timestr, int *result, enum ast_timelen unit)
3264{
3265 int res;
3266 char u[10];
3267#ifdef HAVE_LONG_DOUBLE_WIDER
3268 long double amount;
3269 #define FMT "%30Lf%9s"
3270#else
3271 double amount;
3272 #define FMT "%30lf%9s"
3273#endif
3274 if (!timestr) {
3275 return -1;
3276 }
3277
3278 res = sscanf(timestr, FMT, &amount, u);
3279
3280 if (res == 0 || res == EOF) {
3281#undef FMT
3282 return -1;
3283 } else if (res == 2) {
3284 switch (u[0]) {
3285 case 'h':
3286 case 'H':
3287 unit = TIMELEN_HOURS;
3288 if (u[1] != '\0') {
3289 return -1;
3290 }
3291 break;
3292 case 's':
3293 case 'S':
3294 unit = TIMELEN_SECONDS;
3295 if (u[1] != '\0') {
3296 return -1;
3297 }
3298 break;
3299 case 'm':
3300 case 'M':
3301 if (toupper(u[1]) == 'S') {
3302 unit = TIMELEN_MILLISECONDS;
3303 if (u[2] != '\0') {
3304 return -1;
3305 }
3306 } else if (u[1] == '\0') {
3307 unit = TIMELEN_MINUTES;
3308 } else {
3309 return -1;
3310 }
3311 break;
3312 default:
3313 return -1;
3314 }
3315 }
3316
3317 switch (unit) {
3318 case TIMELEN_HOURS:
3319 amount *= 60;
3320 /* fall-through */
3321 case TIMELEN_MINUTES:
3322 amount *= 60;
3323 /* fall-through */
3324 case TIMELEN_SECONDS:
3325 amount *= 1000;
3326 /* fall-through */
3328 ;
3329 }
3330 *result = amount > INT_MAX ? INT_MAX : (int) amount;
3331 return 0;
3332}
3333
3335{
3336 return queue_topic_all;
3337}
3338
3339struct stasis_topic *ast_queue_topic(const char *queuename)
3340{
3342}
3343
3344static void app_cleanup(void)
3345{
3346#ifdef HAS_CAP
3347 cap_free(child_cap);
3348#endif
3353}
3354
3355int app_init(void)
3356{
3358#ifdef HAVE_CAP
3359 child_cap = cap_from_text("cap_net_admin-eip");
3360#endif
3361 queue_topic_all = stasis_topic_create("queue:all");
3362 if (!queue_topic_all) {
3363 return -1;
3364 }
3366 if (!queue_topic_pool) {
3367 return -1;
3368 }
3369 return 0;
3370}
#define paren
Definition: ael_lex.c:962
#define comment
Definition: ael_lex.c:965
char digit
@ ignore_hangup
jack_status_t status
Definition: app_jack.c:146
const char * str
Definition: app_jack.c:147
unsigned int stop
Definition: app_sla.c:336
static int skipms
static const struct ast_vm_functions vm_table
static int messagecount(const char *mailbox_id, const char *folder)
static int inboxcount(const char *mailbox, int *newmsgs, int *oldmsgs)
static int silencethreshold
static int maxsilence
static int has_voicemail(const char *mailbox, const char *folder)
Determines if the given folder has messages.
static int inboxcount2(const char *mailbox, int *urgentmsgs, int *newmsgs, int *oldmsgs)
Check the given mailbox's message count.
static int sayname(struct ast_channel *chan, const char *mailbox, const char *context)
static struct ast_str * prompt
Definition: asterisk.c:2763
Asterisk main include file. File version handling, generic pbx functions.
int ast_register_cleanup(void(*func)(void))
Register a function to be executed before Asterisk gracefully exits.
Definition: clicompat.c:19
#define AST_FILE_MODE
Definition: asterisk.h:32
#define PATH_MAX
Definition: asterisk.h:40
#define ast_alloca(size)
call __builtin_alloca to ensure we get gcc builtin semantics
Definition: astmm.h:288
#define ast_free(a)
Definition: astmm.h:180
#define ast_strdup(str)
A wrapper for strdup()
Definition: astmm.h:241
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition: astmm.h:298
#define ast_calloc(num, len)
A wrapper for calloc()
Definition: astmm.h:202
#define ast_malloc(len)
A wrapper for malloc()
Definition: astmm.h:191
#define ast_log
Definition: astobj2.c:42
@ AO2_ALLOC_OPT_LOCK_NOLOCK
Definition: astobj2.h:367
#define ao2_global_obj_replace_unref(holder, obj)
Replace an ao2 object in the global holder, throwing away any old object.
Definition: astobj2.h:901
#define ao2_cleanup(obj)
Definition: astobj2.h:1934
#define ao2_global_obj_ref(holder)
Get a reference to the object stored in the global holder.
Definition: astobj2.h:918
#define ao2_alloc_options(data_size, destructor_fn, options)
Definition: astobj2.h:404
#define ao2_global_obj_release(holder)
Release the ao2 object held in the global holder.
Definition: astobj2.h:859
#define ao2_bump(obj)
Bump refcount on an AO2 object by one, returning the object.
Definition: astobj2.h:480
static int tmp()
Definition: bt_open.c:389
static void suspend(struct cc_core_instance *core_instance)
Definition: ccss.c:3160
static char * table
Definition: cdr_odbc.c:55
static PGresult * result
Definition: cel_pgsql.c:84
static const char type[]
Definition: chan_ooh323.c:109
General Asterisk PBX channel definitions.
int ast_waitfordigit(struct ast_channel *c, int ms)
Waits for a digit.
Definition: channel.c:3175
const char * ast_channel_name(const struct ast_channel *chan)
int ast_autoservice_stop(struct ast_channel *chan)
Stop servicing a channel for us...
Definition: autoservice.c:266
int ast_activate_generator(struct ast_channel *chan, struct ast_generator *gen, void *params)
Definition: channel.c:2951
int ast_readstring_full(struct ast_channel *c, char *s, int len, int timeout, int rtimeout, char *enders, int audiofd, int ctrlfd)
Definition: channel.c:6563
int ast_queue_hangup(struct ast_channel *chan)
Queue a hangup frame.
Definition: channel.c:1150
int ast_senddigit(struct ast_channel *chan, char digit, unsigned int duration)
Send a DTMF digit to a channel.
Definition: channel.c:4974
struct ast_silence_generator * ast_channel_start_silence_generator(struct ast_channel *chan)
Starts a silence generator on the given channel.
Definition: channel.c:8164
int ast_waitfor(struct ast_channel *chan, int ms)
Wait for input on a channel.
Definition: channel.c:3162
struct ast_flags * ast_channel_flags(struct ast_channel *chan)
void ast_channel_stop_silence_generator(struct ast_channel *chan, struct ast_silence_generator *state)
Stops a previously-started silence generator on the given channel.
Definition: channel.c:8210
int ast_check_hangup_locked(struct ast_channel *chan)
Definition: channel.c:459
int ast_write(struct ast_channel *chan, struct ast_frame *frame)
Write a frame to a channel This function writes the given frame to the indicated channel.
Definition: channel.c:5144
int ast_autoservice_start(struct ast_channel *chan)
Automatically service a channel for us...
Definition: autoservice.c:200
struct ast_frame * ast_read(struct ast_channel *chan)
Reads a frame.
Definition: channel.c:4257
int ast_senddigit_mf_end(struct ast_channel *chan)
End sending an MF digit to a channel.
Definition: channel.c:4943
int ast_senddigit_external(struct ast_channel *chan, char digit, unsigned int duration)
Send a DTMF digit to a channel from an external thread.
Definition: channel.c:4987
int ast_set_read_format(struct ast_channel *chan, struct ast_format *format)
Sets read format on channel chan.
Definition: channel.c:5762
struct ast_frame * ast_read_noaudio(struct ast_channel *chan)
Reads a frame, returning AST_FRAME_NULL frame if audio.
Definition: channel.c:4267
struct ast_tone_zone * ast_channel_zone(const struct ast_channel *chan)
struct ast_format * ast_channel_writeformat(struct ast_channel *chan)
int ast_set_write_format(struct ast_channel *chan, struct ast_format *format)
Sets write format on channel chan.
Definition: channel.c:5803
const char * ast_channel_language(const struct ast_channel *chan)
int ast_senddigit_mf(struct ast_channel *chan, char digit, unsigned int duration, unsigned int durationkp, unsigned int durationst, int is_external)
Send an MF digit to a channel.
Definition: channel.c:4952
@ AST_FLAG_WRITE_INT
Definition: channel.h:983
struct ast_filestream * ast_channel_stream(const struct ast_channel *chan)
struct ast_pbx * ast_channel_pbx(const struct ast_channel *chan)
struct ast_party_caller * ast_channel_caller(struct ast_channel *chan)
int ast_readstring(struct ast_channel *c, char *s, int len, int timeout, int rtimeout, char *enders)
Reads multiple digits.
Definition: channel.c:6558
int ast_indicate(struct ast_channel *chan, int condition)
Indicates condition of channel.
Definition: channel.c:4277
int ast_safe_sleep(struct ast_channel *chan, int ms)
Wait for a specified amount of time, looking for hangups.
Definition: channel.c:1574
#define AST_MAX_EXTENSION
Definition: channel.h:134
struct ast_format * ast_channel_readformat(struct ast_channel *chan)
ast_lock_type
Definition: check_expr.c:35
Convenient Signal Processing routines.
void ast_dsp_set_threshold(struct ast_dsp *dsp, int threshold)
Set the minimum average magnitude threshold to determine talking by the DSP.
Definition: dsp.c:1788
void ast_dsp_free(struct ast_dsp *dsp)
Definition: dsp.c:1783
@ THRESHOLD_SILENCE
Definition: dsp.h:73
int ast_dsp_silence(struct ast_dsp *dsp, struct ast_frame *f, int *totalsilence)
Process the audio frame for silence.
Definition: dsp.c:1488
int ast_dsp_get_threshold_from_settings(enum threshold which)
Get silence threshold from dsp.conf.
Definition: dsp.c:2009
struct ast_dsp * ast_dsp_new(void)
Allocates a new dsp, assumes 8khz for internal sample rate.
Definition: dsp.c:1758
char * end
Definition: eagi_proxy.c:73
char buf[BUFSIZE]
Definition: eagi_proxy.c:66
long int flag
Definition: f2c.h:83
Generic File Format Support. Should be included by clients of the file handling routines....
off_t ast_tellstream(struct ast_filestream *fs)
Tell where we are in a stream.
Definition: file.c:1085
int ast_waitstream_fr_w_cb(struct ast_channel *c, const char *breakon, const char *forward, const char *rewind, int ms, ast_waitstream_fr_cb cb)
Same as waitstream_fr but allows a callback to be alerted when a user fastforwards or rewinds the fil...
Definition: file.c:1798
struct ast_frame * ast_readframe(struct ast_filestream *s)
Read a frame from a filestream.
Definition: file.c:936
void() ast_waitstream_fr_cb(struct ast_channel *chan, long ms, enum ast_waitstream_fr_cb_values val)
callback used during dtmf controlled file playback to indicate location of playback in a file after r...
Definition: file.h:65
int ast_stopstream(struct ast_channel *c)
Stops a stream.
Definition: file.c:222
int ast_writestream(struct ast_filestream *fs, struct ast_frame *f)
Writes a frame to a stream.
Definition: file.c:244
int ast_seekstream(struct ast_filestream *fs, off_t sample_offset, int whence)
Seeks into stream.
Definition: file.c:1075
int ast_stream_rewind(struct ast_filestream *fs, off_t ms)
Rewind stream ms.
Definition: file.c:1100
int ast_filerename(const char *oldname, const char *newname, const char *fmt)
Renames a file.
Definition: file.c:1146
int ast_waitstream_fr(struct ast_channel *c, const char *breakon, const char *forward, const char *rewind, int ms)
Same as waitstream but allows stream to be forwarded or rewound.
Definition: file.c:1809
struct ast_filestream * ast_readfile(const char *filename, const char *type, const char *comment, int flags, int check, mode_t mode)
Starts reading from a file.
Definition: file.c:1371
int ast_streamfile(struct ast_channel *c, const char *filename, const char *preflang)
Streams a file.
Definition: file.c:1293
struct ast_filestream * ast_writefile(const char *filename, const char *type, const char *comment, int flags, int check, mode_t mode)
Starts writing a file.
Definition: file.c:1423
int ast_stream_and_wait(struct ast_channel *chan, const char *file, const char *digits)
stream file until digit If the file name is non-empty, try to play it.
Definition: file.c:1878
int ast_truncstream(struct ast_filestream *fs)
Trunc stream at current location.
Definition: file.c:1080
int ast_closestream(struct ast_filestream *f)
Closes a stream.
Definition: file.c:1111
int ast_filedelete(const char *filename, const char *fmt)
Deletes a file.
Definition: file.c:1141
#define AST_DIGIT_ANY
Definition: file.h:48
int ast_waitstream(struct ast_channel *c, const char *breakon)
Waits for a stream to stop or digit to be pressed.
Definition: file.c:1840
enum ast_format_cmp_res ast_format_cmp(const struct ast_format *format1, const struct ast_format *format2)
Compare two formats.
Definition: format.c:201
@ AST_FORMAT_CMP_NOT_EQUAL
Definition: format.h:38
const char * ast_format_get_name(const struct ast_format *format)
Get the name associated with a format.
Definition: format.c:334
Media Format Cache API.
struct ast_format * ast_format_slin
Built-in cached signed linear 8kHz format.
Definition: format_cache.c:41
static int array(struct ast_channel *chan, const char *cmd, char *var, const char *value)
static int quote(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
Application convenience functions, designed to give consistent look and feel to Asterisk apps.
ast_getdata_result
@ AST_GETDATA_EMPTY_END_TERMINATED
@ AST_LOCK_SUCCESS
@ AST_LOCK_PATH_NOT_FOUND
@ AST_LOCK_TIMEOUT
@ AST_LOCK_FAILURE
ast_vm_snapshot_sort_val
void ast_replace_sigchld(void)
Replace the SIGCHLD handler.
Definition: extconf.c:801
#define VM_GREETER_MODULE_VERSION
#define VM_MODULE_VERSION
AST_LOCK_TYPE
Type of locking to use in ast_lock_path / ast_unlock_path.
@ AST_LOCK_TYPE_LOCKFILE
@ AST_LOCK_TYPE_FLOCK
void() ast_vm_msg_play_cb(struct ast_channel *chan, const char *playfile, int duration)
Voicemail playback callback function definition.
@ TIMELEN_MILLISECONDS
@ TIMELEN_MINUTES
@ TIMELEN_SECONDS
@ TIMELEN_HOURS
ast_record_if_exists
@ AST_RECORD_IF_EXISTS_FAIL
@ AST_RECORD_IF_EXISTS_APPEND
@ AST_RECORD_IF_EXISTS_OVERWRITE
@ AST_RECORD_IF_EXISTS_ERROR
@ AST_ACTION_UPONE
@ AST_ACTION_BACKLIST
@ AST_ACTION_PLAYBACK
@ AST_ACTION_RESTART
@ AST_ACTION_PLAYLIST
@ AST_ACTION_CALLBACK
@ AST_ACTION_NOOP
@ AST_ACTION_EXIT
@ AST_ACTION_BACKGROUND
@ AST_ACTION_WAITOPTION
@ AST_ACTION_MENU
@ AST_ACTION_REPEAT
@ AST_ACTION_TRANSFER
void ast_unreplace_sigchld(void)
Restore the SIGCHLD handler.
Definition: extconf.c:815
char * strsep(char **str, const char *delims)
void closefrom(int lowfd)
#define AST_MALLOCD_HDR
#define AST_FRAME_DTMF
void ast_frame_dtor(struct ast_frame *frame)
NULL-safe wrapper for ast_frfree, good for RAII_VAR.
Definition: main/frame.c:187
#define ast_frfree(fr)
#define AST_FRIENDLY_OFFSET
Offset into a frame's data buffer.
@ AST_FRAME_VIDEO
@ AST_FRAME_VOICE
@ AST_FRAME_CONTROL
@ AST_CONTROL_RECORD_CANCEL
@ AST_CONTROL_WINK
@ AST_CONTROL_STREAM_RESTART
@ AST_CONTROL_STREAM_SUSPEND
@ AST_CONTROL_BUSY
@ AST_CONTROL_VIDUPDATE
@ AST_CONTROL_STREAM_REVERSE
@ AST_CONTROL_RECORD_STOP
@ AST_CONTROL_CONGESTION
@ AST_CONTROL_RECORD_MUTE
@ AST_CONTROL_HANGUP
@ AST_CONTROL_STREAM_STOP
@ AST_CONTROL_STREAM_FORWARD
@ AST_CONTROL_FLASH
@ AST_CONTROL_RECORD_SUSPEND
#define ast_debug(level,...)
Log a DEBUG message.
#define LOG_ERROR
#define ast_verb(level,...)
#define LOG_NOTICE
#define LOG_WARNING
Tone Indication Support.
static struct ast_tone_zone_sound * ast_tone_zone_sound_unref(struct ast_tone_zone_sound *ts)
Release a reference to an ast_tone_zone_sound.
Definition: indications.h:227
int ast_playtones_start(struct ast_channel *chan, int vol, const char *tonelist, int interruptible)
Start playing a list of tones on a channel.
Definition: indications.c:302
void ast_playtones_stop(struct ast_channel *chan)
Stop playing tones on a channel.
Definition: indications.c:393
struct ast_tone_zone_sound * ast_get_indication_tone(const struct ast_tone_zone *zone, const char *indication)
Locate a tone zone sound.
Definition: indications.c:461
static struct ast_tone_zone * ast_tone_zone_unref(struct ast_tone_zone *tz)
Release a reference to an ast_tone_zone.
Definition: indications.h:205
struct ast_tone_zone * ast_get_indication_zone(const char *country)
locate ast_tone_zone
Definition: indications.c:439
Asterisk JSON abstraction layer.
A set of macros to manage forward-linked lists.
#define AST_RWLIST_REMOVE_CURRENT
Definition: linkedlists.h:570
#define AST_RWLIST_RDLOCK(head)
Read locks a list.
Definition: linkedlists.h:78
#define AST_LIST_HEAD_STATIC(name, type)
Defines a structure to be used to hold a list of specified type, statically initialized.
Definition: linkedlists.h:291
#define AST_RWLIST_TRAVERSE_SAFE_BEGIN
Definition: linkedlists.h:545
#define AST_RWLIST_WRLOCK(head)
Write locks a list.
Definition: linkedlists.h:52
#define AST_RWLIST_UNLOCK(head)
Attempts to unlock a read/write based list.
Definition: linkedlists.h:151
#define AST_RWLIST_HEAD_STATIC(name, type)
Defines a structure to be used to hold a read/write list of specified type, statically initialized.
Definition: linkedlists.h:333
#define AST_LIST_EMPTY(head)
Checks whether the specified list contains any entries.
Definition: linkedlists.h:450
#define AST_LIST_INSERT_TAIL(head, elm, field)
Appends a list entry to the tail of a list.
Definition: linkedlists.h:731
#define AST_RWLIST_FIRST
Definition: linkedlists.h:423
#define AST_LIST_ENTRY(type)
Declare a forward link structure inside a list entry.
Definition: linkedlists.h:410
#define AST_RWLIST_TRAVERSE_SAFE_END
Definition: linkedlists.h:617
#define AST_LIST_TRAVERSE_SAFE_END
Closes a safe loop traversal block.
Definition: linkedlists.h:615
#define AST_LIST_LOCK(head)
Locks a list.
Definition: linkedlists.h:40
#define AST_RWLIST_TRAVERSE
Definition: linkedlists.h:494
#define AST_LIST_TRAVERSE_SAFE_BEGIN(head, var, field)
Loops safely over (traverses) the entries in a list.
Definition: linkedlists.h:529
#define AST_LIST_REMOVE_CURRENT(field)
Removes the current entry from a list during a traversal.
Definition: linkedlists.h:557
#define AST_RWLIST_INSERT_TAIL
Definition: linkedlists.h:741
#define AST_LIST_UNLOCK(head)
Attempts to unlock a list.
Definition: linkedlists.h:140
#define AST_LIST_FIRST(head)
Returns the first entry contained in a list.
Definition: linkedlists.h:421
#define AST_LIST_NEXT(elm, field)
Returns the next entry in the list after the given entry.
Definition: linkedlists.h:439
Asterisk locking-related definitions:
#define AST_PTHREADT_NULL
Definition: lock.h:66
const char * ast_app_expand_sub_args(struct ast_channel *chan, const char *args)
Add missing context/exten to subroutine argument string.
Definition: main/app.c:278
static enum control_tone_frame_response_result control_tone_frame_response(struct ast_channel *chan, struct ast_frame *fr, struct ast_tone_zone_sound *ts, const char *tone, int *paused)
Definition: main/app.c:1486
int ast_app_group_get_count(const char *group, const char *category)
Get the current channel count of the specified group and category.
Definition: main/app.c:2230
int __ast_vm_greeter_register(const struct ast_vm_greeter_functions *vm_table, struct ast_module *module)
Set voicemail greeter function callbacks.
Definition: main/app.c:479
static int global_maxsilence
Definition: main/app.c:1702
AST_THREADSTORAGE_PUBLIC(ast_str_thread_global_buf)
static int ast_ivr_menu_run_internal(struct ast_channel *chan, struct ast_ivr_menu *menu, void *cbdata)
Definition: main/app.c:2847
int ast_sf_stream(struct ast_channel *chan, struct ast_channel *peer, struct ast_channel *chan2, const char *digits, int frequency, int is_external)
Send a string of SF digits to a channel.
Definition: main/app.c:1097
int ast_play_and_record_full(struct ast_channel *chan, const char *playfile, const char *recordfile, int maxtime, const char *fmt, int *duration, int *sound_duration, int beep, int silencethreshold, int maxsilence, const char *path, const char *acceptdtmf, const char *canceldtmf, int skip_confirmation_sound, enum ast_record_if_exists if_exists)
Record a file based on input from a channel This function will play "auth-thankyou" upon successful r...
Definition: main/app.c:2139
int ast_app_getdata_full(struct ast_channel *c, const char *prompt, char *s, int maxlen, int timeout, int audiofd, int ctrlfd)
Full version with audiofd and controlfd. NOTE: returns '2' on ctrlfd available, not '1' like other fu...
Definition: main/app.c:247
void ast_safe_fork_cleanup(void)
Common routine to cleanup after fork'ed process is complete (if reaping was stopped)
Definition: main/app.c:3258
#define VM_API_CALL(res, api_call, api_parms)
Definition: main/app.c:547
int ast_str_get_encoded_str(struct ast_str **str, int maxlen, const char *stream)
Decode a stream of encoded control or extended ASCII characters.
Definition: main/app.c:3165
int ast_app_messagecount(const char *mailbox_id, const char *folder)
Get the number of messages in a given mailbox folder.
Definition: main/app.c:645
static int parse_options(const struct ast_app_option *options, void *_flags, char **args, char *optstr, int flaglen)
Definition: main/app.c:2973
static int ast_unlock_path_flock(const char *path)
Definition: main/app.c:2569
int __ast_vm_register(const struct ast_vm_functions *vm_table, struct ast_module *module)
Set voicemail function callbacks.
Definition: main/app.c:368
unsigned int __ast_app_separate_args(char *buf, char delim, int remove_chars, char **array, int arraylen)
Separate a string into arguments in an array.
Definition: main/app.c:2346
static int option_exists(struct ast_ivr_menu *menu, char *option)
Definition: main/app.c:2806
int ast_get_encoded_char(const char *stream, char *result, size_t *consumed)
Decode an encoded control or extended ASCII character.
Definition: main/app.c:3077
int ast_linear_stream(struct ast_channel *chan, const char *filename, int fd, int allowoverride)
Stream a filename (or file descriptor) as a generator.
Definition: main/app.c:1235
int ast_control_streamfile_w_cb(struct ast_channel *chan, const char *file, const char *fwd, const char *rev, const char *stop, const char *suspend, const char *restart, int skipms, long *offsetms, ast_waitstream_fr_cb cb)
Stream a file with fast forward, pause, reverse, restart.
Definition: main/app.c:1451
static const char default_acceptdtmf[]
Definition: main/app.c:2136
int app_init(void)
Initialize the application core.
Definition: main/app.c:3355
static pthread_t shaun_of_the_dead_thread
Definition: main/app.c:74
int ast_play_and_prepend(struct ast_channel *chan, char *playfile, char *recordfile, int maxtime, char *fmt, int *duration, int *sound_duration, int beep, int silencethreshold, int maxsilence)
Record a file based on input frm a channel. Recording is performed in 'prepend' mode which works a li...
Definition: main/app.c:2149
static int control_streamfile(struct ast_channel *chan, const char *file, const char *fwd, const char *rev, const char *stop, const char *suspend, const char *restart, int skipms, long *offsetms, const char *lang, ast_waitstream_fr_cb cb)
Definition: main/app.c:1277
int ast_app_group_update(struct ast_channel *old, struct ast_channel *new)
Update all group counting for a channel to a new one.
Definition: main/app.c:2290
void ast_dtmf_stream_external(struct ast_channel *chan, const char *digits, int between, unsigned int duration)
Send a string of DTMF digits to a channel from an external thread.
Definition: main/app.c:1142
int ast_vm_msg_play(struct ast_channel *chan, const char *mailbox, const char *context, const char *folder, const char *msg_num, ast_vm_msg_play_cb *cb)
Play a voicemail msg back on a channel.
Definition: main/app.c:726
static int global_silence_threshold
Definition: main/app.c:1701
struct stasis_topic * ast_queue_topic(const char *queuename)
Get the Stasis Message Bus API topic for queue messages for a particular queue name.
Definition: main/app.c:3339
static int linear_generator(struct ast_channel *chan, void *data, int len, int samples)
Definition: main/app.c:1171
#define FMT
static int ast_unlock_path_lockfile(const char *path)
Definition: main/app.c:2447
int ast_app_run_sub(struct ast_channel *autoservice_chan, struct ast_channel *sub_chan, const char *sub_location, const char *sub_args, int ignore_hangup)
Run a subroutine on a channel, placing an optional second channel into autoservice.
Definition: main/app.c:328
static const struct ast_app_stack_funcs * app_stack_callbacks
Definition: main/app.c:271
int ast_control_streamfile(struct ast_channel *chan, const char *file, const char *fwd, const char *rev, const char *stop, const char *suspend, const char *restart, int skipms, long *offsetms)
Stream a file with fast forward, pause, reverse, restart.
Definition: main/app.c:1465
static int dtmf_stream(struct ast_channel *chan, const char *digits, int between, unsigned int duration, int is_external)
Definition: main/app.c:1029
static int option_matchmore(struct ast_ivr_menu *menu, char *option)
Definition: main/app.c:2817
static struct ast_frame * make_silence(const struct ast_frame *orig)
Construct a silence frame of the same duration as orig.
Definition: main/app.c:1640
int ast_app_parse_timelen(const char *timestr, int *result, enum ast_timelen unit)
Common routine to parse time lengths, with optional time unit specifier.
Definition: main/app.c:3263
void ast_vm_greeter_unregister(const char *module_name)
Unregister the specified voicemail greeter provider.
Definition: main/app.c:511
static struct ast_generator linearstream
Definition: main/app.c:1228
int ast_control_tone(struct ast_channel *chan, const char *tone)
Controls playback of a tone.
Definition: main/app.c:1545
enum ast_getdata_result ast_app_getdata(struct ast_channel *c, const char *prompt, char *s, int maxlen, int timeout)
Plays a stream and gets DTMF data from a channel.
Definition: main/app.c:188
static enum AST_LOCK_RESULT ast_lock_path_lockfile(const char *path)
Definition: main/app.c:2410
#define SF_BETWEEN
static int __ast_play_and_record(struct ast_channel *chan, const char *playfile, const char *recordfile, int maxtime, const char *fmt, int *duration, int *sound_duration, int beep, int silencethreshold, int maxsilence, const char *path, int prepend, const char *acceptdtmf, const char *canceldtmf, int skip_confirmation_sound, enum ast_record_if_exists if_exists)
Definition: main/app.c:1727
static enum AST_LOCK_RESULT ast_lock_path_flock(const char *path)
Definition: main/app.c:2484
static const char default_canceldtmf[]
Definition: main/app.c:2137
static struct stasis_topic_pool * queue_topic_pool
Definition: main/app.c:91
int ast_app_group_match_get_count(const char *groupmatch, const char *category)
Get the current channel count of all groups that match the specified pattern and category.
Definition: main/app.c:2250
int ast_app_group_set_channel(struct ast_channel *chan, const char *data)
Set the group for a channel, splitting the provided data into group and category, if specified.
Definition: main/app.c:2183
int ast_app_group_list_wrlock(void)
Write Lock the group count list.
Definition: main/app.c:2326
static struct stasis_topic * queue_topic_all
Define Stasis Message Bus API topic objects.
Definition: main/app.c:90
enum AST_LOCK_RESULT ast_lock_path(const char *path)
Lock a filesystem path.
Definition: main/app.c:2604
char * ast_read_textfile(const char *filename)
Read a file into asterisk.
Definition: main/app.c:2939
int ast_app_has_voicemail(const char *mailboxes, const char *folder)
Determine if a given mailbox has any voicemail If folder is NULL, defaults to "INBOX"....
Definition: main/app.c:582
static void path_lock_destroy(struct path_lock *obj)
Definition: main/app.c:2473
int ast_safe_fork(int stop_reaper)
Common routine to safely fork without a chance of a signal handler firing badly in the child.
Definition: main/app.c:3197
static int sf_stream(struct ast_channel *chan, struct ast_channel *chan2, const char *digits, int frequency, int is_external)
Definition: main/app.c:763
static void app_cleanup(void)
Definition: main/app.c:3344
void ast_install_stack_functions(const struct ast_app_stack_funcs *funcs)
Set stack application function callbacks.
Definition: main/app.c:273
#define VM_GREETER_API_CALL(res, api_call, api_parms)
Definition: main/app.c:568
int ast_record_review(struct ast_channel *chan, const char *playfile, const char *recordfile, int maxtime, const char *fmt, int *duration, const char *path)
Allow to record message and have a review option.
Definition: main/app.c:2636
int ast_vm_msg_forward(const char *from_mailbox, const char *from_context, const char *from_folder, const char *to_mailbox, const char *to_context, const char *to_folder, size_t num_msgs, const char *msg_ids[], int delete_old)
forward a message from one mailbox to another.
Definition: main/app.c:709
static int external_sleep(struct ast_channel *chan, int ms)
Definition: main/app.c:757
control_tone_frame_response_result
Definition: main/app.c:1480
@ CONTROL_TONE_RESPONSE_FAILED
Definition: main/app.c:1481
@ CONTROL_TONE_RESPONSE_NORMAL
Definition: main/app.c:1482
@ CONTROL_TONE_RESPONSE_FINISHED
Definition: main/app.c:1483
static int read_newoption(struct ast_channel *chan, struct ast_ivr_menu *menu, char *exten, int maxexten)
Definition: main/app.c:2829
struct ast_vm_mailbox_snapshot * ast_vm_mailbox_snapshot_destroy(struct ast_vm_mailbox_snapshot *mailbox_snapshot)
destroy a snapshot
Definition: main/app.c:675
#define SF_BUF_LEN
enum ast_getdata_result ast_app_getdata_terminator(struct ast_channel *c, const char *prompt, char *s, int maxlen, int timeout, char *terminator)
Plays a stream and gets DTMF data from a channel.
Definition: main/app.c:193
#define RES_UPONE
Definition: main/app.c:2730
int ast_app_copy_recording_to_vm(struct ast_vm_recording_data *vm_rec_data)
param[in] vm_rec_data Contains data needed to make the recording. retval 0 voicemail successfully cre...
Definition: main/app.c:596
int ast_vm_greeter_is_registered(void)
Determine if a voicemail greeter provider is registered.
Definition: main/app.c:468
int ast_app_group_list_unlock(void)
Unlock the group count list.
Definition: main/app.c:2341
static int mf_stream(struct ast_channel *chan, struct ast_channel *chan2, const char *digits, int between, unsigned int duration, unsigned int durationkp, unsigned int durationst, int is_external)
Definition: main/app.c:914
#define AST_MAX_FORMATS
Definition: main/app.c:122
void ast_vm_unregister(const char *module_name)
Unregister the specified voicemail provider.
Definition: main/app.c:400
static void * shaun_of_the_dead(void *data)
Definition: main/app.c:95
void ast_set_lock_type(enum AST_LOCK_TYPE type)
Set the type of locks used by ast_lock_path()
Definition: main/app.c:2599
int ast_app_inboxcount(const char *mailboxes, int *newmsgs, int *oldmsgs)
Determine number of new/old messages in a mailbox.
Definition: main/app.c:604
static int vm_greeter_warnings
Definition: main/app.c:466
int ast_app_group_split_group(const char *data, char *group, int group_max, char *category, int category_max)
Split a group string into group and category, returning a default category if none is provided.
Definition: main/app.c:2156
static AO2_GLOBAL_OBJ_STATIC(vm_provider)
The container for the voicemail provider.
int ast_play_and_wait(struct ast_channel *chan, const char *fn)
Play a stream and wait for a digit, returning the digit that was pressed.
Definition: main/app.c:1616
int ast_control_streamfile_lang(struct ast_channel *chan, const char *file, const char *fwd, const char *rev, const char *stop, const char *suspend, const char *restart, int skipms, const char *lang, long *offsetms)
Version of ast_control_streamfile() which allows the language of the media file to be specified.
Definition: main/app.c:1473
#define SF_OFF
int ast_app_sayname(struct ast_channel *chan, const char *mailbox_id)
Play a recorded user name for the mailbox to the specified channel.
Definition: main/app.c:637
static int vm_warnings
Definition: main/app.c:355
#define RES_EXIT
Definition: main/app.c:2731
int ast_ivr_menu_run(struct ast_channel *chan, struct ast_ivr_menu *menu, void *cbdata)
Runs an IVR menu.
Definition: main/app.c:2932
const char * ast_vm_index_to_foldername(int id)
Return name of folder, given an id.
Definition: main/app.c:653
int ast_app_inboxcount2(const char *mailboxes, int *urgentmsgs, int *newmsgs, int *oldmsgs)
Determine number of urgent/new/old messages in a mailbox.
Definition: main/app.c:619
int ast_vm_msg_move(const char *mailbox, const char *context, size_t num_msgs, const char *oldfolder, const char *old_msg_ids[], const char *newfolder)
Move messages from one folder to another.
Definition: main/app.c:683
static void vm_greeter_warn_no_provider(void)
Definition: main/app.c:561
struct ast_group_info * ast_app_group_list_head(void)
Get the head of the group count list.
Definition: main/app.c:2336
void ast_app_options2str64(const struct ast_app_option *options, struct ast_flags64 *flags, char *buf, size_t len)
Given a list of options array, return an option string based on passed flags.
Definition: main/app.c:3066
int ast_app_group_discard(struct ast_channel *chan)
Discard all group counting for a channel.
Definition: main/app.c:2309
#define RES_REPEAT
Definition: main/app.c:2732
int ast_play_and_record(struct ast_channel *chan, const char *playfile, const char *recordfile, int maxtime, const char *fmt, int *duration, int *sound_duration, int silencethreshold, int maxsilence, const char *path)
Record a file based on input from a channel. Use default accept and cancel DTMF. This function will p...
Definition: main/app.c:2144
int ast_vm_is_registered(void)
Determine if a voicemail provider is registered.
Definition: main/app.c:357
static int set_read_to_slin(struct ast_channel *chan, struct ast_format **orig_format)
Sets a channel's read format to ast_format_slin, recording its original format.
Definition: main/app.c:1692
struct stasis_topic * ast_queue_topic_all(void)
Get the Stasis Message Bus API topic for queue messages.
Definition: main/app.c:3334
static void vm_warn_no_provider(void)
Definition: main/app.c:540
int ast_app_exec_sub(struct ast_channel *autoservice_chan, struct ast_channel *sub_chan, const char *sub_args, int ignore_hangup)
Run a subroutine on a channel, placing an optional second channel into autoservice.
Definition: main/app.c:297
struct ast_vm_mailbox_snapshot * ast_vm_mailbox_snapshot_create(const char *mailbox, const char *context, const char *folder, int descending, enum ast_vm_snapshot_sort_val sort_val, int combine_INBOX_and_OLD)
Create a snapshot of a mailbox which contains information about every msg.
Definition: main/app.c:661
int ast_dtmf_stream(struct ast_channel *chan, struct ast_channel *peer, const char *digits, int between, unsigned int duration)
Send a string of DTMF digits to a channel.
Definition: main/app.c:1127
int ast_mf_stream(struct ast_channel *chan, struct ast_channel *peer, struct ast_channel *chan2, const char *digits, int between, unsigned int duration, unsigned int durationkp, unsigned int durationst, int is_external)
Send a string of MF digits to a channel.
Definition: main/app.c:1113
int ast_app_parse_options(const struct ast_app_option *options, struct ast_flags *flags, char **args, char *optstr)
Parses a string containing application options and sets flags/arguments.
Definition: main/app.c:3056
int ast_app_dtget(struct ast_channel *chan, const char *context, char *collect, size_t size, int maxlen, int timeout)
This function presents a dialtone and reads an extension into 'collect' which must be a pointer to a ...
Definition: main/app.c:138
int ast_vm_msg_remove(const char *mailbox, const char *context, size_t num_msgs, const char *folder, const char *msgs[])
Remove/delete messages from a mailbox folder.
Definition: main/app.c:697
int ast_unlock_path(const char *path)
Unlock a path.
Definition: main/app.c:2620
static int parse_tone_uri(char *tone_parser, const char **tone_indication, const char **tone_zone)
Definition: main/app.c:1524
static int ivr_dispatch(struct ast_channel *chan, struct ast_ivr_option *option, char *exten, void *cbdata)
Definition: main/app.c:2737
int ast_app_parse_options64(const struct ast_app_option *options, struct ast_flags64 *flags, char **args, char *optstr)
Parses a string containing application options and sets flags/arguments.
Definition: main/app.c:3061
char * ast_get_encoded_str(const char *stream, char *result, size_t result_size)
Decode a stream of encoded control or extended ASCII characters.
Definition: main/app.c:3152
int ast_app_group_list_rdlock(void)
Read Lock the group count list.
Definition: main/app.c:2331
static void linear_release(struct ast_channel *chan, void *params)
Definition: main/app.c:1154
void ast_close_fds_above_n(int n)
Common routine for child processes, to close all fds prior to exec(2)
Definition: main/app.c:3192
#define SF_ON
#define RES_RESTART
Definition: main/app.c:2733
static void * linear_alloc(struct ast_channel *chan, void *params)
Definition: main/app.c:1201
int errno
Asterisk module definitions.
#define ast_module_unref(mod)
Release a reference to the module.
Definition: module.h:469
#define ast_module_running_ref(mod)
Hold a reference to the module if it is running.
Definition: module.h:455
@ AST_MODULE_LOAD_DECLINE
Module has failed to load, may be in an inconsistent state.
Definition: module.h:78
def from_mailbox(key, val, section, pjsip, nmapped)
#define ast_opt_transmit_silence
Definition: options.h:124
Asterisk file paths, configured in asterisk.conf.
const char * ast_config_AST_DATA_DIR
Definition: options.c:158
Core PBX routines and definitions.
int ast_exists_extension(struct ast_channel *c, const char *context, const char *exten, int priority, const char *callerid)
Determine whether an extension exists.
Definition: pbx.c:4175
int ast_ignore_pattern(const char *context, const char *pattern)
Checks to see if a number should be ignored.
Definition: pbx.c:6879
int ast_matchmore_extension(struct ast_channel *c, const char *context, const char *exten, int priority, const char *callerid)
Looks to see if adding anything to this extension might match something. (exists ^ canmatch)
Definition: pbx.c:4195
int ast_parseable_goto(struct ast_channel *chan, const char *goto_string)
Definition: pbx.c:8866
#define ast_poll(a, b, c)
Definition: poll-compat.h:88
static int maxretries
Definition: res_adsi.c:60
static struct stasis_rest_handlers mailboxes
REST handler for /api-docs/mailboxes.json.
#define NULL
Definition: resample.c:96
Stasis Message Bus API. See Stasis Message Bus API for detailed documentation.
struct stasis_topic * stasis_topic_pool_get_topic(struct stasis_topic_pool *pool, const char *topic_name)
Find or create a topic in the pool.
Definition: stasis.c:1884
struct stasis_topic * stasis_topic_create(const char *name)
Create a new topic.
Definition: stasis.c:617
struct stasis_topic_pool * stasis_topic_pool_create(struct stasis_topic *pooled_topic)
Create a topic pool that routes messages from dynamically generated topics to the given topic.
Definition: stasis.c:1833
char * ast_str_buffer(const struct ast_str *buf)
Returns the string buffer within the ast_str buf.
Definition: strings.h:761
#define S_OR(a, b)
returns the equivalent of logic or for strings: first one if not empty, otherwise second one.
Definition: strings.h:80
#define S_COR(a, b, c)
returns the equivalent of logic or for strings, with an additional boolean check: second one if not e...
Definition: strings.h:87
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition: strings.h:65
@ AST_STRSEP_TRIM
Definition: strings.h:256
@ AST_STRSEP_STRIP
Definition: strings.h:255
int ast_str_set(struct ast_str **buf, ssize_t max_len, const char *fmt,...)
Set a dynamic string using variable arguments.
Definition: strings.h:1113
#define ast_str_make_space(buf, new_len)
Definition: strings.h:828
void ast_str_update(struct ast_str *buf)
Update the length of the buffer, after using ast_str merely as a buffer.
Definition: strings.h:703
void ast_copy_string(char *dst, const char *src, size_t size)
Size-limited null-terminating string copy.
Definition: strings.h:425
size_t ast_str_size(const struct ast_str *buf)
Returns the current maximum length (without reallocation) of the current buffer.
Definition: strings.h:742
char * ast_strsep(char **s, const char sep, uint32_t flags)
Act like strsep but ignore separators inside quotes.
Definition: utils.c:1835
A structure to hold the description of an application 'option'.
Stack applications callback functions.
int(* run_sub)(struct ast_channel *chan, const char *args, int ignore_hangup)
Callback for the routine to run a subroutine on a channel.
const char *(* expand_sub_args)(struct ast_channel *chan, const char *args)
Add missing context/exten to Gosub application argument string.
Main Channel structure associated with a channel.
char exten[AST_MAX_EXTENSION]
char x
Definition: extconf.c:81
Definition: dsp.c:407
int totalsilence
Definition: dsp.c:411
This structure is allocated by file.c in one chunk, together with buf_size and desc_size bytes of mem...
Definition: mod_format.h:101
Structure used to handle a large number of boolean flags == used only in app_dial?
Definition: utils.h:204
uint64_t flags
Definition: utils.h:205
Structure used to handle boolean flags.
Definition: utils.h:199
unsigned int flags
Definition: utils.h:200
Definition of a media format.
Definition: format.c:43
struct ast_format * format
Data structure associated with a single frame of data.
struct ast_frame_subclass subclass
union ast_frame::@226 data
enum ast_frame_type frametype
void *(* alloc)(struct ast_channel *chan, void *params)
Definition: channel.h:226
channel group info
Definition: channel.h:2915
struct ast_group_info::@210 group_list
char * category
Definition: channel.h:2917
char * group
Definition: channel.h:2918
struct ast_channel * chan
Definition: channel.h:2916
struct ast_ivr_option * options
ast_ivr_action action
int rtimeoutms
Definition: pbx.h:216
int dtimeoutms
Definition: pbx.h:215
Support for dynamic strings.
Definition: strings.h:623
Description of a tone.
Definition: indications.h:35
const char * data
Description of a tone.
Definition: indications.h:52
A set of tones for a given locale.
Definition: indications.h:74
Voicemail function table definition.
unsigned int module_version
The version of this function table.
const char * module_name
The name of the module that provides the voicemail functionality.
struct ast_module * module
The module for the voicemail provider.
Voicemail greeter function table definition.
const char * module_name
The name of the module that provides the voicemail greeter functionality.
struct ast_module * module
The module for the voicemail greeter provider.
Structure used for ast_copy_recording_to_vm in order to cleanly supply data needed for making the rec...
struct ast_format * origwfmt
Definition: main/app.c:1151
int allowoverride
Definition: main/app.c:1150
Number structure.
Definition: app_followme.c:154
char * path
Definition: main/app.c:2468
struct path_lock::@294 le
struct zombie::@293 list
pid_t pid
Definition: main/app.c:77
Test Framework API.
#define ast_test_suite_event_notify(s, f,...)
Definition: test.h:189
const char * args
static struct test_options options
static struct test_val d
static struct test_val c
Definitions to aid in the use of thread local storage.
Utility functions.
#define RAII_VAR(vartype, varname, initval, dtor)
Declare a variable that will call a destructor function when it goes out of scope.
Definition: utils.h:941
#define ast_test_flag64(p, flag)
Definition: utils.h:120
#define ast_assert(a)
Definition: utils.h:739
#define ast_pthread_create_background(a, b, c, d)
Definition: utils.h:592
#define ast_clear_flag(p, flag)
Definition: utils.h:77
long int ast_random(void)
Definition: utils.c:2312
#define ast_set_flag64(p, flag)
Definition: utils.h:127
#define ast_set_flag(p, flag)
Definition: utils.h:70
#define AST_FLAGS_ALL
Definition: utils.h:196