diff --git a/conf/default_context.xml b/conf/default_context.xml index 2776a48997..7c4a4023df 100644 --- a/conf/default_context.xml +++ b/conf/default_context.xml @@ -23,14 +23,14 @@ - + - + diff --git a/src/include/switch_utils.h b/src/include/switch_utils.h index 21a5fbc82a..1f93b89085 100644 --- a/src/include/switch_utils.h +++ b/src/include/switch_utils.h @@ -328,6 +328,7 @@ SWITCH_DECLARE(switch_status_t) switch_string_match(const char *string, size_t s #define SWITCH_READ_ACCEPTABLE(status) (status == SWITCH_STATUS_SUCCESS || status == SWITCH_STATUS_BREAK) SWITCH_DECLARE(size_t) switch_url_encode(char *url, char *buf, size_t len); SWITCH_DECLARE(char *) switch_url_decode(char *s); +SWITCH_DECLARE(switch_bool_t) switch_simple_email(char *to, char *from, char *headers, char *body, char *file); /* malloc or DIE macros */ #ifdef NDEBUG diff --git a/src/mod/applications/mod_voicemail/mod_voicemail.c b/src/mod/applications/mod_voicemail/mod_voicemail.c index 6940d648ef..9b52c22c08 100644 --- a/src/mod/applications/mod_voicemail/mod_voicemail.c +++ b/src/mod/applications/mod_voicemail/mod_voicemail.c @@ -83,136 +83,6 @@ struct vm_profile { }; typedef struct vm_profile vm_profile_t; - -#define B64BUFFLEN 1024 -static const char c64[65] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; - -static int write_buf(int fd, char *buf) -{ - - int len = (int) strlen(buf); - if (fd && write(fd, buf, len) != len) { - close(fd); - return 0; - } - - return 1; -} -static switch_bool_t vm_email(char *to, char *from, char *headers, char *body, char *file) -{ - char *bound = "XXXX_boundary_XXXX"; - char filename[80], buf[B64BUFFLEN]; - int fd = 0, ifd = 0; - int x = 0, y = 0, bytes = 0, ilen = 0; - unsigned int b = 0, l = 0; - unsigned char in[B64BUFFLEN]; - unsigned char out[B64BUFFLEN + 512]; - char *path = NULL; - - snprintf(filename, 80, "%smail.%ld%04x", SWITCH_GLOBAL_dirs.temp_dir, time(NULL), rand() & 0xffff); - - if ((fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0644))) { - if (file) { - path = file; - if ((ifd = open(path, O_RDONLY)) < 1) { - return SWITCH_FALSE; - } - - snprintf(buf, B64BUFFLEN, "MIME-Version: 1.0\nContent-Type: multipart/mixed; boundary=\"%s\"\n", bound); - if (!write_buf(fd, buf)) { - return SWITCH_FALSE; - } - } - - if (headers && !write_buf(fd, headers)) - return SWITCH_FALSE; - - if (!write_buf(fd, "\n\n")) - return SWITCH_FALSE; - - if (file) { - snprintf(buf, B64BUFFLEN, "--%s\nContent-Type: text/plain\n\n", bound); - if (!write_buf(fd, buf)) - return SWITCH_FALSE; - } - - if (body) { - if (!write_buf(fd, body)) { - return SWITCH_FALSE; - } - } - - if (file) { - snprintf(buf, B64BUFFLEN, "\n\n--%s\nContent-Type: application/octet-stream\n" - "Content-Transfer-Encoding: base64\n" - "Content-Description: Sound attachment.\n" "Content-Disposition: attachment; filename=\"%s\"\n\n", bound, switch_cut_path(file)); - if (!write_buf(fd, buf)) - return SWITCH_FALSE; - - while ((ilen = read(ifd, in, B64BUFFLEN))) { - for (x = 0; x < ilen; x++) { - b = (b << 8) + in[x]; - l += 8; - while (l >= 6) { - out[bytes++] = c64[(b >> (l -= 6)) % 64]; - if (++y != 72) - continue; - out[bytes++] = '\n'; - y = 0; - } - } - if (write(fd, &out, bytes) != bytes) { - return -1; - } else - bytes = 0; - - } - - if (l > 0) { - out[bytes++] = c64[((b % 16) << (6 - l)) % 64]; - } - if (l != 0) - while (l < 6) { - out[bytes++] = '=', l += 2; - } - if (write(fd, &out, bytes) != bytes) { - return -1; - } - - } - - - - if (file) { - snprintf(buf, B64BUFFLEN, "\n\n--%s--\n.\n", bound); - if (!write_buf(fd, buf)) - return SWITCH_FALSE; - } - } - - if (fd) { - close(fd); - } - if (ifd) { - close(ifd); - } - snprintf(buf, B64BUFFLEN, "/bin/cat %s | /usr/sbin/sendmail -tf \"%s\" %s", filename, from, to); - if(system(buf)) { - switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Unable to execute command: %s\n", buf); - } - - unlink(filename); - - - if (file) { - switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Emailed file [%s] to [%s]\n", filename, to); - } else { - switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Emailed data to [%s]\n", to); - } - - return SWITCH_TRUE; -} - static switch_status_t vm_execute_sql(vm_profile_t *profile, char *sql, switch_mutex_t *mutex) { switch_core_db_t *db; @@ -953,7 +823,7 @@ static switch_status_t listen_file(switch_core_session_t *session, vm_profile_t } //TBD add better formatting to the body - vm_email(cbt->email, from, headers, body, cbt->file_path); + switch_simple_email(cbt->email, from, headers, body, cbt->file_path); switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Sending message to %s\n", cbt->email); switch_safe_free(body); TRY_CODE(switch_ivr_phrase_macro(session, VM_ACK_MACRO, "emailed", NULL, NULL)); @@ -1532,7 +1402,7 @@ static switch_status_t voicemail_leave_main(switch_core_session_t *session, char } //TBD add better formatting to the body - vm_email(email_vm, from, headers, body, file_path); + switch_simple_email(email_vm, from, headers, body, file_path); switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Sending message to %s\n", email_vm); switch_safe_free(body); unlink(file_path); diff --git a/src/mod/endpoints/mod_sofia/sofia.c b/src/mod/endpoints/mod_sofia/sofia.c index d72ea7b938..f48e288b2d 100644 --- a/src/mod/endpoints/mod_sofia/sofia.c +++ b/src/mod/endpoints/mod_sofia/sofia.c @@ -1924,6 +1924,8 @@ void sofia_handle_sip_i_invite(nua_t *nua, sofia_profile_t *profile, nua_handle_ from_host = sip->sip_from->a_url->url_host; channel_name = url_set_chanvars(session, sip->sip_from->a_url, sip_from); + switch_channel_set_variable(channel, "sip_mailbox", from_user); + if (!switch_strlen_zero(from_user)) { if (*from_user == '+') { switch_channel_set_variable(channel, "sip_from_user_stripped", (const char *) (from_user + 1)); diff --git a/src/mod/endpoints/mod_sofia/sofia_reg.c b/src/mod/endpoints/mod_sofia/sofia_reg.c index 6673443560..037ff3c7b6 100644 --- a/src/mod/endpoints/mod_sofia/sofia_reg.c +++ b/src/mod/endpoints/mod_sofia/sofia_reg.c @@ -747,6 +747,7 @@ auth_res_t sofia_reg_parse_auth(sofia_profile_t *profile, sip_authorization_t co const char *passwd = NULL; const char *a1_hash = NULL; char *sql; + char *mailbox = NULL; switch_xml_t domain, xml = NULL, user, param, xparams; char hexdigest[2 * SU_MD5_DIGEST_SIZE + 1] = ""; char *pbuf = NULL; @@ -829,7 +830,11 @@ auth_res_t sofia_reg_parse_auth(sofia_profile_t *profile, sip_authorization_t co ret = AUTH_FORBIDDEN; goto end; } - + + if (!(mailbox = (char *)switch_xml_attr(user, "mailbox"))) { + mailbox = username; + } + for (param = switch_xml_child(xparams, "param"); param; param = param->next) { const char *var = switch_xml_attr_soft(param, "name"); const char *val = switch_xml_attr_soft(param, "value"); @@ -899,6 +904,7 @@ auth_res_t sofia_reg_parse_auth(sofia_profile_t *profile, sip_authorization_t co if (first && ret == AUTH_OK) { if (v_event && (xparams = switch_xml_child(user, "variables"))) { if (switch_event_create(v_event, SWITCH_EVENT_MESSAGE) == SWITCH_STATUS_SUCCESS) { + switch_event_add_header(*v_event, SWITCH_STACK_BOTTOM, "sip_mailbox", "%s", mailbox); for (param = switch_xml_child(xparams, "variable"); param; param = param->next) { const char *var = switch_xml_attr_soft(param, "name"); const char *val = switch_xml_attr_soft(param, "value"); diff --git a/src/switch_utils.c b/src/switch_utils.c index 0287eede17..ce2683c3f0 100644 --- a/src/switch_utils.c +++ b/src/switch_utils.c @@ -68,6 +68,135 @@ SWITCH_DECLARE(switch_status_t) switch_b64_encode(unsigned char *in, switch_size } + +static int write_buf(int fd, char *buf) +{ + + int len = (int) strlen(buf); + if (fd && write(fd, buf, len) != len) { + close(fd); + return 0; + } + + return 1; +} + +SWITCH_DECLARE(switch_bool_t) switch_simple_email(char *to, char *from, char *headers, char *body, char *file) +{ + char *bound = "XXXX_boundary_XXXX"; + char filename[80], buf[B64BUFFLEN]; + int fd = 0, ifd = 0; + int x = 0, y = 0, bytes = 0, ilen = 0; + unsigned int b = 0, l = 0; + unsigned char in[B64BUFFLEN]; + unsigned char out[B64BUFFLEN + 512]; + char *path = NULL; + + snprintf(filename, 80, "%smail.%ld%04x", SWITCH_GLOBAL_dirs.temp_dir, time(NULL), rand() & 0xffff); + + if ((fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0644))) { + if (file) { + path = file; + if ((ifd = open(path, O_RDONLY)) < 1) { + return SWITCH_FALSE; + } + + snprintf(buf, B64BUFFLEN, "MIME-Version: 1.0\nContent-Type: multipart/mixed; boundary=\"%s\"\n", bound); + if (!write_buf(fd, buf)) { + return SWITCH_FALSE; + } + } + + if (headers && !write_buf(fd, headers)) + return SWITCH_FALSE; + + if (!write_buf(fd, "\n\n")) + return SWITCH_FALSE; + + if (file) { + snprintf(buf, B64BUFFLEN, "--%s\nContent-Type: text/plain\n\n", bound); + if (!write_buf(fd, buf)) + return SWITCH_FALSE; + } + + if (body) { + if (!write_buf(fd, body)) { + return SWITCH_FALSE; + } + } + + if (file) { + snprintf(buf, B64BUFFLEN, "\n\n--%s\nContent-Type: application/octet-stream\n" + "Content-Transfer-Encoding: base64\n" + "Content-Description: Sound attachment.\n" "Content-Disposition: attachment; filename=\"%s\"\n\n", bound, switch_cut_path(file)); + if (!write_buf(fd, buf)) + return SWITCH_FALSE; + + while ((ilen = read(ifd, in, B64BUFFLEN))) { + for (x = 0; x < ilen; x++) { + b = (b << 8) + in[x]; + l += 8; + while (l >= 6) { + out[bytes++] = switch_b64_table[(b >> (l -= 6)) % 64]; + if (++y != 72) + continue; + out[bytes++] = '\n'; + y = 0; + } + } + if (write(fd, &out, bytes) != bytes) { + return -1; + } else + bytes = 0; + + } + + if (l > 0) { + out[bytes++] = switch_b64_table[((b % 16) << (6 - l)) % 64]; + } + if (l != 0) + while (l < 6) { + out[bytes++] = '=', l += 2; + } + if (write(fd, &out, bytes) != bytes) { + return -1; + } + + } + + + + if (file) { + snprintf(buf, B64BUFFLEN, "\n\n--%s--\n.\n", bound); + if (!write_buf(fd, buf)) + return SWITCH_FALSE; + } + } + + if (fd) { + close(fd); + } + if (ifd) { + close(ifd); + } + snprintf(buf, B64BUFFLEN, "/bin/cat %s | /usr/sbin/sendmail -tf \"%s\" %s", filename, from, to); + if(system(buf)) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Unable to execute command: %s\n", buf); + } + + unlink(filename); + + + if (file) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Emailed file [%s] to [%s]\n", filename, to); + } else { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Emailed data to [%s]\n", to); + } + + return SWITCH_TRUE; +} + + SWITCH_DECLARE(switch_status_t) switch_find_local_ip(char *buf, int len, int family) { switch_status_t status = SWITCH_STATUS_FALSE;