diff options
Diffstat (limited to 'toxcore')
-rw-r--r-- | toxcore/Messenger.c | 121 | ||||
-rw-r--r-- | toxcore/Messenger.h | 56 | ||||
-rw-r--r-- | toxcore/group_chats.h | 1 |
3 files changed, 174 insertions, 4 deletions
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 | */ | ||
680 | void 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 | */ | ||
690 | void 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 | } | ||
696 | static 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 | */ |
717 | static int del_groupchat(Messenger *m, int groupnumber) | 754 | int 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 | */ | ||
791 | static 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 | */ | ||
801 | int 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 | */ |
756 | int join_groupchat(Messenger *m, int friendnumber, uint8_t *friend_group_public_key) | 826 | int 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 | |||
854 | int 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 | |||
779 | static int handle_group(void *object, IP_Port source, uint8_t *packet, uint32_t length) | 871 | static 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 | */ |
370 | void m_callback_connectionstatus(Messenger *m, void (*function)(Messenger *m, int, uint8_t, void *), void *userdata); | 373 | void 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 | */ | ||
381 | void 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 | */ | ||
387 | void 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 | */ | ||
395 | int 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 | */ | ||
402 | int del_groupchat(Messenger *m, int groupnumber); | ||
403 | |||
404 | /* invite friendnumber to groupnumber | ||
405 | * return 0 on success | ||
406 | * return -1 on failure | ||
407 | */ | ||
408 | int 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 | */ | ||
415 | int 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 | |||
422 | int 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 | */ |
81 | uint32_t group_sendmessage(Group_Chat *chat, uint8_t *message, uint32_t length); | 82 | uint32_t group_sendmessage(Group_Chat *chat, uint8_t *message, uint32_t length); |
82 | 83 | ||