Skip to content

Commit

Permalink
Refactor some Messenger code
Browse files Browse the repository at this point in the history
Separate m_add_contact_no_request into two functions and remove some redundant public key
checks that always evaluate to false
  • Loading branch information
JFreegman authored and iphydf committed May 1, 2020
1 parent e968e2f commit d03bc75
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 24 deletions.
25 changes: 9 additions & 16 deletions toxcore/Messenger.c
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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;
Expand All @@ -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);
}

Expand All @@ -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);
Expand All @@ -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;
Expand Down
6 changes: 0 additions & 6 deletions toxcore/Messenger.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -109,7 +104,6 @@ typedef enum Friend_Add_Error {
FAERR_BADCHECKSUM = -6,
FAERR_SETNEWNOSPAM = -7,
FAERR_NOMEM = -8,
FAERR_BADCONTYPE = -9,
} Friend_Add_Error;


Expand Down
4 changes: 2 additions & 2 deletions toxcore/group_chats.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down

0 comments on commit d03bc75

Please sign in to comment.