diff --git a/coreapi/chat.c b/coreapi/chat.c index 2e241d44b..5a471adb7 100644 --- a/coreapi/chat.c +++ b/coreapi/chat.c @@ -668,8 +668,9 @@ LinphoneReason linphone_core_message_received(LinphoneCore *lc, SalOp *op, const cr = linphone_core_get_chat_room(lc, addr); /* Check if this is a duplicate message */ - if (linphone_chat_room_find_message(cr, sal_op_get_call_id(op)) != NULL) { + if ((msg = linphone_chat_room_find_message_with_dir(cr, sal_op_get_call_id(op), LinphoneChatMessageIncoming))) { reason = lc->chat_deny_code; + linphone_chat_message_unref(msg); goto end; } @@ -882,7 +883,7 @@ static void process_imdn(LinphoneChatRoom *cr, xmlparsing_context_t *xml_ctx) { } if ((message_id_str != NULL) && (datetime_str != NULL)) { - LinphoneChatMessage *cm = linphone_chat_room_find_message(cr, message_id_str); + LinphoneChatMessage *cm = linphone_chat_room_find_message_with_dir(cr, message_id_str, LinphoneChatMessageOutgoing); if (cm == NULL) { ms_warning("Received IMDN for unknown message %s", message_id_str); } else { diff --git a/coreapi/message_storage.c b/coreapi/message_storage.c index d07bcf525..2e9ae541f 100644 --- a/coreapi/message_storage.c +++ b/coreapi/message_storage.c @@ -627,25 +627,52 @@ bctbx_list_t *linphone_chat_room_get_history(LinphoneChatRoom *cr,int nb_message return linphone_chat_room_get_history_range(cr, 0, nb_message-1); } -LinphoneChatMessage * linphone_chat_room_find_message(LinphoneChatRoom *cr, const char *message_id) { + +bctbx_list_t* linphone_chat_room_find_messages(LinphoneChatRoom *cr, const char *message_id) { LinphoneCore *lc = linphone_chat_room_get_core(cr); - LinphoneChatMessage *cm = NULL; char *buf; char *peer; - + bctbx_list_t* messages; + if (lc->db == NULL) return NULL; peer = linphone_address_as_string_uri_only(linphone_chat_room_get_peer_address(cr)); cr->messages_hist = NULL; buf = sqlite3_mprintf("SELECT * FROM history WHERE remoteContact = %Q AND messageId = %Q", peer, message_id); linphone_sql_request_message(lc->db, buf, cr); sqlite3_free(buf); - - if (cr->messages_hist) { - cm = (LinphoneChatMessage *)bctbx_list_nth_data(cr->messages_hist, 0); - } - - cr->messages_hist = NULL; ms_free(peer); + messages = cr->messages_hist; + cr->messages_hist = NULL; + return messages; +} + +LinphoneChatMessage * linphone_chat_room_find_message_with_dir(LinphoneChatRoom *cr, const char *message_id, LinphoneChatMessageDir dir) { + bctbx_list_t* messages = linphone_chat_room_find_messages(cr, message_id); + bctbx_list_t* it; + LinphoneChatMessage *ret = NULL; + for (it = messages; it != NULL; it = it->next) { + LinphoneChatMessage * cm = (LinphoneChatMessage*)it->data; + if (cm->dir == dir) { + linphone_chat_message_ref(cm); + ret = cm; + break; + } + } + if (messages) + bctbx_list_free_with_data(messages, (bctbx_list_free_func)linphone_chat_message_unref); + + return ret; + +} + +LinphoneChatMessage * linphone_chat_room_find_message(LinphoneChatRoom *cr, const char *message_id) { + bctbx_list_t* messages = linphone_chat_room_find_messages(cr, message_id); + LinphoneChatMessage *cm = NULL; + if (messages) { + cm = (LinphoneChatMessage *)bctbx_list_nth_data(messages, 0); + linphone_chat_message_ref(cm); + bctbx_list_free_with_data(messages, (bctbx_list_free_func)linphone_chat_message_unref); + } return cm; } diff --git a/coreapi/private.h b/coreapi/private.h index 08b24bfd2..d3c2b933f 100644 --- a/coreapi/private.h +++ b/coreapi/private.h @@ -278,6 +278,11 @@ struct _LinphoneChatMessage { #endif }; +/* + *Gets a Message with a given message id and direction. + */ +LINPHONE_PUBLIC LinphoneChatMessage * linphone_chat_room_find_message_with_dir(LinphoneChatRoom *cr, const char *message_id,LinphoneChatMessageDir dir); + BELLE_SIP_DECLARE_VPTR_NO_EXPORT(LinphoneChatMessage); typedef struct StunCandidate{