From 68532f69bc8725131f1852bfbb97410068c27ad7 Mon Sep 17 00:00:00 2001
From: Jehan Monnier <jehan.monnier@linphone.org>
Date: Mon, 1 Jun 2015 12:49:53 +0200
Subject: [PATCH] make lime optional for file sharing

---
 coreapi/chat.c          | 28 +++++++++++++++++-----------
 coreapi/linphonecore.c  |  4 ++++
 coreapi/private.h       |  2 ++
 tester/message_tester.c | 14 +++++++++++++-
 4 files changed, 36 insertions(+), 12 deletions(-)

diff --git a/coreapi/chat.c b/coreapi/chat.c
index c750c4f698..da3746c294 100644
--- a/coreapi/chat.c
+++ b/coreapi/chat.c
@@ -323,7 +323,7 @@ static void linphone_chat_message_process_response_from_post_file(void *data, co
 			belle_sip_body_handler_t *first_part_bh;
 
 			/* shall we encrypt the file */
-			if (linphone_core_lime_enabled(msg->chat_room->lc)) {
+			if (linphone_core_lime_for_file_sharing_enabled(msg->chat_room->lc)) {
 				char keyBuffer[FILE_TRANSFER_KEY_SIZE]; /* temporary storage of generated key: 192 bits of key + 64 bits of initial vector */
 				/* generate a random 192 bits key + 64 bits of initial vector and store it into the file_transfer_information->key field of the message */
 				sal_get_random_bytes((unsigned char *)keyBuffer, FILE_TRANSFER_KEY_SIZE);
@@ -739,20 +739,26 @@ static void _linphone_chat_room_send_message(LinphoneChatRoom *cr, LinphoneChatM
 		sal_message_send(op,identity,cr->peer,content_type, NULL, NULL);
 		ms_free(content_type);
 	} else {
-		if (linphone_core_lime_enabled(cr->lc)) { /* shall we try to encrypt messages? */
-			linphone_chat_message_ref(msg); /* ref the message or it may be destroyed by callback if the encryption failed */
-			if ((msg->content_type != NULL) && (strcmp(msg->content_type, "application/vnd.gsma.rcs-ft-http+xml") == 0 )) { /* it's a file transfer, content type shall be set to application/cipher.vnd.gsma.rcs-ft-http+xml*/
-				sal_message_send(op, identity, cr->peer, "application/cipher.vnd.gsma.rcs-ft-http+xml", msg->message, linphone_address_as_string_uri_only(linphone_chat_room_get_peer_address(cr)));
+		char *peer_uri = linphone_address_as_string_uri_only(linphone_chat_room_get_peer_address(cr));
+		const char * content_type;
+
+		if (linphone_core_lime_enabled(cr->lc)) {
+			linphone_chat_message_ref(msg);  /* ref the message or it may be destroyed by callback if the encryption failed */
+			if (msg->content_type && strcmp(msg->content_type, "application/vnd.gsma.rcs-ft-http+xml") == 0) {
+				content_type = "application/cipher.vnd.gsma.rcs-ft-http+xml"; /* it's a file transfer, content type shall be set to application/cipher.vnd.gsma.rcs-ft-http+xml*/
 			} else {
-				sal_message_send(op, identity, cr->peer, "xml/cipher", msg->message, linphone_address_as_string_uri_only(linphone_chat_room_get_peer_address(cr)));
+				content_type = "xml/cipher";
 			}
 		} else {
-			if (msg->content_type == NULL) {
-				sal_text_send(op, identity, cr->peer,msg->message);
-			} else { /* rcs file transfer */
-				sal_message_send(op, identity, cr->peer, msg->content_type, msg->message, NULL);
-			}
+			content_type = msg->content_type;
+		}
+
+		if (content_type == NULL) {
+			sal_text_send(op, identity, cr->peer,msg->message);
+		} else {
+			sal_message_send(op, identity, cr->peer, content_type, msg->message, peer_uri);
 		}
+		ms_free(peer_uri);
 	}
 
 	msg->dir=LinphoneChatMessageOutgoing;
diff --git a/coreapi/linphonecore.c b/coreapi/linphonecore.c
index 580bd57da7..dd49a4c9e8 100644
--- a/coreapi/linphonecore.c
+++ b/coreapi/linphonecore.c
@@ -1871,6 +1871,10 @@ bool_t linphone_core_lime_enabled(const LinphoneCore *lc){
 	return (lp_config_get_int(lc->config,"sip", "lime", FALSE) && lime_is_available());
 }
 
+bool_t linphone_core_lime_for_file_sharing_enabled(const LinphoneCore *lc){
+	return linphone_core_lime_enabled(lc) && (lp_config_get_int(lc->config,"sip", "lime_for_file_sharing", TRUE) && lime_is_available());
+}
+
 /**
  * Same as linphone_core_get_primary_contact() but the result is a LinphoneAddress object
  * instead of const char*
diff --git a/coreapi/private.h b/coreapi/private.h
index 8faa8ad35f..40fd9b2e49 100644
--- a/coreapi/private.h
+++ b/coreapi/private.h
@@ -1184,6 +1184,8 @@ void _linphone_core_add_listener(LinphoneCore *lc, LinphoneCoreVTable *vtable, b
 MSWebCam *linphone_call_get_video_device(const LinphoneCall *call);
 MSWebCam *get_nowebcam_device();
 #endif
+bool_t linphone_core_lime_for_file_sharing_enabled(const LinphoneCore *lc);
+
 #ifdef __cplusplus
 }
 #endif
diff --git a/tester/message_tester.c b/tester/message_tester.c
index acb99c278d..63a8e794ba 100644
--- a/tester/message_tester.c
+++ b/tester/message_tester.c
@@ -584,7 +584,7 @@ static FILE* fopen_from_write_dir(const char * name, const char * mode) {
 	return file;
 }
 
-static void lime_file_transfer_message(void) {
+static void lime_file_transfer_message_base(bool_t encrypt_file) {
 	int i;
 	char *to;
 	FILE *ZIDCacheMarieFD, *ZIDCachePaulineFD;
@@ -612,6 +612,10 @@ static void lime_file_transfer_message(void) {
 	/* make sure lime is enabled */
 	linphone_core_enable_lime(marie->lc, 1);
 	linphone_core_enable_lime(pauline->lc, 1);
+	if (!encrypt_file) {
+		LpConfig *pauline_lp = linphone_core_get_config(pauline->lc);
+		lp_config_set_int(pauline_lp,"sip","lime_for_file_sharing",0);
+	}
 
 	/* set the zid caches files : create two ZID cache from this valid one inserting the auto-generated sip URI for the peer account as keys in ZID cache are indexed by peer sip uri */
 	ZIDCacheMarieFD = fopen_from_write_dir("tmpZIDCacheMarie.xml", "wb");
@@ -677,6 +681,13 @@ static void lime_file_transfer_message(void) {
 	linphone_core_manager_destroy(pauline);
 
 }
+static  void lime_file_transfer_message() {
+	lime_file_transfer_message_base(TRUE);
+}
+
+static  void lime_file_transfer_message_without_encryption() {
+	lime_file_transfer_message_base(FALSE);
+}
 
 static void printHex(char *title, uint8_t *data, uint32_t length) {
 	int i;
@@ -1600,6 +1611,7 @@ test_t message_tests[] = {
 #ifdef HAVE_LIME
 	,{ "Lime Text Message", lime_text_message }
 	,{ "Lime File transfer message", lime_file_transfer_message }
+	,{ "Lime File transfer message encryption only", lime_file_transfer_message_without_encryption}
 	,{ "Lime Unitary", lime_unit }
 #endif /* HAVE_LIME */
 #ifdef MSG_STORAGE_ENABLED
-- 
GitLab