RTS API Documentation  1.10.11
switch_ivr_menu.c
Go to the documentation of this file.
1 /*
2  * FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
3  * Copyright (C) 2005-2014, Anthony Minessale II <anthm@freeswitch.org>
4  *
5  * Version: MPL 1.1
6  *
7  * The contents of this file are subject to the Mozilla Public License Version
8  * 1.1 (the "License"); you may not use this file except in compliance with
9  * the License. You may obtain a copy of the License at
10  * http://www.mozilla.org/MPL/
11  *
12  * Software distributed under the License is distributed on an "AS IS" basis,
13  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14  * for the specific language governing rights and limitations under the
15  * License.
16  *
17  * The Original Code is FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
18  *
19  * The Initial Developer of the Original Code is
20  * Anthony Minessale II <anthm@freeswitch.org>
21  * Portions created by the Initial Developer are Copyright (C)
22  * the Initial Developer. All Rights Reserved.
23  *
24  * Contributor(s):
25  *
26  * Anthony Minessale II <anthm@freeswitch.org>
27  * Neal Horman <neal at wanlink dot com>
28  *
29  * switch_ivr_menu.c -- IVR Library (menu code)
30  *
31  */
32 
33 #include <switch.h>
34 
36 
38  char *name;
42  char *exit_sound;
44  char *buf;
45  char *ptr;
47  char *confirm_key;
48  char *tts_engine;
49  char *tts_voice;
51  int digit_len;
54  int timeout;
59  uint32_t flags;
64  char *pin;
66  char *bad_pin_file;
67 };
68 
72  char *arg;
73  char *bind;
74  int re;
76 };
77 
78 #define MENU_EVENT_ENTER "menu::enter"
79 #define MENU_EVENT_EXIT "menu::exit"
80 
81 static void ivr_send_event(switch_core_session_t *session, char *event_type, switch_ivr_menu_t *menu)
82 {
84  switch_event_t *event = NULL;
86  switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "Menu-Name", menu->name);
87  switch_channel_event_set_data(channel, event);
88  switch_event_fire(&event);
89  }
90 }
91 
93 {
94  switch_ivr_menu_t *ret;
95  for (ret = stack; ret; ret = ret->next) {
96  if (!name || !strcmp(ret->name, name))
97  break;
98  }
99  return ret;
100 }
101 
103 {
105 
106  for (ptr = *top; ptr && ptr->next; ptr = ptr->next);
107 
108  if (ptr) {
109  ptr->next = bottom;
110  } else {
111  *top = bottom;
112  }
113 
114 }
115 
118  const char *name,
119  const char *greeting_sound,
120  const char *short_greeting_sound,
121  const char *invalid_sound,
122  const char *exit_sound,
123  const char *transfer_sound,
124  const char *confirm_macro,
125  const char *confirm_key,
126  const char *tts_engine,
127  const char *tts_voice,
128  int confirm_attempts,
129  int inter_timeout,
131 {
132  switch_ivr_menu_t *menu;
133  uint8_t newpool = 0;
134 
135  if (!pool) {
138  return SWITCH_STATUS_MEMERR;
139  }
140  newpool = 1;
141  }
142 
143  if (!(menu = switch_core_alloc(pool, sizeof(*menu)))) {
144  if (newpool) {
146  }
147 
149  return SWITCH_STATUS_MEMERR;
150  }
151 
152  menu->pool = pool;
153 
154  if (!confirm_attempts) {
155  confirm_attempts = 3;
156  }
157 
158  if (!inter_timeout) {
159  inter_timeout = timeout / 2;
160  }
161 
162  if (!zstr(name)) {
163  menu->name = switch_core_strdup(menu->pool, name);
164  }
165 
166  if (!zstr(greeting_sound)) {
167  menu->greeting_sound = switch_core_strdup(menu->pool, greeting_sound);
168  }
169 
170  if (!zstr(short_greeting_sound)) {
171  menu->short_greeting_sound = switch_core_strdup(menu->pool, short_greeting_sound);
172  }
173 
174  if (!zstr(invalid_sound)) {
175  menu->invalid_sound = switch_core_strdup(menu->pool, invalid_sound);
176  }
177 
178  if (!zstr(transfer_sound)) {
179  menu->transfer_sound = switch_core_strdup(menu->pool, transfer_sound);
180  }
181 
182  if (!zstr(exit_sound)) {
183  menu->exit_sound = switch_core_strdup(menu->pool, exit_sound);
184  }
185 
186  if (!zstr(confirm_key)) {
187  menu->confirm_key = switch_core_strdup(menu->pool, confirm_key);
188  }
189 
190  if (!zstr(confirm_macro)) {
191  menu->confirm_macro = switch_core_strdup(menu->pool, confirm_macro);
192  }
193 
194  if (!zstr(tts_engine)) {
195  menu->tts_engine = switch_core_strdup(menu->pool, tts_engine);
196  }
197 
198  if (!zstr(tts_voice)) {
199  menu->tts_voice = switch_core_strdup(menu->pool, tts_voice);
200  }
201 
203 
204  menu->inlen = digit_len;
205 
206  if (max_failures > 0) {
207  menu->max_failures = max_failures;
208  } else {
209  menu->max_failures = 3;
210  }
211 
212  if (max_timeouts > 0) {
213  menu->max_timeouts = max_timeouts;
214  } else {
215  menu->max_timeouts = 3;
216  }
217 
218  menu->timeout = timeout;
219 
221 
222  menu->actions = NULL;
223 
224  if (newpool) {
226  }
227 
228  if (menu->timeout <= 0) {
229  menu->timeout = 10000;
230  }
231 
232  if (main) {
233  switch_ivr_menu_stack_add(&main, menu);
234  } else {
236  }
237 
238  menu->buf = switch_core_alloc(menu->pool, 1024);
239 
240  *new_menu = menu;
241 
242  return SWITCH_STATUS_SUCCESS;
243 }
244 
246 {
247  switch_ivr_menu_action_t *action, *ap;
248  uint32_t len;
249 
250  if ((action = switch_core_alloc(menu->pool, sizeof(*action)))) {
251  action->bind = switch_core_strdup(menu->pool, bind);
252  action->arg = switch_core_strdup(menu->pool, arg);
253  if (*action->bind == '/') {
254  action->re = 1;
255  } else {
256  len = (uint32_t) strlen(action->bind);
257  if (len > menu->inlen) {
258  menu->inlen = len;
259  }
260  }
261  action->ivr_action = ivr_action;
262 
263  if (menu->actions) {
264  for(ap = menu->actions; ap && ap->next; ap = ap->next);
265  ap->next = action;
266  } else {
267  menu->actions = action;
268  }
269 
270  return SWITCH_STATUS_SUCCESS;
271  }
272 
273  return SWITCH_STATUS_MEMERR;
274 }
275 
277  switch_ivr_menu_action_function_t *function, const char *arg, const char *bind)
278 {
279  switch_ivr_menu_action_t *action, *ap;
280  uint32_t len;
281 
282  if ((action = switch_core_alloc(menu->pool, sizeof(*action)))) {
283  action->bind = switch_core_strdup(menu->pool, bind);
284  action->arg = switch_core_strdup(menu->pool, arg);
285 
286  if (*action->bind == '/') {
287  action->re = 1;
288  } else {
289  len = (uint32_t) strlen(action->bind);
290  if (len > menu->inlen) {
291  menu->inlen = len;
292  }
293  }
294 
295  action->function = function;
296 
297  if (menu->actions) {
298  for(ap = menu->actions; ap && ap->next; ap = ap->next);
299  ap->next = action;
300  } else {
301  menu->actions = action;
302  }
303 
304  return SWITCH_STATUS_SUCCESS;
305  }
306 
307  return SWITCH_STATUS_MEMERR;
308 }
309 
311 {
313 
314  if (stack != NULL && stack->pool != NULL) {
317  switch_memory_pool_t *pool = stack->pool;
318  status = switch_core_destroy_memory_pool(&pool);
319  } else {
320  status = SWITCH_STATUS_SUCCESS;
321  }
322  }
323 
324  return status;
325 }
326 
328 {
329  char terminator;
330  uint32_t len;
331  char *ptr;
333  switch_input_args_t args = { 0 };
334  switch_channel_t *channel;
335  char *sound_expanded = sound;
336  switch_size_t menu_buf_len = 0;
337  const char *terminator_str = "#";
338 
339  if (!session || !menu || zstr(sound)) {
340  return status;
341  }
342 
343  if ((channel = switch_core_session_get_channel(session))) {
344  const char *tmp;
345  sound_expanded = switch_channel_expand_variables(channel, sound);
346  if ((tmp = switch_channel_get_variable(channel, "ivr_menu_terminator")) && !zstr(tmp)) {
347  terminator_str = tmp;
348  }
349  }
350 
351  memset(menu->buf, 0, menu->inlen + 1);
352  menu->ptr = menu->buf;
353 
354  if (!need) {
355  len = 1;
356  ptr = NULL;
357  } else {
358  len = (uint32_t) menu->inlen + 1;
359  ptr = menu->ptr;
360  }
361  args.buf = ptr;
362  args.buflen = len;
363 
364  status = switch_ivr_play_file(session, NULL, sound_expanded, &args);
365 
366  if (sound_expanded != sound) {
367  switch_safe_free(sound_expanded);
368  }
369 
370  if (!need) {
371  return status;
372  }
373 
374  menu_buf_len = strlen(menu->buf);
375 
376  menu->ptr += menu_buf_len;
377  if (menu_buf_len < need) {
378  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "waiting for %u/%u digits t/o %d\n",
379  (uint32_t) (menu->inlen - strlen(menu->buf)), (uint32_t) need, menu->inter_timeout);
380  status = switch_ivr_collect_digits_count(session, menu->ptr, menu->inlen - strlen(menu->buf),
381  need, terminator_str, &terminator, menu_buf_len ? menu->inter_timeout : menu->timeout,
382  menu->inter_timeout, menu->timeout);
383  }
384 
385  if (menu->confirm_macro && status == SWITCH_STATUS_SUCCESS && *menu->buf != '\0') {
386  switch_input_args_t confirm_args = { 0 }, *ap = NULL;
387  char buf[10] = "";
388  char terminator_key;
389  int att = menu->confirm_attempts;
390 
391  while (att) {
392  confirm_args.buf = buf;
393  confirm_args.buflen = sizeof(buf);
394  memset(buf, 0, confirm_args.buflen);
395 
396  if (menu->confirm_key) {
397  ap = &confirm_args;
398  }
399 
400  switch_ivr_phrase_macro(session, menu->confirm_macro, menu->buf, NULL, ap);
401 
402  if (menu->confirm_key && *buf == '\0') {
403  switch_ivr_collect_digits_count(session, buf, sizeof(buf), 1, terminator_str, &terminator_key, menu->timeout, 0, 0);
404  }
405 
406  if (menu->confirm_key && *buf != '\0') {
407  if (*menu->confirm_key == *buf) {
409  "approving digits '%s' via confirm key %s\n", menu->buf, menu->confirm_key);
410  break;
411  } else {
412  att = 0;
413  break;
414  }
415  }
416  att--;
417  }
418  if (!att) {
419  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "rejecting digits '%s' via confirm key %s\n", menu->buf,
420  menu->confirm_key);
421  *menu->buf = '\0';
422  }
423  }
424 
425  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "digits '%s'\n", menu->buf);
426 
427  return status;
428 }
429 
430 static void exec_app(switch_core_session_t *session, char *app_str)
431 {
433  char *app = switch_core_session_strdup(session, app_str);
434  char *data = strchr(app, ' ');
435  char *expanded = NULL;
436 
437  if (data) {
438  *data++ = '\0';
439  }
440 
441  expanded = switch_channel_expand_variables(channel, data);
442 
443  switch_core_session_execute_application(session, app, expanded);
444 
445  if (expanded && expanded != data) {
446  free(expanded);
447  }
448 
449 }
450 
452 {
453  int reps = 0, errs = 0, timeouts = 0, match = 0, running = 1;
454  char *greeting_sound = NULL, *aptr = NULL;
455  char arg[512];
458  switch_ivr_menu_t *menu = NULL;
459  switch_channel_t *channel;
461 
462  switch_assert(stack);
463 
464  if (++stack->stack_count > 12) {
465  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Too many levels of recursion.\n");
467  }
468 
469  if (!session || zstr(name)) {
470  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Invalid menu context\n");
472  }
473 
474  channel = switch_core_session_get_channel(session);
475 
476  if (!(menu = switch_ivr_menu_find(stack, name))) {
477  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Invalid Menu!\n");
479  }
480 
481  if (!zstr(menu->tts_engine) && !zstr(menu->tts_voice)) {
482  switch_channel_set_variable(channel, "tts_engine", menu->tts_engine);
483  switch_channel_set_variable(channel, "tts_voice", menu->tts_voice);
484  }
485 
486  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "Executing IVR menu %s\n", menu->name);
487  switch_channel_set_variable(channel, "ivr_menu_status", "success");
488 
489  ivr_send_event(session, MENU_EVENT_ENTER, menu);
490 
491  if (!zstr(menu->pin)) {
492  char digit_buffer[128] = "";
493  char *digits_regex = switch_core_session_sprintf(session, "^%s$", menu->pin);
494 
495  if (switch_play_and_get_digits(session, (uint32_t)strlen(menu->pin), (uint32_t)strlen(menu->pin), 3, 3000, "#",
496  menu->prompt_pin_file, menu->bad_pin_file, NULL, digit_buffer, sizeof(digit_buffer),
497  digits_regex, 10000, NULL) != SWITCH_STATUS_SUCCESS) {
499  }
500  }
501 
502 
503  for (reps = 0; running && status == SWITCH_STATUS_SUCCESS; reps++) {
504  if (!switch_channel_ready(channel)) {
505  break;
506  }
507  if (errs == menu->max_failures) {
508  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "Maximum failures\n");
509  switch_channel_set_variable(channel, "ivr_menu_status", "failure");
510  if (!zstr(menu->exec_on_max_fail)) {
511  exec_app(session, menu->exec_on_max_fail);
512  }
513  break;
514  }
515  if (timeouts == menu->max_timeouts) {
516  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "Maximum timeouts\n");
517  switch_channel_set_variable(channel, "ivr_menu_status", "timeout");
518  if (!zstr(menu->exec_on_max_timeout)) {
519  exec_app(session, menu->exec_on_max_timeout);
520  }
521  break;
522  }
523 
524  if (reps > 0 && menu->short_greeting_sound) {
525  greeting_sound = menu->short_greeting_sound;
526  } else {
527  greeting_sound = menu->greeting_sound;
528  }
529 
530  match = 0;
531  aptr = NULL;
532 
533  memset(arg, 0, sizeof(arg));
534 
535  memset(menu->buf, 0, menu->inlen + 1);
536 
537  if (play_and_collect(session, menu, greeting_sound, menu->inlen) == SWITCH_STATUS_TIMEOUT && *menu->buf == '\0') {
538  timeouts++;
539  continue;
540  }
541 
542  if (*menu->buf != '\0') {
543 
544  for (ap = menu->actions; ap; ap = ap->next) {
545  int ok = 0;
546  char substituted[1024];
547  char *use_arg = ap->arg;
548 
549  if (!zstr(menu->tts_engine) && !zstr(menu->tts_voice)) {
550  switch_channel_set_variable(channel, "tts_engine", menu->tts_engine);
551  switch_channel_set_variable(channel, "tts_voice", menu->tts_voice);
552  }
553 
554  if (ap->re) {
555  switch_regex_t *re = NULL;
556  int ovector[30];
557 
558  if ((ok = switch_regex_perform(menu->buf, ap->bind, &re, ovector, sizeof(ovector) / sizeof(ovector[0])))) {
559  switch_perform_substitution(re, ok, ap->arg, menu->buf, substituted, sizeof(substituted), ovector);
560  use_arg = substituted;
561  }
562  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "action regex [%s] [%s] [%d]\n", menu->buf, ap->bind, ok);
563 
565  } else {
566  ok = !strcmp(menu->buf, ap->bind);
567  }
568 
569  if (ok) {
570  match++;
571  errs = 0;
572  if (ap->function) {
574  "IVR function on menu '%s' matched '%s' param '%s'\n", menu->name, menu->buf, use_arg);
575  todo = ap->function(menu, use_arg, arg, sizeof(arg), obj);
576  aptr = arg;
577  } else {
578  todo = ap->ivr_action;
579  aptr = use_arg;
581  "IVR action on menu '%s' matched '%s' param '%s'\n", menu->name, menu->buf, aptr);
582  }
583 
584  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "switch_ivr_menu_execute todo=[%d]\n", todo);
585 
586  switch (todo) {
588  status = SWITCH_STATUS_FALSE;
589  break;
591  status = switch_ivr_play_file(session, NULL, aptr, NULL);
592  break;
594  if (!strcmp(aptr, menu->name)) {
595  status = SWITCH_STATUS_SUCCESS;
596  } else {
597  reps = -1;
598  ivr_send_event(session, MENU_EVENT_EXIT, menu);
599  status = switch_ivr_menu_execute(session, stack, aptr, obj);
600  ivr_send_event(session, MENU_EVENT_ENTER, menu);
601  }
602  break;
604  {
605  switch_application_interface_t *application_interface;
606  char *app_name;
607  char *app_arg = NULL;
608 
609  status = SWITCH_STATUS_FALSE;
610 
611  if (!zstr(aptr)) {
612  app_name = switch_core_session_strdup(session, aptr);
613  if ((app_arg = strchr(app_name, ' '))) {
614  *app_arg++ = '\0';
615  }
616 
617  if ((application_interface = switch_loadable_module_get_application_interface(app_name))) {
618  if (!zstr(menu->transfer_sound) && !strcmp(app_name, "transfer")) {
619  play_and_collect(session, menu, menu->transfer_sound, 0);
620  }
621 
622  switch_core_session_exec(session, application_interface, app_arg);
623  UNPROTECT_INTERFACE(application_interface);
624  status = SWITCH_STATUS_SUCCESS;
625  }
626  }
627  }
628  break;
630  running = 0;
631  status = SWITCH_STATUS_SUCCESS;
632  break;
635  status = SWITCH_STATUS_BREAK;
636  break;
638  status = SWITCH_STATUS_SUCCESS;
639  break;
640  default:
642  break;
643  }
644  }
645  }
646 
647  if (switch_test_flag(menu, SWITCH_IVR_MENU_FLAG_STACK)) { /* top level */
648  if (switch_test_flag(stack, SWITCH_IVR_MENU_FLAG_FALLTOMAIN)) { /* catch the fallback and recover */
650  status = SWITCH_STATUS_SUCCESS;
651  running = 1;
652  continue;
653  }
654  }
655  }
656  if (!match) {
657  if (*menu->buf) {
658  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "IVR menu '%s' caught invalid input '%s'\n", menu->name,
659  menu->buf);
660  if (menu->invalid_sound) {
661  play_and_collect(session, menu, menu->invalid_sound, 0);
662  }
663  } else {
664  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "IVR menu '%s' no input detected\n", menu->name);
665  }
666  errs++;
667 
668  /* breaks are ok too */
669  if (SWITCH_STATUS_IS_BREAK(status)) {
670  status = SWITCH_STATUS_SUCCESS;
671  }
672  }
673  }
674 
675  if (stack->stack_count == 1) {
676  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "exit-sound '%s'\n", menu->exit_sound);
677  if (!zstr(menu->exit_sound)) {
678  status = play_and_collect(session, menu, menu->exit_sound, 0);
679  }
680  }
681 
682  end:
683 
684  stack->stack_count--;
685 
686  if (menu) {
687  ivr_send_event(session, MENU_EVENT_EXIT, menu);
688  }
689 
690  return status;
691 }
692 
693 /******************************************************************************************************/
694 
695 typedef struct switch_ivr_menu_xml_map {
696  char *name;
701 
706 };
707 
709 {
710  switch_ivr_menu_xml_map_t *map = (xml_ctx != NULL ? xml_ctx->map : NULL);
711  int rc = -1;
712 
713  while (map != NULL && (rc = strcasecmp(map->name, name)) != 0) {
714  map = map->next;
715  }
716 
717  return (rc == 0 ? map : NULL);
718 }
719 
722 {
724 
725  /* if this action/function does not exist yet */
726  if (xml_ctx != NULL && name != NULL && xml_ctx->pool != NULL && switch_ivr_menu_stack_xml_find(xml_ctx, name) == NULL) {
728 
729  if (map != NULL) {
730  map->name = switch_core_strdup(xml_ctx->pool, name);
731  map->action = action;
732  map->function = function;
733 
734  if (map->name != NULL) {
735  /* insert map item at top of list */
736  map->next = xml_ctx->map;
737  xml_ctx->map = map;
738  status = SWITCH_STATUS_SUCCESS;
739  } else {
740  status = SWITCH_STATUS_MEMERR;
741  }
742  } else {
743  status = SWITCH_STATUS_MEMERR;
744  }
745 
746  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "switch_ivr_menu_stack_xml_add binding '%s'\n", name);
747  } else {
748  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Unable to add binding %s\n", name);
749  }
750 
751  return status;
752 }
753 
754 static struct iam_s {
755  const char *name;
757 } iam[] = {
758  {
759  "menu-exit", SWITCH_IVR_ACTION_DIE}, {
760  "menu-sub", SWITCH_IVR_ACTION_EXECMENU}, {
761  "menu-exec-app", SWITCH_IVR_ACTION_EXECAPP}, {
762  "menu-play-sound", SWITCH_IVR_ACTION_PLAYSOUND}, {
763  "menu-back", SWITCH_IVR_ACTION_BACK}, {
764  "menu-top", SWITCH_IVR_ACTION_TOMAIN}, {
765  NULL, 0}
766 };
767 
769 {
770  int i;
771 
772  if (!zstr(action_name)) {
773  for (i = 0;; i++) {
774  if (!iam[i].name) {
775  break;
776  }
777 
778  if (!strcasecmp(iam[i].name, action_name)) {
779  *action = iam[i].action;
780  return SWITCH_STATUS_SUCCESS;
781  }
782  }
783  }
784 
785  return SWITCH_STATUS_FALSE;
786 }
787 
788 static switch_bool_t is_valid_action(const char *action)
789 {
790  int i;
791 
792  if (!zstr(action)) {
793  for (i = 0;; i++) {
794  if (!iam[i].name) {
795  break;
796  }
797 
798  if (!strcmp(iam[i].name, action)) {
799  return SWITCH_TRUE;
800  }
801  }
802  }
803 
804  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "Invalid Action [%s]\n", switch_str_nil(action));
805  return SWITCH_FALSE;
806 }
807 
809 {
811  int autocreated = 0;
812 
813  /* build a memory pool ? */
814  if (pool == NULL) {
815  status = switch_core_new_memory_pool(&pool);
816  autocreated = 1;
817  }
818  /* allocate the xml context */
819  if (xml_menu_ctx != NULL && pool != NULL) {
820  *xml_menu_ctx = switch_core_alloc(pool, sizeof(switch_ivr_menu_xml_ctx_t));
821  if (*xml_menu_ctx != NULL) {
822  (*xml_menu_ctx)->pool = pool;
823  (*xml_menu_ctx)->autocreated = autocreated;
824  (*xml_menu_ctx)->map = NULL;
825  status = SWITCH_STATUS_SUCCESS;
826  } else {
827  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Unable to alloc xml_ctx\n");
828  status = SWITCH_STATUS_FALSE;
829  }
830  }
831  /* build the standard/default xml menu handler mappings */
832  if (status == SWITCH_STATUS_SUCCESS && xml_menu_ctx != NULL && *xml_menu_ctx != NULL) {
833  int i;
834 
835  for (i = 0; iam[i].name && status == SWITCH_STATUS_SUCCESS; i++) {
836  status = switch_ivr_menu_stack_xml_add(*xml_menu_ctx, iam[i].name, iam[i].action, NULL);
837  }
838  }
839 
840  return status;
841 }
842 
844  const char *name, switch_ivr_menu_action_function_t *function)
845 {
846  return switch_ivr_menu_stack_xml_add(xml_menu_ctx, name, -1, function);
847 }
848 
850  switch_ivr_menu_t ** menu_stack, switch_xml_t xml_menus, switch_xml_t xml_menu)
851 {
853 
854  if (xml_menu_ctx != NULL && menu_stack != NULL && xml_menu != NULL) {
855  const char *menu_name = switch_xml_attr_soft(xml_menu, "name"); /* if the attr doesn't exist, return "" */
856  const char *greet_long = switch_xml_attr(xml_menu, "greet-long"); /* if the attr doesn't exist, return NULL */
857  const char *greet_short = switch_xml_attr(xml_menu, "greet-short"); /* if the attr doesn't exist, return NULL */
858  const char *invalid_sound = switch_xml_attr(xml_menu, "invalid-sound"); /* if the attr doesn't exist, return NULL */
859  const char *exit_sound = switch_xml_attr(xml_menu, "exit-sound"); /* if the attr doesn't exist, return NULL */
860  const char *transfer_sound = switch_xml_attr(xml_menu, "transfer-sound"); /* if the attr doesn't exist, return NULL */
861  const char *timeout = switch_xml_attr_soft(xml_menu, "timeout"); /* if the attr doesn't exist, return "" */
862  const char *max_failures = switch_xml_attr_soft(xml_menu, "max-failures"); /* if the attr doesn't exist, return "" */
863  const char *max_timeouts = switch_xml_attr_soft(xml_menu, "max-timeouts");
864  const char *exec_on_max_fail = switch_xml_attr(xml_menu, "exec-on-max-failures");
865  const char *exec_on_max_timeout = switch_xml_attr(xml_menu, "exec-on-max-timeouts");
866  const char *confirm_macro = switch_xml_attr(xml_menu, "confirm-macro");
867  const char *confirm_key = switch_xml_attr(xml_menu, "confirm-key");
868  const char *tts_engine = switch_xml_attr(xml_menu, "tts-engine");
869  const char *tts_voice = switch_xml_attr(xml_menu, "tts-voice");
870  const char *confirm_attempts = switch_xml_attr_soft(xml_menu, "confirm-attempts");
871  const char *digit_len = switch_xml_attr_soft(xml_menu, "digit-len");
872  const char *inter_timeout = switch_xml_attr_soft(xml_menu, "inter-digit-timeout");
873  const char *pin = switch_xml_attr_soft(xml_menu, "pin");
874  const char *prompt_pin_file = switch_xml_attr_soft(xml_menu, "pin-file");
875  const char *bad_pin_file = switch_xml_attr_soft(xml_menu, "bad-pin-file");
876 
877  switch_ivr_menu_t *menu = NULL;
878 
879  if (zstr(max_timeouts)) {
880  max_timeouts = max_failures;
881  }
882 
883  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "building menu '%s'\n", menu_name);
884 
885  status = switch_ivr_menu_init(&menu,
886  *menu_stack,
887  menu_name,
888  greet_long,
889  greet_short,
890  invalid_sound,
891  exit_sound,
892  transfer_sound,
893  confirm_macro,
894  confirm_key,
895  tts_engine,
896  tts_voice,
897  atoi(confirm_attempts),
898  atoi(inter_timeout),
899  atoi(digit_len),
900  atoi(timeout),
901  strlen(max_failures) ? atoi(max_failures) : 0, strlen(max_timeouts) ? atoi(max_timeouts) : 0, xml_menu_ctx->pool);
902 
903  switch_assert(menu);
904 
905  if (!zstr(exec_on_max_fail)) {
906  menu->exec_on_max_fail = switch_core_strdup(menu->pool, exec_on_max_fail);
907  }
908 
909  if (!zstr(exec_on_max_timeout)) {
910  menu->exec_on_max_timeout = switch_core_strdup(menu->pool, exec_on_max_timeout);
911  }
912 
913  if (!zstr(pin)) {
914  if (zstr(prompt_pin_file)) {
915  prompt_pin_file = "ivr/ivr-please_enter_pin_followed_by_pound.wav";
916  }
917  if (zstr(bad_pin_file)) {
918  bad_pin_file = "conference/conf-bad-pin.wav";
919  }
920  menu->pin = switch_core_strdup(menu->pool, pin);
921  menu->prompt_pin_file = switch_core_strdup(menu->pool, prompt_pin_file);
922  menu->bad_pin_file = switch_core_strdup(menu->pool, bad_pin_file);
923  }
924 
925  /* set the menu_stack for the caller */
926  if (status == SWITCH_STATUS_SUCCESS && *menu_stack == NULL) {
927  *menu_stack = menu;
928 
929  if (xml_menu_ctx->autocreated) {
931  }
932  }
933 
934  if (status == SWITCH_STATUS_SUCCESS) {
935  switch_xml_t xml_kvp;
936 
937  /* build menu entries */
938  for (xml_kvp = switch_xml_child(xml_menu, "entry"); xml_kvp != NULL && status == SWITCH_STATUS_SUCCESS; xml_kvp = xml_kvp->next) {
939  const char *action = switch_xml_attr(xml_kvp, "action");
940  const char *digits = switch_xml_attr(xml_kvp, "digits");
941  const char *param = switch_xml_attr_soft(xml_kvp, "param");
942 
943  if (is_valid_action(action) && !zstr(digits)) {
944  switch_ivr_menu_xml_map_t *xml_map = xml_menu_ctx->map;
945  int found = 0;
946 
947  /* find and appropriate xml handler */
948  while (xml_map != NULL && !found) {
949  if (!(found = (strcasecmp(xml_map->name, action) == 0))) {
950  xml_map = xml_map->next;
951  }
952  }
953 
954  if (found && xml_map != NULL) {
955  /* do we need to build a new sub-menu ? */
956  if (xml_map->action == SWITCH_IVR_ACTION_EXECMENU && switch_ivr_menu_find(*menu_stack, param) == NULL) {
957  if ((xml_menu = switch_xml_find_child(xml_menus, "menu", "name", param)) != NULL) {
958  status = switch_ivr_menu_stack_xml_build(xml_menu_ctx, menu_stack, xml_menus, xml_menu);
959  }
960  }
961  /* finally bind the menu entry */
962  if (status == SWITCH_STATUS_SUCCESS) {
963  if (xml_map->function != NULL) {
965  "binding menu caller control '%s'/'%s' to '%s'\n", xml_map->name, param, digits);
966  status = switch_ivr_menu_bind_function(menu, xml_map->function, param, digits);
967  } else {
968  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "binding menu action '%s' to '%s'\n", xml_map->name, digits);
969  status = switch_ivr_menu_bind_action(menu, xml_map->action, param, digits);
970  }
971  }
972  }
973  } else {
974  status = SWITCH_STATUS_FALSE;
975  }
976  }
977  }
978  }
979 
980  if (status != SWITCH_STATUS_SUCCESS) {
981  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Unable to build xml menu\n");
982  }
983 
984  return status;
985 }
986 
987 /* For Emacs:
988  * Local Variables:
989  * mode:c
990  * indent-tabs-mode:t
991  * tab-width:4
992  * c-basic-offset:4
993  * End:
994  * For VIM:
995  * vim:set softtabstop=4 shiftwidth=4 tabstop=4 noet:
996  */
#define switch_event_fire(event)
Fire an event filling in most of the arguements with obvious values.
Definition: switch_event.h:413
#define switch_regex_safe_free(re)
Definition: switch_regex.h:79
#define switch_core_new_memory_pool(p)
Create a new sub memory pool from the core&#39;s master pool.
Definition: switch_core.h:633
A module interface to implement an application.
const char * switch_xml_attr_soft(_In_ switch_xml_t xml, _In_z_ const char *attr)
returns the value of the requested tag attribute, or "" if not found
char * switch_core_session_sprintf(_In_ switch_core_session_t *session, _In_z_ _Printf_format_string_ const char *fmt,...)
printf-style style printing routine. The data is output to a string allocated from the session ...
switch_ivr_menu_action_function_t * function
#define SWITCH_CHANNEL_SESSION_LOG(x)
#define switch_set_flag(obj, flag)
Set a flag on an arbitrary object.
Definition: switch_utils.h:700
const char * name
#define SWITCH_CHANNEL_LOG
switch_xml_t switch_xml_find_child(_In_ switch_xml_t node, _In_z_ const char *childname, _In_opt_z_ const char *attrname, _In_opt_z_ const char *value)
find a child tag in a node called &#39;childname&#39; with an attribute &#39;attrname&#39; which equals &#39;value&#39; ...
const char * switch_xml_attr(_In_opt_ switch_xml_t xml, _In_opt_z_ const char *attr)
returns the value of the requested tag attribute, or NULL if not found
switch_status_t switch_play_and_get_digits(switch_core_session_t *session, uint32_t min_digits, uint32_t max_digits, uint32_t max_tries, uint32_t timeout, const char *valid_terminators, const char *audio_file, const char *bad_input_audio_file, const char *var_name, char *digit_buffer, uint32_t digit_buffer_length, const char *digits_regex, uint32_t digit_timeout, const char *transfer_on_failure)
Play a sound and gather digits with the number of retries specified if the user doesn&#39;t give digits i...
#define SWITCH_STATUS_IS_BREAK(x)
Definition: switch_utils.h:633
void switch_perform_substitution(switch_regex_t *re, int match_count, const char *data, const char *field_data, char *substituted, switch_size_t len, int *ovector)
Definition: switch_regex.c:131
void switch_channel_event_set_data(_In_ switch_channel_t *channel, _In_ switch_event_t *event)
Add information about a given channel to an event object.
switch_bool_t
Definition: switch_types.h:437
#define switch_core_strdup(_pool, _todup)
Copy a string using memory allocation from a given pool.
Definition: switch_core.h:733
struct switch_ivr_menu_xml_map switch_ivr_menu_xml_map_t
switch_status_t switch_ivr_menu_bind_action(switch_ivr_menu_t *menu, switch_ivr_action_t ivr_action, const char *arg, const char *bind)
switch_ivr_menu_bind_action: Bind a keystroke to an action.
#define switch_core_destroy_memory_pool(p)
Returns a subpool back to the main pool.
Definition: switch_core.h:642
Representation of an event.
Definition: switch_event.h:80
#define switch_channel_ready(_channel)
switch_status_t switch_core_session_exec(_In_ switch_core_session_t *session, _In_ const switch_application_interface_t *application_interface, _In_opt_z_ const char *arg)
Execute an application on a session.
switch_status_t switch_ivr_play_file(switch_core_session_t *session, switch_file_handle_t *fh, const char *file, switch_input_args_t *args)
play a file from the disk to the session
A representation of an XML tree.
Definition: switch_xml.h:79
struct real_pcre switch_regex_t
Definition: switch_regex.h:43
#define switch_event_create_subclass(_e, _eid, _sn)
Definition: switch_event.h:153
switch_status_t switch_ivr_menu_execute(switch_core_session_t *session, switch_ivr_menu_t *stack, char *name, void *obj)
Execute a menu.
switch_status_t switch_ivr_menu_stack_xml_add_custom(switch_ivr_menu_xml_ctx_t *xml_menu_ctx, const char *name, switch_ivr_menu_action_function_t *function)
struct switch_ivr_menu_action * actions
#define zstr(x)
Definition: switch_utils.h:314
switch_size_t inlen
static void ivr_send_event(switch_core_session_t *session, char *event_type, switch_ivr_menu_t *menu)
#define switch_core_session_execute_application(_a, _b, _c)
Execute an application on a session.
Definition: switch_core.h:1129
_Ret_ switch_channel_t * switch_core_session_get_channel(_In_ switch_core_session_t *session)
Retrieve a pointer to the channel object associated with a given session.
#define UNPROTECT_INTERFACE(_it)
#define switch_clear_flag(obj, flag)
Clear a flag on an arbitrary object while locked.
Definition: switch_utils.h:724
int main(int argc, char *argv[])
Definition: fs_encode.c:56
switch_xml_t next
Definition: switch_xml.h:91
switch_status_t switch_ivr_collect_digits_count(switch_core_session_t *session, char *buf, switch_size_t buflen, switch_size_t maxdigits, const char *terminators, char *terminator, uint32_t first_timeout, uint32_t digit_timeout, uint32_t abs_timeout)
Wait for specified number of DTMF digits, untile terminator is received or until the channel hangs up...
Definition: switch_ivr.c:1342
static void exec_app(switch_core_session_t *session, char *app_str)
static switch_ivr_menu_xml_map_t * switch_ivr_menu_stack_xml_find(switch_ivr_menu_xml_ctx_t *xml_ctx, const char *name)
switch_application_interface_t * switch_loadable_module_get_application_interface(const char *name)
Retrieve the application interface by it&#39;s registered name.
#define switch_core_alloc(_pool, _mem)
Allocate memory directly from a memory pool.
Definition: switch_core.h:684
#define switch_channel_get_variable(_c, _v)
switch_status_t switch_event_add_header_string(switch_event_t *event, switch_stack_t stack, const char *header_name, const char *data)
Add a string header to an event.
switch_ivr_action_t ivr_action
#define switch_safe_free(it)
Free a pointer and set it to NULL unless it already is NULL.
Definition: switch_utils.h:885
uintptr_t switch_size_t
switch_ivr_action_t action
static switch_status_t play_and_collect(switch_core_session_t *session, switch_ivr_menu_t *menu, char *sound, switch_size_t need)
switch_memory_pool_t * pool
int switch_regex_perform(const char *field, const char *expression, switch_regex_t **new_re, int *ovector, uint32_t olen)
Definition: switch_regex.c:55
#define MENU_EVENT_EXIT
switch_ivr_action_t switch_ivr_menu_action_function_t(struct switch_ivr_menu *, char *, char *, size_t, void *)
Definition: switch_ivr.h:816
static int32_t running
struct switch_ivr_menu_xml_map * next
#define switch_str_nil(s)
Make a null string a blank string instead.
Definition: switch_utils.h:993
static switch_bool_t is_valid_action(const char *action)
#define switch_channel_expand_variables(_channel, _in)
switch_status_t switch_ivr_menu_init(switch_ivr_menu_t **new_menu, switch_ivr_menu_t *main, const char *name, const char *greeting_sound, const char *short_greeting_sound, const char *invalid_sound, const char *exit_sound, const char *transfer_sound, const char *confirm_macro, const char *confirm_key, const char *tts_engine, const char *tts_voice, int confirm_attempts, int inter_timeout, int digit_len, int timeout, int max_failures, int max_timeouts, switch_memory_pool_t *pool)
Create a new menu object.
switch_status_t
Common return values.
switch_status_t switch_ivr_menu_stack_xml_build(switch_ivr_menu_xml_ctx_t *xml_menu_ctx, switch_ivr_menu_t **menu_stack, switch_xml_t xml_menus, switch_xml_t xml_menu)
Build a menu stack from an xml source.
#define switch_goto_status(_status, _label)
Definition: switch_utils.h:287
char * exec_on_max_timeout
Main Library Header.
switch_ivr_action_t
Definition: switch_ivr.h:806
switch_memory_pool_t * pool
#define SWITCH_DECLARE(type)
switch_xml_t switch_xml_child(_In_ switch_xml_t xml, _In_z_ const char *name)
returns the first child tag (one level deeper) with the given name or NULL \ if not found ...
switch_status_t switch_ivr_menu_str2action(const char *action_name, switch_ivr_action_t *action)
static struct iam_s iam[]
switch_ivr_menu_action_function_t * function
#define switch_test_flag(obj, flag)
Test for the existance of a flag on an arbitary object.
Definition: switch_utils.h:693
void switch_log_printf(_In_ switch_text_channel_t channel, _In_z_ const char *file, _In_z_ const char *func, _In_ int line, _In_opt_z_ const char *userdata, _In_ switch_log_level_t level, _In_z_ _Printf_format_string_ const char *fmt,...) PRINTF_FUNCTION(7
Write log data to the logging engine.
#define MENU_EVENT_ENTER
switch_status_t switch_ivr_menu_stack_xml_init(switch_ivr_menu_xml_ctx_t **xml_menu_ctx, switch_memory_pool_t *pool)
static void switch_ivr_menu_stack_add(switch_ivr_menu_t **top, switch_ivr_menu_t *bottom)
#define switch_ivr_phrase_macro(session, macro_name, data, lang, args)
Definition: switch_ivr.h:936
struct fspr_pool_t switch_memory_pool_t
#define switch_core_session_strdup(_session, _todup)
Copy a string using memory allocation from a session&#39;s pool.
Definition: switch_core.h:719
struct switch_ivr_menu_action * next
#define switch_assert(expr)
#define switch_channel_set_variable(_channel, _var, _val)
static switch_status_t switch_ivr_menu_stack_xml_add(switch_ivr_menu_xml_ctx_t *xml_ctx, const char *name, int action, switch_ivr_menu_action_function_t *function)
struct switch_ivr_menu_xml_map * map
memset(buf, 0, buflen)
struct switch_ivr_menu * next
char * short_greeting_sound
switch_ivr_action_t action
static switch_ivr_menu_t * switch_ivr_menu_find(switch_ivr_menu_t *stack, const char *name)
switch_status_t switch_ivr_menu_bind_function(switch_ivr_menu_t *menu, switch_ivr_menu_action_function_t *function, const char *arg, const char *bind)
Bind a keystroke to a callback function.
switch_status_t switch_ivr_menu_stack_free(switch_ivr_menu_t *stack)
free a stack of menu objects.