From 5825f68e8bc892b5e9699d1e491528c8e900355e Mon Sep 17 00:00:00 2001
From: Joshua Colp <jcolp@digium.com>
Date: Tue, 27 Oct 2009 13:30:27 +0000
Subject: [PATCH] Add support for receiving unsolicited MWI NOTIFY messages.

This change adds a configuration option to SIP peers, unsolicited_mailbox, which
configures a virtual mailbox to use for received new/old MWI information. This
virtual mailbox can then be used by any device supporting MWI.

(closes issue #13028)
Reported by: AsteriskRocks
Patches:
      bug_13028_chan_sip_external_mwi_20090707.patch uploaded by cmaj (license 830)


git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@226060 65c4cc65-6c06-0410-ace0-fbb531ad65f3
---
 CHANGES                 |  3 +++
 channels/chan_sip.c     | 31 ++++++++++++++++++++++---------
 configs/sip.conf.sample |  5 +++++
 3 files changed, 30 insertions(+), 9 deletions(-)

diff --git a/CHANGES b/CHANGES
index 18bfc1e8b0..20694ba052 100644
--- a/CHANGES
+++ b/CHANGES
@@ -50,6 +50,9 @@ SIP Changes
    accessible via AMI and CLI.
  * Added 'media_address' configuration option which can be used to explicitly specify
    the IP address to use in the SDP for media (audio, video, and text) streams.
+ * Added 'unsolicited_mailbox' configuration option which specifies the virtual mailbox
+   that the new/old count should be stored on if an unsolicited MWI NOTIFY message is
+   received.
 
 IAX2 Changes
 -----------
diff --git a/channels/chan_sip.c b/channels/chan_sip.c
index a8daf87bd7..2a10ac639a 100644
--- a/channels/chan_sip.c
+++ b/channels/chan_sip.c
@@ -1995,6 +1995,7 @@ struct sip_peer {
 		AST_STRING_FIELD(useragent);		/*!<  User agent in SIP request (saved from registration) */
 		AST_STRING_FIELD(mwi_from);         /*!< Name to place in From header for outgoing NOTIFY requests */
 		AST_STRING_FIELD(engine);               /*!<  RTP Engine to use */
+		AST_STRING_FIELD(unsolicited_mailbox);  /*!< Mailbox to store received unsolicited MWI NOTIFY messages information in */
 		);
 	struct sip_socket socket;	/*!< Socket used for this peer */
 	enum sip_transport default_outbound_transport;    /*!< Peer Registration may change the default outbound transport.
@@ -7628,10 +7629,6 @@ static struct sip_pvt *find_call(struct sip_request *req, struct sockaddr_in *si
 		if (intended_method == SIP_REFER) {
 			/* We do support REFER, but not outside of a dialog yet */
 			transmit_response_using_temp(callid, sin, 1, intended_method, req, "603 Declined (no dialog)");
-		} else if (intended_method == SIP_NOTIFY) {
-			/* We do not support out-of-dialog NOTIFY either,
-		   	like voicemail notification, so cancel that early */
-			transmit_response_using_temp(callid, sin, 1, intended_method, req, "489 Bad event");
 		} else {
 			/* Ok, time to create a new SIP dialog object, a pvt */
 			if ((p = sip_alloc(callid, sin, 1, intended_method, req)))  {
@@ -19829,25 +19826,39 @@ static int handle_request_notify(struct sip_pvt *p, struct sip_request *req, str
 		}
 		/* Confirm that we received this packet */
 		transmit_response(p, "200 OK", req);
-	} else if (p->mwi && !strcmp(event, "message-summary")) {
+	} else if (!strcmp(event, "message-summary")) {
+		const char *mailbox = NULL;
 		char *c = ast_strdupa(get_body(req, "Voice-Message", ':'));
 
-		if (!ast_strlen_zero(c)) {
+		if (!p->mwi) {
+			struct sip_peer *peer = find_peer(NULL, &p->recv, TRUE, FINDPEERS, FALSE, p->socket.type);
+
+			if (peer) {
+				mailbox = ast_strdupa(peer->unsolicited_mailbox);
+				unref_peer(peer, "removing unsolicited mwi ref");
+			}
+		} else {
+			mailbox = p->mwi->mailbox;
+		}
+
+		if (!ast_strlen_zero(mailbox) && !ast_strlen_zero(c)) {
 			char *old = strsep(&c, " ");
 			char *new = strsep(&old, "/");
 			struct ast_event *event;
 
 			if ((event = ast_event_new(AST_EVENT_MWI,
-						   AST_EVENT_IE_MAILBOX, AST_EVENT_IE_PLTYPE_STR, p->mwi->mailbox,
+						   AST_EVENT_IE_MAILBOX, AST_EVENT_IE_PLTYPE_STR, mailbox,
 						   AST_EVENT_IE_CONTEXT, AST_EVENT_IE_PLTYPE_STR, "SIP_Remote",
 						   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))) {
 				ast_event_queue_and_cache(event);
 			}
+			transmit_response(p, "200 OK", req);
+		} else {
+			transmit_response(p, "489 Bad event", req);
+			res = -1;
 		}
-
-		transmit_response(p, "200 OK", req);
 	} else if (!strcmp(event, "keep-alive")) {
 		 /* Used by Sipura/Linksys for NAT pinhole,
 		  * just confirm that we recieved the packet. */
@@ -24897,6 +24908,8 @@ static struct sip_peer *build_peer(const char *name, struct ast_variable *v, str
 			} else if (!strcasecmp(v->name, "disallowed_methods")) {
 				char *disallow = ast_strdupa(v->value);
 				mark_parsed_methods(&peer->disallowed_methods, disallow);
+			} else if (!strcasecmp(v->name, "unsolicited_mailbox")) {
+				ast_string_field_set(peer, unsolicited_mailbox, v->value);
 			}
 		}
 
diff --git a/configs/sip.conf.sample b/configs/sip.conf.sample
index 857886c735..27cf368188 100644
--- a/configs/sip.conf.sample
+++ b/configs/sip.conf.sample
@@ -975,6 +975,7 @@ srvlookup=yes                   ; Enable DNS SRV lookups on outbound calls
 ; contactdeny           ; is to register at the same IP as a SIP provider,
 ;                       ; then call oneself, and get redirected to that
 ;                       ; same location).
+; unsolicited_mailbox
 
 ;[sip_proxy]
 ; For incoming calls only. Example: FWD (Free World Dialup)
@@ -1015,6 +1016,10 @@ srvlookup=yes                   ; Enable DNS SRV lookups on outbound calls
 ;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 <configured value>@SIP_Remote as the
+;                                 ;   mailbox.
 
 ;
 ; Because you might have a large number of similar sections, it is generally
-- 
GitLab