diff options
-rw-r--r-- | testing/nTox.c | 12 | ||||
-rw-r--r-- | toxcore/Messenger.c | 14 | ||||
-rw-r--r-- | toxcore/group_chats.c | 57 | ||||
-rw-r--r-- | toxcore/group_chats.h | 9 |
4 files changed, 85 insertions, 7 deletions
diff --git a/testing/nTox.c b/testing/nTox.c index e1e25b45..a227ed33 100644 --- a/testing/nTox.c +++ b/testing/nTox.c | |||
@@ -838,8 +838,16 @@ void print_groupmessage(Tox *m, int groupnumber, int peernumber, uint8_t *messag | |||
838 | { | 838 | { |
839 | char msg[256 + length]; | 839 | char msg[256 + length]; |
840 | uint8_t name[TOX_MAX_NAME_LENGTH]; | 840 | uint8_t name[TOX_MAX_NAME_LENGTH]; |
841 | tox_group_peername(m, groupnumber, peernumber, name); | 841 | int len = tox_group_peername(m, groupnumber, peernumber, name); |
842 | sprintf(msg, "[g] %u: <%s>: %s", groupnumber, name, message); | 842 | |
843 | if (len <= 0) | ||
844 | name[0] = 0; | ||
845 | |||
846 | if (name[0] != 0) | ||
847 | sprintf(msg, "[g] %u: %u <%s>: %s", groupnumber, peernumber, name, message); | ||
848 | else | ||
849 | sprintf(msg, "[g] %u: %u Unknown: %s", groupnumber, peernumber, message); | ||
850 | |||
843 | new_lines(msg); | 851 | new_lines(msg); |
844 | } | 852 | } |
845 | 853 | ||
diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c index 66faa4db..6ec54587 100644 --- a/toxcore/Messenger.c +++ b/toxcore/Messenger.c | |||
@@ -435,6 +435,9 @@ int setname(Messenger *m, uint8_t *name, uint16_t length) | |||
435 | for (i = 0; i < m->numfriends; ++i) | 435 | for (i = 0; i < m->numfriends; ++i) |
436 | m->friendlist[i].name_sent = 0; | 436 | m->friendlist[i].name_sent = 0; |
437 | 437 | ||
438 | for (i = 0; i < m->numchats; i++) | ||
439 | m->chats[i]->last_sent_nick = 0; /* or send the new name right away? */ | ||
440 | |||
438 | return 0; | 441 | return 0; |
439 | } | 442 | } |
440 | 443 | ||
@@ -969,6 +972,17 @@ int group_message_send(Messenger *m, int groupnumber, uint8_t *message, uint32_t | |||
969 | if (m->chats[groupnumber] == NULL) | 972 | if (m->chats[groupnumber] == NULL) |
970 | return -1; | 973 | return -1; |
971 | 974 | ||
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 | |||
972 | if (group_sendmessage(m->chats[groupnumber], message, length) > 0) | 986 | if (group_sendmessage(m->chats[groupnumber], message, length) > 0) |
973 | return 0; | 987 | return 0; |
974 | 988 | ||
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 | ||
56 | typedef 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 | ||
286 | static 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 | ||
300 | static int send_sendnodes(Group_Chat *chat, IP_Port ip_port, int peernum, uint64_t pingid) | 328 | static 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 | ||
563 | uint32_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 | |||
528 | uint32_t group_newpeer(Group_Chat *chat, uint8_t *client_id) | 575 | uint32_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) | |||
585 | static void del_dead_peers(Group_Chat *chat) | 631 | static 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 | } |
diff --git a/toxcore/group_chats.h b/toxcore/group_chats.h index b3f2e5a8..66fb641f 100644 --- a/toxcore/group_chats.h +++ b/toxcore/group_chats.h | |||
@@ -63,12 +63,15 @@ typedef struct Group_Chat { | |||
63 | uint32_t message_number; | 63 | uint32_t message_number; |
64 | void (*group_message)(struct Group_Chat *m, int, uint8_t *, uint16_t, void *); | 64 | void (*group_message)(struct Group_Chat *m, int, uint8_t *, uint16_t, void *); |
65 | void *group_message_userdata; | 65 | void *group_message_userdata; |
66 | |||
66 | uint64_t last_sent_ping; | 67 | uint64_t last_sent_ping; |
67 | 68 | ||
69 | uint64_t last_sent_nick; | ||
68 | } Group_Chat; | 70 | } Group_Chat; |
69 | 71 | ||
70 | #define GROUP_CHAT_PING 0 | 72 | #define GROUP_CHAT_PING 0 |
71 | #define GROUP_CHAT_NEW_PEER 16 | 73 | #define GROUP_CHAT_NEW_PEER 16 |
74 | #define GROUP_CHAT_PEER_NICK 17 | ||
72 | #define GROUP_CHAT_CHAT_MESSAGE 64 | 75 | #define GROUP_CHAT_CHAT_MESSAGE 64 |
73 | 76 | ||
74 | /* Copy the name of peernum to name. | 77 | /* Copy the name of peernum to name. |
@@ -95,6 +98,12 @@ void callback_groupmessage(Group_Chat *chat, void (*function)(Group_Chat *chat, | |||
95 | */ | 98 | */ |
96 | uint32_t group_sendmessage(Group_Chat *chat, uint8_t *message, uint32_t length); | 99 | uint32_t group_sendmessage(Group_Chat *chat, uint8_t *message, uint32_t length); |
97 | 100 | ||
101 | /* | ||
102 | * Send id/nick combo to the group. | ||
103 | * | ||
104 | * returns the number of peers it has sent it to. | ||
105 | */ | ||
106 | uint32_t group_send_nick(Group_Chat *chat, uint8_t *client_id, uint8_t *nick, uint16_t nick_len); | ||
98 | 107 | ||
99 | /* | 108 | /* |
100 | * Tell everyone about a new peer (a person we are inviting for example.) | 109 | * Tell everyone about a new peer (a person we are inviting for example.) |