commit 5f187deccd1685385b03e20d7f3b55f600adf6ac
parent 1edfdc77b42d647384c7ef09e2a2b0a59e69633a
Author: TheJackiMonster <thejackimonster@gmail.com>
Date: Fri, 3 Dec 2021 22:40:43 +0100
Adjusted invitations to groups to check for common contexts
Signed-off-by: TheJackiMonster <thejackimonster@gmail.com>
Diffstat:
6 files changed, 120 insertions(+), 75 deletions(-)
diff --git a/src/gnunet_chat_contact.c b/src/gnunet_chat_contact.c
@@ -23,6 +23,10 @@
*/
#include "gnunet_chat_contact.h"
+#include "gnunet_chat_context.h"
+#include "gnunet_chat_handle.h"
+
+#include "gnunet_chat_contact_intern.c"
struct GNUNET_CHAT_Contact*
contact_create_from_member (struct GNUNET_CHAT_Handle *handle,
@@ -64,6 +68,65 @@ contact_update_key (struct GNUNET_CHAT_Contact *contact)
contact->public_key = GNUNET_IDENTITY_public_key_to_string(pubkey);
}
+struct GNUNET_CHAT_Context*
+contact_find_context (struct GNUNET_CHAT_Contact *contact)
+{
+ GNUNET_assert(contact);
+
+ if (contact->context)
+ return contact->context;
+
+ struct GNUNET_CHAT_ContactFindRoom find;
+ find.room = NULL;
+ GNUNET_MESSENGER_find_rooms(
+ contact->handle->messenger,
+ contact->member,
+ it_contact_find_room,
+ &find
+ );
+
+ // TODO: Check if the found room is a group or not
+
+ if (!(find.room))
+ return NULL;
+
+ struct GNUNET_HashCode key;
+ GNUNET_CRYPTO_random_block(GNUNET_CRYPTO_QUALITY_WEAK, &key, sizeof(key));
+
+ if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains(
+ contact->handle->contexts, &key))
+ return NULL;
+
+ struct GNUNET_MESSENGER_Room *room = GNUNET_MESSENGER_open_room(
+ contact->handle->messenger, &key
+ );
+
+ if (!room)
+ return NULL;
+
+ struct GNUNET_CHAT_Context *context = context_create_from_room(
+ contact->handle, room
+ );
+
+ if (GNUNET_OK != GNUNET_CONTAINER_multihashmap_put(
+ contact->handle->contexts, &key, context,
+ GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
+ {
+ context_destroy(context);
+ return NULL;
+ }
+
+ struct GNUNET_MESSENGER_Message msg;
+ msg.header.kind = GNUNET_MESSENGER_KIND_INVITE;
+ GNUNET_CRYPTO_get_peer_identity(contact->handle->cfg, &(msg.body.invite.door));
+ GNUNET_memcpy(&(msg.body.invite.key), &key, sizeof(msg.body.invite.key));
+
+ GNUNET_MESSENGER_send_message(find.room, &msg, contact->member);
+
+ contact->context = context;
+ return contact->context;
+}
+
void
contact_destroy (struct GNUNET_CHAT_Contact* contact)
{
diff --git a/src/gnunet_chat_contact.h b/src/gnunet_chat_contact.h
@@ -51,6 +51,9 @@ contact_create_from_member (struct GNUNET_CHAT_Handle *handle,
void
contact_update_key (struct GNUNET_CHAT_Contact *contact);
+struct GNUNET_CHAT_Context*
+contact_find_context (struct GNUNET_CHAT_Contact *contact);
+
void
contact_destroy (struct GNUNET_CHAT_Contact* contact);
diff --git a/src/gnunet_chat_contact_intern.c b/src/gnunet_chat_contact_intern.c
@@ -0,0 +1,42 @@
+/*
+ This file is part of GNUnet.
+ Copyright (C) 2021 GNUnet e.V.
+
+ GNUnet is free software: you can redistribute it and/or modify it
+ under the terms of the GNU Affero General Public License as published
+ by the Free Software Foundation, either version 3 of the License,
+ or (at your option) any later version.
+
+ GNUnet is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Affero General Public License for more details.
+
+ You should have received a copy of the GNU Affero General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+ SPDX-License-Identifier: AGPL3.0-or-later
+ */
+/*
+ * @author Tobias Frisch
+ * @file gnunet_chat_contact_intern.c
+ */
+
+#define GNUNET_UNUSED __attribute__ ((unused))
+
+struct GNUNET_CHAT_ContactFindRoom
+{
+ struct GNUNET_MESSENGER_Room *room;
+};
+
+int
+it_contact_find_room (void *cls,
+ struct GNUNET_MESSENGER_Room *room,
+ GNUNET_UNUSED const struct GNUNET_MESSENGER_Contact *member)
+{
+ GNUNET_assert((cls) && (room));
+
+ struct GNUNET_CHAT_ContactFindRoom *find = cls;
+ find->room = room;
+ return GNUNET_NO;
+}
diff --git a/src/gnunet_chat_handle_intern.c b/src/gnunet_chat_handle_intern.c
@@ -306,13 +306,13 @@ request_handle_context_by_room (struct GNUNET_CHAT_Handle *handle,
room, check_handle_room_members, &check
);
- if (check.contact)
+ if ((check.contact) &&
+ (GNUNET_OK == intern_provide_contact_for_member(handle,
+ check.contact,
+ context)))
{
context->type = GNUNET_CHAT_CONTEXT_TYPE_CONTACT;
-
- if (GNUNET_OK == intern_provide_contact_for_member(
- handle, check.contact, context))
- return GNUNET_OK;
+ return GNUNET_OK;
}
else if (checks >= 2)
{
diff --git a/src/gnunet_chat_lib.c b/src/gnunet_chat_lib.c
@@ -293,58 +293,7 @@ GNUNET_CHAT_contact_get_context (struct GNUNET_CHAT_Contact *contact)
if (!contact)
return NULL;
- if (contact->context)
- return contact->context;
-
- struct GNUNET_CHAT_ContactFindRoom find;
- find.room = NULL;
- GNUNET_MESSENGER_find_rooms(
- contact->handle->messenger,
- contact->member,
- it_contact_find_room,
- &find
- );
-
- // TODO: Check if the found room is a group or not
-
- if (!(find.room))
- return NULL;
-
- struct GNUNET_HashCode key;
- GNUNET_CRYPTO_random_block(GNUNET_CRYPTO_QUALITY_WEAK, &key, sizeof(key));
-
- if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains(
- contact->handle->contexts, &key))
- return NULL;
-
- struct GNUNET_MESSENGER_Room *room = GNUNET_MESSENGER_open_room(
- contact->handle->messenger, &key
- );
-
- if (!room)
- return NULL;
-
- struct GNUNET_CHAT_Context *context = context_create_from_room(
- contact->handle, room
- );
-
- if (GNUNET_OK != GNUNET_CONTAINER_multihashmap_put(
- contact->handle->contexts, &key, context,
- GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
- {
- context_destroy(context);
- return NULL;
- }
-
- struct GNUNET_MESSENGER_Message msg;
- msg.header.kind = GNUNET_MESSENGER_KIND_INVITE;
- GNUNET_CRYPTO_get_peer_identity(contact->handle->cfg, &(msg.body.invite.door));
- GNUNET_memcpy(&(msg.body.invite.key), &key, sizeof(msg.body.invite.key));
-
- GNUNET_MESSENGER_send_message(find.room, &msg, contact->member);
-
- contact->context = context;
- return contact->context;
+ return contact_find_context(contact);
}
@@ -444,6 +393,11 @@ GNUNET_CHAT_group_invite_contact (struct GNUNET_CHAT_Group *group,
if ((!group) || (!contact))
return;
+ struct GNUNET_CHAT_Context *context = contact_find_context(contact);
+
+ if (!context)
+ return;
+
const struct GNUNET_HashCode *key = GNUNET_MESSENGER_room_get_key(
group->context->room
);
@@ -457,7 +411,7 @@ GNUNET_CHAT_group_invite_contact (struct GNUNET_CHAT_Group *group,
GNUNET_CRYPTO_get_peer_identity(group->handle->cfg, &(msg.body.invite.door));
GNUNET_memcpy(&(msg.body.invite.key), key, sizeof(msg.body.invite.key));
- GNUNET_MESSENGER_send_message(contact->context->room, &msg, contact->member);
+ GNUNET_MESSENGER_send_message(context->room, &msg, contact->member);
}
diff --git a/src/gnunet_chat_lib_intern.c b/src/gnunet_chat_lib_intern.c
@@ -93,23 +93,6 @@ it_handle_iterate_groups (void *cls,
return it->cb(it->cls, it->handle, group);
}
-struct GNUNET_CHAT_ContactFindRoom
-{
- struct GNUNET_MESSENGER_Room *room;
-};
-
-int
-it_contact_find_room (void *cls,
- struct GNUNET_MESSENGER_Room *room,
- GNUNET_UNUSED const struct GNUNET_MESSENGER_Contact *member)
-{
- GNUNET_assert((cls) && (room));
-
- struct GNUNET_CHAT_ContactFindRoom *find = cls;
- find->room = room;
- return GNUNET_NO;
-}
-
struct GNUNET_CHAT_RoomFindContact
{
const struct GNUNET_MESSENGER_Contact *contact;