--- asterisk-1.2.4.orig/channels/chan_sip.c	2006-01-28 08:52:15.000000000 -0500
+++ asterisk-1.2.4/channels/chan_sip.c	2006-03-03 09:39:40.971781264 -0500
@@ -586,6 +586,7 @@
 	ast_mutex_t lock;			/*!< Channel private lock */
 	int method;				/*!< SIP method of this packet */
 	char callid[80];			/*!< Global CallID */
+	char calledid[256];			/*!< Called Party RPID header SIP tag */
 	char randdata[80];			/*!< Random data */
 	struct ast_codec_pref prefs;		/*!< codec prefs */
 	unsigned int ocseq;			/*!< Current outgoing seqno */
@@ -878,6 +879,7 @@
 
 
 static int transmit_response(struct sip_pvt *p, char *msg, struct sip_request *req);
+static int transmit_response_with_calledrpid(struct sip_pvt *p, char *msg, struct sip_request *req);
 static int transmit_response_with_sdp(struct sip_pvt *p, char *msg, struct sip_request *req, int retrans);
 static int transmit_response_with_unsupported(struct sip_pvt *p, char *msg, struct sip_request *req, char *unsupported);
 static int transmit_response_with_auth(struct sip_pvt *p, char *msg, struct sip_request *req, char *rand, int reliable, char *header, int stale);
@@ -2639,7 +2641,11 @@
 			if (!ast_test_flag(p, SIP_PROGRESS_SENT) ||
 			    (ast_test_flag(p, SIP_PROG_INBAND) == SIP_PROG_INBAND_NEVER)) {
 				/* Send 180 ringing if out-of-band seems reasonable */
-				transmit_response(p, "180 Ringing", &p->initreq);
+				if (!ast_strlen_zero(p->calledid)) {
+					transmit_response_with_calledrpid(p, "180 Ringing", &p->initreq);
+				} else {
+					transmit_response(p, "180 Ringing", &p->initreq);
+				}	
 				ast_set_flag(p, SIP_RINGING);
 				if (ast_test_flag(p, SIP_PROG_INBAND) != SIP_PROG_INBAND_YES)
 					break;
@@ -4139,6 +4145,16 @@
 	return __transmit_response(p, msg, req, 0);
 }
 
+/*! \brief  transmit_response_with_calledrpid: Transmit response, no retransmits */
+static int transmit_response_with_calledrpid(struct sip_pvt *p, char *msg, struct sip_request *req) 
+{
+	struct sip_request resp;
+	respprep(&resp, p, msg, req);
+	append_date(&resp);
+	add_header(&resp, "Remote-Party-ID", p->calledid);
+	return send_response(p, &resp, 0, 0);
+}
+
 /*! \brief  transmit_response_with_unsupported: Transmit response, no retransmits */
 static int transmit_response_with_unsupported(struct sip_pvt *p, char *msg, struct sip_request *req, char *unsupported) 
 {
@@ -4509,6 +4525,8 @@
 		return -1;
 	}
 	respprep(&resp, p, msg, req);
+	if (!ast_strlen_zero(p->calledid))
+		add_header(&resp, "Remote-Party-ID", p->calledid);
 	if (p->rtp) {
 		ast_rtp_offered_from_local(p->rtp, 0);
 		add_sdp(&resp, p);
@@ -12750,6 +12768,20 @@
 "Skips to priority+101 if header does not exist\n"
 "Otherwise returns 0\n";
 
+static char *app_sipcalledrpid = "SIPCalledRPID";
+static char *synopsis_sipcalledrpid = "Set the RPID header for Called Party Identification";
+ 
+static char *descrip_sipcalledrpid = ""
+"  SIPCalledRPID(name,number): \n"
+"Sets the Remote-Party-ID header information to be sent to a call\n"
+"originator on further progress through this extension. Many SIP\n"
+"phones use this header to display the name and number of the party\n"
+"which has been called from the telephone. This progress may be\n"
+"updated throughout the call progress whenever Asterisk sends a new\n"
+"180 Ringing, 183 Call Progress, or 200 OK response to the calling\n"
+"channel."
+"Always returns 0\n";
+
 /*! \brief  sip_dtmfmode: change the DTMFmode for a SIP call (application) ---*/
 static int sip_dtmfmode(struct ast_channel *chan, void *data)
 {
@@ -12879,6 +12911,45 @@
 	return 0;
 }
 
+/*! \brief  sip_calledrpid: Set the value of the RPID header for called party identification ---*/
+static int sip_calledrpid(struct ast_channel *chan, void *data)
+{
+	struct sip_pvt *p;
+	char *argv, *name = NULL, *number = NULL;
+	char rpid[256];
+	
+	argv = ast_strdupa(data);
+	if (!argv) {
+		ast_log(LOG_DEBUG, "Memory allocation failed\n");
+		return 0;
+	}
+
+	if (strchr (argv, '|') ) {	/* Pick out argumenets */
+		name = strsep (&argv, "|");
+		number = strsep (&argv, "\0");
+	}
+
+	if (!name || !number) {
+		ast_log(LOG_DEBUG, "SIPCalledRPID: Ignoring command, Syntax error in argument\n");
+		return 0;
+	}
+
+	ast_mutex_lock(&chan->lock);
+
+	if (chan->type != channeltype) {
+		ast_log(LOG_WARNING, "Call this application only on incoming SIP calls\n");
+		ast_mutex_unlock(&chan->lock);
+		return 0;
+	}
+	
+	p = chan->tech_pvt;
+	sprintf(rpid, "\"%s\" <sip:%s@foo.com>;party=called;id-type=subscriber;screen=yes",name, number);
+	ast_copy_string(p->calledid, rpid, sizeof(p->calledid));
+	
+	ast_mutex_unlock(&chan->lock);
+	return 0;
+}
+
 /*! \brief  sip_sipredirect: Transfer call before connect with a 302 redirect ---*/
 /* Called by the transfer() dialplan application through the sip_transfer() */
 /* pbx interface function if the call is in ringing state */
@@ -13116,6 +13187,8 @@
 	/* These will be removed soon */
 	ast_register_application(app_sipaddheader, sip_addheader, synopsis_sipaddheader, descrip_sipaddheader);
 	ast_register_application(app_sipgetheader, sip_getheader, synopsis_sipgetheader, descrip_sipgetheader);
+	
+	ast_register_application(app_sipcalledrpid, sip_calledrpid, synopsis_sipcalledrpid, descrip_sipcalledrpid);
 
 	/* Register dialplan functions */
 	ast_custom_function_register(&sip_header_function);
@@ -13153,6 +13226,7 @@
 	ast_unregister_application(app_dtmfmode);
 	ast_unregister_application(app_sipaddheader);
 	ast_unregister_application(app_sipgetheader);
+	ast_unregister_application(app_sipcalledrpid);
 
 	ast_cli_unregister_multiple(my_clis, sizeof(my_clis) / sizeof(my_clis[0]));
 
