diff --git a/src/mod/applications/mod_voicemail/mod_voicemail.c b/src/mod/applications/mod_voicemail/mod_voicemail.c index b023979095..9fc68c657e 100644 --- a/src/mod/applications/mod_voicemail/mod_voicemail.c +++ b/src/mod/applications/mod_voicemail/mod_voicemail.c @@ -5620,6 +5620,158 @@ done: return SWITCH_STATUS_SUCCESS; } +#define VM_FSDB_MSG_EMAIL_USAGE " " +SWITCH_STANDARD_API(vm_fsdb_msg_email_function) +{ + const char *id = NULL, *domain = NULL, *profile_name = NULL, *uuid = NULL, *email = NULL; + vm_profile_t *profile = NULL; + char *argv[7] = { 0 }; + char *mycmd = NULL; + msg_get_callback_t cbt = { 0 }; + char *sql; + switch_memory_pool_t *pool; + + switch_core_new_memory_pool(&pool); + + if (!zstr(cmd)) { + mycmd = switch_core_strdup(pool, cmd); + switch_separate_string(mycmd, ' ', argv, (sizeof(argv) / sizeof(argv[0]))); + } + + if (argv[0]) + profile_name = argv[0]; + if (argv[1]) + domain = argv[1]; + if (argv[2]) + id = argv[2]; + if (argv[3]) + uuid = argv[3]; + if (argv[4]) + email = argv[4]; + + if (!profile_name || !domain || !id || !uuid || !email) { + stream->write_function(stream, "-ERR Missing Arguments\n"); + goto done; + } + + if (!(profile = get_profile(profile_name))) { + stream->write_function(stream, "-ERR Profile not found\n"); + goto done; + } else { + char *from; + char *headers, *header_string; + char *body; + int priority = 3; + switch_size_t retsize; + switch_time_exp_t tm; + char date[80] = ""; + int total_new_messages = 0; + int total_saved_messages = 0; + int total_new_urgent_messages = 0; + int total_saved_urgent_messages = 0; + int32_t message_len = 0; + char *p; + switch_time_t l_duration = 0; + switch_core_time_duration_t duration; + char duration_str[80]; + char *formatted_cid_num = NULL; + + sql = switch_mprintf("SELECT * FROM voicemail_msgs WHERE username = '%q' AND domain = '%q' AND uuid = '%q' ORDER BY read_flags, created_epoch", id, domain, uuid); + memset(&cbt, 0, sizeof(cbt)); + switch_event_create(&cbt.my_params, SWITCH_EVENT_GENERAL); + vm_execute_sql_callback(profile, profile->mutex, sql, message_get_callback, &cbt); + switch_safe_free(sql); + + if (!strcasecmp(switch_event_get_header(cbt.my_params, "VM-Message-Read-Flags"), URGENT_FLAG_STRING)) { + priority = 1; + } + + message_count(profile, id, domain, switch_event_get_header(cbt.my_params, "VM-Message-Folder"), &total_new_messages, &total_saved_messages, + &total_new_urgent_messages, &total_saved_urgent_messages); + + switch_time_exp_lt(&tm, switch_time_make(atol(switch_event_get_header(cbt.my_params, "VM-Message-Received-Epoch")), 0)); + switch_strftime(date, &retsize, sizeof(date), profile->date_fmt, &tm); + + formatted_cid_num = switch_format_number(switch_event_get_header(cbt.my_params, "VM-Message-Caller-Number")); + + /* Legacy Mod_VoiceMail variable */ + switch_event_add_header_string(cbt.my_params, SWITCH_STACK_BOTTOM, "Message-Type", "forwarded-voicemail"); + switch_event_add_header(cbt.my_params, SWITCH_STACK_BOTTOM, "voicemail_total_new_messages", "%d", total_new_messages); + switch_event_add_header(cbt.my_params, SWITCH_STACK_BOTTOM, "voicemail_total_saved_messages", "%d", total_saved_messages); + switch_event_add_header(cbt.my_params, SWITCH_STACK_BOTTOM, "voicemail_urgent_new_messages", "%d", total_new_urgent_messages); + switch_event_add_header(cbt.my_params, SWITCH_STACK_BOTTOM, "voicemail_urgent_saved_messages", "%d", total_saved_urgent_messages); + switch_event_add_header_string(cbt.my_params, SWITCH_STACK_BOTTOM, "voicemail_current_folder", switch_event_get_header(cbt.my_params, "VM-Message-Folder")); + switch_event_add_header_string(cbt.my_params, SWITCH_STACK_BOTTOM, "voicemail_account", id); + switch_event_add_header_string(cbt.my_params, SWITCH_STACK_BOTTOM, "voicemail_domain", domain); + switch_event_add_header_string(cbt.my_params, SWITCH_STACK_BOTTOM, "voicemail_caller_id_number", switch_event_get_header(cbt.my_params, "VM-Message-Caller-Number")); + switch_event_add_header_string(cbt.my_params, SWITCH_STACK_BOTTOM, "voicemail_formatted_caller_id_number", formatted_cid_num); + switch_event_add_header_string(cbt.my_params, SWITCH_STACK_BOTTOM, "voicemail_caller_id_name", switch_event_get_header(cbt.my_params, "VM-Message-Caller-Name")); + switch_event_add_header_string(cbt.my_params, SWITCH_STACK_BOTTOM, "voicemail_file_path", switch_event_get_header(cbt.my_params, "VM-Message-File-Path")); + switch_event_add_header_string(cbt.my_params, SWITCH_STACK_BOTTOM, "voicemail_read_flags", switch_event_get_header(cbt.my_params, "VM-Message-Read-Flags")); + switch_event_add_header_string(cbt.my_params, SWITCH_STACK_BOTTOM, "voicemail_time", date); + switch_event_add_header(cbt.my_params, SWITCH_STACK_BOTTOM, "voicemail_priority", "%d", priority); + + + message_len = atoi(switch_event_get_header(cbt.my_params, "VM-Message-Duration")); + switch_safe_free(formatted_cid_num); + + l_duration = switch_time_make(atol(switch_event_get_header(cbt.my_params, "VM-Message-Duration")), 0); + switch_core_measure_time(l_duration, &duration); + duration.day += duration.yr * 365; + duration.hr += duration.day * 24; + + switch_snprintf(duration_str, sizeof(duration_str), "%.2u:%.2u:%.2u", duration.hr, duration.min, duration.sec); + + switch_event_add_header_string(cbt.my_params, SWITCH_STACK_BOTTOM, "voicemail_message_len", duration_str); + switch_event_add_header_string(cbt.my_params, SWITCH_STACK_BOTTOM, "voicemail_email", email); + + if (zstr(profile->email_from)) { + from = switch_core_sprintf(pool, "%s@%s", id, domain); + } else { + from = switch_event_expand_headers(cbt.my_params, profile->email_from);; + } + + if (zstr(profile->email_headers)) { + headers = switch_core_sprintf(pool, + "From: FreeSWITCH mod_voicemail <%s@%s>\nSubject: Voicemail from %s %s\nX-Priority: %d", + id, domain, switch_event_get_header(cbt.my_params, "VM-Message-Caller-Name"), + switch_event_get_header(cbt.my_params, "VM-Message-Caller-Number"), priority); + } else { + headers = switch_event_expand_headers(cbt.my_params, profile->email_headers); + } + + p = headers + (strlen(headers) - 1); + if (*p == '\n') { + if (*(p - 1) == '\r') { + p--; + } + *p = '\0'; + } + + header_string = switch_core_sprintf(pool, "%s\nX-Voicemail-Length: %u", headers, message_len); + + if (profile->email_body) { + body = switch_event_expand_headers(cbt.my_params, profile->email_body); + } else { + body = switch_mprintf("%u second Voicemail from %s %s", message_len, switch_event_get_header(cbt.my_params, "VM-Message-Caller-Name"), switch_event_get_header(cbt.my_params, "VM-Message-Caller-Number")); + } + + switch_simple_email(email, from, header_string, body, switch_event_get_header(cbt.my_params, "VM-Message-File-Path"), profile->convert_cmd, profile->convert_ext); + switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "Sending message to %s\n", email); + switch_safe_free(body); + + switch_event_fire(&cbt.my_params); + + + profile_rwunlock(profile); + } + stream->write_function(stream, "-OK\n"); +done: + switch_core_destroy_memory_pool(&pool); + + return SWITCH_STATUS_SUCCESS; +} + #define VM_FSDB_MSG_COUNT_USAGE " " SWITCH_STANDARD_API(vm_fsdb_msg_count_function) { @@ -5746,6 +5898,7 @@ SWITCH_MODULE_LOAD_FUNCTION(mod_voicemail_load) SWITCH_ADD_API(commands_api_interface, "vm_fsdb_msg_get", "vm_fsdb_msg_get", vm_fsdb_msg_get_function, VM_FSDB_MSG_GET_USAGE); SWITCH_ADD_API(commands_api_interface, "vm_fsdb_msg_delete", "vm_fsdb_msg_delete", vm_fsdb_msg_delete_function, VM_FSDB_MSG_DELETE_USAGE); SWITCH_ADD_API(commands_api_interface, "vm_fsdb_msg_undelete", "vm_fsdb_msg_undelete", vm_fsdb_msg_undelete_function, VM_FSDB_MSG_UNDELETE_USAGE); + SWITCH_ADD_API(commands_api_interface, "vm_fsdb_msg_email", "vm_fsdb_msg_email", vm_fsdb_msg_email_function, VM_FSDB_MSG_EMAIL_USAGE); SWITCH_ADD_API(commands_api_interface, "vm_fsdb_msg_purge", "vm_fsdb_msg_purge", vm_fsdb_msg_purge_function, VM_FSDB_MSG_PURGE_USAGE); SWITCH_ADD_API(commands_api_interface, "vm_fsdb_msg_save", "vm_fsdb_msg_save", vm_fsdb_msg_save_function, VM_FSDB_MSG_SAVE_USAGE); SWITCH_ADD_API(commands_api_interface, "vm_fsdb_msg_forward", "vm_fsdb_msg_forward", vm_fsdb_msg_forward_function, VM_FSDB_MSG_FORWARD_USAGE);