aboutsummaryrefslogtreecommitdiff
path: root/src/gnunet_chat_lib.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/gnunet_chat_lib.c')
-rw-r--r--src/gnunet_chat_lib.c104
1 files changed, 84 insertions, 20 deletions
diff --git a/src/gnunet_chat_lib.c b/src/gnunet_chat_lib.c
index 1d12bdd..23b2c3e 100644
--- a/src/gnunet_chat_lib.c
+++ b/src/gnunet_chat_lib.c
@@ -293,7 +293,20 @@ 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 return contact_find_context(contact); 296 if (contact->context)
297 return contact->context;
298
299 struct GNUNET_CHAT_Context *context = contact_find_context(contact);
300
301 if ((context) && (GNUNET_CHAT_CONTEXT_TYPE_CONTACT == context->type))
302 return context;
303
304 context = context_create_from_contact(contact->handle, contact->member);
305
306 if (context)
307 contact->context = context;
308
309 return context;
297} 310}
298 311
299 312
@@ -444,15 +457,79 @@ GNUNET_CHAT_group_get_context (struct GNUNET_CHAT_Group *group)
444} 457}
445 458
446 459
460int
461GNUNET_CHAT_context_get_status (const struct GNUNET_CHAT_Context *context)
462{
463 if ((!context) || (!(context->room)))
464 return GNUNET_SYSERR;
465
466 if (1 <= GNUNET_MESSENGER_iterate_members(context->room, NULL, NULL))
467 return GNUNET_NO;
468
469 return GNUNET_YES;
470}
471
472
473void
474GNUNET_CHAT_context_request (struct GNUNET_CHAT_Context *context)
475{
476 if ((!context) || (context->room))
477 return;
478
479 struct GNUNET_CHAT_Handle *handle = context->handle;
480
481 if ((!handle) || (!(context->contact)))
482 return;
483
484 struct GNUNET_CHAT_Contact *contact = contact_create_from_member(
485 handle, context->contact
486 );
487
488 if (!contact)
489 return;
490
491 struct GNUNET_CHAT_Context *other = contact_find_context(contact);
492
493 if ((!other) || (!(other->room)))
494 return;
495
496 struct GNUNET_HashCode key;
497 GNUNET_CRYPTO_random_block(GNUNET_CRYPTO_QUALITY_WEAK, &key, sizeof(key));
498
499 if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains(
500 handle->contexts, &key))
501 return;
502
503 struct GNUNET_MESSENGER_Room *room = GNUNET_MESSENGER_open_room(
504 handle->messenger, &key
505 );
506
507 if (!room)
508 return;
509
510 struct GNUNET_MESSENGER_Message msg;
511 msg.header.kind = GNUNET_MESSENGER_KIND_INVITE;
512 GNUNET_CRYPTO_get_peer_identity(handle->cfg, &(msg.body.invite.door));
513 GNUNET_memcpy(&(msg.body.invite.key), &key, sizeof(msg.body.invite.key));
514
515 GNUNET_MESSENGER_send_message(other->room, &msg, context->contact);
516 context->room = room;
517}
518
519
447const struct GNUNET_CHAT_Contact* 520const struct GNUNET_CHAT_Contact*
448GNUNET_CHAT_context_get_contact (const struct GNUNET_CHAT_Context *context) 521GNUNET_CHAT_context_get_contact (const struct GNUNET_CHAT_Context *context)
449{ 522{
450 if ((!context) || (GNUNET_CHAT_CONTEXT_TYPE_CONTACT != context->type)) 523 if ((!context) || (GNUNET_CHAT_CONTEXT_TYPE_CONTACT != context->type))
451 return NULL; 524 return NULL;
452 525
526 if (context->contact)
527 return handle_get_contact_from_messenger(context->handle, context->contact);
528
453 struct GNUNET_MESSENGER_Room *room = context->room; 529 struct GNUNET_MESSENGER_Room *room = context->room;
454 struct GNUNET_CHAT_RoomFindContact find; 530 struct GNUNET_CHAT_RoomFindContact find;
455 531
532 find.ignore_key = GNUNET_MESSENGER_get_key(context->handle->messenger);
456 find.contact = NULL; 533 find.contact = NULL;
457 534
458 GNUNET_MESSENGER_iterate_members( 535 GNUNET_MESSENGER_iterate_members(
@@ -464,16 +541,7 @@ GNUNET_CHAT_context_get_contact (const struct GNUNET_CHAT_Context *context)
464 if (!find.contact) 541 if (!find.contact)
465 return NULL; 542 return NULL;
466 543
467 struct GNUNET_ShortHashCode shorthash; 544 return handle_get_contact_from_messenger(context->handle, find.contact);
468 util_shorthash_from_member(find.contact, &shorthash);
469
470 const struct GNUNET_CHAT_Contact *contact;
471 contact = GNUNET_CONTAINER_multishortmap_get(
472 context->handle->contacts, &shorthash
473 );
474
475 GNUNET_assert((contact == NULL) || (contact->context == context));
476 return contact;
477} 545}
478 546
479 547
@@ -524,7 +592,7 @@ int
524GNUNET_CHAT_context_send_text (struct GNUNET_CHAT_Context *context, 592GNUNET_CHAT_context_send_text (struct GNUNET_CHAT_Context *context,
525 const char *text) 593 const char *text)
526{ 594{
527 if ((!context) || (!text)) 595 if ((!context) || (!text) || (!(context->room)))
528 return GNUNET_SYSERR; 596 return GNUNET_SYSERR;
529 597
530 struct GNUNET_MESSENGER_Message msg; 598 struct GNUNET_MESSENGER_Message msg;
@@ -544,7 +612,7 @@ GNUNET_CHAT_context_send_file (struct GNUNET_CHAT_Context *context,
544 GNUNET_CHAT_FileUploadCallback callback, 612 GNUNET_CHAT_FileUploadCallback callback,
545 void *cls) 613 void *cls)
546{ 614{
547 if ((!context) || (!path)) 615 if ((!context) || (!path) || (!(context->room)))
548 return GNUNET_SYSERR; 616 return GNUNET_SYSERR;
549 617
550 if (!(context->handle->directory)) 618 if (!(context->handle->directory))
@@ -620,7 +688,8 @@ int
620GNUNET_CHAT_context_share_file (struct GNUNET_CHAT_Context *context, 688GNUNET_CHAT_context_share_file (struct GNUNET_CHAT_Context *context,
621 const struct GNUNET_CHAT_File *file) 689 const struct GNUNET_CHAT_File *file)
622{ 690{
623 if ((!context) || (!file) || (strlen(file->name) > NAME_MAX)) 691 if ((!context) || (!file) || (strlen(file->name) > NAME_MAX) ||
692 (!(context->room)))
624 return GNUNET_SYSERR; 693 return GNUNET_SYSERR;
625 694
626 struct GNUNET_MESSENGER_Message msg; 695 struct GNUNET_MESSENGER_Message msg;
@@ -741,12 +810,7 @@ GNUNET_CHAT_message_get_sender (const struct GNUNET_CHAT_Message *message)
741 if (!sender) 810 if (!sender)
742 return NULL; 811 return NULL;
743 812
744 struct GNUNET_ShortHashCode shorthash; 813 return handle_get_contact_from_messenger(message->context->handle, sender);
745 util_shorthash_from_member(sender, &shorthash);
746
747 return GNUNET_CONTAINER_multishortmap_get(
748 message->context->handle->contacts, &shorthash
749 );
750} 814}
751 815
752 816