summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorirungentoo <irungentoo@gmail.com>2015-02-17 16:21:47 -0500
committerirungentoo <irungentoo@gmail.com>2015-02-17 16:21:47 -0500
commit61ca9529a1314aadd9085ce1b7ffdf8b6bce7c81 (patch)
treea8a579f276d0328d843d8e42f8d3f7ecc77854b1
parentc6a59e64b7a7fb86ed8a99c21a0b7b023559b0ea (diff)
More new api functions implemented.
Some small self explainatory changes to the api.
-rw-r--r--toxcore/tox.c239
-rw-r--r--toxcore/tox.h18
2 files changed, 251 insertions, 6 deletions
diff --git a/toxcore/tox.c b/toxcore/tox.c
index 71397b9e..a9c7d4dd 100644
--- a/toxcore/tox.c
+++ b/toxcore/tox.c
@@ -407,3 +407,242 @@ void tox_self_get_status_message(Tox const *tox, uint8_t *status)
407 m_copy_self_statusmessage(m, status); 407 m_copy_self_statusmessage(m, status);
408 } 408 }
409} 409}
410
411void tox_self_set_status(Tox *tox, TOX_STATUS user_status)
412{
413 Messenger *m = tox;
414 m_set_userstatus(m, user_status);
415}
416
417TOX_STATUS tox_self_get_status(Tox const *tox)
418{
419 const Messenger *m = tox;
420 return m_get_self_userstatus(m);
421}
422
423static void set_friend_error(int32_t ret, TOX_ERR_FRIEND_ADD *error)
424{
425 switch (ret) {
426 case FAERR_TOOLONG:
427 SET_ERROR_PARAMETER(error, TOX_ERR_FRIEND_ADD_TOO_LONG);
428 break;
429
430 case FAERR_NOMESSAGE:
431 SET_ERROR_PARAMETER(error, TOX_ERR_FRIEND_ADD_NO_MESSAGE);
432 break;
433
434 case FAERR_OWNKEY:
435 SET_ERROR_PARAMETER(error, TOX_ERR_FRIEND_ADD_OWN_KEY);
436 break;
437
438 case FAERR_ALREADYSENT:
439 SET_ERROR_PARAMETER(error, TOX_ERR_FRIEND_ADD_ALREADY_SENT);
440 break;
441
442 case FAERR_BADCHECKSUM:
443 SET_ERROR_PARAMETER(error, TOX_ERR_FRIEND_ADD_BAD_CHECKSUM);
444 break;
445
446 case FAERR_SETNEWNOSPAM:
447 SET_ERROR_PARAMETER(error, TOX_ERR_FRIEND_ADD_SET_NEW_NOSPAM);
448 break;
449
450 case FAERR_NOMEM:
451 SET_ERROR_PARAMETER(error, TOX_ERR_FRIEND_ADD_MALLOC);
452 break;
453
454 }
455}
456
457uint32_t tox_friend_add(Tox *tox, uint8_t const *address, uint8_t const *message, size_t length,
458 TOX_ERR_FRIEND_ADD *error)
459{
460 if (!address || !message) {
461 SET_ERROR_PARAMETER(error, TOX_ERR_FRIEND_ADD_NULL);
462 return UINT32_MAX;
463 }
464
465 Messenger *m = tox;
466 int32_t ret = m_addfriend(m, address, message, length);
467
468 if (ret >= 0) {
469 SET_ERROR_PARAMETER(error, TOX_ERR_FRIEND_ADD_OK);
470 return ret;
471 }
472
473 set_friend_error(ret, error);
474 return UINT32_MAX;
475}
476
477uint32_t tox_friend_add_norequest(Tox *tox, uint8_t const *public_key, TOX_ERR_FRIEND_ADD *error)
478{
479 if (!public_key) {
480 SET_ERROR_PARAMETER(error, TOX_ERR_FRIEND_ADD_NULL);
481 return UINT32_MAX;
482 }
483
484 Messenger *m = tox;
485 int32_t ret = m_addfriend_norequest(m, public_key);
486
487 if (ret >= 0) {
488 SET_ERROR_PARAMETER(error, TOX_ERR_FRIEND_ADD_OK);
489 return ret;
490 }
491
492 set_friend_error(ret, error);
493 return UINT32_MAX;
494}
495
496bool tox_friend_delete(Tox *tox, uint32_t friend_number, TOX_ERR_FRIEND_DELETE *error)
497{
498 Messenger *m = tox;
499 int ret = m_delfriend(m, friend_number);
500
501 //TODO handle if realloc fails?
502 if (ret == -1) {
503 SET_ERROR_PARAMETER(error, TOX_ERR_FRIEND_DELETE_FRIEND_NOT_FOUND);
504 return 0;
505 }
506
507 SET_ERROR_PARAMETER(error, TOX_ERR_FRIEND_DELETE_OK);
508 return 1;
509}
510
511uint32_t tox_friend_by_public_key(Tox const *tox, uint8_t const *public_key, TOX_ERR_FRIEND_BY_PUBLIC_KEY *error)
512{
513 if (!public_key) {
514 SET_ERROR_PARAMETER(error, TOX_ERR_FRIEND_BY_PUBLIC_KEY_NULL);
515 return UINT32_MAX;
516 }
517
518 const Messenger *m = tox;
519 int32_t ret = getfriend_id(m, public_key);
520
521 if (ret == -1) {
522 SET_ERROR_PARAMETER(error, TOX_ERR_FRIEND_BY_PUBLIC_KEY_NOT_FOUND);
523 return UINT32_MAX;
524 }
525
526 SET_ERROR_PARAMETER(error, TOX_ERR_FRIEND_BY_PUBLIC_KEY_OK);
527 return ret;
528}
529
530bool tox_friend_get_public_key(Tox const *tox, uint32_t friend_number, uint8_t *public_key,
531 TOX_ERR_FRIEND_GET_PUBLIC_KEY *error)
532{
533 if (!public_key) {
534 SET_ERROR_PARAMETER(error, TOX_ERR_FRIEND_GET_PUBLIC_KEY_NULL);
535 return 0;
536 }
537
538 const Messenger *m = tox;
539
540 if (get_real_pk(m, friend_number, public_key) == -1) {
541 SET_ERROR_PARAMETER(error, TOX_ERR_FRIEND_GET_PUBLIC_KEY_FRIEND_NOT_FOUND);
542 return 0;
543 }
544
545 SET_ERROR_PARAMETER(error, TOX_ERR_FRIEND_GET_PUBLIC_KEY_OK);
546 return 1;
547}
548
549bool tox_friend_exists(Tox const *tox, uint32_t friend_number)
550{
551 const Messenger *m = tox;
552 return m_friend_exists(m, friend_number);
553}
554
555size_t tox_friend_list_size(Tox const *tox)
556{
557 const Messenger *m = tox;
558 return count_friendlist(m);
559}
560
561void tox_friend_list(Tox const *tox, uint32_t *list)
562{
563 if (list) {
564 const Messenger *m = tox;
565 //TODO: size parameter?
566 copy_friendlist(m, list, tox_friend_list_size(tox));
567 }
568}
569
570size_t tox_friend_get_name_size(Tox const *tox, uint32_t friend_number, TOX_ERR_FRIEND_QUERY *error)
571{
572 const Messenger *m = tox;
573 int ret = m_get_name_size(m, friend_number);
574
575 if (ret == -1) {
576 SET_ERROR_PARAMETER(error, TOX_ERR_FRIEND_QUERY_FRIEND_NOT_FOUND);
577 return SIZE_MAX;
578 }
579
580 SET_ERROR_PARAMETER(error, TOX_ERR_FRIEND_QUERY_OK);
581 return ret;
582}
583
584bool tox_friend_get_name(Tox const *tox, uint32_t friend_number, uint8_t *name, TOX_ERR_FRIEND_QUERY *error)
585{
586 if (!name) {
587 SET_ERROR_PARAMETER(error, TOX_ERR_FRIEND_QUERY_NULL);
588 return 0;
589 }
590
591 const Messenger *m = tox;
592 int ret = getname(m, friend_number, name);
593
594 if (ret == -1) {
595 SET_ERROR_PARAMETER(error, TOX_ERR_FRIEND_QUERY_FRIEND_NOT_FOUND);
596 return 0;
597 }
598
599 SET_ERROR_PARAMETER(error, TOX_ERR_FRIEND_QUERY_OK);
600 return 1;
601}
602
603void tox_callback_friend_name(Tox *tox, tox_friend_name_cb *function, void *user_data)
604{
605 Messenger *m = tox;
606 m_callback_friendmessage(m, function, user_data);
607}
608
609size_t tox_friend_get_status_message_size(Tox const *tox, uint32_t friend_number, TOX_ERR_FRIEND_QUERY *error)
610{
611 const Messenger *m = tox;
612 int ret = m_get_statusmessage_size(m, friend_number);
613
614 if (ret == -1) {
615 SET_ERROR_PARAMETER(error, TOX_ERR_FRIEND_QUERY_FRIEND_NOT_FOUND);
616 return SIZE_MAX;
617 }
618
619 SET_ERROR_PARAMETER(error, TOX_ERR_FRIEND_QUERY_OK);
620 return ret;
621}
622
623bool tox_friend_get_status_message(Tox const *tox, uint32_t friend_number, uint8_t *message,
624 TOX_ERR_FRIEND_QUERY *error)
625{
626 if (!message) {
627 SET_ERROR_PARAMETER(error, TOX_ERR_FRIEND_QUERY_NULL);
628 return 0;
629 }
630
631 const Messenger *m = tox;
632 //TODO: size parameter?
633 int ret = m_copy_statusmessage(m, friend_number, message, m_get_statusmessage_size(m, friend_number));
634
635 if (ret == -1) {
636 SET_ERROR_PARAMETER(error, TOX_ERR_FRIEND_QUERY_FRIEND_NOT_FOUND);
637 return 0;
638 }
639
640 SET_ERROR_PARAMETER(error, TOX_ERR_FRIEND_QUERY_OK);
641 return 1;
642}
643
644void tox_callback_friend_status_message(Tox *tox, tox_friend_status_message_cb *function, void *user_data)
645{
646 Messenger *m = tox;
647 m_callback_statusmessage(m, function, user_data);
648}
diff --git a/toxcore/tox.h b/toxcore/tox.h
index 2dc1594c..1a189ae3 100644
--- a/toxcore/tox.h
+++ b/toxcore/tox.h
@@ -274,7 +274,11 @@ typedef enum TOX_STATUS {
274 * User is busy. Signals to other clients that this client does not 274 * User is busy. Signals to other clients that this client does not
275 * currently wish to communicate. 275 * currently wish to communicate.
276 */ 276 */
277 TOX_STATUS_BUSY 277 TOX_STATUS_BUSY,
278 /**
279 * Invalid status used when function returns an error.
280 */
281 TOX_STATUS_INVALID
278} TOX_STATUS; 282} TOX_STATUS;
279 283
280 284
@@ -833,7 +837,7 @@ typedef enum TOX_ERR_FRIEND_ADD {
833 * be the same as before. Deleting a friend creates a gap in the friend number 837 * be the same as before. Deleting a friend creates a gap in the friend number
834 * set, which is filled by the next adding of a friend. 838 * set, which is filled by the next adding of a friend.
835 * 839 *
836 * If more than UINT32_MAX friends are added, this function causes undefined 840 * If more than INT32_MAX friends are added, this function causes undefined
837 * behaviour. 841 * behaviour.
838 * 842 *
839 * @param address The address of the friend (returned by tox_self_get_address of 843 * @param address The address of the friend (returned by tox_self_get_address of
@@ -841,7 +845,7 @@ typedef enum TOX_ERR_FRIEND_ADD {
841 * @param message The message that will be sent along with the friend request. 845 * @param message The message that will be sent along with the friend request.
842 * @param length The length of the data byte array. 846 * @param length The length of the data byte array.
843 * 847 *
844 * @return the friend number. 848 * @return the friend number on success, UINT32_MAX on failure.
845 */ 849 */
846uint32_t tox_friend_add(Tox *tox, uint8_t const *address, uint8_t const *message, size_t length, 850uint32_t tox_friend_add(Tox *tox, uint8_t const *address, uint8_t const *message, size_t length,
847 TOX_ERR_FRIEND_ADD *error); 851 TOX_ERR_FRIEND_ADD *error);
@@ -862,7 +866,7 @@ uint32_t tox_friend_add(Tox *tox, uint8_t const *address, uint8_t const *message
862 * @param public_key A byte array of length TOX_PUBLIC_KEY_SIZE containing the 866 * @param public_key A byte array of length TOX_PUBLIC_KEY_SIZE containing the
863 * Public Key (not the Address) of the friend to add. 867 * Public Key (not the Address) of the friend to add.
864 * 868 *
865 * @return the friend number. 869 * @return the friend number on success, UINT32_MAX on failure.
866 * @see tox_friend_add for a more detailed description of friend numbers. 870 * @see tox_friend_add for a more detailed description of friend numbers.
867 */ 871 */
868uint32_t tox_friend_add_norequest(Tox *tox, uint8_t const *public_key, TOX_ERR_FRIEND_ADD *error); 872uint32_t tox_friend_add_norequest(Tox *tox, uint8_t const *public_key, TOX_ERR_FRIEND_ADD *error);
@@ -909,6 +913,7 @@ typedef enum TOX_ERR_FRIEND_BY_PUBLIC_KEY {
909/** 913/**
910 * Return the friend number associated with that Public Key. 914 * Return the friend number associated with that Public Key.
911 * 915 *
916 * @return the friend number on success, UINT32_MAX on failure.
912 * @param public_key A byte array containing the Public Key. 917 * @param public_key A byte array containing the Public Key.
913 */ 918 */
914uint32_t tox_friend_by_public_key(Tox const *tox, uint8_t const *public_key, TOX_ERR_FRIEND_BY_PUBLIC_KEY *error); 919uint32_t tox_friend_by_public_key(Tox const *tox, uint8_t const *public_key, TOX_ERR_FRIEND_BY_PUBLIC_KEY *error);
@@ -916,6 +921,7 @@ uint32_t tox_friend_by_public_key(Tox const *tox, uint8_t const *public_key, TOX
916 921
917typedef enum TOX_ERR_FRIEND_GET_PUBLIC_KEY { 922typedef enum TOX_ERR_FRIEND_GET_PUBLIC_KEY {
918 TOX_ERR_FRIEND_GET_PUBLIC_KEY_OK, 923 TOX_ERR_FRIEND_GET_PUBLIC_KEY_OK,
924 TOX_ERR_FRIEND_GET_PUBLIC_KEY_NULL,
919 /** 925 /**
920 * No friend with the given number exists on the friend list. 926 * No friend with the given number exists on the friend list.
921 */ 927 */
@@ -990,7 +996,7 @@ typedef enum TOX_ERR_FRIEND_QUERY {
990 996
991/** 997/**
992 * Return the length of the friend's name. If the friend number is invalid, the 998 * Return the length of the friend's name. If the friend number is invalid, the
993 * return value is unspecified. 999 * return value is SIZE_MAX.
994 * 1000 *
995 * The return value is equal to the `length` argument received by the last 1001 * The return value is equal to the `length` argument received by the last
996 * `friend_name` callback. 1002 * `friend_name` callback.
@@ -1034,7 +1040,7 @@ void tox_callback_friend_name(Tox *tox, tox_friend_name_cb *function, void *user
1034 1040
1035/** 1041/**
1036 * Return the length of the friend's status message. If the friend number is 1042 * Return the length of the friend's status message. If the friend number is
1037 * invalid, the return value is unspecified. 1043 * invalid, the return value is SIZE_MAX.
1038 */ 1044 */
1039size_t tox_friend_get_status_message_size(Tox const *tox, uint32_t friend_number, TOX_ERR_FRIEND_QUERY *error); 1045size_t tox_friend_get_status_message_size(Tox const *tox, uint32_t friend_number, TOX_ERR_FRIEND_QUERY *error);
1040 1046