[Asterisk-code-review] res pjsip session: add new flag use callerid contact (asterisk[16])

George Joseph asteriskteam at digium.com
Mon Oct 29 13:21:39 CDT 2018


George Joseph has submitted this change and it was merged. ( https://gerrit.asterisk.org/10529 )

Change subject: res_pjsip_session: add new flag use_callerid_contact
......................................................................

res_pjsip_session: add new flag use_callerid_contact

Add a new global flag to res_pjsip to allow the callerid to be used
as the username in the contact header.  This allows chan_pjsip to have
the same behavour as chan_sip

ASTERISK-28087 #close

Change-Id: I9a720e058323f6862a91c62f8a8c1a4b5c087b95
---
A contrib/ast-db-manage/config/versions/2bb1a85135ad_pjsip_add_use_callerid_contact.py
M include/asterisk/res_pjsip.h
M res/res_pjsip/config_global.c
M res/res_pjsip_session.c
4 files changed, 74 insertions(+), 0 deletions(-)

Approvals:
  Joshua Colp: Looks good to me, but someone else must approve
  George Joseph: Looks good to me, approved; Approved for Submit



diff --git a/contrib/ast-db-manage/config/versions/2bb1a85135ad_pjsip_add_use_callerid_contact.py b/contrib/ast-db-manage/config/versions/2bb1a85135ad_pjsip_add_use_callerid_contact.py
new file mode 100644
index 0000000..88c94f5
--- /dev/null
+++ b/contrib/ast-db-manage/config/versions/2bb1a85135ad_pjsip_add_use_callerid_contact.py
@@ -0,0 +1,34 @@
+"""pjsip add use_callerid_contact
+
+Revision ID: 2bb1a85135ad
+Revises: 7f85dd44c775
+Create Date: 2018-10-18 15:13:40.462354
+
+"""
+
+# revision identifiers, used by Alembic.
+revision = '2bb1a85135ad'
+down_revision = '7f85dd44c775'
+
+from alembic import op
+import sqlalchemy as sa
+from sqlalchemy.dialects.postgresql import ENUM
+
+YESNO_NAME = 'yesno_values'
+YESNO_VALUES = ['yes', 'no']
+
+
+def upgrade():
+    ############################# Enums ##############################
+
+    # yesno_values have already been created, so use postgres enum object
+    # type to get around "already created" issue - works okay with mysql
+    yesno_values = ENUM(*YESNO_VALUES, name=YESNO_NAME, create_type=False)
+
+    op.add_column('ps_globals', sa.Column('use_callerid_contact', yesno_values))
+
+
+def downgrade():
+    if op.get_context().bind.dialect.name == 'mssql':
+        op.drop_constraint('ck_ps_globals_use_callerid_contact_yesno_values','ps_globals')
+    op.drop_column('ps_globals', 'use_callerid_contact')
diff --git a/include/asterisk/res_pjsip.h b/include/asterisk/res_pjsip.h
index b323bc1..d9e6f2e 100644
--- a/include/asterisk/res_pjsip.h
+++ b/include/asterisk/res_pjsip.h
@@ -2748,6 +2748,14 @@
 unsigned int ast_sip_get_mwi_disable_initial_unsolicited(void);
 
 /*!
+ * \brief Retrieve the global setting 'use_callerid_contact'.
+ * \since 13.24.0
+ *
+ * \retval non zero if CALLERID(num) is to be used as the default username in the contact
+ */
+unsigned int ast_sip_get_use_callerid_contact(void);
+
+/*!
  * \brief Retrieve the global setting 'ignore_uri_user_options'.
  * \since 13.12.0
  *
diff --git a/res/res_pjsip/config_global.c b/res/res_pjsip/config_global.c
index b3d6fa2..a02cacd 100644
--- a/res/res_pjsip/config_global.c
+++ b/res/res_pjsip/config_global.c
@@ -48,6 +48,7 @@
 #define DEFAULT_MWI_TPS_QUEUE_LOW -1
 #define DEFAULT_MWI_DISABLE_INITIAL_UNSOLICITED 0
 #define DEFAULT_IGNORE_URI_USER_OPTIONS 0
+#define DEFAULT_USE_CALLERID_CONTACT 0
 
 /*!
  * \brief Cached global config object
@@ -103,6 +104,8 @@
 	} mwi;
 	/*! Nonzero if URI user field options are ignored. */
 	unsigned int ignore_uri_user_options;
+	/*! Nonzero if CALLERID(num) is to be used as the default contact username instead of default_from_user */
+	unsigned int use_callerid_contact;
 };
 
 static void global_destructor(void *obj)
