aboutsummaryrefslogtreecommitdiff
path: root/src/gnunet_chat_context.c
blob: 3e6ab4d0f42cb2b14d3e2822fa9dd86929f02813 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
/*
   This file is part of GNUnet.
   Copyright (C) 2021--2024 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_context.c
 */

#include "gnunet_chat_context.h"
#include "gnunet_chat_file.h"
#include "gnunet_chat_handle.h"
#include "gnunet_chat_message.h"
#include "gnunet_chat_util.h"

#include "gnunet_chat_context_intern.c"
#include <gnunet/gnunet_common.h>
#include <gnunet/gnunet_messenger_service.h>
#include <gnunet/gnunet_namestore_service.h>
#include <gnunet/gnunet_scheduler_lib.h>
#include <string.h>

static const unsigned int initial_map_size_of_room = 8;
static const unsigned int initial_map_size_of_contact = 4;

static void
init_new_context (struct GNUNET_CHAT_Context *context,
                  unsigned int initial_map_size)
{
  GNUNET_assert(context);

  context->flags = 0;
  context->nick = NULL;
  context->topic = NULL;
  context->deleted = GNUNET_NO;

  context->request_task = NULL;

  context->timestamps = GNUNET_CONTAINER_multishortmap_create(
    initial_map_size, GNUNET_NO);
  context->dependencies = GNUNET_CONTAINER_multihashmap_create(
    initial_map_size, GNUNET_NO);
  context->messages = GNUNET_CONTAINER_multihashmap_create(
    initial_map_size, GNUNET_NO);
  context->requests = GNUNET_CONTAINER_multihashmap_create(
    initial_map_size, GNUNET_NO);
  context->taggings = GNUNET_CONTAINER_multihashmap_create(
    initial_map_size, GNUNET_NO);
  context->invites = GNUNET_CONTAINER_multihashmap_create(
    initial_map_size, GNUNET_NO);
  context->files = GNUNET_CONTAINER_multihashmap_create(
    initial_map_size, GNUNET_NO);
  
  context->user_pointer = NULL;

  context->member_pointers = GNUNET_CONTAINER_multishortmap_create(
    initial_map_size, GNUNET_NO);

  context->query = NULL;
}

struct GNUNET_CHAT_Context*
context_create_from_room (struct GNUNET_CHAT_Handle *handle,
			                    struct GNUNET_MESSENGER_Room *room)
{
  GNUNET_assert((handle) && (room));

  struct GNUNET_CHAT_Context* context = GNUNET_new(struct GNUNET_CHAT_Context);

  context->handle = handle;
  context->type = GNUNET_CHAT_CONTEXT_TYPE_UNKNOWN;
  
  init_new_context(context, initial_map_size_of_room);

  context->room = room;
  context->contact = NULL;

  return context;
}

struct GNUNET_CHAT_Context*
context_create_from_contact (struct GNUNET_CHAT_Handle *handle,
			                       const struct GNUNET_MESSENGER_Contact *contact)
{
  GNUNET_assert((handle) && (contact));

  struct GNUNET_CHAT_Context* context = GNUNET_new(struct GNUNET_CHAT_Context);

  context->handle = handle;
  context->type = GNUNET_CHAT_CONTEXT_TYPE_CONTACT;

  init_new_context(context, initial_map_size_of_contact);

  context->room = NULL;
  context->contact = contact;

  return context;
}

void
context_destroy (struct GNUNET_CHAT_Context *context)
{
  GNUNET_assert(
    (context) &&
		(context->timestamps) &&
    (context->dependencies) &&
		(context->messages) &&
    (context->taggings) &&
		(context->invites) &&
		(context->files)
  );

  if (context->request_task)
    GNUNET_SCHEDULER_cancel(context->request_task);

  if (context->query)
    GNUNET_NAMESTORE_cancel(context->query);

  GNUNET_CONTAINER_multishortmap_iterate(
    context->timestamps, it_destroy_context_timestamps, NULL
  );

  GNUNET_CONTAINER_multihashmap_clear(context->dependencies);
  GNUNET_CONTAINER_multihashmap_iterate(
    context->messages, it_destroy_context_messages, NULL
  );

  GNUNET_CONTAINER_multihashmap_iterate(
    context->taggings, it_destroy_context_taggings, NULL
  );

  GNUNET_CONTAINER_multihashmap_iterate(
    context->invites, it_destroy_context_invites, NULL
  );

  GNUNET_CONTAINER_multishortmap_destroy(context->member_pointers);

  GNUNET_CONTAINER_multishortmap_destroy(context->timestamps);
  GNUNET_CONTAINER_multihashmap_destroy(context->dependencies);
  GNUNET_CONTAINER_multihashmap_destroy(context->messages);
  GNUNET_CONTAINER_multihashmap_destroy(context->requests);
  GNUNET_CONTAINER_multihashmap_destroy(context->taggings);
  GNUNET_CONTAINER_multihashmap_destroy(context->invites);
  GNUNET_CONTAINER_multihashmap_destroy(context->files);

  if (context->topic)
    GNUNET_free(context->topic);

  GNUNET_free(context);
}

