From 04fc29c8b2e7db9b66a138f47412770867819f7f Mon Sep 17 00:00:00 2001
From: Mark Spencer <markster@digium.com>
Date: Sat, 2 Oct 2004 00:58:31 +0000
Subject: [PATCH] Huge callerid rework (might break H.323, others)

git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@3874 65c4cc65-6c06-0410-ace0-fbb531ad65f3
---
 apps/Makefile                  |   7 +-
 apps/app_alarmreceiver.c       |   4 +-
 apps/app_chanisavail.c         |   4 +-
 apps/app_db.c                  |   6 +-
 apps/app_dial.c                |  92 +++++-----
 apps/app_directory.c           |   6 +-
 apps/app_disa.c                |  10 +-
 apps/app_enumlookup.c          |   8 +-
 apps/app_groupcount.c          |   2 +-
 apps/app_hasnewvoicemail.c     |   2 +-
 apps/app_image.c               |   6 +-
 apps/app_lookupblacklist.c     |  28 ++-
 apps/app_lookupcidname.c       |  31 +---
 apps/app_macro.c               |  10 +-
 apps/app_meetme.c              |   2 +-
 apps/app_osplookup.c           |  14 +-
 apps/app_parkandannounce.c     |  10 +-
 apps/app_privacy.c             |  11 +-
 apps/app_queue.c               |  56 +++---
 apps/app_sendtext.c            |   6 +-
 apps/app_setcallerid.c         |   7 +-
 apps/app_setcidname.c          |  24 +--
 apps/app_setcidnum.c           |  24 +--
 apps/app_sms.c                 |  13 +-
 apps/app_system.c              |   2 +-
 apps/app_talkdetect.c          |   4 +-
 apps/app_transfer.c            |   6 +-
 apps/app_txtcidname.c          |   2 +-
 apps/app_url.c                 |   8 +-
 apps/app_voicemail.c           |  80 ++++-----
 apps/app_zapateller.c          |   6 +-
 callerid.c                     |  62 +++++--
 cdr.c                          |  97 +++++-----
 channel.c                      | 109 +++++++-----
 channels/chan_agent.c          |  26 +--
 channels/chan_iax2.c           |  86 +++++----
 channels/chan_local.c          |  28 +--
 channels/chan_mgcp.c           | 100 ++++++-----
 channels/chan_modem.c          |  18 +-
 channels/chan_modem_bestdata.c |  15 +-
 channels/chan_modem_i4l.c      |  20 +--
 channels/chan_oss.c            |   6 +-
 channels/chan_phone.c          |  52 +++---
 channels/chan_sip.c            |  89 +++++-----
 channels/chan_skinny.c         |  60 ++++---
 channels/chan_zap.c            | 311 ++++++++++++++++++---------------
 cli.c                          |   8 +-
 include/asterisk/callerid.h    |  13 +-
 include/asterisk/channel.h     |  50 +++---
 include/asterisk/pbx.h         |   4 +-
 include/asterisk/vmodem.h      |   6 +-
 manager.c                      |  51 ++++--
 pbx.c                          | 112 ++++++------
 pbx/pbx_spool.c                |  12 +-
 res/res_agi.c                  |  23 ++-
 res/res_features.c             |  12 +-
 56 files changed, 999 insertions(+), 862 deletions(-)

diff --git a/apps/Makefile b/apps/Makefile
index ebd149c76a..ce0fef4ca5 100755
--- a/apps/Makefile
+++ b/apps/Makefile
@@ -17,7 +17,7 @@ USE_POSTGRES_VM_INTERFACE=0
 #APPS=app_dial.so app_playback.so app_directory.so app_intercom.so app_mp3.so 
 APPS=app_dial.so app_playback.so app_voicemail.so app_directory.so app_mp3.so\
      app_system.so app_echo.so app_record.so app_image.so app_url.so app_disa.so \
-     app_qcall.so app_adsiprog.so app_getcpeid.so app_milliwatt.so \
+     app_adsiprog.so app_getcpeid.so app_milliwatt.so \
      app_zapateller.so app_setcallerid.so app_festival.so \
      app_queue.so app_senddtmf.so app_parkandannounce.so app_striplsd.so \
      app_setcidname.so app_lookupcidname.so app_substring.so app_macro.so \
@@ -35,6 +35,11 @@ ifneq (${OSARCH},Darwin)
 APPS+=app_intercom.so
 endif
 
+#
+# Obsolete things...
+#
+#APPS+=app_qcall.so
+
 #APPS+=app_sql_postgres.so
 #APPS+=app_sql_odbc.so
 #APPS+=app_rpt.so
