PageRenderTime 52ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/src/mod/applications/mod_protovm/util.c

https://github.com/mzeena/FreeSWITCH-qmod
C | 175 lines | 109 code | 33 blank | 33 comment | 28 complexity | baad067c355d26bc2f5f568de479df1c MD5 | raw file
  1. /*
  2. * FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
  3. * Copyright (C) 2005-2011, 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. * Marc Olivier Chouinard <mochouinard@moctel.com>
  27. *
  28. *
  29. * utils.c -- MT VoiceMail / Different utility that might need to go into the core (after cleanup)
  30. *
  31. */
  32. #include <switch.h>
  33. #include "util.h"
  34. switch_status_t mt_merge_media_files(const char** inputs, const char *output) {
  35. switch_status_t status = SWITCH_STATUS_SUCCESS;
  36. switch_file_handle_t fh_output = { 0 };
  37. int channels = 1;
  38. int rate = 8000; /* TODO Make this configurable */
  39. int j = 0;
  40. if (switch_core_file_open(&fh_output, output, channels, rate, SWITCH_FILE_FLAG_WRITE | SWITCH_FILE_DATA_SHORT, NULL) != SWITCH_STATUS_SUCCESS) {
  41. switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Couldn't open %s\n", output);
  42. goto end;
  43. }
  44. for (j = 0; inputs[j] != NULL && j < 128 && status == SWITCH_STATUS_SUCCESS; j++) {
  45. switch_file_handle_t fh_input = { 0 };
  46. char buf[2048];
  47. switch_size_t len = sizeof(buf) / 2;
  48. if (switch_core_file_open(&fh_input, inputs[j], channels, rate, SWITCH_FILE_FLAG_READ | SWITCH_FILE_DATA_SHORT, NULL) != SWITCH_STATUS_SUCCESS) {
  49. switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Couldn't open %s\n", inputs[j]);
  50. status = SWITCH_STATUS_GENERR;
  51. break;
  52. }
  53. while (switch_core_file_read(&fh_input, buf, &len) == SWITCH_STATUS_SUCCESS) {
  54. if (switch_core_file_write(&fh_output, buf, &len) != SWITCH_STATUS_SUCCESS) {
  55. switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Write error\n");
  56. status = SWITCH_STATUS_GENERR;
  57. break;
  58. }
  59. }
  60. if (fh_input.file_interface) {
  61. switch_core_file_close(&fh_input);
  62. }
  63. }
  64. if (fh_output.file_interface) {
  65. switch_core_file_close(&fh_output);
  66. }
  67. end:
  68. return status;
  69. }
  70. switch_event_t *jsonapi2event(switch_core_session_t *session, switch_event_t *apply_event, const char *api, const char *data) {
  71. switch_event_t *phrases_event = NULL;
  72. switch_stream_handle_t stream = { 0 };
  73. SWITCH_STANDARD_STREAM(stream);
  74. switch_api_execute(api, data, session, &stream);
  75. switch_event_create_json(&phrases_event, (char *) stream.data);
  76. switch_safe_free(stream.data);
  77. if (apply_event) {
  78. switch_event_header_t *hp;
  79. for (hp = phrases_event->headers; hp; hp = hp->next) {
  80. if (!strncasecmp(hp->name, "VM-", 3)) {
  81. switch_event_add_header(apply_event, SWITCH_STACK_BOTTOM, hp->name, "%s", hp->value);
  82. }
  83. }
  84. switch_event_destroy(&phrases_event);
  85. phrases_event = apply_event;
  86. }
  87. return phrases_event;
  88. }
  89. char *generate_random_file_name(switch_core_session_t *session, const char *mod_name, char *file_extension) {
  90. char rand_uuid[SWITCH_UUID_FORMATTED_LENGTH + 1] = "";
  91. switch_uuid_t srand_uuid;
  92. switch_uuid_get(&srand_uuid);
  93. switch_uuid_format(rand_uuid, &srand_uuid);
  94. return switch_core_session_sprintf(session, "%s%s%s_%s.%s", SWITCH_GLOBAL_dirs.temp_dir, SWITCH_PATH_SEPARATOR, mod_name, rand_uuid, file_extension);
  95. }
  96. switch_status_t mt_api_execute(switch_core_session_t *session, const char *apiname, const char *arguments) {
  97. switch_status_t status = SWITCH_STATUS_SUCCESS;
  98. switch_stream_handle_t stream = { 0 };
  99. SWITCH_STANDARD_STREAM(stream);
  100. switch_api_execute(apiname, arguments, session, &stream);
  101. if (!strncasecmp(stream.data, "-ERR", 4)) {
  102. status = SWITCH_STATUS_GENERR;
  103. }
  104. switch_safe_free(stream.data);
  105. return status;
  106. }
  107. void append_event_profile(switch_event_t *phrase_params, vmivr_profile_t *profile, vmivr_menu_profile_t menu) {
  108. /* Used for some appending function */
  109. if (profile->name && profile->id && profile->domain) {
  110. switch_event_add_header(phrase_params, SWITCH_STACK_BOTTOM, "VM-Profile", "%s", profile->name);
  111. switch_event_add_header(phrase_params, SWITCH_STACK_BOTTOM, "VM-Account-ID", "%s", profile->id);
  112. switch_event_add_header(phrase_params, SWITCH_STACK_BOTTOM, "VM-Account-Domain", "%s", profile->domain);
  113. }
  114. }
  115. void populate_dtmfa_from_event(switch_event_t *phrase_params, vmivr_profile_t *profile, vmivr_menu_profile_t menu, char **dtmfa) {
  116. int i = 0;
  117. if (menu.event_keys_dtmf) {
  118. switch_event_header_t *hp;
  119. for (hp = menu.event_keys_dtmf->headers; hp; hp = hp->next) {
  120. if (strlen(hp->name) < 3 && hp->value) { /* TODO This is a hack to discard default FS Events ! */
  121. const char *varphrasename = switch_event_get_header(menu.event_keys_varname, hp->value);
  122. dtmfa[i++] = hp->name;
  123. if (varphrasename && !zstr(varphrasename)) {
  124. switch_event_add_header(phrase_params, SWITCH_STACK_BOTTOM, varphrasename, "%s", hp->name);
  125. }
  126. }
  127. }
  128. }
  129. }
  130. void append_event_message(switch_core_session_t *session, vmivr_profile_t *profile, switch_event_t *phrase_params, switch_event_t *msg_list_event, size_t current_msg) {
  131. char *varname;
  132. char *apicmd;
  133. varname = switch_mprintf("VM-List-Message-%" SWITCH_SIZE_T_FMT "-UUID", current_msg);
  134. apicmd = switch_mprintf("json %s %s %s %s", profile->api_profile, profile->domain, profile->id, switch_event_get_header(msg_list_event, varname));
  135. switch_safe_free(varname);
  136. jsonapi2event(session, phrase_params, profile->api_msg_get, apicmd);
  137. /* TODO Set these 2 header correctly */
  138. switch_event_add_header(phrase_params, SWITCH_STACK_BOTTOM, "VM-Message-Type", "%s", "new");
  139. switch_event_add_header(phrase_params, SWITCH_STACK_BOTTOM, "VM-Message-Number", "%"SWITCH_SIZE_T_FMT, current_msg);
  140. switch_event_add_header_string(phrase_params, SWITCH_STACK_BOTTOM, "VM-Message-Private-Local-Copy", "False");
  141. switch_safe_free(apicmd);
  142. }