aboutsummaryrefslogtreecommitdiff
path: root/src/messenger/messenger_api_room.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/messenger/messenger_api_room.c')
-rw-r--r--src/messenger/messenger_api_room.c96
1 files changed, 81 insertions, 15 deletions
diff --git a/src/messenger/messenger_api_room.c b/src/messenger/messenger_api_room.c
index c3e8bc957..fb4e11fd4 100644
--- a/src/messenger/messenger_api_room.c
+++ b/src/messenger/messenger_api_room.c
@@ -1,6 +1,6 @@
1/* 1/*
2 This file is part of GNUnet. 2 This file is part of GNUnet.
3 Copyright (C) 2020--2021 GNUnet e.V. 3 Copyright (C) 2020--2023 GNUnet e.V.
4 4
5 GNUnet is free software: you can redistribute it and/or modify it 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 6 under the terms of the GNU Affero General Public License as published
@@ -39,14 +39,19 @@ create_room (struct GNUNET_MESSENGER_Handle *handle,
39 room->handle = handle; 39 room->handle = handle;
40 GNUNET_memcpy(&(room->key), key, sizeof(*key)); 40 GNUNET_memcpy(&(room->key), key, sizeof(*key));
41 41
42 memset(&(room->last_message), 0, sizeof(room->last_message));
43
42 room->opened = GNUNET_NO; 44 room->opened = GNUNET_NO;
43 room->contact_id = NULL; 45 room->use_handle_name = GNUNET_YES;
46 room->sender_id = NULL;
44 47
45 init_list_tunnels (&(room->entries)); 48 init_list_tunnels (&(room->entries));
46 49
47 room->messages = GNUNET_CONTAINER_multihashmap_create (8, GNUNET_NO); 50 room->messages = GNUNET_CONTAINER_multihashmap_create (8, GNUNET_NO);
48 room->members = GNUNET_CONTAINER_multishortmap_create (8, GNUNET_NO); 51 room->members = GNUNET_CONTAINER_multishortmap_create (8, GNUNET_NO);
49 52
53 init_queue_messages (&(room->queue));
54
50 return room; 55 return room;
51} 56}
52 57
@@ -68,6 +73,7 @@ destroy_room (struct GNUNET_MESSENGER_Room *room)
68{ 73{
69 GNUNET_assert(room); 74 GNUNET_assert(room);
70 75
76 clear_queue_messages(&(room->queue));
71 clear_list_tunnels (&(room->entries)); 77 clear_list_tunnels (&(room->entries));
72 78
73 if (room->messages) 79 if (room->messages)
@@ -80,12 +86,57 @@ destroy_room (struct GNUNET_MESSENGER_Room *room)
80 if (room->members) 86 if (room->members)
81 GNUNET_CONTAINER_multishortmap_destroy (room->members); 87 GNUNET_CONTAINER_multishortmap_destroy (room->members);
82 88
83 if (room->contact_id) 89 if (room->sender_id)
84 GNUNET_free(room->contact_id); 90 GNUNET_free(room->sender_id);
85 91
86 GNUNET_free(room); 92 GNUNET_free(room);
87} 93}
88 94
95enum GNUNET_GenericReturnValue
96is_room_available (const struct GNUNET_MESSENGER_Room *room)
97{
98 GNUNET_assert (room);
99
100 if (!get_room_sender_id(room))
101 return GNUNET_NO;
102
103 if ((GNUNET_YES == room->opened) || (room->entries.head))
104 return GNUNET_YES;
105 else
106 return GNUNET_NO;
107}
108
109const struct GNUNET_ShortHashCode*
110get_room_sender_id (const struct GNUNET_MESSENGER_Room *room)
111{
112 GNUNET_assert (room);
113
114 return room->sender_id;
115}
116
117void
118set_room_sender_id (struct GNUNET_MESSENGER_Room *room,
119 const struct GNUNET_ShortHashCode *id)
120{
121 GNUNET_assert (room);
122
123 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Set member id for room: %s\n", GNUNET_h2s (&(room->key)));
124
125 if (!id)
126 {
127 if (room->sender_id)
128 GNUNET_free (room->sender_id);
129
130 room->sender_id = NULL;
131 return;
132 }
133
134 if (!room->sender_id)
135 room->sender_id = GNUNET_new (struct GNUNET_ShortHashCode);
136
137 GNUNET_memcpy (room->sender_id, id, sizeof(struct GNUNET_ShortHashCode));
138}
139
89const struct GNUNET_MESSENGER_Message* 140const struct GNUNET_MESSENGER_Message*
90get_room_message (const struct GNUNET_MESSENGER_Room *room, 141get_room_message (const struct GNUNET_MESSENGER_Room *room,
91 const struct GNUNET_HashCode *hash) 142 const struct GNUNET_HashCode *hash)
@@ -146,9 +197,6 @@ handle_leave_message (struct GNUNET_MESSENGER_Room *room,
146 (GNUNET_YES != GNUNET_CONTAINER_multishortmap_remove(room->members, &(message->header.sender_id), sender))) 197 (GNUNET_YES != GNUNET_CONTAINER_multishortmap_remove(room->members, &(message->header.sender_id), sender)))
147 return; 198 return;
148 199
149 struct GNUNET_HashCode context;
150 get_context_from_member(&(room->key), &(message->header.sender_id), &context);
151
152 if (GNUNET_YES == decrease_contact_rc(sender)) 200 if (GNUNET_YES == decrease_contact_rc(sender))
153 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "A contact does not share any room with you anymore!\n"); 201 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "A contact does not share any room with you anymore!\n");
154} 202}
@@ -157,8 +205,19 @@ static void
157handle_name_message (struct GNUNET_MESSENGER_Room *room, 205handle_name_message (struct GNUNET_MESSENGER_Room *room,
158 struct GNUNET_MESSENGER_Contact *sender, 206 struct GNUNET_MESSENGER_Contact *sender,
159 const struct GNUNET_MESSENGER_Message *message, 207 const struct GNUNET_MESSENGER_Message *message,
160 const struct GNUNET_HashCode *hash) 208 const struct GNUNET_HashCode *hash,
209 enum GNUNET_MESSENGER_MessageFlags flags)
161{ 210{
211 if (GNUNET_MESSENGER_FLAG_SENT & flags)
212 {
213 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Set rule for using handle name in room: %s\n", GNUNET_h2s (&(room->key)));
214
215 const char* handle_name = get_handle_name (room->handle);
216
217 if ((handle_name) && (0 == strcmp (handle_name, message->body.name.name)))
218 room->use_handle_name = GNUNET_YES;
219 }
220
162 if (!sender) 221 if (!sender)
163 return; 222 return;
164 223
@@ -186,8 +245,12 @@ static void
186handle_id_message (struct GNUNET_MESSENGER_Room *room, 245handle_id_message (struct GNUNET_MESSENGER_Room *room,
187 struct GNUNET_MESSENGER_Contact *sender, 246 struct GNUNET_MESSENGER_Contact *sender,
188 const struct GNUNET_MESSENGER_Message *message, 247 const struct GNUNET_MESSENGER_Message *message,
189 const struct GNUNET_HashCode *hash) 248 const struct GNUNET_HashCode *hash,
249 enum GNUNET_MESSENGER_MessageFlags flags)
190{ 250{
251 if (GNUNET_MESSENGER_FLAG_SENT & flags)
252 set_room_sender_id (room, &(message->body.id.id));
253
191 if ((!sender) || 254 if ((!sender) ||
192 (GNUNET_YES != GNUNET_CONTAINER_multishortmap_remove(room->members, &(message->header.sender_id), sender)) || 255 (GNUNET_YES != GNUNET_CONTAINER_multishortmap_remove(room->members, &(message->header.sender_id), sender)) ||
193 (GNUNET_OK != GNUNET_CONTAINER_multishortmap_put(room->members, &(message->body.id.id), sender, 256 (GNUNET_OK != GNUNET_CONTAINER_multishortmap_put(room->members, &(message->body.id.id), sender,
@@ -207,9 +270,10 @@ static void
207handle_miss_message (struct GNUNET_MESSENGER_Room *room, 270handle_miss_message (struct GNUNET_MESSENGER_Room *room,
208 struct GNUNET_MESSENGER_Contact *sender, 271 struct GNUNET_MESSENGER_Contact *sender,
209 const struct GNUNET_MESSENGER_Message *message, 272 const struct GNUNET_MESSENGER_Message *message,
210 const struct GNUNET_HashCode *hash) 273 const struct GNUNET_HashCode *hash,
274 enum GNUNET_MESSENGER_MessageFlags flags)
211{ 275{
212 if ((room->contact_id) && (0 == GNUNET_memcmp(&(message->header.sender_id), room->contact_id))) 276 if (GNUNET_MESSENGER_FLAG_SENT & flags)
213 { 277 {
214 struct GNUNET_MESSENGER_ListTunnel *match = find_list_tunnels (&(room->entries), &(message->body.miss.peer), NULL); 278 struct GNUNET_MESSENGER_ListTunnel *match = find_list_tunnels (&(room->entries), &(message->body.miss.peer), NULL);
215 279
@@ -240,7 +304,8 @@ struct GNUNET_MESSENGER_Contact*
240handle_room_message (struct GNUNET_MESSENGER_Room *room, 304handle_room_message (struct GNUNET_MESSENGER_Room *room,
241 struct GNUNET_MESSENGER_Contact *sender, 305 struct GNUNET_MESSENGER_Contact *sender,
242 const struct GNUNET_MESSENGER_Message *message, 306 const struct GNUNET_MESSENGER_Message *message,
243 const struct GNUNET_HashCode *hash) 307 const struct GNUNET_HashCode *hash,
308 enum GNUNET_MESSENGER_MessageFlags flags)
244{ 309{
245 if (GNUNET_NO != GNUNET_CONTAINER_multihashmap_contains (room->messages, hash)) 310 if (GNUNET_NO != GNUNET_CONTAINER_multihashmap_contains (room->messages, hash))
246 return sender; 311 return sender;
@@ -254,16 +319,16 @@ handle_room_message (struct GNUNET_MESSENGER_Room *room,
254 handle_leave_message (room, sender, message, hash); 319 handle_leave_message (room, sender, message, hash);
255 break; 320 break;
256 case GNUNET_MESSENGER_KIND_NAME: 321 case GNUNET_MESSENGER_KIND_NAME:
257 handle_name_message (room, sender, message, hash); 322 handle_name_message (room, sender, message, hash, flags);
258 break; 323 break;
259 case GNUNET_MESSENGER_KIND_KEY: 324 case GNUNET_MESSENGER_KIND_KEY:
260 handle_key_message (room, sender, message, hash); 325 handle_key_message (room, sender, message, hash);
261 break; 326 break;
262 case GNUNET_MESSENGER_KIND_ID: 327 case GNUNET_MESSENGER_KIND_ID:
263 handle_id_message (room, sender, message, hash); 328 handle_id_message (room, sender, message, hash, flags);
264 break; 329 break;
265 case GNUNET_MESSENGER_KIND_MISS: 330 case GNUNET_MESSENGER_KIND_MISS:
266 handle_miss_message (room, sender, message, hash); 331 handle_miss_message (room, sender, message, hash, flags);
267 break; 332 break;
268 case GNUNET_MESSENGER_KIND_DELETE: 333 case GNUNET_MESSENGER_KIND_DELETE:
269 handle_delete_message (room, sender, message, hash); 334 handle_delete_message (room, sender, message, hash);
@@ -287,6 +352,7 @@ handle_room_message (struct GNUNET_MESSENGER_Room *room,
287 GNUNET_free(entry); 352 GNUNET_free(entry);
288 } 353 }
289 354
355 GNUNET_memcpy (&(room->last_message), hash, sizeof(room->last_message));
290 return sender; 356 return sender;
291} 357}
292 358