From 2977261ef17e7ae7d5f97d4057852665fb89eaf3 Mon Sep 17 00:00:00 2001 From: irungentoo Date: Fri, 3 Oct 2014 12:25:29 -0400 Subject: [PATCH] Added name packet to group chats, send one when appropriate. --- toxcore/group.c | 76 ++++++++++++++++++++++++++++++++++++++++++++++++- toxcore/group.h | 4 +++ toxcore/tox.c | 8 +++++- 3 files changed, 86 insertions(+), 2 deletions(-) diff --git a/toxcore/group.c b/toxcore/group.c index 96f5ba2f..fcdba603 100644 --- a/toxcore/group.c +++ b/toxcore/group.c @@ -454,6 +454,30 @@ static int delpeer(Group_Chats *g_c, int groupnumber, int peer_index) return 0; } +static int setnick(Group_Chats *g_c, int groupnumber, int peer_index, const uint8_t *nick, uint16_t nick_len) +{ + if (nick_len > MAX_NAME_LENGTH || nick_len == 0) + return -1; + + Group_c *g = get_group_c(g_c, groupnumber); + + if (!g) + return -1; + + /* same name as already stored? */ + if (g->group[peer_index].nick_len == nick_len) + if (!memcmp(g->group[peer_index].nick, nick, nick_len)) + return 0; + + memcpy(g->group[peer_index].nick, nick, nick_len); + g->group[peer_index].nick_len = nick_len; + + if (g_c->peer_namelistchange) + g_c->peer_namelistchange(g_c->m, groupnumber, peer_index, CHAT_CHANGE_PEER_NAME, g_c->group_namelistchange_userdata); + + return 0; +} + static int remove_close_conn(Group_Chats *g_c, int groupnumber, int friendcon_id) { Group_c *g = get_group_c(g_c, groupnumber); @@ -583,7 +607,14 @@ int add_groupchat(Group_Chats *g_c) new_symmetric_key(g->identifier); g->peer_number = 0; /* Founder is peer 0. */ memcpy(g->real_pk, g_c->m->net_crypto->self_public_key, crypto_box_PUBLICKEYBYTES); - addpeer(g_c, groupnumber, g->real_pk, g_c->m->dht->self_public_key, 0); + int peer_index = addpeer(g_c, groupnumber, g->real_pk, g_c->m->dht->self_public_key, 0); + + if (peer_index == -1) { + return -1; + } + + setnick(g_c, groupnumber, peer_index, g_c->m->name, g_c->m->name_length); + return groupnumber; } @@ -852,6 +883,24 @@ int group_kill_peer_send(const Group_Chats *g_c, int groupnumber, uint16_t peer_ } } +#define GROUP_MESSAGE_NAME_ID 48 + +/* send a name message + * return 0 on success + * return -1 on failure + */ +static int group_name_send(const Group_Chats *g_c, int groupnumber, const uint8_t *nick, uint16_t nick_len) +{ + if (nick_len > MAX_NAME_LENGTH) + return -1; + + if (send_message_group(g_c, groupnumber, GROUP_MESSAGE_NAME_ID, nick, nick_len)) { + return 0; + } else { + return -1; + } +} + static void handle_friend_invite_packet(Messenger *m, int32_t friendnumber, const uint8_t *data, uint16_t length) { Group_Chats *g_c = m->group_chat_object; @@ -1066,6 +1115,7 @@ static int handle_send_peers(Group_Chats *g_c, int groupnumber, const uint8_t *d && memcmp(d, g_c->m->net_crypto->self_public_key, crypto_box_PUBLICKEYBYTES) == 0) { g->peer_number = peer_num; g->status = GROUPCHAT_STATUS_CONNECTED; + group_name_send(g_c, groupnumber, g_c->m->name, g_c->m->name_length); } d += crypto_box_PUBLICKEYBYTES * 2; @@ -1264,6 +1314,12 @@ static void handle_message_packet_group(Group_Chats *g_c, int groupnumber, const } break; + case GROUP_MESSAGE_NAME_ID: { + if (setnick(g_c, groupnumber, index, msg_data, msg_data_len) == -1) + return; + } + break; + case PACKET_ID_MESSAGE: { if (msg_data_len == 0) return; @@ -1367,6 +1423,24 @@ static int groupchat_clear_timedout(Group_Chats *g_c, int groupnumber) return 0; } +/* Send current name (set in messenger) to all online groups. + */ +void send_name_all_groups(Group_Chats *g_c) +{ + unsigned int i; + + for (i = 0; i < g_c->num_chats; ++i) { + Group_c *g = get_group_c(g_c, i); + + if (!g) + continue; + + if (g->status == GROUPCHAT_STATUS_CONNECTED) { + group_name_send(g_c, i, g_c->m->name, g_c->m->name_length); + } + } +} + /* Create new groupchat instance. */ Group_Chats *new_groupchats(Messenger *m) { diff --git a/toxcore/group.h b/toxcore/group.h index d3a330b2..c1262664 100644 --- a/toxcore/group.h +++ b/toxcore/group.h @@ -208,6 +208,10 @@ int group_number_peers(const Group_Chats *g_c, int groupnumber); int group_names(const Group_Chats *g_c, int groupnumber, uint8_t names[][MAX_NAME_LENGTH], uint16_t lengths[], uint16_t length); +/* Send current name (set in messenger) to all online groups. + */ +void send_name_all_groups(Group_Chats *g_c); + /* Create new groupchat instance. */ Group_Chats *new_groupchats(Messenger *m); diff --git a/toxcore/tox.c b/toxcore/tox.c index 999d81d6..f01d7cf4 100644 --- a/toxcore/tox.c +++ b/toxcore/tox.c @@ -168,7 +168,13 @@ uint32_t tox_send_action(Tox *tox, int32_t friendnumber, const uint8_t *action, int tox_set_name(Tox *tox, const uint8_t *name, uint16_t length) { Messenger *m = tox; - return setname(m, name, length); + + if (setname(m, name, length) == 0) { + send_name_all_groups(m->group_chat_object); + return 0; + } else { + return -1; + } } /* Get your nickname.