editor: another undo fix
[iv.d.git] / tox.d
blob7ed58eb198099afba8eaa37f8bbb4a6582703d94
1 /*
2 * The Tox public API.
3 */
5 /*
6 * Copyright © 2016-2017 The TokTok team.
7 * Copyright © 2013 Tox project.
9 * This file is part of Tox, the free peer to peer instant messenger.
11 * Tox is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation, version 3 of the License ONLY.
15 * Tox is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with Tox. If not, see <http://www.gnu.org/licenses/>.
23 module iv.tox /*is aliced*/;
24 pragma(lib, "toxcore");
25 //pragma(lib, "toxencryptsave");
26 pragma(lib, "sodium"); // for static linking
27 import iv.alice;
28 // WARNING: functions with callbacks (and callbacks 'emselves) will be marked `@system`!
29 extern(C) nothrow @trusted {
32 /** core Public core API for Tox clients.
34 * Every function that can fail takes a function-specific error code pointer
35 * that can be used to diagnose problems with the Tox state or the function
36 * arguments. The error code pointer can be NULL, which does not influence the
37 * function's behaviour, but can be done if the reason for failure is irrelevant
38 * to the client.
40 * The exception to this rule are simple allocation functions whose only failure
41 * mode is allocation failure. They return NULL in that case, and do not set an
42 * error code.
44 * Every error code type has an OK value to which functions will set their error
45 * code value on success. Clients can keep their error code uninitialised before
46 * passing it to a function. The library guarantees that after returning, the
47 * value pointed to by the error code pointer has been initialised.
49 * Functions with pointer parameters often have a NULL error code, meaning they
50 * could not perform any operation, because one of the required parameters was
51 * NULL. Some functions operate correctly or are defined as effectless on NULL.
53 * Some functions additionally return a value outside their
54 * return type domain, or a bool containing true on success and false on
55 * failure.
57 * All functions that take a Tox instance pointer will cause undefined behaviour
58 * when passed a NULL Tox pointer.
60 * All integer values are expected in host byte order.
62 * Functions with parameters with enum types cause unspecified behaviour if the
63 * enumeration value is outside the valid range of the type. If possible, the
64 * function will try to use a sane default, but there will be no error code,
65 * and one possible action for the function to take is to have no effect.
67 * Integer constants and the memory layout of publicly exposed structs are not
68 * part of the ABI.
71 /** events Events and callbacks
73 * Events are handled by callbacks. One callback can be registered per event.
74 * All events have a callback function type named `tox_{event}_cb` and a
75 * function to register it named `tox_callback_{event}`. Passing a NULL
76 * callback will result in no callback being registered for that event. Only
77 * one callback per event can be registered, so if a client needs multiple
78 * event listeners, it needs to implement the dispatch functionality itself.
80 * The last argument to a callback is the user data pointer. It is passed from
81 * tox_iterate to each callback in sequence.
83 * The user data pointer is never stored or dereferenced by any library code, so
84 * can be any pointer, including NULL. Callbacks must all operate on the same
85 * object type. In the apidsl code (tox.in.h), this is denoted with `any`. The
86 * `any` in tox_iterate must be the same `any` as in all callbacks. In C,
87 * lacking parametric polymorphism, this is a pointer to void.
89 * Old style callbacks that are registered together with a user data pointer
90 * receive that pointer as argument when they are called. They can each have
91 * their own user data pointer of their own type.
94 /** threading Threading implications
96 * It is possible to run multiple concurrent threads with a Tox instance for
97 * each thread. It is also possible to run all Tox instances in the same thread.
98 * A common way to run Tox (multiple or single instance) is to have one thread
99 * running a simple tox_iterate loop, sleeping for tox_iteration_interval
100 * milliseconds on each iteration.
102 * If you want to access a single Tox instance from multiple threads, access
103 * to the instance must be synchronised. While multiple threads can concurrently
104 * access multiple different Tox instances, no more than one API function can
105 * operate on a single instance at any given time.
107 * Functions that write to variable length byte arrays will always have a size
108 * function associated with them. The result of this size function is only valid
109 * until another mutating function (one that takes a pointer to non-const Tox)
110 * is called. Thus, clients must ensure that no other thread calls a mutating
111 * function between the call to the size function and the call to the retrieval
112 * function.
114 * E.g. to get the current nickname, one would write
116 * ---
117 * auto length = tox_self_get_name_size(tox);
118 * auto name = new char[](length);
119 * tox_self_get_name(tox, name.ptr);
120 * ---
122 * If any other thread calls tox_self_set_name while this thread is allocating
123 * memory, the length may have become invalid, and the call to
124 * tox_self_get_name may cause undefined behaviour.
129 * The Tox instance type. All the state associated with a connection is held
130 * within the instance. Multiple instances can exist and operate concurrently.
131 * The maximum number of Tox instances that can exist on a single network
132 * device is limited. Note that this is not just a per-process limit, since the
133 * limiting factor is the number of usable ports on a device.
135 struct Tox {
136 // disable construction and postblit
137 @disable this ();
138 @disable this (this);
140 alias ToxP = Tox*;
143 /*******************************************************************************
145 * :: API version
147 ******************************************************************************/
151 * The major version number. Incremented when the API or ABI changes in an
152 * incompatible way.
154 * The function variants of these constants return the version number of the
155 * library. They can be used to display the Tox library version or to check
156 * whether the client is compatible with the dynamically linked version of Tox.
158 enum TOX_VERSION_MAJOR = 0;
159 uint tox_version_major() @nogc;
162 * The minor version number. Incremented when functionality is added without
163 * breaking the API or ABI. Set to 0 when the major version number is
164 * incremented.
166 enum TOX_VERSION_MINOR = 2;
167 uint tox_version_minor() @nogc;
170 * The patch or revision number. Incremented when bugfixes are applied without
171 * changing any functionality or API or ABI.
173 enum TOX_VERSION_PATCH = 10;
174 uint tox_version_patch() @nogc;
177 * A macro to check at preprocessing time whether the client code is compatible
178 * with the installed version of Tox. Leading zeros in the version number are
179 * ignored. E.g. 0.1.5 is to 0.1.4 what 1.5 is to 1.4, that is: it can add new
180 * features, but can't break the API.
182 bool TOX_VERSION_IS_API_COMPATIBLE(int MAJOR, int MINOR, int PATCH) pure nothrow @safe @nogc {
183 return
184 ((TOX_VERSION_MAJOR > 0 && TOX_VERSION_MAJOR == MAJOR) && (
185 /* 1.x.x, 2.x.x, etc. with matching major version. */
186 TOX_VERSION_MINOR > MINOR ||
187 TOX_VERSION_MINOR == MINOR && TOX_VERSION_PATCH >= PATCH
188 )) || ((TOX_VERSION_MAJOR == 0 && MAJOR == 0) && (
189 /* 0.x.x makes minor behave like major above. */
190 (TOX_VERSION_MINOR > 0 && TOX_VERSION_MINOR == MINOR) && (
191 TOX_VERSION_PATCH >= PATCH
192 )) || ((TOX_VERSION_MINOR == 0 && MINOR == 0) && (
193 /* 0.0.x and 0.0.y are only compatible if x == y. */
194 TOX_VERSION_PATCH == PATCH
201 * Return whether the compiled library version is compatible with the passed
202 * version numbers.
204 bool tox_version_is_compatible(uint major, uint minor, uint patch) nothrow @nogc;
207 * A convenience macro to call tox_version_is_compatible with the currently
208 * compiling API version.
210 bool TOX_VERSION_IS_ABI_COMPATIBLE() nothrow @safe @nogc { return tox_version_is_compatible(TOX_VERSION_MAJOR, TOX_VERSION_MINOR, TOX_VERSION_PATCH); }
213 /*******************************************************************************
215 * :: Numeric constants
217 * The values of these are not part of the ABI. Prefer to use the function
218 * versions of them for code that should remain compatible with future versions
219 * of toxcore.
221 ******************************************************************************/
225 * The size of a Tox Public Key in bytes.
227 enum TOX_PUBLIC_KEY_SIZE = 32;
228 uint tox_public_key_size() @nogc;
231 * The size of a Tox Secret Key in bytes.
233 enum TOX_SECRET_KEY_SIZE = 32;
234 uint tox_secret_key_size() @nogc;
237 * The size of a Tox Conference unique id in bytes.
239 * @deprecated Use TOX_CONFERENCE_ID_SIZE instead.
241 enum TOX_CONFERENCE_UID_SIZE = 32;
243 uint tox_conference_uid_size() @nogc;
246 * The size of a Tox Conference unique id in bytes.
248 enum TOX_CONFERENCE_ID_SIZE = 32;
250 uint tox_conference_id_size() @nogc;
253 * The size of the nospam in bytes when written in a Tox address.
255 enum TOX_NOSPAM_SIZE = cast(uint)(uint.sizeof);
256 uint tox_nospam_size() @nogc;
259 * The size of a Tox address in bytes. Tox addresses are in the format
260 * [Public Key (TOX_PUBLIC_KEY_SIZE bytes)][nospam (4 bytes)][checksum (2 bytes)].
262 * The checksum is computed over the Public Key and the nospam value. The first
263 * byte is an XOR of all the even bytes (0, 2, 4, ...), the second byte is an
264 * XOR of all the odd bytes (1, 3, 5, ...) of the Public Key and nospam.
266 enum TOX_ADDRESS_SIZE = cast(uint)(TOX_PUBLIC_KEY_SIZE + TOX_NOSPAM_SIZE + ushort.sizeof);
267 uint tox_address_size() @nogc;
270 * Maximum length of a nickname in bytes.
272 * @deprecated The macro will be removed in 0.3.0. Use the function instead.
274 deprecated enum TOX_MAX_NAME_LENGTH = 128;
275 uint tox_max_name_length() @nogc;
278 * Maximum length of a status message in bytes.
280 * @deprecated The macro will be removed in 0.3.0. Use the function instead.
282 deprecated enum TOX_MAX_STATUS_MESSAGE_LENGTH = 1007;
283 uint tox_max_status_message_length() @nogc;
286 * Maximum length of a friend request message in bytes.
288 * @deprecated The macro will be removed in 0.3.0. Use the function instead.
290 deprecated enum TOX_MAX_FRIEND_REQUEST_LENGTH = 1016;
291 uint tox_max_friend_request_length() @nogc;
294 * Maximum length of a single message after which it should be split.
296 * @deprecated The macro will be removed in 0.3.0. Use the function instead.
298 deprecated enum TOX_MAX_MESSAGE_LENGTH = 1372;
299 uint tox_max_message_length() @nogc;
302 * Maximum size of custom packets. TODO(iphydf): should be LENGTH?
304 * @deprecated The macro will be removed in 0.3.0. Use the function instead.
306 deprecated enum TOX_MAX_CUSTOM_PACKET_SIZE = 1373;
307 uint tox_max_custom_packet_size() @nogc;
310 * The number of bytes in a hash generated by tox_hash.
312 enum TOX_HASH_LENGTH = 32;
313 uint tox_hash_length() @nogc;
316 * The number of bytes in a file id.
318 enum TOX_FILE_ID_LENGTH = 32;
319 uint tox_file_id_length() @nogc;
322 * Maximum file name length for file transfers.
324 * @deprecated The macro will be removed in 0.3.0. Use the function instead.
326 deprecated enum TOX_MAX_FILENAME_LENGTH = 255;
327 uint tox_max_filename_length() @nogc;
330 * Maximum length of a hostname, e.g. proxy or bootstrap node names.
332 * This length does not include the NUL byte. Hostnames are NUL-terminated C
333 * strings, so they are 255 characters plus one NUL byte.
335 * @deprecated The macro will be removed in 0.3.0. Use the function instead.
337 enum TOX_MAX_HOSTNAME_LENGTH = 255;
338 uint tox_max_hostname_length() @nogc;
341 /*******************************************************************************
343 * :: Global enumerations
345 ******************************************************************************/
349 * Represents the possible statuses a client can have.
351 alias TOX_USER_STATUS = int;
352 enum : int {
354 * User is online and available.
356 TOX_USER_STATUS_NONE,
359 * User is away. Clients can set this e.g. after a user defined
360 * inactivity time.
362 TOX_USER_STATUS_AWAY,
365 * User is busy. Signals to other clients that this client does not
366 * currently wish to communicate.
368 TOX_USER_STATUS_BUSY,
373 * Represents message types for tox_friend_send_message and conference
374 * messages.
376 alias TOX_MESSAGE_TYPE = int;
377 enum : int {
379 * Normal text message. Similar to PRIVMSG on IRC.
381 TOX_MESSAGE_TYPE_NORMAL,
384 * A message describing an user action. This is similar to /me (CTCP ACTION)
385 * on IRC.
387 TOX_MESSAGE_TYPE_ACTION,
391 /*******************************************************************************
393 * :: Startup options
395 ******************************************************************************/
399 * Type of proxy used to connect to TCP relays.
401 alias TOX_PROXY_TYPE = int;
402 enum : int {
404 * Don't use a proxy.
406 TOX_PROXY_TYPE_NONE,
409 * HTTP proxy using CONNECT.
411 TOX_PROXY_TYPE_HTTP,
414 * SOCKS proxy for simple socket pipes.
416 TOX_PROXY_TYPE_SOCKS5,
421 * Type of savedata to create the Tox instance from.
423 alias TOX_SAVEDATA_TYPE = int;
424 enum : int {
426 * No savedata.
428 TOX_SAVEDATA_TYPE_NONE,
431 * Savedata is one that was obtained from tox_get_savedata.
433 TOX_SAVEDATA_TYPE_TOX_SAVE,
436 * Savedata is a secret key of length TOX_SECRET_KEY_SIZE.
438 TOX_SAVEDATA_TYPE_SECRET_KEY,
443 * Severity level of log messages.
445 alias TOX_LOG_LEVEL = int;
446 enum : int {
448 * Very detailed traces including all network activity.
450 TOX_LOG_LEVEL_TRACE,
453 * Debug messages such as which port we bind to.
455 TOX_LOG_LEVEL_DEBUG,
458 * Informational log messages such as video call status changes.
460 TOX_LOG_LEVEL_INFO,
463 * Warnings about internal inconsistency or logic errors.
465 TOX_LOG_LEVEL_WARNING,
468 * Severe unexpected errors caused by external or internal inconsistency.
470 TOX_LOG_LEVEL_ERROR,
475 * This event is triggered when the toxcore library logs an internal message.
476 * This is mostly useful for debugging. This callback can be called from any
477 * function, not just tox_iterate. This means the user data lifetime must at
478 * least extend between registering and unregistering it or tox_kill.
480 * Other toxcore modules such as toxav may concurrently call this callback at
481 * any time. Thus, user code must make sure it is equipped to handle concurrent
482 * execution, e.g. by employing appropriate mutex locking.
484 * @param level The severity of the log message.
485 * @param file The source file from which the message originated.
486 * @param line The source line from which the message originated.
487 * @param func The function from which the message originated.
488 * @param message The log message.
489 * @param user_data The user data pointer passed to tox_new in options.
491 alias tox_log_cb = void function (Tox* tox, TOX_LOG_LEVEL level, const(char)* file, uint line, const(char)* func, const(char)* message, void* user_data) nothrow @system;
495 * This struct contains all the startup options for Tox. You must use tox_options_new to
496 * allocate an object of this type.
498 struct Tox_Options {
499 // disable construction and postblit
500 @disable this ();
501 @disable this (this);
506 * The type of socket to create.
508 * If this is set to false, an IPv4 socket is created, which subsequently
509 * only allows IPv4 communication.
510 * If it is set to true, an IPv6 socket is created, allowing both IPv4 and
511 * IPv6 communication.
513 bool tox_options_get_ipv6_enabled(const(Tox_Options)* options) @nogc;
514 /// ditto
515 void tox_options_set_ipv6_enabled(Tox_Options* options, bool ipv6_enabled) @nogc;
518 * Enable the use of UDP communication when available.
520 * Setting this to false will force Tox to use TCP only. Communications will
521 * need to be relayed through a TCP relay node, potentially slowing them down.
523 * If a proxy is enabled, UDP will be disabled if either toxcore or the
524 * proxy don't support proxying UDP messages.
526 bool tox_options_get_udp_enabled(const(Tox_Options)* options) @nogc;
527 /// ditto
528 void tox_options_set_udp_enabled(Tox_Options* options, bool udp_enabled) @nogc;
531 * Enable local network peer discovery.
533 * Disabling this will cause Tox to not look for peers on the local network.
535 bool tox_options_get_local_discovery_enabled(const(Tox_Options)* options) @nogc;
536 /// ditto
537 void tox_options_set_local_discovery_enabled(Tox_Options* options, bool local_discovery_enabled) @nogc;
540 * Pass communications through a proxy.
542 TOX_PROXY_TYPE tox_options_get_proxy_type(const(Tox_Options)* options) @nogc;
543 /// ditto
544 void tox_options_set_proxy_type(Tox_Options* options, TOX_PROXY_TYPE type) @nogc;
547 * The IP address or DNS name of the proxy to be used.
549 * If used, this must be non-NULL and be a valid DNS name. The name must not
550 * exceed TOX_MAX_HOSTNAME_LENGTH characters, and be in a NUL-terminated C string
551 * format (TOX_MAX_HOSTNAME_LENGTH includes the NUL byte).
553 * This member is ignored (it can be NULL) if proxy_type is TOX_PROXY_TYPE_NONE.
555 * The data pointed at by this member is owned by the user, so must
556 * outlive the options object.
558 const(char)* tox_options_get_proxy_host(const(Tox_Options)* options) @nogc;
559 /// ditto
560 void tox_options_set_proxy_host(Tox_Options* options, const(char)* host) @nogc;
563 * The port to use to connect to the proxy server.
565 * Ports must be in the range (1, 65535). The value is ignored if
566 * proxy_type is TOX_PROXY_TYPE_NONE.
568 ushort tox_options_get_proxy_port(const(Tox_Options)* options) @nogc;
569 /// ditto
570 void tox_options_set_proxy_port(Tox_Options* options, ushort port) @nogc;
573 * The start port of the inclusive port range to attempt to use.
575 * If both start_port and end_port are 0, the default port range will be
576 * used: [33445, 33545].
578 * If either start_port or end_port is 0 while the other is non-zero, the
579 * non-zero port will be the only port in the range.
581 * Having start_port > end_port will yield the same behavior as if start_port
582 * and end_port were swapped.
584 ushort tox_options_get_start_port(const(Tox_Options)* options) @nogc;
585 /// ditto
586 void tox_options_set_start_port(Tox_Options* options, ushort start_port) @nogc;
589 * The end port of the inclusive port range to attempt to use.
591 ushort tox_options_get_end_port(const(Tox_Options)* options) @nogc;
592 /// ditto
593 void tox_options_set_end_port(Tox_Options* options, ushort end_port) @nogc;
596 * The port to use for the TCP server (relay). If 0, the TCP server is
597 * disabled.
599 * Enabling it is not required for Tox to function properly.
601 * When enabled, your Tox instance can act as a TCP relay for other Tox
602 * instance. This leads to increased traffic, thus when writing a client
603 * it is recommended to enable TCP server only if the user has an option
604 * to disable it.
606 ushort tox_options_get_tcp_port(const(Tox_Options)* options) @nogc;
607 /// ditto
608 void tox_options_set_tcp_port(Tox_Options* options, ushort tcp_port) @nogc;
611 * Enables or disables UDP hole-punching in toxcore. (Default: enabled).
613 bool tox_options_get_hole_punching_enabled(const(Tox_Options)* options) @nogc;
614 /// ditto
615 void tox_options_set_hole_punching_enabled(Tox_Options* options, bool hole_punching_enabled) @nogc;
618 * The type of savedata to load from.
620 TOX_SAVEDATA_TYPE tox_options_get_savedata_type(const(Tox_Options)* options) @nogc;
621 /// ditto
622 void tox_options_set_savedata_type(Tox_Options* options, TOX_SAVEDATA_TYPE type) @nogc;
625 * The savedata.
627 * The data pointed at by this member is owned by the user, so must
628 * outlive the options object.
630 const(void)* tox_options_get_savedata_data(const(Tox_Options)* options) @nogc;
631 /// ditto
632 void tox_options_set_savedata_data(Tox_Options* options, const(void)* data, usize length) @nogc;
635 * The length of the savedata.
637 usize tox_options_get_savedata_length(const(Tox_Options)* options) @nogc;
638 /// ditto
639 void tox_options_set_savedata_length(Tox_Options* options, usize length) @nogc;
642 * Logging callback for the new tox instance.
644 tox_log_cb tox_options_get_log_callback(const(Tox_Options)* options) @nogc;
645 /// ditto
646 void tox_options_set_log_callback(Tox_Options* options, tox_log_cb callback) @nogc;
649 * User data pointer passed to the logging callback.
651 void* tox_options_get_log_user_data(const(Tox_Options)* options) @nogc;
652 /// ditto
653 void tox_options_set_log_user_data(Tox_Options* options, void* user_data) @nogc;
657 * Initialises a Tox_Options object with the default options.
659 * The result of this function is independent of the original options. All
660 * values will be overwritten, no values will be read (so it is permissible
661 * to pass an uninitialised object).
663 * If options is NULL, this function has no effect.
665 * @param options An options object to be filled with default options.
667 void tox_options_default(Tox_Options* options) @nogc;
670 alias TOX_ERR_OPTIONS_NEW = int;
671 enum : int {
673 * The function returned successfully.
675 TOX_ERR_OPTIONS_NEW_OK,
678 * The function failed to allocate enough memory for the options struct.
680 TOX_ERR_OPTIONS_NEW_MALLOC,
685 * Allocates a new Tox_Options object and initialises it with the default
686 * options. This function can be used to preserve long term ABI compatibility by
687 * giving the responsibility of allocation and deallocation to the Tox library.
689 * Objects returned from this function must be freed using the tox_options_free
690 * function.
692 * @return A new Tox_Options object with default options or NULL on failure.
694 Tox_Options* tox_options_new(TOX_ERR_OPTIONS_NEW* error/*=null*/) @nogc;
697 * Releases all resources associated with an options objects.
699 * Passing a pointer that was not returned by tox_options_new results in
700 * undefined behaviour.
702 void tox_options_free(Tox_Options* options) @nogc;
705 /*******************************************************************************
707 * :: Creation and destruction
709 ******************************************************************************/
712 alias TOX_ERR_NEW = int;
713 enum : int {
715 * The function returned successfully.
717 TOX_ERR_NEW_OK,
720 * One of the arguments to the function was NULL when it was not expected.
722 TOX_ERR_NEW_NULL,
725 * The function was unable to allocate enough memory to store the internal
726 * structures for the Tox object.
728 TOX_ERR_NEW_MALLOC,
731 * The function was unable to bind to a port. This may mean that all ports
732 * have already been bound, e.g. by other Tox instances, or it may mean
733 * a permission error. You may be able to gather more information from errno.
735 TOX_ERR_NEW_PORT_ALLOC,
738 * proxy_type was invalid.
740 TOX_ERR_NEW_PROXY_BAD_TYPE,
743 * proxy_type was valid but the proxy_host passed had an invalid format
744 * or was NULL.
746 TOX_ERR_NEW_PROXY_BAD_HOST,
749 * proxy_type was valid, but the proxy_port was invalid.
751 TOX_ERR_NEW_PROXY_BAD_PORT,
754 * The proxy address passed could not be resolved.
756 TOX_ERR_NEW_PROXY_NOT_FOUND,
759 * The byte array to be loaded contained an encrypted save.
761 TOX_ERR_NEW_LOAD_ENCRYPTED,
764 * The data format was invalid. This can happen when loading data that was
765 * saved by an older version of Tox, or when the data has been corrupted.
766 * When loading from badly formatted data, some data may have been loaded,
767 * and the rest is discarded. Passing an invalid length parameter also
768 * causes this error.
770 TOX_ERR_NEW_LOAD_BAD_FORMAT,
775 * @brief Creates and initialises a new Tox instance with the options passed.
777 * This function will bring the instance into a valid state. Running the event
778 * loop with a new instance will operate correctly.
780 * If loading failed or succeeded only partially, the new or partially loaded
781 * instance is returned and an error code is set.
783 * @param options An options object as described above. If this parameter is
784 * NULL, the default options are used.
786 * @see tox_iterate for the event loop.
788 * @return A new Tox instance pointer on success or NULL on failure.
790 Tox* tox_new(const(Tox_Options)* options, TOX_ERR_NEW* error/*=null*/) @nogc;
793 * Releases all resources associated with the Tox instance and disconnects from
794 * the network.
796 * After calling this function, the Tox pointer becomes invalid. No other
797 * functions can be called, and the pointer value can no longer be read.
799 void tox_kill(Tox* tox) @nogc;
802 * Calculates the number of bytes required to store the tox instance with
803 * tox_get_savedata. This function cannot fail. The result is always greater than 0.
805 * @see threading for concurrency implications.
807 usize tox_get_savedata_size(const(Tox)* tox) @nogc;
810 * Store all information associated with the tox instance to a byte array.
812 * @param savedata A memory region large enough to store the tox instance
813 * data. Call tox_get_savedata_size to find the number of bytes required. If this parameter
814 * is NULL, this function has no effect.
816 void tox_get_savedata(const(Tox)* tox, void* savedata) @nogc;
819 /*******************************************************************************
821 * :: Connection lifecycle and event loop
823 ******************************************************************************/
826 alias TOX_ERR_BOOTSTRAP = int;
827 enum : int {
829 * The function returned successfully.
831 TOX_ERR_BOOTSTRAP_OK,
834 * One of the arguments to the function was NULL when it was not expected.
836 TOX_ERR_BOOTSTRAP_NULL,
839 * The hostname could not be resolved to an IP address, or the IP address
840 * passed was invalid.
842 TOX_ERR_BOOTSTRAP_BAD_HOST,
845 * The port passed was invalid. The valid port range is (1, 65535).
847 TOX_ERR_BOOTSTRAP_BAD_PORT,
852 * Sends a "get nodes" request to the given bootstrap node with IP, port, and
853 * public key to setup connections.
855 * This function will attempt to connect to the node using UDP. You must use
856 * this function even if Tox_Options.udp_enabled was set to false.
858 * @param host The hostname or IP address (IPv4 or IPv6) of the node. Must be
859 * at most TOX_MAX_HOSTNAME_LENGTH chars, including the NUL byte.
860 * @param port The port on the host on which the bootstrap Tox instance is
861 * listening.
862 * @param public_key The long term public key of the bootstrap node
863 * (TOX_PUBLIC_KEY_SIZE bytes).
864 * @return true on success.
866 bool tox_bootstrap(Tox* tox, const(char)* address, ushort port, const(void)* public_key, TOX_ERR_BOOTSTRAP* error/*=null*/) @nogc;
869 * Adds additional host:port pair as TCP relay.
871 * This function can be used to initiate TCP connections to different ports on
872 * the same bootstrap node, or to add TCP relays without using them as
873 * bootstrap nodes.
875 * @param host The hostname or IP address (IPv4 or IPv6) of the node. Must be
876 * at most TOX_MAX_HOSTNAME_LENGTH chars, including the NUL byte.
877 * @param port The port on the host on which the TCP relay is listening.
878 * @param public_key The long term public key of the TCP relay
879 * (TOX_PUBLIC_KEY_SIZE bytes).
880 * @return true on success.
882 bool tox_add_tcp_relay(Tox* tox, const(char)* host, ushort port, const(void)* public_key, TOX_ERR_BOOTSTRAP* error/*=null*/) @nogc;
886 * Protocols that can be used to connect to the network or friends.
888 alias TOX_CONNECTION = int;
889 enum : int {
891 * There is no connection. This instance, or the friend the state change is
892 * about, is now offline.
894 TOX_CONNECTION_NONE,
897 * A TCP connection has been established. For the own instance, this means it
898 * is connected through a TCP relay, only. For a friend, this means that the
899 * connection to that particular friend goes through a TCP relay.
901 TOX_CONNECTION_TCP,
904 * A UDP connection has been established. For the own instance, this means it
905 * is able to send UDP packets to DHT nodes, but may still be connected to
906 * a TCP relay. For a friend, this means that the connection to that
907 * particular friend was built using direct UDP packets.
909 TOX_CONNECTION_UDP,
914 * Return whether we are connected to the DHT. The return value is equal to the
915 * last value received through the `self_connection_status` callback.
917 * @deprecated This getter is deprecated. Use the event and store the status
918 * in the client state.
920 deprecated TOX_CONNECTION tox_self_get_connection_status(const(Tox)* tox) @nogc;
923 * @param connection_status Whether we are connected to the DHT.
925 alias tox_self_connection_status_cb = void function (Tox* tox, TOX_CONNECTION connection_status, void* user_data) nothrow @system;
929 * Set the callback for the `self_connection_status` event. Pass NULL to unset.
931 * This event is triggered whenever there is a change in the DHT connection
932 * state. When disconnected, a client may choose to call tox_bootstrap again, to
933 * reconnect to the DHT. Note that this state may frequently change for short
934 * amounts of time. Clients should therefore not immediately bootstrap on
935 * receiving a disconnect.
937 * TODO(iphydf): how long should a client wait before bootstrapping again?
939 void tox_callback_self_connection_status(Tox* tox, tox_self_connection_status_cb callback) @nogc;
942 * Return the time in milliseconds before tox_iterate() should be called again
943 * for optimal performance.
945 uint tox_iteration_interval(const(Tox)* tox) @nogc;
948 * The main loop that needs to be run in intervals of tox_iteration_interval()
949 * milliseconds.
951 void tox_iterate(Tox* tox, void* user_data) @nogc;
954 /*******************************************************************************
956 * :: Internal client information (Tox address/id)
958 ******************************************************************************/
962 * Writes the Tox friend address of the client to a byte array. The address is
963 * not in human-readable format. If a client wants to display the address,
964 * formatting is required.
966 * @param address A memory region of at least TOX_ADDRESS_SIZE bytes. If this
967 * parameter is NULL, this function has no effect.
968 * @see TOX_ADDRESS_SIZE for the address format.
970 void tox_self_get_address(const(Tox)* tox, void* address) @nogc;
973 * Set the 4-byte nospam part of the address. This value is expected in host
974 * byte order. I.e. 0x12345678 will form the bytes [12, 34, 56, 78] in the
975 * nospam part of the Tox friend address.
977 * @param nospam Any 32 bit unsigned integer.
979 void tox_self_set_nospam(Tox* tox, uint nospam) @nogc;
982 * Get the 4-byte nospam part of the address. This value is returned in host
983 * byte order.
985 uint tox_self_get_nospam(const(Tox)* tox) @nogc;
988 * Copy the Tox Public Key (long term) from the Tox object.
990 * @param public_key A memory region of at least TOX_PUBLIC_KEY_SIZE bytes. If
991 * this parameter is NULL, this function has no effect.
993 void tox_self_get_public_key(const(Tox)* tox, void* public_key) @nogc;
996 * Copy the Tox Secret Key from the Tox object.
998 * @param secret_key A memory region of at least TOX_SECRET_KEY_SIZE bytes. If
999 * this parameter is NULL, this function has no effect.
1001 void tox_self_get_secret_key(const(Tox)* tox, void* secret_key) @nogc;
1004 /*******************************************************************************
1006 * :: User-visible client information (nickname/status)
1008 ******************************************************************************/
1012 * Common error codes for all functions that set a piece of user-visible
1013 * client information.
1015 alias TOX_ERR_SET_INFO = int;
1016 enum : int {
1018 * The function returned successfully.
1020 TOX_ERR_SET_INFO_OK,
1023 * One of the arguments to the function was NULL when it was not expected.
1025 TOX_ERR_SET_INFO_NULL,
1028 * Information length exceeded maximum permissible size.
1030 TOX_ERR_SET_INFO_TOO_LONG,
1035 * Set the nickname for the Tox client.
1037 * Nickname length cannot exceed TOX_MAX_NAME_LENGTH. If length is 0, the name
1038 * parameter is ignored (it can be NULL), and the nickname is set back to empty.
1040 * @param name A byte array containing the new nickname.
1041 * @param length The size of the name byte array.
1043 * @return true on success.
1045 bool tox_self_set_name(Tox* tox, const(void)* name, usize length, TOX_ERR_SET_INFO* error/*=null*/) @nogc;
1048 * Return the length of the current nickname as passed to tox_self_set_name.
1050 * If no nickname was set before calling this function, the name is empty,
1051 * and this function returns 0.
1053 * @see threading for concurrency implications.
1055 usize tox_self_get_name_size(const(Tox)* tox) @nogc;
1058 * Write the nickname set by tox_self_set_name to a byte array.
1060 * If no nickname was set before calling this function, the name is empty,
1061 * and this function has no effect.
1063 * Call tox_self_get_name_size to find out how much memory to allocate for
1064 * the result.
1066 * @param name A valid memory location large enough to hold the nickname.
1067 * If this parameter is NULL, the function has no effect.
1069 void tox_self_get_name(const(Tox)* tox, void* name) @nogc;
1072 * Set the client's status message.
1074 * Status message length cannot exceed TOX_MAX_STATUS_MESSAGE_LENGTH. If
1075 * length is 0, the status parameter is ignored (it can be NULL), and the
1076 * user status is set back to empty.
1078 bool tox_self_set_status_message(Tox* tox, const(void)* status_message, usize length, TOX_ERR_SET_INFO* error/*=null*/) @nogc;
1081 * Return the length of the current status message as passed to tox_self_set_status_message.
1083 * If no status message was set before calling this function, the status
1084 * is empty, and this function returns 0.
1086 * @see threading for concurrency implications.
1088 usize tox_self_get_status_message_size(const(Tox)* tox) @nogc;
1091 * Write the status message set by tox_self_set_status_message to a byte array.
1093 * If no status message was set before calling this function, the status is
1094 * empty, and this function has no effect.
1096 * Call tox_self_get_status_message_size to find out how much memory to allocate for
1097 * the result.
1099 * @param status_message A valid memory location large enough to hold the
1100 * status message. If this parameter is NULL, the function has no effect.
1102 void tox_self_get_status_message(const(Tox)* tox, void* status_message) @nogc;
1105 * Set the client's user status.
1107 * @param status One of the user statuses listed in the enumeration above.
1109 void tox_self_set_status(Tox* tox, TOX_USER_STATUS status) @nogc;
1112 * Returns the client's user status.
1114 TOX_USER_STATUS tox_self_get_status(const(Tox)* tox) @nogc;
1117 /*******************************************************************************
1119 * :: Friend list management
1121 ******************************************************************************/
1124 alias TOX_ERR_FRIEND_ADD = int;
1125 enum : int {
1127 * The function returned successfully.
1129 TOX_ERR_FRIEND_ADD_OK,
1132 * One of the arguments to the function was NULL when it was not expected.
1134 TOX_ERR_FRIEND_ADD_NULL,
1137 * The length of the friend request message exceeded
1138 * TOX_MAX_FRIEND_REQUEST_LENGTH.
1140 TOX_ERR_FRIEND_ADD_TOO_LONG,
1143 * The friend request message was empty. This, and the TOO_LONG code will
1144 * never be returned from tox_friend_add_norequest.
1146 TOX_ERR_FRIEND_ADD_NO_MESSAGE,
1149 * The friend address belongs to the sending client.
1151 TOX_ERR_FRIEND_ADD_OWN_KEY,
1154 * A friend request has already been sent, or the address belongs to a friend
1155 * that is already on the friend list.
1157 TOX_ERR_FRIEND_ADD_ALREADY_SENT,
1160 * The friend address checksum failed.
1162 TOX_ERR_FRIEND_ADD_BAD_CHECKSUM,
1165 * The friend was already there, but the nospam value was different.
1167 TOX_ERR_FRIEND_ADD_SET_NEW_NOSPAM,
1170 * A memory allocation failed when trying to increase the friend list size.
1172 TOX_ERR_FRIEND_ADD_MALLOC,
1177 * Add a friend to the friend list and send a friend request.
1179 * A friend request message must be at least 1 byte long and at most
1180 * TOX_MAX_FRIEND_REQUEST_LENGTH.
1182 * Friend numbers are unique identifiers used in all functions that operate on
1183 * friends. Once added, a friend number is stable for the lifetime of the Tox
1184 * object. After saving the state and reloading it, the friend numbers may not
1185 * be the same as before. Deleting a friend creates a gap in the friend number
1186 * set, which is filled by the next adding of a friend. Any pattern in friend
1187 * numbers should not be relied on.
1189 * If more than INT32_MAX friends are added, this function causes undefined
1190 * behaviour.
1192 * @param address The address of the friend (returned by tox_self_get_address of
1193 * the friend you wish to add) it must be TOX_ADDRESS_SIZE bytes.
1194 * @param message The message that will be sent along with the friend request.
1195 * @param length The length of the data byte array.
1197 * @return the friend number on success, an unspecified value on failure.
1199 uint tox_friend_add(Tox* tox, const(void)* address, const(void)* message, usize length, TOX_ERR_FRIEND_ADD* error/*=null*/) @nogc;
1202 * Add a friend without sending a friend request.
1204 * This function is used to add a friend in response to a friend request. If the
1205 * client receives a friend request, it can be reasonably sure that the other
1206 * client added this client as a friend, eliminating the need for a friend
1207 * request.
1209 * This function is also useful in a situation where both instances are
1210 * controlled by the same entity, so that this entity can perform the mutual
1211 * friend adding. In this case, there is no need for a friend request, either.
1213 * @param public_key A byte array of length TOX_PUBLIC_KEY_SIZE containing the
1214 * Public Key (not the Address) of the friend to add.
1216 * @return the friend number on success, an unspecified value on failure.
1217 * @see tox_friend_add for a more detailed description of friend numbers.
1219 uint tox_friend_add_norequest(Tox* tox, const(void)* public_key, TOX_ERR_FRIEND_ADD* error/*=null*/) @nogc;
1222 alias TOX_ERR_FRIEND_DELETE = int;
1223 enum : int {
1225 * The function returned successfully.
1227 TOX_ERR_FRIEND_DELETE_OK,
1230 * There was no friend with the given friend number. No friends were deleted.
1232 TOX_ERR_FRIEND_DELETE_FRIEND_NOT_FOUND,
1237 * Remove a friend from the friend list.
1239 * This does not notify the friend of their deletion. After calling this
1240 * function, this client will appear offline to the friend and no communication
1241 * can occur between the two.
1243 * @param friend_number Friend number for the friend to be deleted.
1245 * @return true on success.
1247 bool tox_friend_delete(Tox* tox, uint friend_number, TOX_ERR_FRIEND_DELETE* error/*=null*/) @nogc;
1250 /*******************************************************************************
1252 * :: Friend list queries
1254 ******************************************************************************/
1257 alias TOX_ERR_FRIEND_BY_PUBLIC_KEY = int;
1258 enum : int {
1260 * The function returned successfully.
1262 TOX_ERR_FRIEND_BY_PUBLIC_KEY_OK,
1265 * One of the arguments to the function was NULL when it was not expected.
1267 TOX_ERR_FRIEND_BY_PUBLIC_KEY_NULL,
1270 * No friend with the given Public Key exists on the friend list.
1272 TOX_ERR_FRIEND_BY_PUBLIC_KEY_NOT_FOUND,
1277 * Return the friend number associated with that Public Key.
1279 * @return the friend number on success, an unspecified value on failure.
1280 * @param public_key A byte array containing the Public Key.
1282 uint tox_friend_by_public_key(const(Tox)* tox, const(void)* public_key, TOX_ERR_FRIEND_BY_PUBLIC_KEY* error/*=null*/) @nogc;
1285 * Checks if a friend with the given friend number exists and returns true if
1286 * it does.
1288 bool tox_friend_exists(const(Tox)* tox, uint friend_number) @nogc;
1291 * Return the number of friends on the friend list.
1293 * This function can be used to determine how much memory to allocate for
1294 * tox_self_get_friend_list.
1296 usize tox_self_get_friend_list_size(const(Tox)* tox) @nogc;
1299 * Copy a list of valid friend numbers into an array.
1301 * Call tox_self_get_friend_list_size to determine the number of elements to allocate.
1303 * @param friend_list A memory region with enough space to hold the friend
1304 * list. If this parameter is NULL, this function has no effect.
1306 void tox_self_get_friend_list(const(Tox)* tox, uint *friend_list) @nogc;
1309 alias TOX_ERR_FRIEND_GET_PUBLIC_KEY = int;
1310 enum : int {
1312 * The function returned successfully.
1314 TOX_ERR_FRIEND_GET_PUBLIC_KEY_OK,
1317 * No friend with the given number exists on the friend list.
1319 TOX_ERR_FRIEND_GET_PUBLIC_KEY_FRIEND_NOT_FOUND,
1324 * Copies the Public Key associated with a given friend number to a byte array.
1326 * @param friend_number The friend number you want the Public Key of.
1327 * @param public_key A memory region of at least TOX_PUBLIC_KEY_SIZE bytes. If
1328 * this parameter is NULL, this function has no effect.
1330 * @return true on success.
1332 bool tox_friend_get_public_key(const(Tox)* tox, uint friend_number, void* public_key, TOX_ERR_FRIEND_GET_PUBLIC_KEY* error/*=null*/) @nogc;
1335 alias TOX_ERR_FRIEND_GET_LAST_ONLINE = int;
1336 enum : int {
1338 * The function returned successfully.
1340 TOX_ERR_FRIEND_GET_LAST_ONLINE_OK,
1343 * No friend with the given number exists on the friend list.
1345 TOX_ERR_FRIEND_GET_LAST_ONLINE_FRIEND_NOT_FOUND,
1350 * Return a unix-time timestamp of the last time the friend associated with a given
1351 * friend number was seen online. This function will return UINT64_MAX on error.
1353 * @param friend_number The friend number you want to query.
1355 ulong tox_friend_get_last_online(const(Tox)* tox, uint friend_number, TOX_ERR_FRIEND_GET_LAST_ONLINE* error/*=null*/) @nogc;
1358 /*******************************************************************************
1360 * :: Friend-specific state queries (can also be received through callbacks)
1362 ******************************************************************************/
1366 * Common error codes for friend state query functions.
1368 alias TOX_ERR_FRIEND_QUERY = int;
1369 enum : int {
1371 * The function returned successfully.
1373 TOX_ERR_FRIEND_QUERY_OK,
1376 * The pointer parameter for storing the query result (name, message) was
1377 * NULL. Unlike the `_self_` variants of these functions, which have no effect
1378 * when a parameter is NULL, these functions return an error in that case.
1380 TOX_ERR_FRIEND_QUERY_NULL,
1383 * The friend_number did not designate a valid friend.
1385 TOX_ERR_FRIEND_QUERY_FRIEND_NOT_FOUND,
1390 * Return the length of the friend's name. If the friend number is invalid, the
1391 * return value is unspecified.
1393 * The return value is equal to the `length` argument received by the last
1394 * `friend_name` callback.
1396 usize tox_friend_get_name_size(const(Tox)* tox, uint friend_number, TOX_ERR_FRIEND_QUERY* error/*=null*/) @nogc;
1399 * Write the name of the friend designated by the given friend number to a byte
1400 * array.
1402 * Call tox_friend_get_name_size to determine the allocation size for the `name`
1403 * parameter.
1405 * The data written to `name` is equal to the data received by the last
1406 * `friend_name` callback.
1408 * @param name A valid memory region large enough to store the friend's name.
1410 * @return true on success.
1412 bool tox_friend_get_name(const(Tox)* tox, uint friend_number, void* name, TOX_ERR_FRIEND_QUERY* error/*=null*/) @nogc;
1415 * @param friend_number The friend number of the friend whose name changed.
1416 * @param name A byte array containing the same data as
1417 * tox_friend_get_name would write to its `name` parameter.
1418 * @param length A value equal to the return value of
1419 * tox_friend_get_name_size.
1421 alias tox_friend_name_cb = void function (Tox* tox, uint friend_number, const(char)* name, usize length, void* user_data) nothrow @system;
1425 * Set the callback for the `friend_name` event. Pass NULL to unset.
1427 * This event is triggered when a friend changes their name.
1429 void tox_callback_friend_name(Tox* tox, tox_friend_name_cb callback) @nogc;
1432 * Return the length of the friend's status message. If the friend number is
1433 * invalid, the return value is SIZE_MAX.
1435 usize tox_friend_get_status_message_size(const(Tox)* tox, uint friend_number, TOX_ERR_FRIEND_QUERY* error/*=null*/) @nogc;
1438 * Write the status message of the friend designated by the given friend number to a byte
1439 * array.
1441 * Call tox_friend_get_status_message_size to determine the allocation size for the `status_message`
1442 * parameter.
1444 * The data written to `status_message` is equal to the data received by the last
1445 * `friend_status_message` callback.
1447 * @param status_message A valid memory region large enough to store the friend's status message.
1449 bool tox_friend_get_status_message(const(Tox)* tox, uint friend_number, void* status_message, TOX_ERR_FRIEND_QUERY* error/*=null*/) @nogc;
1452 * @param friend_number The friend number of the friend whose status message
1453 * changed.
1454 * @param message A byte array containing the same data as
1455 * tox_friend_get_status_message would write to its `status_message` parameter.
1456 * @param length A value equal to the return value of
1457 * tox_friend_get_status_message_size.
1459 alias tox_friend_status_message_cb = void function (Tox* tox, uint friend_number, const(char)* message, usize length, void* user_data) nothrow @system;
1463 * Set the callback for the `friend_status_message` event. Pass NULL to unset.
1465 * This event is triggered when a friend changes their status message.
1467 void tox_callback_friend_status_message(Tox* tox, tox_friend_status_message_cb callback) @nogc;
1470 * Return the friend's user status (away/busy/...). If the friend number is
1471 * invalid, the return value is unspecified.
1473 * The status returned is equal to the last status received through the
1474 * `friend_status` callback.
1476 * @deprecated This getter is deprecated. Use the event and store the status
1477 * in the client state.
1479 deprecated TOX_USER_STATUS tox_friend_get_status(const(Tox)* tox, uint friend_number, TOX_ERR_FRIEND_QUERY* error/*=null*/) @nogc;
1482 * @param friend_number The friend number of the friend whose user status
1483 * changed.
1484 * @param status The new user status.
1486 alias tox_friend_status_cb = void function (Tox* tox, uint friend_number, TOX_USER_STATUS status, void* user_data) nothrow @system;
1490 * Set the callback for the `friend_status` event. Pass NULL to unset.
1492 * This event is triggered when a friend changes their user status.
1494 void tox_callback_friend_status(Tox* tox, tox_friend_status_cb callback) @nogc;
1497 * Check whether a friend is currently connected to this client.
1499 * The result of this function is equal to the last value received by the
1500 * `friend_connection_status` callback.
1502 * @param friend_number The friend number for which to query the connection
1503 * status.
1505 * @return the friend's connection status as it was received through the
1506 * `friend_connection_status` event.
1508 * @deprecated This getter is deprecated. Use the event and store the status
1509 * in the client state.
1511 deprecated TOX_CONNECTION tox_friend_get_connection_status(const(Tox)* tox, uint friend_number, TOX_ERR_FRIEND_QUERY* error/*=null*/) @nogc;
1514 * @param friend_number The friend number of the friend whose connection status
1515 * changed.
1516 * @param connection_status The result of calling
1517 * tox_friend_get_connection_status on the passed friend_number.
1519 alias tox_friend_connection_status_cb = void function (Tox* tox, uint friend_number, TOX_CONNECTION connection_status, void* user_data) nothrow @system;
1523 * Set the callback for the `friend_connection_status` event. Pass NULL to unset.
1525 * This event is triggered when a friend goes offline after having been online,
1526 * or when a friend goes online.
1528 * This callback is not called when adding friends. It is assumed that when
1529 * adding friends, their connection status is initially offline.
1531 void tox_callback_friend_connection_status(Tox* tox, tox_friend_connection_status_cb callback) @nogc;
1534 * Check whether a friend is currently typing a message.
1536 * @param friend_number The friend number for which to query the typing status.
1538 * @return true if the friend is typing.
1539 * @return false if the friend is not typing, or the friend number was
1540 * invalid. Inspect the error code to determine which case it is.
1542 * @deprecated This getter is deprecated. Use the event and store the status
1543 * in the client state.
1545 deprecated bool tox_friend_get_typing(const(Tox)* tox, uint friend_number, TOX_ERR_FRIEND_QUERY* error/*=null*/) @nogc;
1548 * @param friend_number The friend number of the friend who started or stopped
1549 * typing.
1550 * @param is_typing The result of calling tox_friend_get_typing on the passed
1551 * friend_number.
1553 alias tox_friend_typing_cb = void function (Tox* tox, uint friend_number, bool is_typing, void* user_data) nothrow @system;
1557 * Set the callback for the `friend_typing` event. Pass NULL to unset.
1559 * This event is triggered when a friend starts or stops typing.
1561 void tox_callback_friend_typing(Tox* tox, tox_friend_typing_cb callback) @nogc;
1564 /*******************************************************************************
1566 * :: Sending private messages
1568 ******************************************************************************/
1571 alias TOX_ERR_SET_TYPING = int;
1572 enum : int {
1574 * The function returned successfully.
1576 TOX_ERR_SET_TYPING_OK,
1579 * The friend number did not designate a valid friend.
1581 TOX_ERR_SET_TYPING_FRIEND_NOT_FOUND,
1586 * Set the client's typing status for a friend.
1588 * The client is responsible for turning it on or off.
1590 * @param friend_number The friend to which the client is typing a message.
1591 * @param typing The typing status. True means the client is typing.
1593 * @return true on success.
1595 bool tox_self_set_typing(Tox* tox, uint friend_number, bool typing, TOX_ERR_SET_TYPING* error/*=null*/) @nogc;
1598 alias TOX_ERR_FRIEND_SEND_MESSAGE = int;
1599 enum : int {
1601 * The function returned successfully.
1603 TOX_ERR_FRIEND_SEND_MESSAGE_OK,
1606 * One of the arguments to the function was NULL when it was not expected.
1608 TOX_ERR_FRIEND_SEND_MESSAGE_NULL,
1611 * The friend number did not designate a valid friend.
1613 TOX_ERR_FRIEND_SEND_MESSAGE_FRIEND_NOT_FOUND,
1616 * This client is currently not connected to the friend.
1618 TOX_ERR_FRIEND_SEND_MESSAGE_FRIEND_NOT_CONNECTED,
1621 * An allocation error occurred while increasing the send queue size.
1623 TOX_ERR_FRIEND_SEND_MESSAGE_SENDQ,
1626 * Message length exceeded TOX_MAX_MESSAGE_LENGTH.
1628 TOX_ERR_FRIEND_SEND_MESSAGE_TOO_LONG,
1631 * Attempted to send a zero-length message.
1633 TOX_ERR_FRIEND_SEND_MESSAGE_EMPTY,
1638 * Send a text chat message to an online friend.
1640 * This function creates a chat message packet and pushes it into the send
1641 * queue.
1643 * The message length may not exceed TOX_MAX_MESSAGE_LENGTH. Larger messages
1644 * must be split by the client and sent as separate messages. Other clients can
1645 * then reassemble the fragments. Messages may not be empty.
1647 * The return value of this function is the message ID. If a read receipt is
1648 * received, the triggered `friend_read_receipt` event will be passed this message ID.
1650 * Message IDs are unique per friend. The first message ID is 0. Message IDs are
1651 * incremented by 1 each time a message is sent. If UINT32_MAX messages were
1652 * sent, the next message ID is 0.
1654 * @param type Message type (normal, action, ...).
1655 * @param friend_number The friend number of the friend to send the message to.
1656 * @param message A non-NULL pointer to the first element of a byte array
1657 * containing the message text.
1658 * @param length Length of the message to be sent.
1660 uint tox_friend_send_message(Tox* tox, uint friend_number, TOX_MESSAGE_TYPE type, const(void)* message, usize length, TOX_ERR_FRIEND_SEND_MESSAGE* error/*=null*/) @nogc;
1663 * @param friend_number The friend number of the friend who received the message.
1664 * @param message_id The message ID as returned from tox_friend_send_message
1665 * corresponding to the message sent.
1667 alias tox_friend_read_receipt_cb = void function (Tox* tox, uint friend_number, uint message_id, void* user_data) nothrow @system;
1671 * Set the callback for the `friend_read_receipt` event. Pass NULL to unset.
1673 * This event is triggered when the friend receives the message sent with
1674 * tox_friend_send_message with the corresponding message ID.
1676 void tox_callback_friend_read_receipt(Tox* tox, tox_friend_read_receipt_cb callback) @nogc;
1679 /*******************************************************************************
1681 * :: Receiving private messages and friend requests
1683 ******************************************************************************/
1688 * @param public_key The Public Key of the user who sent the friend request.
1689 * @param message The message they sent along with the request.
1690 * @param length The size of the message byte array.
1692 alias tox_friend_request_cb = void function (Tox* tox, const(ubyte)* public_key, const(char)* message, usize length, void* user_data) nothrow @system;
1696 * Set the callback for the `friend_request` event. Pass NULL to unset.
1698 * This event is triggered when a friend request is received.
1700 void tox_callback_friend_request(Tox* tox, tox_friend_request_cb callback) @nogc;
1703 * @param friend_number The friend number of the friend who sent the message.
1704 * @param message The message data they sent.
1705 * @param length The size of the message byte array.
1707 alias tox_friend_message_cb = void function (Tox* tox, uint friend_number, TOX_MESSAGE_TYPE type, const(char)* message, usize length, void* user_data) nothrow @system;
1711 * Set the callback for the `friend_message` event. Pass NULL to unset.
1713 * This event is triggered when a message from a friend is received.
1715 void tox_callback_friend_message(Tox* tox, tox_friend_message_cb callback) @nogc;
1718 /*******************************************************************************
1720 * :: File transmission: common between sending and receiving
1722 ******************************************************************************/
1727 * Generates a cryptographic hash of the given data.
1729 * This function may be used by clients for any purpose, but is provided
1730 * primarily for validating cached avatars. This use is highly recommended to
1731 * avoid unnecessary avatar updates.
1733 * If hash is NULL or data is NULL while length is not 0 the function returns false,
1734 * otherwise it returns true.
1736 * This function is a wrapper to internal message-digest functions.
1738 * @param hash A valid memory location the hash data. It must be at least
1739 * TOX_HASH_LENGTH bytes in size.
1740 * @param data Data to be hashed or NULL.
1741 * @param length Size of the data array or 0.
1743 * @return true if hash was not NULL.
1745 bool tox_hash(void* hash, const(void)* data, usize length) @nogc;
1749 * A list of pre-defined file kinds. Toxcore itself does not behave
1750 * differently for different file kinds. These are a hint to the client
1751 * telling it what use the sender intended for the file. The `kind` parameter
1752 * in the send function and recv callback are `uint`, not TOX_FILE_KIND, because
1753 * clients can invent their own file kind. Unknown file kinds should be
1754 * treated as TOX_FILE_KIND_DATA.
1756 alias TOX_FILE_KIND = uint;
1757 enum : uint {
1759 * Arbitrary file data. Clients can choose to handle it based on the file name
1760 * or magic or any other way they choose.
1762 TOX_FILE_KIND_DATA,
1765 * Avatar file_id. This consists of tox_hash(image).
1766 * Avatar data. This consists of the image data.
1768 * Avatars can be sent at any time the client wishes. Generally, a client will
1769 * send the avatar to a friend when that friend comes online, and to all
1770 * friends when the avatar changed. A client can save some traffic by
1771 * remembering which friend received the updated avatar already and only send
1772 * it if the friend has an out of date avatar.
1774 * Clients who receive avatar send requests can reject it (by sending
1775 * TOX_FILE_CONTROL_CANCEL before any other controls), or accept it (by
1776 * sending TOX_FILE_CONTROL_RESUME). The file_id of length TOX_HASH_LENGTH bytes
1777 * (same length as TOX_FILE_ID_LENGTH) will contain the hash. A client can compare
1778 * this hash with a saved hash and send TOX_FILE_CONTROL_CANCEL to terminate the avatar
1779 * transfer if it matches.
1781 * When file_size is set to 0 in the transfer request it means that the client
1782 * has no avatar.
1784 TOX_FILE_KIND_AVATAR,
1788 alias TOX_FILE_CONTROL = int;
1789 enum : int {
1791 * Sent by the receiving side to accept a file send request. Also sent after a
1792 * TOX_FILE_CONTROL_PAUSE command to continue sending or receiving.
1794 TOX_FILE_CONTROL_RESUME,
1797 * Sent by clients to pause the file transfer. The initial state of a file
1798 * transfer is always paused on the receiving side and running on the sending
1799 * side. If both the sending and receiving side pause the transfer, then both
1800 * need to send TOX_FILE_CONTROL_RESUME for the transfer to resume.
1802 TOX_FILE_CONTROL_PAUSE,
1805 * Sent by the receiving side to reject a file send request before any other
1806 * commands are sent. Also sent by either side to terminate a file transfer.
1808 TOX_FILE_CONTROL_CANCEL,
1812 alias TOX_ERR_FILE_CONTROL = int;
1813 enum : int {
1815 * The function returned successfully.
1817 TOX_ERR_FILE_CONTROL_OK,
1820 * The friend_number passed did not designate a valid friend.
1822 TOX_ERR_FILE_CONTROL_FRIEND_NOT_FOUND,
1825 * This client is currently not connected to the friend.
1827 TOX_ERR_FILE_CONTROL_FRIEND_NOT_CONNECTED,
1830 * No file transfer with the given file number was found for the given friend.
1832 TOX_ERR_FILE_CONTROL_NOT_FOUND,
1835 * A RESUME control was sent, but the file transfer is running normally.
1837 TOX_ERR_FILE_CONTROL_NOT_PAUSED,
1840 * A RESUME control was sent, but the file transfer was paused by the other
1841 * party. Only the party that paused the transfer can resume it.
1843 TOX_ERR_FILE_CONTROL_DENIED,
1846 * A PAUSE control was sent, but the file transfer was already paused.
1848 TOX_ERR_FILE_CONTROL_ALREADY_PAUSED,
1851 * Packet queue is full.
1853 TOX_ERR_FILE_CONTROL_SENDQ,
1858 * Sends a file control command to a friend for a given file transfer.
1860 * @param friend_number The friend number of the friend the file is being
1861 * transferred to or received from.
1862 * @param file_number The friend-specific identifier for the file transfer.
1863 * @param control The control command to send.
1865 * @return true on success.
1867 bool tox_file_control(Tox* tox, uint friend_number, uint file_number, TOX_FILE_CONTROL control, TOX_ERR_FILE_CONTROL* error/*=null*/) @nogc;
1870 * When receiving TOX_FILE_CONTROL_CANCEL, the client should release the
1871 * resources associated with the file number and consider the transfer failed.
1873 * @param friend_number The friend number of the friend who is sending the file.
1874 * @param file_number The friend-specific file number the data received is
1875 * associated with.
1876 * @param control The file control command received.
1878 alias tox_file_recv_control_cb = void function (Tox* tox, uint friend_number, uint file_number, TOX_FILE_CONTROL control, void* user_data) nothrow @system;
1882 * Set the callback for the `file_recv_control` event. Pass NULL to unset.
1884 * This event is triggered when a file control command is received from a
1885 * friend.
1887 void tox_callback_file_recv_control(Tox* tox, tox_file_recv_control_cb callback) @nogc;
1890 alias TOX_ERR_FILE_SEEK = int;
1891 enum : int {
1893 * The function returned successfully.
1895 TOX_ERR_FILE_SEEK_OK,
1898 * The friend_number passed did not designate a valid friend.
1900 TOX_ERR_FILE_SEEK_FRIEND_NOT_FOUND,
1903 * This client is currently not connected to the friend.
1905 TOX_ERR_FILE_SEEK_FRIEND_NOT_CONNECTED,
1908 * No file transfer with the given file number was found for the given friend.
1910 TOX_ERR_FILE_SEEK_NOT_FOUND,
1913 * File was not in a state where it could be seeked.
1915 TOX_ERR_FILE_SEEK_DENIED,
1918 * Seek position was invalid
1920 TOX_ERR_FILE_SEEK_INVALID_POSITION,
1923 * Packet queue is full.
1925 TOX_ERR_FILE_SEEK_SENDQ,
1930 * Sends a file seek control command to a friend for a given file transfer.
1932 * This function can only be called to resume a file transfer right before
1933 * TOX_FILE_CONTROL_RESUME is sent.
1935 * @param friend_number The friend number of the friend the file is being
1936 * received from.
1937 * @param file_number The friend-specific identifier for the file transfer.
1938 * @param position The position that the file should be seeked to.
1940 bool tox_file_seek(Tox* tox, uint friend_number, uint file_number, ulong position, TOX_ERR_FILE_SEEK* error/*=null*/) @nogc;
1943 alias TOX_ERR_FILE_GET = int;
1944 enum : int {
1946 * The function returned successfully.
1948 TOX_ERR_FILE_GET_OK,
1951 * One of the arguments to the function was NULL when it was not expected.
1953 TOX_ERR_FILE_GET_NULL,
1956 * The friend_number passed did not designate a valid friend.
1958 TOX_ERR_FILE_GET_FRIEND_NOT_FOUND,
1961 * No file transfer with the given file number was found for the given friend.
1963 TOX_ERR_FILE_GET_NOT_FOUND,
1968 * Copy the file id associated to the file transfer to a byte array.
1970 * @param friend_number The friend number of the friend the file is being
1971 * transferred to or received from.
1972 * @param file_number The friend-specific identifier for the file transfer.
1973 * @param file_id A memory region of at least TOX_FILE_ID_LENGTH bytes. If
1974 * this parameter is NULL, this function has no effect.
1976 * @return true on success.
1978 bool tox_file_get_file_id(const(Tox)* tox, uint friend_number, uint file_number, void* file_id, TOX_ERR_FILE_GET* error/*=null*/) @nogc;
1981 /*******************************************************************************
1983 * :: File transmission: sending
1985 ******************************************************************************/
1988 alias TOX_ERR_FILE_SEND = int;
1989 enum : int {
1991 * The function returned successfully.
1993 TOX_ERR_FILE_SEND_OK,
1996 * One of the arguments to the function was NULL when it was not expected.
1998 TOX_ERR_FILE_SEND_NULL,
2001 * The friend_number passed did not designate a valid friend.
2003 TOX_ERR_FILE_SEND_FRIEND_NOT_FOUND,
2006 * This client is currently not connected to the friend.
2008 TOX_ERR_FILE_SEND_FRIEND_NOT_CONNECTED,
2011 * Filename length exceeded TOX_MAX_FILENAME_LENGTH bytes.
2013 TOX_ERR_FILE_SEND_NAME_TOO_LONG,
2016 * Too many ongoing transfers. The maximum number of concurrent file transfers
2017 * is 256 per friend per direction (sending and receiving).
2019 TOX_ERR_FILE_SEND_TOO_MANY,
2024 * Send a file transmission request.
2026 * Maximum filename length is TOX_MAX_FILENAME_LENGTH bytes. The filename
2027 * should generally just be a file name, not a path with directory names.
2029 * If a non-UINT64_MAX file size is provided, it can be used by both sides to
2030 * determine the sending progress. File size can be set to UINT64_MAX for streaming
2031 * data of unknown size.
2033 * File transmission occurs in chunks, which are requested through the
2034 * `file_chunk_request` event.
2036 * When a friend goes offline, all file transfers associated with the friend are
2037 * purged from core.
2039 * If the file contents change during a transfer, the behaviour is unspecified
2040 * in general. What will actually happen depends on the mode in which the file
2041 * was modified and how the client determines the file size.
2043 * - If the file size was increased
2044 * - and sending mode was streaming (file_size = UINT64_MAX), the behaviour
2045 * will be as expected.
2046 * - and sending mode was file (file_size != UINT64_MAX), the
2047 * file_chunk_request callback will receive length = 0 when Core thinks
2048 * the file transfer has finished. If the client remembers the file size as
2049 * it was when sending the request, it will terminate the transfer normally.
2050 * If the client re-reads the size, it will think the friend cancelled the
2051 * transfer.
2052 * - If the file size was decreased
2053 * - and sending mode was streaming, the behaviour is as expected.
2054 * - and sending mode was file, the callback will return 0 at the new
2055 * (earlier) end-of-file, signalling to the friend that the transfer was
2056 * cancelled.
2057 * - If the file contents were modified
2058 * - at a position before the current read, the two files (local and remote)
2059 * will differ after the transfer terminates.
2060 * - at a position after the current read, the file transfer will succeed as
2061 * expected.
2062 * - In either case, both sides will regard the transfer as complete and
2063 * successful.
2065 * @param friend_number The friend number of the friend the file send request
2066 * should be sent to.
2067 * @param kind The meaning of the file to be sent.
2068 * @param file_size Size in bytes of the file the client wants to send, UINT64_MAX if
2069 * unknown or streaming.
2070 * @param file_id A file identifier of length TOX_FILE_ID_LENGTH that can be used to
2071 * uniquely identify file transfers across core restarts. If NULL, a random one will
2072 * be generated by core. It can then be obtained by using tox_file_get_file_id().
2073 * @param filename Name of the file. Does not need to be the actual name. This
2074 * name will be sent along with the file send request.
2075 * @param filename_length Size in bytes of the filename.
2077 * @return A file number used as an identifier in subsequent callbacks. This
2078 * number is per friend. File numbers are reused after a transfer terminates.
2079 * On failure, this function returns an unspecified value. Any pattern in file numbers
2080 * should not be relied on.
2082 uint tox_file_send(Tox* tox, uint friend_number, uint kind, ulong file_size, const(void)* file_id, const(void)* filename, usize filename_length, TOX_ERR_FILE_SEND* error/*=null*/) @nogc;
2085 alias TOX_ERR_FILE_SEND_CHUNK = int;
2086 enum : int {
2088 * The function returned successfully.
2090 TOX_ERR_FILE_SEND_CHUNK_OK,
2093 * The length parameter was non-zero, but data was NULL.
2095 TOX_ERR_FILE_SEND_CHUNK_NULL,
2098 * The friend_number passed did not designate a valid friend.
2100 TOX_ERR_FILE_SEND_CHUNK_FRIEND_NOT_FOUND,
2103 * This client is currently not connected to the friend.
2105 TOX_ERR_FILE_SEND_CHUNK_FRIEND_NOT_CONNECTED,
2108 * No file transfer with the given file number was found for the given friend.
2110 TOX_ERR_FILE_SEND_CHUNK_NOT_FOUND,
2113 * File transfer was found but isn't in a transferring state: (paused, done,
2114 * broken, etc...) (happens only when not called from the request chunk callback).
2116 TOX_ERR_FILE_SEND_CHUNK_NOT_TRANSFERRING,
2119 * Attempted to send more or less data than requested. The requested data size is
2120 * adjusted according to maximum transmission unit and the expected end of
2121 * the file. Trying to send less or more than requested will return this error.
2123 TOX_ERR_FILE_SEND_CHUNK_INVALID_LENGTH,
2126 * Packet queue is full.
2128 TOX_ERR_FILE_SEND_CHUNK_SENDQ,
2131 * Position parameter was wrong.
2133 TOX_ERR_FILE_SEND_CHUNK_WRONG_POSITION,
2138 * Send a chunk of file data to a friend.
2140 * This function is called in response to the `file_chunk_request` callback. The
2141 * length parameter should be equal to the one received though the callback.
2142 * If it is zero, the transfer is assumed complete. For files with known size,
2143 * Core will know that the transfer is complete after the last byte has been
2144 * received, so it is not necessary (though not harmful) to send a zero-length
2145 * chunk to terminate. For streams, core will know that the transfer is finished
2146 * if a chunk with length less than the length requested in the callback is sent.
2148 * @param friend_number The friend number of the receiving friend for this file.
2149 * @param file_number The file transfer identifier returned by tox_file_send.
2150 * @param position The file or stream position from which to continue reading.
2151 * @return true on success.
2153 bool tox_file_send_chunk(Tox* tox, uint friend_number, uint file_number, ulong position, const(void)* data, usize length, TOX_ERR_FILE_SEND_CHUNK* error/*=null*/) @nogc;
2156 * If the length parameter is 0, the file transfer is finished, and the client's
2157 * resources associated with the file number should be released. After a call
2158 * with zero length, the file number can be reused for future file transfers.
2160 * If the requested position is not equal to the client's idea of the current
2161 * file or stream position, it will need to seek. In case of read-once streams,
2162 * the client should keep the last read chunk so that a seek back can be
2163 * supported. A seek-back only ever needs to read from the last requested chunk.
2164 * This happens when a chunk was requested, but the send failed. A seek-back
2165 * request can occur an arbitrary number of times for any given chunk.
2167 * In response to receiving this callback, the client should call the function
2168 * `tox_file_send_chunk` with the requested chunk. If the number of bytes sent
2169 * through that function is zero, the file transfer is assumed complete. A
2170 * client must send the full length of data requested with this callback.
2172 * @param friend_number The friend number of the receiving friend for this file.
2173 * @param file_number The file transfer identifier returned by tox_file_send.
2174 * @param position The file or stream position from which to continue reading.
2175 * @param length The number of bytes requested for the current chunk.
2177 alias tox_file_chunk_request_cb = void function (Tox* tox, uint friend_number, uint file_number, ulong position, usize length, void* user_data) nothrow @system;
2181 * Set the callback for the `file_chunk_request` event. Pass NULL to unset.
2183 * This event is triggered when Core is ready to send more file data.
2185 void tox_callback_file_chunk_request(Tox* tox, tox_file_chunk_request_cb callback) @nogc;
2188 /*******************************************************************************
2190 * :: File transmission: receiving
2192 ******************************************************************************/
2196 * The client should acquire resources to be associated with the file transfer.
2197 * Incoming file transfers start in the PAUSED state. After this callback
2198 * returns, a transfer can be rejected by sending a TOX_FILE_CONTROL_CANCEL
2199 * control command before any other control commands. It can be accepted by
2200 * sending TOX_FILE_CONTROL_RESUME.
2202 * @param friend_number The friend number of the friend who is sending the file
2203 * transfer request.
2204 * @param file_number The friend-specific file number the data received is
2205 * associated with.
2206 * @param kind The meaning of the file that was sent.
2207 * @param file_size Size in bytes of the file the client wants to send,
2208 * UINT64_MAX if unknown or streaming.
2209 * @param filename Name of the file. Does not need to be the actual name. This
2210 * name will be sent along with the file send request.
2211 * @param filename_length Size in bytes of the filename.
2213 alias tox_file_recv_cb = void function (Tox* tox, uint friend_number, uint file_number, uint kind, ulong file_size, const(char)* filename, usize filename_length, void* user_data) nothrow @system;
2217 * Set the callback for the `file_recv` event. Pass NULL to unset.
2219 * This event is triggered when a file transfer request is received.
2221 void tox_callback_file_recv(Tox* tox, tox_file_recv_cb callback) @nogc;
2224 * When length is 0, the transfer is finished and the client should release the
2225 * resources it acquired for the transfer. After a call with length = 0, the
2226 * file number can be reused for new file transfers.
2228 * If position is equal to file_size (received in the file_receive callback)
2229 * when the transfer finishes, the file was received completely. Otherwise, if
2230 * file_size was UINT64_MAX, streaming ended successfully when length is 0.
2232 * @param friend_number The friend number of the friend who is sending the file.
2233 * @param file_number The friend-specific file number the data received is
2234 * associated with.
2235 * @param position The file position of the first byte in data.
2236 * @param data A byte array containing the received chunk.
2237 * @param length The length of the received chunk.
2239 alias tox_file_recv_chunk_cb = void function (Tox* tox, uint friend_number, uint file_number, ulong position, const(void)* data, usize length, void* user_data) nothrow @system;
2243 * Set the callback for the `file_recv_chunk` event. Pass NULL to unset.
2245 * This event is first triggered when a file transfer request is received, and
2246 * subsequently when a chunk of file data for an accepted request was received.
2248 void tox_callback_file_recv_chunk(Tox* tox, tox_file_recv_chunk_cb callback) @nogc;
2251 /*******************************************************************************
2253 * :: Conference management
2255 ******************************************************************************/
2259 * Conference types for the conference_invite event.
2261 alias TOX_CONFERENCE_TYPE = int;
2262 enum : int {
2264 * Text-only conferences that must be accepted with the tox_conference_join function.
2266 TOX_CONFERENCE_TYPE_TEXT,
2269 * Video conference. The function to accept these is in toxav.
2271 TOX_CONFERENCE_TYPE_AV,
2276 * The invitation will remain valid until the inviting friend goes offline
2277 * or exits the conference.
2279 * @param friend_number The friend who invited us.
2280 * @param type The conference type (text only or audio/video).
2281 * @param cookie A piece of data of variable length required to join the
2282 * conference.
2283 * @param length The length of the cookie.
2285 alias tox_conference_invite_cb = void function (Tox* tox, uint friend_number, TOX_CONFERENCE_TYPE type, const(void)* cookie, usize length, void* user_data) nothrow @system;
2289 * Set the callback for the `conference_invite` event. Pass NULL to unset.
2291 * This event is triggered when the client is invited to join a conference.
2293 void tox_callback_conference_invite(Tox* tox, tox_conference_invite_cb callback) @nogc;
2297 * @param conference_number The conference number of the conference to which we have connected.
2299 alias tox_conference_connected_cb = void function (Tox *tox, uint conference_number, void *user_data) nothrow @system;
2303 * Set the callback for the `conference_connected` event. Pass NULL to unset.
2305 * This event is triggered when the client successfully connects to a
2306 * conference after joining it with the tox_conference_join function.
2308 void tox_callback_conference_connected(Tox *tox, tox_conference_connected_cb callback) @nogc;
2312 * @param conference_number The conference number of the conference the message is intended for.
2313 * @param peer_number The ID of the peer who sent the message.
2314 * @param type The type of message (normal, action, ...).
2315 * @param message The message data.
2316 * @param length The length of the message.
2318 alias tox_conference_message_cb = void function (Tox* tox, uint conference_number, uint peer_number, TOX_MESSAGE_TYPE type, const(char)* message, usize length, void* user_data) nothrow @system;
2322 * Set the callback for the `conference_message` event. Pass NULL to unset.
2324 * This event is triggered when the client receives a conference message.
2326 void tox_callback_conference_message(Tox* tox, tox_conference_message_cb callback) @nogc;
2329 * @param conference_number The conference number of the conference the title change is intended for.
2330 * @param peer_number The ID of the peer who changed the title.
2331 * @param title The title data.
2332 * @param length The title length.
2334 alias tox_conference_title_cb = void function (Tox* tox, uint conference_number, uint peer_number, const(char)* title, usize length, void* user_data) nothrow @system;
2338 * Set the callback for the `conference_title` event. Pass NULL to unset.
2340 * This event is triggered when a peer changes the conference title.
2342 * If peer_number == UINT32_MAX, then author is unknown (e.g. initial joining the conference).
2344 void tox_callback_conference_title(Tox* tox, tox_conference_title_cb callback) @nogc;
2348 * @param conference_number The conference number of the conference the
2349 * peer is in.
2350 * @param peer_number The ID of the peer who changed their nickname.
2351 * @param name A byte array containing the new nickname.
2352 * @param length The size of the name byte array.
2354 alias tox_conference_peer_name_cb = void function (Tox* tox, uint conference_number, uint peer_number, const(char)* name, usize length, void* user_data) nothrow @system;
2358 * Set the callback for the `conference_peer_name` event. Pass NULL to unset.
2360 * This event is triggered when a peer changes their name.
2362 void tox_callback_conference_peer_name(Tox* tox, tox_conference_peer_name_cb callback) @nogc;
2365 * @param conference_number The conference number of the conference the
2366 * peer is in.
2368 alias tox_conference_peer_list_changed_cb = void function (Tox* tox, uint conference_number, void* user_data) nothrow @system;
2372 * Set the callback for the `conference_peer_list_changed` event. Pass NULL to unset.
2374 * This event is triggered when a peer joins or leaves the conference.
2376 void tox_callback_conference_peer_list_changed(Tox* tox, tox_conference_peer_list_changed_cb callback) @nogc;
2379 alias TOX_ERR_CONFERENCE_NEW = int;
2380 enum : int {
2382 * The function returned successfully.
2384 TOX_ERR_CONFERENCE_NEW_OK,
2387 * The conference instance failed to initialize.
2389 TOX_ERR_CONFERENCE_NEW_INIT,
2394 * Creates a new conference.
2396 * This function creates a new text conference.
2398 * @return conference number on success, or an unspecified value on failure.
2400 uint tox_conference_new(Tox* tox, TOX_ERR_CONFERENCE_NEW* error/*=null*/) @nogc;
2403 alias TOX_ERR_CONFERENCE_DELETE = int;
2404 enum : int {
2406 * The function returned successfully.
2408 TOX_ERR_CONFERENCE_DELETE_OK,
2411 * The conference number passed did not designate a valid conference.
2413 TOX_ERR_CONFERENCE_DELETE_CONFERENCE_NOT_FOUND,
2418 * This function deletes a conference.
2420 * @param conference_number The conference number of the conference to be deleted.
2422 * @return true on success.
2424 bool tox_conference_delete(Tox* tox, uint conference_number, TOX_ERR_CONFERENCE_DELETE* error/*=null*/) @nogc;
2428 * Error codes for peer info queries.
2430 alias TOX_ERR_CONFERENCE_PEER_QUERY = int;
2431 enum : int {
2433 * The function returned successfully.
2435 TOX_ERR_CONFERENCE_PEER_QUERY_OK,
2438 * The conference number passed did not designate a valid conference.
2440 TOX_ERR_CONFERENCE_PEER_QUERY_CONFERENCE_NOT_FOUND,
2443 * The peer number passed did not designate a valid peer.
2445 TOX_ERR_CONFERENCE_PEER_QUERY_PEER_NOT_FOUND,
2448 * The client is not connected to the conference.
2450 TOX_ERR_CONFERENCE_PEER_QUERY_NO_CONNECTION,
2455 * Return the number of peers in the conference. Return value is unspecified on failure.
2457 uint tox_conference_peer_count(const(Tox)* tox, uint conference_number, TOX_ERR_CONFERENCE_PEER_QUERY* error/*=null*/) @nogc;
2460 * Return the length of the peer's name. Return value is unspecified on failure.
2462 usize tox_conference_peer_get_name_size(const(Tox)* tox, uint conference_number, uint peer_number, TOX_ERR_CONFERENCE_PEER_QUERY* error/*=null*/) @nogc;
2465 * Copy the name of peer_number who is in conference_number to name.
2467 * Call tox_conference_peer_get_name_size to determine the allocation size for the `name` parameter.
2469 * @param name A valid memory region large enough to store the peer's name.
2471 * @return true on success.
2473 bool tox_conference_peer_get_name(const(Tox)* tox, uint conference_number, uint peer_number, void* name, TOX_ERR_CONFERENCE_PEER_QUERY* error/*=null*/) @nogc;
2476 * Copy the public key of peer_number who is in conference_number to public_key.
2477 * public_key must be TOX_PUBLIC_KEY_SIZE long.
2479 * @return true on success.
2481 bool tox_conference_peer_get_public_key(const(Tox)* tox, uint conference_number, uint peer_number, void* public_key, TOX_ERR_CONFERENCE_PEER_QUERY* error/*=null*/) @nogc;
2484 * Return true if passed peer_number corresponds to our own.
2486 bool tox_conference_peer_number_is_ours(const(Tox)* tox, uint conference_number, uint peer_number, TOX_ERR_CONFERENCE_PEER_QUERY* error/*=null*/) @nogc;
2489 * Return the number of offline peers in the conference. Return value is unspecified on failure.
2491 uint tox_conference_offline_peer_count(const(Tox)* tox, uint conference_number, TOX_ERR_CONFERENCE_PEER_QUERY *error);
2494 * Return the length of the offline peer's name. Return value is unspecified on failure.
2496 usize tox_conference_offline_peer_get_name_size(const(Tox)* tox, uint conference_number, uint offline_peer_number, TOX_ERR_CONFERENCE_PEER_QUERY *error) @nogc;
2499 * Copy the name of offline_peer_number who is in conference_number to name.
2501 * Call tox_conference_offline_peer_get_name_size to determine the allocation size for the `name` parameter.
2503 * @param name A valid memory region large enough to store the peer's name.
2505 * @return true on success.
2507 bool tox_conference_offline_peer_get_name(const(Tox)* tox, uint conference_number, uint offline_peer_number, void *name, TOX_ERR_CONFERENCE_PEER_QUERY *error) @nogc;
2510 * Copy the public key of offline_peer_number who is in conference_number to public_key.
2511 * public_key must be TOX_PUBLIC_KEY_SIZE long.
2513 * @return true on success.
2515 bool tox_conference_offline_peer_get_public_key(const(Tox)* tox, uint conference_number, uint offline_peer_number, void *public_key, TOX_ERR_CONFERENCE_PEER_QUERY *error) @nogc;
2518 * Return a unix-time timestamp of the last time offline_peer_number was seen to be active.
2520 ulong tox_conference_offline_peer_get_last_active(const(Tox)* tox, uint conference_number, uint offline_peer_number, TOX_ERR_CONFERENCE_PEER_QUERY *error) @nogc;
2522 alias TOX_ERR_CONFERENCE_SET_MAX_OFFLINE = int;
2523 enum : int {
2525 * The function returned successfully.
2527 TOX_ERR_CONFERENCE_SET_MAX_OFFLINE_OK,
2530 * The conference number passed did not designate a valid conference.
2532 TOX_ERR_CONFERENCE_SET_MAX_OFFLINE_CONFERENCE_NOT_FOUND,
2536 * Set maximum number of offline peers to store, overriding the default.
2538 bool tox_conference_set_max_offline(Tox *tox, uint conference_number, uint max_offline_peers,
2539 TOX_ERR_CONFERENCE_SET_MAX_OFFLINE *error) @nogc;
2542 alias TOX_ERR_CONFERENCE_INVITE = int;
2543 enum : int {
2545 * The function returned successfully.
2547 TOX_ERR_CONFERENCE_INVITE_OK,
2550 * The conference number passed did not designate a valid conference.
2552 TOX_ERR_CONFERENCE_INVITE_CONFERENCE_NOT_FOUND,
2555 * The invite packet failed to send.
2557 TOX_ERR_CONFERENCE_INVITE_FAIL_SEND,
2560 * The client is not connected to the conference.
2562 TOX_ERR_CONFERENCE_INVITE_NO_CONNECTION,
2567 * Invites a friend to a conference.
2569 * @param friend_number The friend number of the friend we want to invite.
2570 * @param conference_number The conference number of the conference we want to invite the friend to.
2572 * @return true on success.
2574 bool tox_conference_invite(Tox* tox, uint friend_number, uint conference_number, TOX_ERR_CONFERENCE_INVITE* error/*=null*/) @nogc;
2577 alias TOX_ERR_CONFERENCE_JOIN = int;
2578 enum : int {
2580 * The function returned successfully.
2582 TOX_ERR_CONFERENCE_JOIN_OK,
2585 * The cookie passed has an invalid length.
2587 TOX_ERR_CONFERENCE_JOIN_INVALID_LENGTH,
2590 * The conference is not the expected type. This indicates an invalid cookie.
2592 TOX_ERR_CONFERENCE_JOIN_WRONG_TYPE,
2595 * The friend number passed does not designate a valid friend.
2597 TOX_ERR_CONFERENCE_JOIN_FRIEND_NOT_FOUND,
2600 * Client is already in this conference.
2602 TOX_ERR_CONFERENCE_JOIN_DUPLICATE,
2605 * Conference instance failed to initialize.
2607 TOX_ERR_CONFERENCE_JOIN_INIT_FAIL,
2610 * The join packet failed to send.
2612 TOX_ERR_CONFERENCE_JOIN_FAIL_SEND,
2617 * Joins a conference that the client has been invited to.
2619 * @param friend_number The friend number of the friend who sent the invite.
2620 * @param cookie Received via the `conference_invite` event.
2621 * @param length The size of cookie.
2623 * @return conference number on success, UINT32_MAX on failure.
2625 uint tox_conference_join(Tox* tox, uint friend_number, const(void)* cookie, usize length, TOX_ERR_CONFERENCE_JOIN* error/*=null*/) @nogc;
2628 alias TOX_ERR_CONFERENCE_SEND_MESSAGE = int;
2629 enum : int {
2631 * The function returned successfully.
2633 TOX_ERR_CONFERENCE_SEND_MESSAGE_OK,
2636 * The conference number passed did not designate a valid conference.
2638 TOX_ERR_CONFERENCE_SEND_MESSAGE_CONFERENCE_NOT_FOUND,
2641 * The message is too long.
2643 TOX_ERR_CONFERENCE_SEND_MESSAGE_TOO_LONG,
2646 * The client is not connected to the conference.
2648 TOX_ERR_CONFERENCE_SEND_MESSAGE_NO_CONNECTION,
2651 * The message packet failed to send.
2653 TOX_ERR_CONFERENCE_SEND_MESSAGE_FAIL_SEND,
2658 * Send a text chat message to the conference.
2660 * This function creates a conference message packet and pushes it into the send
2661 * queue.
2663 * The message length may not exceed TOX_MAX_MESSAGE_LENGTH. Larger messages
2664 * must be split by the client and sent as separate messages. Other clients can
2665 * then reassemble the fragments.
2667 * @param conference_number The conference number of the conference the message is intended for.
2668 * @param type Message type (normal, action, ...).
2669 * @param message A non-NULL pointer to the first element of a byte array
2670 * containing the message text.
2671 * @param length Length of the message to be sent.
2673 * @return true on success.
2675 bool tox_conference_send_message(Tox* tox, uint conference_number, TOX_MESSAGE_TYPE type, const(void)* message, usize length, TOX_ERR_CONFERENCE_SEND_MESSAGE* error/*=null*/) @nogc;
2678 alias TOX_ERR_CONFERENCE_TITLE = int;
2679 enum : int {
2681 * The function returned successfully.
2683 TOX_ERR_CONFERENCE_TITLE_OK,
2686 * The conference number passed did not designate a valid conference.
2688 TOX_ERR_CONFERENCE_TITLE_CONFERENCE_NOT_FOUND,
2691 * The title is too long or empty.
2693 TOX_ERR_CONFERENCE_TITLE_INVALID_LENGTH,
2696 * The title packet failed to send.
2698 TOX_ERR_CONFERENCE_TITLE_FAIL_SEND,
2703 * Return the length of the conference title. Return value is unspecified on failure.
2705 * The return value is equal to the `length` argument received by the last
2706 * `conference_title` callback.
2708 usize tox_conference_get_title_size(const(Tox)* tox, uint conference_number, TOX_ERR_CONFERENCE_TITLE* error/*=null*/) @nogc;
2711 * Write the title designated by the given conference number to a byte array.
2713 * Call tox_conference_get_title_size to determine the allocation size for the `title` parameter.
2715 * The data written to `title` is equal to the data received by the last
2716 * `conference_title` callback.
2718 * @param title A valid memory region large enough to store the title.
2719 * If this parameter is NULL, this function has no effect.
2721 * @return true on success.
2723 bool tox_conference_get_title(const(Tox)* tox, uint conference_number, void* title, TOX_ERR_CONFERENCE_TITLE* error/*=null*/) @nogc;
2726 * Set the conference title and broadcast it to the rest of the conference.
2728 * Title length cannot be longer than TOX_MAX_NAME_LENGTH.
2730 * @return true on success.
2732 bool tox_conference_set_title(Tox* tox, uint conference_number, const(void)* title, usize length, TOX_ERR_CONFERENCE_TITLE* error/*=null*/) @nogc;
2735 * Return the number of conferences in the Tox instance.
2736 * This should be used to determine how much memory to allocate for `tox_conference_get_chatlist`.
2738 usize tox_conference_get_chatlist_size(const(Tox)* tox) @nogc;
2741 * Copy a list of valid conference IDs into the array chatlist. Determine how much space
2742 * to allocate for the array with the `tox_conference_get_chatlist_size` function.
2744 void tox_conference_get_chatlist(const(Tox)* tox, uint *chatlist) @nogc;
2748 * Returns the type of conference (TOX_CONFERENCE_TYPE) that conference_number is. Return value is
2749 * unspecified on failure.
2751 alias TOX_ERR_CONFERENCE_GET_TYPE = int;
2752 enum : int {
2754 * The function returned successfully.
2756 TOX_ERR_CONFERENCE_GET_TYPE_OK,
2759 * The conference number passed did not designate a valid conference.
2761 TOX_ERR_CONFERENCE_GET_TYPE_CONFERENCE_NOT_FOUND,
2765 TOX_CONFERENCE_TYPE tox_conference_get_type(const(Tox)* tox, uint conference_number, TOX_ERR_CONFERENCE_GET_TYPE* error/*=null*/) @nogc;
2768 * Get the conference unique ID.
2770 * If id is NULL, this function has no effect.
2772 * @param id A memory region large enough to store TOX_CONFERENCE_ID_SIZE bytes.
2774 * @return true on success.
2776 bool tox_conference_get_id(const(Tox)* tox, uint conference_number, void *id) @nogc;
2778 alias TOX_ERR_CONFERENCE_BY_ID = int;
2779 enum : int {
2781 * The function returned successfully.
2783 TOX_ERR_CONFERENCE_BY_ID_OK,
2786 * One of the arguments to the function was NULL when it was not expected.
2788 TOX_ERR_CONFERENCE_BY_ID_NULL,
2791 * No conference with the given id exists on the conference list.
2793 TOX_ERR_CONFERENCE_BY_ID_NOT_FOUND,
2799 * Return the conference number associated with the specified id.
2801 * @param id A byte array containing the conference id (TOX_CONFERENCE_ID_SIZE).
2803 * @return the conference number on success, an unspecified value on failure.
2805 uint tox_conference_by_id(const(Tox)* tox, const(void)* id, TOX_ERR_CONFERENCE_BY_ID *error) @nogc;
2808 * Get the conference unique ID.
2810 * If uid is NULL, this function has no effect.
2812 * @param uid A memory region large enough to store TOX_CONFERENCE_UID_SIZE bytes.
2814 * @return true on success.
2815 * @deprecated use tox_conference_get_id instead (exactly the same function, just renamed).
2817 bool tox_conference_get_uid(const(Tox)* tox, uint conference_number, void *uid) @nogc;
2819 alias TOX_ERR_CONFERENCE_BY_UID = int;
2820 enum : int {
2822 * The function returned successfully.
2824 TOX_ERR_CONFERENCE_BY_UID_OK,
2827 * One of the arguments to the function was NULL when it was not expected.
2829 TOX_ERR_CONFERENCE_BY_UID_NULL,
2832 * No conference with the given uid exists on the conference list.
2834 TOX_ERR_CONFERENCE_BY_UID_NOT_FOUND,
2840 * Return the conference number associated with the specified uid.
2842 * @param uid A byte array containing the conference id (TOX_CONFERENCE_UID_SIZE).
2844 * @return the conference number on success, an unspecified value on failure.
2845 * @deprecated use tox_conference_by_id instead (exactly the same function, just renamed).
2847 uint tox_conference_by_uid(const(Tox)* tox, const(void)* uid, TOX_ERR_CONFERENCE_BY_UID *error) @nogc;
2850 /*******************************************************************************
2852 * :: Low-level custom packet sending and receiving
2854 ******************************************************************************/
2857 alias TOX_ERR_FRIEND_CUSTOM_PACKET = int;
2858 enum : int {
2860 * The function returned successfully.
2862 TOX_ERR_FRIEND_CUSTOM_PACKET_OK,
2865 * One of the arguments to the function was NULL when it was not expected.
2867 TOX_ERR_FRIEND_CUSTOM_PACKET_NULL,
2870 * The friend number did not designate a valid friend.
2872 TOX_ERR_FRIEND_CUSTOM_PACKET_FRIEND_NOT_FOUND,
2875 * This client is currently not connected to the friend.
2877 TOX_ERR_FRIEND_CUSTOM_PACKET_FRIEND_NOT_CONNECTED,
2880 * The first byte of data was not in the specified range for the packet type.
2881 * This range is 200-254 for lossy, and 160-191 for lossless packets.
2883 TOX_ERR_FRIEND_CUSTOM_PACKET_INVALID,
2886 * Attempted to send an empty packet.
2888 TOX_ERR_FRIEND_CUSTOM_PACKET_EMPTY,
2891 * Packet data length exceeded TOX_MAX_CUSTOM_PACKET_SIZE.
2893 TOX_ERR_FRIEND_CUSTOM_PACKET_TOO_LONG,
2896 * Packet queue is full.
2898 TOX_ERR_FRIEND_CUSTOM_PACKET_SENDQ,
2903 * Send a custom lossy packet to a friend.
2905 * The first byte of data must be in the range 200-254. Maximum length of a
2906 * custom packet is TOX_MAX_CUSTOM_PACKET_SIZE.
2908 * Lossy packets behave like UDP packets, meaning they might never reach the
2909 * other side or might arrive more than once (if someone is messing with the
2910 * connection) or might arrive in the wrong order.
2912 * Unless latency is an issue, it is recommended that you use lossless custom
2913 * packets instead.
2915 * @param friend_number The friend number of the friend this lossy packet
2916 * should be sent to.
2917 * @param data A byte array containing the packet data.
2918 * @param length The length of the packet data byte array.
2920 * @return true on success.
2922 bool tox_friend_send_lossy_packet(Tox* tox, uint friend_number, const(void)* data, usize length, TOX_ERR_FRIEND_CUSTOM_PACKET* error/*=null*/) @nogc;
2925 * Send a custom lossless packet to a friend.
2927 * The first byte of data must be in the range 160-191. Maximum length of a
2928 * custom packet is TOX_MAX_CUSTOM_PACKET_SIZE.
2930 * Lossless packet behaviour is comparable to TCP (reliability, arrive in order)
2931 * but with packets instead of a stream.
2933 * @param friend_number The friend number of the friend this lossless packet
2934 * should be sent to.
2935 * @param data A byte array containing the packet data.
2936 * @param length The length of the packet data byte array.
2938 * @return true on success.
2940 bool tox_friend_send_lossless_packet(Tox* tox, uint friend_number, const(void)* data, usize length, TOX_ERR_FRIEND_CUSTOM_PACKET* error/*=null*/) @nogc;
2943 * @param friend_number The friend number of the friend who sent a lossy packet.
2944 * @param data A byte array containing the received packet data.
2945 * @param length The length of the packet data byte array.
2947 alias tox_friend_lossy_packet_cb = void function (Tox* tox, uint friend_number, const(void)* data, usize length, void* user_data) nothrow @system;
2951 * Set the callback for the `friend_lossy_packet` event. Pass NULL to unset.
2954 void tox_callback_friend_lossy_packet(Tox* tox, tox_friend_lossy_packet_cb callback) @nogc;
2957 * @param friend_number The friend number of the friend who sent the packet.
2958 * @param data A byte array containing the received packet data.
2959 * @param length The length of the packet data byte array.
2961 alias tox_friend_lossless_packet_cb = void function (Tox* tox, uint friend_number, const(void)* data, usize length, void* user_data) nothrow @system;
2965 * Set the callback for the `friend_lossless_packet` event. Pass NULL to unset.
2968 void tox_callback_friend_lossless_packet(Tox* tox, tox_friend_lossless_packet_cb callback) @nogc;
2971 /*******************************************************************************
2973 * :: Low-level network information
2975 ******************************************************************************/
2979 * Writes the temporary DHT public key of this instance to a byte array.
2981 * This can be used in combination with an externally accessible IP address and
2982 * the bound port (from tox_self_get_udp_port) to run a temporary bootstrap node.
2984 * Be aware that every time a new instance is created, the DHT public key
2985 * changes, meaning this cannot be used to run a permanent bootstrap node.
2987 * @param dht_id A memory region of at least TOX_PUBLIC_KEY_SIZE bytes. If this
2988 * parameter is NULL, this function has no effect.
2990 void tox_self_get_dht_id(const(Tox)* tox, void* dht_id) @nogc;
2993 alias TOX_ERR_GET_PORT = int;
2994 enum : int {
2996 * The function returned successfully.
2998 TOX_ERR_GET_PORT_OK,
3001 * The instance was not bound to any port.
3003 TOX_ERR_GET_PORT_NOT_BOUND,
3008 * Return the UDP port this Tox instance is bound to.
3010 ushort tox_self_get_udp_port(const(Tox)* tox, TOX_ERR_GET_PORT* error/*=null*/) @nogc;
3013 * Return the TCP port this Tox instance is bound to. This is only relevant if
3014 * the instance is acting as a TCP relay.
3016 ushort tox_self_get_tcp_port(const(Tox)* tox, TOX_ERR_GET_PORT* error/*=null*/) @nogc;
3019 // ////////////////////////////////////////////////////////////////////////// //
3021 * Batch encryption functions.
3025 * Copyright © 2016-2017 The TokTok team.
3026 * Copyright © 2013-2016 Tox Developers.
3028 * This file is part of Tox, the free peer to peer instant messenger.
3030 * Tox is free software: you can redistribute it and/or modify
3031 * it under the terms of the GNU General Public License as published by
3032 * the Free Software Foundation, version 3 of the License ONLY.
3034 * Tox is distributed in the hope that it will be useful,
3035 * but WITHOUT ANY WARRANTY; without even the implied warranty of
3036 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3037 * GNU General Public License for more details.
3039 * You should have received a copy of the GNU General Public License
3040 * along with Tox. If not, see <http://www.gnu.org/licenses/>.
3044 /*******************************************************************************
3046 * This module is organized into two parts.
3048 * 1. A simple API operating on plain text/cipher text data and a password to
3049 * encrypt or decrypt it.
3050 * 2. A more advanced API that splits key derivation and encryption into two
3051 * separate function calls.
3053 * The first part is implemented in terms of the second part and simply calls
3054 * the separate functions in sequence. Since key derivation is very expensive
3055 * compared to the actual encryption, clients that do a lot of crypto should
3056 * prefer the advanced API and reuse pass-key objects.
3058 * To use the second part, first derive an encryption key from a password with
3059 * tox_pass_key_derive, then use the derived key to encrypt the data.
3061 * The encrypted data is prepended with a magic number, to aid validity
3062 * checking (no guarantees are made of course). Any data to be decrypted must
3063 * start with the magic number.
3065 * Clients should consider alerting their users that, unlike plain data, if
3066 * even one bit becomes corrupted, the data will be entirely unrecoverable.
3067 * Ditto if they forget their password, there is no way to recover the data.
3069 ******************************************************************************/
3074 * The size of the salt part of a pass-key.
3076 enum TOX_PASS_SALT_LENGTH = 32;
3078 uint tox_pass_salt_length() @nogc;
3081 * The size of the key part of a pass-key.
3083 enum TOX_PASS_KEY_LENGTH = 32;
3085 uint tox_pass_key_length() @nogc;
3088 * The amount of additional data required to store any encrypted byte array.
3089 * Encrypting an array of N bytes requires N + TOX_PASS_ENCRYPTION_EXTRA_LENGTH
3090 * bytes in the encrypted byte array.
3092 enum TOX_PASS_ENCRYPTION_EXTRA_LENGTH = 80;
3094 uint tox_pass_encryption_extra_length() @nogc;
3097 alias TOX_ERR_KEY_DERIVATION = int;
3098 enum : int {
3100 * The function returned successfully.
3102 TOX_ERR_KEY_DERIVATION_OK,
3105 * One of the arguments to the function was NULL when it was not expected.
3107 TOX_ERR_KEY_DERIVATION_NULL,
3110 * The crypto lib was unable to derive a key from the given passphrase,
3111 * which is usually a lack of memory issue.
3113 TOX_ERR_KEY_DERIVATION_FAILED,
3117 alias TOX_ERR_ENCRYPTION = int;
3118 enum : int {
3120 * The function returned successfully.
3122 TOX_ERR_ENCRYPTION_OK,
3125 * One of the arguments to the function was NULL when it was not expected.
3127 TOX_ERR_ENCRYPTION_NULL,
3130 * The crypto lib was unable to derive a key from the given passphrase,
3131 * which is usually a lack of memory issue. The functions accepting keys
3132 * do not produce this error.
3134 TOX_ERR_ENCRYPTION_KEY_DERIVATION_FAILED,
3137 * The encryption itself failed.
3139 TOX_ERR_ENCRYPTION_FAILED,
3143 alias TOX_ERR_DECRYPTION = int;
3144 enum : int {
3146 * The function returned successfully.
3148 TOX_ERR_DECRYPTION_OK,
3151 * One of the arguments to the function was NULL when it was not expected.
3153 TOX_ERR_DECRYPTION_NULL,
3156 * The input data was shorter than TOX_PASS_ENCRYPTION_EXTRA_LENGTH bytes
3158 TOX_ERR_DECRYPTION_INVALID_LENGTH,
3161 * The input data is missing the magic number (i.e. wasn't created by this
3162 * module, or is corrupted).
3164 TOX_ERR_DECRYPTION_BAD_FORMAT,
3167 * The crypto lib was unable to derive a key from the given passphrase,
3168 * which is usually a lack of memory issue. The functions accepting keys
3169 * do not produce this error.
3171 TOX_ERR_DECRYPTION_KEY_DERIVATION_FAILED,
3174 * The encrypted byte array could not be decrypted. Either the data was
3175 * corrupted or the password/key was incorrect.
3177 TOX_ERR_DECRYPTION_FAILED,
3182 /*******************************************************************************
3184 * BEGIN PART 1
3186 * The simple API is presented first. If your code spends too much time using
3187 * these functions, consider using the advanced functions instead and caching
3188 * the generated pass-key.
3190 ******************************************************************************/
3194 * Encrypts the given data with the given passphrase.
3196 * The output array must be at least `plaintext_len + TOX_PASS_ENCRYPTION_EXTRA_LENGTH`
3197 * bytes long. This delegates to tox_pass_key_derive and
3198 * tox_pass_key_encrypt.
3200 * @param plaintext A byte array of length `plaintext_len`.
3201 * @param plaintext_len The length of the plain text array. Bigger than 0.
3202 * @param passphrase The user-provided password. Can be empty.
3203 * @param passphrase_len The length of the password.
3204 * @param ciphertext The cipher text array to write the encrypted data to.
3206 * @return true on success.
3208 bool tox_pass_encrypt(const(void)* plaintext, usize plaintext_len, const(void)* passphrase, usize passphrase_len, void* ciphertext, TOX_ERR_ENCRYPTION* error/*=null*/) @nogc;
3211 * Decrypts the given data with the given passphrase.
3213 * The output array must be at least `ciphertext_len - TOX_PASS_ENCRYPTION_EXTRA_LENGTH`
3214 * bytes long. This delegates to tox_pass_key_decrypt.
3216 * @param ciphertext A byte array of length `ciphertext_len`.
3217 * @param ciphertext_len The length of the cipher text array. At least TOX_PASS_ENCRYPTION_EXTRA_LENGTH.
3218 * @param passphrase The user-provided password. Can be empty.
3219 * @param passphrase_len The length of the password.
3220 * @param plaintext The plain text array to write the decrypted data to.
3222 * @return true on success.
3224 bool tox_pass_decrypt(const(void)* ciphertext, usize ciphertext_len, const(void)* passphrase, usize passphrase_len, void* plaintext, TOX_ERR_DECRYPTION* error/*=null*/) @nogc;
3227 /*******************************************************************************
3229 * BEGIN PART 2
3231 * And now part 2, which does the actual encryption, and can be used to write
3232 * less CPU intensive client code than part one.
3234 ******************************************************************************/
3238 * This type represents a pass-key.
3240 * A pass-key and a password are two different concepts: a password is given
3241 * by the user in plain text. A pass-key is the generated symmetric key used
3242 * for encryption and decryption. It is derived from a salt and the user-
3243 * provided password.
3245 * The Tox_Pass_Key structure is hidden in the implementation. It can be created
3246 * using tox_pass_key_derive or tox_pass_key_derive_with_salt and must be deallocated using tox_pass_key_free.
3248 struct Tox_Pass_Key {
3249 // disable construction and postblit
3250 @disable this ();
3251 @disable this (this);
3255 * Deallocate a Tox_Pass_Key. This function behaves like free(), so NULL is an
3256 * acceptable argument value.
3258 void tox_pass_key_free(Tox_Pass_Key* _key) @nogc;
3261 * Generates a secret symmetric key from the given passphrase.
3263 * Be sure to not compromise the key! Only keep it in memory, do not write
3264 * it to disk.
3266 * Note that this function is not deterministic; to derive the same key from
3267 * a password, you also must know the random salt that was used. A
3268 * deterministic version of this function is tox_pass_key_derive_with_salt.
3270 * @param passphrase The user-provided password. Can be empty.
3271 * @param passphrase_len The length of the password.
3273 * @return non-NULL on success.
3275 Tox_Pass_Key* tox_pass_key_derive(const(void)* passphrase, usize passphrase_len, TOX_ERR_KEY_DERIVATION* error/*=null*/) @nogc;
3278 * Same as above, except use the given salt for deterministic key derivation.
3280 * @param passphrase The user-provided password. Can be empty.
3281 * @param passphrase_len The length of the password.
3282 * @param salt An array of at least TOX_PASS_SALT_LENGTH bytes.
3284 * @return non-NULL on success.
3286 Tox_Pass_Key* tox_pass_key_derive_with_salt(const(void)* passphrase, usize passphrase_len, const(void)* salt, TOX_ERR_KEY_DERIVATION* error/*=null*/) @nogc;
3289 * Encrypt a plain text with a key produced by tox_pass_key_derive or tox_pass_key_derive_with_salt.
3291 * The output array must be at least `plaintext_len + TOX_PASS_ENCRYPTION_EXTRA_LENGTH`
3292 * bytes long.
3294 * @param plaintext A byte array of length `plaintext_len`.
3295 * @param plaintext_len The length of the plain text array. Bigger than 0.
3296 * @param ciphertext The cipher text array to write the encrypted data to.
3298 * @return true on success.
3300 bool tox_pass_key_encrypt(const(Tox_Pass_Key)* _key, const(void)* plaintext, usize plaintext_len, void* ciphertext, TOX_ERR_ENCRYPTION* error/*=null*/) @nogc;
3303 * This is the inverse of tox_pass_key_encrypt, also using only keys produced by
3304 * tox_pass_key_derive or tox_pass_key_derive_with_salt.
3306 * @param ciphertext A byte array of length `ciphertext_len`.
3307 * @param ciphertext_len The length of the cipher text array. At least TOX_PASS_ENCRYPTION_EXTRA_LENGTH.
3308 * @param plaintext The plain text array to write the decrypted data to.
3310 * @return true on success.
3312 bool tox_pass_key_decrypt(const(Tox_Pass_Key)* _key, const(void)* ciphertext, usize ciphertext_len, void* plaintext, TOX_ERR_DECRYPTION* error/*=null*/) @nogc;
3315 alias TOX_ERR_GET_SALT = int;
3316 enum : int {
3318 * The function returned successfully.
3320 TOX_ERR_GET_SALT_OK,
3323 * One of the arguments to the function was NULL when it was not expected.
3325 TOX_ERR_GET_SALT_NULL,
3328 * The input data is missing the magic number (i.e. wasn't created by this
3329 * module, or is corrupted).
3331 TOX_ERR_GET_SALT_BAD_FORMAT,
3336 * Retrieves the salt used to encrypt the given data.
3338 * The retrieved salt can then be passed to tox_pass_key_derive_with_salt to
3339 * produce the same key as was previously used. Any data encrypted with this
3340 * module can be used as input.
3342 * The cipher text must be at least TOX_PASS_ENCRYPTION_EXTRA_LENGTH bytes in length.
3343 * The salt must be TOX_PASS_SALT_LENGTH bytes in length.
3344 * If the passed byte arrays are smaller than required, the behaviour is
3345 * undefined.
3347 * If the cipher text pointer or the salt is NULL, this function returns false.
3349 * Success does not say anything about the validity of the data, only that
3350 * data of the appropriate size was copied.
3352 * @return true on success.
3354 bool tox_get_salt(const(void)* ciphertext, void* salt, TOX_ERR_GET_SALT* error/*=null*/) @nogc;
3357 * Determines whether or not the given data is encrypted by this module.
3359 * It does this check by verifying that the magic number is the one put in
3360 * place by the encryption functions.
3362 * The data must be at least TOX_PASS_ENCRYPTION_EXTRA_LENGTH bytes in length.
3363 * If the passed byte array is smaller than required, the behaviour is
3364 * undefined.
3366 * If the data pointer is NULL, the behaviour is undefined
3368 * @return true if the data is encrypted by this module.
3370 bool tox_is_data_encrypted(const(void)* data) @nogc;
3373 } // extern(C) nothrow @trusted
3376 // ////////////////////////////////////////////////////////////////////////// //
3377 // Ketmar's added utilities
3378 //import std.net.curl;
3381 bool tox_unhex() (ubyte[] dest, const(char)[] str) nothrow @trusted @nogc {
3382 if (dest.length == 0) return false;
3383 typeof(dest.length) dpos = 0;
3384 foreach (char ch; str) {
3385 if (ch <= ' ' || ch == '_') continue;
3386 if (dpos >= dest.length*2) return false;
3387 int n;
3388 if (ch >= '0' && ch <= '9') n = ch-'0';
3389 else if (ch >= 'A' && ch <= 'F') n = ch-'A'+10;
3390 else if (ch >= 'a' && ch <= 'f') n = ch-'a'+10;
3391 else return false;
3392 if (dpos%2 == 0) dest[dpos/2] = cast(ubyte)(n*16); else dest[dpos/2] |= cast(ubyte)n;
3393 ++dpos;
3395 return (dpos == dest.length*2);
3399 string tox_hex() (const(void)[] src) nothrow @trusted {
3400 assert(src.length < int.max/2);
3401 if (src.length == 0) return null;
3402 auto res = new char[](src.length*2);
3403 int dpos = 0;
3404 foreach (ubyte b; cast(const(ubyte)[])src) {
3405 immutable hb = (b>>4);
3406 b &= 0x0f;
3407 res[dpos++] = cast(char)(hb+'0'+(hb > 9 ? 7 : 0));
3408 res[dpos++] = cast(char)(b+'0'+(b > 9 ? 7 : 0));
3410 return cast(string)res; // it is safe to cast here
3414 struct ToxBootstrapServer {
3415 string maintainer;
3416 ubyte[TOX_PUBLIC_KEY_SIZE] pubkey;
3417 string ipv4; // always contains trailing zero
3418 ushort port;
3419 bool tcp;
3420 bool udp;
3421 ushort[] tcpports;
3425 ToxBootstrapServer[] tox_download_bootstrap_list() (string url=null) {
3426 import std.net.curl : get;
3427 import std.json;
3428 if (url.length == 0) url = "https://nodes.tox.chat/json";
3429 auto lst = get(url);
3430 auto js = parseJSON(lst);
3431 ToxBootstrapServer[] res;
3432 foreach (ref svx; js["nodes"].array) {
3433 import std.conv : to;
3434 auto sv = ToxBootstrapServer();
3435 sv.maintainer = svx["maintainer"].str;
3436 if (!tox_unhex(sv.pubkey, svx["public_key"].str)) continue;
3437 //if (tox_hex(sv.pubkey) != svx["public_key"].str) assert(0, "wtf?!");
3438 auto loc = svx["location"].str;
3439 if (loc.length == 2 && (loc[0] == 'R' || loc[0] == 'r') && (loc[1] == 'U' || loc[1] == 'u')) continue;
3440 auto v4 = svx["ipv4"].str;
3441 if (v4.length < 1) continue;
3442 auto tv4 = new char[](v4.length+1);
3443 tv4[] = 0;
3444 tv4[0..$-1] = v4[];
3445 sv.ipv4 = cast(string)tv4; // it is safe to cast here
3446 if (!sv.ipv4.length) continue;
3447 if (svx["port"].type == JSON_TYPE.INTEGER) {
3448 auto pp = svx["port"].integer;
3449 if (pp < 1 || pp > 65535) continue;
3450 sv.port = cast(ushort)pp;
3451 } else {
3452 continue;
3454 if (svx["status_udp"].type == JSON_TYPE.TRUE) sv.udp = true;
3455 if (svx["status_tcp"].type == JSON_TYPE.TRUE) sv.tcp = true;
3456 if (!sv.tcp && !sv.udp) continue;
3457 if (sv.tcp) {
3458 tcpportloop: foreach (ref tpp; svx["tcp_ports"].array) {
3459 if (tpp.type != JSON_TYPE.INTEGER) continue;
3460 auto pp = svx["port"].integer;
3461 if (pp < 1 || pp > 65535) continue;
3462 foreach (immutable ushort t; sv.tcpports) if (t == pp) continue tcpportloop;
3463 sv.tcpports ~= cast(ushort)pp;
3465 if (sv.tcpports.length == 0) {
3466 sv.tcp = false;
3467 if (!sv.udp) continue;
3470 res ~= sv;
3472 return res;