Refactored offline messaging.
This commit is contained in:
parent
d45167971d
commit
3ed68cd364
6 changed files with 106 additions and 51 deletions
|
@ -26,29 +26,88 @@
|
|||
|
||||
#include "tox-weechat-messages.h"
|
||||
|
||||
/**
|
||||
* Return an existing unsent message recipient object or NULL.
|
||||
*/
|
||||
struct t_tox_weechat_unsent_message_recipient *
|
||||
tox_weechat_unsent_message_recipient_with_id(struct t_tox_weechat_identity *identity,
|
||||
const uint8_t *id)
|
||||
{
|
||||
struct t_tox_weechat_unsent_message_recipient *recipient;
|
||||
for (recipient = identity->unsent_message_recipients;
|
||||
recipient;
|
||||
recipient = recipient->next_recipient)
|
||||
{
|
||||
if (memcmp(recipient->recipient_id, id, TOX_CLIENT_ID_SIZE) == 0)
|
||||
return recipient;
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create and return a new unsent message recipient object.
|
||||
*/
|
||||
struct t_tox_weechat_unsent_message_recipient *
|
||||
tox_weechat_unsent_message_recipient_new(struct t_tox_weechat_identity *identity,
|
||||
const uint8_t *id)
|
||||
{
|
||||
struct t_tox_weechat_unsent_message_recipient *recipient = malloc(sizeof(*recipient));
|
||||
if (!recipient)
|
||||
return NULL;
|
||||
|
||||
memcpy(recipient->recipient_id, id, TOX_CLIENT_ID_SIZE);
|
||||
|
||||
recipient->unsent_messages = recipient->last_unsent_message = NULL;
|
||||
|
||||
recipient->prev_recipient = identity->last_unsent_message_recipient;
|
||||
recipient->next_recipient = NULL;
|
||||
|
||||
if (identity->unsent_message_recipients == NULL)
|
||||
identity->unsent_message_recipients = recipient;
|
||||
else
|
||||
identity->last_unsent_message_recipient->next_recipient = recipient;
|
||||
|
||||
identity->last_unsent_message_recipient = recipient;
|
||||
|
||||
return recipient;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a new message to the unsent messages queue.
|
||||
*/
|
||||
void
|
||||
tox_weechat_add_unsent_message(struct t_tox_weechat_identity *identity,
|
||||
const uint8_t *recipient_id,
|
||||
const char *message)
|
||||
{
|
||||
struct t_tox_weechat_unsent_message_recipient *recipient
|
||||
= tox_weechat_unsent_message_recipient_with_id(identity, recipient_id);
|
||||
if (!recipient)
|
||||
recipient = tox_weechat_unsent_message_recipient_new(identity, recipient_id);
|
||||
if (!recipient)
|
||||
return;
|
||||
|
||||
struct t_tox_weechat_unsent_message *unsent_message = malloc(sizeof(*unsent_message));
|
||||
if (!message)
|
||||
return;
|
||||
|
||||
memcpy(unsent_message->recipient_id, recipient_id, TOX_CLIENT_ID_SIZE);
|
||||
unsent_message->message = strdup(message);
|
||||
|
||||
unsent_message->prev_message = identity->last_unsent_message;
|
||||
unsent_message->prev_message = recipient->last_unsent_message;
|
||||
unsent_message->next_message = NULL;
|
||||
|
||||
if (identity->unsent_messages == NULL)
|
||||
identity->unsent_messages = unsent_message;
|
||||
if (recipient->unsent_messages == NULL)
|
||||
recipient->unsent_messages = unsent_message;
|
||||
else
|
||||
identity->last_unsent_message->next_message = unsent_message;
|
||||
recipient->last_unsent_message->next_message = unsent_message;
|
||||
|
||||
identity->last_unsent_message = unsent_message;
|
||||
recipient->last_unsent_message = unsent_message;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends a message to a friend. Does message splitting and queuing.
|
||||
*/
|
||||
uint32_t
|
||||
tox_weechat_send_friend_message(struct t_tox_weechat_identity *identity,
|
||||
int32_t friend_number,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue