forked from irungentoo/toxcore
-
Notifications
You must be signed in to change notification settings - Fork 291
/
Copy pathMessenger.c
3380 lines (2725 loc) · 99.4 KB
/
Messenger.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* SPDX-License-Identifier: GPL-3.0-or-later
* Copyright © 2016-2018 The TokTok team.
* Copyright © 2013 Tox project.
*/
/**
* An implementation of a simple text chat only messenger on the tox network core.
*/
#include "Messenger.h"
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "logger.h"
#include "mono_time.h"
#include "network.h"
#include "state.h"
#include "util.h"
static_assert(MAX_CONCURRENT_FILE_PIPES <= UINT8_MAX + 1,
"uint8_t cannot represent all file transfer numbers");
/**
* Determines if the friendnumber passed is valid in the Messenger object.
*
* @param friendnumber The index in the friend list.
*/
non_null()
static bool friend_is_valid(const Messenger *m, int32_t friendnumber)
{
return (unsigned int)friendnumber < m->numfriends && m->friendlist[friendnumber].status != 0;
}
/** Set the size of the friend list to numfriends.
*
* return -1 if realloc fails.
*/
non_null()
static int realloc_friendlist(Messenger *m, uint32_t num)
{
if (num == 0) {
free(m->friendlist);
m->friendlist = nullptr;
return 0;
}
Friend *newfriendlist = (Friend *)realloc(m->friendlist, num * sizeof(Friend));
if (newfriendlist == nullptr) {
return -1;
}
m->friendlist = newfriendlist;
return 0;
}
/** return the friend number associated to that public key.
* return -1 if no such friend.
*/
int32_t getfriend_id(const Messenger *m, const uint8_t *real_pk)
{
for (uint32_t i = 0; i < m->numfriends; ++i) {
if (m->friendlist[i].status > 0) {
if (id_equal(real_pk, m->friendlist[i].real_pk)) {
return i;
}
}
}
return -1;
}
/** Copies the public key associated to that friend id into real_pk buffer.
* Make sure that real_pk is of size CRYPTO_PUBLIC_KEY_SIZE.
*
* return 0 if success.
* return -1 if failure.
*/
int get_real_pk(const Messenger *m, int32_t friendnumber, uint8_t *real_pk)
{
if (!friend_is_valid(m, friendnumber)) {
return -1;
}
memcpy(real_pk, m->friendlist[friendnumber].real_pk, CRYPTO_PUBLIC_KEY_SIZE);
return 0;
}
/** return friend connection id on success.
* return -1 if failure.
*/
int getfriendcon_id(const Messenger *m, int32_t friendnumber)
{
if (!friend_is_valid(m, friendnumber)) {
return -1;
}
return m->friendlist[friendnumber].friendcon_id;
}
/**
* return a uint16_t that represents the checksum of address of length len.
*/
non_null()
static uint16_t address_checksum(const uint8_t *address, uint32_t len)
{
uint8_t checksum[2] = {0};
uint16_t check;
for (uint32_t i = 0; i < len; ++i) {
checksum[i % 2] ^= address[i];
}
memcpy(&check, checksum, sizeof(check));
return check;
}
/** Format: `[real_pk (32 bytes)][nospam number (4 bytes)][checksum (2 bytes)]`
*
* return FRIEND_ADDRESS_SIZE byte address to give to others.
*/
void getaddress(const Messenger *m, uint8_t *address)
{
id_copy(address, nc_get_self_public_key(m->net_crypto));
uint32_t nospam = get_nospam(m->fr);
memcpy(address + CRYPTO_PUBLIC_KEY_SIZE, &nospam, sizeof(nospam));
uint16_t checksum = address_checksum(address, FRIEND_ADDRESS_SIZE - sizeof(checksum));
memcpy(address + CRYPTO_PUBLIC_KEY_SIZE + sizeof(nospam), &checksum, sizeof(checksum));
}
non_null()
static int send_online_packet(Messenger *m, int32_t friendnumber)
{
if (!friend_is_valid(m, friendnumber)) {
return 0;
}
uint8_t packet = PACKET_ID_ONLINE;
return write_cryptpacket(m->net_crypto, friend_connection_crypt_connection_id(m->fr_c,
m->friendlist[friendnumber].friendcon_id), &packet, sizeof(packet), false) != -1;
}
non_null()
static int send_offline_packet(Messenger *m, int friendcon_id)
{
uint8_t packet = PACKET_ID_OFFLINE;
return write_cryptpacket(m->net_crypto, friend_connection_crypt_connection_id(m->fr_c, friendcon_id), &packet,
sizeof(packet), false) != -1;
}
non_null(1) nullable(4)
static int m_handle_status(void *object, int i, bool status, void *userdata);
non_null(1, 3) nullable(5)
static int m_handle_packet(void *object, int i, const uint8_t *temp, uint16_t len, void *userdata);
non_null(1, 3) nullable(5)
static int m_handle_lossy_packet(void *object, int friend_num, const uint8_t *packet, uint16_t length,
void *userdata);
non_null()
static int32_t init_new_friend(Messenger *m, const uint8_t *real_pk, uint8_t status)
{
if (m->numfriends == UINT32_MAX) {
LOGGER_ERROR(m->log, "Friend list full: we have more than 4 billion friends");
/* This is technically incorrect, but close enough. */
return FAERR_NOMEM;
}
/* Resize the friend list if necessary. */
if (realloc_friendlist(m, m->numfriends + 1) != 0) {
return FAERR_NOMEM;
}
memset(&m->friendlist[m->numfriends], 0, sizeof(Friend));
const int friendcon_id = new_friend_connection(m->fr_c, real_pk);
if (friendcon_id == -1) {
return FAERR_NOMEM;
}
for (uint32_t i = 0; i <= m->numfriends; ++i) {
if (m->friendlist[i].status == NOFRIEND) {
m->friendlist[i].status = status;
m->friendlist[i].friendcon_id = friendcon_id;
m->friendlist[i].friendrequest_lastsent = 0;
id_copy(m->friendlist[i].real_pk, real_pk);
m->friendlist[i].statusmessage_length = 0;
m->friendlist[i].userstatus = USERSTATUS_NONE;
m->friendlist[i].is_typing = 0;
m->friendlist[i].message_id = 0;
friend_connection_callbacks(m->fr_c, friendcon_id, MESSENGER_CALLBACK_INDEX, &m_handle_status, &m_handle_packet,
&m_handle_lossy_packet, m, i);
if (m->numfriends == i) {
++m->numfriends;
}
if (friend_con_connected(m->fr_c, friendcon_id) == FRIENDCONN_STATUS_CONNECTED) {
send_online_packet(m, i);
}
return i;
}
}
return FAERR_NOMEM;
}
/**
* Add a friend.
*
* Set the data that will be sent along with friend request.
*
* @param address is the address of the friend (returned by getaddress of the friend
* you wish to add) it must be FRIEND_ADDRESS_SIZE bytes.
* TODO(irungentoo): add checksum.
* @param data is the data.
* @param length is the length.
*
* return the friend number if success.
* return FA_TOOLONG if message length is too long.
* return FAERR_NOMESSAGE if no message (message length must be >= 1 byte).
* return FAERR_OWNKEY if user's own key.
* return FAERR_ALREADYSENT if friend request already sent or already a friend.
* return FAERR_BADCHECKSUM if bad checksum in address.
* return FAERR_SETNEWNOSPAM if the friend was already there but the nospam was different.
* (the nospam for that friend was set to the new one).
* return FAERR_NOMEM if increasing the friend list size fails.
*/
int32_t m_addfriend(Messenger *m, const uint8_t *address, const uint8_t *data, uint16_t length)
{
if (length > MAX_FRIEND_REQUEST_DATA_SIZE) {
return FAERR_TOOLONG;
}
uint8_t real_pk[CRYPTO_PUBLIC_KEY_SIZE];
id_copy(real_pk, address);
if (!public_key_valid(real_pk)) {
return FAERR_BADCHECKSUM;
}
uint16_t check;
const uint16_t checksum = address_checksum(address, FRIEND_ADDRESS_SIZE - sizeof(checksum));
memcpy(&check, address + CRYPTO_PUBLIC_KEY_SIZE + sizeof(uint32_t), sizeof(check));
if (check != checksum) {
return FAERR_BADCHECKSUM;
}
if (length < 1) {
return FAERR_NOMESSAGE;
}
if (id_equal(real_pk, nc_get_self_public_key(m->net_crypto))) {
return FAERR_OWNKEY;
}
const int32_t friend_id = getfriend_id(m, real_pk);
if (friend_id != -1) {
if (m->friendlist[friend_id].status >= FRIEND_CONFIRMED) {
return FAERR_ALREADYSENT;
}
uint32_t nospam;
memcpy(&nospam, address + CRYPTO_PUBLIC_KEY_SIZE, sizeof(nospam));
if (m->friendlist[friend_id].friendrequest_nospam == nospam) {
return FAERR_ALREADYSENT;
}
m->friendlist[friend_id].friendrequest_nospam = nospam;
return FAERR_SETNEWNOSPAM;
}
const int32_t ret = init_new_friend(m, real_pk, FRIEND_ADDED);
if (ret < 0) {
return ret;
}
m->friendlist[ret].friendrequest_timeout = FRIENDREQUEST_TIMEOUT;
memcpy(m->friendlist[ret].info, data, length);
m->friendlist[ret].info_size = length;
memcpy(&m->friendlist[ret].friendrequest_nospam, address + CRYPTO_PUBLIC_KEY_SIZE, sizeof(uint32_t));
return ret;
}
int32_t m_addfriend_norequest(Messenger *m, const uint8_t *real_pk)
{
if (getfriend_id(m, real_pk) != -1) {
return FAERR_ALREADYSENT;
}
if (!public_key_valid(real_pk)) {
return FAERR_BADCHECKSUM;
}
if (id_equal(real_pk, nc_get_self_public_key(m->net_crypto))) {
return FAERR_OWNKEY;
}
return init_new_friend(m, real_pk, FRIEND_CONFIRMED);
}
non_null()
static int clear_receipts(Messenger *m, int32_t friendnumber)
{
if (!friend_is_valid(m, friendnumber)) {
return -1;
}
struct Receipts *receipts = m->friendlist[friendnumber].receipts_start;
while (receipts != nullptr) {
struct Receipts *temp_r = receipts->next;
free(receipts);
receipts = temp_r;
}
m->friendlist[friendnumber].receipts_start = nullptr;
m->friendlist[friendnumber].receipts_end = nullptr;
return 0;
}
non_null()
static int add_receipt(Messenger *m, int32_t friendnumber, uint32_t packet_num, uint32_t msg_id)
{
if (!friend_is_valid(m, friendnumber)) {
return -1;
}
struct Receipts *new_receipts = (struct Receipts *)calloc(1, sizeof(struct Receipts));
if (new_receipts == nullptr) {
return -1;
}
new_receipts->packet_num = packet_num;
new_receipts->msg_id = msg_id;
if (m->friendlist[friendnumber].receipts_start == nullptr) {
m->friendlist[friendnumber].receipts_start = new_receipts;
} else {
m->friendlist[friendnumber].receipts_end->next = new_receipts;
}
m->friendlist[friendnumber].receipts_end = new_receipts;
new_receipts->next = nullptr;
return 0;
}
/**
* return -1 on failure.
* return 0 if packet was received.
*/
non_null()
static int friend_received_packet(const Messenger *m, int32_t friendnumber, uint32_t number)
{
if (!friend_is_valid(m, friendnumber)) {
return -1;
}
return cryptpacket_received(m->net_crypto, friend_connection_crypt_connection_id(m->fr_c,
m->friendlist[friendnumber].friendcon_id), number);
}
non_null(1) nullable(3)
static int do_receipts(Messenger *m, int32_t friendnumber, void *userdata)
{
if (!friend_is_valid(m, friendnumber)) {
return -1;
}
struct Receipts *receipts = m->friendlist[friendnumber].receipts_start;
while (receipts != nullptr) {
if (friend_received_packet(m, friendnumber, receipts->packet_num) == -1) {
break;
}
if (m->read_receipt != nullptr) {
m->read_receipt(m, friendnumber, receipts->msg_id, userdata);
}
struct Receipts *r_next = receipts->next;
free(receipts);
m->friendlist[friendnumber].receipts_start = r_next;
receipts = r_next;
}
if (m->friendlist[friendnumber].receipts_start == nullptr) {
m->friendlist[friendnumber].receipts_end = nullptr;
}
return 0;
}
/** Remove a friend.
*
* return 0 if success.
* return -1 if failure.
*/
int m_delfriend(Messenger *m, int32_t friendnumber)
{
if (!friend_is_valid(m, friendnumber)) {
return -1;
}
if (m->friend_connectionstatuschange_internal != nullptr) {
m->friend_connectionstatuschange_internal(m, friendnumber, 0, m->friend_connectionstatuschange_internal_userdata);
}
clear_receipts(m, friendnumber);
remove_request_received(m->fr, m->friendlist[friendnumber].real_pk);
friend_connection_callbacks(m->fr_c, m->friendlist[friendnumber].friendcon_id, MESSENGER_CALLBACK_INDEX, nullptr,
nullptr, nullptr, nullptr, 0);
if (friend_con_connected(m->fr_c, m->friendlist[friendnumber].friendcon_id) == FRIENDCONN_STATUS_CONNECTED) {
send_offline_packet(m, m->friendlist[friendnumber].friendcon_id);
}
kill_friend_connection(m->fr_c, m->friendlist[friendnumber].friendcon_id);
memset(&m->friendlist[friendnumber], 0, sizeof(Friend));
uint32_t i;
for (i = m->numfriends; i != 0; --i) {
if (m->friendlist[i - 1].status != NOFRIEND) {
break;
}
}
m->numfriends = i;
if (realloc_friendlist(m, m->numfriends) != 0) {
return FAERR_NOMEM;
}
return 0;
}
int m_get_friend_connectionstatus(const Messenger *m, int32_t friendnumber)
{
if (!friend_is_valid(m, friendnumber)) {
return -1;
}
if (m->friendlist[friendnumber].status != FRIEND_ONLINE) {
return CONNECTION_NONE;
}
bool direct_connected = 0;
uint32_t num_online_relays = 0;
const int crypt_conn_id = friend_connection_crypt_connection_id(m->fr_c, m->friendlist[friendnumber].friendcon_id);
if (!crypto_connection_status(m->net_crypto, crypt_conn_id, &direct_connected, &num_online_relays)) {
return CONNECTION_NONE;
}
if (direct_connected) {
return CONNECTION_UDP;
}
if (num_online_relays != 0) {
return CONNECTION_TCP;
}
/* if we have a valid friend connection but do not have an established connection
* we leave the connection status unchanged until the friend connection is either
* established or dropped.
*/
return m->friendlist[friendnumber].last_connection_udp_tcp;
}
int m_friend_exists(const Messenger *m, int32_t friendnumber)
{
if (!friend_is_valid(m, friendnumber)) {
return 0;
}
return 1;
}
/** Send a message of type to an online friend.
*
* return -1 if friend not valid.
* return -2 if too large.
* return -3 if friend not online.
* return -4 if send failed (because queue is full).
* return -5 if bad type.
* return 0 if success.
*
* the value in message_id will be passed to your read_receipt callback when the other receives the message.
*/
int m_send_message_generic(Messenger *m, int32_t friendnumber, uint8_t type, const uint8_t *message, uint32_t length,
uint32_t *message_id)
{
if (type > MESSAGE_ACTION) {
LOGGER_WARNING(m->log, "message type %d is invalid", type);
return -5;
}
if (!friend_is_valid(m, friendnumber)) {
LOGGER_WARNING(m->log, "friend number %d is invalid", friendnumber);
return -1;
}
if (length >= MAX_CRYPTO_DATA_SIZE) {
LOGGER_WARNING(m->log, "message length %u is too large", length);
return -2;
}
if (m->friendlist[friendnumber].status != FRIEND_ONLINE) {
LOGGER_WARNING(m->log, "friend %d is not online", friendnumber);
return -3;
}
VLA(uint8_t, packet, length + 1);
packet[0] = PACKET_ID_MESSAGE + type;
if (length > 0) {
memcpy(packet + 1, message, length);
}
const int64_t packet_num = write_cryptpacket(m->net_crypto, friend_connection_crypt_connection_id(m->fr_c,
m->friendlist[friendnumber].friendcon_id), packet, length + 1, false);
if (packet_num == -1) {
LOGGER_WARNING(m->log, "failed to write crypto packet for message of length %d to friend %d",
length, friendnumber);
return -4;
}
const uint32_t msg_id = ++m->friendlist[friendnumber].message_id;
add_receipt(m, friendnumber, packet_num, msg_id);
if (message_id != nullptr) {
*message_id = msg_id;
}
return 0;
}
non_null()
static int write_cryptpacket_id(const Messenger *m, int32_t friendnumber, uint8_t packet_id, const uint8_t *data,
uint32_t length, bool congestion_control)
{
if (!friend_is_valid(m, friendnumber)) {
return 0;
}
if (length >= MAX_CRYPTO_DATA_SIZE || m->friendlist[friendnumber].status != FRIEND_ONLINE) {
return 0;
}
VLA(uint8_t, packet, length + 1);
packet[0] = packet_id;
if (length > 0) {
memcpy(packet + 1, data, length);
}
return write_cryptpacket(m->net_crypto, friend_connection_crypt_connection_id(m->fr_c,
m->friendlist[friendnumber].friendcon_id), packet, length + 1, congestion_control) != -1;
}
/** Send a name packet to friendnumber.
* length is the length with the NULL terminator.
*/
non_null()
static int m_sendname(const Messenger *m, int32_t friendnumber, const uint8_t *name, uint16_t length)
{
if (length > MAX_NAME_LENGTH) {
return 0;
}
return write_cryptpacket_id(m, friendnumber, PACKET_ID_NICKNAME, name, length, false);
}
/** Set the name and name_length of a friend.
* name must be a string of maximum MAX_NAME_LENGTH length.
* length must be at least 1 byte.
* length is the length of name with the NULL terminator.
*
* return 0 if success.
* return -1 if failure.
*/
int setfriendname(Messenger *m, int32_t friendnumber, const uint8_t *name, uint16_t length)
{
if (!friend_is_valid(m, friendnumber)) {
return -1;
}
if (length > MAX_NAME_LENGTH || length == 0) {
return -1;
}
m->friendlist[friendnumber].name_length = length;
memcpy(m->friendlist[friendnumber].name, name, length);
return 0;
}
/** Set our nickname.
* name must be a string of maximum MAX_NAME_LENGTH length.
* length must be at least 1 byte.
* length is the length of name with the NULL terminator.
*
* return 0 if success.
* return -1 if failure.
*/
int setname(Messenger *m, const uint8_t *name, uint16_t length)
{
if (length > MAX_NAME_LENGTH) {
return -1;
}
if (m->name_length == length && (length == 0 || memcmp(name, m->name, length) == 0)) {
return 0;
}
if (length > 0) {
memcpy(m->name, name, length);
}
m->name_length = length;
for (uint32_t i = 0; i < m->numfriends; ++i) {
m->friendlist[i].name_sent = 0;
}
return 0;
}
/**
* Get your nickname.
* m - The messenger context to use.
* name needs to be a valid memory location with a size of at least MAX_NAME_LENGTH bytes.
*
* return length of the name.
* return 0 on error.
*/
uint16_t getself_name(const Messenger *m, uint8_t *name)
{
if (name == nullptr) {
return 0;
}
memcpy(name, m->name, m->name_length);
return m->name_length;
}
/** Get name of friendnumber and put it in name.
* name needs to be a valid memory location with a size of at least MAX_NAME_LENGTH (128) bytes.
*
* return length of name if success.
* return -1 if failure.
*/
int getname(const Messenger *m, int32_t friendnumber, uint8_t *name)
{
if (!friend_is_valid(m, friendnumber)) {
return -1;
}
memcpy(name, m->friendlist[friendnumber].name, m->friendlist[friendnumber].name_length);
return m->friendlist[friendnumber].name_length;
}
int m_get_name_size(const Messenger *m, int32_t friendnumber)
{
if (!friend_is_valid(m, friendnumber)) {
return -1;
}
return m->friendlist[friendnumber].name_length;
}
int m_get_self_name_size(const Messenger *m)
{
return m->name_length;
}
int m_set_statusmessage(Messenger *m, const uint8_t *status, uint16_t length)
{
if (length > MAX_STATUSMESSAGE_LENGTH) {
return -1;
}
if (m->statusmessage_length == length && (length == 0 || memcmp(m->statusmessage, status, length) == 0)) {
return 0;
}
if (length > 0) {
memcpy(m->statusmessage, status, length);
}
m->statusmessage_length = length;
for (uint32_t i = 0; i < m->numfriends; ++i) {
m->friendlist[i].statusmessage_sent = 0;
}
return 0;
}
int m_set_userstatus(Messenger *m, uint8_t status)
{
if (status >= USERSTATUS_INVALID) {
return -1;
}
if (m->userstatus == status) {
return 0;
}
m->userstatus = (Userstatus)status;
for (uint32_t i = 0; i < m->numfriends; ++i) {
m->friendlist[i].userstatus_sent = 0;
}
return 0;
}
/**
* Guaranteed to be at most MAX_STATUSMESSAGE_LENGTH.
*
* returns the length of friendnumber's status message, including null on success.
* returns -1 on failure.
*/
int m_get_statusmessage_size(const Messenger *m, int32_t friendnumber)
{
if (!friend_is_valid(m, friendnumber)) {
return -1;
}
return m->friendlist[friendnumber].statusmessage_length;
}
/** Copy friendnumber's status message into buf, truncating if size is over maxlen.
* Get the size you need to allocate from m_get_statusmessage_size.
* The self variant will copy our own status message.
*
* returns the length of the copied data on success
* returns -1 on failure.
*/
int m_copy_statusmessage(const Messenger *m, int32_t friendnumber, uint8_t *buf, uint32_t maxlen)
{
if (!friend_is_valid(m, friendnumber)) {
return -1;
}
// TODO(iphydf): This should be uint16_t and min_u16. If maxlen exceeds
// uint16_t's range, it won't affect the result.
const uint32_t msglen = min_u32(maxlen, m->friendlist[friendnumber].statusmessage_length);
memcpy(buf, m->friendlist[friendnumber].statusmessage, msglen);
memset(buf + msglen, 0, maxlen - msglen);
return msglen;
}
/** return the size of friendnumber's user status.
* Guaranteed to be at most MAX_STATUSMESSAGE_LENGTH.
*/
int m_get_self_statusmessage_size(const Messenger *m)
{
return m->statusmessage_length;
}
int m_copy_self_statusmessage(const Messenger *m, uint8_t *buf)
{
memcpy(buf, m->statusmessage, m->statusmessage_length);
return m->statusmessage_length;
}
uint8_t m_get_userstatus(const Messenger *m, int32_t friendnumber)
{
if (!friend_is_valid(m, friendnumber)) {
return USERSTATUS_INVALID;
}
uint8_t status = m->friendlist[friendnumber].userstatus;
if (status >= USERSTATUS_INVALID) {
status = USERSTATUS_NONE;
}
return status;
}
uint8_t m_get_self_userstatus(const Messenger *m)
{
return m->userstatus;
}
uint64_t m_get_last_online(const Messenger *m, int32_t friendnumber)
{
if (!friend_is_valid(m, friendnumber)) {
return UINT64_MAX;
}
return m->friendlist[friendnumber].last_seen_time;
}
int m_set_usertyping(Messenger *m, int32_t friendnumber, uint8_t is_typing)
{
if (is_typing != 0 && is_typing != 1) {
return -1;
}
if (!friend_is_valid(m, friendnumber)) {
return -1;
}
if (m->friendlist[friendnumber].user_istyping == is_typing) {
return 0;
}
m->friendlist[friendnumber].user_istyping = is_typing;
m->friendlist[friendnumber].user_istyping_sent = 0;
return 0;
}
int m_get_istyping(const Messenger *m, int32_t friendnumber)
{
if (!friend_is_valid(m, friendnumber)) {
return -1;
}
return m->friendlist[friendnumber].is_typing;
}
non_null()
static int send_statusmessage(const Messenger *m, int32_t friendnumber, const uint8_t *status, uint16_t length)
{
return write_cryptpacket_id(m, friendnumber, PACKET_ID_STATUSMESSAGE, status, length, false);
}
non_null()
static int send_userstatus(const Messenger *m, int32_t friendnumber, uint8_t status)
{
return write_cryptpacket_id(m, friendnumber, PACKET_ID_USERSTATUS, &status, sizeof(status), false);
}
non_null()
static int send_user_istyping(const Messenger *m, int32_t friendnumber, uint8_t is_typing)
{
uint8_t typing = is_typing;
return write_cryptpacket_id(m, friendnumber, PACKET_ID_TYPING, &typing, sizeof(typing), false);
}
non_null()
static int set_friend_statusmessage(const Messenger *m, int32_t friendnumber, const uint8_t *status, uint16_t length)
{
if (!friend_is_valid(m, friendnumber)) {
return -1;
}
if (length > MAX_STATUSMESSAGE_LENGTH) {
return -1;
}
if (length > 0) {
memcpy(m->friendlist[friendnumber].statusmessage, status, length);
}
m->friendlist[friendnumber].statusmessage_length = length;
return 0;
}
non_null()
static void set_friend_userstatus(const Messenger *m, int32_t friendnumber, uint8_t status)
{
m->friendlist[friendnumber].userstatus = (Userstatus)status;
}
non_null()
static void set_friend_typing(const Messenger *m, int32_t friendnumber, uint8_t is_typing)
{
m->friendlist[friendnumber].is_typing = is_typing;
}
/** Set the function that will be executed when a friend request is received. */
void m_callback_friendrequest(Messenger *m, m_friend_request_cb *function)
{
m->friend_request = function;
}
/** Set the function that will be executed when a message from a friend is received. */
void m_callback_friendmessage(Messenger *m, m_friend_message_cb *function)
{
m->friend_message = function;
}
void m_callback_namechange(Messenger *m, m_friend_name_cb *function)
{
m->friend_namechange = function;
}
void m_callback_statusmessage(Messenger *m, m_friend_status_message_cb *function)
{
m->friend_statusmessagechange = function;
}
void m_callback_userstatus(Messenger *m, m_friend_status_cb *function)
{
m->friend_userstatuschange = function;
}
void m_callback_typingchange(Messenger *m, m_friend_typing_cb *function)
{
m->friend_typingchange = function;
}
void m_callback_read_receipt(Messenger *m, m_friend_read_receipt_cb *function)
{
m->read_receipt = function;
}
void m_callback_connectionstatus(Messenger *m, m_friend_connection_status_cb *function)
{
m->friend_connectionstatuschange = function;
}
void m_callback_core_connection(Messenger *m, m_self_connection_status_cb *function)
{
m->core_connection_change = function;
}
void m_callback_connectionstatus_internal_av(Messenger *m, m_friend_connectionstatuschange_internal_cb *function,
void *userdata)
{
m->friend_connectionstatuschange_internal = function;
m->friend_connectionstatuschange_internal_userdata = userdata;
}
non_null(1) nullable(3)
static void check_friend_tcp_udp(Messenger *m, int32_t friendnumber, void *userdata)
{
const int last_connection_udp_tcp = m->friendlist[friendnumber].last_connection_udp_tcp;
const int ret = m_get_friend_connectionstatus(m, friendnumber);
if (ret == -1) {
return;
}
if (last_connection_udp_tcp != ret) {
if (m->friend_connectionstatuschange != nullptr) {
m->friend_connectionstatuschange(m, friendnumber, ret, userdata);
}
}
m->friendlist[friendnumber].last_connection_udp_tcp = (Connection_Status)ret;
}
non_null()
static void break_files(const Messenger *m, int32_t friendnumber);
non_null(1) nullable(4)
static void check_friend_connectionstatus(Messenger *m, int32_t friendnumber, uint8_t status, void *userdata)
{
if (status == NOFRIEND) {
return;
}
const bool was_online = m->friendlist[friendnumber].status == FRIEND_ONLINE;
const bool is_online = status == FRIEND_ONLINE;
if (is_online != was_online) {
if (was_online) {
break_files(m, friendnumber);
clear_receipts(m, friendnumber);
} else {
m->friendlist[friendnumber].name_sent = 0;
m->friendlist[friendnumber].userstatus_sent = 0;
m->friendlist[friendnumber].statusmessage_sent = 0;
m->friendlist[friendnumber].user_istyping_sent = 0;
}
m->friendlist[friendnumber].status = status;
check_friend_tcp_udp(m, friendnumber, userdata);
if (m->friend_connectionstatuschange_internal != nullptr) {
m->friend_connectionstatuschange_internal(m, friendnumber, is_online,
m->friend_connectionstatuschange_internal_userdata);
}
}
}