From b97c2c46c944885b3c4c48094d114d1626d9016f Mon Sep 17 00:00:00 2001 From: Gordon Quad Date: Sat, 3 Dec 2016 13:05:59 +0000 Subject: [PATCH] message split implemented --- src/twc-chat.c | 28 ++++++++++++++--- src/twc-message-queue.c | 70 +++++++++++++++++++++++++++++++++-------- src/twc-utils.c | 9 ++++++ src/twc-utils.h | 3 ++ src/twc.h | 3 ++ 5 files changed, 96 insertions(+), 17 deletions(-) diff --git a/src/twc-chat.c b/src/twc-chat.c index fe124eb..dfaefed 100644 --- a/src/twc-chat.c +++ b/src/twc-chat.c @@ -309,6 +309,7 @@ void twc_chat_send_message(struct t_twc_chat *chat, const char *message, enum TWC_MESSAGE_TYPE message_type) { + int64_t rc = 0; if (chat->friend_number >= 0) { twc_message_queue_add_friend_message(chat->profile, @@ -323,11 +324,30 @@ twc_chat_send_message(struct t_twc_chat *chat, const char *message, else if (chat->group_number >= 0) { if (message_type == TWC_MESSAGE_TYPE_MESSAGE) - tox_group_message_send(chat->profile->tox, chat->group_number, - (uint8_t *)message, strlen(message)); + { + int len = strlen(message); + while (len > 0) + { + int fit_len = twc_fit_utf8(message, TWC_MAX_GROUP_MESSAGE_LENGTH); + rc = tox_group_message_send(chat->profile->tox, chat->group_number, + (uint8_t *)message, fit_len); + if (rc < 0) + break; + message += fit_len; + len -= fit_len; + } + } else if (message_type == TWC_MESSAGE_TYPE_ACTION) - tox_group_action_send(chat->profile->tox, chat->group_number, - (uint8_t *)message, strlen(message)); + rc = tox_group_action_send(chat->profile->tox, chat->group_number, + (uint8_t *)message, strlen(message)); + if (rc < 0) + { + weechat_printf(chat->buffer, + "%s%sFailed to send message%s", + weechat_prefix("error"), + weechat_color("chat_highlight"), + weechat_color("reset")); + } } } diff --git a/src/twc-message-queue.c b/src/twc-message-queue.c index 4c28fc2..e16a8ac 100644 --- a/src/twc-message-queue.c +++ b/src/twc-message-queue.c @@ -58,21 +58,30 @@ twc_message_queue_add_friend_message(struct t_twc_profile *profile, const char *message, enum TWC_MESSAGE_TYPE message_type) { - struct t_twc_queued_message *queued_message - = malloc(sizeof(struct t_twc_queued_message)); + int len = strlen(message); + while (len > 0) + { + int fit_len = twc_fit_utf8(message, TWC_MAX_FRIEND_MESSAGE_LENGTH); - time_t rawtime = time(NULL); - queued_message->time = malloc(sizeof(struct tm)); - memcpy(queued_message->time, gmtime(&rawtime), sizeof(struct tm)); + struct t_twc_queued_message *queued_message + = malloc(sizeof(struct t_twc_queued_message)); - queued_message->message = strdup(message); - queued_message->message_type = message_type; + time_t rawtime = time(NULL); + queued_message->time = malloc(sizeof(struct tm)); + memcpy(queued_message->time, gmtime(&rawtime), sizeof(struct tm)); - // create a queue if needed and add message - struct t_twc_list *message_queue - = twc_message_queue_get_or_create(profile, friend_number); - twc_list_item_new_data_add(message_queue, queued_message); + queued_message->message = strndup(message, fit_len); + queued_message->message_type = message_type; + message += fit_len; + len -= fit_len; + + // create a queue if needed and add message + struct t_twc_list *message_queue + = twc_message_queue_get_or_create(profile, friend_number); + twc_list_item_new_data_add(message_queue, queued_message); + } + // flush if friend is online if (profile->tox && (tox_friend_get_connection_status(profile->tox, friend_number, NULL) != TOX_CONNECTION_NONE)) @@ -88,6 +97,8 @@ twc_message_queue_flush_friend(struct t_twc_profile *profile, { struct t_twc_list *message_queue = twc_message_queue_get_or_create(profile, friend_number); + struct t_twc_chat *friend_chat + = twc_chat_search_friend(profile, friend_number, true); size_t index; struct t_twc_list_item *item; @@ -106,14 +117,47 @@ twc_message_queue_flush_friend(struct t_twc_profile *profile, strlen(queued_message->message), &err); - if (err != TOX_ERR_FRIEND_SEND_MESSAGE_OK) + if (err == TOX_ERR_FRIEND_SEND_MESSAGE_FRIEND_NOT_CONNECTED) { // break if message send failed break; } else { - // message was sent, free it + char *err_str; + // check if error occured + switch (err) + { + case TOX_ERR_FRIEND_SEND_MESSAGE_TOO_LONG: + err_str = "message too long"; + break; + case TOX_ERR_FRIEND_SEND_MESSAGE_NULL: + err_str = "NULL fields for tox_friend_send_message"; + break; + case TOX_ERR_FRIEND_SEND_MESSAGE_FRIEND_NOT_FOUND: + err_str = "friend not found"; + break; + case TOX_ERR_FRIEND_SEND_MESSAGE_SENDQ: + err_str = "queue allocation error"; + break; + case TOX_ERR_FRIEND_SEND_MESSAGE_EMPTY: + err_str = "tried to send empty message"; + break; + case TOX_ERR_FRIEND_SEND_MESSAGE_OK: + err_str = "no error"; + break; + default: + err_str = "unknown error"; + } + if (err != TOX_ERR_FRIEND_SEND_MESSAGE_OK) + { + weechat_printf(friend_chat->buffer, + "%s%sFailed to send message: %s%s", + weechat_prefix("error"), + weechat_color("highlight"), + err_str, + weechat_color("reset")); + } twc_message_queue_free_message(queued_message); item->queued_message = NULL; } diff --git a/src/twc-utils.c b/src/twc-utils.c index c397f3d..f6f3cc9 100644 --- a/src/twc-utils.c +++ b/src/twc-utils.c @@ -198,3 +198,12 @@ twc_hash_tox_id(const uint8_t *tox_id) return hash; } + +/** + * Fit correct unicode string into max chars. Return number of bytes + */ +int +twc_fit_utf8(const char *str, int max) +{ + return weechat_utf8_real_pos(str, weechat_utf8_strnlen(str, max)); +} diff --git a/src/twc-utils.h b/src/twc-utils.h index 8687dab..78dee94 100644 --- a/src/twc-utils.h +++ b/src/twc-utils.h @@ -54,5 +54,8 @@ twc_uint32_reverse_bytes(uint32_t num); unsigned long long twc_hash_tox_id(const uint8_t *tox_id); +int +twc_fit_utf8(const char *str, int max); + #endif // TOX_WEECHAT_UTILS_H diff --git a/src/twc.h b/src/twc.h index aea6638..ab4a1cd 100644 --- a/src/twc.h +++ b/src/twc.h @@ -32,5 +32,8 @@ enum t_twc_rc TWC_RC_ERROR_MALLOC = -2, }; +#define TWC_MAX_FRIEND_MESSAGE_LENGTH (TOX_MAX_MESSAGE_LENGTH-1) +#define TWC_MAX_GROUP_MESSAGE_LENGTH (TOX_MAX_MESSAGE_LENGTH-9) + #endif // TOX_WEECHAT_H