From fa77a3084e9892ccf5ef7d6406904602622ffef6 Mon Sep 17 00:00:00 2001 From: Gordon Quad Date: Sat, 3 Dec 2016 04:37:41 +0000 Subject: [PATCH 01/18] fixed weechat_printf_date_tags call for weechat 1.6 API --- src/twc-chat.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/twc-chat.c b/src/twc-chat.c index 058847e..fe124eb 100644 --- a/src/twc-chat.c +++ b/src/twc-chat.c @@ -287,13 +287,13 @@ twc_chat_print_message(struct t_twc_chat *chat, switch (message_type) { case TWC_MESSAGE_TYPE_MESSAGE: - weechat_printf_tags(chat->buffer, tags, + weechat_printf_date_tags(chat->buffer, 0, tags, "%s%s%s\t%s", color, sender, weechat_color("reset"), message); break; case TWC_MESSAGE_TYPE_ACTION: - weechat_printf_tags(chat->buffer, tags, + weechat_printf_date_tags(chat->buffer, 0, tags, "%s%s%s%s %s", weechat_prefix("action"), color, sender, From bdbbf1178d4f43a6c2a5dd44abc7a35ed5d288ce Mon Sep 17 00:00:00 2001 From: Gordon Quad Date: Sat, 3 Dec 2016 11:33:05 +0000 Subject: [PATCH 02/18] autojoin implemented --- src/twc-config.c | 7 ++++ src/twc-profile.h | 1 + src/twc-tox-callbacks.c | 79 ++++++++++++++++++++++++++++++++--------- 3 files changed, 70 insertions(+), 17 deletions(-) diff --git a/src/twc-config.c b/src/twc-config.c index 02b2b49..a2b5b7f 100644 --- a/src/twc-config.c +++ b/src/twc-config.c @@ -42,6 +42,7 @@ char *twc_profile_option_names[TWC_PROFILE_NUM_OPTIONS] = { "save_file", "autoload", + "autojoin", "max_friend_requests", "proxy_address", "proxy_port", @@ -198,6 +199,12 @@ twc_config_init_option(struct t_config_section *section, "network when WeeChat starts"; default_value = "off"; break; + case TWC_PROFILE_OPTION_AUTOJOIN: + type = "boolean"; + description = "automatically join all groups you are invited in " + "by your friends"; + default_value = "off"; + break; case TWC_PROFILE_OPTION_IPV6: type = "boolean"; description = "use IPv6 as well as IPv4 to connect to the Tox " diff --git a/src/twc-profile.h b/src/twc-profile.h index 9790692..5bbd91f 100644 --- a/src/twc-profile.h +++ b/src/twc-profile.h @@ -30,6 +30,7 @@ enum t_twc_profile_option { TWC_PROFILE_OPTION_SAVEFILE = 0, TWC_PROFILE_OPTION_AUTOLOAD, + TWC_PROFILE_OPTION_AUTOJOIN, TWC_PROFILE_OPTION_MAX_FRIEND_REQUESTS, TWC_PROFILE_OPTION_PROXY_ADDRESS, TWC_PROFILE_OPTION_PROXY_PORT, diff --git a/src/twc-tox-callbacks.c b/src/twc-tox-callbacks.c index 624aabc..379397b 100644 --- a/src/twc-tox-callbacks.c +++ b/src/twc-tox-callbacks.c @@ -22,6 +22,10 @@ #include #include +#ifdef TOXAV_ENABLED + #include +#endif // TOXAV_ENABLED + #include "twc.h" #include "twc-profile.h" #include "twc-chat.h" @@ -209,9 +213,7 @@ twc_group_invite_callback(Tox *tox, char *friend_name = twc_get_name_nt(profile->tox, friend_number); struct t_twc_chat *friend_chat = twc_chat_search_friend(profile, friend_number, true); - - int64_t rc = twc_group_chat_invite_add(profile, friend_number, type, - (uint8_t *)invite_data, length); + int64_t rc; char *type_str; switch (type) @@ -224,25 +226,68 @@ twc_group_invite_callback(Tox *tox, type_str = "a group chat"; break; } - if (rc >= 0) + if (TWC_PROFILE_OPTION_BOOLEAN(profile, TWC_PROFILE_OPTION_AUTOJOIN)) { - weechat_printf(friend_chat->buffer, - "%s%s%s%s invites you to join %s. Type " - "\"/group join %d\" to accept.", - weechat_prefix("network"), - weechat_color("chat_nick_other"), friend_name, - weechat_color("reset"), type_str, rc); + switch (type) + { + case TOX_GROUPCHAT_TYPE_TEXT: + rc = tox_join_groupchat(tox, friend_number, + invite_data, length); + break; +#ifdef TOXAV_ENABLED + case TOX_GROUPCHAT_TYPE_AV: + rc = toxav_join_av_groupchat(tox, friend_number, + invite_data, length, + NULL, NULL); + break; +#endif + default: + rc = -1; + break; + } + + if (rc >= 0) + { + weechat_printf(friend_chat->buffer, + "%sWe joined the %s%s%s's invite to %s.", + weechat_prefix("network"), + weechat_color("chat_nick_other"), friend_name, + weechat_color("reset"), type_str, rc); + } + else + { + weechat_printf(friend_chat->buffer, + "%s%s%s%s invites you to join %s, but we failed to " + "process the invite. Please try again.", + weechat_prefix("network"), + weechat_color("chat_nick_other"), friend_name, + weechat_color("reset"), rc); + } } else { - weechat_printf(friend_chat->buffer, - "%s%s%s%s invites you to join %s, but we failed to " - "process the invite. Please try again.", - weechat_prefix("network"), - weechat_color("chat_nick_other"), friend_name, - weechat_color("reset"), rc); - } + rc = twc_group_chat_invite_add(profile, friend_number, type, + (uint8_t *)invite_data, length); + if (rc >= 0) + { + weechat_printf(friend_chat->buffer, + "%s%s%s%s invites you to join %s. Type " + "\"/group join %d\" to accept.", + weechat_prefix("network"), + weechat_color("chat_nick_other"), friend_name, + weechat_color("reset"), type_str, rc); + } + else + { + weechat_printf(friend_chat->buffer, + "%s%s%s%s invites you to join %s, but we failed to " + "process the invite. Please try again.", + weechat_prefix("network"), + weechat_color("chat_nick_other"), friend_name, + weechat_color("reset"), rc); + } + } free(friend_name); } From b97c2c46c944885b3c4c48094d114d1626d9016f Mon Sep 17 00:00:00 2001 From: Gordon Quad Date: Sat, 3 Dec 2016 13:05:59 +0000 Subject: [PATCH 03/18] 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 From ad2a727a91d7aa545b9c1713243728de12acb77e Mon Sep 17 00:00:00 2001 From: Gordon Quad Date: Sat, 3 Dec 2016 14:45:20 +0000 Subject: [PATCH 04/18] new bootstrap nodes --- src/twc-bootstrap.c | 85 ++++++++++++++++++++++++++++++++------------- 1 file changed, 60 insertions(+), 25 deletions(-) diff --git a/src/twc-bootstrap.c b/src/twc-bootstrap.c index ff68db9..c465398 100644 --- a/src/twc-bootstrap.c +++ b/src/twc-bootstrap.c @@ -24,37 +24,72 @@ #include "twc-bootstrap.h" char *twc_bootstrap_addresses[] = { - "192.254.75.98", - "31.7.57.236", - "107.161.17.51", - "144.76.60.215", - "23.226.230.47", - "37.59.102.176", - "37.187.46.132", - "178.21.112.187", - "192.210.149.121", - "54.199.139.199", - "63.165.243.15", + "178.62.250.138", + "130.133.110.14", + "104.167.101.29", + "205.185.116.116", + "198.98.51.198", + "108.61.165.198", + "194.249.212.109", + "185.25.116.107", + "192.99.168.140", + "95.215.46.114", + "5.189.176.217", + "148.251.23.146", + "104.223.122.15", + "81.4.110.149", + "95.31.20.151", + "104.233.104.126", + "51.254.84.212", + "5.135.59.163", + "185.58.206.164", + "91.121.66.124", + "92.54.84.70", + "195.93.190.6", + "95.215.44.78", + "163.172.136.118", + "37.97.185.116", + "193.124.186.205", + "80.87.193.193", + "46.229.52.198" }; uint16_t twc_bootstrap_ports[] = { - 33445, 443, 33445, 33445, 33445, - 33445, 33445, 33445, 33445, 33445, - 443, + 33445, 33445, 33445, 33445, 33445, 33445, 33445, 33445, + 33445, 33445, 5190, 2306, 33445, 33445, 33445, 33445, + 33445, 33445, 33445, 33445, 33445, 33445, 33445, 33445, + 33445, 5228, 33445, 33445 }; char *twc_bootstrap_keys[] = { - "951C88B7E75C867418ACDB5D273821372BB5BD652740BCDF623A4FA293E75D2F", - "2A4B50D1D525DA2E669592A20C327B5FAD6C7E5962DC69296F9FEC77C4436E4E", - "7BE3951B97CA4B9ECDDA768E8C52BA19E9E2690AB584787BF4C90E04DBB75111", - "04119E835DF3E78BACF0F84235B300546AF8B936F035185E2A8E9E0A67C8924F", - "A09162D68618E742FFBCA1C2C70385E6679604B2D80EA6E84AD0996A1AC8A074", - "B98A2CEAA6C6A2FADC2C3632D284318B60FE5375CCB41EFA081AB67F500C1B0B", - "5EB67C51D3FF5A9D528D242B669036ED2A30F8A60E674C45E7D43010CB2E1331", - "4B2C19E924972CB9B57732FB172F8A8604DE13EEDA2A6234E348983344B23057", - "F404ABAA1C99A9D37D61AB54898F56793E1DEF8BD46B1038B9D822E8460FAB67", - "7F9C31FE850E97CEFD4C4591DF93FC757C7C12549DDD55F8EEAECC34FE76C029", - "8CD087E31C67568103E8C2A28653337E90E6B8EDA0D765D57C6B5172B4F1F04C", + "788236D34978D1D5BD822F0A5BEBD2C53C64CC31CD3149350EE27D4D9A2F9B6B", + "461FA3776EF0FA655F1A05477DF1B3B614F7D6B124F7DB1DD4FE3C08B03B640F", + "5918AC3C06955962A75AD7DF4F80A5D7C34F7DB9E1498D2E0495DE35B3FE8A57", + "A179B09749AC826FF01F37A9613F6B57118AE014D4196A0E1105A98F93A54702", + "1D5A5F2F5D6233058BF0259B09622FB40B482E4FA0931EB8FD3AB8E7BF7DAF6F", + "8E7D0B859922EF569298B4D261A8CCB5FEA14FB91ED412A7603A585A25698832", + "3CEE1F054081E7A011234883BC4FC39F661A55B73637A5AC293DDF1251D9432B", + "DA4E4ED4B697F2E9B000EEFE3A34B554ACD3F45F5C96EAEA2516DD7FF9AF7B43", + "6A4D0607A296838434A6A7DDF99F50EF9D60A2C510BBF31FE538A25CB6B4652F", + "5823FB947FF24CF83DDFAC3F3BAA18F96EA2018B16CC08429CB97FA502F40C23", + "2B2137E094F743AC8BD44652C55F41DFACC502F125E99E4FE24D40537489E32F", + "7AED21F94D82B05774F697B209628CD5A9AD17E0C073D9329076A4C28ED28147", + "0FB96EEBFB1650DDB52E70CF773DDFCABE25A95CC3BB50FC251082E4B63EF82A", + "9E7BD4793FFECA7F32238FA2361040C09025ED3333744483CA6F3039BFF0211E", + "9CA69BB74DE7C056D1CC6B16AB8A0A38725C0349D187D8996766958584D39340", + "EDEE8F2E839A57820DE3DA4156D88350E53D4161447068A3457EE8F59F362414", + "AEC204B9A4501412D5F0BB67D9C81B5DB3EE6ADA64122D32A3E9B093D544327D", + "2D320F971EF2CA18004416C2AAE7BA52BF7949DB34EA8E2E21AF67BD367BE211", + "24156472041E5F220D1FA11D9DF32F7AD697D59845701CDD7BE7D1785EB9DB39", + "4E3F7D37295664BBD0741B6DBCB6431D6CD77FC4105338C2FC31567BF5C8224A", + "5625A62618CB4FCA70E147A71B29695F38CC65FF0CBD68AD46254585BE564802", + "FB4CE0DDEFEED45F26917053E5D24BDDA0FA0A3D83A672A9DA2375928B37023D", + "672DBE27B4ADB9D5FB105A6BB648B2F8FDB89B3323486A7A21968316E012023C", + "2C289F9F37C20D09DA83565588BF496FAB3764853FA38141817A72E3F18ACA0B", + "E59A0E71ADA20D35BD1B0957059D7EF7E7792B3D680AE25C6F4DBBA09114D165", + "9906D65F2A4751068A59D30505C5FC8AE1A95E0843AE9372EAFA3BAB6AC16C2C", + "B38255EE4B054924F6D79A5E6E5889EC94B6ADF6FE9906F97A3D01E3D083223A", + "813C8F4187833EF0655B10F7752141A352248462A567529A38B6BBF73E979307" }; int twc_bootstrap_count = sizeof(twc_bootstrap_addresses) From 56c0c924ac3a5e6a556aad21fc74a8d7d326e09b Mon Sep 17 00:00:00 2001 From: Gordon Quad Date: Sun, 4 Dec 2016 22:50:53 +0000 Subject: [PATCH 05/18] nicks hash moved to peer_number keys --- src/twc-chat.c | 25 +++--------------- src/twc-tox-callbacks.c | 57 ++++++++++++++++++++++++----------------- src/twc-utils.c | 16 +----------- src/twc-utils.h | 3 --- 4 files changed, 37 insertions(+), 64 deletions(-) diff --git a/src/twc-chat.c b/src/twc-chat.c index dfaefed..68a2d3b 100644 --- a/src/twc-chat.c +++ b/src/twc-chat.c @@ -45,25 +45,6 @@ twc_chat_buffer_close_callback(const void *pointer, void *data, struct t_gui_buffer *weechat_buffer); -/** - * Hash a Tox ID for the nicklist hashtable in group chat objects. - */ -unsigned long long -twc_tox_id_hash_callback(struct t_hashtable *hashtable, const void *key) -{ - return twc_hash_tox_id(key); -} - -/** - * Compare two Tox IDs for the nicklist hashtable in group chat objects. - */ -int -twc_tox_id_compare_callback(struct t_hashtable *hashtable, - const void *id1, const void *id2) -{ - return memcmp(id1, id2, TOX_PUBLIC_KEY_SIZE); -} - /** * Create a new chat. */ @@ -137,10 +118,10 @@ twc_chat_new_group(struct t_twc_profile *profile, int32_t group_number) chat->nicklist_group = weechat_nicklist_add_group(chat->buffer, NULL, NULL, NULL, true); chat->nicks = weechat_hashtable_new(32, - WEECHAT_HASHTABLE_BUFFER, + WEECHAT_HASHTABLE_INTEGER, WEECHAT_HASHTABLE_POINTER, - twc_tox_id_hash_callback, - twc_tox_id_compare_callback); + NULL, + NULL); weechat_buffer_set(chat->buffer, "nicklist", "1"); } diff --git a/src/twc-tox-callbacks.c b/src/twc-tox-callbacks.c index 379397b..4a555eb 100644 --- a/src/twc-tox-callbacks.c +++ b/src/twc-tox-callbacks.c @@ -84,6 +84,9 @@ twc_connection_status_callback(Tox *tox, uint32_t friend_number, { struct t_twc_profile *profile = data; char *name = twc_get_name_nt(profile->tox, friend_number); + struct t_twc_chat *chat = twc_chat_search_friend(profile, + friend_number, + false); // TODO: print in friend's buffer if it exists if (status == 0) @@ -92,6 +95,13 @@ twc_connection_status_callback(Tox *tox, uint32_t friend_number, "%s%s just went offline.", weechat_prefix("network"), name); + if (chat) + { + weechat_printf(chat->buffer, + "%s%s just went offline.", + weechat_prefix("network"), + name); + } } else if (status == 1) { @@ -99,6 +109,13 @@ twc_connection_status_callback(Tox *tox, uint32_t friend_number, "%s%s just came online.", weechat_prefix("network"), name); + if (chat) + { + weechat_printf(chat->buffer, + "%s%s just came online.", + weechat_prefix("network"), + name); + } twc_message_queue_flush_friend(profile, friend_number); } free(name); @@ -365,34 +382,26 @@ twc_group_namelist_change_callback(Tox *tox, char *name = twc_get_peer_name_nt(profile->tox, group_number, peer_number); char *prev_name = NULL; - uint8_t pubkey[TOX_PUBLIC_KEY_SIZE]; - int pkrc = tox_group_peer_pubkey(profile->tox, group_number, - peer_number, pubkey); - if (pkrc == 0) + if (change_type == TOX_CHAT_CHANGE_PEER_DEL + || change_type == TOX_CHAT_CHANGE_PEER_NAME) { - if (change_type == TOX_CHAT_CHANGE_PEER_DEL - || change_type == TOX_CHAT_CHANGE_PEER_NAME) + nick = weechat_hashtable_get(chat->nicks, &peer_number); + if (nick) { - nick = weechat_hashtable_get(chat->nicks, pubkey); - if (nick) - { - prev_name = strdup(weechat_nicklist_nick_get_string(chat->buffer, - nick, "name")); - weechat_nicklist_remove_nick(chat->buffer, nick); - weechat_hashtable_remove(chat->nicks, pubkey); - } + prev_name = strdup(weechat_nicklist_nick_get_string(chat->buffer, + nick, "name")); + weechat_nicklist_remove_nick(chat->buffer, nick); + weechat_hashtable_remove(chat->nicks, &peer_number); } + } - if (change_type == TOX_CHAT_CHANGE_PEER_ADD - || change_type == TOX_CHAT_CHANGE_PEER_NAME) - { - nick = weechat_nicklist_add_nick(chat->buffer, chat->nicklist_group, - name, NULL, NULL, NULL, 1); - if (nick) - weechat_hashtable_set_with_size(chat->nicks, - pubkey, TOX_PUBLIC_KEY_SIZE, - nick, 0); - } + if (change_type == TOX_CHAT_CHANGE_PEER_ADD + || change_type == TOX_CHAT_CHANGE_PEER_NAME) + { + nick = weechat_nicklist_add_nick(chat->buffer, chat->nicklist_group, + name, NULL, NULL, NULL, 1); + if (nick) + weechat_hashtable_set(chat->nicks, &peer_number, nick); } switch (change_type) diff --git a/src/twc-utils.c b/src/twc-utils.c index f6f3cc9..e6da153 100644 --- a/src/twc-utils.c +++ b/src/twc-utils.c @@ -123,7 +123,7 @@ twc_get_status_message_nt(Tox *tox, int32_t friend_number) char * twc_get_peer_name_nt(Tox *tox, int32_t group_number, int32_t peer_number) { - uint8_t name[TOX_MAX_NAME_LENGTH] = {0}; + uint8_t name[TOX_MAX_NAME_LENGTH+1] = {0}; int length = tox_group_peername(tox, group_number, peer_number, name); if (length >= 0) @@ -185,20 +185,6 @@ twc_uint32_reverse_bytes(uint32_t num) return res; } -/** - * Hash a Tox ID of size TOX_PUBLIC_KEY_SIZE bytes using a modified djb2 hash. - */ -unsigned long long -twc_hash_tox_id(const uint8_t *tox_id) -{ - unsigned long long hash = 5381; - - for (size_t i = 0; i < TOX_PUBLIC_KEY_SIZE; ++i) - hash ^= (hash << 5) + (hash >> 2) + tox_id[i]; - - return hash; -} - /** * Fit correct unicode string into max chars. Return number of bytes */ diff --git a/src/twc-utils.h b/src/twc-utils.h index 78dee94..9f9cfbb 100644 --- a/src/twc-utils.h +++ b/src/twc-utils.h @@ -51,9 +51,6 @@ twc_get_friend_id_short(Tox *tox, int32_t friend_number); uint32_t 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); From 043784e91dba9eb92f650a9258c2ddab1da3f0ee Mon Sep 17 00:00:00 2001 From: Gordon Quad Date: Sun, 4 Dec 2016 23:44:28 +0000 Subject: [PATCH 06/18] highlight implemented --- src/twc-chat.c | 2 +- src/twc-message-queue.c | 2 +- src/twc-tox-callbacks.c | 11 +++++++++-- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/src/twc-chat.c b/src/twc-chat.c index 68a2d3b..f2df707 100644 --- a/src/twc-chat.c +++ b/src/twc-chat.c @@ -297,7 +297,7 @@ twc_chat_send_message(struct t_twc_chat *chat, const char *message, chat->friend_number, message, message_type); char *name = twc_get_self_name_nt(chat->profile->tox); - twc_chat_print_message(chat, "", + twc_chat_print_message(chat, "notify_message", weechat_color("chat_nick_self"), name, message, message_type); free(name); diff --git a/src/twc-message-queue.c b/src/twc-message-queue.c index e16a8ac..f822d44 100644 --- a/src/twc-message-queue.c +++ b/src/twc-message-queue.c @@ -154,7 +154,7 @@ twc_message_queue_flush_friend(struct t_twc_profile *profile, weechat_printf(friend_chat->buffer, "%s%sFailed to send message: %s%s", weechat_prefix("error"), - weechat_color("highlight"), + weechat_color("chat_highlight"), err_str, weechat_color("reset")); } diff --git a/src/twc-tox-callbacks.c b/src/twc-tox-callbacks.c index 4a555eb..4250e6a 100644 --- a/src/twc-tox-callbacks.c +++ b/src/twc-tox-callbacks.c @@ -71,7 +71,7 @@ twc_friend_message_callback(Tox *tox, uint32_t friend_number, char *name = twc_get_name_nt(profile->tox, friend_number); char *message_nt = twc_null_terminate(message, length); - twc_chat_print_message(chat, "", weechat_color("chat_nick_other"), name, + twc_chat_print_message(chat, "notify_private", weechat_color("chat_nick_other"), name, message_nt, type); free(name); @@ -321,7 +321,9 @@ twc_handle_group_message(Tox *tox, group_number, true); + char myname[TOX_MAX_NAME_LENGTH]; char *name = twc_get_peer_name_nt(profile->tox, group_number, peer_number); + char *tags = "notify_message"; char *message_nt = twc_null_terminate(message, length); const char *nick_color; @@ -330,7 +332,12 @@ twc_handle_group_message(Tox *tox, else nick_color = weechat_info_get("nick_color", name); - twc_chat_print_message(chat, "", nick_color, name, + tox_self_get_name(tox, (uint8_t *)myname); + myname[tox_self_get_name_size(tox)] = '\0'; + + if ((myname[0] != '\0') && weechat_string_has_highlight(message_nt, myname)) + tags = "notify_highlight"; + twc_chat_print_message(chat, tags, nick_color, name, message_nt, message_type); free(name); From 8501d36ee9e888b26631332bab93a1cc3da7aed9 Mon Sep 17 00:00:00 2001 From: Gordon Quad Date: Mon, 5 Dec 2016 00:19:33 +0000 Subject: [PATCH 07/18] nicklist added to proflie buffer to show online friends --- src/twc-profile.c | 4 ++++ src/twc-profile.h | 1 + src/twc-tox-callbacks.c | 18 ++++++++++++++++++ 3 files changed, 23 insertions(+) diff --git a/src/twc-profile.c b/src/twc-profile.c index b1afe2c..75a08fb 100644 --- a/src/twc-profile.c +++ b/src/twc-profile.c @@ -285,6 +285,10 @@ twc_profile_load(struct t_twc_profile *profile) profile, NULL); if (!(profile->buffer)) return TWC_RC_ERROR; + + profile->nicklist_group = weechat_nicklist_add_group(profile->buffer, NULL, + NULL, NULL, true); + weechat_buffer_set(profile->buffer, "nicklist", "1"); } weechat_printf(profile->buffer, diff --git a/src/twc-profile.h b/src/twc-profile.h index 5bbd91f..6279681 100644 --- a/src/twc-profile.h +++ b/src/twc-profile.h @@ -51,6 +51,7 @@ struct t_twc_profile int tox_online; struct t_gui_buffer *buffer; + struct t_gui_nick_group *nicklist_group; struct t_hook *tox_do_timer; struct t_twc_list *chats; diff --git a/src/twc-tox-callbacks.c b/src/twc-tox-callbacks.c index 4250e6a..58bf5e8 100644 --- a/src/twc-tox-callbacks.c +++ b/src/twc-tox-callbacks.c @@ -84,6 +84,7 @@ twc_connection_status_callback(Tox *tox, uint32_t friend_number, { struct t_twc_profile *profile = data; char *name = twc_get_name_nt(profile->tox, friend_number); + struct t_gui_nick *nick = NULL; struct t_twc_chat *chat = twc_chat_search_friend(profile, friend_number, false); @@ -91,6 +92,11 @@ twc_connection_status_callback(Tox *tox, uint32_t friend_number, // TODO: print in friend's buffer if it exists if (status == 0) { + nick = weechat_nicklist_search_nick(profile->buffer, + profile->nicklist_group, name); + if (nick) + weechat_nicklist_remove_nick(profile->buffer, nick); + weechat_printf(profile->buffer, "%s%s just went offline.", weechat_prefix("network"), @@ -105,6 +111,9 @@ twc_connection_status_callback(Tox *tox, uint32_t friend_number, } else if (status == 1) { + weechat_nicklist_add_nick(profile->buffer, profile->nicklist_group, + name, NULL, NULL, NULL, 1); + weechat_printf(profile->buffer, "%s%s just came online.", weechat_prefix("network"), @@ -127,6 +136,7 @@ twc_name_change_callback(Tox *tox, uint32_t friend_number, void *data) { struct t_twc_profile *profile = data; + struct t_gui_nick *nick = NULL; struct t_twc_chat *chat = twc_chat_search_friend(profile, friend_number, false); @@ -146,6 +156,14 @@ twc_name_change_callback(Tox *tox, uint32_t friend_number, old_name, new_name); } + nick = weechat_nicklist_search_nick(profile->buffer, + profile->nicklist_group, old_name); + if (nick) + weechat_nicklist_remove_nick(profile->buffer, nick); + + weechat_nicklist_add_nick(profile->buffer, profile->nicklist_group, + new_name, NULL, NULL, NULL, 1); + weechat_printf(profile->buffer, "%s%s is now known as %s", weechat_prefix("network"), From 1c819672ceb4b5a20dc1008688678cf7a3ac9dbf Mon Sep 17 00:00:00 2001 From: Gordon Quad Date: Mon, 5 Dec 2016 00:37:40 +0000 Subject: [PATCH 08/18] friend remove fix --- src/twc-commands.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/twc-commands.c b/src/twc-commands.c index 1015253..3a77041 100644 --- a/src/twc-commands.c +++ b/src/twc-commands.c @@ -380,7 +380,7 @@ twc_cmd_friend(const void *pointer, void *data, struct t_gui_buffer *buffer, TWC_CHECK_FRIEND_NUMBER(profile, friend_number, argv[2]); char *name = twc_get_name_nt(profile->tox, friend_number); - if (tox_friend_delete(profile->tox, friend_number, NULL) == 0) + if (tox_friend_delete(profile->tox, friend_number, NULL)) { weechat_printf(profile->buffer, "%sRemoved %s from friend list.", From b17f1a89c69b2ccd0c62564158c661713f2c5752 Mon Sep 17 00:00:00 2001 From: Gordon Quad Date: Mon, 5 Dec 2016 01:08:19 +0000 Subject: [PATCH 09/18] friend accept fix --- src/twc-commands.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/twc-commands.c b/src/twc-commands.c index 3a77041..b143e19 100644 --- a/src/twc-commands.c +++ b/src/twc-commands.c @@ -464,7 +464,7 @@ twc_cmd_friend(const void *pointer, void *data, struct t_gui_buffer *buffer, if (accept) { - if (twc_friend_request_accept(request)) + if (!twc_friend_request_accept(request)) { weechat_printf(profile->buffer, "%sCould not accept friend request from %s", From 5ec7423d19cfc0076769979fef9b94a6f2d8c75c Mon Sep 17 00:00:00 2001 From: Gordon Quad Date: Mon, 5 Dec 2016 01:08:39 +0000 Subject: [PATCH 10/18] friend request highlight --- src/twc-friend-request.c | 2 +- src/twc-tox-callbacks.c | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/twc-friend-request.c b/src/twc-friend-request.c index d5d7f82..bc49389 100644 --- a/src/twc-friend-request.c +++ b/src/twc-friend-request.c @@ -67,7 +67,7 @@ twc_friend_request_add(struct t_twc_profile *profile, bool twc_friend_request_accept(struct t_twc_friend_request *request) { - TOX_ERR_FRIEND_ADD err; + TOX_ERR_FRIEND_ADD err = TOX_ERR_FRIEND_ADD_OK; tox_friend_add_norequest(request->profile->tox, request->tox_id, &err); twc_friend_request_remove(request); diff --git a/src/twc-tox-callbacks.c b/src/twc-tox-callbacks.c index 58bf5e8..ee2d9fc 100644 --- a/src/twc-tox-callbacks.c +++ b/src/twc-tox-callbacks.c @@ -211,7 +211,7 @@ twc_friend_request_callback(Tox *tox, const uint8_t *public_key, if (rc == -1) { - weechat_printf(profile->buffer, + weechat_printf_date_tags(profile->buffer, 0, "notify_private", "%sReceived a friend request, but your friend request list is full!", weechat_prefix("warning")); } @@ -220,7 +220,7 @@ twc_friend_request_callback(Tox *tox, const uint8_t *public_key, char hex_address[TOX_PUBLIC_KEY_SIZE * 2 + 1]; twc_bin2hex(public_key, TOX_PUBLIC_KEY_SIZE, hex_address); - weechat_printf(profile->buffer, + weechat_printf_date_tags(profile->buffer, 0, "notify_private", "%sReceived a friend request from %s with message \"%s\"; " "accept it with \"/friend accept %d\"", weechat_prefix("network"), @@ -228,7 +228,7 @@ twc_friend_request_callback(Tox *tox, const uint8_t *public_key, if (rc == -2) { - weechat_printf(profile->buffer, + weechat_printf_date_tags(profile->buffer, 0, "notify_private", "%sFailed to save friend request, try manually " "accepting with /friend add", weechat_prefix("error")); From 44c24bdd38d1ef1b4ff8de6881d669a05faa3821 Mon Sep 17 00:00:00 2001 From: Gordon Quad Date: Mon, 5 Dec 2016 01:25:42 +0000 Subject: [PATCH 11/18] fix unwanted creation of buffers --- src/twc-message-queue.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/twc-message-queue.c b/src/twc-message-queue.c index f822d44..58bd15f 100644 --- a/src/twc-message-queue.c +++ b/src/twc-message-queue.c @@ -97,9 +97,6 @@ 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; twc_list_foreach(message_queue, index, item) @@ -151,6 +148,9 @@ twc_message_queue_flush_friend(struct t_twc_profile *profile, } if (err != TOX_ERR_FRIEND_SEND_MESSAGE_OK) { + struct t_twc_chat *friend_chat + = twc_chat_search_friend(profile, friend_number, true); + weechat_printf(friend_chat->buffer, "%s%sFailed to send message: %s%s", weechat_prefix("error"), From f91b117996e31d03ae304a68e69a76d2d33a1323 Mon Sep 17 00:00:00 2001 From: Gordon Quad Date: Mon, 5 Dec 2016 01:59:18 +0000 Subject: [PATCH 12/18] put invite messages to profile buffer if there is no friend buffer opened --- src/twc-tox-callbacks.c | 98 +++++++++++++++++++++++++++++------------ 1 file changed, 71 insertions(+), 27 deletions(-) diff --git a/src/twc-tox-callbacks.c b/src/twc-tox-callbacks.c index ee2d9fc..c3c77c3 100644 --- a/src/twc-tox-callbacks.c +++ b/src/twc-tox-callbacks.c @@ -211,7 +211,7 @@ twc_friend_request_callback(Tox *tox, const uint8_t *public_key, if (rc == -1) { - weechat_printf_date_tags(profile->buffer, 0, "notify_private", + weechat_printf_date_tags(profile->buffer, 0, "notify_highlight", "%sReceived a friend request, but your friend request list is full!", weechat_prefix("warning")); } @@ -220,7 +220,7 @@ twc_friend_request_callback(Tox *tox, const uint8_t *public_key, char hex_address[TOX_PUBLIC_KEY_SIZE * 2 + 1]; twc_bin2hex(public_key, TOX_PUBLIC_KEY_SIZE, hex_address); - weechat_printf_date_tags(profile->buffer, 0, "notify_private", + weechat_printf_date_tags(profile->buffer, 0, "notify_highlight", "%sReceived a friend request from %s with message \"%s\"; " "accept it with \"/friend accept %d\"", weechat_prefix("network"), @@ -228,7 +228,7 @@ twc_friend_request_callback(Tox *tox, const uint8_t *public_key, if (rc == -2) { - weechat_printf_date_tags(profile->buffer, 0, "notify_private", + weechat_printf_date_tags(profile->buffer, 0, "notify_highlight", "%sFailed to save friend request, try manually " "accepting with /friend add", weechat_prefix("error")); @@ -247,8 +247,9 @@ twc_group_invite_callback(Tox *tox, struct t_twc_profile *profile = data; char *friend_name = twc_get_name_nt(profile->tox, friend_number); struct t_twc_chat *friend_chat - = twc_chat_search_friend(profile, friend_number, true); + = twc_chat_search_friend(profile, friend_number, false); int64_t rc; + char *tags; char *type_str; switch (type) @@ -283,20 +284,41 @@ twc_group_invite_callback(Tox *tox, if (rc >= 0) { - weechat_printf(friend_chat->buffer, - "%sWe joined the %s%s%s's invite to %s.", - weechat_prefix("network"), - weechat_color("chat_nick_other"), friend_name, - weechat_color("reset"), type_str, rc); + tags = "notify_private"; + if (friend_chat) + { + weechat_printf_date_tags(friend_chat->buffer, 0, tags, + "%sWe joined the %s%s%s's invite to %s.", + weechat_prefix("network"), + weechat_color("chat_nick_other"), friend_name, + weechat_color("reset"), type_str); + tags = ""; + } + weechat_printf_date_tags(profile->buffer, 0, tags, + "%sWe joined the %s%s%s's invite to %s.", + weechat_prefix("network"), + weechat_color("chat_nick_other"), friend_name, + weechat_color("reset"), type_str); } else { - weechat_printf(friend_chat->buffer, - "%s%s%s%s invites you to join %s, but we failed to " - "process the invite. Please try again.", - weechat_prefix("network"), - weechat_color("chat_nick_other"), friend_name, - weechat_color("reset"), rc); + tags = "notify_highlight"; + if (friend_chat) + { + weechat_printf_date_tags(friend_chat->buffer, 0, tags, + "%s%s%s%s invites you to join %s, but we failed to " + "process the invite. Please try again.", + weechat_prefix("network"), + weechat_color("chat_nick_other"), friend_name, + weechat_color("reset")); + tags = ""; + } + weechat_printf_date_tags(profile->buffer, 0, tags, + "%s%s%s%s invites you to join %s, but we failed to " + "process the invite. Please try again.", + weechat_prefix("network"), + weechat_color("chat_nick_other"), friend_name, + weechat_color("reset")); } } else @@ -306,21 +328,43 @@ twc_group_invite_callback(Tox *tox, if (rc >= 0) { - weechat_printf(friend_chat->buffer, - "%s%s%s%s invites you to join %s. Type " - "\"/group join %d\" to accept.", - weechat_prefix("network"), - weechat_color("chat_nick_other"), friend_name, - weechat_color("reset"), type_str, rc); + tags = "notify_highlight"; + if (friend_chat) + { + weechat_printf_date_tags(friend_chat->buffer, 0, tags, + "%s%s%s%s invites you to join %s. Type " + "\"/group join %d\" to accept.", + weechat_prefix("network"), + weechat_color("chat_nick_other"), friend_name, + weechat_color("reset"), type_str, rc); + tags = ""; + } + weechat_printf_date_tags(profile->buffer, 0, tags, + "%s%s%s%s invites you to join %s. Type " + "\"/group join %d\" to accept.", + weechat_prefix("network"), + weechat_color("chat_nick_other"), friend_name, + weechat_color("reset"), type_str, rc); } else { - weechat_printf(friend_chat->buffer, - "%s%s%s%s invites you to join %s, but we failed to " - "process the invite. Please try again.", - weechat_prefix("network"), - weechat_color("chat_nick_other"), friend_name, - weechat_color("reset"), rc); + tags = "notify_highlight"; + if (friend_chat) + { + weechat_printf_date_tags(friend_chat->buffer, 0, tags, + "%s%s%s%s invites you to join %s, but we failed to " + "process the invite. Please try again.", + weechat_prefix("network"), + weechat_color("chat_nick_other"), friend_name, + weechat_color("reset"), rc); + tags = ""; + } + weechat_printf_date_tags(profile->buffer, 0, tags, + "%s%s%s%s invites you to join %s, but we failed to " + "process the invite. Please try again.", + weechat_prefix("network"), + weechat_color("chat_nick_other"), friend_name, + weechat_color("reset"), rc); } } free(friend_name); From 19d279faa8037e39739685c0929c2b849bf0fd60 Mon Sep 17 00:00:00 2001 From: Gordon Quad Date: Mon, 5 Dec 2016 03:15:19 +0000 Subject: [PATCH 13/18] get self name fixed --- src/twc-tox-callbacks.c | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/twc-tox-callbacks.c b/src/twc-tox-callbacks.c index c3c77c3..3dbf942 100644 --- a/src/twc-tox-callbacks.c +++ b/src/twc-tox-callbacks.c @@ -383,7 +383,7 @@ twc_handle_group_message(Tox *tox, group_number, true); - char myname[TOX_MAX_NAME_LENGTH]; + char *myname = twc_get_self_name_nt(profile->tox); char *name = twc_get_peer_name_nt(profile->tox, group_number, peer_number); char *tags = "notify_message"; char *message_nt = twc_null_terminate(message, length); @@ -394,15 +394,13 @@ twc_handle_group_message(Tox *tox, else nick_color = weechat_info_get("nick_color", name); - tox_self_get_name(tox, (uint8_t *)myname); - myname[tox_self_get_name_size(tox)] = '\0'; - - if ((myname[0] != '\0') && weechat_string_has_highlight(message_nt, myname)) + if (weechat_string_has_highlight(message_nt, myname)) tags = "notify_highlight"; twc_chat_print_message(chat, tags, nick_color, name, message_nt, message_type); free(name); + free(myname); free(message_nt); } From 6969ac387c365185ff6eecf5e81e98abbd20dad1 Mon Sep 17 00:00:00 2001 From: Gordon Quad Date: Mon, 5 Dec 2016 03:23:13 +0000 Subject: [PATCH 14/18] friend list improved --- src/twc-commands.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/twc-commands.c b/src/twc-commands.c index b143e19..4521878 100644 --- a/src/twc-commands.c +++ b/src/twc-commands.c @@ -242,7 +242,7 @@ twc_cmd_friend(const void *pointer, void *data, struct t_gui_buffer *buffer, } weechat_printf(profile->buffer, - "%s[#] Name [Tox ID (short)]", + "%s[#] Name [Tox ID (short)] Status", weechat_prefix("network")); for (size_t i = 0; i < friend_count; ++i) @@ -252,12 +252,16 @@ twc_cmd_friend(const void *pointer, void *data, struct t_gui_buffer *buffer, char *hex_address = twc_get_friend_id_short(profile->tox, friend_number); + char *status = twc_get_status_message_nt(profile->tox, friend_number); + char *online_color = + (tox_friend_get_connection_status(profile->tox, friend_number, NULL) != TOX_CONNECTION_NONE) ? "chat_nick" : "chat_nick_offline"; weechat_printf(profile->buffer, - "%s[%d] %s [%s]", + "%s[%d] %s%s [%s]%s %s", weechat_prefix("network"), - friend_number, name, hex_address); + friend_number, weechat_color(online_color), name, hex_address, weechat_color("reset"), status); free(name); + free(status); free(hex_address); } From de6ba32b25a42f6386fd70bc0fa8807e8c3c465e Mon Sep 17 00:00:00 2001 From: Gordon Quad Date: Mon, 5 Dec 2016 09:42:57 +0000 Subject: [PATCH 15/18] adjust max groupchat message --- src/twc.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/twc.h b/src/twc.h index ab4a1cd..8cd2275 100644 --- a/src/twc.h +++ b/src/twc.h @@ -33,7 +33,7 @@ enum t_twc_rc }; #define TWC_MAX_FRIEND_MESSAGE_LENGTH (TOX_MAX_MESSAGE_LENGTH-1) -#define TWC_MAX_GROUP_MESSAGE_LENGTH (TOX_MAX_MESSAGE_LENGTH-9) +#define TWC_MAX_GROUP_MESSAGE_LENGTH (TOX_MAX_MESSAGE_LENGTH-16) #endif // TOX_WEECHAT_H From 8d395d80112f36af35318c2718c62bf4478a141b Mon Sep 17 00:00:00 2001 From: Gordon Quad Date: Tue, 6 Dec 2016 14:17:19 +0000 Subject: [PATCH 16/18] bug of creating duplicate buffers fixed --- src/twc-chat.c | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/src/twc-chat.c b/src/twc-chat.c index f2df707..8e9821a 100644 --- a/src/twc-chat.c +++ b/src/twc-chat.c @@ -62,9 +62,24 @@ twc_chat_new(struct t_twc_profile *profile, const char *name) size_t full_name_size = strlen(profile->name) + 1 + strlen(name) + 1; char *full_name = malloc(full_name_size); snprintf(full_name, full_name_size, "%s/%s", profile->name, name); - chat->buffer = weechat_buffer_new(full_name, - twc_chat_buffer_input_callback, chat, NULL, - twc_chat_buffer_close_callback, chat, NULL); + chat->buffer = weechat_buffer_search("tox", full_name); + if (!(chat->buffer)) + { + chat->buffer = weechat_buffer_new(full_name, + twc_chat_buffer_input_callback, chat, NULL, + twc_chat_buffer_close_callback, chat, NULL); + } + else + { + weechat_buffer_set_pointer(chat->buffer, + "input_callback", + twc_chat_buffer_input_callback); + weechat_buffer_set_pointer(chat->buffer, "input_callback_pointer", chat); + weechat_buffer_set_pointer(chat->buffer, + "close_callback", + twc_chat_buffer_close_callback); + weechat_buffer_set_pointer(chat->buffer, "close_callback_pointer", chat); + } free(full_name); if (!(chat->buffer)) @@ -380,6 +395,7 @@ twc_chat_buffer_close_callback(const void *pointer, void *data, void twc_chat_free(struct t_twc_chat *chat) { + weechat_nicklist_remove_all(chat->buffer); if (chat->nicks) weechat_hashtable_free(chat->nicks); free(chat); From cc9d370105997c10c3f403809e908b22e0d7092b Mon Sep 17 00:00:00 2001 From: Gordon Quad Date: Thu, 22 Dec 2016 12:35:08 +0000 Subject: [PATCH 17/18] migration to TokTok c-toxcore --- src/twc-chat.c | 66 +++++++++++++++-------------- src/twc-chat.h | 10 +---- src/twc-commands.c | 40 ++++++++++-------- src/twc-group-invite.c | 12 +++--- src/twc-group-invite.h | 2 +- src/twc-message-queue.c | 6 +-- src/twc-message-queue.h | 4 +- src/twc-profile.c | 21 +++++----- src/twc-tox-callbacks.c | 92 +++++++++++++++++------------------------ src/twc-tox-callbacks.h | 29 +++++-------- src/twc-utils.c | 13 ++++-- 11 files changed, 141 insertions(+), 154 deletions(-) diff --git a/src/twc-chat.c b/src/twc-chat.c index 8e9821a..de827d1 100644 --- a/src/twc-chat.c +++ b/src/twc-chat.c @@ -80,6 +80,7 @@ twc_chat_new(struct t_twc_profile *profile, const char *name) twc_chat_buffer_close_callback); weechat_buffer_set_pointer(chat->buffer, "close_callback_pointer", chat); } + free(full_name); if (!(chat->buffer)) @@ -152,6 +153,9 @@ twc_chat_refresh(const struct t_twc_chat *chat) { char *name = NULL; char *title = NULL; + bool rc = false; + TOX_ERR_CONFERENCE_TITLE err = TOX_ERR_CONFERENCE_TITLE_OK; + if (chat->friend_number >= 0) { name = twc_get_name_nt(chat->profile->tox, @@ -162,10 +166,12 @@ twc_chat_refresh(const struct t_twc_chat *chat) else if (chat->group_number >= 0) { char group_name[TOX_MAX_NAME_LENGTH + 1] = {0}; - int len = tox_group_get_title(chat->profile->tox, chat->group_number, - (uint8_t *)group_name, - TOX_MAX_NAME_LENGTH); - if (len <= 0) + int len = tox_conference_get_title_size(chat->profile->tox, chat->group_number, + &err); + if ((err == TOX_ERR_CONFERENCE_TITLE_OK) && (len <= TOX_MAX_NAME_LENGTH)) + rc = tox_conference_get_title(chat->profile->tox, chat->group_number, + (uint8_t *)group_name, &err); + if (!rc) sprintf(group_name, "Group Chat %d", chat->group_number); name = title = strdup((char *)group_name); @@ -278,17 +284,17 @@ twc_chat_print_message(struct t_twc_chat *chat, const char *color, const char *sender, const char *message, - enum TWC_MESSAGE_TYPE message_type) + TOX_MESSAGE_TYPE message_type) { switch (message_type) { - case TWC_MESSAGE_TYPE_MESSAGE: + case TOX_MESSAGE_TYPE_NORMAL: weechat_printf_date_tags(chat->buffer, 0, tags, "%s%s%s\t%s", color, sender, weechat_color("reset"), message); break; - case TWC_MESSAGE_TYPE_ACTION: + case TOX_MESSAGE_TYPE_ACTION: weechat_printf_date_tags(chat->buffer, 0, tags, "%s%s%s%s %s", weechat_prefix("action"), @@ -303,9 +309,9 @@ twc_chat_print_message(struct t_twc_chat *chat, */ void twc_chat_send_message(struct t_twc_chat *chat, const char *message, - enum TWC_MESSAGE_TYPE message_type) + TOX_MESSAGE_TYPE message_type) { - int64_t rc = 0; + TOX_ERR_CONFERENCE_SEND_MESSAGE err = TOX_ERR_CONFERENCE_SEND_MESSAGE_OK; if (chat->friend_number >= 0) { twc_message_queue_add_friend_message(chat->profile, @@ -319,29 +325,26 @@ 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) + int len = strlen(message); + while (len > 0) { - 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; - } + int fit_len = twc_fit_utf8(message, TWC_MAX_GROUP_MESSAGE_LENGTH); + err = TOX_ERR_CONFERENCE_SEND_MESSAGE_OK; + tox_conference_send_message(chat->profile->tox, chat->group_number, + message_type, (uint8_t *)message, fit_len, + &err); + if (err != TOX_ERR_CONFERENCE_SEND_MESSAGE_OK) + break; + message += fit_len; + len -= fit_len; } - else if (message_type == TWC_MESSAGE_TYPE_ACTION) - rc = tox_group_action_send(chat->profile->tox, chat->group_number, - (uint8_t *)message, strlen(message)); - if (rc < 0) + if (err != TOX_ERR_CONFERENCE_SEND_MESSAGE_OK) { weechat_printf(chat->buffer, - "%s%sFailed to send message%s", + "%s%sFailed to send message with error %d%s", weechat_prefix("error"), weechat_color("chat_highlight"), + err, weechat_color("reset")); } } @@ -357,7 +360,7 @@ twc_chat_buffer_input_callback(const void *pointer, void *data, { /* TODO: don't strip the const */ struct t_twc_chat *chat = (void *)pointer; - twc_chat_send_message(chat, input_data, TWC_MESSAGE_TYPE_MESSAGE); + twc_chat_send_message(chat, input_data, TOX_MESSAGE_TYPE_NORMAL); return WEECHAT_RC_OK; } @@ -370,16 +373,17 @@ twc_chat_buffer_close_callback(const void *pointer, void *data, struct t_gui_buffer *weechat_buffer) { /* TODO: don't strip the const */ + TOX_ERR_CONFERENCE_DELETE err = TOX_ERR_CONFERENCE_DELETE_OK; struct t_twc_chat *chat = (void *)pointer; if (chat->profile->tox && chat->group_number >= 0) { - int rc = tox_del_groupchat(chat->profile->tox, chat->group_number); - if (rc != 0) + tox_conference_delete(chat->profile->tox, chat->group_number, &err); + if (err != TOX_ERR_CONFERENCE_DELETE_OK) { weechat_printf(chat->profile->buffer, - "%swarning: failed to leave group chat", - weechat_prefix("error")); + "%swarning: failed to leave group chat with error %d", + weechat_prefix("error"), err); } } diff --git a/src/twc-chat.h b/src/twc-chat.h index 83a7226..37b2377 100644 --- a/src/twc-chat.h +++ b/src/twc-chat.h @@ -29,12 +29,6 @@ extern const char *twc_tag_unsent_message; extern const char *twc_tag_sent_message; extern const char *twc_tag_received_message; -enum TWC_MESSAGE_TYPE -{ - TWC_MESSAGE_TYPE_MESSAGE, - TWC_MESSAGE_TYPE_ACTION, -}; - struct t_twc_chat { struct t_twc_profile *profile; @@ -64,11 +58,11 @@ twc_chat_print_message(struct t_twc_chat *chat, const char *color, const char *sender, const char *message, - enum TWC_MESSAGE_TYPE message_type); + TOX_MESSAGE_TYPE message_type); void twc_chat_send_message(struct t_twc_chat *chat, const char *message, - enum TWC_MESSAGE_TYPE message_type); + TOX_MESSAGE_TYPE message_type); void twc_chat_queue_refresh(struct t_twc_chat *chat); diff --git a/src/twc-commands.c b/src/twc-commands.c index 4521878..def511f 100644 --- a/src/twc-commands.c +++ b/src/twc-commands.c @@ -534,19 +534,20 @@ twc_cmd_group(const void *pointer, void *data, struct t_gui_buffer *buffer, int argc, char **argv, char **argv_eol) { struct t_twc_profile *profile = twc_profile_search_buffer(buffer); + TOX_ERR_CONFERENCE_NEW err = TOX_ERR_CONFERENCE_NEW_OK; TWC_CHECK_PROFILE(profile); TWC_CHECK_PROFILE_LOADED(profile); // /group create if (argc == 2 && weechat_strcasecmp(argv[1], "create") == 0) { - int rc = tox_add_groupchat(profile->tox); - if (rc >= 0) + int rc = tox_conference_new(profile->tox, &err); + if (err == TOX_ERR_CONFERENCE_NEW_OK) twc_chat_search_group(profile, rc, true); else weechat_printf(profile->buffer, - "%sCould not create group chat (unknown error)", - weechat_prefix("error")); + "%sCould not create group chat with error %d", + weechat_prefix("error"), err); return WEECHAT_RC_OK; } @@ -626,6 +627,7 @@ twc_cmd_invite(const void *pointer, void *data, struct t_gui_buffer *buffer, if (argc == 1) return WEECHAT_RC_ERROR; + TOX_ERR_CONFERENCE_INVITE err = TOX_ERR_CONFERENCE_INVITE_OK; struct t_twc_chat *chat = twc_chat_search_buffer(buffer); TWC_CHECK_PROFILE_LOADED(chat->profile); @@ -634,10 +636,9 @@ twc_cmd_invite(const void *pointer, void *data, struct t_gui_buffer *buffer, int32_t friend_number = twc_match_friend(chat->profile, argv_eol[1]); TWC_CHECK_FRIEND_NUMBER(chat->profile, friend_number, argv_eol[1]); - int rc = tox_invite_friend(chat->profile->tox, - friend_number, chat->group_number); + tox_conference_invite(chat->profile->tox, friend_number, chat->group_number, &err); - if (rc == 0) + if (err == TOX_ERR_CONFERENCE_INVITE_OK) { char *friend_name = twc_get_name_nt(chat->profile->tox, friend_number); weechat_printf(chat->buffer, "%sInvited %s to the group chat.", @@ -647,8 +648,8 @@ twc_cmd_invite(const void *pointer, void *data, struct t_gui_buffer *buffer, else { weechat_printf(chat->buffer, - "%sFailed to send group chat invite (unknown error)", - weechat_prefix("error")); + "%sFailed to send group chat invite with error %d", + weechat_prefix("error"), err); } return WEECHAT_RC_OK; @@ -668,7 +669,7 @@ twc_cmd_me(const void *pointer, void *data, struct t_gui_buffer *buffer, TWC_CHECK_CHAT(chat); TWC_CHECK_PROFILE_LOADED(chat->profile); - twc_chat_send_message(chat, argv_eol[1], TWC_MESSAGE_TYPE_ACTION); + twc_chat_send_message(chat, argv_eol[1], TOX_MESSAGE_TYPE_ACTION); return WEECHAT_RC_OK; } @@ -714,7 +715,7 @@ twc_cmd_msg(const void *pointer, void *data, struct t_gui_buffer *buffer, if (message) twc_chat_send_message(chat, weechat_string_strip(message, 1, 1, " "), - TWC_MESSAGE_TYPE_MESSAGE); + TOX_MESSAGE_TYPE_NORMAL); return WEECHAT_RC_OK; } @@ -860,11 +861,12 @@ twc_cmd_part(const void *pointer, void *data, struct t_gui_buffer *buffer, int argc, char **argv, char **argv_eol) { struct t_twc_chat *chat = twc_chat_search_buffer(buffer); + TOX_ERR_CONFERENCE_DELETE err = TOX_ERR_CONFERENCE_DELETE_OK; TWC_CHECK_PROFILE_LOADED(chat->profile); TWC_CHECK_GROUP_CHAT(chat); - int rc = tox_del_groupchat(chat->profile->tox, chat->group_number); - if (rc == 0) + tox_conference_delete(chat->profile->tox, chat->group_number, &err); + if (err == TOX_ERR_CONFERENCE_DELETE_OK) { weechat_printf(chat->buffer, "%sYou have left the group chat", @@ -873,8 +875,8 @@ twc_cmd_part(const void *pointer, void *data, struct t_gui_buffer *buffer, else { weechat_printf(chat->buffer, - "%sFailed to leave group chat", - weechat_prefix("error")); + "%sFailed to leave group chat with error %d", + weechat_prefix("error"), err); } weechat_buffer_set_pointer(chat->buffer, "input_callback", NULL); @@ -996,6 +998,8 @@ twc_cmd_topic(const void *pointer, void *data, struct t_gui_buffer *buffer, if (argc == 1) return WEECHAT_RC_ERROR; + TOX_ERR_CONFERENCE_TITLE err = TOX_ERR_CONFERENCE_TITLE_OK; + struct t_twc_chat *chat = twc_chat_search_buffer(buffer); TWC_CHECK_CHAT(chat); TWC_CHECK_PROFILE_LOADED(chat->profile); @@ -1011,9 +1015,9 @@ twc_cmd_topic(const void *pointer, void *data, struct t_gui_buffer *buffer, char *topic = argv_eol[1]; - int result = tox_group_set_title(chat->profile->tox, chat->group_number, - (uint8_t *)topic, strlen(topic)); - if (result == -1) + tox_conference_set_title(chat->profile->tox, chat->group_number, + (uint8_t *)topic, strlen(topic), &err); + if (err != TOX_ERR_CONFERENCE_TITLE_OK) { weechat_printf(chat->buffer, "%s%s", weechat_prefix("error"), "Could not set topic."); diff --git a/src/twc-group-invite.c b/src/twc-group-invite.c index f72641d..545db67 100644 --- a/src/twc-group-invite.c +++ b/src/twc-group-invite.c @@ -71,15 +71,15 @@ int twc_group_chat_invite_join(struct t_twc_group_chat_invite *invite) { int rc; + TOX_ERR_CONFERENCE_JOIN err = TOX_ERR_CONFERENCE_JOIN_OK; switch (invite->group_chat_type) { - case TOX_GROUPCHAT_TYPE_TEXT: - rc = tox_join_groupchat(invite->profile->tox, - invite->friend_number, - invite->data, invite->data_size); + case TOX_CONFERENCE_TYPE_TEXT: + rc = tox_conference_join(invite->profile->tox, invite->friend_number, + invite->data, invite->data_size, &err); break; #ifdef TOXAV_ENABLED - case TOX_GROUPCHAT_TYPE_AV: + case TOX_CONFERENCE_TYPE_AV: rc = toxav_join_av_groupchat(invite->profile->tox, invite->friend_number, invite->data, invite->data_size, @@ -93,6 +93,8 @@ twc_group_chat_invite_join(struct t_twc_group_chat_invite *invite) twc_group_chat_invite_remove(invite); + if (err != TOX_ERR_CONFERENCE_JOIN_OK) + return -1; return rc; } diff --git a/src/twc-group-invite.h b/src/twc-group-invite.h index 8e8bf67..e531102 100644 --- a/src/twc-group-invite.h +++ b/src/twc-group-invite.h @@ -33,7 +33,7 @@ struct t_twc_group_chat_invite { struct t_twc_profile *profile; - int32_t friend_number; + uint32_t friend_number; uint8_t group_chat_type; uint8_t *data; size_t data_size; diff --git a/src/twc-message-queue.c b/src/twc-message-queue.c index 58bd15f..9528eea 100644 --- a/src/twc-message-queue.c +++ b/src/twc-message-queue.c @@ -56,7 +56,7 @@ void twc_message_queue_add_friend_message(struct t_twc_profile *profile, int32_t friend_number, const char *message, - enum TWC_MESSAGE_TYPE message_type) + TOX_MESSAGE_TYPE message_type) { int len = strlen(message); while (len > 0) @@ -107,9 +107,7 @@ twc_message_queue_flush_friend(struct t_twc_profile *profile, TOX_ERR_FRIEND_SEND_MESSAGE err; (void)tox_friend_send_message(profile->tox, friend_number, - queued_message->message_type == TWC_MESSAGE_TYPE_MESSAGE? - TOX_MESSAGE_TYPE_NORMAL: - TOX_MESSAGE_TYPE_ACTION, + queued_message->message_type, (uint8_t *)queued_message->message, strlen(queued_message->message), &err); diff --git a/src/twc-message-queue.h b/src/twc-message-queue.h index ef7ef8a..1983945 100644 --- a/src/twc-message-queue.h +++ b/src/twc-message-queue.h @@ -33,14 +33,14 @@ struct t_twc_queued_message { struct tm *time; char *message; - enum TWC_MESSAGE_TYPE message_type; + TOX_MESSAGE_TYPE message_type; }; void twc_message_queue_add_friend_message(struct t_twc_profile *profile, int32_t friend_number, const char *message, - enum TWC_MESSAGE_TYPE message_type); + TOX_MESSAGE_TYPE message_type); void twc_message_queue_flush_friend(struct t_twc_profile *profile, diff --git a/src/twc-profile.c b/src/twc-profile.c index 75a08fb..e5255b4 100644 --- a/src/twc-profile.c +++ b/src/twc-profile.c @@ -416,17 +416,16 @@ twc_profile_load(struct t_twc_profile *profile) twc_do_timer_cb(profile, NULL, 0); // register Tox callbacks - tox_callback_friend_message(profile->tox, twc_friend_message_callback, profile); - tox_callback_friend_connection_status(profile->tox, twc_connection_status_callback, profile); - tox_callback_friend_name(profile->tox, twc_name_change_callback, profile); - tox_callback_friend_status(profile->tox, twc_user_status_callback, profile); - tox_callback_friend_status_message(profile->tox, twc_status_message_callback, profile); - tox_callback_friend_request(profile->tox, twc_friend_request_callback, profile); - tox_callback_group_invite(profile->tox, twc_group_invite_callback, profile); - tox_callback_group_message(profile->tox, twc_group_message_callback, profile); - tox_callback_group_action(profile->tox, twc_group_action_callback, profile); - tox_callback_group_namelist_change(profile->tox, twc_group_namelist_change_callback, profile); - tox_callback_group_title(profile->tox, twc_group_title_callback, profile); + tox_callback_friend_message(profile->tox, twc_friend_message_callback); + tox_callback_friend_connection_status(profile->tox, twc_connection_status_callback); + tox_callback_friend_name(profile->tox, twc_name_change_callback); + tox_callback_friend_status(profile->tox, twc_user_status_callback); + tox_callback_friend_status_message(profile->tox, twc_status_message_callback); + tox_callback_friend_request(profile->tox, twc_friend_request_callback); + tox_callback_conference_invite(profile->tox, twc_group_invite_callback); + tox_callback_conference_message(profile->tox, twc_group_message_callback); + tox_callback_conference_namelist_change(profile->tox, twc_group_namelist_change_callback); + tox_callback_conference_title(profile->tox, twc_group_title_callback); return TWC_RC_OK; } diff --git a/src/twc-tox-callbacks.c b/src/twc-tox-callbacks.c index 3dbf942..97194c2 100644 --- a/src/twc-tox-callbacks.c +++ b/src/twc-tox-callbacks.c @@ -43,7 +43,7 @@ twc_do_timer_cb(const void *pointer, void *data, /* TODO: don't strip the const */ struct t_twc_profile *profile = (void *)pointer; - tox_iterate(profile->tox); + tox_iterate(profile->tox, profile); struct t_hook *hook = weechat_hook_timer(tox_iteration_interval(profile->tox), 0, 1, twc_do_timer_cb, profile, NULL); profile->tox_do_timer = hook; @@ -240,10 +240,11 @@ twc_friend_request_callback(Tox *tox, const uint8_t *public_key, void twc_group_invite_callback(Tox *tox, - int32_t friend_number, uint8_t type, - const uint8_t *invite_data, uint16_t length, + uint32_t friend_number, TOX_CONFERENCE_TYPE type, + const uint8_t *invite_data, size_t length, void *data) { + TOX_ERR_CONFERENCE_JOIN err = TOX_ERR_CONFERENCE_JOIN_OK; struct t_twc_profile *profile = data; char *friend_name = twc_get_name_nt(profile->tox, friend_number); struct t_twc_chat *friend_chat @@ -254,10 +255,10 @@ twc_group_invite_callback(Tox *tox, char *type_str; switch (type) { - case TOX_GROUPCHAT_TYPE_TEXT: + case TOX_CONFERENCE_TYPE_TEXT: type_str = "a text-only group chat"; break; - case TOX_GROUPCHAT_TYPE_AV: - type_str = "an audio/video group chat"; break; + case TOX_CONFERENCE_TYPE_AV: + type_str = "an audio/vikdeo group chat"; break; default: type_str = "a group chat"; break; } @@ -266,12 +267,12 @@ twc_group_invite_callback(Tox *tox, { switch (type) { - case TOX_GROUPCHAT_TYPE_TEXT: - rc = tox_join_groupchat(tox, friend_number, - invite_data, length); + case TOX_CONFERENCE_TYPE_TEXT: + rc = tox_conference_join(tox, friend_number, + invite_data, length, &err); break; #ifdef TOXAV_ENABLED - case TOX_GROUPCHAT_TYPE_AV: + case TOX_CONFERENCE_TYPE_AV: rc = toxav_join_av_groupchat(tox, friend_number, invite_data, length, NULL, NULL); @@ -282,7 +283,7 @@ twc_group_invite_callback(Tox *tox, break; } - if (rc >= 0) + if (rc >= 0 && err == TOX_ERR_CONFERENCE_JOIN_OK) { tags = "notify_private"; if (friend_chat) @@ -353,10 +354,10 @@ twc_group_invite_callback(Tox *tox, { weechat_printf_date_tags(friend_chat->buffer, 0, tags, "%s%s%s%s invites you to join %s, but we failed to " - "process the invite. Please try again.", + "process the invite with error %d. Please try again.", weechat_prefix("network"), weechat_color("chat_nick_other"), friend_name, - weechat_color("reset"), rc); + weechat_color("reset"), rc, err); tags = ""; } weechat_printf_date_tags(profile->buffer, 0, tags, @@ -375,8 +376,10 @@ twc_handle_group_message(Tox *tox, int32_t group_number, int32_t peer_number, const uint8_t *message, uint16_t length, void *data, - enum TWC_MESSAGE_TYPE message_type) + TOX_MESSAGE_TYPE message_type) { + TOX_ERR_CONFERENCE_PEER_QUERY err = TOX_ERR_CONFERENCE_PEER_QUERY_OK; + bool rc; struct t_twc_profile *profile = data; struct t_twc_chat *chat = twc_chat_search_group(profile, @@ -389,7 +392,8 @@ twc_handle_group_message(Tox *tox, char *message_nt = twc_null_terminate(message, length); const char *nick_color; - if (tox_group_peernumber_is_ours(tox, group_number, peer_number)) + rc = tox_conference_peer_number_is_ours(tox, group_number, peer_number, &err); + if (rc && (err == TOX_ERR_CONFERENCE_PEER_QUERY_OK)) nick_color = weechat_color("chat_nick_self"); else nick_color = weechat_info_get("nick_color", name); @@ -406,9 +410,9 @@ twc_handle_group_message(Tox *tox, void twc_group_message_callback(Tox *tox, - int32_t group_number, int32_t peer_number, - const uint8_t *message, uint16_t length, - void *data) + uint32_t group_number, uint32_t peer_number, + TOX_MESSAGE_TYPE type, const uint8_t *message, + size_t length, void *data) { twc_handle_group_message(tox, group_number, @@ -416,28 +420,13 @@ twc_group_message_callback(Tox *tox, message, length, data, - TWC_MESSAGE_TYPE_MESSAGE); -} - -void -twc_group_action_callback(Tox *tox, - int32_t group_number, int32_t peer_number, - const uint8_t *message, uint16_t length, - void *data) -{ - twc_handle_group_message(tox, - group_number, - peer_number, - message, - length, - data, - TWC_MESSAGE_TYPE_ACTION); + type); } void twc_group_namelist_change_callback(Tox *tox, - int group_number, int peer_number, - uint8_t change_type, + uint32_t group_number, uint32_t peer_number, + TOX_CONFERENCE_STATE_CHANGE change_type, void *data) { struct t_twc_profile *profile = data; @@ -449,8 +438,8 @@ twc_group_namelist_change_callback(Tox *tox, char *name = twc_get_peer_name_nt(profile->tox, group_number, peer_number); char *prev_name = NULL; - if (change_type == TOX_CHAT_CHANGE_PEER_DEL - || change_type == TOX_CHAT_CHANGE_PEER_NAME) + if (change_type == TOX_CONFERENCE_STATE_CHANGE_PEER_EXIT + || change_type == TOX_CONFERENCE_STATE_CHANGE_PEER_NAME_CHANGE) { nick = weechat_hashtable_get(chat->nicks, &peer_number); if (nick) @@ -462,8 +451,8 @@ twc_group_namelist_change_callback(Tox *tox, } } - if (change_type == TOX_CHAT_CHANGE_PEER_ADD - || change_type == TOX_CHAT_CHANGE_PEER_NAME) + if (change_type == TOX_CONFERENCE_STATE_CHANGE_PEER_JOIN + || change_type == TOX_CONFERENCE_STATE_CHANGE_PEER_NAME_CHANGE) { nick = weechat_nicklist_add_nick(chat->buffer, chat->nicklist_group, name, NULL, NULL, NULL, 1); @@ -473,17 +462,17 @@ twc_group_namelist_change_callback(Tox *tox, switch (change_type) { - case TOX_CHAT_CHANGE_PEER_NAME: + case TOX_CONFERENCE_STATE_CHANGE_PEER_NAME_CHANGE: if (prev_name && name) weechat_printf(chat->buffer, "%s%s is now known as %s", weechat_prefix("network"), prev_name, name); break; - case TOX_CHAT_CHANGE_PEER_ADD: + case TOX_CONFERENCE_STATE_CHANGE_PEER_JOIN: if (name) weechat_printf(chat->buffer, "%s%s just joined the group chat", weechat_prefix("join"), name); break; - case TOX_CHAT_CHANGE_PEER_DEL: + case TOX_CONFERENCE_STATE_CHANGE_PEER_EXIT: if (prev_name) weechat_printf(chat->buffer, "%s%s just left the group chat", weechat_prefix("quit"), prev_name); @@ -496,8 +485,8 @@ twc_group_namelist_change_callback(Tox *tox, void twc_group_title_callback(Tox *tox, - int group_number, int peer_number, - const uint8_t *title, uint8_t length, + uint32_t group_number, uint32_t peer_number, + const uint8_t *title, size_t length, void *data) { struct t_twc_profile *profile = data; @@ -506,14 +495,11 @@ twc_group_title_callback(Tox *tox, true); twc_chat_queue_refresh(chat); - if (peer_number >= 0) - { - char *name = twc_get_peer_name_nt(profile->tox, group_number, peer_number); + char *name = twc_get_peer_name_nt(profile->tox, group_number, peer_number); - char *topic = strndup((char *)title, length); - weechat_printf(chat->buffer, "%s%s has changed the topic to \"%s\"", - weechat_prefix("network"), name, topic); - free(topic); - } + char *topic = strndup((char *)title, length); + weechat_printf(chat->buffer, "%s%s has changed the topic to \"%s\"", + weechat_prefix("network"), name, topic); + free(topic); } diff --git a/src/twc-tox-callbacks.h b/src/twc-tox-callbacks.h index 072a307..5ceacd2 100644 --- a/src/twc-tox-callbacks.h +++ b/src/twc-tox-callbacks.h @@ -28,9 +28,8 @@ twc_do_timer_cb(const void *pointer, void *data, void twc_friend_message_callback(Tox *tox, uint32_t friend_number, - TOX_MESSAGE_TYPE type, - const uint8_t *message, size_t length, - void *data); + TOX_MESSAGE_TYPE type, const uint8_t *message, + size_t length, void *data); void twc_connection_status_callback(Tox *tox, uint32_t friend_number, @@ -57,32 +56,26 @@ twc_friend_request_callback(Tox *tox, const uint8_t *public_key, void twc_group_invite_callback(Tox *tox, - int32_t friend_number, uint8_t type, - const uint8_t *invite_data, uint16_t length, + uint32_t friend_number, TOX_CONFERENCE_TYPE type, + const uint8_t *invite_data, size_t length, void *data); void twc_group_message_callback(Tox *tox, - int32_t group_number, int32_t peer_number, - const uint8_t *message, uint16_t length, - void *data); - -void -twc_group_action_callback(Tox *tox, - int32_t group_number, int32_t peer_number, - const uint8_t *message, uint16_t length, - void *data); + uint32_t group_number, uint32_t peer_number, + TOX_MESSAGE_TYPE type, const uint8_t *message, + size_t length, void *data); void twc_group_namelist_change_callback(Tox *tox, - int group_number, int peer_number, - uint8_t change_type, + uint32_t group_number, uint32_t peer_number, + TOX_CONFERENCE_STATE_CHANGE change_type, void *data); void twc_group_title_callback(Tox *tox, - int group_number, int peer_number, - const uint8_t *title, uint8_t length, + uint32_t group_number, uint32_t peer_number, + const uint8_t *title, size_t length, void *data); #endif // TOX_WEECHAT_TOX_CALLBACKS_H diff --git a/src/twc-utils.c b/src/twc-utils.c index e6da153..f3f951f 100644 --- a/src/twc-utils.c +++ b/src/twc-utils.c @@ -124,10 +124,17 @@ char * twc_get_peer_name_nt(Tox *tox, int32_t group_number, int32_t peer_number) { uint8_t name[TOX_MAX_NAME_LENGTH+1] = {0}; + TOX_ERR_CONFERENCE_PEER_QUERY err = TOX_ERR_CONFERENCE_PEER_QUERY_OK; - int length = tox_group_peername(tox, group_number, peer_number, name); - if (length >= 0) - return twc_null_terminate(name, length); + int length = tox_conference_peer_get_name_size(tox, group_number, peer_number, &err); + if ((err == TOX_ERR_CONFERENCE_PEER_QUERY_OK) && (length <= TOX_MAX_NAME_LENGTH)) + { + tox_conference_peer_get_name(tox, group_number, peer_number, name, &err); + if (err == TOX_ERR_CONFERENCE_PEER_QUERY_OK) + return twc_null_terminate(name, length); + else + return ""; + } else return ""; } From 4021d9da0e75a441d5475bc3ca93bdf2b7356926 Mon Sep 17 00:00:00 2001 From: Gordon Quad Date: Tue, 3 Jan 2017 12:41:35 +0000 Subject: [PATCH 18/18] group nicklist handling fixed --- src/twc-chat.c | 11 +++--- src/twc-chat.h | 2 +- src/twc-tox-callbacks.c | 80 +++++++++++++++++++++++------------------ 3 files changed, 52 insertions(+), 41 deletions(-) diff --git a/src/twc-chat.c b/src/twc-chat.c index de827d1..a99c0dd 100644 --- a/src/twc-chat.c +++ b/src/twc-chat.c @@ -133,11 +133,7 @@ twc_chat_new_group(struct t_twc_profile *profile, int32_t group_number) chat->nicklist_group = weechat_nicklist_add_group(chat->buffer, NULL, NULL, NULL, true); - chat->nicks = weechat_hashtable_new(32, - WEECHAT_HASHTABLE_INTEGER, - WEECHAT_HASHTABLE_POINTER, - NULL, - NULL); + chat->nicks = weechat_list_new(); weechat_buffer_set(chat->buffer, "nicklist", "1"); } @@ -401,7 +397,10 @@ twc_chat_free(struct t_twc_chat *chat) { weechat_nicklist_remove_all(chat->buffer); if (chat->nicks) - weechat_hashtable_free(chat->nicks); + { + weechat_list_remove_all(chat->nicks); + weechat_list_free(chat->nicks); + } free(chat); } diff --git a/src/twc-chat.h b/src/twc-chat.h index 37b2377..9b03811 100644 --- a/src/twc-chat.h +++ b/src/twc-chat.h @@ -38,7 +38,7 @@ struct t_twc_chat int32_t group_number; struct t_gui_nick_group *nicklist_group; - struct t_hashtable *nicks; + struct t_weelist *nicks; }; struct t_twc_chat * diff --git a/src/twc-tox-callbacks.c b/src/twc-tox-callbacks.c index 97194c2..bf54af1 100644 --- a/src/twc-tox-callbacks.c +++ b/src/twc-tox-callbacks.c @@ -435,52 +435,64 @@ twc_group_namelist_change_callback(Tox *tox, true); struct t_gui_nick *nick = NULL; - char *name = twc_get_peer_name_nt(profile->tox, group_number, peer_number); - char *prev_name = NULL; + int i, npeers; + TOX_ERR_CONFERENCE_PEER_QUERY err = TOX_ERR_CONFERENCE_PEER_QUERY_OK; - if (change_type == TOX_CONFERENCE_STATE_CHANGE_PEER_EXIT - || change_type == TOX_CONFERENCE_STATE_CHANGE_PEER_NAME_CHANGE) + struct t_weelist *new_nicks; + struct t_weelist_item *n; + + npeers = tox_conference_peer_count(profile->tox, group_number, &err); + + if (err == TOX_ERR_CONFERENCE_PEER_QUERY_OK) { - nick = weechat_hashtable_get(chat->nicks, &peer_number); - if (nick) + new_nicks = weechat_list_new(); + for (i = 0; i < npeers; i++) { - prev_name = strdup(weechat_nicklist_nick_get_string(chat->buffer, - nick, "name")); - weechat_nicklist_remove_nick(chat->buffer, nick); - weechat_hashtable_remove(chat->nicks, &peer_number); + char *name = twc_get_peer_name_nt(profile->tox, group_number, i); + weechat_list_add(new_nicks, name, WEECHAT_LIST_POS_SORT, NULL); + free(name); } } + else + return; - if (change_type == TOX_CONFERENCE_STATE_CHANGE_PEER_JOIN - || change_type == TOX_CONFERENCE_STATE_CHANGE_PEER_NAME_CHANGE) + // searching for exits + n = weechat_list_get(chat->nicks, 0); + + while (n) { - nick = weechat_nicklist_add_nick(chat->buffer, chat->nicklist_group, - name, NULL, NULL, NULL, 1); - if (nick) - weechat_hashtable_set(chat->nicks, &peer_number, nick); + const char *name = weechat_list_string(n); + if (!weechat_list_search(new_nicks, name)) + { + weechat_printf(chat->buffer, "%s%s just left the group chat", + weechat_prefix("quit"), name); + nick = weechat_nicklist_search_nick(chat->buffer, + chat->nicklist_group, + name); + weechat_nicklist_remove_nick(chat->buffer, nick); + } + n = weechat_list_next(n); } - switch (change_type) + // searching for joins + n = weechat_list_get(new_nicks, 0); + + while (n) { - case TOX_CONFERENCE_STATE_CHANGE_PEER_NAME_CHANGE: - if (prev_name && name) - weechat_printf(chat->buffer, "%s%s is now known as %s", - weechat_prefix("network"), prev_name, name); - break; - case TOX_CONFERENCE_STATE_CHANGE_PEER_JOIN: - if (name) - weechat_printf(chat->buffer, "%s%s just joined the group chat", - weechat_prefix("join"), name); - break; - case TOX_CONFERENCE_STATE_CHANGE_PEER_EXIT: - if (prev_name) - weechat_printf(chat->buffer, "%s%s just left the group chat", - weechat_prefix("quit"), prev_name); - break; + const char *name = weechat_list_string(n); + if (!weechat_list_search(chat->nicks, name)) + { + weechat_printf(chat->buffer, "%s%s just joined the group chat", + weechat_prefix("join"), name); + weechat_nicklist_add_nick(chat->buffer, chat->nicklist_group, + name, NULL, NULL, NULL, 1); + } + n = weechat_list_next(n); } - if (prev_name) - free(prev_name); + weechat_list_remove_all(chat->nicks); + weechat_list_free(chat->nicks); + chat->nicks = new_nicks; } void