summaryrefslogtreecommitdiff
path: root/toxcore/group_chats.c
diff options
context:
space:
mode:
Diffstat (limited to 'toxcore/group_chats.c')
-rw-r--r--toxcore/group_chats.c57
1 files changed, 52 insertions, 5 deletions
diff --git a/toxcore/group_chats.c b/toxcore/group_chats.c
index 02c1d534..0903ecb8 100644
--- a/toxcore/group_chats.c
+++ b/toxcore/group_chats.c
@@ -53,6 +53,11 @@ typedef struct {
53 53
54} sendnodes_data; 54} sendnodes_data;
55 55
56typedef struct {
57 uint8_t client_id[crypto_box_PUBLICKEYBYTES];
58 uint8_t nickname[MAX_NICK_BYTES];
59} peernick_data;
60
56 61
57/* 62/*
58 * check if peer with client_id is in peer array. 63 * check if peer with client_id is in peer array.
@@ -211,10 +216,12 @@ static int addpeer(Group_Chat *chat, uint8_t *client_id)
211 216
212 memset(&(temp[chat->numpeers]), 0, sizeof(Group_Peer)); 217 memset(&(temp[chat->numpeers]), 0, sizeof(Group_Peer));
213 chat->group = temp; 218 chat->group = temp;
219
214 id_copy(chat->group[chat->numpeers].client_id, client_id); 220 id_copy(chat->group[chat->numpeers].client_id, client_id);
215 chat->group[chat->numpeers].last_recv = unix_time(); 221 chat->group[chat->numpeers].last_recv = unix_time();
216 chat->group[chat->numpeers].last_recv_msgping = unix_time(); 222 chat->group[chat->numpeers].last_recv_msgping = unix_time();
217 ++chat->numpeers; 223 ++chat->numpeers;
224
218 return (chat->numpeers - 1); 225 return (chat->numpeers - 1);
219} 226}
220 227
@@ -267,14 +274,34 @@ int group_peername(Group_Chat *chat, int peernum, uint8_t *name)
267 return -1; 274 return -1;
268 275
269 if (chat->group[peernum].nick_len == 0) { 276 if (chat->group[peernum].nick_len == 0) {
270 memcpy(name, "NSA Agent", 10); /* Kindly remind the user that someone with no name might be an NSA agent.*/ 277 /* memcpy(name, "NSA agent", 10); */ /* Srsly? */ /* Kindly remind the user that someone with no name might be a moronic NSA agent.*/
271 return 10; 278 name[0] = 0;
279 return 0;
272 } 280 }
273 281
274 memcpy(name, chat->group[peernum].nick, chat->group[peernum].nick_len); 282 memcpy(name, chat->group[peernum].nick, chat->group[peernum].nick_len);
275 return chat->group[peernum].nick_len; 283 return chat->group[peernum].nick_len;
276} 284}
277 285
286static void setnick(Group_Chat *chat, uint8_t *contents, uint16_t contents_len)
287{
288 if (contents_len > CLIENT_ID_SIZE + MAX_NICK_BYTES)
289 return;
290
291 peernick_data *data = (peernick_data *)contents;
292
293 size_t i;
294
295 for (i = 0; i < chat->numpeers; i++)
296 if (id_equal(chat->group[i].client_id, data->client_id)) {
297 uint16_t nick_len = contents_len - CLIENT_ID_SIZE;
298
299 memcpy(chat->group[i].nick, data->nickname, nick_len);
300 chat->group[i].nick_len = nick_len;
301
302 return;
303 }
304}
278 305
279/* min time between pings sent to one peer in seconds */ 306/* min time between pings sent to one peer in seconds */
280/* TODO: move this to global section */ 307/* TODO: move this to global section */
@@ -294,7 +321,8 @@ static int send_getnodes(Group_Chat *chat, IP_Port ip_port, int peernum)
294 chat->group[peernum].last_pinged = unix_time(); 321 chat->group[peernum].last_pinged = unix_time();
295 chat->group[peernum].pingid = contents.pingid; 322 chat->group[peernum].pingid = contents.pingid;
296 323
297 return send_groupchatpacket(chat, ip_port, chat->group[peernum].client_id, (uint8_t *)&contents, sizeof(contents), CRYPTO_PACKET_GROUP_CHAT_GET_NODES); 324 return send_groupchatpacket(chat, ip_port, chat->group[peernum].client_id, (uint8_t *)&contents, sizeof(contents),
325 CRYPTO_PACKET_GROUP_CHAT_GET_NODES);
298} 326}
299 327
300static int send_sendnodes(Group_Chat *chat, IP_Port ip_port, int peernum, uint64_t pingid) 328static int send_sendnodes(Group_Chat *chat, IP_Port ip_port, int peernum, uint64_t pingid)
@@ -433,6 +461,13 @@ static int handle_data(Group_Chat *chat, uint8_t *data, uint32_t len)
433 addpeer(chat, contents); 461 addpeer(chat, contents);
434 break; 462 break;
435 463
464 case GROUP_CHAT_PEER_NICK:
465 if (contents_len < crypto_box_PUBLICKEYBYTES)
466 return 1;
467
468 setnick(chat, contents, contents_len);
469 break;
470
436 case GROUP_CHAT_CHAT_MESSAGE: /* If message is chat message */ 471 case GROUP_CHAT_CHAT_MESSAGE: /* If message is chat message */
437 if (chat->group_message != NULL) 472 if (chat->group_message != NULL)
438 (*chat->group_message)(chat, peernum, contents, contents_len, chat->group_message_userdata); 473 (*chat->group_message)(chat, peernum, contents, contents_len, chat->group_message_userdata);
@@ -525,6 +560,18 @@ uint32_t group_sendmessage(Group_Chat *chat, uint8_t *message, uint32_t length)
525 return send_data(chat, message, length, GROUP_CHAT_CHAT_MESSAGE); //TODO: better return values? 560 return send_data(chat, message, length, GROUP_CHAT_CHAT_MESSAGE); //TODO: better return values?
526} 561}
527 562
563uint32_t group_send_nick(Group_Chat *chat, uint8_t *client_id, uint8_t *nick, uint16_t nick_len)
564{
565 peernick_data peernick;
566 id_copy(peernick.client_id, client_id);
567 memcpy(peernick.nickname, nick, nick_len);
568
569 /* also set for self */
570 setnick(chat, (uint8_t *)&peernick, sizeof(peernick.client_id) + nick_len);
571
572 return send_data(chat, (uint8_t *)&peernick, sizeof(peernick.client_id) + nick_len, GROUP_CHAT_PEER_NICK);
573}
574
528uint32_t group_newpeer(Group_Chat *chat, uint8_t *client_id) 575uint32_t group_newpeer(Group_Chat *chat, uint8_t *client_id)
529{ 576{
530 addpeer(chat, client_id); 577 addpeer(chat, client_id);
@@ -558,7 +605,6 @@ static void ping_close(Group_Chat *chat)
558 uint32_t i; 605 uint32_t i;
559 606
560 for (i = 0; i < GROUP_CLOSE_CONNECTIONS; ++i) { 607 for (i = 0; i < GROUP_CLOSE_CONNECTIONS; ++i) {
561 /* previous condition was always true, assuming this is the wanted one: */
562 if (!is_timeout(chat->close[i].last_recv, BAD_GROUPNODE_TIMEOUT)) { 608 if (!is_timeout(chat->close[i].last_recv, BAD_GROUPNODE_TIMEOUT)) {
563 int peernum = peer_in_chat(chat, chat->close[i].client_id); 609 int peernum = peer_in_chat(chat, chat->close[i].client_id);
564 610
@@ -585,8 +631,9 @@ static void ping_group(Group_Chat *chat)
585static void del_dead_peers(Group_Chat *chat) 631static void del_dead_peers(Group_Chat *chat)
586{ 632{
587 uint32_t i; 633 uint32_t i;
634
588 for (i = 0; i < chat->numpeers; ++i) { 635 for (i = 0; i < chat->numpeers; ++i) {
589 if (is_timeout(chat->group[i].last_recv_msgping, GROUP_PING_INTERVAL*2)) { 636 if (is_timeout(chat->group[i].last_recv_msgping, GROUP_PING_INTERVAL * 2)) {
590 delpeer(chat, chat->group[i].client_id); 637 delpeer(chat, chat->group[i].client_id);
591 } 638 }
592 } 639 }