void
context_request_message (struct GNUNET_CHAT_Context* context,
                         const struct GNUNET_HashCode *hash)
{
  GNUNET_assert((context) && (hash));

  if (GNUNET_OK != GNUNET_CONTAINER_multihashmap_put(context->requests,
      hash, NULL, GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE))
    return;
  
  if (context->request_task)
    return;

  context->request_task = GNUNET_SCHEDULER_add_with_priority(
    GNUNET_SCHEDULER_PRIORITY_IDLE,
    cb_context_request_messages,
    context
  );
}

void
context_update_room (struct GNUNET_CHAT_Context *context,
		                 struct GNUNET_MESSENGER_Room *room)
{
  GNUNET_assert(context);

  if (room == context->room)
    return;

  GNUNET_CONTAINER_multishortmap_iterate(
    context->timestamps, it_destroy_context_timestamps, NULL
  );

  GNUNET_CONTAINER_multihashmap_iterate(
    context->messages, it_destroy_context_messages, NULL
  );

  GNUNET_CONTAINER_multihashmap_iterate(
    context->invites, it_destroy_context_invites, NULL
  );

  GNUNET_CONTAINER_multishortmap_destroy(context->timestamps);
  context->timestamps = GNUNET_CONTAINER_multishortmap_create(
    initial_map_size_of_room, GNUNET_NO);

  GNUNET_CONTAINER_multihashmap_clear(context->messages);
  GNUNET_CONTAINER_multihashmap_clear(context->requests);
  GNUNET_CONTAINER_multihashmap_clear(context->invites);
  GNUNET_CONTAINER_multihashmap_clear(context->files);

  context->room = room;

  if (!(context->room))
    return;

  context_write_records(context);
}

void
context_update_nick (struct GNUNET_CHAT_Context *context,
		                 const char *nick)
{
  GNUNET_assert(context);

  if (context->nick)
    GNUNET_free(context->nick);

  if (nick)
    context->nick = GNUNET_strdup(nick);
  else
    context->nick = NULL;

  if (!(context->handle))
    return;

  handle_send_internal_message(
    context->handle,
    NULL,
    context,
    GNUNET_CHAT_FLAG_UPDATE_CONTEXT,
    NULL
  );
}

void
context_read_records (struct GNUNET_CHAT_Context *context,
                      const char *label,
                      unsigned int count,
                      const struct GNUNET_GNSRECORD_Data *data)
{
  GNUNET_assert((context) && (context->room));

  char *nick = NULL;
  char *topic = NULL;
  uint32_t flags = 0;

  for (unsigned int i = 0; i < count; i++)
  {
    if (!(GNUNET_GNSRECORD_RF_SUPPLEMENTAL & data[i].flags))
      continue;

    if (GNUNET_GNSRECORD_TYPE_MESSENGER_ROOM_DETAILS == data[i].record_type)
    {
      if (nick)
	      continue;

      const struct GNUNET_MESSENGER_RoomDetailsRecord *record = data[i].data;

      nick = GNUNET_strndup(record->name, sizeof(record->name));
      flags = record->flags;
    }

    if (GNUNET_DNSPARSER_TYPE_TXT == data[i].record_type)
    {
      if (topic)
	      continue;

      topic = GNUNET_strndup(data[i].data, data[i].data_size);
    }
  }

  context->flags = flags;
  context_update_nick(context, nick);

  if (nick)
    GNUNET_free(nick);

  const struct GNUNET_HashCode *hash = GNUNET_MESSENGER_room_get_key(
    context->room
  );

  if (topic)
  {
    struct GNUNET_HashCode topic_hash;
    GNUNET_CRYPTO_hash(topic, strlen(topic), &topic_hash);

    if (0 != GNUNET_CRYPTO_hash_cmp(&topic_hash, hash))
    {
      GNUNET_free(topic);
      topic = NULL;
    }
  }

  util_set_name_field(topic, &(context->topic));

  if (topic)
    GNUNET_free(topic);

  context->type = util_get_context_label_type(label, hash);
}

