diff -urN certified-asterisk-11.2-cert2/channels/chan_sip.c certified-asterisk-11.2-cert2-patched/channels/chan_sip.c --- certified-asterisk-11.2-cert2/channels/chan_sip.c 2013-08-27 15:22:36.000000000 -0400 +++ certified-asterisk-11.2-cert2-patched/channels/chan_sip.c 2013-10-13 20:12:13.347928334 -0400 @@ -24373,14 +24373,88 @@ } else if (!strcmp(event, "message-summary")) { const char *mailbox = NULL; char *c = ast_strdupa(get_content_line(req, "Voice-Message", ':')); - + char *context = "SIP_Remote"; + if (!p->mwi) { + ast_debug(4, "Unsolicited MWI received from: %s\n", ast_sockaddr_stringify(&p->recv)); + /* If you intend to light unsolicited MWI lights on your phones, + they must be in the same context as the unsolicited_mailbox parameter in the format of: + default_box@context */ + struct sip_peer *peer = sip_find_peer(NULL, &p->recv, TRUE, FINDPEERS, FALSE, p->socket.type); if (peer) { - mailbox = ast_strdupa(peer->unsolicited_mailbox); + char *unsolicited_mailbox = ast_strdupa(peer->unsolicited_mailbox); + if(!unsolicited_mailbox || ast_strlen_zero(unsolicited_mailbox)) { + ast_debug(2, "unsolicited_mailbox was not set for peer %s, unsolicited MWI will not be delivered.\n", ast_sockaddr_stringify(&p->recv)); + } + else { + { + char* mailbox_only = strchr(unsolicited_mailbox, '@'); + if (mailbox_only) { + *mailbox_only = '\0'; + } + } + + /* If the unsolicited_mailbox parameter has an @ symbol, use that for the context, otherwise leave it at the default of SIP_Remote. */ + char *unsolicited_context = ast_strdupa(peer->unsolicited_mailbox); + if(unsolicited_context) { + unsolicited_context = strchr(unsolicited_context, '@'); + if(unsolicited_context) { + unsolicited_context++; + } + if(unsolicited_context && !ast_strlen_zero(unsolicited_context)) { + context = unsolicited_context; + } + } + /* If the user has specfied a mailbox to send *all* unsolicited messages to, send them directly there. */ + if(unsolicited_mailbox && !ast_strlen_zero(unsolicited_mailbox)) { + mailbox = unsolicited_mailbox; + } + else + { + /* RFC3842 (For the Message-Account header) states: + If the Request-URI or To header in a message-summary subscription + corresponds to a group or collection of individual messaging + accounts, the notifier MUST specify to which account the message- + summary body corresponds. + + Therefore, use Message-Account from the body first, and then failback to the To header. + */ + char *specified_extension = ast_strdupa(get_content_line(req, "Message-Account", ':')); + if(!specified_extension || ast_strlen_zero(specified_extension)) { + specified_extension = ast_strdupa(sip_get_header(req, "To")); + } + + if(specified_extension && !ast_strlen_zero(specified_extension)) { + /* Get the mailbox from the format: */ + char *specified_mailbox = strchr(specified_extension, '@'); + if (specified_mailbox) { + *specified_mailbox = '\0'; + } + specified_mailbox = strchr(specified_extension, ':'); + if (specified_mailbox) { + specified_mailbox++; + + if(specified_mailbox && !ast_strlen_zero(specified_mailbox)) { + mailbox = specified_mailbox; + } + } + } + } + + if(mailbox && !ast_strlen_zero(mailbox)) { + ast_debug(4, "Unsolicited MWI being delivered to: %s@%s\n", mailbox, context); + } else { + ast_debug(2, "Could not find a valid mailbox. Unsolicited MWI will not be delivered."); + } + } + sip_unref_peer(peer, "removing unsolicited mwi ref"); } + else { + ast_debug(2, "Unsolicited MWI received from an unknown peer and wont be routed: %s\n", ast_sockaddr_stringify(&p->recv)); + } } else { mailbox = p->mwi->mailbox; } @@ -24392,7 +24466,7 @@ if ((event = ast_event_new(AST_EVENT_MWI, AST_EVENT_IE_MAILBOX, AST_EVENT_IE_PLTYPE_STR, mailbox, - AST_EVENT_IE_CONTEXT, AST_EVENT_IE_PLTYPE_STR, "SIP_Remote", + AST_EVENT_IE_CONTEXT, AST_EVENT_IE_PLTYPE_STR, context, AST_EVENT_IE_NEWMSGS, AST_EVENT_IE_PLTYPE_UINT, atoi(new), AST_EVENT_IE_OLDMSGS, AST_EVENT_IE_PLTYPE_UINT, atoi(old), AST_EVENT_IE_END))) { diff -urN certified-asterisk-11.2-cert2/configs/sip.conf.sample certified-asterisk-11.2-cert2-patched/configs/sip.conf.sample --- certified-asterisk-11.2-cert2/configs/sip.conf.sample 2012-11-11 12:08:58.000000000 -0500 +++ certified-asterisk-11.2-cert2-patched/configs/sip.conf.sample 2013-10-13 20:06:13.287057184 -0400 @@ -1322,10 +1322,15 @@ ;transport=udp,tcp ; This sets the transport type to udp for outgoing, and will ; ; accept both tcp and udp. Default is udp. The first transport ; ; listed will always be used for outgoing connections. -;unsolicited_mailbox=4015552299 ; If the remote SIP server sends an unsolicited MWI NOTIFY message the new/old -; ; message count will be stored in the configured virtual mailbox. It can be used -; ; by any device supporting MWI by specifying @SIP_Remote as the -; ; mailbox. +;unsolicited_mailbox ; This specifies mailbox and or context to deliver unsolicited +; ; sip notify / MWI messages to in the format of box@context. +; ; If a box is specified, all unsolicited notifies will be delivered +; ; to that box. +; ; If a context is specified, unsolicited messages will be delivered +; ; in that context. (Otherwise SIP_Remote is the default context). +; ; If no box is specified (IE: unsolicied_mailbox=@default) +; ; unsolicited messages will be delivered to the box specified in +; ; the SIP notify at the context specified. ; ; Because you might have a large number of similar sections, it is generally