Commit 8e61e415 authored by jehan's avatar jehan

Add temporary function a allow to send message asynchronously from im encryption engine

parent fb3a215b
......@@ -370,6 +370,35 @@ static void store_or_update_chat_message(LinphoneChatMessage *msg) {
}
}
static void linphone_chat_message_send_to_sal_from_op(SalOp *op, LinphoneChatMessage *msg) {
LinphoneChatRoom *cr = msg->chat_room;
char *peer_uri = linphone_address_as_string_uri_only(linphone_chat_room_get_peer_address(cr));
char *identity = linphone_address_as_string_uri_only(linphone_chat_message_get_from_address(msg));
const char *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);
ms_free(identity);
if (msg->message && msg->clear_text_message && strcmp(msg->message, msg->clear_text_message) != 0) {
// We replace the encrypted message by the original one so it can be correctly stored and displayed by the application
ms_free(msg->message);
msg->message = ms_strdup(msg->clear_text_message);
}
if (msg->content_type && msg->clear_text_content_type && (strcmp(msg->content_type, msg->clear_text_content_type) != 0)) {
/* We replace the encrypted content type by the original one */
ms_free(msg->content_type);
msg->content_type = ms_strdup(msg->clear_text_content_type);
}
msg->message_id = ms_strdup(sal_op_get_call_id(op)); /* must be known at that time */
store_or_update_chat_message(msg);
}
void linphone_chat_message_send_to_sal(LinphoneChatMessage *msg) {
linphone_chat_message_send_to_sal_from_op(msg->op,msg);
}
void _linphone_chat_room_send_message(LinphoneChatRoom *cr, LinphoneChatMessage *msg) {
int retval = -1;
LinphoneCore *lc = cr->lc;
......@@ -400,16 +429,13 @@ void _linphone_chat_room_send_message(LinphoneChatRoom *cr, LinphoneChatMessage
} else {
SalOp *op = msg->op;
LinphoneCall *call=NULL;
char *content_type;
const char *identity = NULL;
char *clear_text_message = NULL;
char *clear_text_content_type = NULL;
if (msg->message) {
clear_text_message = ms_strdup(msg->message);
msg->clear_text_message = ms_strdup(msg->message);
}
if (msg->content_type) {
clear_text_content_type = ms_strdup(msg->content_type);
msg->clear_text_content_type = ms_strdup(msg->content_type);
}
/* Add to transient list */
......@@ -449,7 +475,7 @@ void _linphone_chat_room_send_message(LinphoneChatRoom *cr, LinphoneChatMessage
LinphoneImEncryptionEngineCbsOutgoingMessageCb cb_process_outgoing_message = linphone_im_encryption_engine_cbs_get_process_outgoing_message(imee_cbs);
if (cb_process_outgoing_message) {
retval = cb_process_outgoing_message(imee, cr, msg);
if(retval == 0) {
if(retval == 0 || retval == 1) {
msg->is_secured = TRUE;
}
}
......@@ -463,7 +489,7 @@ void _linphone_chat_room_send_message(LinphoneChatRoom *cr, LinphoneChatMessage
sal_op_set_user_pointer(op, msg); /*if out of call, directly store msg*/
}
if (retval > 0) {
if (retval > 1) {
sal_error_info_set((SalErrorInfo *)sal_op_get_error_info(op), SalReasonNotAcceptable, "SIP", retval, "Unable to encrypt IM", NULL);
store_or_update_chat_message(msg);
linphone_chat_message_update_state(msg, LinphoneChatMessageStateNotDelivered);
......@@ -472,32 +498,18 @@ void _linphone_chat_room_send_message(LinphoneChatRoom *cr, LinphoneChatMessage
}
if (msg->external_body_url) {
content_type = ms_strdup_printf("message/external-body; access-type=URL; URL=\"%s\"", msg->external_body_url);
sal_message_send(op, identity, cr->peer, content_type, NULL, NULL);
ms_free(content_type);
msg->content_type = ms_strdup_printf("message/external-body; access-type=URL; URL=\"%s\"", msg->external_body_url);
linphone_chat_message_send_to_sal(msg);
} else {
char *peer_uri = linphone_address_as_string_uri_only(linphone_chat_room_get_peer_address(cr));
const char *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);
if (retval != 1) {
if (msg->op)
linphone_chat_message_send_to_sal(msg);
else
linphone_chat_message_send_to_sal_from_op(op,msg);/*special case for in dialog chat message*/
}
ms_free(peer_uri);
}
if (msg->message && clear_text_message && strcmp(msg->message, clear_text_message) != 0) {
// We replace the encrypted message by the original one so it can be correctly stored and displayed by the application
ms_free(msg->message);
msg->message = ms_strdup(clear_text_message);
}
if (msg->content_type && clear_text_content_type && (strcmp(msg->content_type, clear_text_content_type) != 0)) {
/* We replace the encrypted content type by the original one */
ms_free(msg->content_type);
msg->content_type = ms_strdup(clear_text_content_type);
}
msg->message_id = ms_strdup(sal_op_get_call_id(op)); /* must be known at that time */
store_or_update_chat_message(msg);
if (cr->is_composing == LinphoneIsComposingActive) {
cr->is_composing = LinphoneIsComposingIdle;
......@@ -505,12 +517,7 @@ void _linphone_chat_room_send_message(LinphoneChatRoom *cr, LinphoneChatMessage
linphone_chat_room_delete_composing_idle_timer(cr);
linphone_chat_room_delete_composing_refresh_timer(cr);
if (clear_text_message) {
ms_free(clear_text_message);
}
if (clear_text_content_type) {
ms_free(clear_text_content_type);
}
if (call && call->op == op) {
/*In this case, chat delivery status is not notified, so unrefing chat message right now*/
......@@ -1706,6 +1713,12 @@ static void _linphone_chat_message_destroy(LinphoneChatMessage *msg) {
if (msg->callbacks) {
linphone_chat_message_cbs_unref(msg->callbacks);
}
if (msg->clear_text_message) {
ms_free(msg->clear_text_message);
}
if (msg->clear_text_content_type) {
ms_free(msg->clear_text_content_type);
}
}
LinphoneChatMessage *linphone_chat_message_ref(LinphoneChatMessage *msg) {
......
......@@ -242,6 +242,7 @@ struct _LinphoneChatMessage {
LinphoneErrorInfo *ei;
LinphoneChatMessageDir dir;
char* message;
char* clear_text_message;
void* message_state_changed_user_data;
void* message_userdata;
char* appdata;
......@@ -257,6 +258,7 @@ struct _LinphoneChatMessage {
SalOp *op;
LinphoneContent *file_transfer_information; /**< used to store file transfer information when the message is of file transfer type */
char *content_type; /**< is used to specified the type of message to be sent, used only for file transfer message */
char *clear_text_content_type;
bool_t to_be_stored;
belle_http_request_t *http_request; /**< keep a reference to the http_request in case of file transfer in order to be able to cancel the transfer */
belle_http_request_listener_t *http_listener; /* our listener, only owned by us*/
......@@ -663,7 +665,8 @@ void linphone_chat_message_send_display_notification(LinphoneChatMessage *cm);
void _linphone_chat_message_cancel_file_transfer(LinphoneChatMessage *msg, bool_t unref);
int linphone_chat_room_upload_file(LinphoneChatMessage *msg);
void _linphone_chat_room_send_message(LinphoneChatRoom *cr, LinphoneChatMessage *msg);
LinphoneChatMessageCbs *linphone_chat_message_cbs_new(void);
void linphone_chat_message_send_to_sal(LinphoneChatMessage *msg);
LINPHONE_PUBLIC LinphoneChatMessageCbs *linphone_chat_message_cbs_new(void);
LinphoneChatRoom *_linphone_core_create_chat_room_from_call(LinphoneCall *call);
void linphone_chat_room_add_transient_message(LinphoneChatRoom *cr, LinphoneChatMessage *msg);
void linphone_chat_room_remove_transient_message(LinphoneChatRoom *cr, LinphoneChatMessage *msg);
......
......@@ -2426,6 +2426,58 @@ void im_encryption_engine_b64(void) {
linphone_core_manager_destroy(marie);
linphone_core_manager_destroy(pauline);
}
/*temporaly private function*/
extern void linphone_chat_message_send_to_sal(LinphoneChatMessage* msg);
static bool_t im_encryption_engine_process_outgoing_message_async_impl(LinphoneChatMessage** msg) {
if (*msg) {
im_encryption_engine_process_outgoing_message_cb(NULL,NULL,*msg);
linphone_chat_message_send_to_sal(*msg);
linphone_chat_message_unref(*msg);
*msg=NULL;
}
return TRUE;
}
static LinphoneChatMessage* pending_message=NULL; /*limited to one message at a time */
static int im_encryption_engine_process_outgoing_message_async(LinphoneImEncryptionEngine *engine, LinphoneChatRoom *room, LinphoneChatMessage *msg) {
pending_message=msg;
linphone_chat_message_ref(pending_message);
linphone_core_add_iterate_hook(linphone_chat_room_get_core(room), (LinphoneCoreIterateHook)im_encryption_engine_process_outgoing_message_async_impl,&pending_message);
return 1;/*temporaly code to defer message sending*/
}
void im_encryption_engine_b64_async(void) {
LinphoneChatMessage *chat_msg = NULL;
LinphoneChatRoom* chat_room = NULL;
LinphoneCoreManager* marie = linphone_core_manager_new("marie_rc");
LinphoneImEncryptionEngine *marie_imee = linphone_im_encryption_engine_new();
LinphoneImEncryptionEngineCbs *marie_cbs = linphone_im_encryption_engine_get_callbacks(marie_imee);
LinphoneCoreManager* pauline = linphone_core_manager_new( "pauline_tcp_rc");
LinphoneImEncryptionEngine *pauline_imee = linphone_im_encryption_engine_new();
LinphoneImEncryptionEngineCbs *pauline_cbs = linphone_im_encryption_engine_get_callbacks(pauline_imee);
linphone_im_encryption_engine_cbs_set_process_incoming_message(marie_cbs, im_encryption_engine_process_incoming_message_cb);
linphone_im_encryption_engine_cbs_set_process_outgoing_message(marie_cbs, im_encryption_engine_process_outgoing_message_cb);
linphone_im_encryption_engine_cbs_set_process_incoming_message(pauline_cbs, im_encryption_engine_process_incoming_message_cb);
linphone_im_encryption_engine_cbs_set_process_outgoing_message(pauline_cbs, im_encryption_engine_process_outgoing_message_async);
linphone_core_set_im_encryption_engine(marie->lc, marie_imee);
linphone_core_set_im_encryption_engine(pauline->lc, pauline_imee);
chat_room = linphone_core_get_chat_room(pauline->lc, marie->identity);
chat_msg = linphone_chat_room_create_message(chat_room, "Bla bla bla bla");
linphone_chat_room_send_chat_message(chat_room, chat_msg);
BC_ASSERT_TRUE(wait_for(pauline->lc,marie->lc,&marie->stat.number_of_LinphoneMessageReceived,1));
BC_ASSERT_PTR_NOT_NULL(marie->stat.last_received_chat_message);
if (marie->stat.last_received_chat_message) {
BC_ASSERT_STRING_EQUAL(linphone_chat_message_get_text(marie->stat.last_received_chat_message), "Bla bla bla bla");
}
BC_ASSERT_PTR_NOT_NULL(linphone_core_get_chat_room(marie->lc,pauline->identity));
linphone_im_encryption_engine_unref(marie_imee);
linphone_im_encryption_engine_unref(pauline_imee);
linphone_core_manager_destroy(marie);
linphone_core_manager_destroy(pauline);
}
test_t message_tests[] = {
TEST_NO_TAG("Text message", text_message),
......@@ -2503,7 +2555,8 @@ test_t message_tests[] = {
#ifdef SQLITE_STORAGE_ENABLED
TEST_ONE_TAG("Text message with custom content-type and lime", text_message_with_custom_content_type_and_lime, "LIME"),
#endif
TEST_NO_TAG("IM Encryption Engine b64", im_encryption_engine_b64)
TEST_NO_TAG("IM Encryption Engine b64", im_encryption_engine_b64),
TEST_NO_TAG("IM Encryption Engine b64 async", im_encryption_engine_b64_async)
};
static int message_tester_before_suite(void) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment