diff options
Diffstat (limited to 'core/Messenger.c')
-rw-r--r-- | core/Messenger.c | 39 |
1 files changed, 17 insertions, 22 deletions
diff --git a/core/Messenger.c b/core/Messenger.c index 07145416..690a81b1 100644 --- a/core/Messenger.c +++ b/core/Messenger.c | |||
@@ -75,9 +75,9 @@ int getclient_id(Messenger *m, int friend_id, uint8_t *client_id) | |||
75 | 75 | ||
76 | return -1; | 76 | return -1; |
77 | } | 77 | } |
78 | /* | 78 | /* |
79 | * returns a uint16_t that represents the checksum of address of length len | 79 | * returns a uint16_t that represents the checksum of address of length len |
80 | * | 80 | * |
81 | * TODO: Another checksum algorithm might be better. | 81 | * TODO: Another checksum algorithm might be better. |
82 | */ | 82 | */ |
83 | static uint16_t address_checksum(uint8_t *address, uint32_t len) | 83 | static uint16_t address_checksum(uint8_t *address, uint32_t len) |
@@ -94,7 +94,7 @@ static uint16_t address_checksum(uint8_t *address, uint32_t len) | |||
94 | /* | 94 | /* |
95 | * returns a FRIEND_ADDRESS_SIZE byte address to give to others. | 95 | * returns a FRIEND_ADDRESS_SIZE byte address to give to others. |
96 | * format: [client_id (32 bytes)][nospam number (4 bytes)][checksum (2 bytes)] | 96 | * format: [client_id (32 bytes)][nospam number (4 bytes)][checksum (2 bytes)] |
97 | * | 97 | * |
98 | */ | 98 | */ |
99 | void getaddress(Messenger *m, uint8_t *address) | 99 | void getaddress(Messenger *m, uint8_t *address) |
100 | { | 100 | { |
@@ -118,7 +118,7 @@ void getaddress(Messenger *m, uint8_t *address) | |||
118 | * return FAERR_ALREADYSENT if friend request already sent or already a friend | 118 | * return FAERR_ALREADYSENT if friend request already sent or already a friend |
119 | * return FAERR_UNKNOWN for unknown error | 119 | * return FAERR_UNKNOWN for unknown error |
120 | * return FAERR_BADCHECKSUM if bad checksum in address | 120 | * return FAERR_BADCHECKSUM if bad checksum in address |
121 | * return FAERR_SETNEWNOSPAM if the friend was already there but the nospam was different | 121 | * return FAERR_SETNEWNOSPAM if the friend was already there but the nospam was different |
122 | * (the nospam for that friend was set to the new one) | 122 | * (the nospam for that friend was set to the new one) |
123 | */ | 123 | */ |
124 | int m_addfriend(Messenger *m, uint8_t *address, uint8_t *data, uint16_t length) | 124 | int m_addfriend(Messenger *m, uint8_t *address, uint8_t *data, uint16_t length) |
@@ -449,8 +449,7 @@ void m_set_sends_receipts(Messenger *m, int friendnumber, int yesno) | |||
449 | m->friendlist[friendnumber].receives_read_receipts = yesno; | 449 | m->friendlist[friendnumber].receives_read_receipts = yesno; |
450 | } | 450 | } |
451 | 451 | ||
452 | /* static void (*friend_request)(uint8_t *, uint8_t *, uint16_t); | 452 | /* static void (*friend_request)(uint8_t *, uint8_t *, uint16_t); */ |
453 | static uint8_t friend_request_isset = 0; */ | ||
454 | /* set the function that will be executed when a friend request is received. */ | 453 | /* set the function that will be executed when a friend request is received. */ |
455 | void m_callback_friendrequest(Messenger *m, void (*function)(uint8_t *, uint8_t *, uint16_t, void*), void* userdata) | 454 | void m_callback_friendrequest(Messenger *m, void (*function)(uint8_t *, uint8_t *, uint16_t, void*), void* userdata) |
456 | { | 455 | { |
@@ -461,55 +460,48 @@ void m_callback_friendrequest(Messenger *m, void (*function)(uint8_t *, uint8_t | |||
461 | void m_callback_friendmessage(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t, void*), void* userdata) | 460 | void m_callback_friendmessage(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t, void*), void* userdata) |
462 | { | 461 | { |
463 | m->friend_message = function; | 462 | m->friend_message = function; |
464 | m->friend_message_isset = 1; | ||
465 | m->friend_message_userdata = userdata; | 463 | m->friend_message_userdata = userdata; |
466 | } | 464 | } |
467 | 465 | ||
468 | void m_callback_action(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t, void*), void* userdata) | 466 | void m_callback_action(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t, void*), void* userdata) |
469 | { | 467 | { |
470 | m->friend_action = function; | 468 | m->friend_action = function; |
471 | m->friend_action_isset = 1; | ||
472 | m->friend_action_userdata = userdata; | 469 | m->friend_action_userdata = userdata; |
473 | } | 470 | } |
474 | 471 | ||
475 | void m_callback_namechange(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t, void*), void* userdata) | 472 | void m_callback_namechange(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t, void*), void* userdata) |
476 | { | 473 | { |
477 | m->friend_namechange = function; | 474 | m->friend_namechange = function; |
478 | m->friend_namechange_isset = 1; | ||
479 | m->friend_namechange_userdata = userdata; | 475 | m->friend_namechange_userdata = userdata; |
480 | } | 476 | } |
481 | 477 | ||
482 | void m_callback_statusmessage(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t, void*), void* userdata) | 478 | void m_callback_statusmessage(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t, void*), void* userdata) |
483 | { | 479 | { |
484 | m->friend_statusmessagechange = function; | 480 | m->friend_statusmessagechange = function; |
485 | m->friend_statusmessagechange_isset = 1; | ||
486 | m->friend_statuschange_userdata = userdata; | 481 | m->friend_statuschange_userdata = userdata; |
487 | } | 482 | } |
488 | 483 | ||
489 | void m_callback_userstatus(Messenger *m, void (*function)(Messenger *m, int, USERSTATUS, void*), void* userdata) | 484 | void m_callback_userstatus(Messenger *m, void (*function)(Messenger *m, int, USERSTATUS, void*), void* userdata) |
490 | { | 485 | { |
491 | m->friend_userstatuschange = function; | 486 | m->friend_userstatuschange = function; |
492 | m->friend_userstatuschange_isset = 1; | ||
493 | m->friend_userstatuschange_userdata = userdata; | 487 | m->friend_userstatuschange_userdata = userdata; |
494 | } | 488 | } |
495 | 489 | ||
496 | void m_callback_read_receipt(Messenger *m, void (*function)(Messenger *m, int, uint32_t, void*), void* userdata) | 490 | void m_callback_read_receipt(Messenger *m, void (*function)(Messenger *m, int, uint32_t, void*), void* userdata) |
497 | { | 491 | { |
498 | m->read_receipt = function; | 492 | m->read_receipt = function; |
499 | m->read_receipt_isset = 1; | ||
500 | m->read_receipt_userdata = userdata; | 493 | m->read_receipt_userdata = userdata; |
501 | } | 494 | } |
502 | 495 | ||
503 | void m_callback_connectionstatus(Messenger *m, void (*function)(Messenger *m, int, uint8_t, void*), void* userdata) | 496 | void m_callback_connectionstatus(Messenger *m, void (*function)(Messenger *m, int, uint8_t, void*), void* userdata) |
504 | { | 497 | { |
505 | m->friend_connectionstatuschange = function; | 498 | m->friend_connectionstatuschange = function; |
506 | m->friend_connectionstatuschange_isset = 1; | ||
507 | m->friend_connectionstatuschange_userdata = userdata; | 499 | m->friend_connectionstatuschange_userdata = userdata; |
508 | } | 500 | } |
509 | 501 | ||
510 | static void check_friend_connectionstatus(Messenger *m, int friendnumber, uint8_t status) | 502 | static void check_friend_connectionstatus(Messenger *m, int friendnumber, uint8_t status) |
511 | { | 503 | { |
512 | if (!m->friend_connectionstatuschange_isset) | 504 | if (!m->friend_connectionstatuschange) |
513 | return; | 505 | return; |
514 | if (status == NOFRIEND) | 506 | if (status == NOFRIEND) |
515 | return; | 507 | return; |
@@ -580,7 +572,10 @@ Messenger * initMessenger(void) | |||
580 | 572 | ||
581 | /* run this before closing shop */ | 573 | /* run this before closing shop */ |
582 | void cleanupMessenger(Messenger *m){ | 574 | void cleanupMessenger(Messenger *m){ |
583 | /* FIXME TODO it seems no one frees friendlist or all the elements status */ | 575 | /* FIXME TODO ideally cleanupMessenger will mirror initMessenger |
576 | * this requires the other modules to expose cleanup functions | ||
577 | */ | ||
578 | free(m->friendlist); | ||
584 | free(m); | 579 | free(m); |
585 | } | 580 | } |
586 | 581 | ||
@@ -648,7 +643,7 @@ void doFriends(Messenger *m) | |||
648 | case PACKET_ID_NICKNAME: { | 643 | case PACKET_ID_NICKNAME: { |
649 | if (data_length >= MAX_NAME_LENGTH || data_length == 0) | 644 | if (data_length >= MAX_NAME_LENGTH || data_length == 0) |
650 | break; | 645 | break; |
651 | if(m->friend_namechange_isset) | 646 | if(m->friend_namechange) |
652 | m->friend_namechange(m, i, data, data_length, m->friend_namechange_userdata); | 647 | m->friend_namechange(m, i, data, data_length, m->friend_namechange_userdata); |
653 | memcpy(m->friendlist[i].name, data, data_length); | 648 | memcpy(m->friendlist[i].name, data, data_length); |
654 | m->friendlist[i].name[data_length - 1] = 0; /* make sure the NULL terminator is present. */ | 649 | m->friendlist[i].name[data_length - 1] = 0; /* make sure the NULL terminator is present. */ |
@@ -659,7 +654,7 @@ void doFriends(Messenger *m) | |||
659 | break; | 654 | break; |
660 | uint8_t *status = calloc(MIN(data_length, MAX_STATUSMESSAGE_LENGTH), 1); | 655 | uint8_t *status = calloc(MIN(data_length, MAX_STATUSMESSAGE_LENGTH), 1); |
661 | memcpy(status, data, MIN(data_length, MAX_STATUSMESSAGE_LENGTH)); | 656 | memcpy(status, data, MIN(data_length, MAX_STATUSMESSAGE_LENGTH)); |
662 | if (m->friend_statusmessagechange_isset) | 657 | if (m->friend_statusmessagechange) |
663 | m->friend_statusmessagechange(m, i, status, MIN(data_length, MAX_STATUSMESSAGE_LENGTH), | 658 | m->friend_statusmessagechange(m, i, status, MIN(data_length, MAX_STATUSMESSAGE_LENGTH), |
664 | m->friend_statuschange_userdata); | 659 | m->friend_statuschange_userdata); |
665 | set_friend_statusmessage(m, i, status, MIN(data_length, MAX_STATUSMESSAGE_LENGTH)); | 660 | set_friend_statusmessage(m, i, status, MIN(data_length, MAX_STATUSMESSAGE_LENGTH)); |
@@ -670,7 +665,7 @@ void doFriends(Messenger *m) | |||
670 | if (data_length != 1) | 665 | if (data_length != 1) |
671 | break; | 666 | break; |
672 | USERSTATUS status = data[0]; | 667 | USERSTATUS status = data[0]; |
673 | if (m->friend_userstatuschange_isset) | 668 | if (m->friend_userstatuschange) |
674 | m->friend_userstatuschange(m, i, status, m->friend_userstatuschange_userdata); | 669 | m->friend_userstatuschange(m, i, status, m->friend_userstatuschange_userdata); |
675 | set_friend_userstatus(m, i, status); | 670 | set_friend_userstatus(m, i, status); |
676 | break; | 671 | break; |
@@ -683,12 +678,12 @@ void doFriends(Messenger *m) | |||
683 | if (m->friendlist[i].receives_read_receipts) { | 678 | if (m->friendlist[i].receives_read_receipts) { |
684 | write_cryptpacket_id(m, i, PACKET_ID_RECEIPT, message_id, message_id_length); | 679 | write_cryptpacket_id(m, i, PACKET_ID_RECEIPT, message_id, message_id_length); |
685 | } | 680 | } |
686 | if (m->friend_message_isset) | 681 | if (m->friend_message) |
687 | (*m->friend_message)(m, i, message, message_length, m->friend_message_userdata); | 682 | (*m->friend_message)(m, i, message, message_length, m->friend_message_userdata); |
688 | break; | 683 | break; |
689 | } | 684 | } |
690 | case PACKET_ID_ACTION: { | 685 | case PACKET_ID_ACTION: { |
691 | if (m->friend_action_isset) | 686 | if (m->friend_action) |
692 | (*m->friend_action)(m, i, data, data_length, m->friend_action_userdata); | 687 | (*m->friend_action)(m, i, data, data_length, m->friend_action_userdata); |
693 | break; | 688 | break; |
694 | } | 689 | } |
@@ -698,7 +693,7 @@ void doFriends(Messenger *m) | |||
698 | break; | 693 | break; |
699 | memcpy(&msgid, data, sizeof(msgid)); | 694 | memcpy(&msgid, data, sizeof(msgid)); |
700 | msgid = ntohl(msgid); | 695 | msgid = ntohl(msgid); |
701 | if (m->read_receipt_isset) | 696 | if (m->read_receipt) |
702 | (*m->read_receipt)(m, i, msgid, m->read_receipt_userdata); | 697 | (*m->read_receipt)(m, i, msgid, m->read_receipt_userdata); |
703 | break; | 698 | break; |
704 | } | 699 | } |
@@ -744,7 +739,7 @@ void doMessenger(Messenger *m) | |||
744 | doNetCrypto(); | 739 | doNetCrypto(); |
745 | doInbound(m); | 740 | doInbound(m); |
746 | doFriends(m); | 741 | doFriends(m); |
747 | 742 | ||
748 | timer_poll(); | 743 | timer_poll(); |
749 | } | 744 | } |
750 | 745 | ||