-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbindings.go
1111 lines (878 loc) · 29.1 KB
/
bindings.go
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
package gotox
//#include <tox/tox.h>
//#include <stdlib.h>
import "C"
import "time"
import "unsafe"
/* VersionMajor returns the major version number of the used Tox library */
func VersionMajor() uint32 {
return uint32(C.tox_version_major())
}
/* VersionMinor returns the minor version number of the used Tox library */
func VersionMinor() uint32 {
return uint32(C.tox_version_minor())
}
/* VersionPatch returns the patch number of the used Tox library */
func VersionPatch() uint32 {
return uint32(C.tox_version_patch())
}
/* VersionIsCompatible returns whether the compiled Tox library version is
* compatible with the passed version numbers. */
func VersionIsCompatible(major uint32, minor uint32, patch uint32) bool {
return bool(C.tox_version_is_compatible((C.uint32_t)(major), (C.uint32_t)(minor), (C.uint32_t)(patch)))
}
/* New creates and initialises a new Tox instance and returns the corresponding
* gotox instance. */
func New(options *Options) (*Tox, error) {
var cTox *C.Tox
var toxErrNew C.TOX_ERR_NEW
var toxErrOptionsNew C.TOX_ERR_OPTIONS_NEW
var cOptions *C.struct_Tox_Options = C.tox_options_new(&toxErrOptionsNew)
if cOptions == nil || ToxErrOptionsNew(toxErrOptionsNew) != TOX_ERR_OPTIONS_NEW_OK {
return nil, ErrFuncFail
}
if options == nil {
cOptions = nil
} else {
// map options from Options to C.Tox_Options
cOptions.ipv6_enabled = C.bool(options.IPv6Enabled)
cOptions.udp_enabled = C.bool(options.UDPEnabled)
var cProxyType C.TOX_PROXY_TYPE = C.TOX_PROXY_TYPE_NONE
if options.ProxyType == TOX_PROXY_TYPE_HTTP {
cProxyType = C.TOX_PROXY_TYPE_HTTP
} else if options.ProxyType == TOX_PROXY_TYPE_SOCKS5 {
cProxyType = C.TOX_PROXY_TYPE_SOCKS5
}
cOptions.proxy_type = cProxyType
// max ProxyHost length is 255
if len(options.ProxyHost) > 255 {
return nil, ErrArgs
}
cProxyHost := C.CString(options.ProxyHost)
cOptions.proxy_host = cProxyHost
defer C.free(unsafe.Pointer(cProxyHost))
cOptions.proxy_port = C.uint16_t(options.ProxyPort)
cOptions.start_port = C.uint16_t(options.StartPort)
cOptions.end_port = C.uint16_t(options.EndPort)
cOptions.tcp_port = C.uint16_t(options.TcpPort)
if options.SaveDataType == TOX_SAVEDATA_TYPE_TOX_SAVE {
cOptions.savedata_type = C.TOX_SAVEDATA_TYPE_TOX_SAVE
} else if options.SaveDataType == TOX_SAVEDATA_TYPE_SECRET_KEY {
cOptions.savedata_type = C.TOX_SAVEDATA_TYPE_SECRET_KEY
}
if len(options.SaveData) > 0 {
cOptions.savedata_data = (*C.uint8_t)(&options.SaveData[0])
} else {
cOptions.savedata_data = nil
}
cOptions.savedata_length = C.size_t(len(options.SaveData))
}
cTox = C.tox_new(cOptions, &toxErrNew)
if cTox == nil || ToxErrNew(toxErrNew) != TOX_ERR_NEW_OK {
C.tox_options_free(cOptions)
switch ToxErrNew(toxErrNew) {
case TOX_ERR_NEW_NULL:
return nil, ErrArgs
case TOX_ERR_NEW_MALLOC:
return nil, ErrNewMalloc
case TOX_ERR_NEW_PORT_ALLOC:
return nil, ErrNewPortAlloc
case TOX_ERR_NEW_PROXY_BAD_TYPE:
return nil, ErrNewProxy
case TOX_ERR_NEW_PROXY_BAD_HOST:
return nil, ErrNewProxy
case TOX_ERR_NEW_PROXY_BAD_PORT:
return nil, ErrNewProxy
case TOX_ERR_NEW_PROXY_NOT_FOUND:
return nil, ErrNewProxy
case TOX_ERR_NEW_LOAD_ENCRYPTED:
return nil, ErrNewLoadEnc
case TOX_ERR_NEW_LOAD_BAD_FORMAT:
return nil, ErrNewLoadBadFormat
}
if cTox == nil {
return nil, ErrToxNew
}
return nil, ErrUnknown
}
t := &Tox{tox: cTox, cOptions: cOptions}
return t, nil
}
/* Kill releases all resources associated with the Tox instance and disconnects
* from the network.
* After calling this function `t *TOX` becomes invalid. Do not use it again! */
func (t *Tox) Kill() error {
if t.tox == nil {
return ErrToxInit
}
C.tox_options_free(t.cOptions)
C.tox_kill(t.tox)
return nil
}
/* GetSaveDataSize returns the size of the savedata returned by GetSavedata. */
func (t *Tox) GetSaveDataSize() (uint32, error) {
if t.tox == nil {
return 0, ErrToxInit
}
return uint32(C.tox_get_savedata_size(t.tox)), nil
}
/* GetSavedata returns a byte slice of all information associated with the tox
* instance. */
func (t *Tox) GetSavedata() ([]byte, error) {
if t.tox == nil {
return nil, ErrToxInit
}
size, err := t.GetSaveDataSize()
if err != nil || size == 0 {
return nil, ErrFuncFail
}
data := make([]byte, size)
if size > 0 {
C.tox_get_savedata(t.tox, (*C.uint8_t)(&data[0]))
}
return data, nil
}
/* Bootstrap sends a "get nodes" request to the given bootstrap node with IP,
* port, and public key to setup connections. */
func (t *Tox) Bootstrap(address string, port uint16, publickey []byte) error {
if t.tox == nil {
return ErrToxInit
}
if len(publickey) != TOX_PUBLIC_KEY_SIZE {
return ErrArgs
}
caddr := C.CString(address)
defer C.free(unsafe.Pointer(caddr))
var toxErrBootstrap C.TOX_ERR_BOOTSTRAP
success := C.tox_bootstrap(t.tox, caddr, (C.uint16_t)(port), (*C.uint8_t)(&publickey[0]), &toxErrBootstrap)
switch ToxErrBootstrap(toxErrBootstrap) {
case TOX_ERR_BOOTSTRAP_OK:
return nil
case TOX_ERR_BOOTSTRAP_NULL:
return ErrArgs
case TOX_ERR_BOOTSTRAP_BAD_HOST:
return ErrFuncFail
case TOX_ERR_BOOTSTRAP_BAD_PORT:
return ErrFuncFail
}
if !bool(success) {
return ErrFuncFail
}
return ErrUnknown
}
/* AddTCPRelay adds the given node with IP, port, and public key without using
* it as a boostrap node. */
func (t *Tox) AddTCPRelay(address string, port uint16, publickey []byte) error {
if t.tox == nil {
return ErrToxInit
}
if len(publickey) != TOX_PUBLIC_KEY_SIZE {
return ErrArgs
}
caddr := C.CString(address)
defer C.free(unsafe.Pointer(caddr))
var toxErrBootstrap C.TOX_ERR_BOOTSTRAP
success := C.tox_add_tcp_relay(t.tox, caddr, (C.uint16_t)(port), (*C.uint8_t)(&publickey[0]), &toxErrBootstrap)
switch ToxErrBootstrap(toxErrBootstrap) {
case TOX_ERR_BOOTSTRAP_OK:
return nil
case TOX_ERR_BOOTSTRAP_NULL:
return ErrArgs
case TOX_ERR_BOOTSTRAP_BAD_HOST:
return ErrFuncFail
case TOX_ERR_BOOTSTRAP_BAD_PORT:
return ErrFuncFail
}
if !bool(success) {
return ErrFuncFail
}
return ErrUnknown
}
/* SelfGetConnectionStatus returns true if Tox is connected to the DHT. */
func (t *Tox) SelfGetConnectionStatus() (ToxConnection, error) {
if t.tox == nil {
return TOX_CONNECTION_NONE, ErrToxInit
}
return ToxConnection(C.tox_self_get_connection_status(t.tox)), nil
}
/* IterationInterval returns the time in milliseconds before Iterate() should be
* called again. */
func (t *Tox) IterationInterval() (uint32, error) {
if t.tox == nil {
return 0, ErrToxInit
}
ret := C.tox_iteration_interval(t.tox)
return uint32(ret), nil
}
/* Iterate is the main loop. It needs to be called every IterationInterval()
* milliseconds. */
func (t *Tox) Iterate() error {
if t.tox == nil {
return ErrToxInit
}
t.mtx.Lock()
C.tox_iterate(t.tox, unsafe.Pointer(t))
t.mtx.Unlock()
return nil
}
/* SelfGetAddress returns the public address to give to others. */
func (t *Tox) SelfGetAddress() ([]byte, error) {
if t.tox == nil {
return nil, ErrToxInit
}
address := make([]byte, TOX_ADDRESS_SIZE)
C.tox_self_get_address(t.tox, (*C.uint8_t)(&address[0]))
return address, nil
}
/* SelfSetNospam sets the nospam of your ID. */
func (t *Tox) SelfSetNospam(nospam uint32) error {
if t.tox == nil {
return ErrToxInit
}
C.tox_self_set_nospam(t.tox, (C.uint32_t)(nospam))
return nil
}
/* SelfGetNospam returns the nospam of your ID. */
func (t *Tox) SelfGetNospam() (uint32, error) {
if t.tox == nil {
return 0, ErrToxInit
}
n := C.tox_self_get_nospam(t.tox)
return uint32(n), nil
}
/* SelfGetPublicKey returns the publickey of your profile. */
func (t *Tox) SelfGetPublicKey() ([]byte, error) {
if t.tox == nil {
return nil, ErrToxInit
}
publickey := make([]byte, TOX_PUBLIC_KEY_SIZE)
C.tox_self_get_public_key(t.tox, (*C.uint8_t)(&publickey[0]))
return publickey, nil
}
/* SelfGetSecretKey returns the secretkey of your profile. */
func (t *Tox) SelfGetSecretKey() ([]byte, error) {
if t.tox == nil {
return nil, ErrToxInit
}
secretkey := make([]byte, TOX_SECRET_KEY_SIZE)
C.tox_self_get_secret_key(t.tox, (*C.uint8_t)(&secretkey[0]))
return secretkey, nil
}
/* SelfSetName sets your nickname. The maximum name length is MAX_NAME_LENGTH. */
func (t *Tox) SelfSetName(name string) error {
if t.tox == nil {
return ErrToxInit
}
var cName (*C.uint8_t)
if len(name) == 0 {
cName = nil
} else {
cName = (*C.uint8_t)(&[]byte(name)[0])
}
var setInfoError C.TOX_ERR_SET_INFO = C.TOX_ERR_SET_INFO_OK
success := C.tox_self_set_name(t.tox, cName, (C.size_t)(len(name)), &setInfoError)
if !bool(success) || ToxErrSetInfo(setInfoError) != TOX_ERR_SET_INFO_OK {
return ErrFuncFail
}
return nil
}
/* SelfGetNameSize returns the length of your name. */
func (t *Tox) SelfGetNameSize() (int64, error) {
if t.tox == nil {
return 0, ErrToxInit
}
ret := C.tox_self_get_name_size(t.tox)
return int64(ret), nil
}
/* SelfGetName returns your nickname. */
func (t *Tox) SelfGetName() (string, error) {
if t.tox == nil {
return "", ErrToxInit
}
length, err := t.SelfGetNameSize()
if err != nil {
return "", ErrFuncFail
}
name := make([]byte, length)
if length > 0 {
C.tox_self_get_name(t.tox, (*C.uint8_t)(&name[0]))
}
return string(name), nil
}
/* SelfSetStatusMessage sets your status message.
* The maximum status length is MAX_STATUS_MESSAGE_LENGTH. */
func (t *Tox) SelfSetStatusMessage(status string) error {
if t.tox == nil {
return ErrToxInit
}
var cStatus (*C.uint8_t)
if len(status) == 0 {
cStatus = nil
} else {
cStatus = (*C.uint8_t)(&[]byte(status)[0])
}
var setInfoError C.TOX_ERR_SET_INFO = C.TOX_ERR_SET_INFO_OK
C.tox_self_set_status_message(t.tox, cStatus, (C.size_t)(len(status)), &setInfoError)
if ToxErrSetInfo(setInfoError) != TOX_ERR_SET_INFO_OK {
return ErrFuncFail
}
return nil
}
/* SelfGetStatusMessageSize returns the size of your status message. */
func (t *Tox) SelfGetStatusMessageSize() (int64, error) {
if t.tox == nil {
return 0, ErrToxInit
}
ret := C.tox_self_get_status_message_size(t.tox)
return int64(ret), nil
}
/* SelfGetStatusMessage returns your status message. */
func (t *Tox) SelfGetStatusMessage() (string, error) {
if t.tox == nil {
return "", ErrToxInit
}
length, err := t.SelfGetStatusMessageSize()
if err != nil {
return "", ErrFuncFail
}
statusMessage := make([]byte, length)
if length > 0 {
C.tox_self_get_status_message(t.tox, (*C.uint8_t)(&statusMessage[0]))
}
return string(statusMessage), nil
}
/* SelfSetStatus sets your userstatus. */
func (t *Tox) SelfSetStatus(userstatus ToxUserStatus) error {
if t.tox == nil {
return ErrToxInit
}
C.tox_self_set_status(t.tox, (C.TOX_USER_STATUS)(userstatus))
return nil
}
/* SelfGetStatus returns your status. */
func (t *Tox) SelfGetStatus() (ToxUserStatus, error) {
if t.tox == nil {
return TOX_USERSTATUS_NONE, ErrToxInit
}
n := C.tox_self_get_status(t.tox)
return ToxUserStatus(n), nil
}
/* FriendAdd adds a friend by sending a friend request containing the given
* message.
* Returns the friend number on success, or a ToxErrFriendAdd on failure.
*/
func (t *Tox) FriendAdd(address []byte, message string) (uint32, error) {
if t.tox == nil {
return 0, ErrToxInit
}
if len(address) != TOX_ADDRESS_SIZE || len(message) == 0 {
return 0, ErrArgs
}
caddr := (*C.uint8_t)(&address[0])
cmessage := (*C.uint8_t)(&[]byte(message)[0])
var toxErrFriendAdd C.TOX_ERR_FRIEND_ADD
ret := C.tox_friend_add(t.tox, caddr, cmessage, (C.size_t)(len(message)), &toxErrFriendAdd)
switch ToxErrFriendAdd(toxErrFriendAdd) {
case TOX_ERR_FRIEND_ADD_OK:
return uint32(ret), nil
case TOX_ERR_FRIEND_ADD_NULL:
return uint32(ret), ErrArgs
case TOX_ERR_FRIEND_ADD_TOO_LONG:
return uint32(ret), ErrFriendAddTooLong
case TOX_ERR_FRIEND_ADD_NO_MESSAGE:
return uint32(ret), ErrFriendAddNoMessage
case TOX_ERR_FRIEND_ADD_OWN_KEY:
return uint32(ret), ErrFriendAddOwnKey
case TOX_ERR_FRIEND_ADD_ALREADY_SENT:
return uint32(ret), ErrFriendAddAlreadySent
case TOX_ERR_FRIEND_ADD_BAD_CHECKSUM:
return uint32(ret), ErrFriendAddBadChecksum
case TOX_ERR_FRIEND_ADD_SET_NEW_NOSPAM:
return uint32(ret), ErrFriendAddSetNewNospam
case TOX_ERR_FRIEND_ADD_MALLOC:
return uint32(ret), ErrFriendAddNoMem
default:
return uint32(ret), ErrFuncFail
}
return uint32(ret), ErrUnknown
}
/* FriendAddNorequest adds a friend without sending a friend request.
* Returns the friend number on success.
*/
func (t *Tox) FriendAddNorequest(publickey []byte) (uint32, error) {
if t.tox == nil {
return C.UINT32_MAX, ErrToxInit
}
if len(publickey) != TOX_PUBLIC_KEY_SIZE {
return C.UINT32_MAX, ErrArgs
}
var toxErrFriendAdd C.TOX_ERR_FRIEND_ADD
ret := C.tox_friend_add_norequest(t.tox, (*C.uint8_t)(&publickey[0]), &toxErrFriendAdd)
if ret == C.UINT32_MAX {
return C.UINT32_MAX, ErrFuncFail
}
switch ToxErrFriendAdd(toxErrFriendAdd) {
case TOX_ERR_FRIEND_ADD_OK:
return uint32(ret), nil
case TOX_ERR_FRIEND_ADD_NULL:
return uint32(ret), ErrArgs
case TOX_ERR_FRIEND_ADD_TOO_LONG:
return uint32(ret), ErrFriendAddTooLong
case TOX_ERR_FRIEND_ADD_NO_MESSAGE:
return uint32(ret), ErrFriendAddNoMessage
case TOX_ERR_FRIEND_ADD_OWN_KEY:
return uint32(ret), ErrFriendAddOwnKey
case TOX_ERR_FRIEND_ADD_ALREADY_SENT:
return uint32(ret), ErrFriendAddAlreadySent
case TOX_ERR_FRIEND_ADD_BAD_CHECKSUM:
return uint32(ret), ErrFriendAddBadChecksum
case TOX_ERR_FRIEND_ADD_SET_NEW_NOSPAM:
return uint32(ret), ErrFriendAddSetNewNospam
case TOX_ERR_FRIEND_ADD_MALLOC:
return uint32(ret), ErrFriendAddNoMem
default:
return uint32(ret), ErrFuncFail
}
return uint32(ret), ErrUnknown
}
/* FriendDelete removes a friend. */
func (t *Tox) FriendDelete(friendNumber uint32) error {
if t.tox == nil {
return ErrToxInit
}
var toxErrFriendDelete C.TOX_ERR_FRIEND_DELETE = C.TOX_ERR_FRIEND_DELETE_OK
C.tox_friend_delete(t.tox, (C.uint32_t)(friendNumber), &toxErrFriendDelete)
switch ToxErrFriendDelete(toxErrFriendDelete) {
case TOX_ERR_FRIEND_DELETE_OK:
return nil
case TOX_ERR_FRIEND_DELETE_FRIEND_NOT_FOUND:
return ErrArgs
default:
return ErrFuncFail
}
return ErrUnknown
}
/* FriendByPublicKey returns the friend number associated to a given publickey. */
func (t *Tox) FriendByPublicKey(publickey []byte) (uint32, error) {
if t.tox == nil {
return C.UINT32_MAX, ErrToxInit
}
if len(publickey) != TOX_PUBLIC_KEY_SIZE {
return C.UINT32_MAX, ErrArgs
}
var toxErrFriendByPublicKey C.TOX_ERR_FRIEND_BY_PUBLIC_KEY
n := C.tox_friend_by_public_key(t.tox, (*C.uint8_t)(&publickey[0]), &toxErrFriendByPublicKey)
switch ToxErrFriendByPublicKey(toxErrFriendByPublicKey) {
case TOX_ERR_FRIEND_BY_PUBLIC_KEY_OK:
return uint32(n), nil
case TOX_ERR_FRIEND_BY_PUBLIC_KEY_NULL:
return uint32(n), ErrArgs
default:
return uint32(n), ErrFuncFail
}
return uint32(n), ErrUnknown
}
/* FriendExists returns true if a friend exists with given friendNumber. */
func (t *Tox) FriendExists(friendNumber uint32) (bool, error) {
if t.tox == nil {
return false, ErrToxInit
}
success := C.tox_friend_exists(t.tox, (C.uint32_t)(friendNumber))
return bool(success), nil
}
/* SelfGetFriendlistSize returns the number of friends on the friendlist. */
func (t *Tox) SelfGetFriendlistSize() (int64, error) {
if t.tox == nil {
return 0, ErrToxInit
}
n := C.tox_self_get_friend_list_size(t.tox)
return int64(n), nil
}
/* SelfGetFriendlist returns a slice of uint32 containing the friendNumbers. */
func (t *Tox) SelfGetFriendlist() ([]uint32, error) {
if t.tox == nil {
return nil, ErrToxInit
}
size, err := t.SelfGetFriendlistSize()
if err != nil {
return nil, ErrFuncFail
}
friendlist := make([]uint32, size)
if size > 0 {
C.tox_self_get_friend_list(t.tox, (*C.uint32_t)(&friendlist[0]))
}
return friendlist, nil
}
/* FriendGetPublickey returns the publickey associated to that friendNumber. */
func (t *Tox) FriendGetPublickey(friendNumber uint32) ([]byte, error) {
if t.tox == nil {
return nil, ErrToxInit
}
publickey := make([]byte, TOX_PUBLIC_KEY_SIZE)
var toxErrFriendGetPublicKey C.TOX_ERR_FRIEND_GET_PUBLIC_KEY = C.TOX_ERR_FRIEND_GET_PUBLIC_KEY_OK
C.tox_friend_get_public_key(t.tox, (C.uint32_t)(friendNumber), (*C.uint8_t)(&publickey[0]), &toxErrFriendGetPublicKey)
switch ToxErrFriendGetPublicKey(toxErrFriendGetPublicKey) {
case TOX_ERR_FRIEND_GET_PUBLIC_KEY_OK:
return publickey, nil
case TOX_ERR_FRIEND_GET_PUBLIC_KEY_FRIEND_NOT_FOUND:
return nil, ErrArgs
default:
return nil, ErrFuncFail
}
return nil, ErrUnknown
}
/* FriendGetLastOnline returns the timestamp of the last time the friend with
* the given friendNumber was seen online. */
func (t *Tox) FriendGetLastOnline(friendNumber uint32) (time.Time, error) {
if t.tox == nil {
return time.Time{}, ErrToxInit
}
var toxErrFriendGetLastOnline C.TOX_ERR_FRIEND_GET_LAST_ONLINE = C.TOX_ERR_FRIEND_GET_LAST_ONLINE_OK
ret := C.tox_friend_get_last_online(t.tox, (C.uint32_t)(friendNumber), &toxErrFriendGetLastOnline)
if ret == C.INT64_MAX || ToxErrFriendGetLastOnline(toxErrFriendGetLastOnline) != TOX_ERR_FRIEND_GET_LAST_ONLINE_OK {
return time.Time{}, ErrFuncFail
}
last := time.Unix(int64(ret), 0)
return last, nil
}
/* FriendGetNameSize returns the length of the name of friendNumber. */
func (t *Tox) FriendGetNameSize(friendNumber uint32) (int64, error) {
if t.tox == nil {
return 0, ErrToxInit
}
var toxErrFriendQuery C.TOX_ERR_FRIEND_QUERY = C.TOX_ERR_FRIEND_QUERY_OK
ret := C.tox_friend_get_name_size(t.tox, (C.uint32_t)(friendNumber), &toxErrFriendQuery)
if ToxErrFriendQuery(toxErrFriendQuery) != TOX_ERR_FRIEND_QUERY_OK {
return 0, ErrFuncFail
}
return int64(ret), nil
}
/* FriendGetName returns the name of friendNumber. */
func (t *Tox) FriendGetName(friendNumber uint32) (string, error) {
if t.tox == nil {
return "", ErrToxInit
}
length, err := t.FriendGetNameSize(friendNumber)
if err != nil {
return "", ErrFuncFail
}
name := make([]byte, length)
if length > 0 {
var toxErrFriendQuery C.TOX_ERR_FRIEND_QUERY = C.TOX_ERR_FRIEND_QUERY_OK
success := C.tox_friend_get_name(t.tox, (C.uint32_t)(friendNumber), (*C.uint8_t)(&name[0]), &toxErrFriendQuery)
if success != true || ToxErrFriendQuery(toxErrFriendQuery) != TOX_ERR_FRIEND_QUERY_OK {
return "", ErrFuncFail
}
}
return string(name), nil
}
/* FriendGetStatusMessageSize returns the size of the status of a friend with
* the given friendNumber.
*/
func (t *Tox) FriendGetStatusMessageSize(friendNumber uint32) (int64, error) {
if t.tox == nil {
return 0, ErrToxInit
}
var toxErrFriendQuery C.TOX_ERR_FRIEND_QUERY = C.TOX_ERR_FRIEND_QUERY_OK
ret := C.tox_friend_get_status_message_size(t.tox, (C.uint32_t)(friendNumber), &toxErrFriendQuery)
if ToxErrFriendQuery(toxErrFriendQuery) != TOX_ERR_FRIEND_QUERY_OK {
return 0, ErrFuncFail
}
return int64(ret), nil
}
/* FriendGetStatusMessage returns the status message of friend with the given
* friendNumber.
*/
func (t *Tox) FriendGetStatusMessage(friendNumber uint32) (string, error) {
if t.tox == nil {
return "", ErrToxInit
}
var toxErrFriendQuery C.TOX_ERR_FRIEND_QUERY = C.TOX_ERR_FRIEND_QUERY_OK
size, error := t.FriendGetStatusMessageSize(friendNumber)
if error != nil {
return "", ErrFuncFail
}
statusMessage := make([]byte, size)
if size > 0 {
toxErrFriendQuery = C.TOX_ERR_FRIEND_QUERY_OK
n := C.tox_friend_get_status_message(t.tox, (C.uint32_t)(friendNumber), (*C.uint8_t)(&statusMessage[0]), &toxErrFriendQuery)
if n != true || ToxErrFriendQuery(toxErrFriendQuery) != TOX_ERR_FRIEND_QUERY_OK {
return "", ErrFuncFail
}
}
return string(statusMessage), nil
}
/* FriendGetStatus returns the status of friendNumber. */
func (t *Tox) FriendGetStatus(friendNumber uint32) (ToxUserStatus, error) {
if t.tox == nil {
return TOX_USERSTATUS_NONE, ErrToxInit
}
var toxErrFriendQuery C.TOX_ERR_FRIEND_QUERY = C.TOX_ERR_FRIEND_QUERY_OK
status := C.tox_friend_get_status(t.tox, (C.uint32_t)(friendNumber), &toxErrFriendQuery)
if ToxErrFriendQuery(toxErrFriendQuery) != TOX_ERR_FRIEND_QUERY_OK {
return TOX_USERSTATUS_NONE, ErrFuncFail
}
return ToxUserStatus(status), nil
}
/* FriendGetConnectionStatus returns true if the friend is connected. */
func (t *Tox) FriendGetConnectionStatus(friendNumber uint32) (ToxConnection, error) {
if t.tox == nil {
return TOX_CONNECTION_NONE, ErrToxInit
}
var toxErrFriendQuery C.TOX_ERR_FRIEND_QUERY = C.TOX_ERR_FRIEND_QUERY_OK
status := C.tox_friend_get_connection_status(t.tox, (C.uint32_t)(friendNumber), &toxErrFriendQuery)
if ToxErrFriendQuery(toxErrFriendQuery) != TOX_ERR_FRIEND_QUERY_OK {
return TOX_CONNECTION_NONE, ErrFuncFail
}
return ToxConnection(status), nil
}
/* FriendGetTyping returns true if friendNumber is typing. */
func (t *Tox) FriendGetTyping(friendNumber uint32) (bool, error) {
if t.tox == nil {
return false, ErrToxInit
}
var toxErrFriendQuery C.TOX_ERR_FRIEND_QUERY = C.TOX_ERR_FRIEND_QUERY_OK
istyping := C.tox_friend_get_typing(t.tox, (C.uint32_t)(friendNumber), &toxErrFriendQuery)
if ToxErrFriendQuery(toxErrFriendQuery) != TOX_ERR_FRIEND_QUERY_OK {
return false, ErrFuncFail
}
return bool(istyping), nil
}
/* SelfSetTyping sets your typing status to a friend. */
func (t *Tox) SelfSetTyping(friendNumber uint32, typing bool) error {
if t.tox == nil {
return ErrToxInit
}
var toxErrSetTyping C.TOX_ERR_SET_TYPING = C.TOX_ERR_SET_TYPING_OK
success := C.tox_self_set_typing(t.tox, (C.uint32_t)(friendNumber), (C._Bool)(typing), &toxErrSetTyping)
if !bool(success) || ToxErrSetTyping(toxErrSetTyping) != TOX_ERR_SET_TYPING_OK {
return ErrFuncFail
}
return nil
}
/* FriendSendMessage sends a message to a friend if he/she is online.
* Maximum message length is MAX_MESSAGE_LENGTH.
* messagetype is the type of the message (normal, action, ...).
* Returns the message ID if successful, an error otherwise.
*/
func (t *Tox) FriendSendMessage(friendNumber uint32, messagetype ToxMessageType, message string) (uint32, error) {
if t.tox == nil {
return 0, ErrToxInit
}
if len(message) == 0 {
return 0, ErrArgs
}
var cMessageType C.TOX_MESSAGE_TYPE
if messagetype == TOX_MESSAGE_TYPE_ACTION {
cMessageType = C.TOX_MESSAGE_TYPE_ACTION
} else {
cMessageType = C.TOX_MESSAGE_TYPE_NORMAL
}
cMessage := (*C.uint8_t)(&[]byte(message)[0])
var toxFriendSendMessageError C.TOX_ERR_FRIEND_SEND_MESSAGE = C.TOX_ERR_FRIEND_SEND_MESSAGE_OK
n := C.tox_friend_send_message(t.tox, (C.uint32_t)(friendNumber), cMessageType, cMessage, (C.size_t)(len(message)), &toxFriendSendMessageError)
if ToxErrFriendSendMessage(toxFriendSendMessageError) != TOX_ERR_FRIEND_SEND_MESSAGE_OK {
return 0, ErrFuncFail
}
return uint32(n), nil
}
/* Hash generates a cryptographic hash of the given data (can be used to cache
* avatars). */
func (t *Tox) Hash(data []byte) ([]byte, error) {
if t.tox == nil {
return nil, ErrToxInit
}
var cData *C.uint8_t
if len(data) == 0 {
cData = nil
} else {
cData = (*C.uint8_t)(&data[0])
}
hash := make([]byte, TOX_HASH_LENGTH)
success := C.tox_hash((*C.uint8_t)(&hash[0]), cData, C.size_t(len(data)))
if !bool(success) {
return nil, ErrFuncFail
}
return hash, nil
}
/* FileControl sends a FileControl to a friend with the given friendNumber. */
func (t *Tox) FileControl(friendNumber uint32, fileNumber uint32, fileControl ToxFileControl) error {
if t.tox == nil {
return ErrToxInit
}
var cFileControl C.TOX_FILE_CONTROL
switch ToxFileControl(fileControl) {
case TOX_FILE_CONTROL_RESUME:
cFileControl = C.TOX_FILE_CONTROL_RESUME
case TOX_FILE_CONTROL_PAUSE:
cFileControl = C.TOX_FILE_CONTROL_PAUSE
case TOX_FILE_CONTROL_CANCEL:
cFileControl = C.TOX_FILE_CONTROL_CANCEL
}
var toxErrFileControl C.TOX_ERR_FILE_CONTROL
success := C.tox_file_control(t.tox, (C.uint32_t)(friendNumber), (C.uint32_t)(fileNumber), cFileControl, &toxErrFileControl)
if !bool(success) || ToxErrFileControl(toxErrFileControl) != TOX_ERR_FILE_CONTROL_OK {
return ErrFuncFail
}
return nil
}
/* FileSeek sends a file seek control command to a friend for a given file
* transfer. */
func (t *Tox) FileSeek(friendNumber uint32, fileNumber uint32, position uint64) error {
if t.tox == nil {
return ErrToxInit
}
var toxErrFileSeek C.TOX_ERR_FILE_SEEK
success := C.tox_file_seek(t.tox, C.uint32_t(friendNumber), C.uint32_t(fileNumber), C.uint64_t(position), &toxErrFileSeek)
if !bool(success) || ToxErrFileSeek(toxErrFileSeek) != TOX_ERR_FILE_SEEK_OK {
return ErrFuncFail
}
return nil
}
/* FileGetFileId returns the file id associated to the file transfer. */
func (t *Tox) FileGetFileId(friendNumber uint32, fileNumber uint32) ([]byte, error) {
if t.tox == nil {
return nil, ErrToxInit
}
fileId := make([]byte, TOX_FILE_ID_LENGTH)
var toxErrFileGet C.TOX_ERR_FILE_GET
success := C.tox_file_get_file_id(t.tox, C.uint32_t(friendNumber), C.uint32_t(fileNumber), (*C.uint8_t)(&fileId[0]), &toxErrFileGet)
if !bool(success) || ToxErrFileGet(toxErrFileGet) != TOX_ERR_FILE_GET_OK {
return nil, ErrFuncFail
}
return fileId, nil
}
/* FileSend sends a file transmission request. */
func (t *Tox) FileSend(friendNumber uint32, fileKind ToxFileKind, fileLength uint64, fileID []byte, fileName string) (uint32, error) {
if t.tox == nil {
return 0, ErrToxInit
}
var cFileKind = C.TOX_FILE_KIND_DATA
switch ToxFileKind(fileKind) {
case TOX_FILE_KIND_AVATAR:
cFileKind = C.TOX_FILE_KIND_AVATAR
case TOX_FILE_KIND_DATA:
cFileKind = C.TOX_FILE_KIND_DATA
}
var cFileID *C.uint8_t
if fileID == nil {
cFileID = nil
} else {
if len(fileID) != TOX_FILE_ID_LENGTH {
return 0, ErrFileSendInvalidFileID
}
cFileID = (*C.uint8_t)(&[]byte(fileID)[0])
}
if len(fileName) == 0 {
return 0, ErrArgs
}
cFileName := (*C.uint8_t)(&[]byte(fileName)[0])
var toxErrFileSend C.TOX_ERR_FILE_SEND
n := C.tox_file_send(t.tox, (C.uint32_t)(friendNumber), (C.uint32_t)(cFileKind), (C.uint64_t)(fileLength), cFileID, cFileName, (C.size_t)(len(fileName)), &toxErrFileSend)
if n == C.UINT32_MAX || ToxErrFileSend(toxErrFileSend) != TOX_ERR_FILE_SEND_OK {
return 0, ErrFuncFail
}
return uint32(n), nil
}
/* FileSendChunk sends a chunk of file data to a friend. */
func (t *Tox) FileSendChunk(friendNumber uint32, fileNumber uint32, position uint64, data []byte) error {
if t.tox == nil {
return ErrToxInit
}
var cData *C.uint8_t
if len(data) == 0 {
cData = nil
} else {
cData = (*C.uint8_t)(&data[0])
}
var toxErrFileSendChunk C.TOX_ERR_FILE_SEND_CHUNK
success := C.tox_file_send_chunk(t.tox, (C.uint32_t)(friendNumber), (C.uint32_t)(fileNumber), (C.uint64_t)(position), cData, (C.size_t)(len(data)), &toxErrFileSendChunk)
if !bool(success) || ToxErrFileSendChunk(toxErrFileSendChunk) != TOX_ERR_FILE_SEND_CHUNK_OK {