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.c213
1 files changed, 167 insertions, 46 deletions
diff --git a/src/gnunet_chat_lib.c b/src/gnunet_chat_lib.c
index 23c541d..044a1be 100644
--- a/src/gnunet_chat_lib.c
+++ b/src/gnunet_chat_lib.c
@@ -40,6 +40,7 @@
40 40
41struct GNUNET_CHAT_Handle* 41struct GNUNET_CHAT_Handle*
42GNUNET_CHAT_start (const struct GNUNET_CONFIGURATION_Handle *cfg, 42GNUNET_CHAT_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
43 const char *directory,
43 const char *name, 44 const char *name,
44 GNUNET_CHAT_WarningCallback warn_cb, void *warn_cls, 45 GNUNET_CHAT_WarningCallback warn_cb, void *warn_cls,
45 GNUNET_CHAT_ContextMessageCallback msg_cb, void *msg_cls) 46 GNUNET_CHAT_ContextMessageCallback msg_cb, void *msg_cls)
@@ -47,11 +48,10 @@ GNUNET_CHAT_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
47 if (!cfg) 48 if (!cfg)
48 return NULL; 49 return NULL;
49 50
50 if (!name)
51 return NULL;
52
53 return handle_create_from_config( 51 return handle_create_from_config(
54 cfg, name, msg_cb, msg_cls, warn_cb, warn_cls 52 cfg, directory, name,
53 msg_cb, msg_cls,
54 warn_cb, warn_cls
55 ); 55 );
56} 56}
57 57
@@ -332,6 +332,7 @@ GNUNET_CHAT_group_invite_contact (struct GNUNET_CHAT_Group *group,
332 GNUNET_MESSENGER_send_message(contact->context->room, &msg, contact->member); 332 GNUNET_MESSENGER_send_message(contact->context->room, &msg, contact->member);
333} 333}
334 334
335
335int 336int
336GNUNET_CHAT_group_iterate_contacts (struct GNUNET_CHAT_Group *group, 337GNUNET_CHAT_group_iterate_contacts (struct GNUNET_CHAT_Group *group,
337 GNUNET_CHAT_GroupContactCallback callback, 338 GNUNET_CHAT_GroupContactCallback callback,
@@ -360,12 +361,13 @@ GNUNET_CHAT_group_get_context (struct GNUNET_CHAT_Group *group)
360 return group->context; 361 return group->context;
361} 362}
362 363
363void 364
365int
364GNUNET_CHAT_context_send_text (struct GNUNET_CHAT_Context *context, 366GNUNET_CHAT_context_send_text (struct GNUNET_CHAT_Context *context,
365 const char *text) 367 const char *text)
366{ 368{
367 if ((!context) || (!text)) 369 if ((!context) || (!text))
368 return; 370 return GNUNET_SYSERR;
369 371
370 struct GNUNET_MESSENGER_Message msg; 372 struct GNUNET_MESSENGER_Message msg;
371 msg.header.kind = GNUNET_MESSENGER_KIND_TEXT; 373 msg.header.kind = GNUNET_MESSENGER_KIND_TEXT;
@@ -374,42 +376,109 @@ GNUNET_CHAT_context_send_text (struct GNUNET_CHAT_Context *context,
374 GNUNET_MESSENGER_send_message(context->room, &msg, NULL); 376 GNUNET_MESSENGER_send_message(context->room, &msg, NULL);
375 377
376 GNUNET_free(msg.body.text.text); 378 GNUNET_free(msg.body.text.text);
379 return GNUNET_OK;
377} 380}
378 381
379 382
380void 383int
381GNUNET_CHAT_context_send_file (struct GNUNET_CHAT_Context *context, 384GNUNET_CHAT_context_send_file (struct GNUNET_CHAT_Context *context,
382 const char *path) 385 const char *path)
383{ 386{
384 if ((!context) || (!path)) 387 if ((!context) || (!path))
385 return; 388 return GNUNET_SYSERR;
389
390 if (!(context->handle->directory))
391 return GNUNET_SYSERR;
392
393 struct GNUNET_HashCode hash;
394 if (GNUNET_OK != util_hash_file(path, &hash))
395 return GNUNET_SYSERR;
396
397 char *filename;
398 util_get_filename (context->handle->directory, &hash, &filename);
399
400 if ((GNUNET_OK != GNUNET_DISK_directory_create_for_file(filename)) ||
401 (GNUNET_OK != GNUNET_DISK_file_copy(path, filename)))
402 {
403 GNUNET_free(filename);
404 return GNUNET_SYSERR;
405 }
406
407 struct GNUNET_CRYPTO_SymmetricSessionKey key;
408 GNUNET_CRYPTO_symmetric_create_session_key(&key);
409
410 if (GNUNET_OK != util_encrypt_file(filename, &key))
411 {
412 GNUNET_free(filename);
413 return GNUNET_SYSERR;
414 }
415
416 char* p = GNUNET_strdup(path);
386 417
387 // TODO: encrypt file, publish file, share file 418 struct GNUNET_CHAT_File *file = file_create_from_disk(
419 context->handle, basename(p), &hash, &key
420 );
421
422 GNUNET_free(p);
423
424 struct GNUNET_FS_BlockOptions bo;
425
426 bo.anonymity_level = 1;
427 bo.content_priority = 100;
428 bo.replication_level = 1;
429
430 bo.expiration_time = GNUNET_TIME_absolute_add(
431 GNUNET_TIME_absolute_get(), GNUNET_TIME_relative_get_hour_()
432 );
433
434 struct GNUNET_FS_FileInformation* fi = GNUNET_FS_file_information_create_from_file(
435 context->handle->fs,
436 file,
437 filename,
438 NULL,
439 file->meta,
440 GNUNET_YES,
441 &bo
442 );
443
444 file->publish = GNUNET_FS_publish_start(
445 context->handle->fs, fi,
446 NULL, NULL, NULL,
447 GNUNET_FS_PUBLISH_OPTION_NONE
448 );
449
450 GNUNET_free(filename);
451
452 // TODO: share file
453
454 return GNUNET_OK;
388} 455}
389 456
390 457
391void 458int
392GNUNET_CHAT_context_send_uri (struct GNUNET_CHAT_Context *context, 459GNUNET_CHAT_context_send_uri (struct GNUNET_CHAT_Context *context,
393 const char *uri) 460 const char *uri)
394{ 461{
395 if ((!context) || (!uri)) 462 if ((!context) || (!uri))
396 return; 463 return GNUNET_SYSERR;
397 464
398 struct GNUNET_FS_Uri *furi = GNUNET_FS_uri_parse(uri, NULL); 465 struct GNUNET_FS_Uri *furi = GNUNET_FS_uri_parse(uri, NULL);
399 466
400 if (!furi) 467 if (!furi)
401 return; 468 return GNUNET_SYSERR;
402 469
403 // TODO: download file, hash file, share file 470 // TODO: download file, hash file, share file
471
472 return GNUNET_SYSERR;
404} 473}
405 474
406 475
407void 476int
408GNUNET_CHAT_context_share_file (struct GNUNET_CHAT_Context *context, 477GNUNET_CHAT_context_share_file (struct GNUNET_CHAT_Context *context,
409 const struct GNUNET_CHAT_File *file) 478 const struct GNUNET_CHAT_File *file)
410{ 479{
411 if ((!context) || (!file) || (strlen(file->name) > NAME_MAX)) 480 if ((!context) || (!file) || (strlen(file->name) > NAME_MAX))
412 return; 481 return GNUNET_SYSERR;
413 482
414 struct GNUNET_MESSENGER_Message msg; 483 struct GNUNET_MESSENGER_Message msg;
415 msg.header.kind = GNUNET_MESSENGER_KIND_FILE; 484 msg.header.kind = GNUNET_MESSENGER_KIND_FILE;
@@ -421,26 +490,10 @@ GNUNET_CHAT_context_share_file (struct GNUNET_CHAT_Context *context,
421 GNUNET_MESSENGER_send_message(context->room, &msg, NULL); 490 GNUNET_MESSENGER_send_message(context->room, &msg, NULL);
422 491
423 GNUNET_free(msg.body.file.uri); 492 GNUNET_free(msg.body.file.uri);
493 return GNUNET_OK;
424} 494}
425 495
426 496
427void
428GNUNET_CHAT_context_delete_message (const struct GNUNET_CHAT_Message *message,
429 struct GNUNET_TIME_Relative delay)
430{
431 if (!message)
432 return;
433
434 struct GNUNET_MESSENGER_Message msg;
435 msg.header.kind = GNUNET_MESSENGER_KIND_DELETE;
436 GNUNET_memcpy(&(msg.body.delete.hash), &(message->hash), sizeof(message->hash));
437 msg.body.delete.delay = GNUNET_TIME_relative_hton(delay);
438
439 GNUNET_MESSENGER_send_message(message->context->room, &msg, NULL);
440}
441
442
443
444int 497int
445GNUNET_CHAT_context_iterate_messages (struct GNUNET_CHAT_Context *context, 498GNUNET_CHAT_context_iterate_messages (struct GNUNET_CHAT_Context *context,
446 GNUNET_CHAT_ContextMessageCallback callback, 499 GNUNET_CHAT_ContextMessageCallback callback,
@@ -571,6 +624,23 @@ GNUNET_CHAT_message_get_invitation (const struct GNUNET_CHAT_Message *message)
571} 624}
572 625
573 626
627int
628GNUNET_CHAT_message_delete (const struct GNUNET_CHAT_Message *message,
629 struct GNUNET_TIME_Relative delay)
630{
631 if (!message)
632 return GNUNET_SYSERR;
633
634 struct GNUNET_MESSENGER_Message msg;
635 msg.header.kind = GNUNET_MESSENGER_KIND_DELETE;
636 GNUNET_memcpy(&(msg.body.delete.hash), &(message->hash), sizeof(message->hash));
637 msg.body.delete.delay = GNUNET_TIME_relative_hton(delay);
638
639 GNUNET_MESSENGER_send_message(message->context->room, &msg, NULL);
640 return GNUNET_OK;
641}
642
643
574const struct GNUNET_HashCode* 644const struct GNUNET_HashCode*
575GNUNET_CHAT_file_get_hash (const struct GNUNET_CHAT_File *file) 645GNUNET_CHAT_file_get_hash (const struct GNUNET_CHAT_File *file)
576{ 646{
@@ -590,11 +660,15 @@ GNUNET_CHAT_file_get_size (const struct GNUNET_CHAT_File *file)
590 if (file->uri) 660 if (file->uri)
591 return GNUNET_FS_uri_chk_get_file_size(file->uri); 661 return GNUNET_FS_uri_chk_get_file_size(file->uri);
592 662
593 const char *path = ""; // TODO: path = download_directory + file->name 663 char *filename;
664 util_get_filename (file->handle->directory, &(file->hash), &filename);
594 665
595 // TODO: check size through info or check locally? 666 uint64_t size;
667 if (GNUNET_OK != GNUNET_DISK_file_size(filename, &size, GNUNET_NO, GNUNET_YES))
668 size = 0;
596 669
597 return 0; 670 GNUNET_free(filename);
671 return size;
598} 672}
599 673
600 674
@@ -604,11 +678,13 @@ GNUNET_CHAT_file_is_local (const struct GNUNET_CHAT_File *file)
604 if (!file) 678 if (!file)
605 return GNUNET_SYSERR; 679 return GNUNET_SYSERR;
606 680
607 const char *path = ""; // TODO: path = download_directory + file->name 681 char *filename;
682 util_get_filename (file->handle->directory, &(file->hash), &filename);
608 683
609 // TODO: check locally? 684 int result = GNUNET_DISK_file_test(filename);
610 685
611 return GNUNET_SYSERR; 686 GNUNET_free(filename);
687 return result;
612} 688}
613 689
614 690
@@ -617,27 +693,44 @@ GNUNET_CHAT_file_start_download (struct GNUNET_CHAT_File *file,
617 GNUNET_CHAT_MessageFileDownloadCallback callback, 693 GNUNET_CHAT_MessageFileDownloadCallback callback,
618 void *cls) 694 void *cls)
619{ 695{
620 if (!file) 696 if ((!file) || (!(file->uri)))
621 return GNUNET_SYSERR; 697 return GNUNET_SYSERR;
622 698
623 // TODO: check if downloading? 699 if (file->download)
700 {
701 GNUNET_FS_download_resume(file->download);
702 return GNUNET_OK;
703 }
704
705 const uint64_t size = GNUNET_FS_uri_chk_get_file_size(file->uri);
624 706
625 const char *path = ""; // TODO: path = download_directory + file->name 707 char *filename;
708 util_get_filename (file->handle->directory, &(file->hash), &filename);
709
710 uint64_t offset;
711 if (GNUNET_OK != GNUNET_DISK_file_size(filename, &offset, GNUNET_NO, GNUNET_YES))
712 offset = 0;
713
714 if (offset >= size)
715 return GNUNET_OK;
716
717 const uint64_t remaining = (size - offset);
626 718
627 file->download = GNUNET_FS_download_start( 719 file->download = GNUNET_FS_download_start(
628 file->handle->fs, 720 file->handle->fs,
629 file->uri, 721 file->uri,
722 file->meta,
723 filename,
630 NULL, 724 NULL,
631 path, 725 offset,
632 NULL, 726 remaining,
633 0, 727 1,
634 0,
635 0,
636 GNUNET_FS_DOWNLOAD_OPTION_NONE, 728 GNUNET_FS_DOWNLOAD_OPTION_NONE,
637 NULL, 729 file,
638 NULL 730 NULL
639 ); 731 );
640 732
733 GNUNET_free(filename);
641 return GNUNET_OK; 734 return GNUNET_OK;
642} 735}
643 736
@@ -676,6 +769,34 @@ GNUNET_CHAT_file_stop_download (struct GNUNET_CHAT_File *file)
676} 769}
677 770
678 771
772int
773GNUNET_CHAT_file_unindex (struct GNUNET_CHAT_File *file)
774{
775 if (!file)
776 return GNUNET_SYSERR;
777
778 if (file->publish)
779 {
780 GNUNET_FS_publish_stop(file->publish);
781 file->publish = NULL;
782 return GNUNET_OK;
783 }
784
785 if (file->unindex)
786 return GNUNET_OK;
787
788 char *filename;
789 util_get_filename (file->handle->directory, &(file->hash), &filename);
790
791 file->unindex = GNUNET_FS_unindex_start(
792 file->handle->fs, filename, file
793 );
794
795 GNUNET_free(filename);
796 return GNUNET_OK;
797}
798
799
679void 800void
680GNUNET_CHAT_invitation_accept (struct GNUNET_CHAT_Invitation *invitation) 801GNUNET_CHAT_invitation_accept (struct GNUNET_CHAT_Invitation *invitation)
681{ 802{