summaryrefslogtreecommitdiff
path: root/toxcore
diff options
context:
space:
mode:
Diffstat (limited to 'toxcore')
-rw-r--r--toxcore/Messenger.c18
-rw-r--r--toxcore/group_chats.c75
-rw-r--r--toxcore/group_chats.h12
3 files changed, 58 insertions, 47 deletions
diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c
index 6ec54587..57cafc3f 100644
--- a/toxcore/Messenger.c
+++ b/toxcore/Messenger.c
@@ -436,7 +436,8 @@ int setname(Messenger *m, uint8_t *name, uint16_t length)
436 m->friendlist[i].name_sent = 0; 436 m->friendlist[i].name_sent = 0;
437 437
438 for (i = 0; i < m->numchats; i++) 438 for (i = 0; i < m->numchats; i++)
439 m->chats[i]->last_sent_nick = 0; /* or send the new name right away? */ 439 if (m->chats[i] != NULL)
440 set_nick(m->chats[i], name, length); /* TODO: remove this (group nicks should not be tied to the global one) */
440 441
441 return 0; 442 return 0;
442} 443}
@@ -789,6 +790,8 @@ int add_groupchat(Messenger *m)
789 return -1; 790 return -1;
790 791
791 callback_groupmessage(newchat, &group_message_function, m); 792 callback_groupmessage(newchat, &group_message_function, m);
793 /* TODO: remove this (group nicks should not be tied to the global one) */
794 set_nick(newchat, m->name, m->name_length);
792 m->chats[i] = newchat; 795 m->chats[i] = newchat;
793 return i; 796 return i;
794 } 797 }
@@ -807,6 +810,8 @@ int add_groupchat(Messenger *m)
807 810
808 m->chats = temp; 811 m->chats = temp;
809 callback_groupmessage(temp[m->numchats], &group_message_function, m); 812 callback_groupmessage(temp[m->numchats], &group_message_function, m);
813 /* TODO: remove this (group nicks should not be tied to the global one) */
814 set_nick(temp[m->numchats], m->name, m->name_length);
810 ++m->numchats; 815 ++m->numchats;
811 return (m->numchats - 1); 816 return (m->numchats - 1);
812} 817}
@@ -972,17 +977,6 @@ int group_message_send(Messenger *m, int groupnumber, uint8_t *message, uint32_t
972 if (m->chats[groupnumber] == NULL) 977 if (m->chats[groupnumber] == NULL)
973 return -1; 978 return -1;
974 979
975 /* send own nick from time to time, to let newly added peers be informed
976 * first time only: use a shorter timeframe, because we might not be in our own
977 * peer list yet */
978 if (is_timeout(m->chats[groupnumber]->last_sent_nick, 180))
979 if (group_send_nick(m->chats[groupnumber], m->chats[groupnumber]->self_public_key, m->name, m->name_length) > 0) {
980 if (!m->chats[groupnumber]->last_sent_nick)
981 m->chats[groupnumber]->last_sent_nick = unix_time() - 150;
982 else
983 m->chats[groupnumber]->last_sent_nick = unix_time();
984 }
985
986 if (group_sendmessage(m->chats[groupnumber], message, length) > 0) 980 if (group_sendmessage(m->chats[groupnumber], message, length) > 0)
987 return 0; 981 return 0;
988 982
diff --git a/toxcore/group_chats.c b/toxcore/group_chats.c
index 0903ecb8..5376713c 100644
--- a/toxcore/group_chats.c
+++ b/toxcore/group_chats.c
@@ -53,12 +53,6 @@ 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
61
62/* 56/*
63 * check if peer with client_id is in peer array. 57 * check if peer with client_id is in peer array.
64 * 58 *
@@ -246,6 +240,7 @@ static int delpeer(Group_Chat *chat, uint8_t *client_id)
246 chat->group = NULL; 240 chat->group = NULL;
247 return 0; 241 return 0;
248 } 242 }
243
249 if (chat->numpeers != i) 244 if (chat->numpeers != i)
250 memcpy(&chat->group[i], &chat->group[chat->numpeers], sizeof(Group_Peer)); 245 memcpy(&chat->group[i], &chat->group[chat->numpeers], sizeof(Group_Peer));
251 246
@@ -283,24 +278,15 @@ int group_peername(Group_Chat *chat, int peernum, uint8_t *name)
283 return chat->group[peernum].nick_len; 278 return chat->group[peernum].nick_len;
284} 279}
285 280
286static void setnick(Group_Chat *chat, uint8_t *contents, uint16_t contents_len) 281static void setnick(Group_Chat *chat, int peernum, uint8_t *contents, uint16_t contents_len)
287{ 282{
288 if (contents_len > CLIENT_ID_SIZE + MAX_NICK_BYTES) 283 if (contents_len > MAX_NICK_BYTES || contents_len == 0)
289 return; 284 return;
290 285
291 peernick_data *data = (peernick_data *)contents; 286 memcpy(chat->group[peernum].nick, contents, contents_len);
292 287 /* Force null termination */
293 size_t i; 288 chat->group[peernum].nick[contents_len - 1] = 0;
294 289 chat->group[peernum].nick_len = contents_len;
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} 290}
305 291
306/* min time between pings sent to one peer in seconds */ 292/* min time between pings sent to one peer in seconds */
@@ -462,10 +448,10 @@ static int handle_data(Group_Chat *chat, uint8_t *data, uint32_t len)
462 break; 448 break;
463 449
464 case GROUP_CHAT_PEER_NICK: 450 case GROUP_CHAT_PEER_NICK:
465 if (contents_len < crypto_box_PUBLICKEYBYTES) 451 if (contents_len > MAX_NICK_BYTES || contents_len == 0)
466 return 1; 452 return 1;
467 453
468 setnick(chat, contents, contents_len); 454 setnick(chat, peernum, contents, contents_len);
469 break; 455 break;
470 456
471 case GROUP_CHAT_CHAT_MESSAGE: /* If message is chat message */ 457 case GROUP_CHAT_CHAT_MESSAGE: /* If message is chat message */
@@ -560,16 +546,28 @@ uint32_t group_sendmessage(Group_Chat *chat, uint8_t *message, uint32_t length)
560 return send_data(chat, message, length, GROUP_CHAT_CHAT_MESSAGE); //TODO: better return values? 546 return send_data(chat, message, length, GROUP_CHAT_CHAT_MESSAGE); //TODO: better return values?
561} 547}
562 548
563uint32_t group_send_nick(Group_Chat *chat, uint8_t *client_id, uint8_t *nick, uint16_t nick_len) 549/*
550 * Send id/nick combo to the group.
551 *
552 * returns the number of peers it has sent it to.
553 */
554static uint32_t group_send_nick(Group_Chat *chat, uint8_t *nick, uint16_t nick_len)
564{ 555{
565 peernick_data peernick; 556 if (nick_len > MAX_NICK_BYTES)
566 id_copy(peernick.client_id, client_id); 557 return 0;
567 memcpy(peernick.nickname, nick, nick_len); 558
559 return send_data(chat, nick, nick_len, GROUP_CHAT_PEER_NICK);
560}
568 561
569 /* also set for self */ 562int set_nick(Group_Chat *chat, uint8_t *nick, uint16_t nick_len)
570 setnick(chat, (uint8_t *)&peernick, sizeof(peernick.client_id) + nick_len); 563{
564 if (nick_len > MAX_NICK_BYTES || nick_len == 0)
565 return -1;
571 566
572 return send_data(chat, (uint8_t *)&peernick, sizeof(peernick.client_id) + nick_len, GROUP_CHAT_PEER_NICK); 567 memcpy(chat->nick, nick, nick_len);
568 chat->nick_len = nick_len;
569 group_send_nick(chat, chat->nick, chat->nick_len);
570 return 0;
573} 571}
574 572
575uint32_t group_newpeer(Group_Chat *chat, uint8_t *client_id) 573uint32_t group_newpeer(Group_Chat *chat, uint8_t *client_id)
@@ -639,6 +637,22 @@ static void del_dead_peers(Group_Chat *chat)
639 } 637 }
640} 638}
641 639
640#define NICK_SEND_INTERVAL 180
641
642static void send_names(Group_Chat *chat)
643{
644 /* send own nick from time to time, to let newly added peers be informed
645 * first time only: use a shorter timeframe, because we might not be in our own
646 * peer list yet */
647 if (is_timeout(chat->last_sent_nick, 180))
648 if (group_send_nick(chat, chat->nick, chat->nick_len) > 0) {
649 if (!chat->last_sent_nick)
650 chat->last_sent_nick = (unix_time() - NICK_SEND_INTERVAL) + 30;
651 else
652 chat->last_sent_nick = unix_time();
653 }
654}
655
642void do_groupchat(Group_Chat *chat) 656void do_groupchat(Group_Chat *chat)
643{ 657{
644 unix_time_update(); 658 unix_time_update();
@@ -646,6 +660,7 @@ void do_groupchat(Group_Chat *chat)
646 ping_group(chat); 660 ping_group(chat);
647 /* TODO: Maybe run this less? */ 661 /* TODO: Maybe run this less? */
648 del_dead_peers(chat); 662 del_dead_peers(chat);
663 send_names(chat);
649} 664}
650 665
651void kill_groupchat(Group_Chat *chat) 666void kill_groupchat(Group_Chat *chat)
diff --git a/toxcore/group_chats.h b/toxcore/group_chats.h
index 66fb641f..74e2f2d7 100644
--- a/toxcore/group_chats.h
+++ b/toxcore/group_chats.h
@@ -66,12 +66,14 @@ typedef struct Group_Chat {
66 66
67 uint64_t last_sent_ping; 67 uint64_t last_sent_ping;
68 68
69 uint8_t nick[MAX_NICK_BYTES];
70 uint16_t nick_len;
69 uint64_t last_sent_nick; 71 uint64_t last_sent_nick;
70} Group_Chat; 72} Group_Chat;
71 73
72#define GROUP_CHAT_PING 0 74#define GROUP_CHAT_PING 0
73#define GROUP_CHAT_NEW_PEER 16 75#define GROUP_CHAT_NEW_PEER 16
74#define GROUP_CHAT_PEER_NICK 17 76#define GROUP_CHAT_PEER_NICK 48
75#define GROUP_CHAT_CHAT_MESSAGE 64 77#define GROUP_CHAT_CHAT_MESSAGE 64
76 78
77/* Copy the name of peernum to name. 79/* Copy the name of peernum to name.
@@ -99,11 +101,11 @@ void callback_groupmessage(Group_Chat *chat, void (*function)(Group_Chat *chat,
99uint32_t group_sendmessage(Group_Chat *chat, uint8_t *message, uint32_t length); 101uint32_t group_sendmessage(Group_Chat *chat, uint8_t *message, uint32_t length);
100 102
101/* 103/*
102 * Send id/nick combo to the group. 104 * Set our nick for this group.
103 * 105 *
104 * returns the number of peers it has sent it to. 106 * returns -1 on failure, 0 on success.
105 */ 107 */
106uint32_t group_send_nick(Group_Chat *chat, uint8_t *client_id, uint8_t *nick, uint16_t nick_len); 108int set_nick(Group_Chat *chat, uint8_t *nick, uint16_t nick_len);
107 109
108/* 110/*
109 * Tell everyone about a new peer (a person we are inviting for example.) 111 * Tell everyone about a new peer (a person we are inviting for example.)