diff --git a/src/tox-weechat-data.c b/src/tox-weechat-data.c index b8ef54e..43f708a 100644 --- a/src/tox-weechat-data.c +++ b/src/tox-weechat-data.c @@ -120,6 +120,9 @@ tox_weechat_data_unsent_messages_json(struct t_tox_weechat_identity *identity) recipient; recipient = recipient->next_recipient) { + if (!(recipient->unsent_messages)) + continue; + char hex_id[TOX_CLIENT_ID_SIZE * 2 + 1]; tox_weechat_bin2hex(recipient->recipient_id, TOX_CLIENT_ID_SIZE, hex_id); @@ -204,9 +207,26 @@ tox_weechat_data_load_friend_requests(struct t_tox_weechat_identity *identity, */ void tox_weechat_data_load_unsent_messages(struct t_tox_weechat_identity *identity, - json_t *friend_requests) + json_t *recipient_object) { - // TODO + tox_weechat_unsent_messages_free(identity); + + const char *key; + json_t *message_array; + json_object_foreach(recipient_object, key, message_array) + { + uint8_t client_id[TOX_CLIENT_ID_SIZE]; + tox_weechat_hex2bin(key, TOX_CLIENT_ID_SIZE * 2, (char *)client_id); + + size_t index; + json_t *message; + json_array_foreach(message_array, index, message) + { + tox_weechat_add_unsent_message(identity, + client_id, + json_string_value(message)); + } + } } /** diff --git a/src/tox-weechat-friend-requests.c b/src/tox-weechat-friend-requests.c index a3ba524..103d232 100644 --- a/src/tox-weechat-friend-requests.c +++ b/src/tox-weechat-friend-requests.c @@ -74,7 +74,8 @@ tox_weechat_friend_request_add(struct t_tox_weechat_identity *identity, void tox_weechat_friend_request_remove(struct t_tox_weechat_friend_request *request) { - struct t_tox_weechat_identity *identity = request->identity; if (request == identity->last_friend_request) + struct t_tox_weechat_identity *identity = request->identity; + if (request == identity->last_friend_request) identity->last_friend_request = request->prev_request; if (request->prev_request) diff --git a/src/tox-weechat-messages.c b/src/tox-weechat-messages.c index 737f7e1..e17e670 100644 --- a/src/tox-weechat-messages.c +++ b/src/tox-weechat-messages.c @@ -57,7 +57,7 @@ tox_weechat_unsent_message_recipient_new(struct t_tox_weechat_identity *identity return NULL; memcpy(recipient->recipient_id, id, TOX_CLIENT_ID_SIZE); - + recipient->identity = identity; recipient->unsent_messages = recipient->last_unsent_message = NULL; recipient->prev_recipient = identity->last_unsent_message_recipient; @@ -93,6 +93,7 @@ tox_weechat_add_unsent_message(struct t_tox_weechat_identity *identity, return; unsent_message->message = strdup(message); + unsent_message->recipient = recipient; unsent_message->prev_message = recipient->last_unsent_message; unsent_message->next_message = NULL; @@ -105,6 +106,25 @@ tox_weechat_add_unsent_message(struct t_tox_weechat_identity *identity, recipient->last_unsent_message = unsent_message; } +void +tox_weechat_remove_unsent_message(struct t_tox_weechat_unsent_message *message) +{ + struct t_tox_weechat_unsent_message_recipient *recipient = message->recipient; + if (message == recipient->last_unsent_message) + recipient->last_unsent_message = message->prev_message; + + if (message->prev_message) + message->prev_message->next_message = message->next_message; + else + recipient->unsent_messages = message->next_message; + + if (message->next_message) + message->next_message->prev_message = message->prev_message; + + free(message->message); + free(message); +} + /** * Sends a message to a friend. Does message splitting and queuing. */ @@ -128,3 +148,22 @@ tox_weechat_send_friend_message(struct t_tox_weechat_identity *identity, return rc; } +void +tox_weechat_unsent_messages_free(struct t_tox_weechat_identity *identity) +{ + struct t_tox_weechat_unsent_message_recipient *recipient; + for (recipient = identity->unsent_message_recipients; + recipient; + recipient = recipient->next_recipient) + { + while (recipient->unsent_messages) + tox_weechat_remove_unsent_message(recipient->unsent_messages); + } +} + +void +tox_weechat_attempt_message_flush(struct t_tox_weechat_identity *identity, + int32_t friend_number) +{ + +} diff --git a/src/tox-weechat-messages.h b/src/tox-weechat-messages.h index 8ebc743..88f9e46 100644 --- a/src/tox-weechat-messages.h +++ b/src/tox-weechat-messages.h @@ -27,6 +27,7 @@ struct t_tox_weechat_identity; struct t_tox_weechat_unsent_message_recipient { uint8_t recipient_id[TOX_CLIENT_ID_SIZE]; + struct t_tox_weechat_identity *identity; struct t_tox_weechat_unsent_message *unsent_messages; struct t_tox_weechat_unsent_message *last_unsent_message; @@ -37,7 +38,8 @@ struct t_tox_weechat_unsent_message_recipient struct t_tox_weechat_unsent_message { - const char *message; + char *message; + struct t_tox_weechat_unsent_message_recipient *recipient; struct t_tox_weechat_unsent_message *next_message; struct t_tox_weechat_unsent_message *prev_message; @@ -53,4 +55,7 @@ tox_weechat_add_unsent_message(struct t_tox_weechat_identity *identity, const uint8_t *recipient_id, const char *message); +void +tox_weechat_unsent_messages_free(struct t_tox_weechat_identity *identity); + #endif // TOX_WEECHAT_MESSAGES_H