From 89637e7d2f65ec75d4f2b06109a3ee2b69746ea4 Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Mon, 12 Jan 2015 01:09:56 -0500 Subject: [PATCH] implement group join rejection callback --- src/groupchat.c | 61 ++++++++++++++++++++++++++++++++++++++++++++++--- src/groupchat.h | 1 + src/toxic.c | 1 + src/toxic.h | 1 + src/windows.c | 10 ++++++++ src/windows.h | 1 + 6 files changed, 72 insertions(+), 3 deletions(-) diff --git a/src/groupchat.c b/src/groupchat.c index ff89a42..703a31e 100644 --- a/src/groupchat.c +++ b/src/groupchat.c @@ -196,8 +196,14 @@ void set_nick_all_groups(Tox *m, const char *nick, uint16_t length) for (i = 0; i < max_groupchat_index; ++i) { if (groupchats[i].active) { ToxWindow *self = get_window_ptr(groupchats[i].chatwin); - tox_group_set_name(m, groupchats[i].groupnumber, (uint8_t *) nick, length); - line_info_add(self, timefrmt, NULL, nick, NAME_CHANGE, 0, MAGENTA, "You are now known as "); + int ret = tox_group_set_name(m, groupchats[i].groupnumber, (uint8_t *) nick, length); + + if (ret == -1 && groupchats[i].is_connected) + line_info_add(self, timefrmt, NULL, 0, SYS_MSG, 0, 0, "Invalid nick"); + else if (ret == -2) + line_info_add(self, timefrmt, NULL, 0, SYS_MSG, 0, RED, "-!- That nick is already in use"); + else + line_info_add(self, timefrmt, NULL, nick, NAME_CHANGE, 0, MAGENTA, "You are now known as "); } } } @@ -482,6 +488,15 @@ static void groupchat_onGroupSelfJoin(ToxWindow *self, Tox *m, int groupnum) char timefrmt[TIME_STR_SIZE]; get_time_str(timefrmt, sizeof(timefrmt)); + int i; + + for (i = 0; i < max_groupchat_index; ++i) { + if (groupchats[i].active && groupchats[i].groupnumber == groupnum) { + groupchats[i].is_connected = true; + break; + } + } + line_info_add(self, timefrmt, NULL, NULL, SYS_MSG, 0, MAGENTA, "-!- Topic set to: %s", topic); } @@ -490,7 +505,46 @@ static void groupchat_onGroupSelfTimeout(ToxWindow *self, Tox *m, int groupnum) if (groupnum != self->num) return; - line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Disconnected from group. Attempting to reconnect..."); + int i; + + for (i = 0; i < max_groupchat_index; ++i) { + if (groupchats[i].active && groupchats[i].groupnumber == groupnum) { + groupchats[i].is_connected = false; + break; + } + } + + char timefrmt[TIME_STR_SIZE]; + get_time_str(timefrmt, sizeof(timefrmt)); + + line_info_add(self, timefrmt, NULL, NULL, SYS_MSG, 0, RED, "-!- Disconnected from group"); +} + +static void groupchat_onGroupRejected(ToxWindow *self, Tox *m, int groupnum, uint8_t type) +{ + if (groupnum != self->num) + return; + + const char *msg; + + switch (type) { + case TOX_GJ_NICK_TAKEN: + msg = "That nick is already in use. Please change your nick with the '/nick' command."; + break; + case TOX_GJ_GROUP_FULL: + msg = "Group is full."; + break; + case TOX_GJ_INVITES_DISABLED: + msg = "Invites for this group have been disabled."; + break; + default: + return; + } + + char timefrmt[TIME_STR_SIZE]; + get_time_str(timefrmt, sizeof(timefrmt)); + + line_info_add(self, timefrmt, NULL, NULL, SYS_MSG, 0, RED, "-!- %s", msg); } static void groupchat_onGroupOpCertificate(ToxWindow *self, Tox *m, int groupnum, uint32_t src_peernum, @@ -929,6 +983,7 @@ ToxWindow new_group_chat(Tox *m, int groupnum, const char *groupname, int length ret.onGroupNickChange = &groupchat_onGroupNickChange; ret.onGroupSelfJoin = &groupchat_onGroupSelfJoin; ret.onGroupSelfTimeout = &groupchat_onGroupSelfTimeout; + ret.onGroupRejected = &groupchat_onGroupRejected; #ifdef AUDIO ret.onWriteDevice = &groupchat_onWriteDevice; diff --git a/src/groupchat.h b/src/groupchat.h index 0305358..9b235b3 100644 --- a/src/groupchat.h +++ b/src/groupchat.h @@ -62,6 +62,7 @@ typedef struct { int groupnumber; int chatwin; bool active; + bool is_connected; int num_peers; int side_pos; /* current position of the sidebar - used for scrolling up and down */ uint8_t *peer_names; diff --git a/src/toxic.c b/src/toxic.c index f2a0936..edeea94 100644 --- a/src/toxic.c +++ b/src/toxic.c @@ -321,6 +321,7 @@ static Tox *init_tox(void) tox_callback_group_topic_change(m, on_group_topic_change, NULL); tox_callback_group_self_join(m, on_group_self_join, NULL); tox_callback_group_self_timeout(m, on_group_self_timeout, NULL); + tox_callback_group_rejected(m, on_group_rejected, NULL); tox_set_name(m, (uint8_t *) "Toxic User", strlen("Toxic User")); diff --git a/src/toxic.h b/src/toxic.h index 5527060..b0da87e 100644 --- a/src/toxic.h +++ b/src/toxic.h @@ -122,6 +122,7 @@ void on_group_nick_change(Tox *m, int groupnumber, uint32_t peernumber, const ui void on_group_op_certificate(Tox *m, int groupnumber, uint32_t src_peernum, uint32_t tgt_peernum, uint8_t cert_type, void *userdata); void on_group_self_join(Tox *m, int groupnumber, void *userdata); void on_group_self_timeout(Tox *m, int groupnumber, void *userdata); +void on_group_rejected(Tox *m, int groupnumber, uint8_t type, void *userdata); #ifdef AUDIO void write_device_callback_group(Tox *m, int groupnum, int peernum, const int16_t *pcm, unsigned int samples, diff --git a/src/windows.c b/src/windows.c index bd25d27..2c5911f 100644 --- a/src/windows.c +++ b/src/windows.c @@ -305,6 +305,16 @@ void on_group_self_timeout(Tox *m, int groupnumber, void *userdata) } } +void on_group_rejected(Tox *m, int groupnumber, uint8_t type, void *userdata) +{ + int i; + + for (i = 0; i < MAX_WINDOWS_NUM; ++i) { + if (windows[i].onGroupRejected != NULL) + windows[i].onGroupRejected(&windows[i], m, groupnumber, type); + } +} + void on_file_sendrequest(Tox *m, int32_t friendnumber, uint8_t filenumber, uint64_t filesize, const uint8_t *filename, uint16_t filename_length, void *userdata) { diff --git a/src/windows.h b/src/windows.h index a368b6c..53f7cd5 100644 --- a/src/windows.h +++ b/src/windows.h @@ -134,6 +134,7 @@ struct ToxWindow { void(*onGroupTopicChange)(ToxWindow *, Tox *, int, uint32_t, const char *, uint16_t); void(*onGroupSelfJoin)(ToxWindow *, Tox *, int); void(*onGroupSelfTimeout)(ToxWindow *, Tox *, int); + void(*onGroupRejected)(ToxWindow *, Tox *, int, uint8_t); #ifdef AUDIO