void
context_delete_message (struct GNUNET_CHAT_Context *context,
                        const struct GNUNET_CHAT_Message *message)
{
  GNUNET_assert((context) && (message));

  if (GNUNET_YES != message_has_msg(message))
    return;

  switch (message->msg->header.kind)
  {
    case GNUNET_MESSENGER_KIND_INVITE:
    {
      struct GNUNET_CHAT_Invitation *invite = GNUNET_CONTAINER_multihashmap_get(
        context->invites, &(message->hash)
      );

      if (! invite)
        break;

      if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove(
        context->invites, &(message->hash), invite))
        invitation_destroy(invite);
      
      break;
    }
    case GNUNET_MESSENGER_KIND_FILE:
    {
      if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_contains(context->files, &(message->hash)))
        break;

      GNUNET_CONTAINER_multihashmap_remove_all(context->files, &(message->hash));
      break;
    }
    case GNUNET_MESSENGER_KIND_TAG:
    {
      struct GNUNET_CHAT_Tagging *tagging = GNUNET_CONTAINER_multihashmap_get(
        context->taggings,
        &(message->msg->body.tag.hash)
      );

      if (!tagging)
        break;

      tagging_remove(tagging, message);
      break;
    }
    default:
      break;
  }
}

void
context_write_records (struct GNUNET_CHAT_Context *context)
{
  GNUNET_assert((context) && (context->handle) && (context->room));

  const struct GNUNET_CRYPTO_PrivateKey *zone = handle_get_key(
    context->handle
  );

  if (!zone)
    return;

  const struct GNUNET_HashCode *hash = GNUNET_MESSENGER_room_get_key(
    context->room
  );

  struct GNUNET_TIME_Absolute expiration = GNUNET_TIME_absolute_get_forever_();

  struct GNUNET_MESSENGER_RoomEntryRecord room_entry;
  GNUNET_CRYPTO_get_peer_identity(context->handle->cfg, &(room_entry.door));

  GNUNET_memcpy(
    &(room_entry.key),
    hash,
    sizeof(room_entry.key)
  );

  struct GNUNET_MESSENGER_RoomDetailsRecord room_details;
  memset(room_details.name, 0, sizeof(room_details.name));
  
  const char *topic = context->topic;

  if (topic)
  {
    struct GNUNET_HashCode topic_hash;
    GNUNET_CRYPTO_hash(topic, strlen(topic), &topic_hash);

    if (0 != GNUNET_CRYPTO_hash_cmp(&topic_hash, hash))
      topic = NULL;
  }

  char *label;
  util_get_context_label(context->type, hash, &label);

  unsigned int count = 0;
  struct GNUNET_GNSRECORD_Data data [3];

  if (GNUNET_YES == context->deleted)
    goto skip_record_data;

  data[count].record_type = GNUNET_GNSRECORD_TYPE_MESSENGER_ROOM_ENTRY;
  data[count].data = &room_entry;
  data[count].data_size = sizeof(room_entry);
  data[count].expiration_time = expiration.abs_value_us;
  data[count].flags = GNUNET_GNSRECORD_RF_PRIVATE;
  count++;

  if (context->nick)
  {
    size_t name_len = strlen(context->nick);
    if (name_len >= sizeof(room_details.name))
      name_len = sizeof(room_details.name) - 1;

    GNUNET_memcpy(room_details.name, context->nick, name_len);
    room_details.name[name_len] = '\0';
  }

  if ((context->nick) || (context->flags != 0))
  {
    room_details.flags = context->flags;

    data[count].record_type = GNUNET_GNSRECORD_TYPE_MESSENGER_ROOM_DETAILS;
    data[count].data = &room_details;
    data[count].data_size = sizeof(room_details);
    data[count].expiration_time = expiration.abs_value_us;
    data[count].flags = (
      GNUNET_GNSRECORD_RF_PRIVATE |
      GNUNET_GNSRECORD_RF_SUPPLEMENTAL
    );

    count++;
  }

  if (topic)
  {
    data[count].record_type = GNUNET_DNSPARSER_TYPE_TXT;
    data[count].data = topic;
    data[count].data_size = strlen(topic);
    data[count].expiration_time = expiration.abs_value_us;
    data[count].flags = (
      GNUNET_GNSRECORD_RF_PRIVATE |
      GNUNET_GNSRECORD_RF_SUPPLEMENTAL
    );

    count++;
  }

skip_record_data:
  if (context->query)
    GNUNET_NAMESTORE_cancel(context->query);

  context->query = GNUNET_NAMESTORE_record_set_store(
    context->handle->namestore,
    zone,
    label,
    count,
    data,
    cont_context_write_records,
    context
  );

  GNUNET_free(label);
}