diff --git a/testing/nTox.c b/testing/nTox.c index 7448bf7e..e7216d33 100644 --- a/testing/nTox.c +++ b/testing/nTox.c @@ -1003,7 +1003,13 @@ void print_groupmessage(Tox *m, int groupnumber, int peernumber, uint8_t *messag new_lines(msg); } - +void print_groupnamelistchange(Tox *m, int groupnumber, void *userdata) +{ + char msg[256]; + sprintf(msg, "[g] %u: Name list changed:", groupnumber); + new_lines(msg); + print_groupchatpeers(m, groupnumber); +} void file_request_accept(Tox *m, int friendnumber, uint8_t filenumber, uint64_t filesize, uint8_t *filename, uint16_t filename_length, void *userdata) { @@ -1105,6 +1111,7 @@ int main(int argc, char *argv[]) tox_callback_file_data(m, write_file, NULL); tox_callback_file_control(m, file_print_control, NULL); tox_callback_file_sendrequest(m, file_request_accept, NULL); + tox_callback_group_namelistchange(m, print_groupnamelistchange, NULL); initscr(); noecho(); diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c index 889de367..acb038df 100644 --- a/toxcore/Messenger.c +++ b/toxcore/Messenger.c @@ -779,20 +779,51 @@ void m_callback_group_message(Messenger *m, void (*function)(Messenger *m, int, m->group_message = function; m->group_message_userdata = userdata; } + +/* Set callback function for peer name list changes. + * + * It gets called every time the name list changes(new peer/name, deleted peer) + * Function(Tox *tox, int groupnumber, void *userdata) + */ +void m_callback_group_namelistchange(Messenger *m, void (*function)(Messenger *m, int, void *), void *userdata) +{ + m->group_namelistchange = function; + m->group_namelistchange_userdata = userdata; +} + +static int get_chat_num(Messenger *m, Group_Chat *chat) +{ + uint32_t i; + for (i = 0; i < m->numchats; ++i) { //TODO: remove this + if (m->chats[i] == chat) + return i; + } + return -1; +} + static void group_message_function(Group_Chat *chat, int peer_number, uint8_t *message, uint16_t length, void *userdata) { Messenger *m = userdata; - uint32_t i; - - for (i = 0; i < m->numchats; ++i) { //TODO: remove this - if (m->chats[i] == chat) - break; - } + int i = get_chat_num(m, chat); + if (i == -1) + return; if (m->group_message) (*m->group_message)(m, i, peer_number, message, length, m->group_message_userdata); } +static void group_namelistchange_function(Group_Chat *chat, void *userdata) +{ + Messenger *m = userdata; + int i = get_chat_num(m, chat); + if (i == -1) + return; + + if (m->group_namelistchange) + (*m->group_namelistchange)(m, i, m->group_namelistchange_userdata); +} + + /* Creates a new groupchat and puts it in the chats array. * * return group number on success. @@ -810,6 +841,7 @@ int add_groupchat(Messenger *m) return -1; callback_groupmessage(newchat, &group_message_function, m); + callback_namelistchange(newchat, &group_namelistchange_function, m); /* TODO: remove this (group nicks should not be tied to the global one) */ set_nick(newchat, m->name, m->name_length); m->chats[i] = newchat; @@ -830,6 +862,7 @@ int add_groupchat(Messenger *m) m->chats = temp; callback_groupmessage(temp[m->numchats], &group_message_function, m); + callback_namelistchange(temp[m->numchats], &group_namelistchange_function, m); /* TODO: remove this (group nicks should not be tied to the global one) */ set_nick(temp[m->numchats], m->name, m->name_length); ++m->numchats; diff --git a/toxcore/Messenger.h b/toxcore/Messenger.h index 651f3f12..bf8a0572 100644 --- a/toxcore/Messenger.h +++ b/toxcore/Messenger.h @@ -198,6 +198,8 @@ typedef struct Messenger { void *group_invite_userdata; void (*group_message)(struct Messenger *m, int, int, uint8_t *, uint16_t, void *); void *group_message_userdata; + void (*group_namelistchange)(struct Messenger *m, int, void *); + void *group_namelistchange_userdata; void (*file_sendrequest)(struct Messenger *m, int, uint8_t, uint64_t, uint8_t *, uint16_t, void *); void *file_sendrequest_userdata; @@ -455,6 +457,13 @@ void m_callback_group_invite(Messenger *m, void (*function)(Messenger *m, int, u void m_callback_group_message(Messenger *m, void (*function)(Messenger *m, int, int, uint8_t *, uint16_t, void *), void *userdata); +/* Set callback function for peer name list changes. + * + * It gets called every time the name list changes(new peer/name, deleted peer) + * Function(Tox *tox, int groupnumber, void *userdata) + */ +void m_callback_group_namelistchange(Messenger *m, void (*function)(Messenger *m, int, void *), void *userdata); + /* Creates a new groupchat and puts it in the chats array. * * return group number on success. diff --git a/toxcore/group_chats.c b/toxcore/group_chats.c index 34c84c40..7e18a334 100644 --- a/toxcore/group_chats.c +++ b/toxcore/group_chats.c @@ -218,6 +218,9 @@ static int addpeer(Group_Chat *chat, uint8_t *client_id) chat->group[chat->numpeers].last_recv_msgping = unix_time(); ++chat->numpeers; + if (chat->peer_namelistchange != NULL) + (*chat->peer_namelistchange)(chat, chat->group_namelistchange_userdata); + return (chat->numpeers - 1); } @@ -250,6 +253,10 @@ static int delpeer(Group_Chat *chat, int peernum) return -1; chat->group = temp; + + if (chat->peer_namelistchange != NULL) + (*chat->peer_namelistchange)(chat, chat->group_namelistchange_userdata); + return 0; } @@ -283,6 +290,8 @@ static void setnick(Group_Chat *chat, int peernum, uint8_t *contents, uint16_t c /* Force null termination */ chat->group[peernum].nick[contents_len - 1] = 0; chat->group[peernum].nick_len = contents_len; + if (chat->peer_namelistchange != NULL) + (*chat->peer_namelistchange)(chat, chat->group_namelistchange_userdata); } /* min time between pings sent to one peer in seconds */ @@ -616,6 +625,12 @@ void callback_groupmessage(Group_Chat *chat, void (*function)(Group_Chat *chat, chat->group_message_userdata = userdata; } +void callback_namelistchange(Group_Chat *chat, void (*function)(Group_Chat *chat, void *), void *userdata) +{ + chat->peer_namelistchange = function; + chat->group_namelistchange_userdata = userdata; +} + uint32_t group_numpeers(Group_Chat *chat) { return chat->numpeers; diff --git a/toxcore/group_chats.h b/toxcore/group_chats.h index 4257b1f5..2235a6e3 100644 --- a/toxcore/group_chats.h +++ b/toxcore/group_chats.h @@ -65,6 +65,8 @@ typedef struct Group_Chat { uint32_t message_number; void (*group_message)(struct Group_Chat *m, int, uint8_t *, uint16_t, void *); void *group_message_userdata; + void (*peer_namelistchange)(struct Group_Chat *m, void *); + void *group_namelistchange_userdata; uint64_t last_sent_ping; @@ -94,9 +96,16 @@ int group_peername(Group_Chat *chat, int peernum, uint8_t *name); * * format of function is: function(Group_Chat *chat, peer number, message, message length, userdata) */ - void callback_groupmessage(Group_Chat *chat, void (*function)(Group_Chat *chat, int, uint8_t *, uint16_t, void *), void *userdata); +/* + * Set callback function for peer name list changes. + * + * It gets called every time the name list changes(new peer/name, deleted peer) + * + * format of function is: function(Group_Chat *chat, userdata) + */ +void callback_namelistchange(Group_Chat *chat, void (*function)(Group_Chat *chat, void *), void *userdata); /* * Send a message to the group. diff --git a/toxcore/tox.c b/toxcore/tox.c index b5e754bb..4ba17e34 100644 --- a/toxcore/tox.c +++ b/toxcore/tox.c @@ -415,6 +415,19 @@ void tox_callback_group_message(Tox *tox, void (*function)(Messenger *tox, int, Messenger *m = tox; m_callback_group_message(m, function, userdata); } + +/* Set callback function for peer name list changes. + * + * It gets called every time the name list changes(new peer/name, deleted peer) + * Function(Tox *tox, int groupnumber, void *userdata) + */ + +void tox_callback_group_namelistchange(Tox *tox, void (*function)(Tox *tox, int, void *), void *userdata) +{ + Messenger *m = tox; + m_callback_group_namelistchange(m, function, userdata); +} + /* Creates a new groupchat and puts it in the chats array. * * return group number on success. diff --git a/toxcore/tox.h b/toxcore/tox.h index a0093ff3..fedc2c8a 100644 --- a/toxcore/tox.h +++ b/toxcore/tox.h @@ -363,6 +363,14 @@ void tox_callback_group_invite(Tox *tox, void (*function)(Tox *tox, int, uint8_t void tox_callback_group_message(Tox *tox, void (*function)(Tox *tox, int, int, uint8_t *, uint16_t, void *), void *userdata); +/* Set callback function for peer name list changes. + * + * It gets called every time the name list changes(new peer/name, deleted peer) + * Function(Tox *tox, int groupnumber, void *userdata) + */ + +void tox_callback_group_namelistchange(Tox *tox, void (*function)(Tox *tox, int, void *), void *userdata); + /* Creates a new groupchat and puts it in the chats array. * * return group number on success.