aboutsummaryrefslogtreecommitdiff
path: root/src/gnunet_chat_lib_intern.c
blob: 3e8e858c7fba08161f4d0c9e859585f1609acfee (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
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
/*
   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_lib_intern.c
 */

#include "gnunet_chat_account.h"
#include "gnunet_chat_contact.h"
#include "gnunet_chat_handle.h"

#include <gnunet/gnunet_common.h>
#include <gnunet/gnunet_messenger_service.h>
#include <gnunet/gnunet_reclaim_lib.h>
#include <gnunet/gnunet_reclaim_service.h>
#include <stdlib.h>
#include <string.h>

#define GNUNET_UNUSED __attribute__ ((unused))

void
task_handle_destruction (void *cls)
{
  GNUNET_assert(cls);

  struct GNUNET_CHAT_Handle *handle = (struct GNUNET_CHAT_Handle*) cls;

  struct GNUNET_CHAT_InternalAccounts *accounts = handle->accounts_head;
  while (accounts)
  {
    if ((accounts->op) && (GNUNET_CHAT_ACCOUNT_NONE != accounts->method))
      break;

    accounts = accounts->next;
  }

  if (accounts)
  {
    handle->destruction = GNUNET_SCHEDULER_add_at_with_priority(
      GNUNET_TIME_absolute_add(
          GNUNET_TIME_absolute_get(),
          GNUNET_TIME_relative_get_millisecond_()
      ),
      GNUNET_SCHEDULER_PRIORITY_IDLE,
      task_handle_destruction,
      handle
    );

    return;
  }

  handle->destruction = NULL;
  handle_destroy(handle);
}

void
task_handle_connection (void *cls)
{
  GNUNET_assert(cls);

  struct GNUNET_CHAT_Handle *handle = (struct GNUNET_CHAT_Handle*) cls;

  handle->connection = NULL;

  if (! handle->next)
    return;

  const struct GNUNET_CHAT_Account *account = handle->next;
  handle->next = NULL;

  handle_connect(handle, account);
}

void
task_handle_disconnection (void *cls)
{
  GNUNET_assert(cls);

  struct GNUNET_CHAT_Handle *handle = (struct GNUNET_CHAT_Handle*) cls;

  handle->connection = NULL;
  handle_disconnect(handle);

  if (! handle->next)
    return;

  const struct GNUNET_CHAT_Account *account = handle->next;
  handle->next = NULL;

  handle_connect(handle, account);
}

void
cb_lobby_lookup (void *cls,
                 uint32_t count,
                 const struct GNUNET_GNSRECORD_Data *data)
{
  GNUNET_assert(cls);

  struct GNUNET_CHAT_UriLookups *lookups = (struct GNUNET_CHAT_UriLookups*) cls;

  if ((!(lookups->handle)) || (!(lookups->uri)) ||
      (GNUNET_CHAT_URI_TYPE_CHAT != lookups->uri->type))
    goto drop_lookup;

  struct GNUNET_CHAT_Context *context = handle_process_records(
    lookups->handle,
    lookups->uri->chat.label,
    count,
    data
  );

  if (context)
    context_write_records(context);

drop_lookup:
  if (lookups->uri)
    uri_destroy(lookups->uri);

  if (lookups->handle)
    GNUNET_CONTAINER_DLL_remove(
      lookups->handle->lookups_head,
      lookups->handle->lookups_tail,
      lookups
    );

  GNUNET_free(lookups);
}

struct GNUNET_CHAT_IterateFiles
{
  struct GNUNET_CHAT_Handle *handle;
  GNUNET_CHAT_FileCallback cb;
  void *cls;
};

enum GNUNET_GenericReturnValue
it_iterate_files (void *cls,
                  GNUNET_UNUSED const struct GNUNET_HashCode *key,
                  void *value)
{
  GNUNET_assert((cls) && (key));

  struct GNUNET_CHAT_IterateFiles *it = cls;

  if (!(it->cb))
    return GNUNET_YES;

  struct GNUNET_CHAT_File *file = (struct GNUNET_CHAT_File*) value;

  if (!file)
    return GNUNET_YES;

  return it->cb(it->cls, it->handle, file);
}

struct GNUNET_CHAT_HandleIterateContacts
{
  struct GNUNET_CHAT_Handle *handle;
  GNUNET_CHAT_ContactCallback cb;
  void *cls;
};

enum GNUNET_GenericReturnValue
it_handle_iterate_contacts (void *cls,
                            GNUNET_UNUSED const struct GNUNET_ShortHashCode *key,
                            void *value)
{
  GNUNET_assert((cls) && (value));

  struct GNUNET_CHAT_HandleIterateContacts *it = cls;

  if (!(it->cb))
    return GNUNET_YES;

  struct GNUNET_CHAT_Contact *contact = value;

  return it->cb(it->cls, it->handle, contact);
}

struct GNUNET_CHAT_HandleIterateGroups
{
  struct GNUNET_CHAT_Handle *handle;
  GNUNET_CHAT_GroupCallback cb;
  void *cls;
};

enum GNUNET_GenericReturnValue
it_handle_iterate_groups (void *cls,
                          GNUNET_UNUSED const struct GNUNET_HashCode *key,
                          void *value)
{
  GNUNET_assert((cls) && (value));

  struct GNUNET_CHAT_HandleIterateGroups *it = cls;

  if (!(it->cb))
    return GNUNET_YES;

  struct GNUNET_CHAT_Group *group = value;

  return it->cb(it->cls, it->handle, group);
}

typedef void
(*GNUNET_CHAT_ContactIterateContextCallback) (struct GNUNET_CHAT_Contact *contact,
                                              struct GNUNET_CHAT_Context *context,
                                              const char *tag);

struct GNUNET_CHAT_ContactIterateContexts
{
  struct GNUNET_CHAT_Contact *contact;
  const char *tag;

  GNUNET_CHAT_ContactIterateContextCallback cb;
};

enum GNUNET_GenericReturnValue
it_contact_iterate_contexts (void *cls,
                             const struct GNUNET_HashCode *key,
                             GNUNET_UNUSED void *value)
{
  GNUNET_assert((cls) && (key));

  struct GNUNET_CHAT_ContactIterateContexts *it = cls;

  if (!(it->cb))
    return GNUNET_YES;

  struct GNUNET_CHAT_Handle *handle = it->contact->handle;
  struct GNUNET_CHAT_Context *context = GNUNET_CONTAINER_multihashmap_get(
    handle->contexts, key);
  
  if (! context)
    return GNUNET_YES;

  it->cb(it->contact, context, it->tag);
  return GNUNET_YES;
}

struct GNUNET_CHAT_RoomFindContact
{
  const struct GNUNET_CRYPTO_PublicKey *ignore_key;
  const struct GNUNET_MESSENGER_Contact *contact;
};

enum GNUNET_GenericReturnValue
it_room_find_contact (void *cls,
                      GNUNET_UNUSED struct GNUNET_MESSENGER_Room *room,
                      const struct GNUNET_MESSENGER_Contact *member)
{
  GNUNET_assert((cls) && (member));

  const struct GNUNET_CRYPTO_PublicKey *key = GNUNET_MESSENGER_contact_get_key(
      member
  );

  struct GNUNET_CHAT_RoomFindContact *find = cls;

  if ((find->ignore_key) && (key) &&
      (0 == GNUNET_memcmp(find->ignore_key, key)))
    return GNUNET_YES;

  find->contact = member;
  return GNUNET_NO;
}

void
task_group_destruction (void *cls)
{
  GNUNET_assert(cls);

  struct GNUNET_CHAT_Group *group = (struct GNUNET_CHAT_Group*) cls;
  struct GNUNET_HashCode key;

  GNUNET_memcpy(&key, GNUNET_MESSENGER_room_get_key(
    group->context->room
  ), sizeof(key));

  GNUNET_CONTAINER_multihashmap_remove(
    group->handle->groups, &key, group
  );

  context_delete(group->context, GNUNET_YES);

  group->destruction = NULL;

  group_destroy(group);
}

struct GNUNET_CHAT_GroupIterateContacts
{
  const struct GNUNET_CHAT_Group *group;
  GNUNET_CHAT_GroupContactCallback cb;
  void *cls;
};

enum GNUNET_GenericReturnValue
it_group_iterate_contacts (void* cls,
			                     GNUNET_UNUSED struct GNUNET_MESSENGER_Room *room,
                           const struct GNUNET_MESSENGER_Contact *member)
{
  GNUNET_assert((cls) && (member));

  struct GNUNET_CHAT_GroupIterateContacts *it = cls;

  if (!(it->cb))
    return GNUNET_YES;

  return it->cb(it->cls, it->group, handle_get_contact_from_messenger(
    it->group->handle, member
  ));
}

struct GNUNET_CHAT_ContextIterateMessages
{
  struct GNUNET_CHAT_Context *context;
  GNUNET_CHAT_ContextMessageCallback cb;
  void *cls;
};

enum GNUNET_GenericReturnValue
it_context_iterate_messages (void *cls,
                             GNUNET_UNUSED const struct GNUNET_HashCode *key,
                             void *value)
{
  GNUNET_assert((cls) && (value));

  struct GNUNET_CHAT_ContextIterateMessages *it = cls;

  if (!(it->cb))
    return GNUNET_YES;

  struct GNUNET_CHAT_Message *message = value;

  return it->cb(it->cls, it->context, message);
}

struct GNUNET_CHAT_ContextIterateFiles
{
  struct GNUNET_CHAT_Context *context;
  GNUNET_CHAT_ContextFileCallback cb;
  void *cls;
};

enum GNUNET_GenericReturnValue
it_context_iterate_files (void *cls,
                          const struct GNUNET_HashCode *key,
                          GNUNET_UNUSED void *value)
{
  GNUNET_assert((cls) && (key));

  struct GNUNET_CHAT_ContextIterateFiles *it = cls;

  if (!(it->cb))
    return GNUNET_YES;

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

  if ((!message) || (! message->msg))
    return GNUNET_YES;

  struct GNUNET_CHAT_File *file = GNUNET_CONTAINER_multihashmap_get(
    it->context->handle->files, &(message->msg->body.file.hash)
  );

  if (!file)
    return GNUNET_YES;

  return it->cb(it->cls, it->context, file);
}

struct GNUNET_CHAT_MessageIterateReadReceipts
{
  const struct GNUNET_CHAT_Message *message;
  GNUNET_CHAT_MessageReadReceiptCallback cb;
  void *cls;
};

enum GNUNET_GenericReturnValue
it_message_iterate_read_receipts (void *cls,
                                  GNUNET_UNUSED struct GNUNET_MESSENGER_Room *room,
                                  const struct GNUNET_MESSENGER_Contact *member)
{
  GNUNET_assert((cls) && (member));

  struct GNUNET_CHAT_MessageIterateReadReceipts *it = cls;
  struct GNUNET_CHAT_Handle *handle = it->message->context->handle;

  if (!handle)
    return GNUNET_NO;

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

  struct GNUNET_CHAT_Contact *contact = GNUNET_CONTAINER_multishortmap_get(
      handle->contacts, &shorthash
  );

  if (!contact)
    return GNUNET_YES;

  struct GNUNET_TIME_Absolute *timestamp = GNUNET_CONTAINER_multishortmap_get(
    it->message->context->timestamps, &shorthash
  );

  if (!timestamp)
    return GNUNET_YES;

  struct GNUNET_TIME_Relative delta = GNUNET_TIME_absolute_get_difference(
    *timestamp, GNUNET_CHAT_message_get_timestamp(it->message)
  );

  int read_receipt;
  if (GNUNET_TIME_relative_get_zero_().rel_value_us == delta.rel_value_us)
    read_receipt = GNUNET_YES;
  else
    read_receipt = GNUNET_NO;

  if (it->cb)
    it->cb(it->cls, it->message, contact, read_receipt);

  return GNUNET_YES;
}

void
cont_update_attribute_with_status (void *cls,
                                   int32_t success,
                                   const char *emsg)
{
  GNUNET_assert(cls);

  struct GNUNET_CHAT_AttributeProcess *attributes = (
    (struct GNUNET_CHAT_AttributeProcess*) cls
  );

  attributes->op = NULL;

  const struct GNUNET_CHAT_Account *account = attributes->account;
  struct GNUNET_CHAT_Handle *handle = attributes->handle;

  const char *attribute_name = NULL;

  if (attributes->attribute)
    attribute_name = attributes->attribute->name;

  if (GNUNET_SYSERR == success)
    handle_send_internal_message(
      handle,
      account,
      NULL,
      GNUNET_CHAT_KIND_WARNING,
      emsg
    );
  else
    handle_send_internal_message(
      handle,
      account,
      NULL,
      GNUNET_CHAT_FLAG_ATTRIBUTES,
      attribute_name
    );
  
  internal_attributes_destroy(attributes);
}

void
cb_task_finish_iterate_attribute (void *cls)
{
  GNUNET_assert(cls);

  struct GNUNET_CHAT_AttributeProcess *attributes = (
    (struct GNUNET_CHAT_AttributeProcess*) cls
  );

  attributes->iter = NULL;

  struct GNUNET_CHAT_Handle *handle = attributes->handle;

  const struct GNUNET_CRYPTO_PrivateKey *key;

  if (attributes->account)
    key = account_get_key(attributes->account);
  else
    key = handle_get_key(handle);

  if (attributes->name)
    GNUNET_free(attributes->name);

  attributes->name = NULL;

  if ((! attributes->op) && (key) &&
      (attributes->attribute))
    attributes->op = GNUNET_RECLAIM_attribute_store(
      handle->reclaim,
      key,
      attributes->attribute,
      &(attributes->expires),
      cont_update_attribute_with_status,
      attributes
    );
  
  if (attributes->data)
    GNUNET_free(attributes->data);

  attributes->data = NULL;

  if (attributes->op)
    return;

  internal_attributes_destroy(attributes);
}

void
cb_task_error_iterate_attribute (void *cls)
{
  GNUNET_assert(cls);

  struct GNUNET_CHAT_AttributeProcess *attributes = (
    (struct GNUNET_CHAT_AttributeProcess*) cls
  );

  handle_send_internal_message(
    attributes->handle,
    attributes->account,
    NULL,
    GNUNET_CHAT_FLAG_WARNING,
    "Attribute iteration failed!"
  );

  cb_task_finish_iterate_attribute(cls);
}

void
cb_store_attribute (void *cls,
                    const struct GNUNET_CRYPTO_PublicKey *identity,
                    const struct GNUNET_RECLAIM_Attribute *attribute)
{
  GNUNET_assert(cls);

  struct GNUNET_CHAT_AttributeProcess *attributes = (
    (struct GNUNET_CHAT_AttributeProcess*) cls
  );

  struct GNUNET_CHAT_Handle *handle = attributes->handle;

  const struct GNUNET_CRYPTO_PrivateKey *key = handle_get_key(
    handle
  );

  if (! attributes->name)
  {
    internal_attributes_stop_iter(attributes);
    return;
  }

  if (0 == strcmp(attribute->name, attributes->name))
  {
    internal_attributes_stop_iter(attributes);

    if (attributes->attribute)
    {
      attributes->attribute->credential = attribute->credential;
      attributes->attribute->flag = attribute->flag;
      attributes->attribute->id = attribute->id;
    }

    attributes->op = GNUNET_RECLAIM_attribute_store(
      handle->reclaim,
      key,
      attributes->attribute,
      &(attributes->expires),
      cont_update_attribute_with_status,
      attributes
    );

    if (attributes->data)
      GNUNET_free(attributes->data);

    attributes->data = NULL;

    GNUNET_free(attributes->name);
    attributes->name = NULL;
    return;
  }

  internal_attributes_next_iter(attributes);
}

void
cb_delete_attribute (void *cls,
                     const struct GNUNET_CRYPTO_PublicKey *identity,
                     const struct GNUNET_RECLAIM_Attribute *attribute)
{
  GNUNET_assert(cls);

  struct GNUNET_CHAT_AttributeProcess *attributes = (
    (struct GNUNET_CHAT_AttributeProcess*) cls
  );

  if (! attributes->name)
  {
    internal_attributes_stop_iter(attributes);
    return;
  }

  struct GNUNET_CHAT_Handle *handle = attributes->handle;

  const struct GNUNET_CRYPTO_PrivateKey *key = handle_get_key(
    handle
  );

  if (0 == strcmp(attribute->name, attributes->name))
  {
    internal_attributes_stop_iter(attributes);

    attributes->op = GNUNET_RECLAIM_attribute_delete(
      handle->reclaim,
      key,
      attribute,
      cont_update_attribute_with_status,
      attributes
    );

    GNUNET_free(attributes->name);
    attributes->name = NULL;
    return;
  }

  internal_attributes_next_iter(attributes);
}

void
cb_iterate_attribute (void *cls,
                      const struct GNUNET_CRYPTO_PublicKey *identity,
                      const struct GNUNET_RECLAIM_Attribute *attribute)
{
  GNUNET_assert(cls);

  struct GNUNET_CHAT_AttributeProcess *attributes = (
    (struct GNUNET_CHAT_AttributeProcess*) cls
  );

  struct GNUNET_CHAT_Handle *handle = attributes->handle;
  enum GNUNET_GenericReturnValue result = GNUNET_YES;

  char *value = GNUNET_RECLAIM_attribute_value_to_string(
    attribute->type,
    attribute->data,
    attribute->data_size
  );

  if (attributes->callback)
    result = attributes->callback(attributes->closure, handle, attribute->name, value);
  else if (attributes->account_callback)
    result = attributes->account_callback(
      attributes->closure,
      attributes->account,
      attribute->name,
      value
    );

  if (value)
    GNUNET_free (value);
  
  if (GNUNET_YES != result)
    internal_attributes_stop_iter(attributes);
  else
    internal_attributes_next_iter(attributes);
}

void
cb_issue_ticket (void *cls,
                 const struct GNUNET_RECLAIM_Ticket *ticket,
                 const struct GNUNET_RECLAIM_PresentationList *presentations)
{
  GNUNET_assert(cls);

  struct GNUNET_CHAT_AttributeProcess *attributes = (
    (struct GNUNET_CHAT_AttributeProcess*) cls
  );

  attributes->op = NULL;

  if ((!(attributes->contact)) || (!(attributes->contact->member)))
    goto skip_sending;

  struct GNUNET_CHAT_Context *context = contact_find_context(
    attributes->contact,
    GNUNET_YES
  );

  if ((!context) || (!ticket))
    goto skip_sending;

  char *identifier = GNUNET_strdup(ticket->gns_name);

  if (!identifier)
    goto skip_sending;

  struct GNUNET_MESSENGER_Message message;
  message.header.kind = GNUNET_MESSENGER_KIND_TICKET;
  message.body.ticket.identifier = identifier;

  GNUNET_MESSENGER_send_message(
    context->room,
    &message,
    attributes->contact->member
  );

  GNUNET_free(identifier);

skip_sending:
  internal_attributes_destroy(attributes);
}

static struct GNUNET_RECLAIM_AttributeList*
attribute_list_from_attribute (const struct GNUNET_RECLAIM_Attribute *attribute)
{
  struct GNUNET_RECLAIM_AttributeList *attrs;
  struct GNUNET_RECLAIM_AttributeListEntry *le;

  attrs = GNUNET_new (struct GNUNET_RECLAIM_AttributeList);

  if (!attrs)
    return NULL;
  
  le = GNUNET_new (struct GNUNET_RECLAIM_AttributeListEntry);

  if (!le)
  {
    GNUNET_free (attrs);
    return NULL;
  }

  le->attribute = GNUNET_RECLAIM_attribute_new (
    attribute->name,
    &(attribute->credential),
    attribute->type,
    attribute->data,
    attribute->data_size
  );

  le->attribute->flag = attribute->flag;
  le->attribute->id = attribute->id;

  GNUNET_CONTAINER_DLL_insert (
    attrs->list_head,
    attrs->list_tail,
    le
  );

  return attrs;
}

void
cb_share_attribute (void *cls,
                    const struct GNUNET_CRYPTO_PublicKey *identity,
                    const struct GNUNET_RECLAIM_Attribute *attribute)
{
  GNUNET_assert(cls);

  struct GNUNET_CHAT_AttributeProcess *attributes = (
    (struct GNUNET_CHAT_AttributeProcess*) cls
  );

  if (! attributes->name)
  {
    internal_attributes_stop_iter(attributes);
    return;
  }

  struct GNUNET_CHAT_Handle *handle = attributes->handle;

  if (0 != strcmp(attribute->name, attributes->name))
  {
    internal_attributes_next_iter(attributes);
    return;
  }
  
  internal_attributes_stop_iter(attributes);

  GNUNET_free(attributes->name);
  attributes->name = NULL;

  const struct GNUNET_CRYPTO_PrivateKey *key = handle_get_key(
    handle
  );

  if (!key)
    return;

  const struct GNUNET_CRYPTO_PublicKey *pubkey = contact_get_key(
    attributes->contact
  );

  if (!pubkey)
    return;

  char *rp_uri = GNUNET_CRYPTO_public_key_to_string(pubkey);

  struct GNUNET_RECLAIM_AttributeList *attrs;
  attrs = attribute_list_from_attribute(attribute);

  if (!attrs)
    goto cleanup;

  attributes->op = GNUNET_RECLAIM_ticket_issue(
    handle->reclaim,
    key,
    rp_uri,
    attrs,
    cb_issue_ticket,
    attributes
  );
  
  GNUNET_RECLAIM_attribute_list_destroy(attrs);

cleanup:
  GNUNET_free(rp_uri);
}

void
cb_task_finish_iterate_ticket (void *cls)
{
  GNUNET_assert(cls);

  struct GNUNET_CHAT_TicketProcess *tickets = (
    (struct GNUNET_CHAT_TicketProcess*) cls
  );

  tickets->iter = NULL;

  internal_tickets_destroy(tickets);
}

void
cb_task_error_iterate_ticket (void *cls)
{
  GNUNET_assert(cls);

  struct GNUNET_CHAT_TicketProcess *tickets = (
    (struct GNUNET_CHAT_TicketProcess*) cls
  );

  handle_send_internal_message(
    tickets->handle,
    NULL,
    NULL,
    GNUNET_CHAT_FLAG_WARNING,
    "Ticket iteration failed!"
  );

  cb_task_finish_iterate_ticket(cls);
}

void
cont_revoke_ticket (void *cls,
                    int32_t success,
                    const char *emsg)
{
  GNUNET_assert(cls);

  struct GNUNET_CHAT_TicketProcess *tickets = (
    (struct GNUNET_CHAT_TicketProcess*) cls
  );

  tickets->op = NULL;

  struct GNUNET_CHAT_Handle *handle = tickets->handle;

  if (success == GNUNET_SYSERR)
    handle_send_internal_message(
      handle,
      NULL,
      NULL,
      GNUNET_CHAT_FLAG_WARNING,
      emsg
    );
  else
    handle_send_internal_message(
      handle,
      NULL,
      NULL,
      GNUNET_CHAT_FLAG_SHARE_ATTRIBUTES,
      NULL
    );

  internal_tickets_destroy(tickets);
}

void
cb_consume_ticket_check (void *cls,
                         const struct GNUNET_CRYPTO_PublicKey *identity,
                         const struct GNUNET_RECLAIM_Attribute *attribute,
                         const struct GNUNET_RECLAIM_Presentation *presentation)
{
  GNUNET_assert(cls);

  struct GNUNET_CHAT_TicketProcess *tickets = (
    (struct GNUNET_CHAT_TicketProcess*) cls
  );

  if ((!identity) && (!attribute) && (!presentation))
  {
    tickets->op = NULL;

    struct GNUNET_CHAT_Handle *handle = tickets->handle;

    const struct GNUNET_CRYPTO_PrivateKey *key = handle_get_key(
      handle
    );

    if (tickets->name)
    {
      GNUNET_free(tickets->name);
      tickets->name = NULL;
    }
    else if (key)
      tickets->op = GNUNET_RECLAIM_ticket_revoke(
        handle->reclaim,
        key,
        tickets->ticket,
        cont_revoke_ticket,
        tickets
      );
    
    if (tickets->ticket)
    {
      GNUNET_free(tickets->ticket);
      tickets->ticket = NULL;
    }

    if (tickets->op)
      return;

    internal_tickets_destroy(tickets);
    return;
  }

  if ((!attribute) || (! tickets->name) || 
      (0 != strcmp(tickets->name, attribute->name)))
    return;

  if (tickets->name)
  {
    GNUNET_free(tickets->name);
    tickets->name = NULL;
  }
}

static enum GNUNET_GenericReturnValue
is_contact_ticket_audience (const struct GNUNET_CHAT_Contact *contact,
                            const char *rp_uri)
{
  GNUNET_assert((contact) && (rp_uri));

  const struct GNUNET_CRYPTO_PublicKey *pubkey;
  pubkey = contact_get_key(contact);

  if (!pubkey)
    return GNUNET_NO;

  struct GNUNET_CRYPTO_PublicKey audience;
  enum GNUNET_GenericReturnValue parsing;

  parsing = GNUNET_CRYPTO_public_key_from_string(rp_uri, &audience);

  if ((GNUNET_OK != parsing) || (0 != GNUNET_memcmp(pubkey, &audience)))
    return GNUNET_NO;

  return GNUNET_YES;
}

void
cb_iterate_ticket_check (void *cls,
                         const struct GNUNET_RECLAIM_Ticket *ticket,
                         const char *rp_uri)
{
  GNUNET_assert(cls);

  struct GNUNET_CHAT_TicketProcess *tickets = (
    (struct GNUNET_CHAT_TicketProcess*) cls
  );

  struct GNUNET_CHAT_Handle *handle = tickets->handle;

  if ((!rp_uri) || (!(tickets->contact)) || 
      (GNUNET_YES != is_contact_ticket_audience(tickets->contact, rp_uri)))
  {
    internal_tickets_next_iter(tickets);
    return;
  }

  const struct GNUNET_CRYPTO_PrivateKey *key = handle_get_key(
    handle
  );

  if (!key)
  {
    internal_tickets_stop_iter(tickets);
    return;
  }

  struct GNUNET_CHAT_TicketProcess *new_tickets;
  new_tickets = internal_tickets_copy(tickets, ticket);

  if (!new_tickets)
  {
    internal_tickets_stop_iter(tickets);
    return;
  }

  new_tickets->op = GNUNET_RECLAIM_ticket_consume(
    handle->reclaim,
    ticket,
    rp_uri,
    cb_consume_ticket_check,
    new_tickets
  );

  internal_tickets_next_iter(tickets);
}

void
cb_consume_ticket (void *cls,
                   const struct GNUNET_CRYPTO_PublicKey *identity,
                   const struct GNUNET_RECLAIM_Attribute *attribute,
                   const struct GNUNET_RECLAIM_Presentation *presentation)
{
  GNUNET_assert(cls);

  struct GNUNET_CHAT_TicketProcess *tickets = (
    (struct GNUNET_CHAT_TicketProcess*) cls
  );

  if ((!identity) && (!attribute) && (!presentation))
  {
    tickets->op = NULL;

    internal_tickets_destroy(tickets);
    return;
  }

  if (!attribute)
    return;

  char *value = GNUNET_RECLAIM_attribute_value_to_string(
    attribute->type,
    attribute->data,
    attribute->data_size
  );

  if (tickets->callback)
    tickets->callback(tickets->closure, tickets->contact, attribute->name, value);

  if (value)
    GNUNET_free (value);
}

void
cb_iterate_ticket (void *cls,
                   const struct GNUNET_RECLAIM_Ticket *ticket,
                   const char *rp_uri)
{
  GNUNET_assert(cls);

  struct GNUNET_CHAT_TicketProcess *tickets = (
    (struct GNUNET_CHAT_TicketProcess*) cls
  );

  struct GNUNET_CHAT_Handle *handle = tickets->handle;

  if ((!rp_uri) || (!(tickets->contact)) || 
      (GNUNET_YES != is_contact_ticket_audience(tickets->contact, rp_uri)))
  {
    internal_tickets_next_iter(tickets);
    return;
  }

  const struct GNUNET_CRYPTO_PrivateKey *key = handle_get_key(
    handle
  );

  if (!key)
  {
    internal_tickets_stop_iter(tickets);
    return;
  }

  struct GNUNET_CHAT_TicketProcess *new_tickets;
  new_tickets = internal_tickets_copy(tickets, NULL);

  if (!new_tickets)
  {
    internal_tickets_stop_iter(tickets);
    return;
  }

  new_tickets->op = GNUNET_RECLAIM_ticket_consume(
    handle->reclaim,
    ticket,
    rp_uri,
    cb_consume_ticket,
    new_tickets
  );

  internal_tickets_next_iter(tickets);
}