@@ -402,6 +405,21 @@
 	return ignore_uri_user_options;
 }
 
+unsigned int ast_sip_get_use_callerid_contact(void)
+{
+	unsigned int use_callerid_contact;
+	struct global_config *cfg;
+
+	cfg = get_global_cfg();
+	if (!cfg) {
+		return DEFAULT_USE_CALLERID_CONTACT;
+	}
+
+	use_callerid_contact = cfg->use_callerid_contact;
+	ao2_ref(cfg, -1);
+	return use_callerid_contact;
+}
+
 /*!
  * \internal
  * \brief Observer to set default global object if none exist.
@@ -553,6 +571,9 @@
 	ast_sorcery_object_field_register(sorcery, "global", "ignore_uri_user_options",
 		DEFAULT_IGNORE_URI_USER_OPTIONS ? "yes" : "no",
 		OPT_BOOL_T, 1, FLDSET(struct global_config, ignore_uri_user_options));
+	ast_sorcery_object_field_register(sorcery, "global", "use_callerid_contact",
+		DEFAULT_USE_CALLERID_CONTACT ? "yes" : "no",
+		OPT_YESNO_T, 1, FLDSET(struct global_config, use_callerid_contact));
 
 	if (ast_sorcery_instance_observer_add(sorcery, &observer_callbacks_global)) {
 		return -1;
diff --git a/res/res_pjsip_session.c b/res/res_pjsip_session.c
index e681dcb..13c123a 100644
--- a/res/res_pjsip_session.c
+++ b/res/res_pjsip_session.c
@@ -1404,8 +1404,10 @@
 	struct ast_party_id connected_id;
 	pj_pool_t *dlg_pool;
 	pjsip_fromto_hdr *dlg_info;
+	pjsip_contact_hdr *dlg_contact;
 	pjsip_name_addr *dlg_info_name_addr;
 	pjsip_sip_uri *dlg_info_uri;
+	pjsip_sip_uri *dlg_contact_uri;
 	int restricted;
 
 	if (!session->channel || session->saved_from_hdr) {
@@ -1426,11 +1428,16 @@
 
 	dlg_pool = session->inv_session->dlg->pool;
 	dlg_info = session->inv_session->dlg->local.info;
+	dlg_contact = session->inv_session->dlg->local.contact;
 	dlg_info_name_addr = (pjsip_name_addr *) dlg_info->uri;
 	dlg_info_uri = pjsip_uri_get_uri(dlg_info_name_addr);
+	dlg_contact_uri = (pjsip_sip_uri*)pjsip_uri_get_uri(dlg_contact->uri);
 
 	if (session->endpoint->id.trust_outbound || !restricted) {
 		ast_sip_modify_id_header(dlg_pool, dlg_info, &connected_id);
+		if (ast_sip_get_use_callerid_contact() && ast_strlen_zero(session->endpoint->contact_user)) {
+			pj_strdup2(dlg_pool, &dlg_contact_uri->user, S_COR(connected_id.number.valid, connected_id.number.str, ""));
+		}
 	}
 
 	ast_party_id_free(&connected_id);
@@ -1460,6 +1467,10 @@
 			pj_strdup2(dlg_pool, &dlg_info_uri->user, "anonymous");
 		}
 
+		if (ast_sip_get_use_callerid_contact() && ast_strlen_zero(session->endpoint->contact_user)) {
+			pj_strdup2(dlg_pool, &dlg_contact_uri->user, "anonymous");
+		}
+
 		if (ast_strlen_zero(session->endpoint->fromdomain)) {
 			pj_strdup2(dlg_pool, &dlg_info_uri->host, "anonymous.invalid");
 		}

-- 
To view, visit https://gerrit.asterisk.org/10529
To unsubscribe, or for help writing mail filters, visit https://gerrit.asterisk.org/settings

Gerrit-Project: asterisk
Gerrit-Branch: 16
Gerrit-MessageType: merged
Gerrit-Change-Id: I9a720e058323f6862a91c62f8a8c1a4b5c087b95
Gerrit-Change-Number: 10529
Gerrit-PatchSet: 2
Gerrit-Owner: Torrey Searle <tsearle at gmail.com>
Gerrit-Reviewer: George Joseph <gjoseph at digium.com>
Gerrit-Reviewer: Jenkins2 (1000185)
Gerrit-Reviewer: Joshua Colp <jcolp at digium.com>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.digium.com/pipermail/asterisk-code-review/attachments/20181029/684d5c9f/attachment.html>


More information about the asterisk-code-review mailing list