aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTheJackiMonster <thejackimonster@gmail.com>2021-12-03 22:40:43 +0100
committerTheJackiMonster <thejackimonster@gmail.com>2021-12-03 22:40:43 +0100
commit5f187deccd1685385b03e20d7f3b55f600adf6ac (patch)
tree20fff12d3aa02d98a6a9a7ab4aca326aae79c50c
parent1edfdc77b42d647384c7ef09e2a2b0a59e69633a (diff)
downloadlibgnunetchat-5f187deccd1685385b03e20d7f3b55f600adf6ac.tar.gz
libgnunetchat-5f187deccd1685385b03e20d7f3b55f600adf6ac.zip
Adjusted invitations to groups to check for common contexts
Signed-off-by: TheJackiMonster <thejackimonster@gmail.com>
-rw-r--r--src/gnunet_chat_contact.c63
-rw-r--r--src/gnunet_chat_contact.h3
-rw-r--r--src/gnunet_chat_contact_intern.c42
-rw-r--r--src/gnunet_chat_handle_intern.c10
-rw-r--r--src/gnunet_chat_lib.c60
-rw-r--r--src/gnunet_chat_lib_intern.c17
6 files changed, 120 insertions, 75 deletions
diff --git a/src/gnunet_chat_contact.c b/src/gnunet_chat_contact.c
index a955088..190c13a 100644
--- a/src/gnunet_chat_contact.c
+++ b/src/gnunet_chat_contact.c
@@ -23,6 +23,10 @@
23 */ 23 */
24 24
25#include "gnunet_chat_contact.h" 25#include "gnunet_chat_contact.h"
26#include "gnunet_chat_context.h"
27#include "gnunet_chat_handle.h"
28
29#include "gnunet_chat_contact_intern.c"
26 30
27struct GNUNET_CHAT_Contact* 31struct GNUNET_CHAT_Contact*
28contact_create_from_member (struct GNUNET_CHAT_Handle *handle, 32contact_create_from_member (struct GNUNET_CHAT_Handle *handle,
@@ -64,6 +68,65 @@ contact_update_key (struct GNUNET_CHAT_Contact *contact)
64 contact->public_key = GNUNET_IDENTITY_public_key_to_string(pubkey); 68 contact->public_key = GNUNET_IDENTITY_public_key_to_string(pubkey);
65} 69}
66 70
71struct GNUNET_CHAT_Context*
72contact_find_context (struct GNUNET_CHAT_Contact *contact)
73{
74 GNUNET_assert(contact);
75
76 if (contact->context)
77 return contact->context;
78
79 struct GNUNET_CHAT_ContactFindRoom find;
80 find.room = NULL;
81 GNUNET_MESSENGER_find_rooms(
82 contact->handle->messenger,
83 contact->member,
84 it_contact_find_room,
85 &find
86 );
87
88 // TODO: Check if the found room is a group or not
89
90 if (!(find.room))
91 return NULL;
92
93 struct GNUNET_HashCode key;
94 GNUNET_CRYPTO_random_block(GNUNET_CRYPTO_QUALITY_WEAK, &key, sizeof(key));
95
96 if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains(
97 contact->handle->contexts, &key))
98 return NULL;
99
100 struct GNUNET_MESSENGER_Room *room = GNUNET_MESSENGER_open_room(
101 contact->handle->messenger, &key
102 );
103
104 if (!room)
105 return NULL;
106
107 struct GNUNET_CHAT_Context *context = context_create_from_room(
108 contact->handle, room
109 );
110
111 if (GNUNET_OK != GNUNET_CONTAINER_multihashmap_put(
112 contact->handle->contexts, &key, context,
113 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
114 {
115 context_destroy(context);
116 return NULL;
117 }
118
119 struct GNUNET_MESSENGER_Message msg;
120 msg.header.kind = GNUNET_MESSENGER_KIND_INVITE;
121 GNUNET_CRYPTO_get_peer_identity(contact->handle->cfg, &(msg.body.invite.door));
122 GNUNET_memcpy(&(msg.body.invite.key), &key, sizeof(msg.body.invite.key));
123
124 GNUNET_MESSENGER_send_message(find.room, &msg, contact->member);
125
126 contact->context = context;
127 return contact->context;
128}
129
67void 130void
68contact_destroy (struct GNUNET_CHAT_Contact* contact) 131contact_destroy (struct GNUNET_CHAT_Contact* contact)
69{ 132{
diff --git a/src/gnunet_chat_contact.h b/src/gnunet_chat_contact.h
index 725e8fe..68078e0 100644
--- 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,
51void 51void
52contact_update_key (struct GNUNET_CHAT_Contact *contact); 52contact_update_key (struct GNUNET_CHAT_Contact *contact);
53 53
54struct GNUNET_CHAT_Context*
55contact_find_context (struct GNUNET_CHAT_Contact *contact);
56
54void 57void
55contact_destroy (struct GNUNET_CHAT_Contact* contact); 58contact_destroy (struct GNUNET_CHAT_Contact* contact);
56 59
diff --git a/src/gnunet_chat_contact_intern.c b/src/gnunet_chat_contact_intern.c
new file mode 100644
index 0000000..3e89663
--- /dev/null
+++ b/src/gnunet_chat_contact_intern.c
@@ -0,0 +1,42 @@
1/*
2 This file is part of GNUnet.
3 Copyright (C) 2021 GNUnet e.V.
4
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version.
9
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details.
14
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 SPDX-License-Identifier: AGPL3.0-or-later
19 */
20/*
21 * @author Tobias Frisch
22 * @file gnunet_chat_contact_intern.c
23 */
24
25#define GNUNET_UNUSED __attribute__ ((unused))
26
27struct GNUNET_CHAT_ContactFindRoom
28{
29 struct GNUNET_MESSENGER_Room *room;
30};
31
32int
33it_contact_find_room (void *cls,
34 struct GNUNET_MESSENGER_Room *room,
35 GNUNET_UNUSED const struct GNUNET_MESSENGER_Contact *member)
36{
37 GNUNET_assert((cls) && (room));
38
39 struct GNUNET_CHAT_ContactFindRoom *find = cls;
40 find->room = room;
41 return GNUNET_NO;
42}
diff --git a/src/gnunet_chat_handle_intern.c b/src/gnunet_chat_handle_intern.c
index 6e158db..7538814 100644
--- 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,
306 room, check_handle_room_members, &check 306 room, check_handle_room_members, &check
307 ); 307 );
308 308
309 if (check.contact) 309 if ((check.contact) &&
310 (GNUNET_OK == intern_provide_contact_for_member(handle,
311 check.contact,
312 context)))
310 { 313 {
311 context->type = GNUNET_CHAT_CONTEXT_TYPE_CONTACT; 314 context->type = GNUNET_CHAT_CONTEXT_TYPE_CONTACT;
312 315 return GNUNET_OK;
313 if (GNUNET_OK == intern_provide_contact_for_member(
314 handle, check.contact, context))
315 return GNUNET_OK;
316 } 316 }
317 else if (checks >= 2) 317 else if (checks >= 2)
318 { 318 {
diff --git a/src/gnunet_chat_lib.c b/src/gnunet_chat_lib.c
index 01a9c78..1d12bdd 100644
--- 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)
293 if (!contact) 293 if (!contact)
294 return NULL; 294 return NULL;
295 295
296 if (contact->context) 296 return contact_find_context(contact);
297 return contact->context;
298
299 struct GNUNET_CHAT_ContactFindRoom find;
300 find.room = NULL;
301 GNUNET_MESSENGER_find_rooms(
302 contact->handle->messenger,
303 contact->member,
304 it_contact_find_room,
305 &find
306 );
307
308 // TODO: Check if the found room is a group or not
309
310 if (!(find.room))
311 return NULL;
312
313 struct GNUNET_HashCode key;
314 GNUNET_CRYPTO_random_block(GNUNET_CRYPTO_QUALITY_WEAK, &key, sizeof(key));
315
316 if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains(
317 contact->handle->contexts, &key))
318 return NULL;
319
320 struct GNUNET_MESSENGER_Room *room = GNUNET_MESSENGER_open_room(
321 contact->handle->messenger, &key
322 );
323
324 if (!room)
325 return NULL;
326
327 struct GNUNET_CHAT_Context *context = context_create_from_room(
328 contact->handle, room
329 );
330
331 if (GNUNET_OK != GNUNET_CONTAINER_multihashmap_put(
332 contact->handle->contexts, &key, context,
333 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
334 {
335 context_destroy(context);
336 return NULL;
337 }
338
339 struct GNUNET_MESSENGER_Message msg;
340 msg.header.kind = GNUNET_MESSENGER_KIND_INVITE;
341 GNUNET_CRYPTO_get_peer_identity(contact->handle->cfg, &(msg.body.invite.door));
342 GNUNET_memcpy(&(msg.body.invite.key), &key, sizeof(msg.body.invite.key));
343
344 GNUNET_MESSENGER_send_message(find.room, &msg, contact->member);
345
346 contact->context = context;
347 return contact->context;
348} 297}
349 298
350 299
@@ -444,6 +393,11 @@ GNUNET_CHAT_group_invite_contact (struct GNUNET_CHAT_Group *group,
444 if ((!group) || (!contact)) 393 if ((!group) || (!contact))
445 return; 394 return;
446 395
396 struct GNUNET_CHAT_Context *context = contact_find_context(contact);
397
398 if (!context)
399 return;
400
447 const struct GNUNET_HashCode *key = GNUNET_MESSENGER_room_get_key( 401 const struct GNUNET_HashCode *key = GNUNET_MESSENGER_room_get_key(
448 group->context->room 402 group->context->room
449 ); 403 );
@@ -457,7 +411,7 @@ GNUNET_CHAT_group_invite_contact (struct GNUNET_CHAT_Group *group,
457 GNUNET_CRYPTO_get_peer_identity(group->handle->cfg, &(msg.body.invite.door)); 411 GNUNET_CRYPTO_get_peer_identity(group->handle->cfg, &(msg.body.invite.door));
458 GNUNET_memcpy(&(msg.body.invite.key), key, sizeof(msg.body.invite.key)); 412 GNUNET_memcpy(&(msg.body.invite.key), key, sizeof(msg.body.invite.key));
459 413
460 GNUNET_MESSENGER_send_message(contact->context->room, &msg, contact->member); 414 GNUNET_MESSENGER_send_message(context->room, &msg, contact->member);
461} 415}
462 416
463 417
diff --git a/src/gnunet_chat_lib_intern.c b/src/gnunet_chat_lib_intern.c
index b010607..a770367 100644
--- a/src/gnunet_chat_lib_intern.c
+++ b/src/gnunet_chat_lib_intern.c
@@ -93,23 +93,6 @@ it_handle_iterate_groups (void *cls,
93 return it->cb(it->cls, it->handle, group); 93 return it->cb(it->cls, it->handle, group);
94} 94}
95 95
96struct GNUNET_CHAT_ContactFindRoom
97{
98 struct GNUNET_MESSENGER_Room *room;
99};
100
101int
102it_contact_find_room (void *cls,
103 struct GNUNET_MESSENGER_Room *room,
104 GNUNET_UNUSED const struct GNUNET_MESSENGER_Contact *member)
105{
106 GNUNET_assert((cls) && (room));
107
108 struct GNUNET_CHAT_ContactFindRoom *find = cls;
109 find->room = room;
110 return GNUNET_NO;
111}
112
113struct GNUNET_CHAT_RoomFindContact 96struct GNUNET_CHAT_RoomFindContact
114{ 97{
115 const struct GNUNET_MESSENGER_Contact *contact; 98 const struct GNUNET_MESSENGER_Contact *contact;