--- asterisk-1.4.23.2/channels/chan_sip.c.original	2009-03-10 16:53:39.000000000 +0100
+++ asterisk-1.4.23.2/channels/chan_sip.c	2009-03-12 11:03:33.000000000 +0100
@@ -509,6 +509,7 @@
 #define DEFAULT_REALM		"asterisk"
 #define DEFAULT_NOTIFYRINGING	TRUE
 #define DEFAULT_PEDANTIC	FALSE
+#define DEFAULT_URLENCODE	FALSE
 #define DEFAULT_AUTOCREATEPEER	FALSE
 #define DEFAULT_QUALIFY		FALSE
 #define DEFAULT_T1MIN		100		/*!< 100 MS for minimal roundtrip time */
@@ -543,6 +544,7 @@
 static int global_alwaysauthreject;	/*!< Send 401 Unauthorized for all failing requests */
 static int srvlookup;			/*!< SRV Lookup on or off. Default is on */
 static int pedanticsipchecking;		/*!< Extra checking ?  Default off */
+static int urlencodesip;		/*!< URL encode ?  Default off */
 static int autocreatepeer;		/*!< Auto creation of peers at registration? Default off. */
 static int global_relaxdtmf;			/*!< Relax DTMF */
 static int global_rtptimeout;		/*!< Time out call if no RTP */
@@ -7113,7 +7115,7 @@
 	else /* Save for any further attempts */
 		ast_string_field_set(p, fromname, n);
 
-	if (pedanticsipchecking) {
+	if ( (pedanticsipchecking) || (urlencodesip) ) {
 		ast_uri_encode(n, tmp, sizeof(tmp), 0);
 		n = tmp;
 		ast_uri_encode(l, tmp2, sizeof(tmp2), 0);
@@ -7134,7 +7136,7 @@
 		ast_build_string(&invite, &invite_max, "sip:");
 		if (!ast_strlen_zero(p->username)) {
 			n = p->username;
-			if (pedanticsipchecking) {
+			if ( (pedanticsipchecking) || (urlencodesip) ) {
 				ast_uri_encode(n, tmp, sizeof(tmp), 0);
 				n = tmp;
 			}
@@ -8802,7 +8804,7 @@
 	*t = '\0';
 	
 	ast_copy_string(tmp, get_header(req, "To"), sizeof(tmp));
-	if (pedanticsipchecking)
+	if ( (pedanticsipchecking) || (urlencodesip) )
 		ast_uri_decode(tmp);
 
 	c = get_in_brackets(tmp);
@@ -8996,7 +8998,7 @@
 	if (req->rlPart2)
 		ast_copy_string(tmp, req->rlPart2, sizeof(tmp));
 	
-	if (pedanticsipchecking)
+	if ( (pedanticsipchecking) || (urlencodesip) )
 		ast_uri_decode(tmp);
 
 	uri = get_in_brackets(tmp);
@@ -9010,7 +9012,7 @@
 	/* Now find the From: caller ID and name */
 	ast_copy_string(tmpf, get_header(req, "From"), sizeof(tmpf));
 	if (!ast_strlen_zero(tmpf)) {
-		if (pedanticsipchecking)
+		if ( (pedanticsipchecking) || (urlencodesip) )
 			ast_uri_decode(tmpf);
 		from = get_in_brackets(tmpf);
 	} else {
@@ -9195,7 +9197,7 @@
 	}
 	h_refer_to = ast_strdupa(p_refer_to);
 	refer_to = get_in_brackets(h_refer_to);
-	if (pedanticsipchecking)
+	if ( (pedanticsipchecking) || (urlencodesip) )
 		ast_uri_decode(refer_to);
 
 	if (strncasecmp(refer_to, "sip:", 4)) {
@@ -9209,7 +9211,7 @@
 	if (!ast_strlen_zero(p_referred_by)) {
 		char *lessthan;
 		h_referred_by = ast_strdupa(p_referred_by);
-		if (pedanticsipchecking)
+		if ( (pedanticsipchecking) || (urlencodesip) )
 			ast_uri_decode(h_referred_by);
 
 		/* Store referrer's caller ID name */
@@ -9348,7 +9350,7 @@
 	ast_copy_string(tmp, get_header(req, "Also"), sizeof(tmp));
 	c = get_in_brackets(tmp);
 
-	if (pedanticsipchecking)
+	if ( (pedanticsipchecking) || (urlencodesip) )
 		ast_uri_decode(c);
 	
 	if (strncasecmp(c, "sip:", 4)) {
@@ -9548,7 +9550,7 @@
 		t++;
 	*t = '\0';
 	ast_copy_string(from, get_header(req, "From"), sizeof(from));	/* XXX bug in original code, overwrote string */
-	if (pedanticsipchecking)
+	if ( (pedanticsipchecking) || (urlencodesip) )
 		ast_uri_decode(from);
 	/* XXX here tries to map the username for invite things */
 	memset(calleridname, 0, sizeof(calleridname));
@@ -10917,6 +10919,7 @@
 	ast_cli(fd, "  MWI NOTIFY mime type:   %s\n", default_notifymime);
 	ast_cli(fd, "  DNS SRV lookup:         %s\n", srvlookup ? "Yes" : "No");
 	ast_cli(fd, "  Pedantic SIP support:   %s\n", pedanticsipchecking ? "Yes" : "No");
+	ast_cli(fd, "  URL encode SIP:         %s\n", urlencodesip ? "Yes" : "No");
 	ast_cli(fd, "  Reg. min duration       %d secs\n", min_expiry);
 	ast_cli(fd, "  Reg. max duration:      %d secs\n", max_expiry);
 	ast_cli(fd, "  Reg. default duration:  %d secs\n", default_expiry);
@@ -17637,6 +17640,7 @@
 	global_reg_timeout = DEFAULT_REGISTRATION_TIMEOUT;
 	global_regattempts_max = 0;
 	pedanticsipchecking = DEFAULT_PEDANTIC;
+	urlencodesip = DEFAULT_URLENCODE;
 	global_mwitime = DEFAULT_MWITIME;
 	autocreatepeer = DEFAULT_AUTOCREATEPEER;
 	global_autoframing = 0;
@@ -17800,6 +17804,8 @@
 			srvlookup = ast_true(v->value);
 		} else if (!strcasecmp(v->name, "pedantic")) {
 			pedanticsipchecking = ast_true(v->value);
+		} else if (!strcasecmp(v->name, "urlencode")) {
+			urlencodesip = ast_true(v->value);
 		} else if (!strcasecmp(v->name, "maxexpirey") || !strcasecmp(v->name, "maxexpiry")) {
 			max_expiry = atoi(v->value);
 			if (max_expiry < 1)
