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