summaryrefslogtreecommitdiff
path: root/toxcore/Messenger.c
diff options
context:
space:
mode:
Diffstat (limited to 'toxcore/Messenger.c')
-rw-r--r--toxcore/Messenger.c124
1 files changed, 118 insertions, 6 deletions
diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c
index d228fcc2..8d0b149f 100644
--- a/toxcore/Messenger.c
+++ b/toxcore/Messenger.c
@@ -674,6 +674,40 @@ static int group_num(Messenger *m, uint8_t *group_public_key)
674 return -1; 674 return -1;
675} 675}
676 676
677/* Set the callback for group invites.
678 *
679 * Function(Messenger *m, int friendnumber, uint8_t *group_public_key, void *userdata)
680 */
681void m_callback_group_invite(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, void *), void *userdata)
682{
683 m->group_invite = function;
684 m->group_invite_userdata = userdata;
685}
686
687/* Set the callback for group messages.
688 *
689 * Function(Messenger *m, int groupnumber, uint8_t * message, uint16_t length, void *userdata)
690 */
691void m_callback_group_message(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t, void *),
692 void *userdata)
693{
694 m->group_message = function;
695 m->group_message_userdata = userdata;
696}
697static void group_message_function(Group_Chat *chat, int peer_number, uint8_t *message, uint16_t length, void *userdata)
698{
699 Messenger *m = userdata;
700 uint32_t i;
701
702 for (i = 0; i < m->numchats; ++i) { //TODO: remove this
703 if (m->chats[i] == chat)
704 break;
705 }
706
707 if (m->group_message)
708 (*m->group_message)(m, i, message, length, m->group_invite_userdata);
709}
710
677/* Creates a new groupchat and puts it in the chats array. 711/* Creates a new groupchat and puts it in the chats array.
678 * 712 *
679 * return group number on success. 713 * return group number on success.
@@ -690,6 +724,7 @@ int add_groupchat(Messenger *m)
690 if (newchat == NULL) 724 if (newchat == NULL)
691 return -1; 725 return -1;
692 726
727 callback_groupmessage(newchat, &group_message_function, m);
693 m->chats[i] = newchat; 728 m->chats[i] = newchat;
694 return i; 729 return i;
695 } 730 }
@@ -706,6 +741,8 @@ int add_groupchat(Messenger *m)
706 if (temp[m->numchats] == NULL) 741 if (temp[m->numchats] == NULL)
707 return -1; 742 return -1;
708 743
744 m->chats = temp;
745 callback_groupmessage(temp[m->numchats], &group_message_function, m);
709 ++m->numchats; 746 ++m->numchats;
710 return (m->numchats - 1); 747 return (m->numchats - 1);
711} 748}
@@ -715,7 +752,7 @@ int add_groupchat(Messenger *m)
715 * return 0 on success. 752 * return 0 on success.
716 * return -1 if failure. 753 * return -1 if failure.
717 */ 754 */
718static int del_groupchat(Messenger *m, int groupnumber) 755int del_groupchat(Messenger *m, int groupnumber)
719{ 756{
720 if ((unsigned int)groupnumber >= m->numchats) 757 if ((unsigned int)groupnumber >= m->numchats)
721 return -1; 758 return -1;
@@ -749,9 +786,42 @@ static int del_groupchat(Messenger *m, int groupnumber)
749 return 0; 786 return 0;
750} 787}
751 788
789/* return 1 if that friend was invited to the group
790 * return 0 if the friend was not or error.
791 */
792static uint8_t group_invited(Messenger *m, int friendnumber, int groupnumber)
793{
794 //TODO: this function;
795 return 1;
796}
797
798/* invite friendnumber to groupnumber
799 * return 0 on success
800 * return -1 on failure
801 */
802int invite_friend(Messenger *m, int friendnumber, int groupnumber)
803{
804 if (friend_not_valid(m, friendnumber) || (unsigned int)groupnumber >= m->numchats)
805 return -1;
806
807 if (m->chats == NULL)
808 return -1;
809
810 if (m->friendlist[friendnumber].status == NOFRIEND || m->chats[groupnumber] == NULL)
811 return -1;
812
813 //TODO: store invited friends.
814 if (write_cryptpacket_id(m, friendnumber, PACKET_ID_INVITE_GROUPCHAT, m->chats[groupnumber]->self_public_key,
815 crypto_box_PUBLICKEYBYTES) == 0)
816 return -1;
817
818 return 0;
819}
820
821
752/* Join a group (you need to have been invited first.) 822/* Join a group (you need to have been invited first.)
753 * 823 *
754 * returns 0 on success 824 * returns group number on success
755 * returns -1 on failure. 825 * returns -1 on failure.
756 */ 826 */
757int join_groupchat(Messenger *m, int friendnumber, uint8_t *friend_group_public_key) 827int join_groupchat(Messenger *m, int friendnumber, uint8_t *friend_group_public_key)
@@ -771,12 +841,34 @@ int join_groupchat(Messenger *m, int friendnumber, uint8_t *friend_group_public_
771 if (write_cryptpacket_id(m, friendnumber, PACKET_ID_JOIN_GROUPCHAT, data, sizeof(data))) { 841 if (write_cryptpacket_id(m, friendnumber, PACKET_ID_JOIN_GROUPCHAT, data, sizeof(data))) {
772 chat_bootstrap_nonlazy(m->chats[groupnum], get_friend_ipport(m, friendnumber), 842 chat_bootstrap_nonlazy(m->chats[groupnum], get_friend_ipport(m, friendnumber),
773 friend_group_public_key); //TODO: check if ip returned is zero? 843 friend_group_public_key); //TODO: check if ip returned is zero?
774 return 0; 844 return groupnum;
775 } 845 }
776 846
777 return -1; 847 return -1;
778} 848}
779 849
850/* send a group message
851 * return 0 on success
852 * return -1 on failure
853 */
854
855int group_message_send(Messenger *m, int groupnumber, uint8_t *message, uint32_t length)
856{
857 if ((unsigned int)groupnumber >= m->numchats)
858 return -1;
859
860 if (m->chats == NULL)
861 return -1;
862
863 if (m->chats[groupnumber] == NULL)
864 return -1;
865
866 if (group_sendmessage(m->chats[groupnumber], message, length) > 0)
867 return 0;
868
869 return -1;
870}
871
780static int handle_group(void *object, IP_Port source, uint8_t *packet, uint32_t length) 872static int handle_group(void *object, IP_Port source, uint8_t *packet, uint32_t length)
781{ 873{
782 Messenger *m = object; 874 Messenger *m = object;
@@ -810,9 +902,6 @@ static void do_allgroupchats(Messenger *m)
810 902
811/*********************************/ 903/*********************************/
812 904
813/* Interval in seconds between LAN discovery packet sending. */
814#define LAN_DISCOVERY_INTERVAL 60
815
816/* Send a LAN discovery packet every LAN_DISCOVERY_INTERVAL seconds. */ 905/* Send a LAN discovery packet every LAN_DISCOVERY_INTERVAL seconds. */
817static void LANdiscovery(Messenger *m) 906static void LANdiscovery(Messenger *m)
818{ 907{
@@ -1065,6 +1154,29 @@ void doFriends(Messenger *m)
1065 1154
1066 break; 1155 break;
1067 } 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 }
1068 } 1180 }
1069 } else { 1181 } else {
1070 if (is_cryptoconnected(m->net_crypto, 1182 if (is_cryptoconnected(m->net_crypto,