summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--testing/nTox.c45
-rw-r--r--testing/nTox.h2
-rw-r--r--toxcore/Messenger.c121
-rw-r--r--toxcore/Messenger.h56
-rw-r--r--toxcore/group_chats.h1
5 files changed, 217 insertions, 8 deletions
diff --git a/testing/nTox.c b/testing/nTox.c
index 438468bd..d6e25ac0 100644
--- a/testing/nTox.c
+++ b/testing/nTox.c
@@ -348,13 +348,34 @@ void line_eval(Tox *m, char *line)
348 do_refresh(); 348 do_refresh();
349 } else if (inpt_command == 'h') { //help 349 } else if (inpt_command == 'h') { //help
350 new_lines(help); 350 new_lines(help);
351 } else if (inpt_command == 'i') { //info 351 } else if (inpt_command == 'x') { //info
352 char idstring[200]; 352 char idstring[200];
353 get_id(m, idstring); 353 get_id(m, idstring);
354 new_lines(idstring); 354 new_lines(idstring);
355 } 355 } else if (inpt_command == 'g') { //create new group chat
356 char msg[256];
357 sprintf(msg, "[g] Created new group chat with number: %u", add_groupchat(m));
358 new_lines(msg);
359 } else if (inpt_command == 'i') { //invite friendnum to groupnum
360 char *posi[1];
361 int friendnumber = strtoul(line + prompt_offset, posi, 0);
362 int groupnumber = strtoul(*posi + 1, NULL, 0);
363 char msg[256];
364 sprintf(msg, "[g] Invited friend number %u to group number %u, returned: %u (0 means success)", friendnumber,
365 groupnumber, invite_friend(m, friendnumber, groupnumber));
366 new_lines(msg);
367 } else if (inpt_command == 'z') { //send message to groupnum
368 char *posi[1];
369 int groupnumber = strtoul(line + prompt_offset, posi, 0);
370
371 if (**posi != 0) {
372 char msg[256 + 1024];
373 sprintf(msg, "[g] sent message: %s to group num: %u returned: %u (0 means success)", *posi + 1, groupnumber,
374 group_message_send(m, groupnumber, (uint8_t *)*posi + 1, strlen(*posi + 1) + 1));
375 new_lines(msg);
376 }
356 377
357 else if (inpt_command == 'q') { //exit 378 } else if (inpt_command == 'q') { //exit
358 endwin(); 379 endwin();
359 exit(EXIT_SUCCESS); 380 exit(EXIT_SUCCESS);
360 } else { 381 } else {
@@ -535,6 +556,22 @@ void print_help(void)
535 puts("\t-f\t-\tSpecify a keyfile to read (or write to) from."); 556 puts("\t-f\t-\tSpecify a keyfile to read (or write to) from.");
536} 557}
537 558
559void print_invite(Messenger *m, int friendnumber, uint8_t *group_public_key, void *userdata)
560{
561 char msg[256];
562 sprintf(msg, "[i] recieved group chat invite from: %u, auto accepting and joining. group number: %u", friendnumber,
563 join_groupchat(m, friendnumber, group_public_key));
564 new_lines(msg);
565}
566
567void print_groupmessage(Messenger *m, int groupnumber, uint8_t *message, uint16_t length, void *userdata)
568{
569 char msg[256 + length];
570 sprintf(msg, "[g] %u: %s", groupnumber, message);
571 new_lines(msg);
572}
573
574
538int main(int argc, char *argv[]) 575int main(int argc, char *argv[])
539{ 576{
540 int on = 0; 577 int on = 0;
@@ -579,6 +616,8 @@ int main(int argc, char *argv[])
579 tox_callback_friendmessage(m, print_message, NULL); 616 tox_callback_friendmessage(m, print_message, NULL);
580 tox_callback_namechange(m, print_nickchange, NULL); 617 tox_callback_namechange(m, print_nickchange, NULL);
581 tox_callback_statusmessage(m, print_statuschange, NULL); 618 tox_callback_statusmessage(m, print_statuschange, NULL);
619 m_callback_group_invite(m, print_invite, NULL);
620 m_callback_group_message(m, print_groupmessage, NULL);
582 621
583 initscr(); 622 initscr();
584 noecho(); 623 noecho();
diff --git a/testing/nTox.h b/testing/nTox.h
index a72ce0c2..8a41965b 100644
--- a/testing/nTox.h
+++ b/testing/nTox.h
@@ -31,7 +31,7 @@
31#include <ctype.h> 31#include <ctype.h>
32 32
33#include "../toxcore/tox.h" 33#include "../toxcore/tox.h"
34 34#include "../toxcore/Messenger.h" //TODO: remove this
35#define STRING_LENGTH 256 35#define STRING_LENGTH 256
36#define HISTORY 50 36#define HISTORY 50
37#define PUB_KEY_BYTES 32 37#define PUB_KEY_BYTES 32
diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c
index 571c7291..951057c5 100644
--- a/toxcore/Messenger.c
+++ b/toxcore/Messenger.c
@@ -673,6 +673,40 @@ static int group_num(Messenger *m, uint8_t *group_public_key)
673 return -1; 673 return -1;
674} 674}
675 675
676/* Set the callback for group invites.
677 *
678 * Function(Messenger *m, int friendnumber, uint8_t *group_public_key, void *userdata)
679 */
680void m_callback_group_invite(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, void *), void *userdata)
681{
682 m->group_invite = function;
683 m->group_invite_userdata = userdata;
684}
685
686/* Set the callback for group messages.
687 *
688 * Function(Messenger *m, int groupnumber, uint8_t * message, uint16_t length, void *userdata)
689 */
690void m_callback_group_message(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t, void *),
691 void *userdata)
692{
693 m->group_message = function;
694 m->group_message_userdata = userdata;
695}
696static void group_message_function(Group_Chat *chat, int peer_number, uint8_t *message, uint16_t length, void *userdata)
697{
698 Messenger *m = userdata;
699 uint32_t i;
700
701 for (i = 0; i < m->numchats; ++i) { //TODO: remove this
702 if (m->chats[i] == chat)
703 break;
704 }
705
706 if (m->group_message)
707 (*m->group_message)(m, i, message, length, m->group_invite_userdata);
708}
709
676/* Creates a new groupchat and puts it in the chats array. 710/* Creates a new groupchat and puts it in the chats array.
677 * 711 *
678 * return group number on success. 712 * return group number on success.
@@ -689,6 +723,7 @@ int add_groupchat(Messenger *m)
689 if (newchat == NULL) 723 if (newchat == NULL)
690 return -1; 724 return -1;
691 725
726 callback_groupmessage(newchat, &group_message_function, m);
692 m->chats[i] = newchat; 727 m->chats[i] = newchat;
693 return i; 728 return i;
694 } 729 }
@@ -705,6 +740,8 @@ int add_groupchat(Messenger *m)
705 if (temp[m->numchats] == NULL) 740 if (temp[m->numchats] == NULL)
706 return -1; 741 return -1;
707 742
743 m->chats = temp;
744 callback_groupmessage(temp[m->numchats], &group_message_function, m);
708 ++m->numchats; 745 ++m->numchats;
709 return (m->numchats - 1); 746 return (m->numchats - 1);
710} 747}
@@ -714,7 +751,7 @@ int add_groupchat(Messenger *m)
714 * return 0 on success. 751 * return 0 on success.
715 * return -1 if failure. 752 * return -1 if failure.
716 */ 753 */
717static int del_groupchat(Messenger *m, int groupnumber) 754int del_groupchat(Messenger *m, int groupnumber)
718{ 755{
719 if ((unsigned int)groupnumber >= m->numchats) 756 if ((unsigned int)groupnumber >= m->numchats)
720 return -1; 757 return -1;
@@ -748,9 +785,42 @@ static int del_groupchat(Messenger *m, int groupnumber)
748 return 0; 785 return 0;
749} 786}
750 787
788/* return 1 if that friend was invited to the group
789 * return 0 if the friend was not or error.
790 */
791static uint8_t group_invited(Messenger *m, int friendnumber, int groupnumber)
792{
793 //TODO: this function;
794 return 1;
795}
796
797/* invite friendnumber to groupnumber
798 * return 0 on success
799 * return -1 on failure
800 */
801int invite_friend(Messenger *m, int friendnumber, int groupnumber)
802{
803 if (friend_not_valid(m, friendnumber) || (unsigned int)groupnumber >= m->numchats)
804 return -1;
805
806 if (m->chats == NULL)
807 return -1;
808
809 if (m->friendlist[friendnumber].status == NOFRIEND || m->chats[groupnumber] == NULL)
810 return -1;
811
812 //TODO: store invited friends.
813 if (write_cryptpacket_id(m, friendnumber, PACKET_ID_INVITE_GROUPCHAT, m->chats[groupnumber]->self_public_key,
814 crypto_box_PUBLICKEYBYTES) == 0)
815 return -1;
816
817 return 0;
818}
819
820
751/* Join a group (you need to have been invited first.) 821/* Join a group (you need to have been invited first.)
752 * 822 *
753 * returns 0 on success 823 * returns group number on success
754 * returns -1 on failure. 824 * returns -1 on failure.
755 */ 825 */
756int join_groupchat(Messenger *m, int friendnumber, uint8_t *friend_group_public_key) 826int join_groupchat(Messenger *m, int friendnumber, uint8_t *friend_group_public_key)
@@ -770,12 +840,34 @@ int join_groupchat(Messenger *m, int friendnumber, uint8_t *friend_group_public_
770 if (write_cryptpacket_id(m, friendnumber, PACKET_ID_JOIN_GROUPCHAT, data, sizeof(data))) { 840 if (write_cryptpacket_id(m, friendnumber, PACKET_ID_JOIN_GROUPCHAT, data, sizeof(data))) {
771 chat_bootstrap_nonlazy(m->chats[groupnum], get_friend_ipport(m, friendnumber), 841 chat_bootstrap_nonlazy(m->chats[groupnum], get_friend_ipport(m, friendnumber),
772 friend_group_public_key); //TODO: check if ip returned is zero? 842 friend_group_public_key); //TODO: check if ip returned is zero?
773 return 0; 843 return groupnum;
774 } 844 }
775 845
776 return -1; 846 return -1;
777} 847}
778 848
849/* send a group message
850 * return 0 on success
851 * return -1 on failure
852 */
853
854int group_message_send(Messenger *m, int groupnumber, uint8_t *message, uint32_t length)
855{
856 if ((unsigned int)groupnumber >= m->numchats)
857 return -1;
858
859 if (m->chats == NULL)
860 return -1;
861
862 if (m->chats[groupnumber] == NULL)
863 return -1;
864
865 if (group_sendmessage(m->chats[groupnumber], message, length) > 0)
866 return 0;
867
868 return -1;
869}
870
779static int handle_group(void *object, IP_Port source, uint8_t *packet, uint32_t length) 871static int handle_group(void *object, IP_Port source, uint8_t *packet, uint32_t length)
780{ 872{
781 Messenger *m = object; 873 Messenger *m = object;
@@ -1062,6 +1154,29 @@ void doFriends(Messenger *m)
1062 1154
1063 break; 1155 break;
1064 } 1156 }
1157
1158 case PACKET_ID_INVITE_GROUPCHAT: {
1159 if (data_length != crypto_box_PUBLICKEYBYTES)
1160 break;
1161
1162 if (m->group_invite)
1163 (*m->group_invite)(m, i, data, m->group_invite_userdata);
1164 }
1165
1166 case PACKET_ID_JOIN_GROUPCHAT: {
1167 if (data_length != crypto_box_PUBLICKEYBYTES * 2)
1168 break;
1169
1170 int groupnum = group_num(m, data);
1171
1172 if (groupnum == -1)
1173 break;
1174
1175 if (!group_invited(m, i, groupnum))
1176 break;
1177
1178 group_newpeer(m->chats[groupnum], data + crypto_box_PUBLICKEYBYTES);
1179 }
1065 } 1180 }
1066 } else { 1181 } else {
1067 if (is_cryptoconnected(m->net_crypto, 1182 if (is_cryptoconnected(m->net_crypto,
diff --git a/toxcore/Messenger.h b/toxcore/Messenger.h
index 7649a779..0656c736 100644
--- a/toxcore/Messenger.h
+++ b/toxcore/Messenger.h
@@ -157,7 +157,10 @@ typedef struct Messenger {
157 void *friend_statuschange_userdata; 157 void *friend_statuschange_userdata;
158 void (*friend_connectionstatuschange)(struct Messenger *m, int, uint8_t, void *); 158 void (*friend_connectionstatuschange)(struct Messenger *m, int, uint8_t, void *);
159 void *friend_connectionstatuschange_userdata; 159 void *friend_connectionstatuschange_userdata;
160 160 void (*group_invite)(struct Messenger *m, int, uint8_t *, void *);
161 void *group_invite_userdata;
162 void (*group_message)(struct Messenger *m, int, uint8_t *, uint16_t, void *);
163 void *group_message_userdata;
161 164
162} Messenger; 165} Messenger;
163 166
@@ -369,6 +372,57 @@ void m_callback_read_receipt(Messenger *m, void (*function)(Messenger *m, int, u
369 */ 372 */
370void m_callback_connectionstatus(Messenger *m, void (*function)(Messenger *m, int, uint8_t, void *), void *userdata); 373void m_callback_connectionstatus(Messenger *m, void (*function)(Messenger *m, int, uint8_t, void *), void *userdata);
371 374
375/**********GROUP CHATS************/
376
377/* Set the callback for group invites.
378 *
379 * Function(Messenger *m, int friendnumber, uint8_t *group_public_key, void *userdata)
380 */
381void m_callback_group_invite(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, void *), void *userdata);
382
383/* Set the callback for group messages.
384 *
385 * Function(Messenger *m, int groupnumber, uint8_t * message, uint16_t length, void *userdata)
386 */
387void m_callback_group_message(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t, void *),
388 void *userdata);
389
390/* Creates a new groupchat and puts it in the chats array.
391 *
392 * return group number on success.
393 * return -1 on failure.
394 */
395int add_groupchat(Messenger *m);
396
397/* Delete a groupchat from the chats array.
398 *
399 * return 0 on success.
400 * return -1 if failure.
401 */
402int del_groupchat(Messenger *m, int groupnumber);
403
404/* invite friendnumber to groupnumber
405 * return 0 on success
406 * return -1 on failure
407 */
408int invite_friend(Messenger *m, int friendnumber, int groupnumber);
409
410/* Join a group (you need to have been invited first.)
411 *
412 * returns group number on success
413 * returns -1 on failure.
414 */
415int join_groupchat(Messenger *m, int friendnumber, uint8_t *friend_group_public_key);
416
417/* send a group message
418 * return 0 on success
419 * return -1 on failure
420 */
421
422int group_message_send(Messenger *m, int groupnumber, uint8_t *message, uint32_t length);
423
424/*********************************/
425
372/* Run this at startup. 426/* Run this at startup.
373 * return allocated instance of Messenger on success. 427 * return allocated instance of Messenger on success.
374 * return 0 if there are problems. 428 * return 0 if there are problems.
diff --git a/toxcore/group_chats.h b/toxcore/group_chats.h
index 535b46db..78a5488c 100644
--- a/toxcore/group_chats.h
+++ b/toxcore/group_chats.h
@@ -77,6 +77,7 @@ void callback_groupmessage(Group_Chat *chat, void (*function)(Group_Chat *chat,
77/* 77/*
78 * Send a message to the group. 78 * Send a message to the group.
79 * 79 *
80 * returns the number of peers it has sent it to.
80 */ 81 */
81uint32_t group_sendmessage(Group_Chat *chat, uint8_t *message, uint32_t length); 82uint32_t group_sendmessage(Group_Chat *chat, uint8_t *message, uint32_t length);
82 83