From d03bc75146f212159a28c9df0960180b64efe5ef Mon Sep 17 00:00:00 2001 From: jfreegman Date: Fri, 1 May 2020 14:54:22 -0400 Subject: [PATCH] Refactor some Messenger code Separate m_add_contact_no_request into two functions and remove some redundant public key checks that always evaluate to false --- toxcore/Messenger.c | 25 +++++++++---------------- toxcore/Messenger.h | 6 ------ toxcore/group_chats.h | 4 ++-- 3 files changed, 11 insertions(+), 24 deletions(-) diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c index 6acb5bc6249..50a4bb794d4 100644 --- a/toxcore/Messenger.c +++ b/toxcore/Messenger.c @@ -41,7 +41,7 @@ bool friend_is_valid(const Messenger *m, int32_t friendnumber) static bool group_is_valid(const Messenger *m, int32_t groupnumber) { - return groupnumber < m->numgroups; + return (unsigned int)groupnumber < m->numgroups && m->grouplist[groupnumber].active; } /* Set the size of the friend list to numfriends. @@ -368,7 +368,7 @@ int32_t m_addfriend(Messenger *m, const uint8_t *address, const uint8_t *data, u return ret; } -static int32_t m_add_contact_no_request(Messenger *m, const uint8_t *real_pk, Contact_Type type) +static int32_t m_add_friend_contact_no_request(Messenger *m, const uint8_t *real_pk) { if (getfriend_id(m, real_pk) != -1) { return FAERR_ALREADYSENT; @@ -378,22 +378,15 @@ static int32_t m_add_contact_no_request(Messenger *m, const uint8_t *real_pk, Co return FAERR_OWNKEY; } - if (type == CONTACT_FRIEND) { - return init_new_friend(m, real_pk, FRIEND_CONFIRMED); - } - - if (type != CONTACT_GROUP) { - return FAERR_BADCONTYPE; - } - -#ifndef VANILLA_NACL + return init_new_friend(m, real_pk, FRIEND_CONFIRMED); +} - if (gc_get_group_by_public_key(m->group_handler, real_pk)) { +static int32_t m_add_group_contact_no_request(Messenger *m, const uint8_t *real_pk) +{ + if (get_group_id(m, real_pk) != -1) { return FAERR_ALREADYSENT; } -#endif - return init_new_group(m, real_pk); } @@ -403,7 +396,7 @@ int32_t m_addfriend_norequest(Messenger *m, const uint8_t *real_pk) return FAERR_BADCHECKSUM; } - return m_add_contact_no_request(m, real_pk, CONTACT_FRIEND); + return m_add_friend_contact_no_request(m, real_pk); } static void try_pack_gc_data(const Messenger *m, GC_Chat *chat, Onion_Friend *onion_friend); @@ -418,7 +411,7 @@ int32_t m_add_group(Messenger *m, GC_Chat *chat) { random_bytes(chat->m_group_public_key, CRYPTO_PUBLIC_KEY_SIZE); - int group_number = m_add_contact_no_request(m, chat->m_group_public_key, CONTACT_GROUP); + int group_number = m_add_group_contact_no_request(m, chat->m_group_public_key); if (group_number < 0) { return -1; diff --git a/toxcore/Messenger.h b/toxcore/Messenger.h index 06ae73390e7..693838fbc3b 100644 --- a/toxcore/Messenger.h +++ b/toxcore/Messenger.h @@ -38,11 +38,6 @@ typedef enum Message_Type { MESSAGE_ACTION, } Message_Type; -typedef enum Contact_Type { - CONTACT_FRIEND, - CONTACT_GROUP, -} Contact_Type; - #ifndef MESSENGER_DEFINED #define MESSENGER_DEFINED typedef struct Messenger Messenger; @@ -109,7 +104,6 @@ typedef enum Friend_Add_Error { FAERR_BADCHECKSUM = -6, FAERR_SETNEWNOSPAM = -7, FAERR_NOMEM = -8, - FAERR_BADCONTYPE = -9, } Friend_Add_Error; diff --git a/toxcore/group_chats.h b/toxcore/group_chats.h index af2a2319bf2..869fe39fe4b 100644 --- a/toxcore/group_chats.h +++ b/toxcore/group_chats.h @@ -291,12 +291,12 @@ typedef struct GC_Chat { /* keeps track of frequency of new inbound connections */ uint8_t connection_O_metre; uint64_t connection_cooldown_timer; - bool block_handshakes; // TODO: ??? + bool block_handshakes; int32_t saved_invites[MAX_GC_SAVED_INVITES]; uint8_t saved_invites_index; - uint8_t m_group_public_key[CRYPTO_PUBLIC_KEY_SIZE]; // Identifier for group's messenger friend connection + uint8_t m_group_public_key[CRYPTO_PUBLIC_KEY_SIZE]; /* Identifier for group's messenger friend connection */ bool should_update_self_announces; bool should_start_sending_handshakes;