diff --git a/delta-connection.c b/delta-connection.c index 5916615..ee882aa 100644 --- a/delta-connection.c +++ b/delta-connection.c @@ -188,7 +188,10 @@ delta_event_handler(void *context) case DC_EVENT_MSGS_CHANGED: { // This event may be issued for a single message, in which case the // message ID is in data2 and we should treat it as an incoming msg + // FIXME: this leads to duplicate messages when it's an outgoing + // message we just sent uint32_t msg_id = dc_event_get_data2_int(event); + pr = delta_build_process_request(conn); if (msg_id) { // FIXME: they won't all be incoming. Some will be changed @@ -432,15 +435,39 @@ delta_recv_im(DeltaConnectionData *conn, uint32_t msg_id) int viewtype = dc_msg_get_viewtype(msg); time_t timestamp = dc_msg_get_timestamp(msg); char *text = dc_msg_get_text(msg); - uint32_t contact_id = dc_msg_get_from_id(msg); + uint32_t chat_id = dc_msg_get_chat_id(msg); + dc_chat_t *chat = dc_get_chat(mailbox, chat_id); + dc_array_t *contacts = dc_get_chat_contacts(mailbox, chat_id); + int num_contacts = dc_array_get_cnt(contacts); - dc_contact_t *contact = dc_get_contact(mailbox, contact_id); - if (contact == NULL) { - purple_debug_info(PLUGIN_ID, "Receiving IM: unknown contact: %d\n", contact_id); + if (chat == NULL) { + purple_debug_info(PLUGIN_ID, "Receiving IM: unknown chat: %d\n", chat_id); goto out; } - char *who = dc_contact_get_addr(contact); + if (dc_chat_get_type(chat) == DC_CHAT_TYPE_GROUP) { + purple_debug_info(PLUGIN_ID, "Receiving IM: group chat with ID %d! Not yet supported\n", chat_id); + goto out; + } + + if (num_contacts != 1) { + purple_debug_info(PLUGIN_ID, "Receiving IM: 1-1 chat %d with %d contacts instead of 1!\n", chat_id, num_contacts); + goto out; + } + + // FIXME: using dc_array_get_contact_id fails here, complaining that it's not an array of locations + dc_contact_t *contact = dc_get_contact(mailbox, dc_array_get_id(contacts, 0)); + char *who = NULL; + + // In the current architecture, delta_send_im and delta_recv_im must agree + // on the value for 'who'. Using the email address is an easy cheat for this + // but gets shaky in the long term. + if (contact != NULL) { + who = dc_contact_get_addr(contact); + } else { + who = dc_chat_get_name(chat); + } + int flags = PURPLE_MESSAGE_RECV; // FIXME: as a massive hack, convert IMEX setup messages into a text message @@ -500,7 +527,10 @@ delta_recv_im(DeltaConnectionData *conn, uint32_t msg_id) dc_markseen_msgs(mailbox, &msg_id, 1); g_free(who); + dc_contact_unref(contact); out: g_free(text); dc_msg_unref(msg); + dc_chat_unref(chat); + dc_array_unref(contacts); }