Refactored offline messaging.
This commit is contained in:
parent
d45167971d
commit
3ed68cd364
@ -41,7 +41,6 @@ const char *tox_weechat_json_friend_request_key_client_id = "client_id";
|
|||||||
const char *tox_weechat_json_friend_request_key_message = "message";
|
const char *tox_weechat_json_friend_request_key_message = "message";
|
||||||
const char *tox_weechat_json_key_unsent_messages = "unsent_messages";
|
const char *tox_weechat_json_key_unsent_messages = "unsent_messages";
|
||||||
const char *tox_weechat_json_unsent_messages_key_recipient_id = "recipient_id";
|
const char *tox_weechat_json_unsent_messages_key_recipient_id = "recipient_id";
|
||||||
const char *tox_weechat_json_unsent_messages_key_message = "message";
|
|
||||||
|
|
||||||
json_t *tox_weechat_json_data = NULL;
|
json_t *tox_weechat_json_data = NULL;
|
||||||
|
|
||||||
@ -111,43 +110,43 @@ tox_weechat_data_friend_requests_json(struct t_tox_weechat_identity *identity)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Save an identity's unsent messages into a json array.
|
* Save an identity's unsent messages into a json object.
|
||||||
*/
|
*/
|
||||||
json_t *
|
json_t *
|
||||||
tox_weechat_data_unsent_messages_json(struct t_tox_weechat_identity *identity)
|
tox_weechat_data_unsent_messages_json(struct t_tox_weechat_identity *identity)
|
||||||
{
|
{
|
||||||
json_t *unsent_messages_array = json_array();
|
json_t *messages_object = json_object();
|
||||||
if (!unsent_messages_array)
|
if (!messages_object)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
for (struct t_tox_weechat_unsent_message *message = identity->unsent_messages;
|
for (struct t_tox_weechat_unsent_message_recipient *recipient = identity->unsent_message_recipients;
|
||||||
|
recipient;
|
||||||
|
recipient = recipient->next_recipient)
|
||||||
|
{
|
||||||
|
char hex_id[TOX_CLIENT_ID_SIZE * 2 + 1];
|
||||||
|
tox_weechat_bin2hex(recipient->recipient_id, TOX_CLIENT_ID_SIZE, hex_id);
|
||||||
|
|
||||||
|
json_t *json_messages = json_array();
|
||||||
|
if (!json_messages)
|
||||||
|
break;
|
||||||
|
|
||||||
|
for (struct t_tox_weechat_unsent_message *message = recipient->unsent_messages;
|
||||||
message;
|
message;
|
||||||
message = message->next_message)
|
message = message->next_message)
|
||||||
{
|
{
|
||||||
char hex_id[TOX_CLIENT_ID_SIZE * 2 + 1];
|
json_t *json_message = json_string(message->message);
|
||||||
tox_weechat_bin2hex(message->recipient_id, TOX_CLIENT_ID_SIZE, hex_id);
|
if (!json_message)
|
||||||
|
|
||||||
json_t *json_message = json_object();
|
|
||||||
json_t *json_id = json_string(hex_id);
|
|
||||||
json_t *json_message_data = json_string(message->message);
|
|
||||||
if (!json_message || !json_id || !json_message_data)
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
json_object_set(json_message,
|
json_array_append(json_messages, json_message);
|
||||||
tox_weechat_json_unsent_messages_key_recipient_id,
|
|
||||||
json_id);
|
|
||||||
json_decref(json_id);
|
|
||||||
|
|
||||||
json_object_set(json_message,
|
|
||||||
tox_weechat_json_unsent_messages_key_message,
|
|
||||||
json_message_data);
|
|
||||||
json_decref(json_message_data);
|
|
||||||
|
|
||||||
json_array_append(unsent_messages_array, json_message);
|
|
||||||
json_decref(json_message);
|
json_decref(json_message);
|
||||||
}
|
}
|
||||||
|
|
||||||
return unsent_messages_array;
|
json_object_set(messages_object, hex_id, json_messages);
|
||||||
|
json_decref(json_messages);
|
||||||
|
}
|
||||||
|
|
||||||
|
return messages_object;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -175,6 +174,7 @@ tox_weechat_data_identity_save(struct t_tox_weechat_identity *identity)
|
|||||||
char *identity_key = tox_weechat_json_get_identity_key(identity);
|
char *identity_key = tox_weechat_json_get_identity_key(identity);
|
||||||
json_object_set(tox_weechat_json_data, identity_key, json_data);
|
json_object_set(tox_weechat_json_data, identity_key, json_data);
|
||||||
free(identity_key);
|
free(identity_key);
|
||||||
|
|
||||||
json_decref(json_data);
|
json_decref(json_data);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -215,23 +215,7 @@ void
|
|||||||
tox_weechat_data_load_unsent_messages(struct t_tox_weechat_identity *identity,
|
tox_weechat_data_load_unsent_messages(struct t_tox_weechat_identity *identity,
|
||||||
json_t *friend_requests)
|
json_t *friend_requests)
|
||||||
{
|
{
|
||||||
size_t index;
|
|
||||||
json_t *json_message;
|
|
||||||
json_array_foreach(friend_requests, index, json_message)
|
|
||||||
{
|
|
||||||
char client_id[TOX_CLIENT_ID_SIZE];
|
|
||||||
const char *message;
|
|
||||||
|
|
||||||
json_t *json_id = json_object_get(json_message,
|
|
||||||
tox_weechat_json_unsent_messages_key_recipient_id);
|
|
||||||
json_t *json_message_data = json_object_get(json_message,
|
|
||||||
tox_weechat_json_unsent_messages_key_message);
|
|
||||||
|
|
||||||
tox_weechat_hex2bin(json_string_value(json_id), TOX_CLIENT_ID_SIZE * 2, client_id);
|
|
||||||
message = json_string_value(json_message_data);
|
|
||||||
|
|
||||||
// TODO
|
// TODO
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -263,6 +247,7 @@ tox_weechat_data_load()
|
|||||||
{
|
{
|
||||||
char *full_path = tox_weechat_json_data_file_path();
|
char *full_path = tox_weechat_json_data_file_path();
|
||||||
|
|
||||||
|
|
||||||
json_error_t error;
|
json_error_t error;
|
||||||
tox_weechat_json_data = json_load_file(full_path, 0, &error);
|
tox_weechat_json_data = json_load_file(full_path, 0, &error);
|
||||||
free(full_path);
|
free(full_path);
|
||||||
|
@ -207,7 +207,7 @@ tox_weechat_identity_new(const char *name)
|
|||||||
identity->tox_do_timer = NULL;
|
identity->tox_do_timer = NULL;
|
||||||
identity->chats = identity->last_chat = NULL;
|
identity->chats = identity->last_chat = NULL;
|
||||||
identity->friend_requests = identity->last_friend_request = NULL;
|
identity->friend_requests = identity->last_friend_request = NULL;
|
||||||
identity->unsent_messages = identity->last_unsent_message = NULL;
|
identity->unsent_message_recipients = identity->last_unsent_message_recipient = NULL;
|
||||||
identity->tox_online = false;
|
identity->tox_online = false;
|
||||||
|
|
||||||
// set up config
|
// set up config
|
||||||
|
@ -52,8 +52,8 @@ struct t_tox_weechat_identity
|
|||||||
struct t_tox_weechat_friend_request *last_friend_request;
|
struct t_tox_weechat_friend_request *last_friend_request;
|
||||||
unsigned int friend_request_count;
|
unsigned int friend_request_count;
|
||||||
|
|
||||||
struct t_tox_weechat_unsent_message *unsent_messages;
|
struct t_tox_weechat_unsent_message_recipient *unsent_message_recipients;
|
||||||
struct t_tox_weechat_unsent_message *last_unsent_message;
|
struct t_tox_weechat_unsent_message_recipient *last_unsent_message_recipient;
|
||||||
|
|
||||||
struct t_tox_weechat_identity *next_identity;
|
struct t_tox_weechat_identity *next_identity;
|
||||||
struct t_tox_weechat_identity *prev_identity;
|
struct t_tox_weechat_identity *prev_identity;
|
||||||
|
@ -26,29 +26,88 @@
|
|||||||
|
|
||||||
#include "tox-weechat-messages.h"
|
#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
|
void
|
||||||
tox_weechat_add_unsent_message(struct t_tox_weechat_identity *identity,
|
tox_weechat_add_unsent_message(struct t_tox_weechat_identity *identity,
|
||||||
const uint8_t *recipient_id,
|
const uint8_t *recipient_id,
|
||||||
const char *message)
|
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));
|
struct t_tox_weechat_unsent_message *unsent_message = malloc(sizeof(*unsent_message));
|
||||||
if (!message)
|
if (!message)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
memcpy(unsent_message->recipient_id, recipient_id, TOX_CLIENT_ID_SIZE);
|
|
||||||
unsent_message->message = strdup(message);
|
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;
|
unsent_message->next_message = NULL;
|
||||||
|
|
||||||
if (identity->unsent_messages == NULL)
|
if (recipient->unsent_messages == NULL)
|
||||||
identity->unsent_messages = unsent_message;
|
recipient->unsent_messages = unsent_message;
|
||||||
else
|
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
|
uint32_t
|
||||||
tox_weechat_send_friend_message(struct t_tox_weechat_identity *identity,
|
tox_weechat_send_friend_message(struct t_tox_weechat_identity *identity,
|
||||||
int32_t friend_number,
|
int32_t friend_number,
|
||||||
|
@ -24,10 +24,20 @@
|
|||||||
|
|
||||||
struct t_tox_weechat_identity;
|
struct t_tox_weechat_identity;
|
||||||
|
|
||||||
|
struct t_tox_weechat_unsent_message_recipient
|
||||||
|
{
|
||||||
|
uint8_t recipient_id[TOX_CLIENT_ID_SIZE];
|
||||||
|
|
||||||
|
struct t_tox_weechat_unsent_message *unsent_messages;
|
||||||
|
struct t_tox_weechat_unsent_message *last_unsent_message;
|
||||||
|
|
||||||
|
struct t_tox_weechat_unsent_message_recipient *next_recipient;
|
||||||
|
struct t_tox_weechat_unsent_message_recipient *prev_recipient;
|
||||||
|
};
|
||||||
|
|
||||||
struct t_tox_weechat_unsent_message
|
struct t_tox_weechat_unsent_message
|
||||||
{
|
{
|
||||||
const char *message;
|
const char *message;
|
||||||
uint8_t recipient_id[TOX_CLIENT_ID_SIZE];
|
|
||||||
|
|
||||||
struct t_tox_weechat_unsent_message *next_message;
|
struct t_tox_weechat_unsent_message *next_message;
|
||||||
struct t_tox_weechat_unsent_message *prev_message;
|
struct t_tox_weechat_unsent_message *prev_message;
|
||||||
|
@ -62,8 +62,9 @@ int
|
|||||||
weechat_plugin_end(struct t_weechat_plugin *plugin)
|
weechat_plugin_end(struct t_weechat_plugin *plugin)
|
||||||
{
|
{
|
||||||
tox_weechat_config_write();
|
tox_weechat_config_write();
|
||||||
tox_weechat_data_save();
|
|
||||||
tox_weechat_identity_free_all();
|
tox_weechat_identity_free_all();
|
||||||
|
tox_weechat_data_save();
|
||||||
|
tox_weechat_data_free();
|
||||||
|
|
||||||
return WEECHAT_RC_OK;
|
return WEECHAT_RC_OK;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user