aboutsummaryrefslogtreecommitdiff
path: root/src/gnunet_chat_handle_intern.c
blob: 17bff20e29639034eb917e254480a45ed7824413 (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
/*
   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_handle_intern.c
 */

#include "gnunet_chat_contact.h"
#include "gnunet_chat_context.h"
#include "gnunet_chat_file.h"
#include "gnunet_chat_group.h"
#include "gnunet_chat_handle.h"
#include "gnunet_chat_invitation.h"
#include "gnunet_chat_message.h"
#include "gnunet_chat_util.h"

#define GNUNET_UNUSED __attribute__ ((unused))

void
on_handle_arm_connection(void *cls, int connected)
{
  struct GNUNET_CHAT_Handle *chat = cls;

  if (GNUNET_YES == connected) {
    GNUNET_ARM_request_service_start(
	chat->arm, "messenger",
	GNUNET_OS_INHERIT_STD_NONE,
	NULL, NULL
    );

    GNUNET_ARM_request_service_start(
	chat->arm, "fs",
	GNUNET_OS_INHERIT_STD_NONE,
	NULL, NULL
    );
  } else {
    GNUNET_ARM_request_service_start(
	chat->arm, "arm",
	GNUNET_OS_INHERIT_STD_NONE,
	NULL, NULL
    );
  }
}

void*
notify_handle_fs_progress(void* cls, const struct GNUNET_FS_ProgressInfo* info)
{
  struct GNUNET_CHAT_Handle *chat = cls;

  if (!chat)
    return NULL;

  switch (info->status) {
    case GNUNET_FS_STATUS_PUBLISH_START: {
      struct GNUNET_CHAT_File *file = info->value.publish.cctx;

      file_update_upload(
	  file,
	  0,
	  info->value.publish.size
      );

      return file;
    } case GNUNET_FS_STATUS_PUBLISH_PROGRESS: {
      struct GNUNET_CHAT_File *file = info->value.publish.cctx;

      file_update_upload(
	  file,
	  info->value.publish.completed,
	  info->value.publish.size
      );

      return file;
    } case GNUNET_FS_STATUS_PUBLISH_COMPLETED: {
      struct GNUNET_CHAT_File *file = info->value.publish.cctx;

      file->uri = GNUNET_FS_uri_dup(
	  info->value.publish.specifics.completed.chk_uri
      );

      file_update_upload(
      	  file,
	  info->value.publish.size,
      	  info->value.publish.size
      );

      file->publish = NULL;
      break;
    } case GNUNET_FS_STATUS_PUBLISH_ERROR: {
      break;
    } case GNUNET_FS_STATUS_DOWNLOAD_START: {
      struct GNUNET_CHAT_File *file = info->value.download.cctx;

      file_update_download(
	  file,
      	  0,
      	  info->value.download.size
      );

      return file;
    } case GNUNET_FS_STATUS_DOWNLOAD_ACTIVE: {
      return info->value.download.cctx;
    } case GNUNET_FS_STATUS_DOWNLOAD_INACTIVE: {
      return info->value.download.cctx;
    } case GNUNET_FS_STATUS_DOWNLOAD_PROGRESS: {
      struct GNUNET_CHAT_File *file = info->value.download.cctx;

      file_update_download(
      	  file,
	  info->value.download.completed,
	  info->value.download.size
      );

      return file;
    } case GNUNET_FS_STATUS_DOWNLOAD_COMPLETED: {
      struct GNUNET_CHAT_File *file = info->value.download.cctx;

      file_update_download(
	  file,
	  info->value.download.size,
	  info->value.download.size
      );

      file->download = NULL;
      break;
    } case GNUNET_FS_STATUS_DOWNLOAD_ERROR: {
      break;
    } case GNUNET_FS_STATUS_UNINDEX_START: {
      struct GNUNET_CHAT_File *file = info->value.unindex.cctx;

      file_update_unindex(
	  file,
      	  0,
      	  info->value.unindex.size
      );

      return file;
    } case GNUNET_FS_STATUS_UNINDEX_PROGRESS: {
      struct GNUNET_CHAT_File *file = info->value.unindex.cctx;

      file_update_unindex(
      	  file,
	  info->value.unindex.completed,
	  info->value.unindex.size
      );

      return file;
    } case GNUNET_FS_STATUS_UNINDEX_COMPLETED: {
      struct GNUNET_CHAT_File *file = info->value.unindex.cctx;

      file_update_unindex(
	  file,
      	  info->value.unindex.size,
      	  info->value.unindex.size
      );

      file->unindex = NULL;
      break;
    } default: {
      break;
    }
  }

  return NULL;
}

struct GNUNET_CHAT_CheckHandleRoomMembers
{
  const struct GNUNET_IDENTITY_PublicKey *ignore_key;
  const struct GNUNET_MESSENGER_Contact *contact;
};

int
check_handle_room_members (void* cls,
			   GNUNET_UNUSED struct GNUNET_MESSENGER_Room *room,
                           const struct GNUNET_MESSENGER_Contact *contact)
{
  struct GNUNET_CHAT_CheckHandleRoomMembers *check = cls;

  const struct GNUNET_IDENTITY_PublicKey *contact_key = (
      GNUNET_MESSENGER_contact_get_key(contact)
  );

  if (0 == GNUNET_memcmp(contact_key, check->ignore_key))
    return GNUNET_YES;

  if (check->contact)
    return GNUNET_NO;

  check->contact = contact;
  return GNUNET_YES;
}

struct GNUNET_CHAT_Context*
request_handle_context_by_room (struct GNUNET_CHAT_Handle *handle,
				struct GNUNET_MESSENGER_Room *room)
{
  const struct GNUNET_HashCode *key = GNUNET_MESSENGER_room_get_key(room);

  struct GNUNET_CHAT_Context *context = GNUNET_CONTAINER_multihashmap_get(
      handle->contexts, key
  );

  if (context)
    return context;

  context = context_create_from_room(handle, room);
  context_load_config(context);

  if (GNUNET_OK != GNUNET_CONTAINER_multihashmap_put(
      handle->contexts, key, context,
      GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
  {
    context_destroy(context);
    return NULL;
  }

  struct GNUNET_CHAT_CheckHandleRoomMembers check;
  check.ignore_key = GNUNET_MESSENGER_get_key(handle->messenger);
  check.contact = NULL;

  GNUNET_MESSENGER_iterate_members(room, check_handle_room_members, &check);

  if (check.contact)
  {
    context->type = GNUNET_CHAT_CONTEXT_TYPE_CONTACT;

    struct GNUNET_CHAT_Contact *contact = contact_create_from_member(
      handle, check.contact
    );

    struct GNUNET_ShortHashCode shorthash;
    util_shorthash_from_member(check.contact, &shorthash);

    if (GNUNET_OK == GNUNET_CONTAINER_multishortmap_put(
      handle->contacts, &shorthash, contact,
      GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
      return context;

    contact_destroy(contact);
  }
  else
  {
    context->type = GNUNET_CHAT_CONTEXT_TYPE_GROUP;

    struct GNUNET_CHAT_Group *group = group_create_from_context(
      handle, context
    );

    group_load_config(group);

    if (group->topic)
      group_publish(group);

    if (GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(
      handle->groups, key, group,
      GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
      return context;

    group_destroy(group);
  }

  GNUNET_CONTAINER_multihashmap_remove(handle->contexts, key, context);
  context_destroy(context);
  return NULL;
}

int
find_handle_rooms (void *cls, struct GNUNET_MESSENGER_Room *room,
		   GNUNET_UNUSED const struct GNUNET_MESSENGER_Contact *member)
{
  struct GNUNET_CHAT_Handle *handle = cls;

  request_handle_context_by_room(
      handle, room
  );

  return GNUNET_YES;
}

void
on_handle_identity(void *cls,
		   GNUNET_UNUSED struct GNUNET_MESSENGER_Handle *messenger)
{
  struct GNUNET_CHAT_Handle *handle = cls;

  GNUNET_MESSENGER_find_rooms(
      handle->messenger, NULL, find_handle_rooms, handle
  );
}

void
on_handle_message (void *cls,
		   struct GNUNET_MESSENGER_Room *room,
		   GNUNET_UNUSED const struct GNUNET_MESSENGER_Contact *sender,
		   const struct GNUNET_MESSENGER_Message *msg,
		   const struct GNUNET_HashCode *hash,
		   enum GNUNET_MESSENGER_MessageFlags flags)
{
  struct GNUNET_CHAT_Handle *handle = cls;

  struct GNUNET_CHAT_Context *context = request_handle_context_by_room(
      handle, room
  );

  if (!context)
    return;

  const struct GNUNET_TIME_Absolute timestamp = GNUNET_TIME_absolute_ntoh(
      msg->header.timestamp
  );

  struct GNUNET_ShortHashCode shorthash;
  util_shorthash_from_member(sender, &shorthash);

  struct GNUNET_TIME_Absolute *time = GNUNET_CONTAINER_multishortmap_get(
      context->timestamps, &shorthash
  );

  if (time)
  {
    time = GNUNET_new(struct GNUNET_TIME_Absolute);
    *time = timestamp;

    if (GNUNET_OK != GNUNET_CONTAINER_multishortmap_put(
	context->timestamps, &shorthash, time,
	GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
      GNUNET_free(time);
  }
  else
  {
    struct GNUNET_TIME_Relative delta = GNUNET_TIME_absolute_get_difference(
	timestamp, *time
    );

    if (GNUNET_TIME_relative_get_zero_().rel_value_us == delta.rel_value_us)
      *time = timestamp;
  }

  struct GNUNET_CHAT_Message *message = GNUNET_CONTAINER_multihashmap_get(
      context->messages, hash
  );

  if (message)
    return;

  message = message_create_from_msg(context, hash, flags, msg);

  switch (msg->header.kind)
  {
    case GNUNET_MESSENGER_KIND_INVITE:
    {
      struct GNUNET_CHAT_Invitation *invitation = invitation_create_from_message(
	  context, &(msg->body.invite)
      );

      if (GNUNET_OK != GNUNET_CONTAINER_multihashmap_put(
	  context->invites, hash, invitation,
	  GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
	invitation_destroy(invitation);
      break;
    }
    case GNUNET_MESSENGER_KIND_FILE:
    {
      GNUNET_CONTAINER_multihashmap_put(
	    context->files, hash, NULL,
	    GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST
      );

      struct GNUNET_CHAT_File *file = file_create_from_message(
	  context->handle, &(msg->body.file)
      );

      if (GNUNET_OK != GNUNET_CONTAINER_multihashmap_put(
	  context->handle->files, &(file->hash), file,
	  GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
	file_destroy(file);
      break;
    }
    case GNUNET_MESSENGER_KIND_DELETE:
    {
      struct GNUNET_CHAT_Message *target = GNUNET_CONTAINER_multihashmap_get(
	  context->messages, &(msg->body.deletion.hash)
      );

      if (target)
	target->msg = NULL;
      break;
    }
    default:
      break;
  }

  if (GNUNET_OK != GNUNET_CONTAINER_multihashmap_put(
      context->messages, hash, message,
      GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
  {
    message_destroy(message);
    return;
  }

  if (handle->msg_cb)
    handle->msg_cb(handle->msg_cls, context, message);
}

int
it_destroy_handle_groups (GNUNET_UNUSED void *cls,
			  GNUNET_UNUSED const struct GNUNET_HashCode *key,
			  void *value)
{
  struct GNUNET_CHAT_Group *group = value;
  group_save_config(group);
  group_destroy(group);
  return GNUNET_YES;
}

int
it_destroy_handle_contacts (GNUNET_UNUSED void *cls,
			    GNUNET_UNUSED const struct GNUNET_ShortHashCode *key,
			    void *value)
{
  struct GNUNET_CHAT_Contact *contact = value;
  contact_destroy(contact);
  return GNUNET_YES;
}

int
it_destroy_handle_contexts (GNUNET_UNUSED void *cls,
			    GNUNET_UNUSED const struct GNUNET_HashCode *key,
			    void *value)
{
  struct GNUNET_CHAT_Context *context = value;
  context_save_config(context);
  context_destroy(context);
  return GNUNET_YES;
}

int
it_destroy_handle_files (GNUNET_UNUSED void *cls,
			 GNUNET_UNUSED const struct GNUNET_HashCode *key,
			 void *value)
{
  struct GNUNET_CHAT_File *file = value;
  file_destroy(file);
  return GNUNET_YES;
}