From e85e9dbb999f1fccd2083224cc52eebe29915bfe Mon Sep 17 00:00:00 2001 From: "Coren[m]" Date: Sun, 24 Nov 2013 21:11:54 +0100 Subject: [PATCH] group_namelistchange(): for ui reasons, divulge the peernumber and the nature of the change (add, del, name change). tox.*, Messenger.*, group_chats.*: - expand interface of callback by peernumber/change groupchats.c: - call callback in addpeer/delpeer/setnick with peernumber and flag nTox.c: - print_groupchatpeers(): print as many names on a line as possible, not one peer per line - print_groupnamelistchange(): only print the change, if possible (i.e. "new peer", "peer's new name") - added command "/p" to print the list of peers --- testing/nTox.c | 98 ++++++++++++++++++++++++++++++++++++++----- toxcore/Messenger.c | 6 +-- toxcore/Messenger.h | 4 +- toxcore/group_chats.c | 8 ++-- toxcore/group_chats.h | 10 ++++- toxcore/tox.c | 2 +- toxcore/tox.h | 9 +++- 7 files changed, 112 insertions(+), 25 deletions(-) diff --git a/testing/nTox.c b/testing/nTox.c index e7216d33..24054f5e 100644 --- a/testing/nTox.c +++ b/testing/nTox.c @@ -92,9 +92,10 @@ char *help_friend2 = /* 216 characters */ char *help_group = "[i] Available group commands:\n+ " - "/g (to create a new group)|" + "/g (to create a group)|" "/i friend no. group no. (to invite a friend to a group)|" "/z group no. message (to send a message to a group)|" + "/p group no. (to list a group's peers)|" "/cg group no. (to talk to that group per default)"; int x, y; @@ -346,8 +347,9 @@ static void print_formatted_message(Tox *m, char *message, int friendnum, uint8_ new_lines(msg); } -/* forward declaration */ +/* forward declarations */ static int save_data(Tox *m); +void print_groupchatpeers(Tox *m, int groupnumber); void line_eval(Tox *m, char *line) { @@ -561,6 +563,25 @@ void line_eval(Tox *m, char *line) } else new_lines("[i] invalid command"); } + } else if (inpt_command == 'p') { //list peers + char *posi = NULL; + int group_number = strtoul(line + prompt_offset, &posi, 0); + + if (posi != NULL) { + char msg[64]; + int peer_cnt = tox_group_number_peers(m, group_number); + + if (peer_cnt < 0) { + new_lines("[g] Invalid group number."); + } else if (peer_cnt == 0) { + sprintf(msg, "[g] #%i: No peers in group.", group_number); + new_lines(msg); + } else { + sprintf(msg, "[g] #%i: Group has %i peers. Names:", group_number, peer_cnt); + new_lines(msg); + print_groupchatpeers(m, group_number); + } + } } else { new_lines("[i] invalid command"); } @@ -974,24 +995,60 @@ void print_invite(Tox *m, int friendnumber, uint8_t *group_public_key, void *use tox_join_groupchat(m, friendnumber, group_public_key)); new_lines(msg); } + void print_groupchatpeers(Tox *m, int groupnumber) { - char msg[256]; int num = tox_group_number_peers(m, groupnumber); - if (num == -1) + + if (num < 0) return; + if (!num) { + new_lines("[g]+ no peers left in group."); + return; + } + uint8_t names[num][TOX_MAX_NAME_LENGTH]; tox_group_copy_names(m, groupnumber, names, num); - uint32_t i; - for (i = 0; i < num; ++i) - new_lines(names[i]); + int i; + char numstr[16]; + char header[] = "[g]+ "; + size_t header_len = strlen(header); + char msg[STRING_LENGTH]; + strcpy(msg, header); + size_t len_total = header_len; + + for (i = 0; i < num; ++i) { + size_t len_name = strlen((char *)names[i]); + size_t len_num = sprintf(numstr, "%i: ", i); + + if (len_num + len_name + len_total + 3 >= STRING_LENGTH) { + new_lines_mark(msg, 1); + + strcpy(msg, header); + len_total = header_len; + } + + strcpy(msg + len_total, numstr); + len_total += len_num; + strcpy(msg + len_total, (char *)names[i]); + len_total += len_name; + + if (i < num - 1) { + strcpy(msg + len_total, "|"); + len_total++; + } + } + + new_lines_mark(msg, 1); } + void print_groupmessage(Tox *m, int groupnumber, int peernumber, uint8_t *message, uint16_t length, void *userdata) { char msg[256 + length]; uint8_t name[TOX_MAX_NAME_LENGTH]; int len = tox_group_peername(m, groupnumber, peernumber, name); + //print_groupchatpeers(m, groupnumber); if (len <= 0) name[0] = 0; @@ -1003,12 +1060,31 @@ void print_groupmessage(Tox *m, int groupnumber, int peernumber, uint8_t *messag new_lines(msg); } -void print_groupnamelistchange(Tox *m, int groupnumber, void *userdata) +void print_groupnamelistchange(Tox *m, int groupnumber, int peernumber, uint8_t change, void *userdata) { char msg[256]; - sprintf(msg, "[g] %u: Name list changed:", groupnumber); - new_lines(msg); - print_groupchatpeers(m, groupnumber); + + if (change == TOX_CHAT_CHANGE_PEER_ADD) { + sprintf(msg, "[g] #%i: New peer %i.", groupnumber, peernumber); + new_lines(msg); + } else if (change == TOX_CHAT_CHANGE_PEER_DEL) { + sprintf(msg, "[g] #%i: Peer %i left. Name list is now:", groupnumber, peernumber); + new_lines(msg); + print_groupchatpeers(m, groupnumber); + } else if (change == TOX_CHAT_CHANGE_PEER_NAME) { + uint8_t peername[TOX_MAX_NAME_LENGTH]; + int len = tox_group_peername(m, groupnumber, peernumber, peername); + + if (len <= 0) + peername[0] = 0; + + sprintf(msg, "[g] #%i: Peer %i's name changed: %s", groupnumber, peernumber, peername); + new_lines(msg); + } else { + sprintf(msg, "[g] #%i: Name list changed (peer %i, change %i?):", groupnumber, peernumber, change); + 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) diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c index cc52c069..1a7fc230 100644 --- a/toxcore/Messenger.c +++ b/toxcore/Messenger.c @@ -785,7 +785,7 @@ void m_callback_group_message(Messenger *m, void (*function)(Messenger *m, int, * 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) +void m_callback_group_namelistchange(Messenger *m, void (*function)(Messenger *m, int, int, uint8_t, void *), void *userdata) { m->group_namelistchange = function; m->group_namelistchange_userdata = userdata; @@ -812,7 +812,7 @@ static void group_message_function(Group_Chat *chat, int peer_number, uint8_t *m (*m->group_message)(m, i, peer_number, message, length, m->group_message_userdata); } -static void group_namelistchange_function(Group_Chat *chat, void *userdata) +static void group_namelistchange_function(Group_Chat *chat, int peer, uint8_t change, void *userdata) { Messenger *m = userdata; int i = get_chat_num(m, chat); @@ -820,7 +820,7 @@ static void group_namelistchange_function(Group_Chat *chat, void *userdata) return; if (m->group_namelistchange) - (*m->group_namelistchange)(m, i, m->group_namelistchange_userdata); + (*m->group_namelistchange)(m, i, peer, change, m->group_namelistchange_userdata); } diff --git a/toxcore/Messenger.h b/toxcore/Messenger.h index bf8a0572..6fc23db4 100644 --- a/toxcore/Messenger.h +++ b/toxcore/Messenger.h @@ -198,7 +198,7 @@ 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)(struct Messenger *m, int, int, uint8_t, void *); void *group_namelistchange_userdata; void (*file_sendrequest)(struct Messenger *m, int, uint8_t, uint64_t, uint8_t *, uint16_t, void *); @@ -462,7 +462,7 @@ void m_callback_group_message(Messenger *m, void (*function)(Messenger *m, int, * 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); +void m_callback_group_namelistchange(Messenger *m, void (*function)(Messenger *m, int, int, uint8_t, void *), void *userdata); /* Creates a new groupchat and puts it in the chats array. * diff --git a/toxcore/group_chats.c b/toxcore/group_chats.c index 7e18a334..a20ec555 100644 --- a/toxcore/group_chats.c +++ b/toxcore/group_chats.c @@ -219,7 +219,7 @@ static int addpeer(Group_Chat *chat, uint8_t *client_id) ++chat->numpeers; if (chat->peer_namelistchange != NULL) - (*chat->peer_namelistchange)(chat, chat->group_namelistchange_userdata); + (*chat->peer_namelistchange)(chat, chat->numpeers - 1, CHAT_CHANGE_PEER_ADD, chat->group_namelistchange_userdata); return (chat->numpeers - 1); } @@ -255,7 +255,7 @@ static int delpeer(Group_Chat *chat, int peernum) chat->group = temp; if (chat->peer_namelistchange != NULL) - (*chat->peer_namelistchange)(chat, chat->group_namelistchange_userdata); + (*chat->peer_namelistchange)(chat, peernum, CHAT_CHANGE_PEER_DEL, chat->group_namelistchange_userdata); return 0; } @@ -291,7 +291,7 @@ static void setnick(Group_Chat *chat, int peernum, uint8_t *contents, uint16_t c 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); + (*chat->peer_namelistchange)(chat, peernum, CHAT_CHANGE_PEER_NAME, chat->group_namelistchange_userdata); } /* min time between pings sent to one peer in seconds */ @@ -625,7 +625,7 @@ 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) +void callback_namelistchange(Group_Chat *chat, void (*function)(Group_Chat *chat, int peer, uint8_t change, void *), void *userdata) { chat->peer_namelistchange = function; chat->group_namelistchange_userdata = userdata; diff --git a/toxcore/group_chats.h b/toxcore/group_chats.h index 2235a6e3..65f573fb 100644 --- a/toxcore/group_chats.h +++ b/toxcore/group_chats.h @@ -65,7 +65,7 @@ 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 (*peer_namelistchange)(struct Group_Chat *m, int peer, uint8_t change, void *); void *group_namelistchange_userdata; uint64_t last_sent_ping; @@ -105,7 +105,13 @@ void callback_groupmessage(Group_Chat *chat, void (*function)(Group_Chat *chat, * * format of function is: function(Group_Chat *chat, userdata) */ -void callback_namelistchange(Group_Chat *chat, void (*function)(Group_Chat *chat, void *), void *userdata); +typedef enum { + CHAT_CHANGE_PEER_ADD, + CHAT_CHANGE_PEER_DEL, + CHAT_CHANGE_PEER_NAME, +} CHAT_CHANGE; + +void callback_namelistchange(Group_Chat *chat, void (*function)(Group_Chat *chat, int peer, uint8_t change, void *), void *userdata); /* * Send a message to the group. diff --git a/toxcore/tox.c b/toxcore/tox.c index 4ba17e34..d5bc1c35 100644 --- a/toxcore/tox.c +++ b/toxcore/tox.c @@ -422,7 +422,7 @@ void tox_callback_group_message(Tox *tox, void (*function)(Messenger *tox, int, * Function(Tox *tox, int groupnumber, void *userdata) */ -void tox_callback_group_namelistchange(Tox *tox, void (*function)(Tox *tox, int, void *), void *userdata) +void tox_callback_group_namelistchange(Tox *tox, void (*function)(Tox *tox, int, int, uint8_t, void *), void *userdata) { Messenger *m = tox; m_callback_group_namelistchange(m, function, userdata); diff --git a/toxcore/tox.h b/toxcore/tox.h index fedc2c8a..08727d2a 100644 --- a/toxcore/tox.h +++ b/toxcore/tox.h @@ -366,10 +366,15 @@ void tox_callback_group_message(Tox *tox, void (*function)(Tox *tox, int, int, u /* 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) + * Function(Tox *tox, int groupnumber, int peernumber, TOX_CHAT_CHANGE change, void *userdata) */ +typedef enum { + TOX_CHAT_CHANGE_PEER_ADD, + TOX_CHAT_CHANGE_PEER_DEL, + TOX_CHAT_CHANGE_PEER_NAME, +} TOX_CHAT_CHANGE; -void tox_callback_group_namelistchange(Tox *tox, void (*function)(Tox *tox, int, void *), void *userdata); +void tox_callback_group_namelistchange(Tox *tox, void (*function)(Tox *tox, int, int, uint8_t, void *), void *userdata); /* Creates a new groupchat and puts it in the chats array. *