summaryrefslogtreecommitdiff
path: root/toxcore/tox.c
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 /toxcore/tox.c
parentc6a59e64b7a7fb86ed8a99c21a0b7b023559b0ea (diff)
More new api functions implemented.
Some small self explainatory changes to the api.
Diffstat (limited to 'toxcore/tox.c')
-rw-r--r--toxcore/tox.c239
1 files changed, 239 insertions, 0 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}