diff --git a/apps/app_alarmreceiver.c b/apps/app_alarmreceiver.c
index 28d28b4c43..2c95be44c6 100755
--- a/apps/app_alarmreceiver.c
+++ b/apps/app_alarmreceiver.c
@@ -318,8 +318,8 @@ static int write_metadata( FILE *logfile, char *signalling_type, struct ast_chan
 	char timestamp[80];
 	
 	/* Extract the caller ID location */
-	
-	strncpy(workstring, chan->callerid, sizeof(workstring) - 1);
+	if (chan->cid.cid_num)
+		strncpy(workstring, chan->cid.cid_num, sizeof(workstring) - 1);
 	workstring[sizeof(workstring) - 1] = '\0';
 	
 	ast_callerid_parse(workstring, &cn, &cl);
diff --git a/apps/app_chanisavail.c b/apps/app_chanisavail.c
index 4438663ce1..d61b0f1d3d 100755
--- a/apps/app_chanisavail.c
+++ b/apps/app_chanisavail.c
@@ -3,7 +3,7 @@
  *
  * Check if Channel is Available
  * 
- * Copyright (C) 2003, Digium
+ * Copyright (C) 2003-2004, Digium, Inc.
  *
  * Mark Spencer <markster@digium.com>
  * James Golovich <james@gnuinter.net>
@@ -97,7 +97,7 @@ static int chanavail_exec(struct ast_channel *chan, void *data)
 	if (res < 1) {
 		pbx_builtin_setvar_helper(chan, "AVAILCHAN", "");
 		pbx_builtin_setvar_helper(chan, "AVAILORIGCHAN", "");
-		if (ast_exists_extension(chan, chan->context, chan->exten, chan->priority + 101, chan->callerid))
+		if (ast_exists_extension(chan, chan->context, chan->exten, chan->priority + 101, chan->cid.cid_num))
 			chan->priority+=100;
 		else
 			return -1;
diff --git a/apps/app_db.c b/apps/app_db.c
index 325b06bbbe..2014f7a8f8 100755
--- a/apps/app_db.c
+++ b/apps/app_db.c
@@ -3,10 +3,10 @@
  *
  * Database access functions
  *
- * Copyright (C) 1999, Mark Spencer
+ * Copyright (C) 1999-2004, Digium, Inc.
  * Copyright (C) 2003, Jefferson Noxon
  *
- * Mark Spencer <markster@linux-support.net>
+ * Mark Spencer <markster@digium.com>
  * Jefferson Noxon <jeff@debian.org>
  *
  * This program is free software, distributed under the terms of
@@ -200,7 +200,7 @@ static int get_exec (struct ast_channel *chan, void *data)
 			if (option_verbose > 2)
 			ast_verbose (VERBOSE_PREFIX_3 "DBget: Value not found in database.\n");
 			/* Send the call to n+101 priority, where n is the current priority */
-			if (ast_exists_extension (chan, chan->context, chan->exten, chan->priority + 101, chan->callerid))
+			if (ast_exists_extension (chan, chan->context, chan->exten, chan->priority + 101, chan->cid.cid_num))
 				chan->priority += 100;
 		}
 
diff --git a/apps/app_dial.c b/apps/app_dial.c
index 7f3c3d7682..e9c435ff59 100755
--- a/apps/app_dial.c
+++ b/apps/app_dial.c
@@ -186,7 +186,7 @@ static struct ast_channel *wait_for_answer(struct ast_channel *in, struct localu
 				else if (numnochan)
 					strncpy(status, "CHANUNAVAIL", statussize - 1);
 				/* See if there is a special busy message */
-				if (ast_exists_extension(in, in->context, in->exten, in->priority + 101, in->callerid)) 
+				if (ast_exists_extension(in, in->context, in->exten, in->priority + 101, in->cid.cid_num)) 
 					in->priority+=100;
 			} else {
 				if (option_verbose > 2)
@@ -233,10 +233,12 @@ static struct ast_channel *wait_for_answer(struct ast_channel *in, struct localu
 						o->stillgoing = 0;
 						numnochan++;
 					} else {
-						if (o->chan->callerid)
-							free(o->chan->callerid);
-
-						o->chan->callerid = NULL;
+						if (o->chan->cid.cid_num)
+							free(o->chan->cid.cid_num);
+						o->chan->cid.cid_num = NULL;
+						if (o->chan->cid.cid_name)
+							free(o->chan->cid.cid_name);
+						o->chan->cid.cid_name = NULL;
 
 						if (o->forcecallerid) {
 							char *newcid = NULL;
@@ -245,36 +247,41 @@ static struct ast_channel *wait_for_answer(struct ast_channel *in, struct localu
 								newcid = in->macroexten;
 							else
 								newcid = in->exten;
-							o->chan->callerid = strdup(newcid);
+							o->chan->cid.cid_num = strdup(newcid);
 							strncpy(o->chan->accountcode, winner->accountcode, sizeof(o->chan->accountcode) - 1);
 							o->chan->cdrflags = winner->cdrflags;
-							if (!o->chan->callerid)
+							if (!o->chan->cid.cid_num)
 								ast_log(LOG_WARNING, "Out of memory\n");
 						} else {
-							if (in->callerid) {
-								o->chan->callerid = strdup(in->callerid);
-								if (!o->chan->callerid)
+							if (in->cid.cid_num) {
+								o->chan->cid.cid_num = strdup(in->cid.cid_num);
+								if (!o->chan->cid.cid_num)
+									ast_log(LOG_WARNING, "Out of memory\n");	
+							}
+							if (in->cid.cid_name) {
+								o->chan->cid.cid_name = strdup(in->cid.cid_name);
+								if (!o->chan->cid.cid_name)
 									ast_log(LOG_WARNING, "Out of memory\n");	
 							}
 							strncpy(o->chan->accountcode, in->accountcode, sizeof(o->chan->accountcode) - 1);
 							o->chan->cdrflags = in->cdrflags;
 						}
 
-						if (in->ani) {
-							if (o->chan->ani)
-								free(o->chan->ani);
-							o->chan->ani = malloc(strlen(in->ani) + 1);
-							if (o->chan->ani)
-								strncpy(o->chan->ani, in->ani, strlen(in->ani));
+						if (in->cid.cid_ani) {
+							if (o->chan->cid.cid_ani)
+								free(o->chan->cid.cid_ani);
+							o->chan->cid.cid_ani = malloc(strlen(in->cid.cid_ani) + 1);
+							if (o->chan->cid.cid_ani)
+								strncpy(o->chan->cid.cid_ani, in->cid.cid_ani, strlen(in->cid.cid_ani) + 1);
 							else
 								ast_log(LOG_WARNING, "Out of memory\n");
 						}
-						if (o->chan->rdnis) 
-							free(o->chan->rdnis);
+						if (o->chan->cid.cid_rdnis) 
+							free(o->chan->cid.cid_rdnis);
 						if (!ast_strlen_zero(in->macroexten))
-							o->chan->rdnis = strdup(in->macroexten);
+							o->chan->cid.cid_rdnis = strdup(in->macroexten);
 						else
-							o->chan->rdnis = strdup(in->exten);
+							o->chan->cid.cid_rdnis = strdup(in->exten);
 						if (ast_call(o->chan, tmpchan, 0)) {
 							ast_log(LOG_NOTICE, "Failed to dial on local channel for call forward to '%s'\n", tmpchan);
 							o->stillgoing = 0;
@@ -429,7 +436,7 @@ static int dial_exec(struct ast_channel *chan, void *data)
 	char restofit[AST_MAX_EXTENSION];
 	char *transfer = NULL;
 	char *newnum;
-	char callerid[256] = "", *l, *n;
+	char *l;
 	char *url=NULL; /* JDG */
 	struct ast_var_t *current;
 	struct varshead *headp, *newheadp;
@@ -668,14 +675,8 @@ static int dial_exec(struct ast_channel *chan, void *data)
 		strncpy(privdb, chan->exten, sizeof(privdb) - 1);
 	}
 	if (privacy) {
-		if (chan->callerid)
-			strncpy(callerid, chan->callerid, sizeof(callerid) - 1);
-		else
-			callerid[0] = '\0';
-		ast_callerid_parse(callerid, &n, &l);
-		if (l) {
-			ast_shrink_phone_number(l);
-		} else
+		l = chan->cid.cid_num;
+		if (!l)
 			l = "";
 		ast_log(LOG_NOTICE, "Privacy DB is '%s', privacy is %d, clid is '%s'\n", privdb, privacy, l);
 	}
@@ -797,28 +798,31 @@ static int dial_exec(struct ast_channel *chan, void *data)
 		tmp->chan->appl = "AppDial";
 		tmp->chan->data = "(Outgoing Line)";
 		tmp->chan->whentohangup = 0;
-		if (tmp->chan->callerid)
-			free(tmp->chan->callerid);
-		if (tmp->chan->ani)
-			free(tmp->chan->ani);
-		if (chan->callerid)
-			tmp->chan->callerid = strdup(chan->callerid);
-		else
-			tmp->chan->callerid = NULL;
+		if (tmp->chan->cid.cid_num)
+			free(tmp->chan->cid.cid_num);
+		tmp->chan->cid.cid_num = NULL;
+		if (tmp->chan->cid.cid_name)
+			free(tmp->chan->cid.cid_name);
+		tmp->chan->cid.cid_name = NULL;
+		if (tmp->chan->cid.cid_ani)
+			free(tmp->chan->cid.cid_ani);
+		tmp->chan->cid.cid_ani = NULL;
+
+		if (chan->cid.cid_num) 
+			tmp->chan->cid.cid_num = strdup(chan->cid.cid_num);
+		if (chan->cid.cid_name) 
+			tmp->chan->cid.cid_name = strdup(chan->cid.cid_name);
+		if (chan->cid.cid_ani) 
+			tmp->chan->cid.cid_ani = strdup(chan->cid.cid_ani);
+		
 		/* Copy language from incoming to outgoing */
 		strncpy(tmp->chan->language, chan->language, sizeof(tmp->chan->language) - 1);
 		strncpy(tmp->chan->accountcode, chan->accountcode, sizeof(tmp->chan->accountcode) - 1);
 		tmp->chan->cdrflags = chan->cdrflags;
 		if (ast_strlen_zero(tmp->chan->musicclass))
 			strncpy(tmp->chan->musicclass, chan->musicclass, sizeof(tmp->chan->musicclass) - 1);
-		if (chan->ani)
-			tmp->chan->ani = strdup(chan->ani);
-		else
-			tmp->chan->ani = NULL;
-		/* Pass hidecallerid setting */
-		tmp->chan->restrictcid = chan->restrictcid;
 		/* Pass callingpres setting */
-		tmp->chan->callingpres = chan->callingpres;
+		tmp->chan->cid.cid_pres = chan->cid.cid_pres;
 		/* Presense of ADSI CPE on outgoing channel follows ours */
 		tmp->chan->adsicpe = chan->adsicpe;
 		/* pass the digital flag */
diff --git a/apps/app_directory.c b/apps/app_directory.c
index e0a962f70b..306c67c084 100755
--- a/apps/app_directory.c
+++ b/apps/app_directory.c
@@ -3,9 +3,9 @@
  *
  * Provide a directory of extensions
  * 
- * Copyright (C) 1999, Mark Spencer
+ * Copyright (C) 1999-2004, Digium, Inc.
  *
- * Mark Spencer <markster@linux-support.net>
+ * Mark Spencer <markster@digium.com>
  *
  * This program is free software, distributed under the terms of
  * the GNU General Public License
@@ -176,7 +176,7 @@ static int play_mailbox_owner(struct ast_channel *chan, char *context, char *dia
 				case '1':
 					/* Name selected */
 					loop = 0;
-					if (ast_exists_extension(chan,dialcontext,ext,1,chan->callerid)) {
+					if (ast_exists_extension(chan,dialcontext,ext,1,chan->cid.cid_num)) {
 						strncpy(chan->exten, ext, sizeof(chan->exten)-1);
 						chan->priority = 0;
 						strncpy(chan->context, dialcontext, sizeof(chan->context)-1);
diff --git a/apps/app_disa.c b/apps/app_disa.c
index 03f13c67c4..6d1964990b 100755
--- a/apps/app_disa.c
+++ b/apps/app_disa.c
@@ -3,7 +3,7 @@
  *
  * DISA -- Direct Inward System Access Application  6/20/2001
  * 
- * Copyright (C) 2001, Linux Support Services, Inc.
+ * Copyright (C) 2001-2004, Digium, Inc.
  *
  * Jim Dixon <jim@lambdatel.com>
  *
@@ -293,20 +293,20 @@ static int disa_exec(struct ast_channel *chan, void *data)
 				}
 
 			  /* if can do some more, do it */
-			if (!ast_matchmore_extension(chan,ourcontext,exten,1, chan->callerid)) {
+			if (!ast_matchmore_extension(chan,ourcontext,exten,1, chan->cid.cid_num)) {
 				break;
 			}
 		}
 	}
 
-	if (k && ast_exists_extension(chan,ourcontext,exten,1, chan->callerid))
+	if (k && ast_exists_extension(chan,ourcontext,exten,1, chan->cid.cid_num))
 	{
 		ast_playtones_stop(chan);
 		/* We're authenticated and have a valid extension */
 		if (ourcallerid && *ourcallerid)
 		{
-			if (chan->callerid) free(chan->callerid);
-			chan->callerid = strdup(ourcallerid);
+			if (chan->cid.cid_num) free(chan->cid.cid_num);
+			chan->cid.cid_num = strdup(ourcallerid);
 		}
 		strncpy(chan->exten, exten, sizeof(chan->exten) - 1);
 		strncpy(chan->context, ourcontext, sizeof(chan->context) - 1);
diff --git a/apps/app_enumlookup.c b/apps/app_enumlookup.c
index d2ea84c295..76593b9178 100755
--- a/apps/app_enumlookup.c
+++ b/apps/app_enumlookup.c
@@ -3,9 +3,9 @@
  *
  * Time of day - Report the time of day
  * 
- * Copyright (C) 1999, Mark Spencer
+ * Copyright (C) 1999-2004, Digium, Inc.
  *
- * Mark Spencer <markster@linux-support.net>
+ * Mark Spencer <markster@digium.com>
  *
  * This program is free software, distributed under the terms of
  * the GNU General Public License
@@ -121,7 +121,7 @@ static int enumlookup_exec(struct ast_channel *chan, void *data)
 				*t = 0;
 				pbx_builtin_setvar_helper(chan, "ENUM", tmp);
 				ast_log(LOG_NOTICE, "tel: ENUM set to \"%s\"\n", tmp);
-				if (ast_exists_extension(chan, chan->context, chan->exten, chan->priority + 51, chan->callerid))
+				if (ast_exists_extension(chan, chan->context, chan->exten, chan->priority + 51, chan->cid.cid_num))
 					chan->priority += 50;
 				else
 					res = 0;
@@ -133,7 +133,7 @@ static int enumlookup_exec(struct ast_channel *chan, void *data)
 	}
 	if (!res) {
 		/* Look for a "busy" place */
-		if (ast_exists_extension(chan, chan->context, chan->exten, chan->priority + 101, chan->callerid))
+		if (ast_exists_extension(chan, chan->context, chan->exten, chan->priority + 101, chan->cid.cid_num))
 			chan->priority += 100;
 	} else if (res > 0)
 		res = 0;
diff --git a/apps/app_groupcount.c b/apps/app_groupcount.c
index 7835d580e8..18838be236 100755
--- a/apps/app_groupcount.c
+++ b/apps/app_groupcount.c
@@ -174,7 +174,7 @@ static int group_check_exec(struct ast_channel *chan, void *data)
 			
 			count = group_get_count(pbx_builtin_getvar_helper(chan, ret), ret);
 			if (count > max) {
-				if (ast_exists_extension(chan, chan->context, chan->exten, chan->priority + 101, chan->callerid))
+				if (ast_exists_extension(chan, chan->context, chan->exten, chan->priority + 101, chan->cid.cid_num))
 					chan->priority += 100;
 				else
 					res = -1;
diff --git a/apps/app_hasnewvoicemail.c b/apps/app_hasnewvoicemail.c
index 733a65ec81..ab6fbad1d2 100755
--- a/apps/app_hasnewvoicemail.c
+++ b/apps/app_hasnewvoicemail.c
@@ -119,7 +119,7 @@ static int hasvoicemail_exec(struct ast_channel *chan, void *data)
 
 		if (vmcount > 0) {
 			/* Branch to the next extension */
-			if (ast_exists_extension(chan, chan->context, chan->exten, chan->priority + 101, chan->callerid)) {
+			if (ast_exists_extension(chan, chan->context, chan->exten, chan->priority + 101, chan->cid.cid_num)) {
 				chan->priority += 100;
 			} else
 				ast_log(LOG_WARNING, "VM box %s@%s has new voicemail, but extension %s, priority %d doesn't exist\n", vmbox, context, chan->exten, chan->priority + 101);
diff --git a/apps/app_image.c b/apps/app_image.c
index 04af0a3155..3665a0fefb 100755
--- a/apps/app_image.c
+++ b/apps/app_image.c
@@ -3,9 +3,9 @@
  *
  * App to transmit an image
  * 
- * Copyright (C) 1999, Mark Spencer
+ * Copyright (C) 1999-2004, Digium, Inc.
  *
- * Mark Spencer <markster@linux-support.net>
+ * Mark Spencer <markster@digium.com>
  *
  * This program is free software, distributed under the terms of
  * the GNU General Public License
@@ -51,7 +51,7 @@ static int sendimage_exec(struct ast_channel *chan, void *data)
 	LOCAL_USER_ADD(u);
 	if (!ast_supports_images(chan)) {
 		/* Does not support transport */
-		if (ast_exists_extension(chan, chan->context, chan->exten, chan->priority + 101, chan->callerid))
+		if (ast_exists_extension(chan, chan->context, chan->exten, chan->priority + 101, chan->cid.cid_num))
 			chan->priority += 100;
 		return 0;
 	}
diff --git a/apps/app_lookupblacklist.c b/apps/app_lookupblacklist.c
index 69743e377c..82a556b15a 100755
--- a/apps/app_lookupblacklist.c
+++ b/apps/app_lookupblacklist.c
@@ -3,9 +3,9 @@
  *
  * App to lookup the callerid number, and see if it is blacklisted
  * 
- * Copyright (C) 1999, Mark Spencer
+ * Copyright (C) 1999-2004, Digium, Inc.
  *
- * Mark Spencer <markster@linux-support.net>
+ * Mark Spencer <markster@digium.com>
  *
  * This program is free software, distributed under the terms of
  * the GNU General Public License
@@ -48,38 +48,30 @@ LOCAL_USER_DECL;
 static int
 lookupblacklist_exec (struct ast_channel *chan, void *data)
 {
-	char old_cid[144] = "", *num, *name;
 	char blacklist[1];
-	char shrunknum[64] = "";
 	struct localuser *u;
 	int bl = 0;
 
 	LOCAL_USER_ADD (u);
-	if (chan->callerid)
+	if (chan->cid.cid_num)
 	{
-		strncpy (old_cid, chan->callerid, sizeof (old_cid) - 1);
-		ast_callerid_parse (old_cid, &name, &num);
-		if (num)
-			strncpy (shrunknum, num, sizeof (shrunknum) - 1);
-		else
-			num = shrunknum;
-		
-		ast_shrink_phone_number (shrunknum);
-		if (!ast_db_get ("blacklist", shrunknum, blacklist, sizeof (blacklist)))
+		if (!ast_db_get ("blacklist", chan->cid.cid_num, blacklist, sizeof (blacklist)))
 		{
 			if (option_verbose > 2)
-				ast_log(LOG_NOTICE, "Blacklisted number %s found\n",shrunknum);
+				ast_log(LOG_NOTICE, "Blacklisted number %s found\n",chan->cid.cid_num);
 			bl = 1;
 		}
-		else if (!ast_db_get ("blacklist", name, blacklist, sizeof (blacklist)))
+	}
+	if (chan->cid.cid_name) {
+		if (!ast_db_get ("blacklist", chan->cid.cid_name, blacklist, sizeof (blacklist))) 
 		{
 			if (option_verbose > 2)
-				ast_log (LOG_NOTICE,"Blacklisted name \"%s\" found\n",name);
+				ast_log (LOG_NOTICE,"Blacklisted name \"%s\" found\n",chan->cid.cid_name);
 			bl = 1;
 		}
 	}
 	
-	if (bl && ast_exists_extension(chan, chan->context, chan->exten, chan->priority + 101, chan->callerid))
+	if (bl && ast_exists_extension(chan, chan->context, chan->exten, chan->priority + 101, chan->cid.cid_num))
 		chan->priority+=100;
 	LOCAL_USER_REMOVE (u);
 	return 0;
diff --git a/apps/app_lookupcidname.c b/apps/app_lookupcidname.c
index ecb0a4e5c2..aac3191fed 100755
--- a/apps/app_lookupcidname.c
+++ b/apps/app_lookupcidname.c
@@ -3,9 +3,9 @@
  *
  * App to set callerid name from database, based on directory number
  * 
- * Copyright (C) 1999, Mark Spencer
+ * Copyright (C) 1999-2004, Digium, Inc.
  *
- * Mark Spencer <markster@linux-support.net>
+ * Mark Spencer <markster@digium.com>
  *
  * This program is free software, distributed under the terms of
  * the GNU General Public License
@@ -46,32 +46,19 @@ LOCAL_USER_DECL;
 static int
 lookupcidname_exec (struct ast_channel *chan, void *data)
 {
-  char old_cid[144] = "", *num, *name;
-  char new_cid[144];
   char dbname[64];
   char shrunknum[64] = "";
   struct localuser *u;
 
   LOCAL_USER_ADD (u);
-  if (chan->callerid)
-    {
-      strncpy (old_cid, chan->callerid, sizeof (old_cid) - 1);
-      ast_callerid_parse (old_cid, &name, &num);	/* this destroys the original string */
-      if (num)			/* It's possible to get an empty number */
-	strncpy (shrunknum, num, sizeof (shrunknum) - 1);
-      else
-	num = shrunknum;
-      ast_shrink_phone_number (shrunknum);
-      if (!ast_db_get ("cidname", shrunknum, dbname, sizeof (dbname)))
-	{
-	  snprintf (new_cid, sizeof (new_cid), "\"%s\" <%s>", dbname, num);
-	  ast_set_callerid (chan, new_cid, 0);
-	  if (option_verbose > 2)
-	    ast_verbose (VERBOSE_PREFIX_3 "Changed Caller*ID to %s\n",
-			 new_cid);
+  if (chan->cid.cid_num) {
+	if (!ast_db_get ("cidname", shrunknum, dbname, sizeof (dbname))) {
+		ast_set_callerid (chan, NULL, dbname, NULL);
+		  if (option_verbose > 2)
+		    ast_verbose (VERBOSE_PREFIX_3 "Changed Caller*ID name to %s\n",
+				 dbname);
 	}
-
-    }
+  }
   LOCAL_USER_REMOVE (u);
   return 0;
 }
diff --git a/apps/app_macro.c b/apps/app_macro.c
index ea6775c90d..dd658689d5 100755
--- a/apps/app_macro.c
+++ b/apps/app_macro.c
@@ -3,7 +3,7 @@
  *
  * Macro Implementation
  * 
- * Copyright (C) 2003, Digium
+ * Copyright (C) 2003-2004, Digium, Inc.
  *
  * Mark Spencer <markster@digium.com>
  *
@@ -85,7 +85,7 @@ static int macro_exec(struct ast_channel *chan, void *data)
 	return 0;
   }
   snprintf(fullmacro, sizeof(fullmacro), "macro-%s", macro);
-  if (!ast_exists_extension(chan, fullmacro, "s", 1, chan->callerid)) {
+  if (!ast_exists_extension(chan, fullmacro, "s", 1, chan->cid.cid_num)) {
   	if (!ast_context_find(fullmacro)) 
 		ast_log(LOG_WARNING, "No such context '%s' for macro '%s'\n", fullmacro, macro);
 	else
@@ -137,8 +137,8 @@ static int macro_exec(struct ast_channel *chan, void *data)
 	pbx_builtin_setvar_helper(chan, varname, cur);
 	argc++;
   }
-  while(ast_exists_extension(chan, chan->context, chan->exten, chan->priority, chan->callerid)) {
-	if ((res = ast_spawn_extension(chan, chan->context, chan->exten, chan->priority, chan->callerid))) {
+  while(ast_exists_extension(chan, chan->context, chan->exten, chan->priority, chan->cid.cid_num)) {
+	if ((res = ast_spawn_extension(chan, chan->context, chan->exten, chan->priority, chan->cid.cid_num))) {
 		/* Something bad happened, or a hangup has been requested. */
 		if (((res >= '0') && (res <= '9')) || ((res >= 'A') && (res <= 'F'))) {
 			/* Just return result as to the previous application as if it had been dialed */
@@ -210,7 +210,7 @@ out:
 			/* Handle macro offset if it's set by checking the availability of step n + offset + 1, otherwise continue
 			   normally if there is any problem */
 			if (sscanf(offsets, "%d", &offset) == 1) {
-				if (ast_exists_extension(chan, chan->context, chan->exten, chan->priority + offset + 1, chan->callerid)) {
+				if (ast_exists_extension(chan, chan->context, chan->exten, chan->priority + offset + 1, chan->cid.cid_num)) {
 					chan->priority += offset;
 				}
 			}
diff --git a/apps/app_meetme.c b/apps/app_meetme.c
index ef2ff3de91..e9630d0a6e 100755
--- a/apps/app_meetme.c
+++ b/apps/app_meetme.c
@@ -821,7 +821,7 @@ zapretry:
 					char tmp[2];
 					tmp[0] = f->subclass;
 					tmp[1] = '\0';
-					if (ast_exists_extension(chan, exitcontext, tmp, 1, chan->callerid)) {
+					if (ast_exists_extension(chan, exitcontext, tmp, 1, chan->cid.cid_num)) {
 						strncpy(chan->context, exitcontext, sizeof(chan->context) - 1);
 						strncpy(chan->exten, tmp, sizeof(chan->exten) - 1);
 						chan->priority = 0;
diff --git a/apps/app_osplookup.c b/apps/app_osplookup.c
index 55216d4ebf..4110d740bb 100755
--- a/apps/app_osplookup.c
+++ b/apps/app_osplookup.c
@@ -1,11 +1,11 @@
 /*
  * Asterisk -- A telephony toolkit for Linux.
  *
- * Time of day - Report the time of day
+ * Open Settlement Protocol Lookup
  * 
- * Copyright (C) 1999, Mark Spencer
+ * Copyright (C) 1999-2004, Digium, Inc.
  *
- * Mark Spencer <markster@linux-support.net>
+ * Mark Spencer <markster@digium.com>
  *
  * This program is free software, distributed under the terms of
  * the GNU General Public License
@@ -110,7 +110,7 @@ static int osplookup_exec(struct ast_channel *chan, void *data)
 	}
 	LOCAL_USER_ADD(u);
 	ast_log(LOG_DEBUG, "Whoo hoo, looking up OSP on '%s' via '%s'\n", temp, provider ? provider : "<default>");
-	if ((res = ast_osp_lookup(chan, provider, temp, chan->callerid, &result)) > 0) {
+	if ((res = ast_osp_lookup(chan, provider, temp, chan->cid.cid_num, &result)) > 0) {
 		char tmp[80];
 		snprintf(tmp, sizeof(tmp), "%d", result.handle);
 		pbx_builtin_setvar_helper(chan, "OSPHANDLE", tmp);
@@ -127,7 +127,7 @@ static int osplookup_exec(struct ast_channel *chan, void *data)
 	}
 	if (!res) {
 		/* Look for a "busy" place */
-		if (ast_exists_extension(chan, chan->context, chan->exten, chan->priority + 101, chan->callerid))
+		if (ast_exists_extension(chan, chan->context, chan->exten, chan->priority + 101, chan->cid.cid_num))
 			chan->priority += 100;
 	} else if (res > 0)
 		res = 0;
@@ -171,7 +171,7 @@ static int ospnext_exec(struct ast_channel *chan, void *data)
 	}
 	if (!res) {
 		/* Look for a "busy" place */
-		if (ast_exists_extension(chan, chan->context, chan->exten, chan->priority + 101, chan->callerid))
+		if (ast_exists_extension(chan, chan->context, chan->exten, chan->priority + 101, chan->cid.cid_num))
 			chan->priority += 100;
 	} else if (res > 0)
 		res = 0;
@@ -218,7 +218,7 @@ static int ospfinished_exec(struct ast_channel *chan, void *data)
 	}
 	if (!res) {
 		/* Look for a "busy" place */
-		if (ast_exists_extension(chan, chan->context, chan->exten, chan->priority + 101, chan->callerid))
+		if (ast_exists_extension(chan, chan->context, chan->exten, chan->priority + 101, chan->cid.cid_num))
 			chan->priority += 100;
 	} else if (res > 0)
 		res = 0;
diff --git a/apps/app_parkandannounce.c b/apps/app_parkandannounce.c
index c3f949ec32..269ee1fdec 100755
--- a/apps/app_parkandannounce.c
+++ b/apps/app_parkandannounce.c
@@ -6,9 +6,9 @@
  *    With TONS of help from Mark!
  * 
  * Asterisk is Copyrighted as follows
- * Copyright (C) 1999, Mark Spencer
+ * Copyright (C) 1999-2004, Digium, Inc.
  *
- * Mark Spencer <markster@linux-support.net>
+ * Mark Spencer <markster@digium.com>
  *
  * This program is free software, distributed under the terms of
  * the GNU General Public License
@@ -142,8 +142,8 @@ static int parkandannounce_exec(struct ast_channel *chan, void *data)
 
 
 	if(option_verbose > 2) {
-		ast_verbose( VERBOSE_PREFIX_3 "Return Context: (%s,%s,%d) ID: %s\n", chan->context,chan->exten, chan->priority, chan->callerid);
-		if(!ast_exists_extension(chan, chan->context, chan->exten, chan->priority, chan->callerid)) {
+		ast_verbose( VERBOSE_PREFIX_3 "Return Context: (%s,%s,%d) ID: %s\n", chan->context,chan->exten, chan->priority, chan->cid.cid_num);
+		if(!ast_exists_extension(chan, chan->context, chan->exten, chan->priority, chan->cid.cid_num)) {
 			ast_verbose( VERBOSE_PREFIX_3 "Warning: Return Context Invalid, call will return to default|s\n");
 		}
 	}
@@ -161,7 +161,7 @@ static int parkandannounce_exec(struct ast_channel *chan, void *data)
 
 	/* Now place the call to the extention */
 
-	dchan = ast_request_and_dial(dialtech, AST_FORMAT_SLINEAR, dialstr,30000, &outstate, chan->callerid);
+	dchan = ast_request_and_dial(dialtech, AST_FORMAT_SLINEAR, dialstr,30000, &outstate, chan->cid.cid_num, chan->cid.cid_name);
 
 	if(dchan) {
 		if(dchan->_state == AST_STATE_UP) {
diff --git a/apps/app_privacy.c b/apps/app_privacy.c
index 7628ea0e73..2972ee73aa 100755
--- a/apps/app_privacy.c
+++ b/apps/app_privacy.c
@@ -3,9 +3,9 @@
  *
  * Block all calls without Caller*ID, require phone # to be entered
  * 
- * Copyright (C) 1999, Mark Spencer
+ * Copyright (C) 1999-2004, Digium, Inc.
  *
- * Mark Spencer <markster@linux-support.net>
+ * Mark Spencer <markster@digium.com>
  *
  * This program is free software, distributed under the terms of
  * the GNU General Public License
@@ -62,7 +62,7 @@ privacy_exec (struct ast_channel *chan, void *data)
 	struct ast_config *cfg;
 
 	LOCAL_USER_ADD (u);
-	if (chan->callerid)
+	if (chan->cid.cid_num)
 	{
 		if (option_verbose > 2)
 			ast_verbose (VERBOSE_PREFIX_3 "CallerID Present: Skipping\n");
@@ -118,13 +118,12 @@ privacy_exec (struct ast_channel *chan, void *data)
 			res = ast_streamfile(chan, "privacy-thankyou", chan->language);
 			if (!res)
 				res = ast_waitstream(chan, "");
-			snprintf (new_cid, sizeof (new_cid), "\"%s\" <%s>", "Privacy Manager", phone);
-			ast_set_callerid (chan, new_cid, 0);
+			ast_set_callerid (chan, phone, "Privacy Manager", NULL);
 			if (option_verbose > 2)
 				ast_verbose (VERBOSE_PREFIX_3 "Changed Caller*ID to %s\n",new_cid);
 		} else {
 			/*Send the call to n+101 priority, where n is the current priority*/
-			if (ast_exists_extension(chan, chan->context, chan->exten, chan->priority + 101, chan->callerid))
+			if (ast_exists_extension(chan, chan->context, chan->exten, chan->priority + 101, chan->cid.cid_num))
 				chan->priority+=100;
 		}
 		if (cfg) 
diff --git a/apps/app_queue.c b/apps/app_queue.c
index 6e1528b0c8..daea410be8 100755
--- a/apps/app_queue.c
+++ b/apps/app_queue.c
@@ -3,9 +3,9 @@
  *
  * True call queues with optional send URL on answer
  * 
- * Copyright (C) 1999, Mark Spencer
+ * Copyright (C) 1999-2004, Digium, Inc.
  *
- * Mark Spencer <markster@linux-support.net>
+ * Mark Spencer <markster@digium.com>
  *
  * 2004-06-04: Priorities in queues added by inAccess Networks (work funded by Hellas On Line (HOL) www.hol.gr).
  *
@@ -318,8 +318,11 @@ static int join_queue(char *queuename, struct queue_ent *qe)
 				q->count++;
 				res = 0;
 				manager_event(EVENT_FLAG_CALL, "Join", 
-					"Channel: %s\r\nCallerID: %s\r\nQueue: %s\r\nPosition: %d\r\nCount: %d\r\n",
-					qe->chan->name, (qe->chan->callerid ? qe->chan->callerid : "unknown"), q->name, qe->pos, q->count );
+					"Channel: %s\r\nCallerID: %s\r\nCallerIDName: %s\r\nQueue: %s\r\nPosition: %d\r\nCount: %d\r\n",
+					qe->chan->name, 
+					qe->chan->cid.cid_num ? qe->chan->cid.cid_num : "unknown",
+					qe->chan->cid.cid_name ? qe->chan->cid.cid_name : "unknown",
+					q->name, qe->pos, q->count );
 #if 0
 ast_log(LOG_NOTICE, "Queue '%s' Join, Channel '%s', Position '%d'\n", q->name, qe->chan->name, qe->pos );
 #endif
@@ -569,18 +572,21 @@ static int ring_entry(struct queue_ent *qe, struct localuser *tmp)
 	tmp->chan->appl = "AppQueue";
 	tmp->chan->data = "(Outgoing Line)";
 	tmp->chan->whentohangup = 0;
-	if (tmp->chan->callerid)
-		free(tmp->chan->callerid);
-	if (tmp->chan->ani)
-		free(tmp->chan->ani);
-	if (qe->chan->callerid)
-		tmp->chan->callerid = strdup(qe->chan->callerid);
-	else
-		tmp->chan->callerid = NULL;
-	if (qe->chan->ani)
-		tmp->chan->ani = strdup(qe->chan->ani);
-	else
-		tmp->chan->ani = NULL;
+	if (tmp->chan->cid.cid_num)
+		free(tmp->chan->cid.cid_num);
+	tmp->chan->cid.cid_num = NULL;
+	if (tmp->chan->cid.cid_name)
+		free(tmp->chan->cid.cid_name);
+	tmp->chan->cid.cid_name = NULL;
+	if (tmp->chan->cid.cid_ani)
+		free(tmp->chan->cid.cid_ani);
+	tmp->chan->cid.cid_ani = NULL;
+	if (qe->chan->cid.cid_num)
+		tmp->chan->cid.cid_num = strdup(qe->chan->cid.cid_num);
+	if (qe->chan->cid.cid_name)
+		tmp->chan->cid.cid_name = strdup(qe->chan->cid.cid_name);
+	if (qe->chan->cid.cid_ani)
+		tmp->chan->cid.cid_ani = strdup(qe->chan->cid.cid_ani);
 	/* Presense of ADSI CPE on outgoing channel follows ours */
 	tmp->chan->adsicpe = qe->chan->adsicpe;
 	/* Place the call, but don't wait on the answer */
@@ -601,11 +607,13 @@ static int ring_entry(struct queue_ent *qe, struct localuser *tmp)
 						"AgentCalled: %s/%s\r\n"
 						"ChannelCalling: %s\r\n"
 						"CallerID: %s\r\n"
+						"CallerIDName: %s\r\n"
 						"Context: %s\r\n"
 						"Extension: %s\r\n"
 						"Priority: %d\r\n",
 						tmp->tech, tmp->numsubst, qe->chan->name,
-						tmp->chan->callerid ? tmp->chan->callerid : "unknown <>",
+						tmp->chan->cid.cid_num ? tmp->chan->cid.cid_num : "unknown",
+						tmp->chan->cid.cid_name ? tmp->chan->cid.cid_name : "unknown",
 						qe->chan->context, qe->chan->exten, qe->chan->priority);
 		}
 		if (option_verbose > 2)
@@ -700,7 +708,7 @@ static int valid_exit(struct queue_ent *qe, char digit)
 		return 0;
 	tmp[0] = digit;
 	tmp[1] = '\0';
-	if (ast_exists_extension(qe->chan, qe->context, tmp, 1, qe->chan->callerid)) {
+	if (ast_exists_extension(qe->chan, qe->context, tmp, 1, qe->chan->cid.cid_num)) {
 		strncpy(qe->chan->context, qe->context, sizeof(qe->chan->context) - 1);
 		strncpy(qe->chan->exten, tmp, sizeof(qe->chan->exten) - 1);
 		qe->chan->priority = 0;
@@ -1410,7 +1418,7 @@ static int rqm_exec(struct ast_channel *chan, void *data)
 		break;
 	case RES_EXISTS:
 		ast_log(LOG_WARNING, "Unable to remove interface '%s' from queue '%s': Not there\n", interface, queuename);
-		if (ast_exists_extension(chan, chan->context, chan->exten, chan->priority + 101, chan->callerid)) {
+		if (ast_exists_extension(chan, chan->context, chan->exten, chan->priority + 101, chan->cid.cid_num)) {
 			chan->priority += 100;
 		}
 		res = 0;
@@ -1487,7 +1495,7 @@ static int aqm_exec(struct ast_channel *chan, void *data)
 		break;
 	case RES_EXISTS:
 		ast_log(LOG_WARNING, "Unable to add interface '%s' to queue '%s': Already there\n", interface, queuename);
-		if (ast_exists_extension(chan, chan->context, chan->exten, chan->priority + 101, chan->callerid)) {
+		if (ast_exists_extension(chan, chan->context, chan->exten, chan->priority + 101, chan->cid.cid_num)) {
 			chan->priority += 100;
 		}
 		res = 0;
@@ -1598,7 +1606,7 @@ static int queue_exec(struct ast_channel *chan, void *data)
 	qe.last_pos_said = 0;
 	qe.last_pos = 0;
 	if (!join_queue(queuename, &qe)) {
-		ast_queue_log(queuename, chan->uniqueid, "NONE", "ENTERQUEUE", "%s|%s", url ? url : "", chan->callerid ? chan->callerid : "");
+		ast_queue_log(queuename, chan->uniqueid, "NONE", "ENTERQUEUE", "%s|%s", url ? url : "", chan->cid.cid_num ? chan->cid.cid_num : "");
 		/* Start music on hold */
 check_turns:
 		if (ringing) {
@@ -2108,10 +2116,14 @@ static int manager_queues_status( struct mansession *s, struct message *m )
 				"Position: %d\r\n"
 				"Channel: %s\r\n"
 				"CallerID: %s\r\n"
+				"CallerIDName: %s\r\n"
 				"Wait: %ld\r\n"
 				"%s"
 				"\r\n", 
-					q->name, pos++, qe->chan->name, (qe->chan->callerid ? qe->chan->callerid : ""), (long)(now - qe->start), idText);
+					q->name, pos++, qe->chan->name, 
+					qe->chan->cid.cid_num ? qe->chan->cid.cid_num : "unknown",
+					qe->chan->cid.cid_name ? qe->chan->cid.cid_name : "unknown",
+					(long)(now - qe->start), idText);
 		ast_mutex_unlock(&s->lock);
 		ast_mutex_unlock(&q->lock);
 	}
diff --git a/apps/app_sendtext.c b/apps/app_sendtext.c
index 73d24a1897..b8f537fa9c 100755
--- a/apps/app_sendtext.c
+++ b/apps/app_sendtext.c
@@ -3,9 +3,9 @@
  *
  * App to transmit a text message
  * 
- * Copyright (C) 1999, Mark Spencer
+ * Copyright (C) 1999-2004, Digium, Inc.
  *
- * Mark Spencer <markster@linux-support.net>
+ * Mark Spencer <markster@digium.com>
  *
  * This program is free software, distributed under the terms of
  * the GNU General Public License
@@ -54,7 +54,7 @@ static int sendtext_exec(struct ast_channel *chan, void *data)
 	if (!chan->pvt->send_text) {
 		ast_mutex_unlock(&chan->lock);
 		/* Does not support transport */
-		if (ast_exists_extension(chan, chan->context, chan->exten, chan->priority + 101, chan->callerid))
+		if (ast_exists_extension(chan, chan->context, chan->exten, chan->priority + 101, chan->cid.cid_num))
 			chan->priority += 100;
 		LOCAL_USER_REMOVE(u);
 		return 0;
diff --git a/apps/app_setcallerid.c b/apps/app_setcallerid.c
index 069c2af980..100fb813c0 100755
--- a/apps/app_setcallerid.c
+++ b/apps/app_setcallerid.c
@@ -89,7 +89,7 @@ static int setcallerid_pres_exec(struct ast_channel *chan, void *data)
 		return 0;
 	}
 	LOCAL_USER_ADD(u);
-	chan->callingpres = pres;
+	chan->cid.cid_pres = pres;
 	LOCAL_USER_REMOVE(u);
 	return res;
 }
@@ -110,6 +110,8 @@ static int setcallerid_exec(struct ast_channel *chan, void *data)
 {
 	int res = 0;
 	char tmp[256] = "";
+	char name[256];
+	char num[256];
 	struct localuser *u;
 	char *opt;
 	int anitoo = 0;
@@ -123,7 +125,8 @@ static int setcallerid_exec(struct ast_channel *chan, void *data)
 			anitoo = 1;
 	}
 	LOCAL_USER_ADD(u);
-	ast_set_callerid(chan, strlen(tmp) ? tmp : NULL, anitoo);
+	ast_callerid_split(tmp, name, sizeof(name), num, sizeof(num));
+	ast_set_callerid(chan, num, name, anitoo ? num : NULL);
 	LOCAL_USER_REMOVE(u);
 	return res;
 }
diff --git a/apps/app_setcidname.c b/apps/app_setcidname.c
index ee430911be..44b9dbd042 100755
--- a/apps/app_setcidname.c
+++ b/apps/app_setcidname.c
@@ -34,7 +34,7 @@ static char *descrip =
 "  SetCIDName(cname[|a]): Set Caller*ID Name on a call to a new\n"
 "value, while preserving the original Caller*ID number.  This is\n"
 "useful for providing additional information to the called\n"
-"party. Sets ANI as well if a flag is used.  Always returns 0\n";
+"party. Always returns 0\n";
 
 STANDARD_LOCAL_USER;
 
@@ -44,36 +44,16 @@ static int setcallerid_exec(struct ast_channel *chan, void *data)
 {
 	int res = 0;
 	char tmp[256] = "";
-	char oldcid[256] = "", *l, *n;
-	char newcid[256] = "";
 	struct localuser *u;
 	char *opt;
-	int anitoo = 0;
 	if (data)
 		strncpy(tmp, (char *)data, sizeof(tmp) - 1);
 	opt = strchr(tmp, '|');
 	if (opt) {
 		*opt = '\0';
-		opt++;
-		if (*opt == 'a')
-			anitoo = 1;
 	}
 	LOCAL_USER_ADD(u);
-	if (chan->callerid) {
-		strncpy(oldcid, chan->callerid, sizeof(oldcid) - 1);
-		ast_callerid_parse(oldcid, &n, &l);
-		n = tmp;
-		if (!ast_strlen_zero(n)) {
-			if (l && !ast_strlen_zero(l))
-				snprintf(newcid, sizeof(newcid), "\"%s\" <%s>", n, l);
-			else
-				strncpy(newcid, tmp, sizeof(newcid) - 1);
-		} else if (l && !ast_strlen_zero(l)) {
-			strncpy(newcid, l, sizeof(newcid) - 1);
-		}
-	} else
-		strncpy(newcid, tmp, sizeof(newcid) - 1);
-	ast_set_callerid(chan, !ast_strlen_zero(newcid) ? newcid : NULL, anitoo);
+	ast_set_callerid(chan, NULL, tmp, NULL);
 	LOCAL_USER_REMOVE(u);
 	return res;
 }
diff --git a/apps/app_setcidnum.c b/apps/app_setcidnum.c
index b96330ec5a..1c9d0003d8 100755
--- a/apps/app_setcidnum.c
+++ b/apps/app_setcidnum.c
@@ -3,9 +3,9 @@
  *
  * App to set callerid
  * 
- * Copyright (C) 1999, Mark Spencer
+ * Copyright (C) 1999-2004, Digium, Inc.
  *
- * Mark Spencer <markster@linux-support.net>
+ * Mark Spencer <markster@digium.com>
  * Oliver Daudey <traveler@xs4all.nl>
  *
  * This program is free software, distributed under the terms of
@@ -44,12 +44,10 @@ LOCAL_USER_DECL;
 static int setcallerid_exec(struct ast_channel *chan, void *data)
 {
 	int res = 0;
-	char tmp[256] = "";
-	char oldcid[256] = "", *l, *n;
-	char newcid[256] = "";
 	struct localuser *u;
 	char *opt;
 	int anitoo = 0;
+	char tmp[256];
 	if (data)
 		strncpy(tmp, (char *)data, sizeof(tmp) - 1);
 	opt = strchr(tmp, '|');
@@ -60,21 +58,7 @@ static int setcallerid_exec(struct ast_channel *chan, void *data)
 			anitoo = 1;
 	}
 	LOCAL_USER_ADD(u);
-	if (chan->callerid) {
-		strncpy(oldcid, chan->callerid, sizeof(oldcid) - 1);
-		ast_callerid_parse(oldcid, &n, &l);
-		l = tmp;
-		if (!ast_strlen_zero(l)) {
-			if (n && !ast_strlen_zero(n))
-				snprintf(newcid, sizeof(newcid), "\"%s\" <%s>", n, l);
-			else
-				strncpy(newcid, tmp, sizeof(newcid) - 1);
-		} else if (n && !ast_strlen_zero(n)) {
-			strncpy(newcid, n, sizeof(newcid) - 1);
-		}
-	} else
-		strncpy(newcid, tmp, sizeof(newcid) - 1);
-	ast_set_callerid(chan, !ast_strlen_zero(newcid) ? newcid : NULL, anitoo);
+	ast_set_callerid(chan, tmp, NULL, anitoo ? tmp : NULL);
 	LOCAL_USER_REMOVE(u);
 	return res;
 }
diff --git a/apps/app_sms.c b/apps/app_sms.c
index a7b55bd463..3a4d5392eb 100755
--- a/apps/app_sms.c
+++ b/apps/app_sms.c
@@ -1050,17 +1050,8 @@ sms_exec(struct ast_channel *chan, void *data)
 		return -1;
 	}
 
-	if (chan->callerid) {
-		/* get caller ID. Used as originating address on sc side receives */
-		char temp[256], *name, *num;
-		strncpy (temp, chan->callerid, sizeof(temp) - 1);
-		ast_callerid_parse (temp, &name, &num);
-		if (!num)
-			num = temp;
-		ast_shrink_phone_number (num);
-		if (strlen (num) < sizeof (h.cli))
-			strncpy(h.cli, num, sizeof(h.cli) - 1);
-	}
+	if (chan->cid.cid_num) 
+		strncpy(h.cli, chan->cid.cid_num, sizeof(h.cli) - 1);
 
 	{
 		char *d = data, *p, answer = 0;
diff --git a/apps/app_system.c b/apps/app_system.c
index 89bddc3e37..d494b9eff9 100755
--- a/apps/app_system.c
+++ b/apps/app_system.c
@@ -72,7 +72,7 @@ static int system_exec_helper(struct ast_channel *chan, void *data, int failmode
 	} else {
 		if (res < 0)
 			res = 0;
-		if (res && ast_exists_extension(chan, chan->context, chan->exten, chan->priority + 101, chan->callerid)) 
+		if (res && ast_exists_extension(chan, chan->context, chan->exten, chan->priority + 101, chan->cid.cid_num)) 
 			chan->priority+=100;
 		res = 0;
 	}
diff --git a/apps/app_talkdetect.c b/apps/app_talkdetect.c
index 0816949af8..5d15d94d8a 100755
--- a/apps/app_talkdetect.c
+++ b/apps/app_talkdetect.c
@@ -124,7 +124,7 @@ static int background_detect_exec(struct ast_channel *chan, void *data)
 						char t[2];
 						t[0] = fr->subclass;
 						t[1] = '\0';
-						if (ast_canmatch_extension(chan, chan->context, t, 1, chan->callerid)) {
+						if (ast_canmatch_extension(chan, chan->context, t, 1, chan->cid.cid_num)) {
 							/* They entered a valid  extension, or might be anyhow */
 							res = fr->subclass;
 							ast_frfree(fr);
@@ -146,7 +146,7 @@ static int background_detect_exec(struct ast_channel *chan, void *data)
 									ms = 0;
 								if ((ms > min) && ((max < 0) || (ms < max))) {
 									ast_log(LOG_DEBUG, "Found qualified token of %d ms\n", ms);
-									if (ast_exists_extension(chan, chan->context, "talk", 1, chan->callerid)) {
+									if (ast_exists_extension(chan, chan->context, "talk", 1, chan->cid.cid_num)) {
 										strncpy(chan->exten, "talk", sizeof(chan->exten) -1 );
 										chan->priority = 0;
 									}
diff --git a/apps/app_transfer.c b/apps/app_transfer.c
index 826c93c05a..31464372e7 100755
--- a/apps/app_transfer.c
+++ b/apps/app_transfer.c
@@ -3,9 +3,9 @@
  *
  * Transfer a caller
  * 
- * Copyright (C) 1999, Mark Spencer
+ * Copyright (C) 1999-2004, Digium, Inc.
  *
- * Mark Spencer <markster@linux-support.net>
+ * Mark Spencer <markster@digium.com>
  *
  * This program is free software, distributed under the terms of
  * the GNU General Public License
@@ -53,7 +53,7 @@ static int transfer_exec(struct ast_channel *chan, void *data)
 	}
 	if (!res) {
 		/* Look for a "busy" place */
-		if (ast_exists_extension(chan, chan->context, chan->exten, chan->priority + 101, chan->callerid))
+		if (ast_exists_extension(chan, chan->context, chan->exten, chan->priority + 101, chan->cid.cid_num))
 			chan->priority += 100;
 	}
 	if (res > 0)
diff --git a/apps/app_txtcidname.c b/apps/app_txtcidname.c
index 418f158017..e9fc107fab 100755
--- a/apps/app_txtcidname.c
+++ b/apps/app_txtcidname.c
@@ -75,7 +75,7 @@ static int txtcidname_exec(struct ast_channel *chan, void *data)
 	}
 	if (!res) {
 		/* Look for a "busy" place */
-		if (ast_exists_extension(chan, chan->context, chan->exten, chan->priority + 101, chan->callerid))
+		if (ast_exists_extension(chan, chan->context, chan->exten, chan->priority + 101, chan->cid.cid_num))
 			chan->priority += 100;
 	} else if (res > 0)
 		res = 0;
diff --git a/apps/app_url.c b/apps/app_url.c
index fdaa270eaf..a1ea624d30 100755
--- a/apps/app_url.c
+++ b/apps/app_url.c
@@ -3,9 +3,9 @@
  *
  * App to transmit a URL
  * 
- * Copyright (C) 1999, Mark Spencer
+ * Copyright (C) 1999-2004, Digium, Inc.
  *
- * Mark Spencer <markster@linux-support.net>
+ * Mark Spencer <markster@digium.com>
  *
  * This program is free software, distributed under the terms of
  * the GNU General Public License
@@ -65,7 +65,7 @@ static int sendurl_exec(struct ast_channel *chan, void *data)
 	LOCAL_USER_ADD(u);
 	if (!ast_channel_supports_html(chan)) {
 		/* Does not support transport */
-		if (ast_exists_extension(chan, chan->context, chan->exten, chan->priority + 101, chan->callerid))
+		if (ast_exists_extension(chan, chan->context, chan->exten, chan->priority + 101, chan->cid.cid_num))
 			chan->priority += 100;
 		LOCAL_USER_REMOVE(u);
 		return 0;
@@ -92,7 +92,7 @@ static int sendurl_exec(struct ast_channel *chan, void *data)
 						break;
 					case AST_HTML_NOSUPPORT:
 						/* Does not support transport */
-						if (ast_exists_extension(chan, chan->context, chan->exten, chan->priority + 101, chan->callerid))
+						if (ast_exists_extension(chan, chan->context, chan->exten, chan->priority + 101, chan->cid.cid_num))
 							chan->priority += 100;
 						res = 0;
 						goto out;
diff --git a/apps/app_voicemail.c b/apps/app_voicemail.c
index fef41ac747..94ecb60250 100755
--- a/apps/app_voicemail.c
+++ b/apps/app_voicemail.c
@@ -830,19 +830,22 @@ static int base_encode(char *filename, FILE *so)
 	return 1;
 }
 
-static void prep_email_sub_vars(struct ast_channel *ast, struct ast_vm_user *vmu, int msgnum, char *mailbox, char *callerid, char *dur, char *date, char *passdata, size_t passdatasize)
+static void prep_email_sub_vars(struct ast_channel *ast, struct ast_vm_user *vmu, int msgnum, char *mailbox, char *cidnum, char *cidname, char *dur, char *date, char *passdata, size_t passdatasize)
 {
+	char callerid[256];
 	/* Prepare variables for substition in email body and subject */
 	pbx_builtin_setvar_helper(ast, "VM_NAME", vmu->fullname);
 	pbx_builtin_setvar_helper(ast, "VM_DUR", dur);
 	snprintf(passdata, passdatasize, "%d", msgnum);
 	pbx_builtin_setvar_helper(ast, "VM_MSGNUM", passdata);
 	pbx_builtin_setvar_helper(ast, "VM_MAILBOX", mailbox);
-	pbx_builtin_setvar_helper(ast, "VM_CALLERID", (callerid ? callerid : "an unknown caller"));
+	pbx_builtin_setvar_helper(ast, "VM_CALLERID", ast_callerid_merge(callerid, sizeof(callerid), cidname, cidnum));
+	pbx_builtin_setvar_helper(ast, "VM_CIDNAME", (cidname ? cidname : "an unknown caller"));
+	pbx_builtin_setvar_helper(ast, "VM_CIDNUM", (cidnum ? cidnum : "an unknown caller"));
 	pbx_builtin_setvar_helper(ast, "VM_DATE", date);
 }
 
-static int sendmail(char *srcemail, struct ast_vm_user *vmu, int msgnum, char *mailbox, char *callerid, char *attach, char *format, int duration, int attach_user_voicemail)
+static int sendmail(char *srcemail, struct ast_vm_user *vmu, int msgnum, char *mailbox, char *cidnum, char *cidname, char *attach, char *format, int duration, int attach_user_voicemail)
 {
 	FILE *p=NULL;
 	int pfd;
@@ -912,7 +915,7 @@ static int sendmail(char *srcemail, struct ast_vm_user *vmu, int msgnum, char *m
 				int vmlen = strlen(fromstring)*3 + 200;
 				if ((passdata = alloca(vmlen))) {
 					memset(passdata, 0, vmlen);
-					prep_email_sub_vars(ast,vmu,msgnum + 1,mailbox,callerid,dur,date,passdata, vmlen);
+					prep_email_sub_vars(ast,vmu,msgnum + 1,mailbox,cidnum, cidname,dur,date,passdata, vmlen);
 					pbx_substitute_variables_helper(ast,fromstring,passdata,vmlen);
 					fprintf(p, "From: %s <%s>\n",passdata,who);
 				} else ast_log(LOG_WARNING, "Cannot allocate workspace for variable substitution\n");
@@ -929,7 +932,7 @@ static int sendmail(char *srcemail, struct ast_vm_user *vmu, int msgnum, char *m
 				int vmlen = strlen(emailsubject)*3 + 200;
 				if ((passdata = alloca(vmlen))) {
 					memset(passdata, 0, vmlen);
-					prep_email_sub_vars(ast,vmu,msgnum + 1,mailbox,callerid,dur,date,passdata, vmlen);
+					prep_email_sub_vars(ast,vmu,msgnum + 1,mailbox,cidnum, cidname,dur,date,passdata, vmlen);
 					pbx_substitute_variables_helper(ast,emailsubject,passdata,vmlen);
 					fprintf(p, "Subject: %s\n",passdata);
 				} else ast_log(LOG_WARNING, "Cannot allocate workspace for variable substitution\n");
@@ -965,7 +968,7 @@ static int sendmail(char *srcemail, struct ast_vm_user *vmu, int msgnum, char *m
 				int vmlen = strlen(emailbody)*3 + 200;
 				if ((passdata = alloca(vmlen))) {
 					memset(passdata, 0, vmlen);
-					prep_email_sub_vars(ast,vmu,msgnum + 1,mailbox,callerid,dur,date,passdata, vmlen);
+					prep_email_sub_vars(ast,vmu,msgnum + 1,mailbox,cidnum, cidname,dur,date,passdata, vmlen);
 					pbx_substitute_variables_helper(ast,emailbody,passdata,vmlen);
 					fprintf(p, "%s\n",passdata);
 				} else ast_log(LOG_WARNING, "Cannot allocate workspace for variable substitution\n");
@@ -976,7 +979,7 @@ static int sendmail(char *srcemail, struct ast_vm_user *vmu, int msgnum, char *m
 
 			"in mailbox %s from %s, on %s so you might\n"
 			"want to check it when you get a chance.  Thanks!\n\n\t\t\t\t--Asterisk\n\n", vmu->fullname, 
-			dur, msgnum + 1, mailbox, (callerid ? callerid : "an unknown caller"), date);
+			dur, msgnum + 1, mailbox, (cidname ? cidname : (cidnum ? cidnum : "an unknown caller")), date);
 		}
 		if (attach_user_voicemail) {
 			fprintf(p, "--%s\n", bound);
@@ -1000,7 +1003,7 @@ static int sendmail(char *srcemail, struct ast_vm_user *vmu, int msgnum, char *m
 	return 0;
 }
 
-static int sendpage(char *srcemail, char *pager, int msgnum, char *mailbox, char *callerid, int duration, struct ast_vm_user *vmu)
+static int sendpage(char *srcemail, char *pager, int msgnum, char *mailbox, char *cidnum, char *cidname, int duration, struct ast_vm_user *vmu)
 {
 	FILE *p=NULL;
 	int pfd;
@@ -1062,7 +1065,7 @@ static int sendpage(char *srcemail, char *pager, int msgnum, char *mailbox, char
 				int vmlen = strlen(fromstring)*3 + 200;
 				if ((passdata = alloca(vmlen))) {
 					memset(passdata, 0, vmlen);
-					prep_email_sub_vars(ast,vmu,msgnum + 1,mailbox,callerid,dur,date,passdata, vmlen);
+					prep_email_sub_vars(ast,vmu,msgnum + 1,mailbox,cidnum, cidname,dur,date,passdata, vmlen);
 					pbx_substitute_variables_helper(ast,pagerfromstring,passdata,vmlen);
 					fprintf(p, "From: %s <%s>\n",passdata,who);
 				} else 
@@ -1075,7 +1078,7 @@ static int sendpage(char *srcemail, char *pager, int msgnum, char *mailbox, char
 		fprintf(p, "Subject: New VM\n\n");
 		strftime(date, sizeof(date), "%A, %B %d, %Y at %r", &tm);
 		fprintf(p, "New %s long msg in box %s\n"
-		           "from %s, on %s", dur, mailbox, (callerid ? callerid : "unknown"), date);
+		           "from %s, on %s", dur, mailbox, (cidname ? cidname : (cidnum ? cidnum : "unknown")), date);
 		fclose(p);
 		snprintf(tmp2, sizeof(tmp2), "( %s < %s ; rm -f %s ) &", mailcmd, tmp, tmp);
 		ast_safe_system(tmp2);
@@ -1218,7 +1221,7 @@ static int copy(char *infile, char *outfile)
 #endif
 }
 
-static int notify_new_message(struct ast_channel *chan, struct ast_vm_user *vmu, int msgnum, long duration, char *fmt, char *callerid);
+static int notify_new_message(struct ast_channel *chan, struct ast_vm_user *vmu, int msgnum, long duration, char *fmt, char *cidnum, char *cidname);
 
 static void copy_message(struct ast_channel *chan, struct ast_vm_user *vmu, int imbox, int msgnum, long duration, struct ast_vm_user *recip, char *fmt)
 {
@@ -1259,7 +1262,7 @@ static void copy_message(struct ast_channel *chan, struct ast_vm_user *vmu, int
 		ast_log(LOG_ERROR, "Recipient mailbox %s@%s is full\n", recip->mailbox, recip->context);
 	}
 
-	notify_new_message(chan, recip, recipmsgnum, duration, fmt, chan->callerid);
+	notify_new_message(chan, recip, recipmsgnum, duration, fmt, chan->cid.cid_num, chan->cid.cid_name);
 }
 
 static void run_externnotify(char *context, char *extension)
@@ -1282,6 +1285,7 @@ static void run_externnotify(char *context, char *extension)
 static int leave_voicemail(struct ast_channel *chan, char *ext, int silent, int busy, int unavail)
 {
 	char txtfile[256];
+	char callerid[256];
 	FILE *txt;
 	int res = 0;
 	int msgnum;
@@ -1341,21 +1345,21 @@ static int leave_voicemail(struct ast_channel *chan, char *ext, int silent, int
 
 		/* Check current or macro-calling context for special extensions */
 		if (!ast_strlen_zero(vmu->exit)) {
-			if (ast_exists_extension(chan, vmu->exit, "o", 1, chan->callerid))
+			if (ast_exists_extension(chan, vmu->exit, "o", 1, chan->cid.cid_num))
 				strncat(ecodes, "0", sizeof(ecodes) - strlen(ecodes) - 1);
-		} else if (ast_exists_extension(chan, chan->context, "o", 1, chan->callerid))
+		} else if (ast_exists_extension(chan, chan->context, "o", 1, chan->cid.cid_num))
 			strncat(ecodes, "0", sizeof(ecodes) - strlen(ecodes) - 1);
-		else if (!ast_strlen_zero(chan->macrocontext) && ast_exists_extension(chan, chan->macrocontext, "o", 1, chan->callerid)) {
+		else if (!ast_strlen_zero(chan->macrocontext) && ast_exists_extension(chan, chan->macrocontext, "o", 1, chan->cid.cid_num)) {
 			strncat(ecodes, "0", sizeof(ecodes) - strlen(ecodes) - 1);
 			ousemacro = 1;
 		}
 
 		if (!ast_strlen_zero(vmu->exit)) {
-			if (ast_exists_extension(chan, vmu->exit, "a", 1, chan->callerid))
+			if (ast_exists_extension(chan, vmu->exit, "a", 1, chan->cid.cid_num))
 				strncat(ecodes, "*", sizeof(ecodes) -  strlen(ecodes) - 1);
-		} else if (ast_exists_extension(chan, chan->context, "a", 1, chan->callerid))
+		} else if (ast_exists_extension(chan, chan->context, "a", 1, chan->cid.cid_num))
 			strncat(ecodes, "*", sizeof(ecodes) -  strlen(ecodes) - 1);
-		else if (!ast_strlen_zero(chan->macrocontext) && ast_exists_extension(chan, chan->macrocontext, "a", 1, chan->callerid)) {
+		else if (!ast_strlen_zero(chan->macrocontext) && ast_exists_extension(chan, chan->macrocontext, "a", 1, chan->cid.cid_num)) {
 			strncat(ecodes, "*", sizeof(ecodes) -  strlen(ecodes) - 1);
 			ausemacro = 1;
 		}
@@ -1463,7 +1467,7 @@ static int leave_voicemail(struct ast_channel *chan, char *ext, int silent, int
 	chan->exten,
 	chan->priority,
 	chan->name,
-	chan->callerid ? chan->callerid : "Unknown",
+	ast_callerid_merge(callerid, sizeof(callerid), chan->cid.cid_name, chan->cid.cid_num),
 	date, (long)time(NULL));
 					fclose(txt);
 				} else
@@ -1505,7 +1509,7 @@ static int leave_voicemail(struct ast_channel *chan, char *ext, int silent, int
 						free_user(recip);
 					}
 				}
-				notify_new_message(chan, vmu, msgnum, duration, fmt, chan->callerid);
+				notify_new_message(chan, vmu, msgnum, duration, fmt, chan->cid.cid_num, chan->cid.cid_name);
 			} else {
 				res = ast_streamfile(chan, "vm-mailboxfull", chan->language);
 				if (!res)
@@ -1519,7 +1523,7 @@ leave_vm_out:
 	} else {
 		ast_log(LOG_WARNING, "No entry in voicemail config file for '%s'\n", ext);
 		/*Send the call to n+101 priority, where n is the current priority*/
-		if (ast_exists_extension(chan, chan->context, chan->exten, chan->priority + 101, chan->callerid))
+		if (ast_exists_extension(chan, chan->context, chan->exten, chan->priority + 101, chan->cid.cid_num))
 			chan->priority+=100;
 	}
 
@@ -2158,7 +2162,7 @@ static int vm_forwardoptions(struct ast_channel *chan, struct ast_vm_user *vmu,
 	return cmd;
 }
 
-static int notify_new_message(struct ast_channel *chan, struct ast_vm_user *vmu, int msgnum, long duration, char *fmt, char *callerid)
+static int notify_new_message(struct ast_channel *chan, struct ast_vm_user *vmu, int msgnum, long duration, char *fmt, char *cidnum, char *cidname)
 {
 	char todir[256], fn[256], ext_context[256], *stringp;
 
@@ -2179,14 +2183,14 @@ static int notify_new_message(struct ast_channel *chan, struct ast_vm_user *vmu,
 				attach_user_voicemail = vmu->attach;
 			if (!ast_strlen_zero(vmu->serveremail))
 				myserveremail = vmu->serveremail;
-			sendmail(myserveremail, vmu, msgnum, vmu->mailbox, callerid, fn, fmt, duration, attach_user_voicemail);
+			sendmail(myserveremail, vmu, msgnum, vmu->mailbox, cidnum, cidname, fn, fmt, duration, attach_user_voicemail);
 		}
 
 		if (!ast_strlen_zero(vmu->pager)) {
 			char *myserveremail = serveremail;
 			if (!ast_strlen_zero(vmu->serveremail))
 				myserveremail = vmu->serveremail;
-			sendpage(myserveremail, vmu->pager, msgnum, vmu->mailbox, callerid, duration, vmu);
+			sendpage(myserveremail, vmu->pager, msgnum, vmu->mailbox, cidnum, cidname, duration, vmu);
 		}
 	} else {
 		ast_log(LOG_ERROR, "Out of memory\n");
@@ -2312,14 +2316,14 @@ static int forward_message(struct ast_channel *chan, char *context, char *dir, i
 							attach_user_voicemail = vmtmp->attach;
 						if (!ast_strlen_zero(vmtmp->serveremail))
 							myserveremail = vmtmp->serveremail;
-						sendmail(myserveremail, vmtmp, todircount, vmtmp->mailbox, callerid, fn, tmp, duration, attach_user_voicemail);
+						sendmail(myserveremail, vmtmp, todircount, vmtmp->mailbox, chan->cid.cid_num, chan->cid.cid_name, fn, tmp, duration, attach_user_voicemail);
 					}
 			     
 					if (!ast_strlen_zero(vmtmp->pager)) {
 						char *myserveremail = serveremail;
 						if (!ast_strlen_zero(vmtmp->serveremail))
 							myserveremail = vmtmp->serveremail;
-						sendpage(myserveremail, vmtmp->pager, todircount, vmtmp->mailbox, callerid, duration, vmtmp);
+						sendpage(myserveremail, vmtmp->pager, todircount, vmtmp->mailbox, chan->cid.cid_num, chan->cid.cid_name, duration, vmtmp);
 					}
 				  
 					ast_destroy(mif); /* or here */
@@ -3177,7 +3181,6 @@ static int vm_execmain(struct ast_channel *chan, void *data)
 	struct ast_vm_user *vmu = NULL, vmus;
 	char *context=NULL;
 	int silentexit = 0;
-	char cid[256]="";
 	char *passptr;
 
 	LOCAL_USER_ADD(u);
@@ -3239,21 +3242,14 @@ static int vm_execmain(struct ast_channel *chan, void *data)
 			goto out;
 		}
 		if (ast_strlen_zero(vms.username)) {
-				char *callerid=NULL, *name=NULL;
-				if(chan->callerid != NULL) {
-					strncpy(cid, chan->callerid, sizeof(cid) - 1);
-					ast_callerid_parse(cid, &name, &callerid);
-				}
-				if(callerid != NULL) {			
-					if (option_verbose > 2)
-					ast_verbose(VERBOSE_PREFIX_3 "No username but # key pressed. Using CID '%s'\n",callerid);
-					strncpy(vms.username, callerid, sizeof(vms.username) - 1);
-				} else {
-					if (option_verbose > 2)
+			if (chan->cid.cid_num) {
+				strncpy(vms.username, chan->cid.cid_num, sizeof(vms.username) - 1);
+			} else {
+				if (option_verbose > 2)
 					ast_verbose(VERBOSE_PREFIX_3 "Username not entered\n");	
-					res = 0;
-					goto out;
-				}
+				res = 0;
+				goto out;
+			}
 		}
 		if (useadsi)
 			adsi_password(chan);
@@ -3746,7 +3742,7 @@ static int vm_box_exists(struct ast_channel *chan, void *data) {
 	}
 
 	if (find_user(&svm, context, box)) {
-		if (ast_exists_extension(chan, chan->context, chan->exten, chan->priority + 101, chan->callerid)) {
+		if (ast_exists_extension(chan, chan->context, chan->exten, chan->priority + 101, chan->cid.cid_num)) {
 			chan->priority += 100;
 		} else
 			ast_log(LOG_WARNING, "VM box %s@%s exists, but extension %s, priority %d doesn't exist\n", box, context, chan->exten, chan->priority + 101);
diff --git a/apps/app_zapateller.c b/apps/app_zapateller.c
index 42777779c5..8c6183b825 100755
--- a/apps/app_zapateller.c
+++ b/apps/app_zapateller.c
@@ -3,9 +3,9 @@
  *
  * Playback the special information tone to get rid of telemarketers
  * 
- * Copyright (C) 1999, Mark Spencer
+ * Copyright (C) 1999-2004, Digium, Inc.
  *
- * Mark Spencer <markster@linux-support.net>
+ * Mark Spencer <markster@digium.com>
  *
  * This program is free software, distributed under the terms of
  * the GNU General Public License
@@ -70,7 +70,7 @@ static int zapateller_exec(struct ast_channel *chan, void *data)
 			res = ast_safe_sleep(chan, 500);
 		}
 	}
-	if (chan->callerid && nocallerid) {
+	if (chan->cid.cid_num && nocallerid) {
 		LOCAL_USER_REMOVE(u);
 		return res;
 	} 
diff --git a/callerid.c b/callerid.c
index 5481676ecd..d88822a2ad 100755
--- a/callerid.c
+++ b/callerid.c
@@ -24,6 +24,7 @@
 #include <asterisk/ulaw.h>
 #include <asterisk/alaw.h>
 #include <asterisk/frame.h>
+#include <asterisk/channel.h>
 #include <asterisk/callerid.h>
 #include <asterisk/logger.h>
 #include <asterisk/fskmodem.h>
@@ -638,30 +639,53 @@ int ast_callerid_parse(char *instr, char **name, char **location)
 	return -1;
 }
 
-static int __ast_callerid_generate(unsigned char *buf, char *callerid, int callwaiting, int codec)
+static int __ast_callerid_generate(unsigned char *buf, char *name, char *number, int callwaiting, int codec)
 {
-	char tmp[256];
-	char *n, *l;
-	if (!callerid)
-		return callerid_generate(buf, NULL, NULL, 0, callwaiting, codec);
-	strncpy(tmp, callerid, sizeof(tmp)-1);
-	if (ast_callerid_parse(tmp, &n, &l)) {
-		ast_log(LOG_WARNING, "Unable to parse '%s' into CallerID name & number\n", callerid);
-		return callerid_generate(buf, NULL, NULL, 0, callwaiting, codec);
-	}
-	if (l)
-		ast_shrink_phone_number(l);
-	if (!ast_isphonenumber(l))
-		return callerid_generate(buf, NULL, n, 0, callwaiting, codec);
-	return callerid_generate(buf, l, n, 0, callwaiting, codec);
+	if (name && ast_strlen_zero(name))
+		name = NULL;
+	if (number && ast_strlen_zero(number))
+		number = NULL;
+	return callerid_generate(buf, number, name, 0, callwaiting, codec);
 }
 
-int ast_callerid_generate(unsigned char *buf, char *callerid, int codec)
+int ast_callerid_generate(unsigned char *buf, char *name, char *number, int codec)
 {
-	return __ast_callerid_generate(buf, callerid, 0, codec);
+	return __ast_callerid_generate(buf, name, number, 0, codec);
 }
 
-int ast_callerid_callwaiting_generate(unsigned char *buf, char *callerid, int codec)
+int ast_callerid_callwaiting_generate(unsigned char *buf, char *name, char *number, int codec)
+{
+	return __ast_callerid_generate(buf, name, number, 1, codec);
+}
+
+char *ast_callerid_merge(char *buf, int bufsiz, const char *name, const char *num)
+{
+	if (name && num)
+		snprintf(buf, bufsiz, "\"%s\" <%s>", name, num);
+	else if (name) 
+		strncpy(buf, name, bufsiz - 1);
+	else if (num)
+		strncpy(buf, num, bufsiz - 1);
+	else
+		strncpy(buf, "<unknown>", bufsiz - 1);
+	return buf;
+}
+int ast_callerid_split(const char *buf, char *name, int namelen, char *num, int numlen)
 {
-	return __ast_callerid_generate(buf, callerid, 1, codec);
+	char *tmp;
+	char *l = NULL, *n = NULL;
+	tmp = ast_strdupa(buf);
+	if (!tmp) {
+		name[0] = '\0';
+		num[0] = '\0';
+		return -1;
+	}
+	ast_callerid_parse(tmp, &n, &l);
+	if (n)
+		strncpy(name, n, namelen - 1);
+	if (l) {
+		ast_shrink_phone_number(l);
+		strncpy(num, l, numlen - 1);
+	}
+	return 0;
 }
diff --git a/cdr.c b/cdr.c
index f4e97efcec..4b9eec855a 100755
--- a/cdr.c
+++ b/cdr.c
@@ -251,23 +251,28 @@ void ast_cdr_setapp(struct ast_cdr *cdr, char *app, char *data)
 int ast_cdr_setcid(struct ast_cdr *cdr, struct ast_channel *c)
 {
 	char tmp[AST_MAX_EXTENSION] = "";
-	char *num, *name;
+	char *num;
 	while (cdr) {
 		if(!ast_cdr_has_flag(cdr,AST_CDR_FLAG_LOCKED)) {
 			/* Grab source from ANI or normal Caller*ID */
-			if (c->ani)
-				strncpy(tmp, c->ani, sizeof(tmp) - 1);
-			else if (c->callerid)
-				strncpy(tmp, c->callerid, sizeof(tmp) - 1);
-			if (c->callerid)
-				strncpy(cdr->clid, c->callerid, sizeof(cdr->clid) - 1);
-			name = NULL;
-			num = NULL;
-			ast_callerid_parse(tmp, &name, &num);
-			if (num) {
-				ast_shrink_phone_number(num);
+			if (c->cid.cid_ani)
+				num = c->cid.cid_ani;
+			else
+				num = c->cid.cid_num;
+			
+			if (c->cid.cid_name && num)
+				snprintf(tmp, sizeof(tmp), "\"%s\" <%s>", c->cid.cid_name, num);
+			else if (c->cid.cid_name)
+				strncpy(tmp, c->cid.cid_name, sizeof(tmp) - 1);
+			else if (num)
+				strncpy(tmp, num, sizeof(tmp) - 1);
+			else
+				strcpy(tmp, "");
+			strncpy(cdr->clid, tmp, sizeof(cdr->clid) - 1);
+			if (num)
 				strncpy(cdr->src, num, sizeof(cdr->src) - 1);
-			}
+			else
+				strcpy(cdr->src, "");
 		}
 		cdr = cdr->next;
 	}
@@ -277,7 +282,7 @@ int ast_cdr_setcid(struct ast_cdr *cdr, struct ast_channel *c)
 int ast_cdr_init(struct ast_cdr *cdr, struct ast_channel *c)
 {
 	char *chan;
-	char *num, *name;
+	char *num;
 	char tmp[AST_MAX_EXTENSION] = "";
 	while (cdr) {
 		if(!ast_cdr_has_flag(cdr,AST_CDR_FLAG_LOCKED)) {
@@ -286,20 +291,25 @@ int ast_cdr_init(struct ast_cdr *cdr, struct ast_channel *c)
 				ast_log(LOG_WARNING, "CDR already initialized on '%s'\n", chan); 
 			strncpy(cdr->channel, c->name, sizeof(cdr->channel) - 1);
 			/* Grab source from ANI or normal Caller*ID */
-			if (c->ani)
-				strncpy(tmp, c->ani, sizeof(tmp) - 1);
-			else if (c->callerid)
-				strncpy(tmp, c->callerid, sizeof(tmp) - 1);
-			if (c->callerid)
-				strncpy(cdr->clid, c->callerid, sizeof(cdr->clid) - 1);
-			name = NULL;
-			num = NULL;
-			ast_callerid_parse(tmp, &name, &num);
-			if (num) {
-				ast_shrink_phone_number(num);
-				strncpy(cdr->src, num, sizeof(cdr->src) - 1);
-			}
+			if (c->cid.cid_ani)
+				num = c->cid.cid_ani;
+			else
+				num = c->cid.cid_num;
 			
+			if (c->cid.cid_name && num)
+				snprintf(tmp, sizeof(tmp), "\"%s\" <%s>", c->cid.cid_name, num);
+			else if (c->cid.cid_name)
+				strncpy(tmp, c->cid.cid_name, sizeof(tmp) - 1);
+			else if (num)
+				strncpy(tmp, num, sizeof(tmp) - 1);
+			else
+				strcpy(tmp, "");
+			strncpy(cdr->clid, tmp, sizeof(cdr->clid) - 1);
+			if (num)
+				strncpy(cdr->src, num, sizeof(cdr->src) - 1);
+			else
+				strcpy(cdr->src, "");
+
 			if (c->_state == AST_STATE_UP)
 				cdr->disposition = AST_CDR_ANSWERED;
 			else
@@ -419,26 +429,31 @@ int ast_cdr_appenduserfield(struct ast_channel *chan, char *userfield)
 int ast_cdr_update(struct ast_channel *c)
 {
 	struct ast_cdr *cdr = c->cdr;
-	char *name, *num;
+	char *num;
 	char tmp[AST_MAX_EXTENSION] = "";
 	/* Grab source from ANI or normal Caller*ID */
 	while (cdr) {
 		if(!ast_cdr_has_flag(cdr,AST_CDR_FLAG_LOCKED)) {
-			if (c->ani)
-				strncpy(tmp, c->ani, sizeof(tmp) - 1);
-			else if (c->callerid && !ast_strlen_zero(c->callerid))
-				strncpy(tmp, c->callerid, sizeof(tmp) - 1);
-			if (c->callerid && !ast_strlen_zero(c->callerid))
-				strncpy(cdr->clid, c->callerid, sizeof(cdr->clid) - 1);
+			/* Grab source from ANI or normal Caller*ID */
+			if (c->cid.cid_ani)
+				num = c->cid.cid_ani;
 			else
-				cdr->clid[0] = '\0';
-			name = NULL;
-			num = NULL;
-			ast_callerid_parse(tmp, &name, &num);
-			if (num) {
-				ast_shrink_phone_number(num);
+				num = c->cid.cid_num;
+			
+			if (c->cid.cid_name && num)
+				snprintf(tmp, sizeof(tmp), "\"%s\" <%s>", c->cid.cid_name, num);
+			else if (c->cid.cid_name)
+				strncpy(tmp, c->cid.cid_name, sizeof(tmp) - 1);
+			else if (num)
+				strncpy(tmp, num, sizeof(tmp) - 1);
+			else
+				strcpy(tmp, "");
+			strncpy(cdr->clid, tmp, sizeof(cdr->clid) - 1);
+			if (num)
 				strncpy(cdr->src, num, sizeof(cdr->src) - 1);
-			}
+			else
+				strcpy(cdr->src, "");
+
 			/* Copy account code et-al */	
 			strncpy(cdr->accountcode, c->accountcode, sizeof(cdr->accountcode) - 1);
 			/* Destination information */
diff --git a/channel.c b/channel.c
index 2f6f28d7f4..cbcc213ad1 100755
--- a/channel.c
+++ b/channel.c
@@ -558,6 +558,20 @@ int ast_safe_sleep(struct ast_channel *chan, int ms)
 	return 0;
 }
 
+static void free_cid(struct ast_callerid *cid)
+{
+	if (cid->cid_dnid)
+		free(cid->cid_dnid);
+	if (cid->cid_num)
+		free(cid->cid_num);	
+	if (cid->cid_name)
+		free(cid->cid_name);	
+	if (cid->cid_ani)
+		free(cid->cid_ani);
+	if (cid->cid_rdnis)
+		free(cid->cid_rdnis);
+}
+
 void ast_channel_free(struct ast_channel *chan)
 {
 	struct ast_channel *last=NULL, *cur;
@@ -607,14 +621,7 @@ void ast_channel_free(struct ast_channel *chan)
 		ast_translator_free_path(chan->pvt->writetrans);
 	if (chan->pbx) 
 		ast_log(LOG_WARNING, "PBX may not have been terminated properly on '%s'\n", chan->name);
-	if (chan->dnid)
-		free(chan->dnid);
-	if (chan->callerid)
-		free(chan->callerid);	
-	if (chan->ani)
-		free(chan->ani);
-	if (chan->rdnis)
-		free(chan->rdnis);
+	free_cid(&chan->cid);
 	ast_mutex_destroy(&chan->lock);
 	/* Close pipes if appropriate */
 	if ((fd = chan->pvt->alertpipe[0]) > -1)
@@ -1743,7 +1750,7 @@ int ast_set_read_format(struct ast_channel *chan, int fmts)
 	return 0;
 }
 
-struct ast_channel *__ast_request_and_dial(char *type, int format, void *data, int timeout, int *outstate, char *callerid, struct outgoing_helper *oh)
+struct ast_channel *__ast_request_and_dial(char *type, int format, void *data, int timeout, int *outstate, char *cid_num, char *cid_name, struct outgoing_helper *oh)
 {
 	int state = 0;
 	struct ast_channel *chan;
@@ -1764,13 +1771,11 @@ struct ast_channel *__ast_request_and_dial(char *type, int format, void *data, i
 			while( (var = strtok_r(NULL, "|", &tmp)) ) {
 				pbx_builtin_setvar( chan, var );
 			} /* /JDG */
-			if (oh->callerid && *oh->callerid)
-				ast_set_callerid(chan, oh->callerid, 1);
+			ast_set_callerid(chan, oh->cid_num, oh->cid_name, oh->cid_num);
 			if (oh->account && *oh->account)
 				ast_cdr_setaccount(chan, oh->account);
 		}
-		if (callerid && !ast_strlen_zero(callerid))
-			ast_set_callerid(chan, callerid, 1);
+		ast_set_callerid(chan, cid_num, cid_name, cid_num);
 
 		if (!ast_call(chan, data, 0)) {
 			while(timeout && (chan->_state != AST_STATE_UP)) {
@@ -1853,9 +1858,9 @@ struct ast_channel *__ast_request_and_dial(char *type, int format, void *data, i
 	return chan;
 }
 
-struct ast_channel *ast_request_and_dial(char *type, int format, void *data, int timeout, int *outstate, char *callerid)
+struct ast_channel *ast_request_and_dial(char *type, int format, void *data, int timeout, int *outstate, char *cidnum, char *cidname)
 {
-	return __ast_request_and_dial(type, format, data, timeout, outstate, callerid, NULL);
+	return __ast_request_and_dial(type, format, data, timeout, outstate, cidnum, cidname, NULL);
 }
 
 struct ast_channel *ast_request(char *type, int format, void *data)
@@ -1889,8 +1894,9 @@ struct ast_channel *ast_request(char *type, int format, void *data)
 					"Channel: %s\r\n"
 					"State: %s\r\n"
 					"Callerid: %s\r\n"
+					"CalleridName: %s\r\n"
 					"Uniqueid: %s\r\n",
-					c->name, ast_state2str(c->_state), c->callerid ? c->callerid : "<unknown>", c->uniqueid);
+					c->name, ast_state2str(c->_state), c->cid.cid_num ? c->cid.cid_num : "<unknown>", c->cid.cid_name ? c->cid.cid_name : "<unknown>",c->uniqueid);
 				}
 			}
 			return c;
@@ -2194,10 +2200,10 @@ int ast_do_masquerade(struct ast_channel *original)
 	int x,i;
 	int res=0;
 	int origstate;
-	char *tmp;
 	struct ast_var_t *varptr;
 	struct ast_frame *cur, *prev;
 	struct ast_channel_pvt *p;
+	struct ast_callerid tmpcid;
 	struct ast_channel *clone = original->masq;
 	int rformat = original->readformat;
 	int wformat = original->writeformat;
@@ -2336,16 +2342,11 @@ int ast_do_masquerade(struct ast_channel *original)
 	/* Stream stuff stays the same */
 	/* Keep the original state.  The fixup code will need to work with it most likely */
 
-	/* dnid and callerid change to become the new, HOWEVER, we also link the original's
-	   fields back into the defunct 'clone' so that they will be freed when
-	   ast_frfree is eventually called */
-	tmp = original->dnid;
-	original->dnid = clone->dnid;
-	clone->dnid = tmp;
-	
-	tmp = original->callerid;
-	original->callerid = clone->callerid;
-	clone->callerid = tmp;
+	/* Just swap the whole structures, nevermind the allocations, they'll work themselves
+	   out. */
+	tmpcid = original->cid;
+	original->cid = clone->cid;
+	clone->cid = tmpcid;
 	
 	/* Restore original timing file descriptor */
 	original->fds[AST_MAX_FDS - 2] = original->timingfd;
@@ -2400,29 +2401,43 @@ int ast_do_masquerade(struct ast_channel *original)
 	return 0;
 }
 
-void ast_set_callerid(struct ast_channel *chan, char *callerid, int anitoo)
+void ast_set_callerid(struct ast_channel *chan, char *callerid, char *calleridname, char *ani)
 {
-	if (chan->callerid)
-		free(chan->callerid);
-	if (anitoo && chan->ani)
-		free(chan->ani);
 	if (callerid) {
-		chan->callerid = strdup(callerid);
-		if (anitoo)
-			chan->ani = strdup(callerid);
-	} else {
-		chan->callerid = NULL;
-		if (anitoo)
-			chan->ani = NULL;
+		if (chan->cid.cid_num)
+			free(chan->cid.cid_num);
+		if (ast_strlen_zero(callerid))
+			chan->cid.cid_num = NULL;
+		else
+			chan->cid.cid_num = strdup(callerid);
+	}
+	if (calleridname) {
+		if (chan->cid.cid_name)
+			free(chan->cid.cid_name);
+		if (ast_strlen_zero(calleridname))
+			chan->cid.cid_name = NULL;
+		else
+			chan->cid.cid_name = strdup(calleridname);
+	}
+	if (ani) {
+		if (chan->cid.cid_ani)
+			free(chan->cid.cid_ani);
+		if (ast_strlen_zero(ani))
+			chan->cid.cid_ani = NULL;
+		else
+			chan->cid.cid_ani = strdup(ani);
 	}
 	if (chan->cdr)
 		ast_cdr_setcid(chan->cdr, chan);
 	manager_event(EVENT_FLAG_CALL, "Newcallerid", 
 				"Channel: %s\r\n"
 				"Callerid: %s\r\n"
+				"CalleridName: %s\r\n"
 				"Uniqueid: %s\r\n",
-				chan->name, chan->callerid ? 
-				chan->callerid : "<Unknown>",
+				chan->name, chan->cid.cid_num ? 
+				chan->cid.cid_num : "<Unknown>",
+				chan->cid.cid_name ? 
+				chan->cid.cid_name : "<Unknown>",
 				chan->uniqueid);
 }
 
@@ -2437,15 +2452,23 @@ int ast_setstate(struct ast_channel *chan, int state)
 			"Channel: %s\r\n"
 			"State: %s\r\n"
 			"Callerid: %s\r\n"
+			"CalleridName: %s\r\n"
 			"Uniqueid: %s\r\n",
-			chan->name, ast_state2str(chan->_state), chan->callerid ? chan->callerid : "<unknown>", chan->uniqueid);
+			chan->name, ast_state2str(chan->_state), 
+			chan->cid.cid_num ? chan->cid.cid_num : "<unknown>", 
+			chan->cid.cid_name ? chan->cid.cid_name : "<unknown>", 
+			chan->uniqueid);
 		} else {
 			manager_event(EVENT_FLAG_CALL, "Newstate", 
 				"Channel: %s\r\n"
 				"State: %s\r\n"
 				"Callerid: %s\r\n"
+				"CalleridName: %s\r\n"
 				"Uniqueid: %s\r\n",
-				chan->name, ast_state2str(chan->_state), chan->callerid ? chan->callerid : "<unknown>", chan->uniqueid);
+				chan->name, ast_state2str(chan->_state), 
+				chan->cid.cid_num ? chan->cid.cid_num : "<unknown>", 
+				chan->cid.cid_name ? chan->cid.cid_name : "<unknown>", 
+				chan->uniqueid);
 		}
 	}
 	return 0;
diff --git a/channels/chan_agent.c b/channels/chan_agent.c
index c5dcc891e5..f0474806bb 100755
--- a/channels/chan_agent.c
+++ b/channels/chan_agent.c
@@ -457,12 +457,18 @@ static int agent_call(struct ast_channel *ast, char *dest, int timeout)
 		/* Call on this agent */
 		if (option_verbose > 2)
 			ast_verbose(VERBOSE_PREFIX_3 "outgoing agentcall, to agent '%s', on '%s'\n", p->agent, p->chan->name);
-		if (p->chan->callerid)
-			free(p->chan->callerid);
-		if (ast->callerid)
-			p->chan->callerid = strdup(ast->callerid);
+		if (p->chan->cid.cid_num)
+			free(p->chan->cid.cid_num);
+		if (ast->cid.cid_num)
+			p->chan->cid.cid_num = strdup(ast->cid.cid_num);
 		else
-			p->chan->callerid = NULL;
+			p->chan->cid.cid_num = NULL;
+		if (p->chan->cid.cid_name)
+			free(p->chan->cid.cid_name);
+		if (ast->cid.cid_name)
+			p->chan->cid.cid_name = strdup(ast->cid.cid_name);
+		else
+			p->chan->cid.cid_name = NULL;
 		res = ast_call(p->chan, p->loginchan, 0);
 		CLEANUP(ast,p);
 		ast_mutex_unlock(&p->lock);
@@ -1290,9 +1296,9 @@ static int __login_exec(struct ast_channel *chan, void *data, int callbackmode)
 									filename = "agent-loggedoff";
 								p->acknowledged = 0;
 								/* store/clear the global variable that stores agentid based on the callerid */
-								if (chan->callerid) {
+								if (chan->cid.cid_num) {
 									char agentvar[AST_MAX_BUF];
-									snprintf(agentvar, sizeof(agentvar), "%s_%s",GETAGENTBYCALLERID, chan->callerid);
+									snprintf(agentvar, sizeof(agentvar), "%s_%s",GETAGENTBYCALLERID, chan->cid.cid_num);
 									if (ast_strlen_zero(p->loginchan))
 										pbx_builtin_setvar_helper(NULL, agentvar, NULL);
 									else
@@ -1516,9 +1522,9 @@ static int agentmonitoroutgoing_exec(struct ast_channel *chan, void *data)
 		if (strchr(data, 'n'))
 			nowarnings = 1;
 	}
-	if (chan->callerid) {
+	if (chan->cid.cid_num) {
 		char agentvar[AST_MAX_BUF];
-		snprintf(agentvar, sizeof(agentvar), "%s_%s", GETAGENTBYCALLERID, chan->callerid);
+		snprintf(agentvar, sizeof(agentvar), "%s_%s", GETAGENTBYCALLERID, chan->cid.cid_num);
 		if ((tmp = pbx_builtin_getvar_helper(NULL, agentvar))) {
 			struct agent_pvt *p = agents;
 			strncpy(agent, tmp, sizeof(agent) - 1);
@@ -1544,7 +1550,7 @@ static int agentmonitoroutgoing_exec(struct ast_channel *chan, void *data)
 	}
 	/* check if there is n + 101 priority */
 	if (res) {
-	       if (ast_exists_extension(chan, chan->context, chan->exten, chan->priority + 101, chan->callerid)) {
+	       if (ast_exists_extension(chan, chan->context, chan->exten, chan->priority + 101, chan->cid.cid_num)) {
 			chan->priority+=100;
 			ast_verbose(VERBOSE_PREFIX_3 "Going to %d priority because there is no callerid or the agentid cannot be found.\n",chan->priority);
 	       }
diff --git a/channels/chan_iax2.c b/channels/chan_iax2.c
index a9beeebc7f..d644dfd6c8 100755
--- a/channels/chan_iax2.c
+++ b/channels/chan_iax2.c
@@ -207,7 +207,8 @@ struct iax2_user {
 	int temponly;
 	int capability;
 	int trunk;
-	char callerid[AST_MAX_EXTENSION];
+	char cid_num[AST_MAX_EXTENSION];
+	char cid_name[AST_MAX_EXTENSION];
 	struct ast_ha *ha;
 	struct iax2_context *contexts;
 	struct iax2_user *next;
@@ -220,7 +221,7 @@ struct iax2_peer {
 	char username[80];		
 	char secret[80];
 	char outkey[80];		/* What key we use to talk to this peer */
-	char context[AST_MAX_EXTENSION];	/* Default context (for transfer really) */
+	char context[AST_MAX_EXTENSION];	/* For transfers only */
 	char regexten[AST_MAX_EXTENSION];	/* Extension to register (if regcontext is used) */
 	char peercontext[AST_MAX_EXTENSION];	/* Context to pass to peer */
 	char mailbox[AST_MAX_EXTENSION];	/* Mailbox */
@@ -236,7 +237,8 @@ struct iax2_peer {
 
 	int hascallerid;
 	/* Suggested caller id if registering */
-	char callerid[AST_MAX_EXTENSION];
+	char cid_num[AST_MAX_EXTENSION];	/* Default context (for transfer really) */
+	char cid_name[AST_MAX_EXTENSION];	/* Default context (for transfer really) */
 	/* Whether or not to send ANI */
 	int sendani;
 	int expire;						/* Schedule entry for expirey */
@@ -410,7 +412,8 @@ struct chan_iax2_pvt {
 	/* Default Context */
 	char context[80];
 	/* Caller ID if available */
-	char callerid[80];
+	char cid_num[80];
+	char cid_name[80];
 	/* Hidden Caller ID (i.e. ANI) if appropriate */
 	char ani[80];
 	/* Whether or not ani should be transmitted in addition to Caller*ID */
@@ -2253,7 +2256,6 @@ static int iax2_call(struct ast_channel *c, char *dest, int timeout)
 	char *username;
 	char *secret = NULL;
 	char *hname;
-	char cid[256] = "";
 	char *l=NULL, *n=NULL;
 	struct iax_ie_data ied;
 	char myrdest [5] = "s";
@@ -2314,12 +2316,8 @@ static int iax2_call(struct ast_channel *c, char *dest, int timeout)
 	if (portno) {
 		sin.sin_port = htons(atoi(portno));
 	}
-	if (c->callerid) {
-		strncpy(cid, c->callerid, sizeof(cid) - 1);
-		ast_callerid_parse(cid, &n, &l);
-		if (l)
-			ast_shrink_phone_number(l);
-	}
+	l = c->cid.cid_num;
+	n = c->cid.cid_name;
 	/* Now build request */	
 	memset(&ied, 0, sizeof(ied));
 	/* On new call, first IE MUST be IAX version of caller */
@@ -2333,19 +2331,13 @@ static int iax2_call(struct ast_channel *c, char *dest, int timeout)
 		iax_ie_append_str(&ied, IAX_IE_CALLING_NUMBER, l);
 	if (n)
 		iax_ie_append_str(&ied, IAX_IE_CALLING_NAME, n);
-	if (iaxs[callno]->sendani && c->ani) {
-		l = n = NULL;
-		strncpy(cid, c->ani, sizeof(cid) - 1);
-		ast_callerid_parse(cid, &n, &l);
-		if (l) {
-			ast_shrink_phone_number(l);
-			iax_ie_append_str(&ied, IAX_IE_CALLING_ANI, l);
-		}
+	if (iaxs[callno]->sendani && c->cid.cid_ani) {
+		iax_ie_append_str(&ied, IAX_IE_CALLING_ANI, c->cid.cid_ani);
 	}
 	if (c->language && !ast_strlen_zero(c->language))
 		iax_ie_append_str(&ied, IAX_IE_LANGUAGE, c->language);
-	if (c->dnid && !ast_strlen_zero(c->dnid))
-		iax_ie_append_str(&ied, IAX_IE_DNID, c->dnid);
+	if (c->cid.cid_dnid && !ast_strlen_zero(c->cid.cid_dnid))
+		iax_ie_append_str(&ied, IAX_IE_DNID, c->cid.cid_dnid);
 	if (rcontext)
 		iax_ie_append_str(&ied, IAX_IE_CALLED_CONTEXT, rcontext);
 	else if (strlen(peercontext))
@@ -2727,14 +2719,16 @@ static struct ast_channel *ast_iax2_new(int callno, int state, int capability)
 		tmp->pvt->setoption = iax2_setoption;
 		tmp->pvt->bridge = iax2_bridge;
 		tmp->pvt->transfer = iax2_transfer;
-		if (!ast_strlen_zero(i->callerid))
-			tmp->callerid = strdup(i->callerid);
+		if (!ast_strlen_zero(i->cid_num))
+			tmp->cid.cid_num = strdup(i->cid_num);
+		if (!ast_strlen_zero(i->cid_name))
+			tmp->cid.cid_name = strdup(i->cid_name);
 		if (!ast_strlen_zero(i->ani))
-			tmp->ani = strdup(i->ani);
+			tmp->cid.cid_ani = strdup(i->ani);
 		if (!ast_strlen_zero(i->language))
 			strncpy(tmp->language, i->language, sizeof(tmp->language)-1);
 		if (!ast_strlen_zero(i->dnid))
-			tmp->dnid = strdup(i->dnid);
+			tmp->cid.cid_dnid = strdup(i->dnid);
 		if (!ast_strlen_zero(i->accountcode))
 			strncpy(tmp->accountcode, i->accountcode, sizeof(tmp->accountcode)-1);
 		if (i->amaflags)
@@ -3599,13 +3593,10 @@ static int check_access(int callno, struct sockaddr_in *sin, struct iax_ies *ies
 		return res;
 	if (ies->called_number)
 		strncpy(iaxs[callno]->exten, ies->called_number, sizeof(iaxs[callno]->exten) - 1);
-	if (ies->calling_number) {
-		if (ies->calling_name)
-			snprintf(iaxs[callno]->callerid, sizeof(iaxs[callno]->callerid), "\"%s\" <%s>", ies->calling_name, ies->calling_number);
-		else
-			strncpy(iaxs[callno]->callerid, ies->calling_number, sizeof(iaxs[callno]->callerid) - 1);
-	} else if (ies->calling_name)
-		strncpy(iaxs[callno]->callerid, ies->calling_name, sizeof(iaxs[callno]->callerid) - 1);
+	if (ies->calling_number) 
+		strncpy(iaxs[callno]->cid_num, ies->calling_number, sizeof(iaxs[callno]->cid_num) - 1);
+	if (ies->calling_name)
+		strncpy(iaxs[callno]->cid_name, ies->calling_name, sizeof(iaxs[callno]->cid_name) - 1);
 	if (ies->calling_ani)
 		strncpy(iaxs[callno]->ani, ies->calling_ani, sizeof(iaxs[callno]->ani) - 1);
 	if (ies->dnid)
@@ -3716,10 +3707,12 @@ static int check_access(int callno, struct sockaddr_in *sin, struct iax_ies *ies
 		/* And the permitted authentication methods */
 		iaxs[callno]->authmethods = user->authmethods;
 		/* If they have callerid, override the given caller id.  Always store the ANI */
-		if (!ast_strlen_zero(iaxs[callno]->callerid)) {
-			if (user->hascallerid)
-				strncpy(iaxs[callno]->callerid, user->callerid, sizeof(iaxs[callno]->callerid)-1);
-			strncpy(iaxs[callno]->ani, user->callerid, sizeof(iaxs[callno]->ani)-1);
+		if (!ast_strlen_zero(iaxs[callno]->cid_num) || !ast_strlen_zero(iaxs[callno]->cid_name)) {
+			if (user->hascallerid) {
+				strncpy(iaxs[callno]->cid_num, user->cid_num, sizeof(iaxs[callno]->cid_num)-1);
+				strncpy(iaxs[callno]->cid_name, user->cid_name, sizeof(iaxs[callno]->cid_name)-1);
+			}
+			strncpy(iaxs[callno]->ani, user->cid_num, sizeof(iaxs[callno]->ani)-1);
 		}
 		if (!ast_strlen_zero(user->accountcode))
 			strncpy(iaxs[callno]->accountcode, user->accountcode, sizeof(iaxs[callno]->accountcode)-1);
@@ -4464,8 +4457,10 @@ static int update_registry(char *name, struct sockaddr_in *sin, int callno, char
 				}
 				iax_ie_append_short(&ied, IAX_IE_MSGCOUNT, msgcount);
 			}
-			if (p->hascallerid)
-				iax_ie_append_str(&ied, IAX_IE_CALLING_NAME, p->callerid);
+			if (p->hascallerid) {
+				iax_ie_append_str(&ied, IAX_IE_CALLING_NUMBER, p->cid_num);
+				iax_ie_append_str(&ied, IAX_IE_CALLING_NAME, p->cid_name);
+			}
 		}
 		version = iax_check_version(devtype);
 		if (version) 
@@ -5399,7 +5394,7 @@ retryowner:
 				/* This might re-enter the IAX code and need the lock */
 				if (strcasecmp(iaxs[fr.callno]->exten, "TBD")) {
 					ast_mutex_unlock(&iaxsl[fr.callno]);
-					exists = ast_exists_extension(NULL, iaxs[fr.callno]->context, iaxs[fr.callno]->exten, 1, iaxs[fr.callno]->callerid);
+					exists = ast_exists_extension(NULL, iaxs[fr.callno]->context, iaxs[fr.callno]->exten, 1, iaxs[fr.callno]->cid_num);
 					ast_mutex_lock(&iaxsl[fr.callno]);
 				} else
 					exists = 0;
@@ -5467,10 +5462,10 @@ retryowner:
 					!(iaxs[fr.callno]->state & IAX_STATE_STARTED) && ies.called_number) {
 					if (iaxcompat) {
 						/* Spawn a thread for the lookup */
-						spawn_dp_lookup(fr.callno, iaxs[fr.callno]->context, ies.called_number, iaxs[fr.callno]->callerid);
+						spawn_dp_lookup(fr.callno, iaxs[fr.callno]->context, ies.called_number, iaxs[fr.callno]->cid_num);
 					} else {
 						/* Just look it up */
-						dp_lookup(fr.callno, iaxs[fr.callno]->context, ies.called_number, iaxs[fr.callno]->callerid, 1);
+						dp_lookup(fr.callno, iaxs[fr.callno]->context, ies.called_number, iaxs[fr.callno]->cid_num, 1);
 					}
 				}
 				break;
@@ -5695,7 +5690,7 @@ retryowner2:
 				}
 				if (strcasecmp(iaxs[fr.callno]->exten, "TBD")) {
 					/* This might re-enter the IAX code and need the lock */
-					exists = ast_exists_extension(NULL, iaxs[fr.callno]->context, iaxs[fr.callno]->exten, 1, iaxs[fr.callno]->callerid);
+					exists = ast_exists_extension(NULL, iaxs[fr.callno]->context, iaxs[fr.callno]->exten, 1, iaxs[fr.callno]->cid_num);
 				} else
 					exists = 0;
 				if (strcmp(iaxs[fr.callno]->exten, "TBD") && !exists) {
@@ -5755,7 +5750,7 @@ retryowner2:
 				if (iaxs[fr.callno]->state & IAX_STATE_TBD) {
 					iaxs[fr.callno]->state &= ~IAX_STATE_TBD;
 					strncpy(iaxs[fr.callno]->exten, ies.called_number ? ies.called_number : "s", sizeof(iaxs[fr.callno]->exten)-1);	
-					if (!ast_exists_extension(NULL, iaxs[fr.callno]->context, iaxs[fr.callno]->exten, 1, iaxs[fr.callno]->callerid)) {
+					if (!ast_exists_extension(NULL, iaxs[fr.callno]->context, iaxs[fr.callno]->exten, 1, iaxs[fr.callno]->cid_num)) {
 						if (authdebug)
 							ast_log(LOG_NOTICE, "Rejected dial attempt from %s, request '%s@%s' does not exist\n", ast_inet_ntoa(iabuf, sizeof(iabuf), sin.sin_addr), iaxs[fr.callno]->exten, iaxs[fr.callno]->context);
 						memset(&ied0, 0, sizeof(ied0));
@@ -6486,7 +6481,8 @@ static struct iax2_peer *build_peer(char *name, struct ast_variable *v)
 				else
 					peer->capability &= ~format;
 			} else if (!strcasecmp(v->name, "callerid")) {
-				strncpy(peer->callerid, v->value, sizeof(peer->callerid)-1);
+				ast_callerid_split(v->value, peer->cid_name, sizeof(peer->cid_name),
+									peer->cid_num, sizeof(peer->cid_num));
 				peer->hascallerid=1;
 			} else if (!strcasecmp(v->name, "sendani")) {
 				peer->sendani = ast_true(v->value);
@@ -6604,7 +6600,7 @@ static struct iax2_user *build_user(char *name, struct ast_variable *v)
 			} else if (!strcasecmp(v->name, "secret")) {
 				strncpy(user->secret, v->value, sizeof(user->secret)-1);
 			} else if (!strcasecmp(v->name, "callerid")) {
-				strncpy(user->callerid, v->value, sizeof(user->callerid)-1);
+				ast_callerid_split(v->value, user->cid_name, sizeof(user->cid_name), user->cid_num, sizeof(user->cid_num));
 				user->hascallerid=1;
 			} else if (!strcasecmp(v->name, "accountcode")) {
 				strncpy(user->accountcode, v->value, sizeof(user->accountcode)-1);
diff --git a/channels/chan_local.c b/channels/chan_local.c
index fc8fd8820a..4c50720dcd 100755
--- a/channels/chan_local.c
+++ b/channels/chan_local.c
@@ -239,18 +239,26 @@ static int local_call(struct ast_channel *ast, char *dest, int timeout)
 	int res;
 	
 	ast_mutex_lock(&p->lock);
-	if (p->owner->callerid)
-		p->chan->callerid = strdup(p->owner->callerid);
+	if (p->owner->cid.cid_num)
+		p->chan->cid.cid_num = strdup(p->owner->cid.cid_num);
+	else 
+		p->chan->cid.cid_num = NULL;
+
+	if (p->owner->cid.cid_name)
+		p->chan->cid.cid_name = strdup(p->owner->cid.cid_name);
+	else 
+		p->chan->cid.cid_name = NULL;
+
+	if (p->owner->cid.cid_rdnis)
+		p->chan->cid.cid_rdnis = strdup(p->owner->cid.cid_rdnis);
 	else
-		p->chan->callerid = NULL;
-	if (p->owner->rdnis)
-		p->chan->rdnis = strdup(p->owner->rdnis);
-	else
-		p->chan->rdnis = NULL;
-	if (p->owner->ani)
-		p->chan->ani = strdup(p->owner->ani);
+		p->chan->cid.cid_rdnis = NULL;
+
+	if (p->owner->cid.cid_ani)
+		p->chan->cid.cid_ani = strdup(p->owner->cid.cid_ani);
 	else
-		p->chan->ani = NULL;
+		p->chan->cid.cid_ani = NULL;
+
 	strncpy(p->chan->language, p->owner->language, sizeof(p->chan->language) - 1);
 	strncpy(p->chan->accountcode, p->owner->accountcode, sizeof(p->chan->accountcode) - 1);
 	p->chan->cdrflags = p->owner->cdrflags;
diff --git a/channels/chan_mgcp.c b/channels/chan_mgcp.c
index 22058c7752..308d7e6c15 100755
--- a/channels/chan_mgcp.c
+++ b/channels/chan_mgcp.c
@@ -150,7 +150,8 @@ static char context[AST_MAX_EXTENSION] = "default";
 
 static char language[MAX_LANGUAGE] = "";
 static char musicclass[MAX_LANGUAGE] = "";
-static char callerid[AST_MAX_EXTENSION] = "";
+static char cid_num[AST_MAX_EXTENSION] = "";
+static char cid_name[AST_MAX_EXTENSION] = "";
 
 static int dtmfmode = 0;
 static int nat = 0;
@@ -356,7 +357,8 @@ struct mgcp_endpoint {
 	char exten[AST_MAX_EXTENSION];		/* Extention where to start */
 	char context[AST_MAX_EXTENSION];
 	char language[MAX_LANGUAGE];
-	char callerid[AST_MAX_EXTENSION];	/* Caller*ID */
+	char cid_num[AST_MAX_EXTENSION];	/* Caller*ID */
+	char cid_name[AST_MAX_EXTENSION];	/* Caller*ID */
 	char lastcallerid[AST_MAX_EXTENSION];	/* Last Caller*ID */
 	char call_forward[AST_MAX_EXTENSION];	/* Last Caller*ID */
     char mailbox[AST_MAX_EXTENSION];
@@ -445,7 +447,7 @@ static struct ast_frame  *mgcp_read(struct ast_channel *ast);
 static int transmit_response(struct mgcp_subchannel *sub, char *msg, struct mgcp_request *req, char *msgrest);
 static int transmit_notify_request(struct mgcp_subchannel *sub, char *tone);
 static int transmit_modify_request(struct mgcp_subchannel *sub);
-static int transmit_notify_request_with_callerid(struct mgcp_subchannel *sub, char *tone, char *callerid);
+static int transmit_notify_request_with_callerid(struct mgcp_subchannel *sub, char *tone, char *callernum, char *callername);
 static int transmit_modify_with_sdp(struct mgcp_subchannel *sub, struct ast_rtp *rtp, int codecs);
 static int transmit_connection_del(struct mgcp_subchannel *sub);
 static int transmit_audit_endpoint(struct mgcp_endpoint *p);
@@ -840,7 +842,7 @@ static int mgcp_call(struct ast_channel *ast, char *dest, int timeout)
             transmit_modify_request(sub->next);
         }
 
-		transmit_notify_request_with_callerid(sub, tone, ast->callerid);
+		transmit_notify_request_with_callerid(sub, tone, ast->cid.cid_num, ast->cid.cid_name);
 		ast_setstate(ast, AST_STATE_RINGING);
 		ast_queue_control(ast, AST_CONTROL_RINGING);
 
@@ -897,7 +899,7 @@ static int mgcp_hangup(struct ast_channel *ast)
     if ((sub == p->sub) && sub->next->owner) {
         if (p->hookstate == MGCP_OFFHOOK) {
             if (sub->next->owner && sub->next->owner->bridge) {
-                transmit_notify_request_with_callerid(p->sub, "L/wt", sub->next->owner->bridge->callerid);
+                transmit_notify_request_with_callerid(p->sub, "L/wt", sub->next->owner->bridge->cid.cid_num, sub->next->owner->bridge->cid.cid_name);
             }
         } else {
             /* set our other connection as the primary and swith over to it */
@@ -905,7 +907,7 @@ static int mgcp_hangup(struct ast_channel *ast)
             p->sub->cxmode = MGCP_CX_RECVONLY;
             transmit_modify_request(p->sub);
             if (sub->next->owner && sub->next->owner->bridge) {
-                transmit_notify_request_with_callerid(p->sub, "L/rg", sub->next->owner->callerid);
+                transmit_notify_request_with_callerid(p->sub, "L/rg", sub->next->owner->bridge->cid.cid_num, sub->next->owner->bridge->cid.cid_name);
             }
         }
 
@@ -1291,8 +1293,10 @@ static struct ast_channel *mgcp_new(struct mgcp_subchannel *sub, int state)
 		strncpy(tmp->call_forward, i->call_forward, sizeof(tmp->call_forward) - 1);
 		strncpy(tmp->context, i->context, sizeof(tmp->context)-1);
 		strncpy(tmp->exten, i->exten, sizeof(tmp->exten)-1);
-		if (strlen(i->callerid))
-			tmp->callerid = strdup(i->callerid);
+		if (!ast_strlen_zero(i->cid_num))
+			tmp->cid.cid_num = strdup(i->cid_num);
+		if (!ast_strlen_zero(i->cid_name))
+			tmp->cid.cid_name = strdup(i->cid_name);
 		if (!i->adsi)
 			tmp->adsicpe = AST_ADSI_UNAVAILABLE;
 		tmp->priority = 1;
@@ -2018,10 +2022,9 @@ static int transmit_notify_request(struct mgcp_subchannel *sub, char *tone)
 	return send_request(p, NULL, &resp, oseq); /* SC */
 }
 
-static int transmit_notify_request_with_callerid(struct mgcp_subchannel *sub, char *tone, char *callerid)
+static int transmit_notify_request_with_callerid(struct mgcp_subchannel *sub, char *tone, char *callernum, char *callername)
 {
 	struct mgcp_request resp;
-	char cid[256];
 	char tone2[256];
 	char *l, *n;
 	time_t t;
@@ -2030,18 +2033,8 @@ static int transmit_notify_request_with_callerid(struct mgcp_subchannel *sub, ch
 	
 	time(&t);
 	localtime_r(&t,&tm);
-	if (callerid)
-		strncpy(cid, callerid, sizeof(cid) - 1);
-	else
-		cid[0] = '\0';
-	ast_callerid_parse(cid, &n, &l);
-	if (l) {
-		ast_shrink_phone_number(l);
-		if (!ast_isphonenumber(l)) {
-			n = l;
-			l = "";
-		}
-	} 
+	n = callername;
+	l = callernum;
 	if (!n)
 		n = "";
 	if (!l)
@@ -2458,8 +2451,8 @@ static void *mgcp_ss(void *data)
             /*tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_DIALTONE);*/
             transmit_notify_request(sub, "L/dl");
         }
-        if (ast_exists_extension(chan, chan->context, exten, 1, p->callerid)) {
-            if (!res || !ast_matchmore_extension(chan, chan->context, exten, 1, p->callerid)) {
+        if (ast_exists_extension(chan, chan->context, exten, 1, p->cid_num)) {
+            if (!res || !ast_matchmore_extension(chan, chan->context, exten, 1, p->cid_num)) {
                 if (getforward) {
                     /* Record this as the forwarding extension */
                     strncpy(p->call_forward, exten, sizeof(p->call_forward) - 1); 
@@ -2484,16 +2477,20 @@ static void *mgcp_ss(void *data)
                     /*res = tone_zone_play_tone(p->subs[index].zfd, -1);*/
                     ast_indicate(chan, -1);
                     strncpy(chan->exten, exten, sizeof(chan->exten)-1);
-                    if (strlen(p->callerid)) {
+                    if (!ast_strlen_zero(p->cid_num)) {
                         if (!p->hidecallerid) {
                             /* SC: free existing chan->callerid */
-                            if (chan->callerid)
-                                free(chan->callerid);
-                            chan->callerid = strdup(p->callerid);
+                            if (chan->cid.cid_num)
+                                free(chan->cid.cid_num);
+                            chan->cid.cid_num = strdup(p->cid_num);
+                            /* SC: free existing chan->callerid */
+                            if (chan->cid.cid_name)
+                                free(chan->cid.cid_name);
+                            chan->cid.cid_name = strdup(p->cid_name);
                         }
-                        if (chan->ani)
-                            free(chan->ani);
-                        chan->ani = strdup(p->callerid);
+                        if (chan->cid.cid_ani)
+                            free(chan->cid.cid_ani);
+                        chan->cid.cid_ani = strdup(p->cid_num);
                     }
                     ast_setstate(chan, AST_STATE_RING);
                     /*zt_enable_ec(p);*/
@@ -2549,9 +2546,12 @@ static void *mgcp_ss(void *data)
             }
             /* Disable Caller*ID if enabled */
             p->hidecallerid = 1;
-            if (chan->callerid)
-                free(chan->callerid);
-            chan->callerid = NULL;
+            if (chan->cid.cid_num)
+                free(chan->cid.cid_num);
+            chan->cid.cid_num = NULL;
+            if (chan->cid.cid_name)
+                free(chan->cid.cid_name);
+            chan->cid.cid_name = NULL;
             /*res = tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_DIALRECALL);*/
             transmit_notify_request(sub, "L/sl");
             len = 0;
@@ -2631,19 +2631,23 @@ static void *mgcp_ss(void *data)
             }
             /* Enable Caller*ID if enabled */
             p->hidecallerid = 0;
-            if (chan->callerid)
-                free(chan->callerid);
-            if (strlen(p->callerid))
-                chan->callerid = strdup(p->callerid);
+            if (chan->cid.cid_num)
+                free(chan->cid.cid_num);
+            if (!ast_strlen_zero(p->cid_num))
+                chan->cid.cid_num = strdup(p->cid_num);
+            if (chan->cid.cid_name)
+                free(chan->cid.cid_name);
+            if (!ast_strlen_zero(p->cid_name))
+                chan->cid.cid_name = strdup(p->cid_name);
             /*res = tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_DIALRECALL);*/
             transmit_notify_request(sub, "L/sl");
             len = 0;
             memset(exten, 0, sizeof(exten));
             timeout = firstdigittimeout;
-        } else if (!ast_canmatch_extension(chan, chan->context, exten, 1, chan->callerid) &&
+        } else if (!ast_canmatch_extension(chan, chan->context, exten, 1, chan->cid.cid_num) &&
                         ((exten[0] != '*') || (strlen(exten) > 2))) {
             if (option_debug)
-                ast_log(LOG_DEBUG, "Can't match %s from '%s' in context %s\n", exten, chan->callerid ? chan->callerid : "<Unknown Caller>", chan->context);
+                ast_log(LOG_DEBUG, "Can't match %s from '%s' in context %s\n", exten, chan->cid.cid_num ? chan->cid.cid_num : "<Unknown Caller>", chan->context);
             break;
         }
         if (!timeout)
@@ -3481,10 +3485,12 @@ static struct mgcp_gateway *build_gateway(char *cat, struct ast_variable *v)
 			} else if (!strcasecmp(v->name, "nat")) {
 				nat = ast_true(v->value);
 			} else if (!strcasecmp(v->name, "callerid")) {
-				if (!strcasecmp(v->value, "asreceived"))
-					callerid[0] = '\0';
-				else
-					strncpy(callerid, v->value, sizeof(callerid) - 1);
+				if (!strcasecmp(v->value, "asreceived")) {
+					cid_num[0] = '\0';
+					cid_name[0] = '\0';
+				} else {
+					ast_callerid_split(v->value, cid_name, sizeof(cid_name), cid_num, sizeof(cid_num));
+				}
 			} else if (!strcasecmp(v->name, "language")) {
 				strncpy(language, v->value, sizeof(language)-1);
             } else if (!strcasecmp(v->name, "accountcode")) {
@@ -3558,7 +3564,8 @@ static struct mgcp_gateway *build_gateway(char *cat, struct ast_variable *v)
 					//strncpy(e->name, "aaln/*", sizeof(e->name) - 1);
 					/* XXX Should we really check for uniqueness?? XXX */
 					strncpy(e->context, context, sizeof(e->context) - 1);
-					strncpy(e->callerid, callerid, sizeof(e->callerid) - 1);
+					strncpy(e->cid_num, cid_num, sizeof(e->cid_num) - 1);
+					strncpy(e->cid_name, cid_name, sizeof(e->cid_name) - 1);
 					strncpy(e->language, language, sizeof(e->language) - 1);
             		strncpy(e->musicclass, musicclass, sizeof(e->musicclass)-1);
             		strncpy(e->mailbox, mailbox, sizeof(e->mailbox)-1);
@@ -3650,7 +3657,8 @@ static struct mgcp_gateway *build_gateway(char *cat, struct ast_variable *v)
                     }
 					/* XXX Should we really check for uniqueness?? XXX */
 					strncpy(e->context, context, sizeof(e->context) - 1);
-					strncpy(e->callerid, callerid, sizeof(e->callerid) - 1);
+					strncpy(e->cid_num, cid_num, sizeof(e->cid_num) - 1);
+					strncpy(e->cid_name, cid_name, sizeof(e->cid_name) - 1);
 					strncpy(e->language, language, sizeof(e->language) - 1);
                     strncpy(e->musicclass, musicclass, sizeof(e->musicclass)-1);
                     strncpy(e->mailbox, mailbox, sizeof(e->mailbox)-1);
diff --git a/channels/chan_modem.c b/channels/chan_modem.c
index c2f3bf0f9e..063725504c 100755
--- a/channels/chan_modem.c
+++ b/channels/chan_modem.c
@@ -3,9 +3,9 @@
  *
  * A/Open ITU-56/2 Voice Modem Driver (Rockwell, IS-101, and others)
  * 
- * Copyright (C) 1999, Mark Spencer
+ * Copyright (C) 1999-2004, Digium, Inc.
  *
- * Mark Spencer <markster@linux-support.net>
+ * Mark Spencer <markster@digium.com>
  *
  * This program is free software, distributed under the terms of
  * the GNU General Public License
@@ -429,7 +429,8 @@ static int modem_hangup(struct ast_channel *ast)
 	if (p->mc->init)
 		p->mc->init(p);
 	ast_setstate(ast, AST_STATE_DOWN);
-	memset(p->cid, 0, sizeof(p->cid));
+	memset(p->cid_num, 0, sizeof(p->cid_num));
+	memset(p->cid_name, 0, sizeof(p->cid_name));
 	memset(p->dnid, 0, sizeof(p->dnid));
 	((struct ast_modem_pvt *)(ast->pvt->pvt))->owner = NULL;
 	ast_mutex_lock(&usecnt_lock);
@@ -529,8 +530,12 @@ struct ast_channel *ast_modem_new(struct ast_modem_pvt *i, int state)
 		tmp->pvt->read = modem_read;
 		tmp->pvt->write = modem_write;
 		strncpy(tmp->context, i->context, sizeof(tmp->context)-1);
-		if (strlen(i->cid))
-			tmp->callerid = strdup(i->cid);
+
+		if (!ast_strlen_zero(i->cid_num))
+			tmp->cid.cid_num = strdup(i->cid_num);
+		if (!ast_strlen_zero(i->cid_name))
+			tmp->cid.cid_name = strdup(i->cid_name);
+
 		if (strlen(i->language))
 			strncpy(tmp->language,i->language, sizeof(tmp->language)-1);
 		if (strlen(i->dnid))
@@ -739,7 +744,8 @@ static struct ast_modem_pvt *mkif(char *iface)
 		tmp->dialtype = dialtype;
 		tmp->mode = gmode;
 		tmp->group = cur_group;
-		memset(tmp->cid, 0, sizeof(tmp->cid));
+		memset(tmp->cid_num, 0, sizeof(tmp->cid_num));
+		memset(tmp->cid_name, 0, sizeof(tmp->cid_name));
 		strncpy(tmp->context, context, sizeof(tmp->context)-1);
 		strncpy(tmp->initstr, initstr, sizeof(tmp->initstr)-1);
 		tmp->next = NULL;
diff --git a/channels/chan_modem_bestdata.c b/channels/chan_modem_bestdata.c
index ddcb720182..f923e478a4 100755
--- a/channels/chan_modem_bestdata.c
+++ b/channels/chan_modem_bestdata.c
@@ -201,13 +201,14 @@ static struct ast_frame *bestdata_handle_escape(struct ast_modem_pvt *p, char es
 		p->gotclid = 1;
 		if ((!strcmp(name,"O")) || (!strcmp(name,"P"))) name[0] = 0;
 		if ((!strcmp(nmbr,"O")) || (!strcmp(nmbr,"P"))) nmbr[0] = 0;
-		if ((name[0]) && (nmbr[0])) snprintf(p->cid,sizeof(p->cid),
-			"\"%s\" <%s>",name,nmbr);
-		else if (name[0]) snprintf(p->cid,sizeof(p->cid),
-			"\"%s\"",name);
-		else if (nmbr[0]) snprintf(p->cid,sizeof(p->cid),
-			"%s",nmbr);
-		if (p->owner) p->owner->callerid = strdup(p->cid);
+		if (name[0])
+			strncpy(p->cid_name, name, sizeof(p->cid_name) - 1);
+		if (nmbr[0])
+			strncpy(p->cid_num, nmbr, sizeof(p->cid_num) - 1);
+		if (p->owner) {
+			p->owner->cid.cid_num = strdup(p->cid_num);
+			p->owner->cid.cid_name = strdup(p->cid_name);
+		}
 		return &p->fr;
 	case '@': /* response from "OK" in command mode */
 		if (p->owner)
diff --git a/channels/chan_modem_i4l.c b/channels/chan_modem_i4l.c
index 892f4c09f1..48e76aa693 100755
--- a/channels/chan_modem_i4l.c
+++ b/channels/chan_modem_i4l.c
@@ -345,7 +345,7 @@ static struct ast_frame *i4l_read(struct ast_modem_pvt *p)
 				return i4l_handle_escape(p, 'b');
 			} else
 			if (!strncasecmp(result, "CALLER NUMBER: ", 15 )) {
-				strncpy(p->cid, result + 15, sizeof(p->cid)-1);
+				strncpy(p->cid_num, result + 15, sizeof(p->cid_num)-1);
 				return i4l_handle_escape(p, 0);
 			} else
 			if (!strcasecmp(result, "RINGING")) {
@@ -584,30 +584,22 @@ static int i4l_dialdigit(struct ast_modem_pvt *p, char digit)
 static int i4l_dial(struct ast_modem_pvt *p, char *stuff)
 {
 	char cmd[80];
-	char tmp[255];
 	char tmpmsn[255];
-	char *name, *num;
 	struct ast_channel *c = p->owner;
 
 	// Find callerid number first, to set the correct A number
-	if (c && c->callerid && ! c->restrictcid) {
-	  ast_log(LOG_DEBUG, "Finding callerid from %s...\n",c->callerid);
-	  strncpy(tmp, c->callerid, sizeof(tmp) - 1);
-	  ast_callerid_parse(tmp, &name, &num);
-	  if (num) {
-	    ast_shrink_phone_number(num);
-	    snprintf(tmpmsn, sizeof(tmpmsn), ",%s,", num);
+	if (c && c->cid.cid_num && !(c->cid.cid_pres & 0x20)) {
+	    snprintf(tmpmsn, sizeof(tmpmsn), ",%s,", c->cid.cid_num);
 	    if(strlen(p->outgoingmsn) && strstr(p->outgoingmsn,tmpmsn) != NULL) {
 	      // Tell ISDN4Linux to use this as A number
-	      snprintf(cmd, sizeof(cmd), "AT&E%s\n", num);
+	      snprintf(cmd, sizeof(cmd), "AT&E%s\n", c->cid.cid_num);
 	      if (ast_modem_send(p, cmd, strlen(cmd))) {
-		ast_log(LOG_WARNING, "Unable to set A number to %s\n",num);
+		ast_log(LOG_WARNING, "Unable to set A number to %s\n", c->cid.cid_num);
 	      }
 
 	    } else {
-	      ast_log(LOG_WARNING, "Outgoing MSN %s not allowed (see outgoingmsn=%s in modem.conf)\n",num,p->outgoingmsn);
+	      ast_log(LOG_WARNING, "Outgoing MSN %s not allowed (see outgoingmsn=%s in modem.conf)\n",c->cid.cid_num,p->outgoingmsn);
 	    }
-	  }
 	}
 
 	snprintf(cmd, sizeof(cmd), "ATD%c %s\n", p->dialtype,stuff);
diff --git a/channels/chan_oss.c b/channels/chan_oss.c
index a74a9d847e..350fd630dc 100755
--- a/channels/chan_oss.c
+++ b/channels/chan_oss.c
@@ -7,9 +7,9 @@
  * VERY BADLY WRITTEN DRIVER so please don't use it as a model for
  * writing a driver.
  * 
- * Copyright (C) 1999, Mark Spencer
+ * Copyright (C) 1999-2004, Digium, Inc.
  *
- * Mark Spencer <markster@linux-support.net>
+ * Mark Spencer <markster@digium.com>
  *
  * This program is free software, distributed under the terms of
  * the GNU General Public License
@@ -941,7 +941,7 @@ static int console_transfer(int fd, int argc, char *argv[])
 			context++;
 		} else
 			context = oss.owner->context;
-		if (ast_exists_extension(oss.owner->bridge, context, tmp, 1, oss.owner->bridge->callerid)) {
+		if (ast_exists_extension(oss.owner->bridge, context, tmp, 1, oss.owner->bridge->cid.cid_num)) {
 			ast_cli(fd, "Whee, transferring %s to %s@%s.\n", 
 					oss.owner->bridge->name, tmp, context);
 			if (ast_async_goto(oss.owner->bridge, context, tmp, 1))
diff --git a/channels/chan_phone.c b/channels/chan_phone.c
index bacfaa3c48..dc985c170a 100755
--- a/channels/chan_phone.c
+++ b/channels/chan_phone.c
@@ -3,9 +3,9 @@
  *
  * Generic Linux Telephony Interface driver
  * 
- * Copyright (C) 1999, Mark Spencer
+ * Copyright (C) 1999-2004, Digium, Inc.
  *
- * Mark Spencer <markster@linux-support.net>
+ * Mark Spencer <markster@digium.com>
  *
  * This program is free software, distributed under the terms of
  * the GNU General Public License
@@ -119,10 +119,12 @@ static struct phone_pvt {
 	char obuf[PHONE_MAX_BUF * 2];
 	char ext[AST_MAX_EXTENSION];
 	char language[MAX_LANGUAGE];
-	char callerid[AST_MAX_EXTENSION];
+	char cid_num[AST_MAX_EXTENSION];
+	char cid_name[AST_MAX_EXTENSION];
 } *iflist = NULL;
 
-static char callerid[AST_MAX_EXTENSION];
+static char cid_num[AST_MAX_EXTENSION];
+static char cid_name[AST_MAX_EXTENSION];
 
 static int phone_digit(struct ast_channel *ast, char digit)
 {
@@ -185,24 +187,13 @@ static int phone_call(struct ast_channel *ast, char *dest, int timeout)
 		snprintf(cid.min, sizeof(cid.min),     "%02d", tm.tm_min);
 	}
 	/* the standard format of ast->callerid is:  "name" <number>, but not always complete */
-	if (!ast->callerid || ast_strlen_zero(ast->callerid)){
+	if (!ast->cid.cid_name || ast_strlen_zero(ast->cid.cid_name))
 		strncpy(cid.name, DEFAULT_CALLER_ID, sizeof(cid.name) - 1);
-		cid.number[0]='\0';
-	} else {
-		char *n, *l;
-		char callerid[256] = "";
-		strncpy(callerid, ast->callerid, sizeof(callerid) - 1);
-		ast_callerid_parse(callerid, &n, &l);
-		if (l) {
-			ast_shrink_phone_number(l);
-			if (!ast_isphonenumber(l))
-				l = NULL;
-		}
-		if (l)
-			strncpy(cid.number, l, sizeof(cid.number) - 1);
-		if (n)
-			strncpy(cid.name, n, sizeof(cid.name) - 1);
-	}
+	else
+		strncpy(cid.name, ast->cid.cid_name, sizeof(cid.name) - 1);
+
+	if (ast->cid.cid_num) 
+		strncpy(cid.number, ast->cid.cid_num, sizeof(cid.number) - 1);
 
 	p = ast->pvt->pvt;
 
@@ -683,8 +674,10 @@ static struct ast_channel *phone_new(struct phone_pvt *i, int state, char *conte
 			strncpy(tmp->exten, "s",  sizeof(tmp->exten) - 1);
 		if (strlen(i->language))
 			strncpy(tmp->language, i->language, sizeof(tmp->language)-1);
-		if (strlen(i->callerid))
-			tmp->callerid = strdup(i->callerid);
+		if (!ast_strlen_zero(i->cid_num))
+			tmp->cid.cid_num = strdup(i->cid_num);
+		if (!ast_strlen_zero(i->cid_name))
+			tmp->cid.cid_name = strdup(i->cid_name);
 		i->owner = tmp;
 		ast_mutex_lock(&usecnt_lock);
 		usecnt++;
@@ -736,7 +729,7 @@ static void phone_check_exception(struct phone_pvt *i)
 			i->dialtone = 0;
 			if (strlen(i->ext) < AST_MAX_EXTENSION - 1)
 				strncat(i->ext, digit, sizeof(i->ext) - strlen(i->ext) - 1);
-			if (ast_exists_extension(NULL, i->context, i->ext, 1, i->callerid)) {
+			if (ast_exists_extension(NULL, i->context, i->ext, 1, i->cid_num)) {
 				/* It's a valid extension in its context, get moving! */
 				phone_new(i, AST_STATE_RING, i->context);
 				/* No need to restart monitor, we are the monitor */
@@ -746,10 +739,10 @@ static void phone_check_exception(struct phone_pvt *i)
 					ast_mutex_unlock(&usecnt_lock);
 					ast_update_use_count();
 				}
-			} else if (!ast_canmatch_extension(NULL, i->context, i->ext, 1, i->callerid)) {
+			} else if (!ast_canmatch_extension(NULL, i->context, i->ext, 1, i->cid_num)) {
 				/* There is nothing in the specified extension that can match anymore.
 				   Try the default */
-				if (ast_exists_extension(NULL, "default", i->ext, 1, i->callerid)) {
+				if (ast_exists_extension(NULL, "default", i->ext, 1, i->cid_num)) {
 					/* Check the default, too... */
 					phone_new(i, AST_STATE_RING, "default");
 					if (i->owner) {
@@ -759,7 +752,7 @@ static void phone_check_exception(struct phone_pvt *i)
 						ast_update_use_count();
 					}
 					/* XXX This should probably be justified better XXX */
-				}  else if (!ast_canmatch_extension(NULL, "default", i->ext, 1, i->callerid)) {
+				}  else if (!ast_canmatch_extension(NULL, "default", i->ext, 1, i->cid_num)) {
 					/* It's not a valid extension, give a busy signal */
 					if (option_debug)
 						ast_log(LOG_DEBUG, "%s can't match anything in %s or default\n", i->ext, i->context);
@@ -1020,7 +1013,8 @@ static struct phone_pvt *mkif(char *iface, int mode, int txgain, int rxgain)
 		tmp->obuflen = 0;
 		tmp->dialtone = 0;
 		tmp->cpt = 0;
-		strncpy(tmp->callerid, callerid, sizeof(tmp->callerid)-1);
+		strncpy(tmp->cid_num, cid_num, sizeof(tmp->cid_num)-1);
+		strncpy(tmp->cid_name, cid_name, sizeof(tmp->cid_name)-1);
 		tmp->txgain = txgain;
 		ioctl(tmp->fd, PHONE_PLAY_VOLUME, tmp->txgain);
 		tmp->rxgain = rxgain;
@@ -1183,7 +1177,7 @@ int load_module()
 		} else if (!strcasecmp(v->name, "language")) {
 			strncpy(language, v->value, sizeof(language)-1);
 		} else if (!strcasecmp(v->name, "callerid")) {
-			strncpy(callerid, v->value, sizeof(callerid)-1);
+			ast_callerid_split(v->value, cid_name, sizeof(cid_name), cid_num, sizeof(cid_num));
 		} else if (!strcasecmp(v->name, "mode")) {
 			if (!strncasecmp(v->value, "di", 2)) 
 				mode = MODE_DIALTONE;
diff --git a/channels/chan_sip.c b/channels/chan_sip.c
index 3d929c3e8f..c2b2f5450f 100755
--- a/channels/chan_sip.c
+++ b/channels/chan_sip.c
@@ -269,6 +269,7 @@ static struct sip_pvt {
 	int peercapability;			/* Supported peer capability */
 	int prefcodec;				/* Preferred codec (outbound only) */
 	int noncodeccapability;
+	int callingpres;			/* Calling presentation */
 	int outgoing;				/* Outgoing or incoming call? */
 	int authtries;				/* Times we've tried to authenticate */
 	int insecure;				/* Don't check source port/ip */
@@ -311,8 +312,8 @@ static struct sip_pvt {
 	char uri[256];				/* Original requested URI */
 	char peersecret[256];
 	char peermd5secret[256];
-	char callerid[256];			/* Caller*ID */
-	int restrictcid;			/* hide presentation from remote user */
+	char cid_num[256];			/* Caller*ID */
+	char cid_name[256];			/* Caller*ID */
 	char via[256];
 	char fullcontact[128];		/* Extra parameters to go in the "To" header */
 	char accountcode[20];			/* Account code */
@@ -384,7 +385,8 @@ struct sip_user {
 	char secret[80];
         char md5secret[80];
 	char context[80];
-	char callerid[80];
+	char cid_num[80];
+	char cid_name[80];
 	char accountcode[20];
 	char language[MAX_LANGUAGE];
 	char musicclass[MAX_LANGUAGE];  /* Music on Hold class */
@@ -394,6 +396,7 @@ struct sip_user {
 	int nat;
 	int hascallerid;
 	int amaflags;
+	int callingpres;
 	int insecure;
 	int canreinvite;
 	int capability;
@@ -406,7 +409,6 @@ struct sip_user {
 	int outUse;
 	int outgoinglimit;
 	int promiscredir;
-	int restrictcid;
 	int trustrpid;
 	int progressinband;
 	struct ast_ha *ha;
@@ -1034,7 +1036,7 @@ static struct sip_user *mysql_user(char *user)
 			mysql_real_escape_string(mysql, name, user, strlen(user));
 		}
 
-		snprintf(query, sizeof(query), "SELECT name, secret, context, username, ipaddr, port, regseconds, callerid, restrictcid FROM sipfriends WHERE name=\"%s\"", name);
+		snprintf(query, sizeof(query), "SELECT name, secret, context, username, ipaddr, port, regseconds, callerid, callingpres FROM sipfriends WHERE name=\"%s\"", name);
 
 		ast_mutex_lock(&mysqllock);
 		mysql_query(mysql, query);
@@ -1057,8 +1059,8 @@ static struct sip_user *mysql_user(char *user)
 						} else if (!strcasecmp(fields[x].name, "regseconds")) {
 							if (sscanf(rowval[x], "%li", &regseconds) != 1)
 								regseconds = 0;
-						} else if (!strcasecmp(fields[x].name, "restrictcid")) {
-							u->restrictcid = 1;
+						} else if (!strcasecmp(fields[x].name, "callingpres")) {
+							u->callingpres = atoi(rowval[x]);
 						} else if (!strcasecmp(fields[x].name, "callerid")) {
 							strncpy(u->callerid, rowval[x], sizeof(u->callerid) - 1);
 							u->hascallerid=1;
@@ -1536,7 +1538,7 @@ static int sip_call(struct ast_channel *ast, char *dest, int timeout)
 	ast_log(LOG_DEBUG, "Outgoing Call for %s\n", p->username);
 	res = update_user_counter(p,INC_OUT_USE);
 	if ( res != -1 ) {
-		p->restrictcid = ast->restrictcid;
+		p->callingpres = ast->cid.cid_pres;
 		p->jointcapability = p->capability;
 		transmit_invite(p, "INVITE", 1, NULL, NULL, vxml_url,distinctive_ring, osptoken, 1);
 		if (p->maxtime) {
@@ -2073,7 +2075,7 @@ static struct ast_channel *sip_new(struct sip_pvt *i, int state, char *title)
 
 		tmp->callgroup = i->callgroup;
 		tmp->pickupgroup = i->pickupgroup;
-		tmp->restrictcid = i->restrictcid;
+		tmp->cid.cid_pres = i->callingpres;
                 if (!ast_strlen_zero(i->accountcode))
                         strncpy(tmp->accountcode, i->accountcode, sizeof(tmp->accountcode)-1);
                 if (i->amaflags)
@@ -2088,12 +2090,14 @@ static struct ast_channel *sip_new(struct sip_pvt *i, int state, char *title)
 		ast_mutex_unlock(&usecnt_lock);
 		strncpy(tmp->context, i->context, sizeof(tmp->context)-1);
 		strncpy(tmp->exten, i->exten, sizeof(tmp->exten)-1);
-		if (!ast_strlen_zero(i->callerid))
-			tmp->callerid = strdup(i->callerid);
+		if (!ast_strlen_zero(i->cid_num))
+			tmp->cid.cid_num = strdup(i->cid_num);
+		if (!ast_strlen_zero(i->cid_name))
+			tmp->cid.cid_name = strdup(i->cid_name);
 		if (!ast_strlen_zero(i->rdnis))
-			tmp->rdnis = strdup(i->rdnis);
+			tmp->cid.cid_rdnis = strdup(i->rdnis);
 		if (!ast_strlen_zero(i->exten) && strcmp(i->exten, "s"))
-			tmp->dnid = strdup(i->exten);
+			tmp->cid.cid_dnid = strdup(i->exten);
 		tmp->priority = 1;
 		if (!ast_strlen_zero(i->domain)) {
 			pbx_builtin_setvar_helper(tmp, "SIPDOMAIN", i->domain);
@@ -3686,22 +3690,16 @@ static void initreqprep(struct sip_request *req, struct sip_pvt *p, char *cmd, c
 	char to[256];
 	char tmp[80];
 	char iabuf[INET_ADDRSTRLEN];
-	char cid[256];
 	char *l = default_callerid, *n=NULL;
 
 	snprintf(p->lastmsg, sizeof(p->lastmsg), "Init: %s", cmd);
 
-	if (p->owner && p->owner->callerid) {
-		strncpy(cid, p->owner->callerid, sizeof(cid) - 1);
-		cid[sizeof(cid) - 1] = '\0';
-		ast_callerid_parse(cid, &n, &l);
-		if (l) 
-			ast_shrink_phone_number(l);
-		if (!l || !ast_isphonenumber(l))
-				l = default_callerid;
-	}
+	l = p->owner->cid.cid_num;
+	n = p->owner->cid.cid_name;
+	if (!l || !ast_isphonenumber(l))
+			l = default_callerid;
 	/* if user want's his callerid restricted */
-	if (p->restrictcid) {
+	if (p->callingpres & AST_PRES_PROHIB_USER_NUMBER_NOT_SCREENED) {
 		l = CALLERID_UNKNOWN;
 		n = l;
 	}
@@ -4684,7 +4682,7 @@ static int check_auth(struct sip_pvt *p, struct sip_request *req, char *randdata
 		if (!strlen(osptoken))
 			return -1;
 		/* Validate token */
-		if (ast_osp_validate(NULL, osptoken, &p->osphandle, &osptimelimit, p->callerid, p->sa.sin_addr, p->exten) < 1)
+		if (ast_osp_validate(NULL, osptoken, &p->osphandle, &osptimelimit, p->cid_num, p->sa.sin_addr, p->exten) < 1)
 			return -1;
 		
 		snprintf(tmp, sizeof(tmp), "%d", p->osphandle);
@@ -5305,7 +5303,7 @@ static int get_rpid_num(char *input,char *output, int maxlen)
 		*end = '\0';
 
 	if(strstr(input,"privacy=full") || strstr(input,"privacy=uri"))
-		return 1;
+		return AST_PRES_PROHIB_USER_NUMBER_NOT_SCREENED;
 
 	return 0;
 }
@@ -5337,7 +5335,7 @@ static int check_user_full(struct sip_pvt *p, struct sip_request *req, char *cmd
 	rpid = get_header(req, "Remote-Party-ID");
 	memset(rpid_num,0,sizeof(rpid_num));
 	if(!ast_strlen_zero(rpid)) 
-	  p->restrictcid = get_rpid_num(rpid,rpid_num, sizeof(rpid_num));
+	  p->callingpres = get_rpid_num(rpid,rpid_num, sizeof(rpid_num));
 
 	of = ditch_braces(from);
 	if (ast_strlen_zero(p->exten)) {
@@ -5360,10 +5358,9 @@ static int check_user_full(struct sip_pvt *p, struct sip_request *req, char *cmd
 		*c = '\0';
 	if ((c = strchr(of, ':')))
 		*c = '\0';
+	strncpy(p->cid_num, of, sizeof(p->cid_num) - 1);
 	if (*calleridname)
-		snprintf(p->callerid,sizeof(p->callerid),"\"%s\" <%s>",calleridname,of);
-	else
-		strncpy(p->callerid, of, sizeof(p->callerid) - 1);
+		strncpy(p->cid_name, calleridname, sizeof(p->cid_name) - 1);
 	if (ast_strlen_zero(of))
 			return 0;
 	ast_mutex_lock(&userl.lock);
@@ -5379,9 +5376,8 @@ static int check_user_full(struct sip_pvt *p, struct sip_request *req, char *cmd
 		/* replace callerid if rpid found, and not restricted */
 		if(!ast_strlen_zero(rpid_num) && p->trustrpid) {
 		  if (*calleridname)
-		    snprintf(p->callerid, sizeof(p->callerid), "\"%s\" <%s>",calleridname,rpid_num);
-		  else
-		    strncpy(p->callerid, rpid_num, sizeof(p->callerid) - 1);
+		  	strncpy(p->cid_name, calleridname, sizeof(p->cid_name) - 1);
+		  strncpy(p->cid_num, rpid_num, sizeof(p->cid_num) - 1);
 		}
 
 		if (p->rtp) {
@@ -5396,8 +5392,10 @@ static int check_user_full(struct sip_pvt *p, struct sip_request *req, char *cmd
 			sip_cancel_destroy(p);
 			if (!ast_strlen_zero(user->context))
 				strncpy(p->context, user->context, sizeof(p->context) - 1);
-			if (!ast_strlen_zero(user->callerid) && !ast_strlen_zero(p->callerid)) 
-				strncpy(p->callerid, user->callerid, sizeof(p->callerid) - 1);
+			if (!ast_strlen_zero(user->cid_num) && !ast_strlen_zero(p->cid_num)) 
+				strncpy(p->cid_num, user->cid_num, sizeof(p->cid_num) - 1);
+			if (!ast_strlen_zero(user->cid_name) && !ast_strlen_zero(p->cid_name)) 
+				strncpy(p->cid_num, user->cid_name, sizeof(p->cid_name) - 1);
 			strncpy(p->username, user->name, sizeof(p->username) - 1);
 			strncpy(p->peersecret, user->secret, sizeof(p->peersecret) - 1);
 			strncpy(p->peermd5secret, user->md5secret, sizeof(p->peermd5secret) - 1);
@@ -5408,7 +5406,7 @@ static int check_user_full(struct sip_pvt *p, struct sip_request *req, char *cmd
 			p->amaflags = user->amaflags;
 			p->callgroup = user->callgroup;
 			p->pickupgroup = user->pickupgroup;
-			p->restrictcid = user->restrictcid;
+			p->callingpres = user->callingpres;
 			p->capability = user->capability;
 			p->jointcapability = user->capability;
 			if (p->peercapability)
@@ -5452,9 +5450,8 @@ static int check_user_full(struct sip_pvt *p, struct sip_request *req, char *cmd
 			/* replace callerid if rpid found, and not restricted */
 			if(!ast_strlen_zero(rpid_num) && p->trustrpid) {
 			  if (*calleridname)
-			    snprintf(p->callerid,sizeof(p->callerid),"\"%s\" <%s>",calleridname,rpid_num);
-			  else
-			    strncpy(p->callerid, rpid_num, sizeof(p->callerid) - 1);
+			  	strncpy(p->cid_name, calleridname, sizeof(p->cid_name) - 1);
+			  strncpy(p->cid_num, rpid_num, sizeof(p->cid_num) - 1);
 			}
 #ifdef OSP_SUPPORT
 			p->ospauth = peer->ospauth;
@@ -5891,7 +5888,7 @@ static int __sip_show_channels(int fd, int argc, char *argv[], int subscriptions
 	while (cur) {
 		if (!cur->subscribed && !subscriptions) {
 		   ast_cli(fd, FORMAT, ast_inet_ntoa(iabuf, sizeof(iabuf), cur->sa.sin_addr), 
-			ast_strlen_zero(cur->username) ? ( ast_strlen_zero(cur->callerid) ? "(None)" : cur->callerid ) : cur->username, 
+			ast_strlen_zero(cur->username) ? ( ast_strlen_zero(cur->cid_num) ? "(None)" : cur->cid_num ) : cur->username, 
 			cur->callid, 
 			cur->ocseq, cur->icseq, 
 			ast_getformatname(cur->owner ? cur->owner->nativeformats : 0), cur->needdestroy ? "(d)" : "" );
@@ -5899,7 +5896,7 @@ static int __sip_show_channels(int fd, int argc, char *argv[], int subscriptions
 		}
 		if (cur->subscribed && subscriptions) {
                    ast_cli(fd, FORMAT3, ast_inet_ntoa(iabuf, sizeof(iabuf), cur->sa.sin_addr),
-			ast_strlen_zero(cur->username) ? ( ast_strlen_zero(cur->callerid) ? "(None)" : cur->callerid ) : cur->username, 
+			ast_strlen_zero(cur->username) ? ( ast_strlen_zero(cur->cid_num) ? "(None)" : cur->cid_num ) : cur->username, 
                         cur->callid, cur->uri);
 
                 }
@@ -5976,8 +5973,8 @@ static int sip_show_channel(int fd, int argc, char *argv[])
 			   ast_cli(fd, "  Peername:               %s\n", cur->peername);
 			if (!ast_strlen_zero(cur->uri))
 			   ast_cli(fd, "  Original uri:           %s\n", cur->uri);
-			if (!ast_strlen_zero(cur->callerid))
-			   ast_cli(fd, "  Caller-ID:              %s\n", cur->callerid);
+			if (!ast_strlen_zero(cur->cid_num))
+			   ast_cli(fd, "  Caller-ID:              %s\n", cur->cid_num);
 			ast_cli(fd, "  Need Destroy:           %d\n", cur->needdestroy);
 			ast_cli(fd, "  Last Message:           %s\n", cur->lastmsg);
 			ast_cli(fd, "  Promiscuous Redir:      %s\n", cur->promiscredir ? "Yes" : "No");
@@ -8106,7 +8103,7 @@ static struct sip_user *build_user(char *name, struct ast_variable *v)
 				else
 					user->nat = SIP_NAT_RFC3581;
 			} else if (!strcasecmp(v->name, "callerid")) {
-				strncpy(user->callerid, v->value, sizeof(user->callerid)-1);
+				ast_callerid_split(v->value, user->cid_name, sizeof(user->cid_name), user->cid_num, sizeof(user->cid_num));
 				user->hascallerid=1;
 			} else if (!strcasecmp(v->name, "callgroup")) {
 				user->callgroup = ast_get_group(v->value);
@@ -8147,8 +8144,8 @@ static struct sip_user *build_user(char *name, struct ast_variable *v)
 					user->capability &= ~format;
 			} else if (!strcasecmp(v->name, "insecure")) {
 				user->insecure = ast_true(v->value);
-			} else if (!strcasecmp(v->name, "restrictcid")) {
-				user->restrictcid = ast_true(v->value);
+			} else if (!strcasecmp(v->name, "callingpres")) {
+				user->callingpres = atoi(v->value);
 			} else if (!strcasecmp(v->name, "trustrpid")) {
 				user->trustrpid = ast_true(v->value);
 			} else if (!strcasecmp(v->name, "progressinband")) {
diff --git a/channels/chan_skinny.c b/channels/chan_skinny.c
index c58315b4d0..56abeb14e0 100755
--- a/channels/chan_skinny.c
+++ b/channels/chan_skinny.c
@@ -488,7 +488,8 @@ static pthread_t accept_t;
 static char context[AST_MAX_EXTENSION] = "default";
 static char language[MAX_LANGUAGE] = "";
 static char musicclass[MAX_LANGUAGE] = "";
-static char callerid[AST_MAX_EXTENSION] = "";
+static char cid_num[AST_MAX_EXTENSION] = "";
+static char cid_name[AST_MAX_EXTENSION] = "";
 static char linelabel[AST_MAX_EXTENSION] ="";
 static int nat = 0;
 static unsigned int cur_callergroup = 0;
@@ -634,7 +635,8 @@ struct skinny_line {
 	char exten[AST_MAX_EXTENSION];			/* Extention where to start */
 	char context[AST_MAX_EXTENSION];
 	char language[MAX_LANGUAGE];
-	char callerid[AST_MAX_EXTENSION];		/* Caller*ID */
+	char cid_num[AST_MAX_EXTENSION];		/* Caller*ID */
+	char cid_name[AST_MAX_EXTENSION];		/* Caller*ID */
 	char lastcallerid[AST_MAX_EXTENSION];		/* Last Caller*ID */
 	char call_forward[AST_MAX_EXTENSION];	
 	char mailbox[AST_MAX_EXTENSION];
@@ -1060,9 +1062,10 @@ static struct skinny_device *build_device(char *cat, struct ast_variable *v)
 				nat = ast_true(v->value);
 			} else if (!strcasecmp(v->name, "callerid")) {
 				if (!strcasecmp(v->value, "asreceived")) {
-					callerid[0] = '\0';
+					cid_num[0] = '\0';
+					cid_name[0] = '\0';
 				} else {
-					strncpy(callerid, v->value, sizeof(callerid) - 1);
+					ast_callerid_split(v->value, cid_name, sizeof(cid_name), cid_num, sizeof(cid_num));
 				}
 			} else if (!strcasecmp(v->name, "language")) {
 				strncpy(language, v->value, sizeof(language)-1);
@@ -1109,7 +1112,8 @@ static struct skinny_device *build_device(char *cat, struct ast_variable *v)
 					/* XXX Should we check for uniqueness?? XXX */
 					
 					strncpy(l->context, context, sizeof(l->context) - 1);
-					strncpy(l->callerid, callerid, sizeof(l->callerid) - 1);
+					strncpy(l->cid_num, cid_num, sizeof(l->cid_num) - 1);
+					strncpy(l->cid_name, cid_name, sizeof(l->cid_name) - 1);
 					strncpy(l->label, linelabel, sizeof(l->label) - 1);
 					strncpy(l->language, language, sizeof(l->language) - 1);
         				strncpy(l->musicclass, musicclass, sizeof(l->musicclass)-1);
@@ -1272,8 +1276,8 @@ static void *skinny_ss(void *data)
         if (!ast_ignore_pattern(chan->context, exten)) {
 			transmit_tone(s, SKINNY_SILENCE);
         } 
-        if (ast_exists_extension(chan, chan->context, exten, 1, l->callerid)) {
-            if (!res || !ast_matchmore_extension(chan, chan->context, exten, 1, l->callerid)) {
+        if (ast_exists_extension(chan, chan->context, exten, 1, l->cid_num)) {
+            if (!res || !ast_matchmore_extension(chan, chan->context, exten, 1, l->cid_num)) {
                 if (getforward) {
                     /* Record this as the forwarding extension */
                     strncpy(l->call_forward, exten, sizeof(l->call_forward) - 1); 
@@ -1294,10 +1298,10 @@ static void *skinny_ss(void *data)
                     getforward = 0;
                 } else  {
                     strncpy(chan->exten, exten, sizeof(chan->exten)-1);
-                    if (!ast_strlen_zero(l->callerid)) {
+                    if (!ast_strlen_zero(l->cid_num)) {
                         if (!l->hidecallerid)
-                            chan->callerid = strdup(l->callerid);
-                        chan->ani = strdup(l->callerid);
+                            chan->cid.cid_num = strdup(l->cid_num);
+                        chan->cid.cid_ani = strdup(l->cid_num);
                     }
                     ast_setstate(chan, AST_STATE_RING);
                     res = ast_pbx_run(chan);
@@ -1346,9 +1350,14 @@ static void *skinny_ss(void *data)
             }
             /* Disable Caller*ID if enabled */
             l->hidecallerid = 1;
-            if (chan->callerid)
-                free(chan->callerid);
-            chan->callerid = NULL;
+            if (chan->cid.cid_num)
+                free(chan->cid.cid_num);
+            chan->cid.cid_num = NULL;
+            
+			if (chan->cid.cid_name)
+                free(chan->cid.cid_name);
+            chan->cid.cid_name = NULL;
+			
             transmit_tone(s, SKINNY_DIALTONE);
             len = 0;
             memset(exten, 0, sizeof(exten));
@@ -1422,17 +1431,23 @@ static void *skinny_ss(void *data)
             }
             /* Enable Caller*ID if enabled */
             l->hidecallerid = 0;
-            if (chan->callerid)
-                free(chan->callerid);
-            if (!ast_strlen_zero(l->callerid))
-                chan->callerid = strdup(l->callerid);
+            if (chan->cid.cid_num)
+                free(chan->cid.cid_num);
+            if (!ast_strlen_zero(l->cid_num))
+                chan->cid.cid_num = strdup(l->cid_num);
+
+            if (chan->cid.cid_name)
+                free(chan->cid.cid_name);
+            if (!ast_strlen_zero(l->cid_name))
+                chan->cid.cid_name = strdup(l->cid_name);
+
             transmit_tone(s, SKINNY_DIALTONE);
             len = 0;
             memset(exten, 0, sizeof(exten));
             timeout = firstdigittimeout;
-        } else if (!ast_canmatch_extension(chan, chan->context, exten, 1, chan->callerid) &&
+        } else if (!ast_canmatch_extension(chan, chan->context, exten, 1, chan->cid.cid_num) &&
                         ((exten[0] != '*') || (!ast_strlen_zero(exten) > 2))) {
-            ast_log(LOG_WARNING, "Can't match [%s] from '%s' in context %s\n", exten, chan->callerid ? chan->callerid : "<Unknown Caller>", chan->context);
+            ast_log(LOG_WARNING, "Can't match [%s] from '%s' in context %s\n", exten, chan->cid.cid_num ? chan->cid.cid_num : "<Unknown Caller>", chan->context);
             transmit_tone(s, SKINNY_REORDER); 
 			sleep(3); // hang out for 3 seconds to let congestion play
 			break;
@@ -1795,8 +1810,11 @@ static struct ast_channel *skinny_new(struct skinny_subchannel *sub, int state)
 		strncpy(tmp->call_forward, l->call_forward, sizeof(tmp->call_forward) - 1);
 		strncpy(tmp->context, l->context, sizeof(tmp->context)-1);
 		strncpy(tmp->exten,l->exten, sizeof(tmp->exten)-1);
-		if (!ast_strlen_zero(l->callerid)) {
-			tmp->callerid = strdup(l->callerid);
+		if (!ast_strlen_zero(l->cid_num)) {
+			tmp->cid.cid_num = strdup(l->cid_num);
+		}
+		if (!ast_strlen_zero(l->cid_name)) {
+			tmp->cid.cid_name = strdup(l->cid_name);
 		}
 		tmp->priority = 1;
 		if (state != AST_STATE_DOWN) {
diff --git a/channels/chan_zap.c b/channels/chan_zap.c
index c5915ee263..ec722be600 100755
--- a/channels/chan_zap.c
+++ b/channels/chan_zap.c
@@ -154,7 +154,8 @@ static char *config = "zapata.conf";
 #define DCHAN_AVAILABLE	(DCHAN_PROVISIONED | DCHAN_NOTINALARM | DCHAN_UP)
 
 static char context[AST_MAX_EXTENSION] = "default";
-static char callerid[256] = "";
+static char cid_num[256] = "";
+static char cid_name[256] = "";
 
 static char language[MAX_LANGUAGE] = "";
 static char musicclass[MAX_LANGUAGE] = "";
@@ -455,10 +456,14 @@ static struct zt_pvt {
 	char exten[AST_MAX_EXTENSION];
 	char language[MAX_LANGUAGE];
 	char musicclass[MAX_LANGUAGE];
-	char callerid[AST_MAX_EXTENSION];
-	char lastcallerid[AST_MAX_EXTENSION];
-	char *origcallerid;			/* malloced original callerid */
-	char callwaitcid[AST_MAX_EXTENSION];
+	char cid_num[AST_MAX_EXTENSION];
+	char cid_name[AST_MAX_EXTENSION];
+	char lastcid_num[AST_MAX_EXTENSION];
+	char lastcid_name[AST_MAX_EXTENSION];
+	char *origcid_num;			/* malloced original callerid */
+	char *origcid_name;			/* malloced original callerid */
+	char callwait_num[AST_MAX_EXTENSION];
+	char callwait_name[AST_MAX_EXTENSION];
 	char rdnis[AST_MAX_EXTENSION];
 	char dnid[AST_MAX_EXTENSION];
 	unsigned int group;
@@ -1383,13 +1388,13 @@ int send_cwcidspill(struct zt_pvt *p)
 	p->cidspill = malloc(MAX_CALLERID_SIZE);
 	if (p->cidspill) {
 		memset(p->cidspill, 0x7f, MAX_CALLERID_SIZE);
-		p->cidlen = ast_callerid_callwaiting_generate(p->cidspill, p->callwaitcid, AST_LAW(p));
+		p->cidlen = ast_callerid_callwaiting_generate(p->cidspill, p->callwait_name, p->callwait_num, AST_LAW(p));
 		/* Make sure we account for the end */
 		p->cidlen += READ_SIZE * 4;
 		p->cidpos = 0;
 		send_callerid(p);
 		if (option_verbose > 2)
-			ast_verbose(VERBOSE_PREFIX_3 "CPE supports Call Waiting Caller*ID.  Sending '%s'\n", p->callwaitcid);
+			ast_verbose(VERBOSE_PREFIX_3 "CPE supports Call Waiting Caller*ID.  Sending '%s/%s'\n", p->callwait_name, p->callwait_num);
 	} else return -1;
 	return 0;
 }
@@ -1472,7 +1477,6 @@ static int zt_call(struct ast_channel *ast, char *rdest, int timeout)
 #ifdef ZAPATA_PRI
 	char *s=NULL;
 #endif
-	char callerid[256];
 	char dest[256]; /* must be same length as p->dialdest */
 	ast_mutex_lock(&p->lock);
 	strncpy(dest, rdest, sizeof(dest) - 1);
@@ -1521,7 +1525,7 @@ static int zt_call(struct ast_channel *ast, char *rdest, int timeout)
 				p->cidspill = malloc(MAX_CALLERID_SIZE);
 				p->callwaitcas = 0;
 				if (p->cidspill) {
-					p->cidlen = ast_callerid_generate(p->cidspill, ast->callerid, AST_LAW(p));
+					p->cidlen = ast_callerid_generate(p->cidspill, ast->cid.cid_name, ast->cid.cid_num, AST_LAW(p));
 					p->cidpos = 0;
 					send_callerid(p);
 				} else
@@ -1564,10 +1568,14 @@ static int zt_call(struct ast_channel *ast, char *rdest, int timeout)
 		} else {
 			/* Call waiting call */
 			p->callwaitrings = 0;
-			if (ast->callerid)
-				strncpy(p->callwaitcid, ast->callerid, sizeof(p->callwaitcid)-1);
+			if (ast->cid.cid_num)
+				strncpy(p->callwait_num, ast->cid.cid_num, sizeof(p->callwait_num)-1);
 			else
-				p->callwaitcid[0] = '\0';
+				p->callwait_num[0] = '\0';
+			if (ast->cid.cid_name)
+				strncpy(p->callwait_name, ast->cid.cid_name, sizeof(p->callwait_name)-1);
+			else
+				p->callwait_name[0] = '\0';
 			/* Call waiting tone instead */
 			if (zt_callwait(ast)) {
 				ast_mutex_unlock(&p->lock);
@@ -1578,20 +1586,16 @@ static int zt_call(struct ast_channel *ast, char *rdest, int timeout)
 				ast_log(LOG_WARNING, "Unable to generate call-wait ring-back on channel %s\n", ast->name);
 				
 		}
-		if (ast->callerid) 
-			strncpy(callerid, ast->callerid, sizeof(callerid)-1);
-		else
-			callerid[0] = '\0';
-		ast_callerid_parse(callerid, &n, &l);
-		if (l) {
-			ast_shrink_phone_number(l);
-			if (!ast_isphonenumber(l))
-				l = NULL;
-		}
+		n = ast->cid.cid_name;
+		l = ast->cid.cid_num;
 		if (l)
-			strncpy(p->lastcallerid, l, sizeof(p->lastcallerid) - 1);
+			strncpy(p->lastcid_num, l, sizeof(p->lastcid_num) - 1);
 		else
-			p->lastcallerid[0] = '\0';
+			p->lastcid_num[0] = '\0';
+		if (n)
+			strncpy(p->lastcid_name, n, sizeof(p->lastcid_name) - 1);
+		else
+			p->lastcid_name[0] = '\0';
 		ast_setstate(ast, AST_STATE_RINGING);
 		index = zt_get_index(ast, p, 0);
 		if (index > -1) {
@@ -1642,32 +1646,14 @@ static int zt_call(struct ast_channel *ast, char *rdest, int timeout)
 		ast_log(LOG_DEBUG, "Dialing '%s'\n", c);
 		p->dop.op = ZT_DIAL_OP_REPLACE;
 		if (p->sig == SIG_FEATD) {
-			if (ast->callerid) {
-				strncpy(callerid, ast->callerid, sizeof(callerid)-1);
-				ast_callerid_parse(callerid, &n, &l);
-				if (l) {
-					ast_shrink_phone_number(l);
-					if (!ast_isphonenumber(l))
-						l = NULL;
-				}
-			} else
-				l = NULL;
+			l = ast->cid.cid_num;
 			if (l) 
 				snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T*%s*%s*", l, c + p->stripmsd);
 			else
 				snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T**%s*", c + p->stripmsd);
 		} else 
 		if (p->sig == SIG_FEATDMF) {
-			if (ast->callerid) {
-				strncpy(callerid, ast->callerid, sizeof(callerid)-1);
-				ast_callerid_parse(callerid, &n, &l);
-				if (l) {
-					ast_shrink_phone_number(l);
-					if (!ast_isphonenumber(l))
-						l = NULL;
-				}
-			} else
-				l = NULL;
+			l = ast->cid.cid_num;
 			if (l) 
 				snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*00%s#*%s#", l, c + p->stripmsd);
 			else
@@ -1726,16 +1712,13 @@ static int zt_call(struct ast_channel *ast, char *rdest, int timeout)
 			c++;
 		else
 			c = dest;
-		if (ast->callerid && !p->hidecallerid) {
-			strncpy(callerid, ast->callerid, sizeof(callerid)-1);
-			ast_callerid_parse(callerid, &n, &l);
-			if (l) {
-				ast_shrink_phone_number(l);
-				if (!ast_isphonenumber(l))
-					l = NULL;
-			}
-		} else
+		if (!p->hidecallerid) {
+			l = ast->cid.cid_num;
+			n = ast->cid.cid_name;
+		} else {
 			l = NULL;
+			n = NULL;
+		}
 		if (strlen(c) < p->stripmsd) {
 			ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
 			ast_mutex_unlock(&p->lock);
@@ -1786,8 +1769,7 @@ static int zt_call(struct ast_channel *ast, char *rdest, int timeout)
 						((p->law == ZT_LAW_ALAW) ? PRI_LAYER_1_ALAW : PRI_LAYER_1_ULAW)));
 		pri_sr_set_called(sr, c + p->stripmsd, p->pri->dialplan - 1,  s ? 1 : 0);
 		pri_sr_set_caller(sr, l, n, p->pri->localdialplan - 1, 
-					l ? (ast->restrictcid ? PRES_PROHIB_USER_NUMBER_PASSED_SCREEN : 
-						(p->use_callingpres ? ast->callingpres : PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN)) : 
+					l ? (p->use_callingpres ? ast->cid.cid_pres : PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN) : 
 						 PRES_NUMBER_NOT_AVAILABLE);
 		if (pri_setup(p->pri->pri, p->call,  sr)) {
 			ast_log(LOG_WARNING, "Unable to setup call to %s\n", c + p->stripmsd);
@@ -1965,10 +1947,15 @@ static int zt_hangup(struct ast_channel *ast)
 	x = 0;
 	zt_confmute(p, 0);
 	restore_gains(p);
-	if (p->origcallerid) {
-		strncpy(p->callerid, p->origcallerid, sizeof(p->callerid) - 1);
-		free(p->origcallerid);
-		p->origcallerid = NULL;
+	if (p->origcid_num) {
+		strncpy(p->cid_num, p->origcid_num, sizeof(p->cid_num) - 1);
+		free(p->origcid_num);
+		p->origcid_num = NULL;
+	}	
+	if (p->origcid_name) {
+		strncpy(p->cid_name, p->origcid_name, sizeof(p->cid_name) - 1);
+		free(p->origcid_name);
+		p->origcid_name = NULL;
 	}	
 	if (p->dsp)
 		ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_DTMF | p->dtmfrelax);
@@ -3462,10 +3449,15 @@ static struct ast_frame *zt_handle_event(struct ast_channel *ast)
 						if (p->subs[SUB_REAL].owner->bridge)
 								ast_moh_stop(p->subs[SUB_REAL].owner->bridge);
 					} else if (!p->subs[SUB_THREEWAY].owner) {
-						char callerid[256];
+						char cid_num[256]="";
+						char cid_name[256]="";
 						if (p->threewaycalling && !check_for_conference(p)) {
-							if (p->zaptrcallerid && p->owner && p->owner->callerid)
-								strncpy(callerid, p->owner->callerid, sizeof(callerid) - 1);
+							if (p->zaptrcallerid && p->owner) {
+								if (p->owner->cid.cid_num)
+									strncpy(cid_num, p->owner->cid.cid_num, sizeof(cid_num) - 1);
+								if (p->owner->cid.cid_name)
+									strncpy(cid_name, p->owner->cid.cid_name, sizeof(cid_name) - 1);
+							}
 							/* XXX This section needs much more error checking!!! XXX */
 							/* Start a 3-way call if feasible */
 							if ((ast->pbx) ||
@@ -3475,13 +3467,20 @@ static struct ast_frame *zt_handle_event(struct ast_channel *ast)
 									/* Make new channel */
 									chan = zt_new(p, AST_STATE_RESERVED, 0, SUB_THREEWAY, 0, 0);
 									if (p->zaptrcallerid) {
-										if (!p->origcallerid) {
-											p->origcallerid = malloc(strlen(p->callerid) + 1);
-											strncpy(p->origcallerid, p->callerid, strlen(p->callerid)); /* safe */
+										if (!p->origcid_num) {
+											p->origcid_num = malloc(strlen(p->origcid_num) + 1);
+											strncpy(p->origcid_num, p->cid_num, strlen(p->cid_num)); /* safe */
+											/* make sure p->origcallerid is terminated */
+											p->origcid_num[strlen(p->cid_num)] = '\0';
+										}
+										if (!p->origcid_name) {
+											p->origcid_name = malloc(strlen(p->origcid_name) + 1);
+											strncpy(p->origcid_name, p->cid_name, strlen(p->cid_name)); /* safe */
 											/* make sure p->origcallerid is terminated */
-											p->origcallerid[strlen(p->callerid)] = '\0';
+											p->origcid_name[strlen(p->cid_name)] = '\0';
 										}
-										strncpy(p->callerid, callerid, sizeof(p->callerid) -1);
+										strncpy(p->cid_num, cid_num, sizeof(p->cid_num) -1);
+										strncpy(p->cid_name, cid_name, sizeof(p->cid_name) -1);
 									}
 									/* Swap things around between the three-way and real call */
 									swap_subs(p, SUB_THREEWAY, SUB_REAL);
@@ -3853,7 +3852,10 @@ struct ast_frame  *zt_read(struct ast_channel *ast)
 	}
 
 	if (p->subs[index].needcallerid) {
-		ast_set_callerid(ast, !ast_strlen_zero(p->lastcallerid) ? p->lastcallerid : NULL, 1);
+		ast_set_callerid(ast, !ast_strlen_zero(p->lastcid_num) ? p->lastcid_num : NULL, 
+							!ast_strlen_zero(p->lastcid_name) ? p->lastcid_name : NULL,
+							!ast_strlen_zero(p->lastcid_num) ? p->lastcid_num : NULL
+							);
 		p->subs[index].needcallerid = 0;
 	}
 	
@@ -4033,7 +4035,7 @@ struct ast_frame  *zt_read(struct ast_channel *ast)
 			if (!p->faxhandled) {
 				p->faxhandled++;
 				if (strcmp(ast->exten, "fax")) {
-					if (ast_exists_extension(ast, ast->context, "fax", 1, ast->callerid)) {
+					if (ast_exists_extension(ast, ast->context, "fax", 1, ast->cid.cid_num)) {
 						if (option_verbose > 2)
 							ast_verbose(VERBOSE_PREFIX_3 "Redirecting %s to fax extension\n", ast->name);
 						/* Save the DID/DNIS when we transfer the fax call to a "fax" extension */
@@ -4441,15 +4443,17 @@ static struct ast_channel *zt_new(struct zt_pvt *i, int state, int startpbx, int
 		if (!ast_strlen_zero(i->exten))
 			strncpy(tmp->exten, i->exten, sizeof(tmp->exten)-1);
 		if (!ast_strlen_zero(i->rdnis))
-			tmp->rdnis = strdup(i->rdnis);
+			tmp->cid.cid_rdnis = strdup(i->rdnis);
 		if (!ast_strlen_zero(i->dnid))
-			tmp->dnid = strdup(i->dnid);
-		if (!ast_strlen_zero(i->callerid)) {
-			tmp->callerid = strdup(i->callerid);
-			tmp->ani = strdup(i->callerid);
+			tmp->cid.cid_dnid = strdup(i->dnid);
+		if (!ast_strlen_zero(i->cid_num)) {
+			tmp->cid.cid_num = strdup(i->cid_num);
+			tmp->cid.cid_ani = strdup(i->cid_num);
+		}
+		if (!ast_strlen_zero(i->cid_name)) {
+			tmp->cid.cid_name = strdup(i->cid_name);
 		}
-		tmp->restrictcid = i->restrictcid;
-		tmp->callingpres = i->callingpres;
+		tmp->cid.cid_pres = i->callingpres;
 #ifdef ZAPATA_PRI
 		set_calltype(tmp, ctype);
 		/* Assume calls are not idle calls unless we're told differently */
@@ -4548,7 +4552,6 @@ static void *ss_thread(void *data)
 	char exten[AST_MAX_EXTENSION]="";
 	char exten2[AST_MAX_EXTENSION]="";
 	unsigned char buf[256];
-	char cid[256];
 	char dtmfcid[300];
 	char dtmfbuf[300];
 	struct callerid_state *cs;
@@ -4584,12 +4587,12 @@ static void *ss_thread(void *data)
 		strncpy(exten, p->exten, sizeof(exten) - 1);
 		len = strlen(exten);
 		res = 0;
-		while((len < AST_MAX_EXTENSION-1) && ast_matchmore_extension(chan, chan->context, exten, 1, p->callerid)) {
+		while((len < AST_MAX_EXTENSION-1) && ast_matchmore_extension(chan, chan->context, exten, 1, p->cid_num)) {
 			if (len && !ast_ignore_pattern(chan->context, exten))
 				tone_zone_play_tone(p->subs[index].zfd, -1);
 			else
 				tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_DIALTONE);
-			if (ast_exists_extension(chan, chan->context, exten, 1, p->callerid))
+			if (ast_exists_extension(chan, chan->context, exten, 1, p->cid_num))
 				timeout = matchdigittimeout;
 			else
 				timeout = gendigittimeout;
@@ -4604,7 +4607,7 @@ static void *ss_thread(void *data)
 				break;
 		}
 		tone_zone_play_tone(p->subs[index].zfd, -1);
-		if (ast_exists_extension(chan, chan->context, exten, 1, p->callerid)) {
+		if (ast_exists_extension(chan, chan->context, exten, 1, p->cid_num)) {
 			/* Start the real PBX */
 			strncpy(chan->exten, exten, sizeof(chan->exten) - 1);
 			ast_dsp_digitreset(p->dsp);
@@ -4710,12 +4713,12 @@ static void *ss_thread(void *data)
 				s1 = strsep(&stringp, "*");
 				s2 = strsep(&stringp, "*");
 				if (s2) {
-					if (!ast_strlen_zero(p->callerid))
-						chan->callerid = strdup(p->callerid);
+					if (!ast_strlen_zero(p->cid_num))
+						chan->cid.cid_num = strdup(p->cid_num);
 					else
-						chan->callerid = strdup(s1);
-					if (chan->callerid)
-						chan->ani = strdup(chan->callerid);
+						chan->cid.cid_num = strdup(s1);
+					if (chan->cid.cid_num)
+						chan->cid.cid_ani = strdup(chan->cid.cid_num);
 					strncpy(exten, s2, sizeof(exten)-1);
 				} else
 					strncpy(exten, s1, sizeof(exten)-1);
@@ -4731,12 +4734,12 @@ static void *ss_thread(void *data)
 				s1 = strsep(&stringp, "#");
 				s2 = strsep(&stringp, "#");
 				if (s2) {
-					if (!ast_strlen_zero(p->callerid))
-						chan->callerid = strdup(p->callerid);
+					if (!ast_strlen_zero(p->cid_num))
+						chan->cid.cid_num = strdup(p->cid_num);
 					else
-						if (*(s1 + 2)) chan->callerid = strdup(s1 + 2);
-					if (chan->callerid)
-						chan->ani = strdup(chan->callerid);
+						if (*(s1 + 2)) chan->cid.cid_num = strdup(s1 + 2);
+					if (chan->cid.cid_num)
+						chan->cid.cid_ani = strdup(chan->cid.cid_num);
 					strncpy(exten, s2 + 1, sizeof(exten)-1);
 				} else
 					strncpy(exten, s1 + 2, sizeof(exten)-1);
@@ -4752,13 +4755,13 @@ static void *ss_thread(void *data)
 				s1 = strsep(&stringp, "#");
 				s2 = strsep(&stringp, "#");
 				if (s2 && (*(s2 + 1) == '0')) {
-					if (*(s2 + 2)) chan->callerid = strdup(s2 + 2);
-					if (chan->callerid)
-						chan->ani = strdup(chan->callerid);
+					if (*(s2 + 2)) chan->cid.cid_num = strdup(s2 + 2);
+					if (chan->cid.cid_num)
+						chan->cid.cid_ani = strdup(chan->cid.cid_num);
 					}
 				if (s1)	strncpy(exten, s1, sizeof(exten)-1);
 				else strncpy(exten, "911", sizeof(exten) - 1);
-				printf("E911: exten: %s, ANI: %s\n",exten,chan->ani);
+				printf("E911: exten: %s, ANI: %s\n",exten, chan->cid.cid_ani);
 			} else
 				ast_log(LOG_WARNING, "Got a non-E911 input on channel %d.  Assuming E&M Wink instead\n", p->channel);
 		}
@@ -4779,7 +4782,7 @@ static void *ss_thread(void *data)
 		zt_enable_ec(p);
 		if ((p->sig == SIG_FEATDMF) || (p->sig == SIG_E911) || (p->sig == SIG_FEATB)) 
 			ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_DTMF | p->dtmfrelax); 
-		if (ast_exists_extension(chan, chan->context, exten, 1, chan->callerid)) {
+		if (ast_exists_extension(chan, chan->context, exten, 1, chan->cid.cid_num)) {
 			strncpy(chan->exten, exten, sizeof(chan->exten)-1);
 			ast_dsp_digitreset(p->dsp);
 			res = ast_pbx_run(chan);
@@ -4835,8 +4838,8 @@ static void *ss_thread(void *data)
 				tone_zone_play_tone(p->subs[index].zfd, -1);
 			else
 				tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_DIALTONE);
-			if (ast_exists_extension(chan, chan->context, exten, 1, p->callerid) && strcmp(exten, ast_parking_ext())) {
-				if (!res || !ast_matchmore_extension(chan, chan->context, exten, 1, p->callerid)) {
+			if (ast_exists_extension(chan, chan->context, exten, 1, p->cid_num) && strcmp(exten, ast_parking_ext())) {
+				if (!res || !ast_matchmore_extension(chan, chan->context, exten, 1, p->cid_num)) {
 					if (getforward) {
 						/* Record this as the forwarding extension */
 						strncpy(p->call_forward, exten, sizeof(p->call_forward) - 1); 
@@ -4855,10 +4858,14 @@ static void *ss_thread(void *data)
 					} else  {
 						res = tone_zone_play_tone(p->subs[index].zfd, -1);
 						strncpy(chan->exten, exten, sizeof(chan->exten)-1);
-						if (!ast_strlen_zero(p->callerid)) {
+						if (!ast_strlen_zero(p->cid_num)) {
 							if (!p->hidecallerid)
-								chan->callerid = strdup(p->callerid);
-							chan->ani = strdup(p->callerid);
+								chan->cid.cid_num = strdup(p->cid_num);
+							chan->cid.cid_ani = strdup(p->cid_num);
+						}
+						if (!ast_strlen_zero(p->cid_name)) {
+							if (!p->hidecallerid)
+								chan->cid.cid_name = strdup(p->cid_name);
 						}
 						ast_setstate(chan, AST_STATE_RING);
 						zt_enable_ec(p);
@@ -4928,9 +4935,12 @@ static void *ss_thread(void *data)
 					ast_verbose(VERBOSE_PREFIX_3 "Disabling Caller*ID on %s\n", chan->name);
 				/* Disable Caller*ID if enabled */
 				p->hidecallerid = 1;
-				if (chan->callerid)
-					free(chan->callerid);
-				chan->callerid = NULL;
+				if (chan->cid.cid_num)
+					free(chan->cid.cid_num);
+				chan->cid.cid_num = NULL;
+				if (chan->cid.cid_name)
+					free(chan->cid.cid_name);
+				chan->cid.cid_name = NULL;
 				res = tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_DIALRECALL);
 				if (res) {
 					ast_log(LOG_WARNING, "Unable to do dial recall on channel %s: %s\n", 
@@ -4941,8 +4951,8 @@ static void *ss_thread(void *data)
 				timeout = firstdigittimeout;
 			} else if (p->callreturn && !strcmp(exten, "*69")) {
 				res = 0;
-				if (!ast_strlen_zero(p->lastcallerid)) {
-					res = ast_say_digit_str(chan, p->lastcallerid, "", chan->language);
+				if (!ast_strlen_zero(p->lastcid_num)) {
+					res = ast_say_digit_str(chan, p->lastcid_num, "", chan->language);
 				}
 				if (!res)
 					res = tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_DIALRECALL);
@@ -4987,10 +4997,10 @@ static void *ss_thread(void *data)
 				if (option_verbose > 2)
 					ast_verbose(VERBOSE_PREFIX_3 "Parking call to '%s'\n", chan->name);
 				break;
-			} else if (!ast_strlen_zero(p->lastcallerid) && !strcmp(exten, "*60")) {
+			} else if (!ast_strlen_zero(p->lastcid_num) && !strcmp(exten, "*60")) {
 				if (option_verbose > 2)
-					ast_verbose(VERBOSE_PREFIX_3 "Blacklisting number %s\n", p->lastcallerid);
-				res = ast_db_put("blacklist", p->lastcallerid, "1");
+					ast_verbose(VERBOSE_PREFIX_3 "Blacklisting number %s\n", p->lastcid_num);
+				res = ast_db_put("blacklist", p->lastcid_num, "1");
 				if (!res) {
 					res = tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_DIALRECALL);
 					memset(exten, 0, sizeof(exten));
@@ -5001,10 +5011,16 @@ static void *ss_thread(void *data)
 					ast_verbose(VERBOSE_PREFIX_3 "Enabling Caller*ID on %s\n", chan->name);
 				/* Enable Caller*ID if enabled */
 				p->hidecallerid = 0;
-				if (chan->callerid)
-					free(chan->callerid);
-				if (!ast_strlen_zero(p->callerid))
-					chan->callerid = strdup(p->callerid);
+				if (chan->cid.cid_num)
+					free(chan->cid.cid_num);
+				chan->cid.cid_num = NULL;
+				if (chan->cid.cid_name)
+					free(chan->cid.cid_name);
+				chan->cid.cid_name = NULL;
+				if (!ast_strlen_zero(p->cid_num))
+					chan->cid.cid_num = strdup(p->cid_num);
+				if (!ast_strlen_zero(p->cid_name))
+					chan->cid.cid_name = strdup(p->cid_name);
 				res = tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_DIALRECALL);
 				if (res) {
 					ast_log(LOG_WARNING, "Unable to do dial recall on channel %s: %s\n", 
@@ -5047,10 +5063,10 @@ static void *ss_thread(void *data)
 					ast_hangup(chan);
 					return NULL;
 				}					
-			} else if (!ast_canmatch_extension(chan, chan->context, exten, 1, chan->callerid) &&
+			} else if (!ast_canmatch_extension(chan, chan->context, exten, 1, chan->cid.cid_num) &&
 							((exten[0] != '*') || (strlen(exten) > 2))) {
 				if (option_debug)
-					ast_log(LOG_DEBUG, "Can't match %s from '%s' in context %s\n", exten, chan->callerid ? chan->callerid : "<Unknown Caller>", chan->context);
+					ast_log(LOG_DEBUG, "Can't match %s from '%s' in context %s\n", exten, chan->cid.cid_num ? chan->cid.cid_num : "<Unknown Caller>", chan->context);
 				break;
 			}
 			if (!timeout)
@@ -5415,21 +5431,23 @@ static void *ss_thread(void *data)
 		}
 		else
 			cs = NULL;
-		if (name && number) {
-			snprintf(cid, sizeof(cid), "\"%s\" <%s>", name, number);
-		} else if (name) {
-			snprintf(cid, sizeof(cid), "\"%s\"", name);
-		} else if (number) {
-			snprintf(cid, sizeof(cid), "%s", number);
-		} else {
-			cid[0] = '\0';
+		if (chan->cid.cid_num) {
+			free(chan->cid.cid_num);
+			chan->cid.cid_num = NULL;
+		}
+		if (chan->cid.cid_name) {
+			free(chan->cid.cid_name);
+			chan->cid.cid_name = NULL;
+		}
+		if (number && !ast_strlen_zero(number)) {
+			chan->cid.cid_num = strdup(number);
+			chan->cid.cid_ani = strdup(number);
 		}
+		if (name && !ast_strlen_zero(name))
+			chan->cid.cid_name = strdup(name);
+
 		if (cs)
 			callerid_free(cs);
-		if (!ast_strlen_zero(cid)) {
-			chan->callerid = strdup(cid);
-			chan->ani = strdup(cid);
-		}
 		ast_setstate(chan, AST_STATE_RING);
 		chan->rings = 1;
 		p->ringt = RINGT;
@@ -6458,7 +6476,8 @@ static struct zt_pvt *mkintf(int channel, int signalling, int radio, struct zt_p
 		strncpy(tmp->language, language, sizeof(tmp->language)-1);
 		strncpy(tmp->musicclass, musicclass, sizeof(tmp->musicclass)-1);
 		strncpy(tmp->context, context, sizeof(tmp->context)-1);
-		strncpy(tmp->callerid, callerid, sizeof(tmp->callerid)-1);
+		strncpy(tmp->cid_num, cid_num, sizeof(tmp->cid_num)-1);
+		strncpy(tmp->cid_name, cid_name, sizeof(tmp->cid_name)-1);
 		strncpy(tmp->mailbox, mailbox, sizeof(tmp->mailbox)-1);
 		tmp->msgstate = -1;
 		tmp->group = cur_group;
@@ -7552,12 +7571,12 @@ static void *pri_dchannel(void *vpri)
 					pri->pvts[chanpos]->call = e->ring.call;
 					/* Get caller ID */
 					if (pri->pvts[chanpos]->use_callerid) {
-						if (!ast_strlen_zero(e->ring.callingname)) {
-							snprintf(pri->pvts[chanpos]->callerid, sizeof(pri->pvts[chanpos]->callerid), "\"%s\" <%s>", e->ring.callingname, e->ring.callingnum);
-						} else
-							strncpy(pri->pvts[chanpos]->callerid, e->ring.callingnum, sizeof(pri->pvts[chanpos]->callerid)-1);
-					} else
-						pri->pvts[chanpos]->callerid[0] = '\0';
+						strncpy(pri->pvts[chanpos]->cid_num, e->ring.callingnum, sizeof(pri->pvts[chanpos]->cid_num)-1);
+						strncpy(pri->pvts[chanpos]->cid_name, e->ring.callingname, sizeof(pri->pvts[chanpos]->cid_name)-1);
+					} else {
+						pri->pvts[chanpos]->cid_num[0] = '\0';
+						pri->pvts[chanpos]->cid_name[0] = '\0';
+					}
 					strncpy(pri->pvts[chanpos]->rdnis, e->ring.redirectingnum, sizeof(pri->pvts[chanpos]->rdnis) - 1);
 					/* If immediate=yes go to s|1 */
 					if (pri->pvts[chanpos]->immediate) {
@@ -7580,8 +7599,8 @@ static void *pri_dchannel(void *vpri)
 						pri->pvts[chanpos]->exten[1] = '\0';
 					}
 					/* Make sure extension exists (or in overlap dial mode, can exist) */
-					if ((pri->overlapdial && ast_canmatch_extension(NULL, pri->pvts[chanpos]->context, pri->pvts[chanpos]->exten, 1, pri->pvts[chanpos]->callerid)) ||
-						ast_exists_extension(NULL, pri->pvts[chanpos]->context, pri->pvts[chanpos]->exten, 1, pri->pvts[chanpos]->callerid)) {
+					if ((pri->overlapdial && ast_canmatch_extension(NULL, pri->pvts[chanpos]->context, pri->pvts[chanpos]->exten, 1, pri->pvts[chanpos]->cid_num)) ||
+						ast_exists_extension(NULL, pri->pvts[chanpos]->context, pri->pvts[chanpos]->exten, 1, pri->pvts[chanpos]->cid_num)) {
 						/* Setup law */
 						int law;
 						if (pri->switchtype != PRI_SWITCH_GR303_TMC) {
@@ -7612,7 +7631,7 @@ static void *pri_dchannel(void *vpri)
 						/* Get the use_callingpres state */
 						pri->pvts[chanpos]->callingpres = e->ring.callingpres;
 						/* Start PBX */
-						if (pri->overlapdial && ast_matchmore_extension(NULL, pri->pvts[chanpos]->context, pri->pvts[chanpos]->exten, 1, pri->pvts[chanpos]->callerid)) {
+						if (pri->overlapdial && ast_matchmore_extension(NULL, pri->pvts[chanpos]->context, pri->pvts[chanpos]->exten, 1, pri->pvts[chanpos]->cid_num)) {
 							/* Release the PRI lock while we create the channel */
 							ast_mutex_unlock(&pri->lock);
 							if (crv) {
@@ -7664,7 +7683,7 @@ static void *pri_dchannel(void *vpri)
 					} else {
 						if (option_verbose > 2)
 							ast_verbose(VERBOSE_PREFIX_3 "Extension '%s' in context '%s' from '%s' does not exist.  Rejecting call on channel %d/%d, span %d\n",
-								pri->pvts[chanpos]->exten, pri->pvts[chanpos]->context, pri->pvts[chanpos]->callerid, pri->pvts[chanpos]->logicalspan, 
+								pri->pvts[chanpos]->exten, pri->pvts[chanpos]->context, pri->pvts[chanpos]->cid_num, pri->pvts[chanpos]->logicalspan, 
 									pri->pvts[chanpos]->prioffset, pri->span);
 						pri_hangup(pri->pri, e->ring.call, PRI_CAUSE_UNALLOCATED);
 						pri->pvts[chanpos]->call = NULL;
@@ -7748,7 +7767,8 @@ static void *pri_dchannel(void *vpri)
 					} else {
 						/* Re-use *69 field for PRI */
 						ast_mutex_lock(&pri->pvts[chanpos]->lock);
-						snprintf(pri->pvts[chanpos]->lastcallerid, sizeof(pri->pvts[chanpos]->lastcallerid), "\"%s\" <%s>", e->facname.callingname, e->facname.callingnum);
+						strncpy(pri->pvts[chanpos]->lastcid_num, e->facname.callingnum, sizeof(pri->pvts[chanpos]->lastcid_num) - 1);
+						strncpy(pri->pvts[chanpos]->lastcid_name, e->facname.callingname, sizeof(pri->pvts[chanpos]->lastcid_name) - 1);
 						pri->pvts[chanpos]->subs[SUB_REAL].needcallerid =1;
 						zt_enable_ec(pri->pvts[chanpos]);
 						ast_mutex_unlock(&pri->pvts[chanpos]->lock);
@@ -8497,7 +8517,8 @@ static int zap_show_channel(int fd, int argc, char **argv)
 			ast_cli(fd, "Extension: %s\n", tmp->exten);
 			ast_cli(fd, "Dialing: %s\n", tmp->dialing ? "yes" : "no");
 			ast_cli(fd, "Context: %s\n", tmp->context);
-			ast_cli(fd, "Caller ID string: %s\n", tmp->callerid);
+			ast_cli(fd, "Caller ID: %s\n", tmp->cid_num);
+			ast_cli(fd, "Caller ID name: %s\n", tmp->cid_name);
 			ast_cli(fd, "Destroy: %d\n", tmp->destroy);
 			ast_cli(fd, "InAlarm: %d\n", tmp->inalarm);
 			ast_cli(fd, "Signalling Type: %s\n", sig2str(tmp->sig));
@@ -8639,7 +8660,7 @@ static int change_callingpres(struct ast_channel *chan, void *data)
 	int mode = 0;
 	if (data) {
 		mode = atoi((char *)data);
-		chan->callingpres = mode;
+		chan->cid.cid_pres = mode;
 	} else
 		ast_log(LOG_NOTICE, "Application %s requres an argument: %s(number)\n", app_callingpres,app_callingpres);
 	return 0;
@@ -9253,10 +9274,12 @@ static int setup_zap(void)
 				ast_log(LOG_WARNING, "Invalid tonezone: %s\n", v->value);
 			}
 		} else if (!strcasecmp(v->name, "callerid")) {
-			if (!strcasecmp(v->value, "asreceived"))
-				callerid[0] = '\0';
-			else
-				strncpy(callerid, v->value, sizeof(callerid)-1);
+			if (!strcasecmp(v->value, "asreceived")) {
+				cid_num[0] = '\0';
+				cid_name[0] = '\0';
+			} else {
+				ast_callerid_split(v->value, cid_name, sizeof(cid_name), cid_num, sizeof(cid_num));
+			}
 		} else if (!strcasecmp(v->name, "useincomingcalleridonzaptransfer")) {
 			zaptrcallerid = ast_true(v->value);
 		} else if (!strcasecmp(v->name, "restrictcid")) {
diff --git a/cli.c b/cli.c
index 613e3a22fc..c2623647df 100755
--- a/cli.c
+++ b/cli.c
@@ -370,7 +370,7 @@ static int handle_chanlist(int fd, int argc, char *argv[])
 		if(concise)
 			ast_cli(fd, CONCISE_FORMAT_STRING, c->name, c->context, c->exten, c->priority, ast_state2str(c->_state),
 					c->appl ? c->appl : "(None)", c->data ? ( !ast_strlen_zero(c->data) ? c->data : "" ): "",
-					(c->callerid && !ast_strlen_zero(c->callerid)) ? c->callerid : "",
+					(c->cid.cid_num && !ast_strlen_zero(c->cid.cid_num)) ? c->cid.cid_num : "",
 					(c->accountcode && !ast_strlen_zero(c->accountcode)) ? c->accountcode : "",c->amaflags);
 		else
 			ast_cli(fd, FORMAT_STRING, c->name, c->context, c->exten, c->priority, ast_state2str(c->_state),
@@ -590,6 +590,7 @@ static int handle_showchan(int fd, int argc, char *argv[])
 	"           Type: %s\n"
 	"       UniqueID: %s\n"
 	"      Caller ID: %s\n"
+	" Caller ID Name: %s\n"
 	"    DNID Digits: %s\n"
 	"          State: %s (%d)\n"
 	"          Rings: %d\n"
@@ -612,8 +613,9 @@ static int handle_showchan(int fd, int argc, char *argv[])
 	"          Stack: %d\n"
 	"    Blocking in: %s\n",
 	c->name, c->type, c->uniqueid,
-	(c->callerid ? c->callerid : "(N/A)"),
-	(c->dnid ? c->dnid : "(N/A)" ), ast_state2str(c->_state), c->_state, c->rings, c->nativeformats, c->writeformat, c->readformat,
+	(c->cid.cid_num ? c->cid.cid_num : "(N/A)"),
+	(c->cid.cid_name ? c->cid.cid_name : "(N/A)"),
+	(c->cid.cid_dnid ? c->cid.cid_dnid : "(N/A)" ), ast_state2str(c->_state), c->_state, c->rings, c->nativeformats, c->writeformat, c->readformat,
 	c->fds[0], c->fin & 0x7fffffff, (c->fin & 0x80000000) ? " (DEBUGGED)" : "",
 	c->fout & 0x7fffffff, (c->fout & 0x80000000) ? " (DEBUGGED)" : "", (long)c->whentohangup,
 	hour, min, sec, 
diff --git a/include/asterisk/callerid.h b/include/asterisk/callerid.h
index 19a6eeb9da..4008192c8a 100755
--- a/include/asterisk/callerid.h
+++ b/include/asterisk/callerid.h
@@ -3,9 +3,9 @@
  *
  * CallerID (and other GR30) Generation support 
  * 
- * Copyright (C) 1999, Mark Spencer
+ * Copyright (C) 1999-2004, Digium, Inc.
  *
- * Mark Spencer <markster@linux-support.net>
+ * Mark Spencer <markster@digium.com>
  *
  * This program is free software, distributed under the terms of
  * the GNU General Public License.
@@ -118,16 +118,16 @@ extern void callerid_free(struct callerid_state *cid);
  *
  * Acts like callerid_generate except uses an asterisk format callerid string.
  */
-extern int ast_callerid_generate(unsigned char *buf, char *astcid, int codec);
+extern int ast_callerid_generate(unsigned char *buf, char *name, char *number, int codec);
 
 //! Generate message waiting indicator 
 extern int vmwi_generate(unsigned char *buf, int active, int mdmf, int codec);
 
-//! Generate Caller-ID spill from the "callerid" field of asterisk (in e-mail address like format) but in a format suitable for Call Waiting(tm)'s Caller*ID(tm)
+//! Generate Caller-ID spill but in a format suitable for Call Waiting(tm)'s Caller*ID(tm)
 /*!
  * See ast_callerid_generate for other details
  */
-extern int ast_callerid_callwaiting_generate(unsigned char *buf, char *astcid, int codec);
+extern int ast_callerid_callwaiting_generate(unsigned char *buf, char *name, char *number, int codec);
 
 //! Destructively parse inbuf into name and location (or number)
 /*!
@@ -163,6 +163,9 @@ extern void ast_shrink_phone_number(char *n);
  */
 extern int ast_isphonenumber(char *n);
 
+extern int ast_callerid_split(const char *src, char *name, int namelen, char *num, int numlen);
+
+extern char *ast_callerid_merge(char *buf, int bufsiz, const char *name, const char *num);
 
 /*
  * Caller*ID and other GR-30 compatible generation
diff --git a/include/asterisk/channel.h b/include/asterisk/channel.h
index 75627cda74..9022e1a515 100755
--- a/include/asterisk/channel.h
+++ b/include/asterisk/channel.h
@@ -48,6 +48,27 @@ struct ast_generator {
 	int (*generate)(struct ast_channel *chan, void *data, int len, int samples);
 };
 
+struct ast_callerid {
+	/*! Malloc'd Dialed Number Identifier */
+	char *cid_dnid;				
+	/*! Malloc'd Caller Number */
+	char *cid_num;
+	/*! Malloc'd Caller Name */
+	char *cid_name;
+	/*! Malloc'd ANI */
+	char *cid_ani;			
+	/*! Malloc'd RDNIS */
+	char *cid_rdnis;
+	/*! Callerid presentation/screening */
+	int cid_pres;
+	/*! Callerid ANI 2 (Info digits) */
+	int cid_ani2;
+	/*! Callerid Type of Number */
+	int cid_ton;
+	/*! Callerid Transit Network Select */
+	int cid_tns;
+};
+
 //! Main Channel structure associated with a channel.
 /*! 
  * This is the side of it mostly used by the pbx and call management.
@@ -144,21 +165,8 @@ struct ast_channel {
 	/*! Requested write format */
 	int writeformat;			
 
-	
-	/*! Malloc'd Dialed Number Identifier */
-	char *dnid;				
-	/*! Malloc'd Caller ID */
-	char *callerid;
-	/*! Malloc'd ANI */
-	char *ani;			
-	/*! Malloc'd RDNIS */
-	char *rdnis;
-	/*! Hide callerid from user */
-	int restrictcid;
-	/*! Callerid presentation/screening */
-	int callingpres;
-
-	
+	struct ast_callerid cid;
+		
 	/*! Current extension context */
 	char context[AST_MAX_EXTENSION];	
 	/*! Current non-macro context */
@@ -288,7 +296,8 @@ struct chanmon;
 	oh.context = context; \
 	oh.exten = exten; \
 	oh.priority = priority; \
-	oh.callerid = callerid; \
+	oh.cid_num = cid_num; \
+	oh.cid_name = cid_name; \
 	oh.variable = variable; \
 	oh.account = account; \
 } 
@@ -297,7 +306,8 @@ struct outgoing_helper {
 	char *context;
 	char *exten;
 	int priority;
-	char *callerid;
+	char *cid_num;
+	char *cid_name;
 	char *variable;
 	char *account;
 };
@@ -401,9 +411,9 @@ int ast_device_state(char *device);
  * Returns an ast_channel on success or no answer, NULL on failure.  Check the value of chan->_state
  * to know if the call was answered or not.
  */
-struct ast_channel *ast_request_and_dial(char *type, int format, void *data, int timeout, int *reason, char *callerid);
+struct ast_channel *ast_request_and_dial(char *type, int format, void *data, int timeout, int *reason, char *cidnum, char *cidname);
 
-struct ast_channel *__ast_request_and_dial(char *type, int format, void *data, int timeout, int *reason, char *callerid, struct outgoing_helper *oh);
+struct ast_channel *__ast_request_and_dial(char *type, int format, void *data, int timeout, int *reason, char *cidnum, char *cidname, struct outgoing_helper *oh);
 
 //! Registers a channel
 /*! 
@@ -780,7 +790,7 @@ int ast_activate_generator(struct ast_channel *chan, struct ast_generator *gen,
 /*! Deactive an active generator */
 void ast_deactivate_generator(struct ast_channel *chan);
 
-void ast_set_callerid(struct ast_channel *chan, char *callerid, int  anitoo);
+void ast_set_callerid(struct ast_channel *chan, char *cidnum, char *cidname, char *ani);
 
 /*! Start a tone going */
 int ast_tonepair_start(struct ast_channel *chan, int freq1, int freq2, int duration, int vol);
diff --git a/include/asterisk/pbx.h b/include/asterisk/pbx.h
index f127a0e69f..5bec931edc 100755
--- a/include/asterisk/pbx.h
+++ b/include/asterisk/pbx.h
@@ -487,11 +487,11 @@ int ast_async_goto_by_name(char *chan, char *context, char *exten, int priority)
 
 /* Synchronously or asynchronously make an outbound call and send it to a
    particular extension */
-int ast_pbx_outgoing_exten(char *type, int format, void *data, int timeout, char *context, char *exten, int priority, int *reason, int sync, char *callerid, char *variable, char *account );
+int ast_pbx_outgoing_exten(char *type, int format, void *data, int timeout, char *context, char *exten, int priority, int *reason, int sync, char *cid_num, char *cid_name, char *variable, char *account );
 
 /* Synchronously or asynchronously make an outbound call and send it to a
    particular application with given extension */
-int ast_pbx_outgoing_app(char *type, int format, void *data, int timeout, char *app, char *appdata, int *reason, int sync, char *callerid, char *variable, char *account);
+int ast_pbx_outgoing_app(char *type, int format, void *data, int timeout, char *app, char *appdata, int *reason, int sync, char *cid_num, char *cid_name, char *variable, char *account);
 
 /* Functions for returning values from structures */
 char *ast_get_context_name(struct ast_context *con);
diff --git a/include/asterisk/vmodem.h b/include/asterisk/vmodem.h
index 69886154b0..c4f271d336 100755
--- a/include/asterisk/vmodem.h
+++ b/include/asterisk/vmodem.h
@@ -3,7 +3,7 @@
  *
  * Voice Modem Definitions
  * 
- * Copyright (C) 1999, Mark Spencer
+ * Copyright (C) 1999-2004, Digium, Inc.
  *
  * Mark Spencer <markster@digium.com>
  *
@@ -118,7 +118,9 @@ struct ast_modem_pvt {
 	/*! Group(s) we belong to if available */
 	unsigned int group;
 	/*! Caller ID if available */
-	char cid[AST_MAX_EXTENSION];	
+	char cid_name[AST_MAX_EXTENSION];	
+	/*! Caller ID if available */
+	char cid_num[AST_MAX_EXTENSION];	
 	/*! DTMF-detection mode (i4l/asterisk) */
 	int dtmfmode;
 	/*! DTMF-generation mode (i4l (outband) / asterisk (inband) */
diff --git a/manager.c b/manager.c
index 4f5e568ad3..02f730f57b 100755
--- a/manager.c
+++ b/manager.c
@@ -29,6 +29,7 @@
 #include <asterisk/file.h>
 #include <asterisk/manager.h>
 #include <asterisk/config.h>
+#include <asterisk/callerid.h>
 #include <asterisk/lock.h>
 #include <asterisk/logger.h>
 #include <asterisk/options.h>
@@ -46,7 +47,8 @@ struct fast_originate_helper
 	int timeout;
 	char app[256];
 	char appdata[256];
-	char callerid[256];
+	char cid_name[256];
+	char cid_num[256];
 	char variable[256];
 	char account[256];
 	char context[256];
@@ -693,6 +695,7 @@ static int action_status(struct mansession *s, struct message *m)
 			"Event: Status\r\n"
 			"Channel: %s\r\n"
 			"CallerID: %s\r\n"
+			"CallerIDName: %s\r\n"
 			"Account: %s\r\n"
 			"State: %s\r\n"
 			"Context: %s\r\n"
@@ -703,7 +706,9 @@ static int action_status(struct mansession *s, struct message *m)
 			"Uniqueid: %s\r\n"
 			"%s"
 			"\r\n",
-			c->name, c->callerid ? c->callerid : "<unknown>", 
+			c->name, 
+			c->cid.cid_num ? c->cid.cid_num : "<unknown>", 
+			c->cid.cid_name ? c->cid.cid_name : "<unknown>", 
 			c->accountcode,
 			ast_state2str(c->_state), c->context,
 			c->exten, c->priority, (long)elapsed_seconds, bridge, c->uniqueid, idText);
@@ -712,13 +717,16 @@ static int action_status(struct mansession *s, struct message *m)
 			"Event: Status\r\n"
 			"Channel: %s\r\n"
 			"CallerID: %s\r\n"
+			"CallerIDName: %s\r\n"
 			"Account: %s\r\n"
 			"State: %s\r\n"
 			"%s"
 			"Uniqueid: %s\r\n"
 			"%s"
 			"\r\n",
-			c->name, c->callerid ? c->callerid : "<unknown>", 
+			c->name, 
+			c->cid.cid_num ? c->cid.cid_num : "<unknown>", 
+			c->cid.cid_name ? c->cid.cid_name : "<unknown>", 
 			c->accountcode,
 			ast_state2str(c->_state), bridge, c->uniqueid, idText);
 		}
@@ -810,9 +818,15 @@ static void *fast_originate(void *data)
 	int res;
 	int reason = 0;
 	if (!ast_strlen_zero(in->app)) {
-		res = ast_pbx_outgoing_app(in->tech, AST_FORMAT_SLINEAR, in->data, in->timeout, in->app, in->appdata, &reason, 1, !ast_strlen_zero(in->callerid) ? in->callerid : NULL, in->variable, in->account);
+		res = ast_pbx_outgoing_app(in->tech, AST_FORMAT_SLINEAR, in->data, in->timeout, in->app, in->appdata, &reason, 1, 
+			!ast_strlen_zero(in->cid_num) ? in->cid_num : NULL, 
+			!ast_strlen_zero(in->cid_name) ? in->cid_name : NULL,
+			in->variable, in->account);
 	} else {
-		res = ast_pbx_outgoing_exten(in->tech, AST_FORMAT_SLINEAR, in->data, in->timeout, in->context, in->exten, in->priority, &reason, 1, !ast_strlen_zero(in->callerid) ? in->callerid : NULL, in->variable, in->account);
+		res = ast_pbx_outgoing_exten(in->tech, AST_FORMAT_SLINEAR, in->data, in->timeout, in->context, in->exten, in->priority, &reason, 1, 
+			!ast_strlen_zero(in->cid_num) ? in->cid_num : NULL, 
+			!ast_strlen_zero(in->cid_name) ? in->cid_name : NULL,
+			in->variable, in->account);
 	}   
 	if (!res)
 		manager_event(EVENT_FLAG_CALL,
@@ -859,18 +873,21 @@ static int action_originate(struct mansession *s, struct message *m)
 	char *priority = astman_get_header(m, "Priority");
 	char *timeout = astman_get_header(m, "Timeout");
 	char *callerid = astman_get_header(m, "CallerID");
-    	char *variable = astman_get_header(m, "Variable");
-    	char *account = astman_get_header(m, "Account");
+    char *variable = astman_get_header(m, "Variable");
+    char *account = astman_get_header(m, "Account");
 	char *app = astman_get_header(m, "Application");
 	char *appdata = astman_get_header(m, "Data");
 	char *async = astman_get_header(m, "Async");
 	char *id = astman_get_header(m, "ActionID");
 	char *tech, *data;
+	char *l=NULL, *n=NULL;
 	int pi = 0;
 	int res;
 	int to = 30000;
 	int reason = 0;
 	char tmp[256];
+	char tmp2[256]="";
+	
 	pthread_t th;
 	pthread_attr_t attr;
 	if (!name) {
@@ -894,6 +911,17 @@ static int action_originate(struct mansession *s, struct message *m)
 	}
 	*data = '\0';
 	data++;
+	strncpy(tmp2, callerid, sizeof(tmp2) - 1);
+	ast_callerid_parse(tmp2, &n, &l);
+	if (n) {
+		if (ast_strlen_zero(n))
+			n = NULL;
+	}
+	if (l) {
+		ast_shrink_phone_number(l);
+		if (ast_strlen_zero(l))
+			l = NULL;
+	}
 	if (ast_true(async)) {
 		struct fast_originate_helper *fast = malloc(sizeof(struct fast_originate_helper));
 		if (!fast) {
@@ -906,7 +934,10 @@ static int action_originate(struct mansession *s, struct message *m)
    			strncpy(fast->data, data, sizeof(fast->data) - 1);
 			strncpy(fast->app, app, sizeof(fast->app) - 1);
 			strncpy(fast->appdata, appdata, sizeof(fast->appdata) - 1);
-			strncpy(fast->callerid, callerid, sizeof(fast->callerid) - 1);
+			if (l)
+				strncpy(fast->cid_num, l, sizeof(fast->cid_num) - 1);
+			if (n)
+				strncpy(fast->cid_name, n, sizeof(fast->cid_name) - 1);
 			strncpy(fast->variable, variable, sizeof(fast->variable) - 1);
 			strncpy(fast->account, account, sizeof(fast->account) - 1);
 			strncpy(fast->context, context, sizeof(fast->context) - 1);
@@ -922,10 +953,10 @@ static int action_originate(struct mansession *s, struct message *m)
 			}
 		}
 	} else if (!ast_strlen_zero(app)) {
-        	res = ast_pbx_outgoing_app(tech, AST_FORMAT_SLINEAR, data, to, app, appdata, &reason, 0, !ast_strlen_zero(callerid) ? callerid : NULL, variable, account);
+        	res = ast_pbx_outgoing_app(tech, AST_FORMAT_SLINEAR, data, to, app, appdata, &reason, 0, l, n, variable, account);
     	} else {
 		if (exten && context && pi)
-	        	res = ast_pbx_outgoing_exten(tech, AST_FORMAT_SLINEAR, data, to, context, exten, pi, &reason, 0, !ast_strlen_zero(callerid) ? callerid : NULL, variable, account);
+	        	res = ast_pbx_outgoing_exten(tech, AST_FORMAT_SLINEAR, data, to, context, exten, pi, &reason, 0, l, n, variable, account);
 		else {
 			astman_send_error(s, m, "Originate with 'Exten' requires 'Context' and 'Priority'");
 			return 0;
diff --git a/pbx.c b/pbx.c
index 16888bdbfd..4a57371f69 100755
--- a/pbx.c
+++ b/pbx.c
@@ -3,7 +3,7 @@
  *
  * Core PBX routines.
  * 
- * Copyright (C) 1999, Mark Spencer
+ * Copyright (C) 1999-2004, Digium, Inc.
  *
  * Mark Spencer <markster@digium.com>
  *
@@ -696,9 +696,7 @@ struct ast_context *ast_context_find(char *name)
 
 static int matchcid(char *cidpattern, char *callerid)
 {
-	char tmp[AST_MAX_EXTENSION];
 	int failresult;
-	char *name, *num;
 	
 	/* If the Caller*ID pattern is empty, then we're matching NO Caller*ID, so
 	   failing to get a number should count as a match, otherwise not */
@@ -712,15 +710,7 @@ static int matchcid(char *cidpattern, char *callerid)
 	if (!callerid)
 		return failresult;
 
-	/* Copy original Caller*ID */
-	strncpy(tmp, callerid, sizeof(tmp)-1);
-	/* Parse Number */
-	if (ast_callerid_parse(tmp, &name, &num)) 
-		return failresult;
-	if (!num)
-		return failresult;
-	ast_shrink_phone_number(num);
-	return ast_extension_match(cidpattern, num);
+	return ast_extension_match(cidpattern, callerid);
 }
 
 static struct ast_exten *pbx_find_extension(struct ast_channel *chan, char *context, char *exten, int priority, char *callerid, int action, char *incstack[], int *stacklen, int *status, struct ast_switch **swo, char **data)
@@ -824,7 +814,6 @@ static void pbx_substitute_variables_temp(struct ast_channel *c, const char *var
 	struct tm brokentime;
 	int offset,offset2;
 	struct ast_var_t *variables;
-	char *name, *num; /* for callerid name + num variables */
 	struct varshead *headp=NULL;
 
 	if (c) 
@@ -876,31 +865,39 @@ static void pbx_substitute_variables_temp(struct ast_channel *c, const char *var
 			*ret+=strlen(*ret)+offset;
 		(*ret)[offset2] = '\0';
 	} else if (c && !strcmp(var, "CALLERIDNUM")) {
-		if (c->callerid)
-			strncpy(workspace, c->callerid, workspacelen - 1);
-		ast_callerid_parse(workspace, &name, &num);
-		if (num) {
-			ast_shrink_phone_number(num);
-			*ret = num;
+		if (c->cid.cid_num) {
+			strncpy(workspace, c->cid.cid_num, workspacelen - 1);
+			*ret = workspace;
 		} else
+			*ret = NULL;
+	} else if (c && !strcmp(var, "CALLERANI")) {
+		if (c->cid.cid_ani) {
+			strncpy(workspace, c->cid.cid_ani, workspacelen - 1);
 			*ret = workspace;
+		} else
+			*ret = NULL;
 	} else if (c && !strcmp(var, "CALLERIDNAME")) {
-		if (c->callerid)
-			strncpy(workspace, c->callerid, workspacelen - 1);
-		ast_callerid_parse(workspace, &name, &num);
-		if (name)
-			*ret = name;
-		else
+		if (c->cid.cid_name) {
+			strncpy(workspace, c->cid.cid_name, workspacelen - 1);
 			*ret = workspace;
+		} else
+			*ret = NULL;
 	} else if (c && !strcmp(var, "CALLERID")) {
-		if (c->callerid) {
-			strncpy(workspace, c->callerid, workspacelen - 1);
+		if (c->cid.cid_num) {
+			if (c->cid.cid_name) {
+				snprintf(workspace, workspacelen, "\"%s\" <%s>", c->cid.cid_name, c->cid.cid_num);
+			} else {
+				strncpy(workspace, c->cid.cid_num, workspacelen - 1);
+			}
+			*ret = workspace;
+		} else if (c->cid.cid_name) {
+			strncpy(workspace, c->cid.cid_name, workspacelen - 1);
 			*ret = workspace;
-		} else 
+		} else
 			*ret = NULL;
 	} else if (c && !strcmp(var, "DNID")) {
-		if (c->dnid) {
-			strncpy(workspace, c->dnid, workspacelen - 1);
+		if (c->cid.cid_dnid) {
+			strncpy(workspace, c->cid.cid_dnid, workspacelen - 1);
 			*ret = workspace;
 		} else
 			*ret = NULL;
@@ -923,8 +920,8 @@ static void pbx_substitute_variables_temp(struct ast_channel *c, const char *var
 		*ret = workspace;
 		ast_log(LOG_WARNING, "The use of 'EXTEN-foo' has been deprecated in favor of 'EXTEN:foo'\n");
 	} else if (c && !strcmp(var, "RDNIS")) {
-		if (c->rdnis) {
-			strncpy(workspace, c->rdnis, workspacelen - 1);
+		if (c->cid.cid_rdnis) {
+			strncpy(workspace, c->cid.cid_rdnis, workspacelen - 1);
 			*ret = workspace;
 		} else
 			*ret = NULL;
@@ -935,7 +932,16 @@ static void pbx_substitute_variables_temp(struct ast_channel *c, const char *var
 		snprintf(workspace, workspacelen, "%d", c->priority);
 		*ret = workspace;
 	} else if (c && !strcmp(var, "CALLINGPRES")) {
-		snprintf(workspace, workspacelen, "%d", c->callingpres);
+		snprintf(workspace, workspacelen, "%d", c->cid.cid_pres);
+		*ret = workspace;
+	} else if (c && !strcmp(var, "CALLINGANI2")) {
+		snprintf(workspace, workspacelen, "%d", c->cid.cid_ani2);
+		*ret = workspace;
+	} else if (c && !strcmp(var, "CALLINGTON")) {
+		snprintf(workspace, workspacelen, "%d", c->cid.cid_ton);
+		*ret = workspace;
+	} else if (c && !strcmp(var, "CALLINGTNS")) {
+		snprintf(workspace, workspacelen, "%d", c->cid.cid_tns);
 		*ret = workspace;
 	} else if (c && !strcmp(var, "CHANNEL")) {
 		strncpy(workspace, c->name, workspacelen - 1);
@@ -1793,12 +1799,12 @@ int ast_pbx_run(struct ast_channel *c)
 	c->pbx->dtimeout = 5;
 
 	/* Start by trying whatever the channel is set to */
-	if (!ast_exists_extension(c, c->context, c->exten, c->priority, c->callerid)) {
+	if (!ast_exists_extension(c, c->context, c->exten, c->priority, c->cid.cid_num)) {
 		/* JK02: If not successfull fall back to 's' */
 		if (option_verbose > 1)
 			ast_verbose( VERBOSE_PREFIX_2 "Starting %s at %s,%s,%d failed so falling back to exten 's'\n", c->name, c->context, c->exten, c->priority);
 		strncpy(c->exten, "s", sizeof(c->exten)-1);
-		if (!ast_exists_extension(c, c->context, c->exten, c->priority, c->callerid)) {
+		if (!ast_exists_extension(c, c->context, c->exten, c->priority, c->cid.cid_num)) {
 			/* JK02: And finally back to default if everything else failed */
 			if (option_verbose > 1)
 				ast_verbose( VERBOSE_PREFIX_2 "Starting %s at %s,%s,%d still failed so falling back to context 'default'\n", c->name, c->context, c->exten, c->priority);
@@ -1811,9 +1817,9 @@ int ast_pbx_run(struct ast_channel *c)
 	for(;;) {
 		pos = 0;
 		digit = 0;
-		while(ast_exists_extension(c, c->context, c->exten, c->priority, c->callerid)) {
+		while(ast_exists_extension(c, c->context, c->exten, c->priority, c->cid.cid_num)) {
 			memset(exten, 0, sizeof(exten));
-			if ((res = ast_spawn_extension(c, c->context, c->exten, c->priority, c->callerid))) {
+			if ((res = ast_spawn_extension(c, c->context, c->exten, c->priority, c->cid.cid_num))) {
 				/* Something bad happened, or a hangup has been requested. */
 				if (((res >= '0') && (res <= '9')) || ((res >= 'A') && (res <= 'F')) ||
 					(res == '*') || (res == '#')) {
@@ -1851,7 +1857,7 @@ int ast_pbx_run(struct ast_channel *c)
 					goto out;
 				}
 			}
-			if ((c->_softhangup == AST_SOFTHANGUP_TIMEOUT) && (ast_exists_extension(c,c->context,"T",1,c->callerid))) {
+			if ((c->_softhangup == AST_SOFTHANGUP_TIMEOUT) && (ast_exists_extension(c,c->context,"T",1,c->cid.cid_num))) {
 				strncpy(c->exten,"T",sizeof(c->exten) - 1);
 				/* If the AbsoluteTimeout is not reset to 0, we'll get an infinite loop */
 				c->whentohangup = 0;
@@ -1865,9 +1871,9 @@ int ast_pbx_run(struct ast_channel *c)
 			firstpass = 0;
 			c->priority++;
 		}
-		if (!ast_exists_extension(c, c->context, c->exten, 1, c->callerid)) {
+		if (!ast_exists_extension(c, c->context, c->exten, 1, c->cid.cid_num)) {
 			/* It's not a valid extension anymore */
-			if (ast_exists_extension(c, c->context, "i", 1, c->callerid)) {
+			if (ast_exists_extension(c, c->context, "i", 1, c->cid.cid_num)) {
 				if (option_verbose > 2)
 					ast_verbose(VERBOSE_PREFIX_3 "Sent into invalid extension '%s' in context '%s' on %s\n", c->exten, c->context, c->name);
 				pbx_builtin_setvar_helper(c, "INVALID_EXTEN", c->exten);
@@ -1887,7 +1893,7 @@ int ast_pbx_run(struct ast_channel *c)
 				waittime = c->pbx->dtimeout;
 			else
 				waittime = c->pbx->rtimeout;
-			while (ast_matchmore_extension(c, c->context, exten, 1, c->callerid)) {
+			while (ast_matchmore_extension(c, c->context, exten, 1, c->cid.cid_num)) {
 				/* As long as we're willing to wait, and as long as it's not defined, 
 				   keep reading digits until we can't possibly get a right answer anymore.  */
 				digit = ast_waitfordigit(c, waittime * 1000);
@@ -1904,7 +1910,7 @@ int ast_pbx_run(struct ast_channel *c)
 					waittime = c->pbx->dtimeout;
 				}
 			}
-			if (ast_exists_extension(c, c->context, exten, 1, c->callerid)) {
+			if (ast_exists_extension(c, c->context, exten, 1, c->cid.cid_num)) {
 				/* Prepare the next cycle */
 				strncpy(c->exten, exten, sizeof(c->exten)-1);
 				c->priority = 1;
@@ -1912,7 +1918,7 @@ int ast_pbx_run(struct ast_channel *c)
 				/* No such extension */
 				if (!ast_strlen_zero(exten)) {
 					/* An invalid extension */
-					if (ast_exists_extension(c, c->context, "i", 1, c->callerid)) {
+					if (ast_exists_extension(c, c->context, "i", 1, c->cid.cid_num)) {
 						if (option_verbose > 2)
 							ast_verbose( VERBOSE_PREFIX_3 "Invalid extension '%s' in context '%s' on %s\n", exten, c->context, c->name);
 						pbx_builtin_setvar_helper(c, "INVALID_EXTEN", exten);
@@ -1924,7 +1930,7 @@ int ast_pbx_run(struct ast_channel *c)
 					}
 				} else {
 					/* A simple timeout */
-					if (ast_exists_extension(c, c->context, "t", 1, c->callerid)) {
+					if (ast_exists_extension(c, c->context, "t", 1, c->cid.cid_num)) {
 						if (option_verbose > 2)
 							ast_verbose( VERBOSE_PREFIX_3 "Timeout on %s\n", c->name);
 						strncpy(c->exten, "t", sizeof(c->exten)-1);
@@ -1945,12 +1951,12 @@ int ast_pbx_run(struct ast_channel *c)
 	if (firstpass) 
 		ast_log(LOG_WARNING, "Don't know what to do with '%s'\n", c->name);
 out:
-	if ((res != AST_PBX_KEEPALIVE) && ast_exists_extension(c, c->context, "h", 1, c->callerid)) {
+	if ((res != AST_PBX_KEEPALIVE) && ast_exists_extension(c, c->context, "h", 1, c->cid.cid_num)) {
 		c->exten[0] = 'h';
 		c->exten[1] = '\0';
 		c->priority = 1;
-		while(ast_exists_extension(c, c->context, c->exten, c->priority, c->callerid)) {
-			if ((res = ast_spawn_extension(c, c->context, c->exten, c->priority, c->callerid))) {
+		while(ast_exists_extension(c, c->context, c->exten, c->priority, c->cid.cid_num)) {
+			if ((res = ast_spawn_extension(c, c->context, c->exten, c->priority, c->cid.cid_num))) {
 				/* Something bad happened, or a hangup has been requested. */
 				if (option_debug)
 					ast_log(LOG_DEBUG, "Spawn extension (%s,%s,%d) exited non-zero on '%s'\n", c->context, c->exten, c->priority, c->name);
@@ -4033,7 +4039,7 @@ static void *async_wait(void *data)
 	return NULL;
 }
 
-int ast_pbx_outgoing_exten(char *type, int format, void *data, int timeout, char *context, char *exten, int priority, int *reason, int sync, char *callerid, char *variable, char *account)
+int ast_pbx_outgoing_exten(char *type, int format, void *data, int timeout, char *context, char *exten, int priority, int *reason, int sync, char *cid_num, char *cid_name, char *variable, char *account)
 {
 	struct ast_channel *chan;
 	struct async_stat *as;
@@ -4044,7 +4050,7 @@ int ast_pbx_outgoing_exten(char *type, int format, void *data, int timeout, char
 		
 	if (sync) {
 		LOAD_OH(oh);
-		chan = __ast_request_and_dial(type, format, data, timeout, reason, callerid, &oh);
+		chan = __ast_request_and_dial(type, format, data, timeout, reason, cid_num, cid_name, &oh);
 		if (chan) {
 			pbx_builtin_setaccount(chan, account);
 			if (chan->_state == AST_STATE_UP) {
@@ -4099,7 +4105,7 @@ int ast_pbx_outgoing_exten(char *type, int format, void *data, int timeout, char
 		if (!as)
 			return -1;
 		memset(as, 0, sizeof(struct async_stat));
-		chan = ast_request_and_dial(type, format, data, timeout, reason, callerid);
+		chan = ast_request_and_dial(type, format, data, timeout, reason, cid_num, cid_name);
 		if (!chan) {
 			free(as);
 			return -1;
@@ -4151,7 +4157,7 @@ static void *ast_pbx_run_app(void *data)
 	return NULL;
 }
 
-int ast_pbx_outgoing_app(char *type, int format, void *data, int timeout, char *app, char *appdata, int *reason, int sync, char *callerid, char *variable, char *account)
+int ast_pbx_outgoing_app(char *type, int format, void *data, int timeout, char *app, char *appdata, int *reason, int sync, char *cid_num, char *cid_name, char *variable, char *account)
 {
 	struct ast_channel *chan;
 	struct async_stat *as;
@@ -4163,7 +4169,7 @@ int ast_pbx_outgoing_app(char *type, int format, void *data, int timeout, char *
 	if (!app || ast_strlen_zero(app))
 		return -1;
 	if (sync) {
-		chan = ast_request_and_dial(type, format, data, timeout, reason, callerid);
+		chan = ast_request_and_dial(type, format, data, timeout, reason, cid_num, cid_name);
 		if (chan) {
 			pbx_builtin_setaccount(chan, account);
 			if (variable) {
@@ -4209,7 +4215,7 @@ int ast_pbx_outgoing_app(char *type, int format, void *data, int timeout, char *
 		if (!as)
 			return -1;
 		memset(as, 0, sizeof(struct async_stat));
-		chan = ast_request_and_dial(type, format, data, timeout, reason, callerid);
+		chan = ast_request_and_dial(type, format, data, timeout, reason, cid_num, cid_name);
 		if (!chan) {
 			free(as);
 			return -1;
diff --git a/pbx/pbx_spool.c b/pbx/pbx_spool.c
index fd51926b0c..be839e7309 100755
--- a/pbx/pbx_spool.c
+++ b/pbx/pbx_spool.c
@@ -3,7 +3,7 @@
  *
  * Full-featured outgoing call spool support
  * 
- * Copyright (C) 2002, Digium
+ * Copyright (C) 2002-2004, Digium, Inc.
  *
  * Mark Spencer <markster@digium.com>
  *
@@ -15,6 +15,7 @@
 #include <asterisk/file.h>
 #include <asterisk/logger.h>
 #include <asterisk/channel.h>
+#include <asterisk/callerid.h>
 #include <asterisk/pbx.h>
 #include <asterisk/module.h>
 #include <asterisk/options.h>
@@ -67,7 +68,8 @@ struct outgoing {
 	int priority;
 
 	/* CallerID Information */
-	char callerid[256];
+	char cid_num[256];
+	char cid_name[256];
 
 	/* Channel variables */
 	char variable[10*256];
@@ -132,7 +134,7 @@ static int apply_outgoing(struct outgoing *o, char *fn, FILE *f)
 							o->tech[0] = '\0';
 						}
 					} else if (!strcasecmp(buf, "callerid")) {
-						strncpy(o->callerid, c, sizeof(o->callerid) - 1);
+						ast_callerid_split(c, o->cid_name, sizeof(o->cid_name), o->cid_num, sizeof(o->cid_num));
 					} else if (!strcasecmp(buf, "application")) {
 						strncpy(o->app, c, sizeof(o->app) - 1);
 					} else if (!strcasecmp(buf, "data")) {
@@ -222,11 +224,11 @@ static void *attempt_thread(void *data)
 	if (!ast_strlen_zero(o->app)) {
 		if (option_verbose > 2)
 			ast_verbose(VERBOSE_PREFIX_3 "Attempting call on %s/%s for application %s(%s) (Retry %d)\n", o->tech, o->dest, o->app, o->data, o->retries);
-		res = ast_pbx_outgoing_app(o->tech, AST_FORMAT_SLINEAR, o->dest, o->waittime * 1000, o->app, o->data, &reason, 2 /* wait to finish */, o->callerid, o->variable, o->account);
+		res = ast_pbx_outgoing_app(o->tech, AST_FORMAT_SLINEAR, o->dest, o->waittime * 1000, o->app, o->data, &reason, 2 /* wait to finish */, o->cid_num, o->cid_name, o->variable, o->account);
 	} else {
 		if (option_verbose > 2)
 			ast_verbose(VERBOSE_PREFIX_3 "Attempting call on %s/%s for %s@%s:%d (Retry %d)\n", o->tech, o->dest, o->exten, o->context,o->priority, o->retries);
-		res = ast_pbx_outgoing_exten(o->tech, AST_FORMAT_SLINEAR, o->dest, o->waittime * 1000, o->context, o->exten, o->priority, &reason, 2 /* wait to finish */, o->callerid, o->variable, o->account);
+		res = ast_pbx_outgoing_exten(o->tech, AST_FORMAT_SLINEAR, o->dest, o->waittime * 1000, o->context, o->exten, o->priority, &reason, 2 /* wait to finish */, o->cid_num, o->cid_name, o->variable, o->account);
 	}
 	if (res) {
 		ast_log(LOG_NOTICE, "Call failed to go through, reason %d\n", reason);
diff --git a/res/res_agi.c b/res/res_agi.c
index 7325d781b1..b6f6edebb8 100755
--- a/res/res_agi.c
+++ b/res/res_agi.c
@@ -23,6 +23,7 @@
 #include <asterisk/pbx.h>
 #include <asterisk/module.h>
 #include <asterisk/astdb.h>
+#include <asterisk/callerid.h>
 #include <math.h>
 #include <stdlib.h>
 #include <unistd.h>
@@ -296,9 +297,10 @@ static void setup_env(struct ast_channel *chan, char *request, int fd, int enhan
 	fdprintf(fd, "agi_uniqueid: %s\n", chan->uniqueid);
 
 	/* ANI/DNIS */
-	fdprintf(fd, "agi_callerid: %s\n", chan->callerid ? chan->callerid : "unknown");
-	fdprintf(fd, "agi_dnid: %s\n", chan->dnid ? chan->dnid : "unknown");
-	fdprintf(fd, "agi_rdnis: %s\n", chan->rdnis ? chan->rdnis : "unknown");
+	fdprintf(fd, "agi_callerid: %s\n", chan->cid.cid_num ? chan->cid.cid_num : "unknown");
+	fdprintf(fd, "agi_calleridname: %s\n", chan->cid.cid_name ? chan->cid.cid_name : "unknown");
+	fdprintf(fd, "agi_dnid: %s\n", chan->cid.cid_dnid ? chan->cid.cid_dnid : "unknown");
+	fdprintf(fd, "agi_rdnis: %s\n", chan->cid.cid_rdnis ? chan->cid.cid_rdnis : "unknown");
 
 	/* Context information */
 	fdprintf(fd, "agi_context: %s\n", chan->context);
@@ -837,8 +839,19 @@ static int handle_exec(struct ast_channel *chan, AGI *agi, int argc, char **argv
 
 static int handle_setcallerid(struct ast_channel *chan, AGI *agi, int argc, char **argv)
 {
-	if (argv[2])
-		ast_set_callerid(chan, argv[2], 0);
+	char tmp[256]="";
+	char *l = NULL, *n = NULL;
+	if (argv[2]) {
+		strncpy(tmp, argv[2], sizeof(tmp) - 1);
+		ast_callerid_parse(tmp, &n, &l);
+		if (l)
+			ast_shrink_phone_number(l);
+		else
+			l = "";
+		if (!n)
+			n = "";
+		ast_set_callerid(chan, l, n, NULL);
+	}
 
 	fdprintf(agi->fd, "200 result=1\n");
 	return RESULT_SUCCESS;
diff --git a/res/res_features.c b/res/res_features.c
index b2fd1a866d..560885a27a 100755
--- a/res/res_features.c
+++ b/res/res_features.c
@@ -210,9 +210,11 @@ int ast_park_call(struct ast_channel *chan, struct ast_channel *peer, int timeou
                                 "From: %s\r\n"
                                 "Timeout: %ld\r\n"
                                 "CallerID: %s\r\n"
+                                "CallerIDName: %s\r\n\r\n"
                                 ,pu->parkingnum, pu->chan->name, peer->name
                                 ,(long)pu->start.tv_sec + (long)(pu->parkingtime/1000) - (long)time(NULL)
-                                ,(pu->chan->callerid ? pu->chan->callerid : "")
+                                ,(pu->chan->cid.cid_num ? pu->chan->cid.cid_num : "")
+                                ,(pu->chan->cid.cid_name ? pu->chan->cid.cid_name : "")
                                 );
 
 			if (peer) {
@@ -450,7 +452,7 @@ int ast_bridge_call(struct ast_channel *chan,struct ast_channel *peer,struct ast
 						break;
 					*(ptr++) = res;
 					if (!ast_matchmore_extension(transferer, transferer_real_context
-								, newext, 1, transferer->callerid)) {
+								, newext, 1, transferer->cid.cid_num)) {
 						break;
 					}
 				}
@@ -479,7 +481,7 @@ int ast_bridge_call(struct ast_channel *chan,struct ast_channel *peer,struct ast
 						ast_log(LOG_WARNING, "Unable to park call %s\n", transferee->name);
 					}
 					/* XXX Maybe we should have another message here instead of invalid extension XXX */
-				} else if (ast_exists_extension(transferee, transferer_real_context, newext, 1, transferer->callerid)) {
+				} else if (ast_exists_extension(transferee, transferer_real_context, newext, 1, transferer->cid.cid_num)) {
 					ast_moh_stop(transferee);
 					res=ast_autoservice_stop(transferee);
 					if (!transferee->pbx) {
@@ -842,11 +844,13 @@ static int manager_parking_status( struct mansession *s, struct message *m )
 			"Channel: %s\r\n"
 			"Timeout: %ld\r\n"
 			"CallerID: %s\r\n"
+			"CallerIDName: %s\r\n"
 			"%s"
 			"\r\n"
                         ,cur->parkingnum, cur->chan->name
                         ,(long)cur->start.tv_sec + (long)(cur->parkingtime/1000) - (long)time(NULL)
-			,(cur->chan->callerid ? cur->chan->callerid : "")
+			,(cur->chan->cid.cid_num ? cur->chan->cid.cid_num : "")
+			,(cur->chan->cid.cid_name ? cur->chan->cid.cid_name : "")
 			,idText);
 			ast_mutex_unlock(&s->lock);
 
-- 
GitLab