iv.nanovega: removed unnecessary bounds checking
[iv.d.git] / tox.d
blobe33b8dce639f15cd5366724a7ca02fbffdd48dfc
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, either version 3 of the License, or
14 * (at your option) any later version.
16 * Tox is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with Tox. If not, see <http://www.gnu.org/licenses/>.
24 module iv.tox /*is aliced*/;
25 pragma(lib, "toxcore");
26 pragma(lib, "toxencryptsave");
27 pragma(lib, "sodium"); // for static linking
28 import iv.alice;
29 // WARNING: functions with callbacks (and callbacks 'emselves) will be marked `@system`!
30 extern(C) nothrow @trusted {
33 /** core Public core API for Tox clients.
35 * Every function that can fail takes a function-specific error code pointer
36 * that can be used to diagnose problems with the Tox state or the function
37 * arguments. The error code pointer can be NULL, which does not influence the
38 * function's behaviour, but can be done if the reason for failure is irrelevant
39 * to the client.
41 * The exception to this rule are simple allocation functions whose only failure
42 * mode is allocation failure. They return NULL in that case, and do not set an
43 * error code.
45 * Every error code type has an OK value to which functions will set their error
46 * code value on success. Clients can keep their error code uninitialised before
47 * passing it to a function. The library guarantees that after returning, the
48 * value pointed to by the error code pointer has been initialised.
50 * Functions with pointer parameters often have a NULL error code, meaning they
51 * could not perform any operation, because one of the required parameters was
52 * NULL. Some functions operate correctly or are defined as effectless on NULL.
54 * Some functions additionally return a value outside their
55 * return type domain, or a bool containing true on success and false on
56 * failure.
58 * All functions that take a Tox instance pointer will cause undefined behaviour
59 * when passed a NULL Tox pointer.
61 * All integer values are expected in host byte order.
63 * Functions with parameters with enum types cause unspecified behaviour if the
64 * enumeration value is outside the valid range of the type. If possible, the
65 * function will try to use a sane default, but there will be no error code,
66 * and one possible action for the function to take is to have no effect.
68 * Integer constants and the memory layout of publicly exposed structs are not
69 * part of the ABI.
72 /** events Events and callbacks
74 * Events are handled by callbacks. One callback can be registered per event.
75 * All events have a callback function type named `tox_{event}_cb` and a
76 * function to register it named `tox_callback_{event}`. Passing a NULL
77 * callback will result in no callback being registered for that event. Only
78 * one callback per event can be registered, so if a client needs multiple
79 * event listeners, it needs to implement the dispatch functionality itself.
81 * The last argument to a callback is the user data pointer. It is passed from
82 * tox_iterate to each callback in sequence.
84 * The user data pointer is never stored or dereferenced by any library code, so
85 * can be any pointer, including NULL. Callbacks must all operate on the same
86 * object type. In the apidsl code (tox.in.h), this is denoted with `any`. The
87 * `any` in tox_iterate must be the same `any` as in all callbacks. In C,
88 * lacking parametric polymorphism, this is a pointer to void.
90 * Old style callbacks that are registered together with a user data pointer
91 * receive that pointer as argument when they are called. They can each have
92 * their own user data pointer of their own type.
95 /** threading Threading implications
97 * It is possible to run multiple concurrent threads with a Tox instance for
98 * each thread. It is also possible to run all Tox instances in the same thread.
99 * A common way to run Tox (multiple or single instance) is to have one thread
100 * running a simple tox_iterate loop, sleeping for tox_iteration_interval
101 * milliseconds on each iteration.
103 * If you want to access a single Tox instance from multiple threads, access
104 * to the instance must be synchronised. While multiple threads can concurrently
105 * access multiple different Tox instances, no more than one API function can
106 * operate on a single instance at any given time.
108 * Functions that write to variable length byte arrays will always have a size
109 * function associated with them. The result of this size function is only valid
110 * until another mutating function (one that takes a pointer to non-const Tox)
111 * is called. Thus, clients must ensure that no other thread calls a mutating
112 * function between the call to the size function and the call to the retrieval
113 * function.
115 * E.g. to get the current nickname, one would write
117 * ---
118 * auto length = tox_self_get_name_size(tox);
119 * auto name = new char[](length);
120 * tox_self_get_name(tox, name.ptr);
121 * ---
123 * If any other thread calls tox_self_set_name while this thread is allocating
124 * memory, the length may have become invalid, and the call to
125 * tox_self_get_name may cause undefined behaviour.
130 * The Tox instance type. All the state associated with a connection is held
131 * within the instance. Multiple instances can exist and operate concurrently.
132 * The maximum number of Tox instances that can exist on a single network
133 * device is limited. Note that this is not just a per-process limit, since the
134 * limiting factor is the number of usable ports on a device.
136 struct Tox {
137 // disable construction and postblit
138 @disable this ();
139 @disable this (this);
141 alias ToxP = Tox*;
144 /*******************************************************************************
146 * :: API version
148 ******************************************************************************/
152 * The major version number. Incremented when the API or ABI changes in an
153 * incompatible way.
155 * The function variants of these constants return the version number of the
156 * library. They can be used to display the Tox library version or to check
157 * whether the client is compatible with the dynamically linked version of Tox.
159 enum TOX_VERSION_MAJOR = 0;
160 uint tox_version_major() @nogc;
163 * The minor version number. Incremented when functionality is added without
164 * breaking the API or ABI. Set to 0 when the major version number is
165 * incremented.
167 enum TOX_VERSION_MINOR = 2;
168 uint tox_version_minor() @nogc;
171 * The patch or revision number. Incremented when bugfixes are applied without
172 * changing any functionality or API or ABI.
174 enum TOX_VERSION_PATCH = 0;
175 uint tox_version_patch() @nogc;
178 * A macro to check at preprocessing time whether the client code is compatible
179 * with the installed version of Tox. Leading zeros in the version number are
180 * ignored. E.g. 0.1.5 is to 0.1.4 what 1.5 is to 1.4, that is: it can add new
181 * features, but can't break the API.
183 bool TOX_VERSION_IS_API_COMPATIBLE(int MAJOR, int MINOR, int PATCH) pure nothrow @safe @nogc {
184 return
185 (TOX_VERSION_MAJOR > 0 && TOX_VERSION_MAJOR == MAJOR) && (
186 /* 1.x.x, 2.x.x, etc. with matching major version. */
187 TOX_VERSION_MINOR > MINOR ||
188 TOX_VERSION_MINOR == MINOR && TOX_VERSION_PATCH >= PATCH
189 ) || (TOX_VERSION_MAJOR == 0 && MAJOR == 0) && (
190 /* 0.x.x makes minor behave like major above. */
191 (TOX_VERSION_MINOR > 0 && TOX_VERSION_MINOR == MINOR) && (
192 TOX_VERSION_PATCH >= PATCH
193 ) || (TOX_VERSION_MINOR == 0 && MINOR == 0) && (
194 /* 0.0.x and 0.0.y are only compatible if x == y. */
195 TOX_VERSION_PATCH == PATCH
202 * Return whether the compiled library version is compatible with the passed
203 * version numbers.
205 bool tox_version_is_compatible(uint major, uint minor, uint patch) nothrow @nogc;
208 * A convenience macro to call tox_version_is_compatible with the currently
209 * compiling API version.
211 bool TOX_VERSION_IS_ABI_COMPATIBLE() nothrow @safe @nogc { return tox_version_is_compatible(TOX_VERSION_MAJOR, TOX_VERSION_MINOR, TOX_VERSION_PATCH); }
214 /*******************************************************************************
216 * :: Numeric constants
218 * The values of these are not part of the ABI. Prefer to use the function
219 * versions of them for code that should remain compatible with future versions
220 * of toxcore.
222 ******************************************************************************/
226 * The size of a Tox Public Key in bytes.
228 enum TOX_PUBLIC_KEY_SIZE = 32;
229 uint tox_public_key_size() @nogc;
232 * The size of a Tox Secret Key in bytes.
234 enum TOX_SECRET_KEY_SIZE = 32;
235 uint tox_secret_key_size() @nogc;
238 * The size of the nospam in bytes when written in a Tox address.
240 enum TOX_NOSPAM_SIZE = cast(uint)(uint.sizeof);
241 uint tox_nospam_size() @nogc;
244 * The size of a Tox address in bytes. Tox addresses are in the format
245 * [Public Key (TOX_PUBLIC_KEY_SIZE bytes)][nospam (4 bytes)][checksum (2 bytes)].
247 * The checksum is computed over the Public Key and the nospam value. The first
248 * byte is an XOR of all the even bytes (0, 2, 4, ...), the second byte is an
249 * XOR of all the odd bytes (1, 3, 5, ...) of the Public Key and nospam.
251 enum TOX_ADDRESS_SIZE = cast(uint)(TOX_PUBLIC_KEY_SIZE + TOX_NOSPAM_SIZE + ushort.sizeof);
252 uint tox_address_size() @nogc;
255 * Maximum length of a nickname in bytes.
257 enum TOX_MAX_NAME_LENGTH = 128;
258 uint tox_max_name_length() @nogc;
261 * Maximum length of a status message in bytes.
263 enum TOX_MAX_STATUS_MESSAGE_LENGTH = 1007;
264 uint tox_max_status_message_length() @nogc;
267 * Maximum length of a friend request message in bytes.
269 enum TOX_MAX_FRIEND_REQUEST_LENGTH = 1016;
270 uint tox_max_friend_request_length() @nogc;
273 * Maximum length of a single message after which it should be split.
275 enum TOX_MAX_MESSAGE_LENGTH = 1372;
276 uint tox_max_message_length() @nogc;
279 * Maximum size of custom packets. TODO(iphydf): should be LENGTH?
281 enum TOX_MAX_CUSTOM_PACKET_SIZE = 1373;
282 uint tox_max_custom_packet_size() @nogc;
285 * The number of bytes in a hash generated by tox_hash.
287 enum TOX_HASH_LENGTH = 32;
288 uint tox_hash_length() @nogc;
291 * The number of bytes in a file id.
293 enum TOX_FILE_ID_LENGTH = 32;
294 uint tox_file_id_length() @nogc;
297 * Maximum file name length for file transfers.
299 enum TOX_MAX_FILENAME_LENGTH = 255;
300 uint tox_max_filename_length() @nogc;
303 /*******************************************************************************
305 * :: Global enumerations
307 ******************************************************************************/
311 * Represents the possible statuses a client can have.
313 alias TOX_USER_STATUS = int;
314 enum : int {
316 * User is online and available.
318 TOX_USER_STATUS_NONE,
321 * User is away. Clients can set this e.g. after a user defined
322 * inactivity time.
324 TOX_USER_STATUS_AWAY,
327 * User is busy. Signals to other clients that this client does not
328 * currently wish to communicate.
330 TOX_USER_STATUS_BUSY,
335 * Represents message types for tox_friend_send_message and conference
336 * messages.
338 alias TOX_MESSAGE_TYPE = int;
339 enum : int {
341 * Normal text message. Similar to PRIVMSG on IRC.
343 TOX_MESSAGE_TYPE_NORMAL,
346 * A message describing an user action. This is similar to /me (CTCP ACTION)
347 * on IRC.
349 TOX_MESSAGE_TYPE_ACTION,
352 * Correction of the last message. With empty message body can be used to mark
353 * last message as deleted.
355 TOX_MESSAGE_TYPE_CORRECTION,
359 /*******************************************************************************
361 * :: Startup options
363 ******************************************************************************/
367 * Type of proxy used to connect to TCP relays.
369 alias TOX_PROXY_TYPE = int;
370 enum : int {
372 * Don't use a proxy.
374 TOX_PROXY_TYPE_NONE,
377 * HTTP proxy using CONNECT.
379 TOX_PROXY_TYPE_HTTP,
382 * SOCKS proxy for simple socket pipes.
384 TOX_PROXY_TYPE_SOCKS5,
389 * Type of savedata to create the Tox instance from.
391 alias TOX_SAVEDATA_TYPE = int;
392 enum : int {
394 * No savedata.
396 TOX_SAVEDATA_TYPE_NONE,
399 * Savedata is one that was obtained from tox_get_savedata.
401 TOX_SAVEDATA_TYPE_TOX_SAVE,
404 * Savedata is a secret key of length TOX_SECRET_KEY_SIZE.
406 TOX_SAVEDATA_TYPE_SECRET_KEY,
411 * Severity level of log messages.
413 alias TOX_LOG_LEVEL = int;
414 enum : int {
416 * Very detailed traces including all network activity.
418 TOX_LOG_LEVEL_TRACE,
421 * Debug messages such as which port we bind to.
423 TOX_LOG_LEVEL_DEBUG,
426 * Informational log messages such as video call status changes.
428 TOX_LOG_LEVEL_INFO,
431 * Warnings about internal inconsistency or logic errors.
433 TOX_LOG_LEVEL_WARNING,
436 * Severe unexpected errors caused by external or internal inconsistency.
438 TOX_LOG_LEVEL_ERROR,
443 * This event is triggered when the toxcore library logs an internal message.
444 * This is mostly useful for debugging. This callback can be called from any
445 * function, not just tox_iterate. This means the user data lifetime must at
446 * least extend between registering and unregistering it or tox_kill.
448 * Other toxcore modules such as toxav may concurrently call this callback at
449 * any time. Thus, user code must make sure it is equipped to handle concurrent
450 * execution, e.g. by employing appropriate mutex locking.
452 * @param level The severity of the log message.
453 * @param file The source file from which the message originated.
454 * @param line The source line from which the message originated.
455 * @param func The function from which the message originated.
456 * @param message The log message.
457 * @param user_data The user data pointer passed to tox_new in options.
459 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;
463 * This struct contains all the startup options for Tox. You must tox_options_new to
464 * allocate an object of this type.
466 struct Tox_Options {
467 // disable construction and postblit
468 @disable this ();
469 @disable this (this);
474 * The type of socket to create.
476 * If this is set to false, an IPv4 socket is created, which subsequently
477 * only allows IPv4 communication.
478 * If it is set to true, an IPv6 socket is created, allowing both IPv4 and
479 * IPv6 communication.
481 bool tox_options_get_ipv6_enabled(const(Tox_Options)* options) @nogc;
482 /// ditto
483 void tox_options_set_ipv6_enabled(Tox_Options* options, bool ipv6_enabled) @nogc;
486 * Enable the use of UDP communication when available.
488 * Setting this to false will force Tox to use TCP only. Communications will
489 * need to be relayed through a TCP relay node, potentially slowing them down.
490 * Disabling UDP support is necessary when using anonymous proxies or Tor.
492 bool tox_options_get_udp_enabled(const(Tox_Options)* options) @nogc;
493 /// ditto
494 void tox_options_set_udp_enabled(Tox_Options* options, bool udp_enabled) @nogc;
497 * Enable local network peer discovery.
499 * Disabling this will cause Tox to not look for peers on the local network.
501 bool tox_options_get_local_discovery_enabled(const(Tox_Options)* options) @nogc;
502 /// ditto
503 void tox_options_set_local_discovery_enabled(Tox_Options* options, bool local_discovery_enabled) @nogc;
506 * Pass communications through a proxy.
508 TOX_PROXY_TYPE tox_options_get_proxy_type(const(Tox_Options)* options) @nogc;
509 /// ditto
510 void tox_options_set_proxy_type(Tox_Options* options, TOX_PROXY_TYPE type) @nogc;
513 * The IP address or DNS name of the proxy to be used.
515 * If used, this must be non-NULL and be a valid DNS name. The name must not
516 * exceed 255 characters, and be in a NUL-terminated C string format
517 * (255 chars + 1 NUL byte).
519 * This member is ignored (it can be NULL) if proxy_type is TOX_PROXY_TYPE_NONE.
521 * The data pointed at by this member is owned by the user, so must
522 * outlive the options object.
524 const(char)* tox_options_get_proxy_host(const(Tox_Options)* options) @nogc;
525 /// ditto
526 void tox_options_set_proxy_host(Tox_Options* options, const(char)* host) @nogc;
529 * The port to use to connect to the proxy server.
531 * Ports must be in the range (1, 65535). The value is ignored if
532 * proxy_type is TOX_PROXY_TYPE_NONE.
534 ushort tox_options_get_proxy_port(const(Tox_Options)* options) @nogc;
535 /// ditto
536 void tox_options_set_proxy_port(Tox_Options* options, ushort port) @nogc;
539 * The start port of the inclusive port range to attempt to use.
541 * If both start_port and end_port are 0, the default port range will be
542 * used: [33445, 33545].
544 * If either start_port or end_port is 0 while the other is non-zero, the
545 * non-zero port will be the only port in the range.
547 * Having start_port > end_port will yield the same behavior as if start_port
548 * and end_port were swapped.
550 ushort tox_options_get_start_port(const(Tox_Options)* options) @nogc;
551 /// ditto
552 void tox_options_set_start_port(Tox_Options* options, ushort start_port) @nogc;
555 * The end port of the inclusive port range to attempt to use.
557 ushort tox_options_get_end_port(const(Tox_Options)* options) @nogc;
558 /// ditto
559 void tox_options_set_end_port(Tox_Options* options, ushort end_port) @nogc;
562 * The port to use for the TCP server (relay). If 0, the TCP server is
563 * disabled.
565 * Enabling it is not required for Tox to function properly.
567 * When enabled, your Tox instance can act as a TCP relay for other Tox
568 * instance. This leads to increased traffic, thus when writing a client
569 * it is recommended to enable TCP server only if the user has an option
570 * to disable it.
572 ushort tox_options_get_tcp_port(const(Tox_Options)* options) @nogc;
573 /// ditto
574 void tox_options_set_tcp_port(Tox_Options* options, ushort tcp_port) @nogc;
577 * Enables or disables UDP hole-punching in toxcore. (Default: enabled).
579 bool tox_options_get_hole_punching_enabled(const(Tox_Options)* options) @nogc;
580 /// ditto
581 void tox_options_set_hole_punching_enabled(Tox_Options* options, bool hole_punching_enabled) @nogc;
584 * The type of savedata to load from.
586 TOX_SAVEDATA_TYPE tox_options_get_savedata_type(const(Tox_Options)* options) @nogc;
587 /// ditto
588 void tox_options_set_savedata_type(Tox_Options* options, TOX_SAVEDATA_TYPE type) @nogc;
591 * The savedata.
593 * The data pointed at by this member is owned by the user, so must
594 * outlive the options object.
596 const(void)* tox_options_get_savedata_data(const(Tox_Options)* options) @nogc;
597 /// ditto
598 void tox_options_set_savedata_data(Tox_Options* options, const(void)* data, usize length) @nogc;
601 * The length of the savedata.
603 usize tox_options_get_savedata_length(const(Tox_Options)* options) @nogc;
604 /// ditto
605 void tox_options_set_savedata_length(Tox_Options* options, usize length) @nogc;
608 * Logging callback for the new tox instance.
610 tox_log_cb tox_options_get_log_callback(const(Tox_Options)* options) @nogc;
611 /// ditto
612 void tox_options_set_log_callback(Tox_Options* options, tox_log_cb callback) @nogc;
615 * User data pointer passed to the logging callback.
617 void* tox_options_get_log_user_data(const(Tox_Options)* options) @nogc;
618 /// ditto
619 void tox_options_set_log_user_data(Tox_Options* options, void* user_data) @nogc;
623 * Initialises a Tox_Options object with the default options.
625 * The result of this function is independent of the original options. All
626 * values will be overwritten, no values will be read (so it is permissible
627 * to pass an uninitialised object).
629 * If options is NULL, this function has no effect.
631 * @param options An options object to be filled with default options.
633 void tox_options_default(Tox_Options* options) @nogc;
636 alias TOX_ERR_OPTIONS_NEW = int;
637 enum : int {
639 * The function returned successfully.
641 TOX_ERR_OPTIONS_NEW_OK,
644 * The function failed to allocate enough memory for the options struct.
646 TOX_ERR_OPTIONS_NEW_MALLOC,
651 * Allocates a new Tox_Options object and initialises it with the default
652 * options. This function can be used to preserve long term ABI compatibility by
653 * giving the responsibility of allocation and deallocation to the Tox library.
655 * Objects returned from this function must be freed using the tox_options_free
656 * function.
658 * @return A new Tox_Options object with default options or NULL on failure.
660 Tox_Options* tox_options_new(TOX_ERR_OPTIONS_NEW* error=null) @nogc;
663 * Releases all resources associated with an options objects.
665 * Passing a pointer that was not returned by tox_options_new results in
666 * undefined behaviour.
668 void tox_options_free(Tox_Options* options) @nogc;
671 /*******************************************************************************
673 * :: Creation and destruction
675 ******************************************************************************/
678 alias TOX_ERR_NEW = int;
679 enum : int {
681 * The function returned successfully.
683 TOX_ERR_NEW_OK,
686 * One of the arguments to the function was NULL when it was not expected.
688 TOX_ERR_NEW_NULL,
691 * The function was unable to allocate enough memory to store the internal
692 * structures for the Tox object.
694 TOX_ERR_NEW_MALLOC,
697 * The function was unable to bind to a port. This may mean that all ports
698 * have already been bound, e.g. by other Tox instances, or it may mean
699 * a permission error. You may be able to gather more information from errno.
701 TOX_ERR_NEW_PORT_ALLOC,
704 * proxy_type was invalid.
706 TOX_ERR_NEW_PROXY_BAD_TYPE,
709 * proxy_type was valid but the proxy_host passed had an invalid format
710 * or was NULL.
712 TOX_ERR_NEW_PROXY_BAD_HOST,
715 * proxy_type was valid, but the proxy_port was invalid.
717 TOX_ERR_NEW_PROXY_BAD_PORT,
720 * The proxy address passed could not be resolved.
722 TOX_ERR_NEW_PROXY_NOT_FOUND,
725 * The byte array to be loaded contained an encrypted save.
727 TOX_ERR_NEW_LOAD_ENCRYPTED,
730 * The data format was invalid. This can happen when loading data that was
731 * saved by an older version of Tox, or when the data has been corrupted.
732 * When loading from badly formatted data, some data may have been loaded,
733 * and the rest is discarded. Passing an invalid length parameter also
734 * causes this error.
736 TOX_ERR_NEW_LOAD_BAD_FORMAT,
741 * @brief Creates and initialises a new Tox instance with the options passed.
743 * This function will bring the instance into a valid state. Running the event
744 * loop with a new instance will operate correctly.
746 * If loading failed or succeeded only partially, the new or partially loaded
747 * instance is returned and an error code is set.
749 * @param options An options object as described above. If this parameter is
750 * NULL, the default options are used.
752 * @see tox_iterate for the event loop.
754 * @return A new Tox instance pointer on success or NULL on failure.
756 Tox* tox_new(const(Tox_Options)* options, TOX_ERR_NEW* error=null) @nogc;
759 * Releases all resources associated with the Tox instance and disconnects from
760 * the network.
762 * After calling this function, the Tox pointer becomes invalid. No other
763 * functions can be called, and the pointer value can no longer be read.
765 void tox_kill(Tox* tox) @nogc;
768 * Calculates the number of bytes required to store the tox instance with
769 * tox_get_savedata. This function cannot fail. The result is always greater than 0.
771 * @see threading for concurrency implications.
773 usize tox_get_savedata_size(const(Tox)* tox) @nogc;
776 * Store all information associated with the tox instance to a byte array.
778 * @param savedata A memory region large enough to store the tox instance
779 * data. Call tox_get_savedata_size to find the number of bytes required. If this parameter
780 * is NULL, this function has no effect.
782 void tox_get_savedata(const(Tox)* tox, void* savedata) @nogc;
785 /*******************************************************************************
787 * :: Connection lifecycle and event loop
789 ******************************************************************************/
792 alias TOX_ERR_BOOTSTRAP = int;
793 enum : int {
795 * The function returned successfully.
797 TOX_ERR_BOOTSTRAP_OK,
800 * One of the arguments to the function was NULL when it was not expected.
802 TOX_ERR_BOOTSTRAP_NULL,
805 * The address could not be resolved to an IP address, or the IP address
806 * passed was invalid.
808 TOX_ERR_BOOTSTRAP_BAD_HOST,
811 * The port passed was invalid. The valid port range is (1, 65535).
813 TOX_ERR_BOOTSTRAP_BAD_PORT,
818 * Sends a "get nodes" request to the given bootstrap node with IP, port, and
819 * public key to setup connections.
821 * This function will attempt to connect to the node using UDP. You must use
822 * this function even if Tox_Options.udp_enabled was set to false.
824 * @param address The hostname or IP address (IPv4 or IPv6) of the node.
825 * @param port The port on the host on which the bootstrap Tox instance is
826 * listening.
827 * @param public_key The long term public key of the bootstrap node
828 * (TOX_PUBLIC_KEY_SIZE bytes).
829 * @return true on success.
831 bool tox_bootstrap(Tox* tox, const(char)* address, ushort port, const(void)* public_key, TOX_ERR_BOOTSTRAP* error=null) @nogc;
834 * Adds additional host:port pair as TCP relay.
836 * This function can be used to initiate TCP connections to different ports on
837 * the same bootstrap node, or to add TCP relays without using them as
838 * bootstrap nodes.
840 * @param address The hostname or IP address (IPv4 or IPv6) of the TCP relay.
841 * @param port The port on the host on which the TCP relay is listening.
842 * @param public_key The long term public key of the TCP relay
843 * (TOX_PUBLIC_KEY_SIZE bytes).
844 * @return true on success.
846 bool tox_add_tcp_relay(Tox* tox, const(char)* address, ushort port, const(void)* public_key, TOX_ERR_BOOTSTRAP* error=null) @nogc;
850 * Protocols that can be used to connect to the network or friends.
852 alias TOX_CONNECTION = int;
853 enum : int {
855 * There is no connection. This instance, or the friend the state change is
856 * about, is now offline.
858 TOX_CONNECTION_NONE,
861 * A TCP connection has been established. For the own instance, this means it
862 * is connected through a TCP relay, only. For a friend, this means that the
863 * connection to that particular friend goes through a TCP relay.
865 TOX_CONNECTION_TCP,
868 * A UDP connection has been established. For the own instance, this means it
869 * is able to send UDP packets to DHT nodes, but may still be connected to
870 * a TCP relay. For a friend, this means that the connection to that
871 * particular friend was built using direct UDP packets.
873 TOX_CONNECTION_UDP,
878 * Return whether we are connected to the DHT. The return value is equal to the
879 * last value received through the `self_connection_status` callback.
881 TOX_CONNECTION tox_self_get_connection_status(const(Tox)* tox) @nogc;
884 * @param connection_status Whether we are connected to the DHT.
886 alias tox_self_connection_status_cb = void function (Tox* tox, TOX_CONNECTION connection_status, void* user_data) nothrow @system;
890 * Set the callback for the `self_connection_status` event. Pass NULL to unset.
892 * This event is triggered whenever there is a change in the DHT connection
893 * state. When disconnected, a client may choose to call tox_bootstrap again, to
894 * reconnect to the DHT. Note that this state may frequently change for short
895 * amounts of time. Clients should therefore not immediately bootstrap on
896 * receiving a disconnect.
898 * TODO(iphydf): how long should a client wait before bootstrapping again?
900 void tox_callback_self_connection_status(Tox* tox, tox_self_connection_status_cb callback) @nogc;
903 * Return the time in milliseconds before tox_iterate() should be called again
904 * for optimal performance.
906 uint tox_iteration_interval(const(Tox)* tox) @nogc;
909 * The main loop that needs to be run in intervals of tox_iteration_interval()
910 * milliseconds.
912 void tox_iterate(Tox* tox, void* user_data) @nogc;
915 /*******************************************************************************
917 * :: Internal client information (Tox address/id)
919 ******************************************************************************/
923 * Writes the Tox friend address of the client to a byte array. The address is
924 * not in human-readable format. If a client wants to display the address,
925 * formatting is required.
927 * @param address A memory region of at least TOX_ADDRESS_SIZE bytes. If this
928 * parameter is NULL, this function has no effect.
929 * @see TOX_ADDRESS_SIZE for the address format.
931 void tox_self_get_address(const(Tox)* tox, void* address) @nogc;
934 * Set the 4-byte nospam part of the address. This value is expected in host
935 * byte order. I.e. 0x12345678 will form the bytes [12, 34, 56, 78] in the
936 * nospam part of the Tox friend address.
938 * @param nospam Any 32 bit unsigned integer.
940 void tox_self_set_nospam(Tox* tox, uint nospam) @nogc;
943 * Get the 4-byte nospam part of the address. This value is returned in host
944 * byte order.
946 uint tox_self_get_nospam(const(Tox)* tox) @nogc;
949 * Copy the Tox Public Key (long term) from the Tox object.
951 * @param public_key A memory region of at least TOX_PUBLIC_KEY_SIZE bytes. If
952 * this parameter is NULL, this function has no effect.
954 void tox_self_get_public_key(const(Tox)* tox, void* public_key) @nogc;
957 * Copy the Tox Secret Key from the Tox object.
959 * @param secret_key A memory region of at least TOX_SECRET_KEY_SIZE bytes. If
960 * this parameter is NULL, this function has no effect.
962 void tox_self_get_secret_key(const(Tox)* tox, void* secret_key) @nogc;
965 /*******************************************************************************
967 * :: User-visible client information (nickname/status)
969 ******************************************************************************/
973 * Common error codes for all functions that set a piece of user-visible
974 * client information.
976 alias TOX_ERR_SET_INFO = int;
977 enum : int {
979 * The function returned successfully.
981 TOX_ERR_SET_INFO_OK,
984 * One of the arguments to the function was NULL when it was not expected.
986 TOX_ERR_SET_INFO_NULL,
989 * Information length exceeded maximum permissible size.
991 TOX_ERR_SET_INFO_TOO_LONG,
996 * Set the nickname for the Tox client.
998 * Nickname length cannot exceed TOX_MAX_NAME_LENGTH. If length is 0, the name
999 * parameter is ignored (it can be NULL), and the nickname is set back to empty.
1001 * @param name A byte array containing the new nickname.
1002 * @param length The size of the name byte array.
1004 * @return true on success.
1006 bool tox_self_set_name(Tox* tox, const(void)* name, usize length, TOX_ERR_SET_INFO* error=null) @nogc;
1009 * Return the length of the current nickname as passed to tox_self_set_name.
1011 * If no nickname was set before calling this function, the name is empty,
1012 * and this function returns 0.
1014 * @see threading for concurrency implications.
1016 usize tox_self_get_name_size(const(Tox)* tox) @nogc;
1019 * Write the nickname set by tox_self_set_name to a byte array.
1021 * If no nickname was set before calling this function, the name is empty,
1022 * and this function has no effect.
1024 * Call tox_self_get_name_size to find out how much memory to allocate for
1025 * the result.
1027 * @param name A valid memory location large enough to hold the nickname.
1028 * If this parameter is NULL, the function has no effect.
1030 void tox_self_get_name(const(Tox)* tox, void* name) @nogc;
1033 * Set the client's status message.
1035 * Status message length cannot exceed TOX_MAX_STATUS_MESSAGE_LENGTH. If
1036 * length is 0, the status parameter is ignored (it can be NULL), and the
1037 * user status is set back to empty.
1039 bool tox_self_set_status_message(Tox* tox, const(void)* status_message, usize length, TOX_ERR_SET_INFO* error=null) @nogc;
1042 * Return the length of the current status message as passed to tox_self_set_status_message.
1044 * If no status message was set before calling this function, the status
1045 * is empty, and this function returns 0.
1047 * @see threading for concurrency implications.
1049 usize tox_self_get_status_message_size(const(Tox)* tox) @nogc;
1052 * Write the status message set by tox_self_set_status_message to a byte array.
1054 * If no status message was set before calling this function, the status is
1055 * empty, and this function has no effect.
1057 * Call tox_self_get_status_message_size to find out how much memory to allocate for
1058 * the result.
1060 * @param status_message A valid memory location large enough to hold the
1061 * status message. If this parameter is NULL, the function has no effect.
1063 void tox_self_get_status_message(const(Tox)* tox, void* status_message) @nogc;
1066 * Set the client's user status.
1068 * @param status One of the user statuses listed in the enumeration above.
1070 void tox_self_set_status(Tox* tox, TOX_USER_STATUS status) @nogc;
1073 * Returns the client's user status.
1075 TOX_USER_STATUS tox_self_get_status(const(Tox)* tox) @nogc;
1078 /*******************************************************************************
1080 * :: Friend list management
1082 ******************************************************************************/
1085 alias TOX_ERR_FRIEND_ADD = int;
1086 enum : int {
1088 * The function returned successfully.
1090 TOX_ERR_FRIEND_ADD_OK,
1093 * One of the arguments to the function was NULL when it was not expected.
1095 TOX_ERR_FRIEND_ADD_NULL,
1098 * The length of the friend request message exceeded
1099 * TOX_MAX_FRIEND_REQUEST_LENGTH.
1101 TOX_ERR_FRIEND_ADD_TOO_LONG,
1104 * The friend request message was empty. This, and the TOO_LONG code will
1105 * never be returned from tox_friend_add_norequest.
1107 TOX_ERR_FRIEND_ADD_NO_MESSAGE,
1110 * The friend address belongs to the sending client.
1112 TOX_ERR_FRIEND_ADD_OWN_KEY,
1115 * A friend request has already been sent, or the address belongs to a friend
1116 * that is already on the friend list.
1118 TOX_ERR_FRIEND_ADD_ALREADY_SENT,
1121 * The friend address checksum failed.
1123 TOX_ERR_FRIEND_ADD_BAD_CHECKSUM,
1126 * The friend was already there, but the nospam value was different.
1128 TOX_ERR_FRIEND_ADD_SET_NEW_NOSPAM,
1131 * A memory allocation failed when trying to increase the friend list size.
1133 TOX_ERR_FRIEND_ADD_MALLOC,
1138 * Add a friend to the friend list and send a friend request.
1140 * A friend request message must be at least 1 byte long and at most
1141 * TOX_MAX_FRIEND_REQUEST_LENGTH.
1143 * Friend numbers are unique identifiers used in all functions that operate on
1144 * friends. Once added, a friend number is stable for the lifetime of the Tox
1145 * object. After saving the state and reloading it, the friend numbers may not
1146 * be the same as before. Deleting a friend creates a gap in the friend number
1147 * set, which is filled by the next adding of a friend. Any pattern in friend
1148 * numbers should not be relied on.
1150 * If more than INT32_MAX friends are added, this function causes undefined
1151 * behaviour.
1153 * @param address The address of the friend (returned by tox_self_get_address of
1154 * the friend you wish to add) it must be TOX_ADDRESS_SIZE bytes.
1155 * @param message The message that will be sent along with the friend request.
1156 * @param length The length of the data byte array.
1158 * @return the friend number on success, UINT32_MAX on failure.
1160 uint tox_friend_add(Tox* tox, const(void)* address, const(void)* message, usize length, TOX_ERR_FRIEND_ADD* error=null) @nogc;
1163 * Add a friend without sending a friend request.
1165 * This function is used to add a friend in response to a friend request. If the
1166 * client receives a friend request, it can be reasonably sure that the other
1167 * client added this client as a friend, eliminating the need for a friend
1168 * request.
1170 * This function is also useful in a situation where both instances are
1171 * controlled by the same entity, so that this entity can perform the mutual
1172 * friend adding. In this case, there is no need for a friend request, either.
1174 * @param public_key A byte array of length TOX_PUBLIC_KEY_SIZE containing the
1175 * Public Key (not the Address) of the friend to add.
1177 * @return the friend number on success, UINT32_MAX on failure.
1178 * @see tox_friend_add for a more detailed description of friend numbers.
1180 uint tox_friend_add_norequest(Tox* tox, const(void)* public_key, TOX_ERR_FRIEND_ADD* error=null) @nogc;
1183 alias TOX_ERR_FRIEND_DELETE = int;
1184 enum : int {
1186 * The function returned successfully.
1188 TOX_ERR_FRIEND_DELETE_OK,
1191 * There was no friend with the given friend number. No friends were deleted.
1193 TOX_ERR_FRIEND_DELETE_FRIEND_NOT_FOUND,
1198 * Remove a friend from the friend list.
1200 * This does not notify the friend of their deletion. After calling this
1201 * function, this client will appear offline to the friend and no communication
1202 * can occur between the two.
1204 * @param friend_number Friend number for the friend to be deleted.
1206 * @return true on success.
1208 bool tox_friend_delete(Tox* tox, uint friend_number, TOX_ERR_FRIEND_DELETE* error=null) @nogc;
1211 /*******************************************************************************
1213 * :: Friend list queries
1215 ******************************************************************************/
1218 alias TOX_ERR_FRIEND_BY_PUBLIC_KEY = int;
1219 enum : int {
1221 * The function returned successfully.
1223 TOX_ERR_FRIEND_BY_PUBLIC_KEY_OK,
1226 * One of the arguments to the function was NULL when it was not expected.
1228 TOX_ERR_FRIEND_BY_PUBLIC_KEY_NULL,
1231 * No friend with the given Public Key exists on the friend list.
1233 TOX_ERR_FRIEND_BY_PUBLIC_KEY_NOT_FOUND,
1238 * Return the friend number associated with that Public Key.
1240 * @return the friend number on success, UINT32_MAX on failure.
1241 * @param public_key A byte array containing the Public Key.
1243 uint tox_friend_by_public_key(const(Tox)* tox, const(void)* public_key, TOX_ERR_FRIEND_BY_PUBLIC_KEY* error=null) @nogc;
1246 * Checks if a friend with the given friend number exists and returns true if
1247 * it does.
1249 bool tox_friend_exists(const(Tox)* tox, uint friend_number) @nogc;
1252 * Return the number of friends on the friend list.
1254 * This function can be used to determine how much memory to allocate for
1255 * tox_self_get_friend_list.
1257 usize tox_self_get_friend_list_size(const(Tox)* tox) @nogc;
1260 * Copy a list of valid friend numbers into an array.
1262 * Call tox_self_get_friend_list_size to determine the number of elements to allocate.
1264 * @param friend_list A memory region with enough space to hold the friend
1265 * list. If this parameter is NULL, this function has no effect.
1267 void tox_self_get_friend_list(const(Tox)* tox, uint *friend_list) @nogc;
1270 alias TOX_ERR_FRIEND_GET_PUBLIC_KEY = int;
1271 enum : int {
1273 * The function returned successfully.
1275 TOX_ERR_FRIEND_GET_PUBLIC_KEY_OK,
1278 * No friend with the given number exists on the friend list.
1280 TOX_ERR_FRIEND_GET_PUBLIC_KEY_FRIEND_NOT_FOUND,
1285 * Copies the Public Key associated with a given friend number to a byte array.
1287 * @param friend_number The friend number you want the Public Key of.
1288 * @param public_key A memory region of at least TOX_PUBLIC_KEY_SIZE bytes. If
1289 * this parameter is NULL, this function has no effect.
1291 * @return true on success.
1293 bool tox_friend_get_public_key(const(Tox)* tox, uint friend_number, void* public_key, TOX_ERR_FRIEND_GET_PUBLIC_KEY* error=null) @nogc;
1296 alias TOX_ERR_FRIEND_GET_LAST_ONLINE = int;
1297 enum : int {
1299 * The function returned successfully.
1301 TOX_ERR_FRIEND_GET_LAST_ONLINE_OK,
1304 * No friend with the given number exists on the friend list.
1306 TOX_ERR_FRIEND_GET_LAST_ONLINE_FRIEND_NOT_FOUND,
1311 * Return a unix-time timestamp of the last time the friend associated with a given
1312 * friend number was seen online. This function will return UINT64_MAX on error.
1314 * @param friend_number The friend number you want to query.
1316 ulong tox_friend_get_last_online(const(Tox)* tox, uint friend_number, TOX_ERR_FRIEND_GET_LAST_ONLINE* error=null) @nogc;
1319 /*******************************************************************************
1321 * :: Friend-specific state queries (can also be received through callbacks)
1323 ******************************************************************************/
1327 * Common error codes for friend state query functions.
1329 alias TOX_ERR_FRIEND_QUERY = int;
1330 enum : int {
1332 * The function returned successfully.
1334 TOX_ERR_FRIEND_QUERY_OK,
1337 * The pointer parameter for storing the query result (name, message) was
1338 * NULL. Unlike the `_self_` variants of these functions, which have no effect
1339 * when a parameter is NULL, these functions return an error in that case.
1341 TOX_ERR_FRIEND_QUERY_NULL,
1344 * The friend_number did not designate a valid friend.
1346 TOX_ERR_FRIEND_QUERY_FRIEND_NOT_FOUND,
1351 * Return the length of the friend's name. If the friend number is invalid, the
1352 * return value is unspecified.
1354 * The return value is equal to the `length` argument received by the last
1355 * `friend_name` callback.
1357 usize tox_friend_get_name_size(const(Tox)* tox, uint friend_number, TOX_ERR_FRIEND_QUERY* error=null) @nogc;
1360 * Write the name of the friend designated by the given friend number to a byte
1361 * array.
1363 * Call tox_friend_get_name_size to determine the allocation size for the `name`
1364 * parameter.
1366 * The data written to `name` is equal to the data received by the last
1367 * `friend_name` callback.
1369 * @param name A valid memory region large enough to store the friend's name.
1371 * @return true on success.
1373 bool tox_friend_get_name(const(Tox)* tox, uint friend_number, void* name, TOX_ERR_FRIEND_QUERY* error=null) @nogc;
1376 * @param friend_number The friend number of the friend whose name changed.
1377 * @param name A byte array containing the same data as
1378 * tox_friend_get_name would write to its `name` parameter.
1379 * @param length A value equal to the return value of
1380 * tox_friend_get_name_size.
1382 alias tox_friend_name_cb = void function (Tox* tox, uint friend_number, const(char)* name, usize length, void* user_data) nothrow @system;
1386 * Set the callback for the `friend_name` event. Pass NULL to unset.
1388 * This event is triggered when a friend changes their name.
1390 void tox_callback_friend_name(Tox* tox, tox_friend_name_cb callback) @nogc;
1393 * Return the length of the friend's status message. If the friend number is
1394 * invalid, the return value is SIZE_MAX.
1396 usize tox_friend_get_status_message_size(const(Tox)* tox, uint friend_number, TOX_ERR_FRIEND_QUERY* error=null) @nogc;
1399 * Write the status message of the friend designated by the given friend number to a byte
1400 * array.
1402 * Call tox_friend_get_status_message_size to determine the allocation size for the `status_name`
1403 * parameter.
1405 * The data written to `status_message` is equal to the data received by the last
1406 * `friend_status_message` callback.
1408 * @param status_message A valid memory region large enough to store the friend's status message.
1410 bool tox_friend_get_status_message(const(Tox)* tox, uint friend_number, void* status_message, TOX_ERR_FRIEND_QUERY* error=null) @nogc;
1413 * @param friend_number The friend number of the friend whose status message
1414 * changed.
1415 * @param message A byte array containing the same data as
1416 * tox_friend_get_status_message would write to its `status_message` parameter.
1417 * @param length A value equal to the return value of
1418 * tox_friend_get_status_message_size.
1420 alias tox_friend_status_message_cb = void function (Tox* tox, uint friend_number, const(char)* message, usize length, void* user_data) nothrow @system;
1424 * Set the callback for the `friend_status_message` event. Pass NULL to unset.
1426 * This event is triggered when a friend changes their status message.
1428 void tox_callback_friend_status_message(Tox* tox, tox_friend_status_message_cb callback) @nogc;
1431 * Return the friend's user status (away/busy/...). If the friend number is
1432 * invalid, the return value is unspecified.
1434 * The status returned is equal to the last status received through the
1435 * `friend_status` callback.
1437 TOX_USER_STATUS tox_friend_get_status(const(Tox)* tox, uint friend_number, TOX_ERR_FRIEND_QUERY* error=null) @nogc;
1440 * @param friend_number The friend number of the friend whose user status
1441 * changed.
1442 * @param status The new user status.
1444 alias tox_friend_status_cb = void function (Tox* tox, uint friend_number, TOX_USER_STATUS status, void* user_data) nothrow @system;
1448 * Set the callback for the `friend_status` event. Pass NULL to unset.
1450 * This event is triggered when a friend changes their user status.
1452 void tox_callback_friend_status(Tox* tox, tox_friend_status_cb callback) @nogc;
1455 * Check whether a friend is currently connected to this client.
1457 * The result of this function is equal to the last value received by the
1458 * `friend_connection_status` callback.
1460 * @param friend_number The friend number for which to query the connection
1461 * status.
1463 * @return the friend's connection status as it was received through the
1464 * `friend_connection_status` event.
1466 TOX_CONNECTION tox_friend_get_connection_status(const(Tox)* tox, uint friend_number, TOX_ERR_FRIEND_QUERY* error=null) @nogc;
1469 * @param friend_number The friend number of the friend whose connection status
1470 * changed.
1471 * @param connection_status The result of calling
1472 * tox_friend_get_connection_status on the passed friend_number.
1474 alias tox_friend_connection_status_cb = void function (Tox* tox, uint friend_number, TOX_CONNECTION connection_status, void* user_data) nothrow @system;
1478 * Set the callback for the `friend_connection_status` event. Pass NULL to unset.
1480 * This event is triggered when a friend goes offline after having been online,
1481 * or when a friend goes online.
1483 * This callback is not called when adding friends. It is assumed that when
1484 * adding friends, their connection status is initially offline.
1486 void tox_callback_friend_connection_status(Tox* tox, tox_friend_connection_status_cb callback) @nogc;
1489 * Check whether a friend is currently typing a message.
1491 * @param friend_number The friend number for which to query the typing status.
1493 * @return true if the friend is typing.
1494 * @return false if the friend is not typing, or the friend number was
1495 * invalid. Inspect the error code to determine which case it is.
1497 bool tox_friend_get_typing(const(Tox)* tox, uint friend_number, TOX_ERR_FRIEND_QUERY* error=null) @nogc;
1500 * @param friend_number The friend number of the friend who started or stopped
1501 * typing.
1502 * @param is_typing The result of calling tox_friend_get_typing on the passed
1503 * friend_number.
1505 alias tox_friend_typing_cb = void function (Tox* tox, uint friend_number, bool is_typing, void* user_data) nothrow @system;
1509 * Set the callback for the `friend_typing` event. Pass NULL to unset.
1511 * This event is triggered when a friend starts or stops typing.
1513 void tox_callback_friend_typing(Tox* tox, tox_friend_typing_cb callback) @nogc;
1516 /*******************************************************************************
1518 * :: Sending private messages
1520 ******************************************************************************/
1523 alias TOX_ERR_SET_TYPING = int;
1524 enum : int {
1526 * The function returned successfully.
1528 TOX_ERR_SET_TYPING_OK,
1531 * The friend number did not designate a valid friend.
1533 TOX_ERR_SET_TYPING_FRIEND_NOT_FOUND,
1538 * Set the client's typing status for a friend.
1540 * The client is responsible for turning it on or off.
1542 * @param friend_number The friend to which the client is typing a message.
1543 * @param typing The typing status. True means the client is typing.
1545 * @return true on success.
1547 bool tox_self_set_typing(Tox* tox, uint friend_number, bool typing, TOX_ERR_SET_TYPING* error=null) @nogc;
1550 alias TOX_ERR_FRIEND_SEND_MESSAGE = int;
1551 enum : int {
1553 * The function returned successfully.
1555 TOX_ERR_FRIEND_SEND_MESSAGE_OK,
1558 * One of the arguments to the function was NULL when it was not expected.
1560 TOX_ERR_FRIEND_SEND_MESSAGE_NULL,
1563 * The friend number did not designate a valid friend.
1565 TOX_ERR_FRIEND_SEND_MESSAGE_FRIEND_NOT_FOUND,
1568 * This client is currently not connected to the friend.
1570 TOX_ERR_FRIEND_SEND_MESSAGE_FRIEND_NOT_CONNECTED,
1573 * An allocation error occurred while increasing the send queue size.
1575 TOX_ERR_FRIEND_SEND_MESSAGE_SENDQ,
1578 * Message length exceeded TOX_MAX_MESSAGE_LENGTH.
1580 TOX_ERR_FRIEND_SEND_MESSAGE_TOO_LONG,
1583 * Attempted to send a zero-length message.
1585 TOX_ERR_FRIEND_SEND_MESSAGE_EMPTY,
1590 * Send a text chat message to an online friend.
1592 * This function creates a chat message packet and pushes it into the send
1593 * queue.
1595 * The message length may not exceed TOX_MAX_MESSAGE_LENGTH. Larger messages
1596 * must be split by the client and sent as separate messages. Other clients can
1597 * then reassemble the fragments. Messages may not be empty.
1599 * The return value of this function is the message ID. If a read receipt is
1600 * received, the triggered `friend_read_receipt` event will be passed this message ID.
1602 * Message IDs are unique per friend. The first message ID is 0. Message IDs are
1603 * incremented by 1 each time a message is sent. If UINT32_MAX messages were
1604 * sent, the next message ID is 0.
1606 * @param type Message type (normal, action, ...).
1607 * @param friend_number The friend number of the friend to send the message to.
1608 * @param message A non-NULL pointer to the first element of a byte array
1609 * containing the message text.
1610 * @param length Length of the message to be sent.
1612 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;
1615 * @param friend_number The friend number of the friend who received the message.
1616 * @param message_id The message ID as returned from tox_friend_send_message
1617 * corresponding to the message sent.
1619 alias tox_friend_read_receipt_cb = void function (Tox* tox, uint friend_number, uint message_id, void* user_data) nothrow @system;
1623 * Set the callback for the `friend_read_receipt` event. Pass NULL to unset.
1625 * This event is triggered when the friend receives the message sent with
1626 * tox_friend_send_message with the corresponding message ID.
1628 void tox_callback_friend_read_receipt(Tox* tox, tox_friend_read_receipt_cb callback) @nogc;
1631 /*******************************************************************************
1633 * :: Receiving private messages and friend requests
1635 ******************************************************************************/
1640 * @param public_key The Public Key of the user who sent the friend request.
1641 * @param message The message they sent along with the request.
1642 * @param length The size of the message byte array.
1644 alias tox_friend_request_cb = void function (Tox* tox, const(ubyte)* public_key, const(char)* message, usize length, void* user_data) nothrow @system;
1648 * Set the callback for the `friend_request` event. Pass NULL to unset.
1650 * This event is triggered when a friend request is received.
1652 void tox_callback_friend_request(Tox* tox, tox_friend_request_cb callback) @nogc;
1655 * @param friend_number The friend number of the friend who sent the message.
1656 * @param message The message data they sent.
1657 * @param length The size of the message byte array.
1659 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;
1663 * Set the callback for the `friend_message` event. Pass NULL to unset.
1665 * This event is triggered when a message from a friend is received.
1667 void tox_callback_friend_message(Tox* tox, tox_friend_message_cb callback) @nogc;
1670 /*******************************************************************************
1672 * :: File transmission: common between sending and receiving
1674 ******************************************************************************/
1679 * Generates a cryptographic hash of the given data.
1681 * This function may be used by clients for any purpose, but is provided
1682 * primarily for validating cached avatars. This use is highly recommended to
1683 * avoid unnecessary avatar updates.
1685 * If hash is NULL or data is NULL while length is not 0 the function returns false,
1686 * otherwise it returns true.
1688 * This function is a wrapper to internal message-digest functions.
1690 * @param hash A valid memory location the hash data. It must be at least
1691 * TOX_HASH_LENGTH bytes in size.
1692 * @param data Data to be hashed or NULL.
1693 * @param length Size of the data array or 0.
1695 * @return true if hash was not NULL.
1697 bool tox_hash(void* hash, const(void)* data, usize length) @nogc;
1700 alias TOX_FILE_KIND = uint;
1701 enum : uint {
1703 * Arbitrary file data. Clients can choose to handle it based on the file name
1704 * or magic or any other way they choose.
1706 TOX_FILE_KIND_DATA,
1709 * Avatar file_id. This consists of tox_hash(image).
1710 * Avatar data. This consists of the image data.
1712 * Avatars can be sent at any time the client wishes. Generally, a client will
1713 * send the avatar to a friend when that friend comes online, and to all
1714 * friends when the avatar changed. A client can save some traffic by
1715 * remembering which friend received the updated avatar already and only send
1716 * it if the friend has an out of date avatar.
1718 * Clients who receive avatar send requests can reject it (by sending
1719 * TOX_FILE_CONTROL_CANCEL before any other controls), or accept it (by
1720 * sending TOX_FILE_CONTROL_RESUME). The file_id of length TOX_HASH_LENGTH bytes
1721 * (same length as TOX_FILE_ID_LENGTH) will contain the hash. A client can compare
1722 * this hash with a saved hash and send TOX_FILE_CONTROL_CANCEL to terminate the avatar
1723 * transfer if it matches.
1725 * When file_size is set to 0 in the transfer request it means that the client
1726 * has no avatar.
1728 TOX_FILE_KIND_AVATAR,
1732 alias TOX_FILE_CONTROL = int;
1733 enum : int {
1735 * Sent by the receiving side to accept a file send request. Also sent after a
1736 * TOX_FILE_CONTROL_PAUSE command to continue sending or receiving.
1738 TOX_FILE_CONTROL_RESUME,
1741 * Sent by clients to pause the file transfer. The initial state of a file
1742 * transfer is always paused on the receiving side and running on the sending
1743 * side. If both the sending and receiving side pause the transfer, then both
1744 * need to send TOX_FILE_CONTROL_RESUME for the transfer to resume.
1746 TOX_FILE_CONTROL_PAUSE,
1749 * Sent by the receiving side to reject a file send request before any other
1750 * commands are sent. Also sent by either side to terminate a file transfer.
1752 TOX_FILE_CONTROL_CANCEL,
1756 alias TOX_ERR_FILE_CONTROL = int;
1757 enum : int {
1759 * The function returned successfully.
1761 TOX_ERR_FILE_CONTROL_OK,
1764 * The friend_number passed did not designate a valid friend.
1766 TOX_ERR_FILE_CONTROL_FRIEND_NOT_FOUND,
1769 * This client is currently not connected to the friend.
1771 TOX_ERR_FILE_CONTROL_FRIEND_NOT_CONNECTED,
1774 * No file transfer with the given file number was found for the given friend.
1776 TOX_ERR_FILE_CONTROL_NOT_FOUND,
1779 * A RESUME control was sent, but the file transfer is running normally.
1781 TOX_ERR_FILE_CONTROL_NOT_PAUSED,
1784 * A RESUME control was sent, but the file transfer was paused by the other
1785 * party. Only the party that paused the transfer can resume it.
1787 TOX_ERR_FILE_CONTROL_DENIED,
1790 * A PAUSE control was sent, but the file transfer was already paused.
1792 TOX_ERR_FILE_CONTROL_ALREADY_PAUSED,
1795 * Packet queue is full.
1797 TOX_ERR_FILE_CONTROL_SENDQ,
1802 * Sends a file control command to a friend for a given file transfer.
1804 * @param friend_number The friend number of the friend the file is being
1805 * transferred to or received from.
1806 * @param file_number The friend-specific identifier for the file transfer.
1807 * @param control The control command to send.
1809 * @return true on success.
1811 bool tox_file_control(Tox* tox, uint friend_number, uint file_number, TOX_FILE_CONTROL control, TOX_ERR_FILE_CONTROL* error=null) @nogc;
1814 * When receiving TOX_FILE_CONTROL_CANCEL, the client should release the
1815 * resources associated with the file number and consider the transfer failed.
1817 * @param friend_number The friend number of the friend who is sending the file.
1818 * @param file_number The friend-specific file number the data received is
1819 * associated with.
1820 * @param control The file control command received.
1822 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;
1826 * Set the callback for the `file_recv_control` event. Pass NULL to unset.
1828 * This event is triggered when a file control command is received from a
1829 * friend.
1831 void tox_callback_file_recv_control(Tox* tox, tox_file_recv_control_cb callback) @nogc;
1834 alias TOX_ERR_FILE_SEEK = int;
1835 enum : int {
1837 * The function returned successfully.
1839 TOX_ERR_FILE_SEEK_OK,
1842 * The friend_number passed did not designate a valid friend.
1844 TOX_ERR_FILE_SEEK_FRIEND_NOT_FOUND,
1847 * This client is currently not connected to the friend.
1849 TOX_ERR_FILE_SEEK_FRIEND_NOT_CONNECTED,
1852 * No file transfer with the given file number was found for the given friend.
1854 TOX_ERR_FILE_SEEK_NOT_FOUND,
1857 * File was not in a state where it could be seeked.
1859 TOX_ERR_FILE_SEEK_DENIED,
1862 * Seek position was invalid
1864 TOX_ERR_FILE_SEEK_INVALID_POSITION,
1867 * Packet queue is full.
1869 TOX_ERR_FILE_SEEK_SENDQ,
1874 * Sends a file seek control command to a friend for a given file transfer.
1876 * This function can only be called to resume a file transfer right before
1877 * TOX_FILE_CONTROL_RESUME is sent.
1879 * @param friend_number The friend number of the friend the file is being
1880 * received from.
1881 * @param file_number The friend-specific identifier for the file transfer.
1882 * @param position The position that the file should be seeked to.
1884 bool tox_file_seek(Tox* tox, uint friend_number, uint file_number, ulong position, TOX_ERR_FILE_SEEK* error=null) @nogc;
1887 alias TOX_ERR_FILE_GET = int;
1888 enum : int {
1890 * The function returned successfully.
1892 TOX_ERR_FILE_GET_OK,
1895 * One of the arguments to the function was NULL when it was not expected.
1897 TOX_ERR_FILE_GET_NULL,
1900 * The friend_number passed did not designate a valid friend.
1902 TOX_ERR_FILE_GET_FRIEND_NOT_FOUND,
1905 * No file transfer with the given file number was found for the given friend.
1907 TOX_ERR_FILE_GET_NOT_FOUND,
1912 * Copy the file id associated to the file transfer to a byte array.
1914 * @param friend_number The friend number of the friend the file is being
1915 * transferred to or received from.
1916 * @param file_number The friend-specific identifier for the file transfer.
1917 * @param file_id A memory region of at least TOX_FILE_ID_LENGTH bytes. If
1918 * this parameter is NULL, this function has no effect.
1920 * @return true on success.
1922 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;
1925 /*******************************************************************************
1927 * :: File transmission: sending
1929 ******************************************************************************/
1932 alias TOX_ERR_FILE_SEND = int;
1933 enum : int {
1935 * The function returned successfully.
1937 TOX_ERR_FILE_SEND_OK,
1940 * One of the arguments to the function was NULL when it was not expected.
1942 TOX_ERR_FILE_SEND_NULL,
1945 * The friend_number passed did not designate a valid friend.
1947 TOX_ERR_FILE_SEND_FRIEND_NOT_FOUND,
1950 * This client is currently not connected to the friend.
1952 TOX_ERR_FILE_SEND_FRIEND_NOT_CONNECTED,
1955 * Filename length exceeded TOX_MAX_FILENAME_LENGTH bytes.
1957 TOX_ERR_FILE_SEND_NAME_TOO_LONG,
1960 * Too many ongoing transfers. The maximum number of concurrent file transfers
1961 * is 256 per friend per direction (sending and receiving).
1963 TOX_ERR_FILE_SEND_TOO_MANY,
1968 * Send a file transmission request.
1970 * Maximum filename length is TOX_MAX_FILENAME_LENGTH bytes. The filename
1971 * should generally just be a file name, not a path with directory names.
1973 * If a non-UINT64_MAX file size is provided, it can be used by both sides to
1974 * determine the sending progress. File size can be set to UINT64_MAX for streaming
1975 * data of unknown size.
1977 * File transmission occurs in chunks, which are requested through the
1978 * `file_chunk_request` event.
1980 * When a friend goes offline, all file transfers associated with the friend are
1981 * purged from core.
1983 * If the file contents change during a transfer, the behaviour is unspecified
1984 * in general. What will actually happen depends on the mode in which the file
1985 * was modified and how the client determines the file size.
1987 * - If the file size was increased
1988 * - and sending mode was streaming (file_size = UINT64_MAX), the behaviour
1989 * will be as expected.
1990 * - and sending mode was file (file_size != UINT64_MAX), the
1991 * file_chunk_request callback will receive length = 0 when Core thinks
1992 * the file transfer has finished. If the client remembers the file size as
1993 * it was when sending the request, it will terminate the transfer normally.
1994 * If the client re-reads the size, it will think the friend cancelled the
1995 * transfer.
1996 * - If the file size was decreased
1997 * - and sending mode was streaming, the behaviour is as expected.
1998 * - and sending mode was file, the callback will return 0 at the new
1999 * (earlier) end-of-file, signalling to the friend that the transfer was
2000 * cancelled.
2001 * - If the file contents were modified
2002 * - at a position before the current read, the two files (local and remote)
2003 * will differ after the transfer terminates.
2004 * - at a position after the current read, the file transfer will succeed as
2005 * expected.
2006 * - In either case, both sides will regard the transfer as complete and
2007 * successful.
2009 * @param friend_number The friend number of the friend the file send request
2010 * should be sent to.
2011 * @param kind The meaning of the file to be sent.
2012 * @param file_size Size in bytes of the file the client wants to send, UINT64_MAX if
2013 * unknown or streaming.
2014 * @param file_id A file identifier of length TOX_FILE_ID_LENGTH that can be used to
2015 * uniquely identify file transfers across core restarts. If NULL, a random one will
2016 * be generated by core. It can then be obtained by using tox_file_get_file_id().
2017 * @param filename Name of the file. Does not need to be the actual name. This
2018 * name will be sent along with the file send request.
2019 * @param filename_length Size in bytes of the filename.
2021 * @return A file number used as an identifier in subsequent callbacks. This
2022 * number is per friend. File numbers are reused after a transfer terminates.
2023 * On failure, this function returns UINT32_MAX. Any pattern in file numbers
2024 * should not be relied on.
2026 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;
2029 alias TOX_ERR_FILE_SEND_CHUNK = int;
2030 enum : int {
2032 * The function returned successfully.
2034 TOX_ERR_FILE_SEND_CHUNK_OK,
2037 * The length parameter was non-zero, but data was NULL.
2039 TOX_ERR_FILE_SEND_CHUNK_NULL,
2042 * The friend_number passed did not designate a valid friend.
2044 TOX_ERR_FILE_SEND_CHUNK_FRIEND_NOT_FOUND,
2047 * This client is currently not connected to the friend.
2049 TOX_ERR_FILE_SEND_CHUNK_FRIEND_NOT_CONNECTED,
2052 * No file transfer with the given file number was found for the given friend.
2054 TOX_ERR_FILE_SEND_CHUNK_NOT_FOUND,
2057 * File transfer was found but isn't in a transferring state: (paused, done,
2058 * broken, etc...) (happens only when not called from the request chunk callback).
2060 TOX_ERR_FILE_SEND_CHUNK_NOT_TRANSFERRING,
2063 * Attempted to send more or less data than requested. The requested data size is
2064 * adjusted according to maximum transmission unit and the expected end of
2065 * the file. Trying to send less or more than requested will return this error.
2067 TOX_ERR_FILE_SEND_CHUNK_INVALID_LENGTH,
2070 * Packet queue is full.
2072 TOX_ERR_FILE_SEND_CHUNK_SENDQ,
2075 * Position parameter was wrong.
2077 TOX_ERR_FILE_SEND_CHUNK_WRONG_POSITION,
2082 * Send a chunk of file data to a friend.
2084 * This function is called in response to the `file_chunk_request` callback. The
2085 * length parameter should be equal to the one received though the callback.
2086 * If it is zero, the transfer is assumed complete. For files with known size,
2087 * Core will know that the transfer is complete after the last byte has been
2088 * received, so it is not necessary (though not harmful) to send a zero-length
2089 * chunk to terminate. For streams, core will know that the transfer is finished
2090 * if a chunk with length less than the length requested in the callback is sent.
2092 * @param friend_number The friend number of the receiving friend for this file.
2093 * @param file_number The file transfer identifier returned by tox_file_send.
2094 * @param position The file or stream position from which to continue reading.
2095 * @return true on success.
2097 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;
2100 * If the length parameter is 0, the file transfer is finished, and the client's
2101 * resources associated with the file number should be released. After a call
2102 * with zero length, the file number can be reused for future file transfers.
2104 * If the requested position is not equal to the client's idea of the current
2105 * file or stream position, it will need to seek. In case of read-once streams,
2106 * the client should keep the last read chunk so that a seek back can be
2107 * supported. A seek-back only ever needs to read from the last requested chunk.
2108 * This happens when a chunk was requested, but the send failed. A seek-back
2109 * request can occur an arbitrary number of times for any given chunk.
2111 * In response to receiving this callback, the client should call the function
2112 * `tox_file_send_chunk` with the requested chunk. If the number of bytes sent
2113 * through that function is zero, the file transfer is assumed complete. A
2114 * client must send the full length of data requested with this callback.
2116 * @param friend_number The friend number of the receiving friend for this file.
2117 * @param file_number The file transfer identifier returned by tox_file_send.
2118 * @param position The file or stream position from which to continue reading.
2119 * @param length The number of bytes requested for the current chunk.
2121 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;
2125 * Set the callback for the `file_chunk_request` event. Pass NULL to unset.
2127 * This event is triggered when Core is ready to send more file data.
2129 void tox_callback_file_chunk_request(Tox* tox, tox_file_chunk_request_cb callback) @nogc;
2132 /*******************************************************************************
2134 * :: File transmission: receiving
2136 ******************************************************************************/
2140 * The client should acquire resources to be associated with the file transfer.
2141 * Incoming file transfers start in the PAUSED state. After this callback
2142 * returns, a transfer can be rejected by sending a TOX_FILE_CONTROL_CANCEL
2143 * control command before any other control commands. It can be accepted by
2144 * sending TOX_FILE_CONTROL_RESUME.
2146 * @param friend_number The friend number of the friend who is sending the file
2147 * transfer request.
2148 * @param file_number The friend-specific file number the data received is
2149 * associated with.
2150 * @param kind The meaning of the file to be sent.
2151 * @param file_size Size in bytes of the file the client wants to send,
2152 * UINT64_MAX if unknown or streaming.
2153 * @param filename Name of the file. Does not need to be the actual name. This
2154 * name will be sent along with the file send request.
2155 * @param filename_length Size in bytes of the filename.
2157 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;
2161 * Set the callback for the `file_recv` event. Pass NULL to unset.
2163 * This event is triggered when a file transfer request is received.
2165 void tox_callback_file_recv(Tox* tox, tox_file_recv_cb callback) @nogc;
2168 * When length is 0, the transfer is finished and the client should release the
2169 * resources it acquired for the transfer. After a call with length = 0, the
2170 * file number can be reused for new file transfers.
2172 * If position is equal to file_size (received in the file_receive callback)
2173 * when the transfer finishes, the file was received completely. Otherwise, if
2174 * file_size was UINT64_MAX, streaming ended successfully when length is 0.
2176 * @param friend_number The friend number of the friend who is sending the file.
2177 * @param file_number The friend-specific file number the data received is
2178 * associated with.
2179 * @param position The file position of the first byte in data.
2180 * @param data A byte array containing the received chunk.
2181 * @param length The length of the received chunk.
2183 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;
2187 * Set the callback for the `file_recv_chunk` event. Pass NULL to unset.
2189 * This event is first triggered when a file transfer request is received, and
2190 * subsequently when a chunk of file data for an accepted request was received.
2192 void tox_callback_file_recv_chunk(Tox* tox, tox_file_recv_chunk_cb callback) @nogc;
2195 /*******************************************************************************
2197 * :: Conference management
2199 ******************************************************************************/
2203 * Conference types for the conference_invite event.
2205 alias TOX_CONFERENCE_TYPE = int;
2206 enum : int {
2208 * Text-only conferences that must be accepted with the tox_conference_join function.
2210 TOX_CONFERENCE_TYPE_TEXT,
2213 * Video conference. The function to accept these is in toxav.
2215 TOX_CONFERENCE_TYPE_AV,
2220 * The invitation will remain valid until the inviting friend goes offline
2221 * or exits the conference.
2223 * @param friend_number The friend who invited us.
2224 * @param type The conference type (text only or audio/video).
2225 * @param cookie A piece of data of variable length required to join the
2226 * conference.
2227 * @param length The length of the cookie.
2229 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;
2233 * Set the callback for the `conference_invite` event. Pass NULL to unset.
2235 * This event is triggered when the client is invited to join a conference.
2237 void tox_callback_conference_invite(Tox* tox, tox_conference_invite_cb callback) @nogc;
2240 * @param conference_number The conference number of the conference the message is intended for.
2241 * @param peer_number The ID of the peer who sent the message.
2242 * @param type The type of message (normal, action, ...).
2243 * @param message The message data.
2244 * @param length The length of the message.
2246 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;
2250 * Set the callback for the `conference_message` event. Pass NULL to unset.
2252 * This event is triggered when the client receives a conference message.
2254 void tox_callback_conference_message(Tox* tox, tox_conference_message_cb callback) @nogc;
2257 * @param conference_number The conference number of the conference the title change is intended for.
2258 * @param peer_number The ID of the peer who changed the title.
2259 * @param title The title data.
2260 * @param length The title length.
2262 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;
2266 * Set the callback for the `conference_title` event. Pass NULL to unset.
2268 * This event is triggered when a peer changes the conference title.
2270 * If peer_number == UINT32_MAX, then author is unknown (e.g. initial joining the conference).
2272 void tox_callback_conference_title(Tox* tox, tox_conference_title_cb callback) @nogc;
2276 * Peer list state change types.
2278 alias TOX_CONFERENCE_STATE_CHANGE = int;
2279 enum : int {
2281 * A peer has joined the conference.
2283 TOX_CONFERENCE_STATE_CHANGE_PEER_JOIN,
2286 * A peer has exited the conference.
2288 TOX_CONFERENCE_STATE_CHANGE_PEER_EXIT,
2291 * A peer has changed their name.
2293 TOX_CONFERENCE_STATE_CHANGE_PEER_NAME_CHANGE,
2298 * @param conference_number The conference number of the conference the title change is intended for.
2299 * @param peer_number The ID of the peer who changed the title.
2300 * @param change The type of change (one of TOX_CONFERENCE_STATE_CHANGE).
2302 alias tox_conference_namelist_change_cb = void function (Tox* tox, uint conference_number, uint peer_number, TOX_CONFERENCE_STATE_CHANGE change, void* user_data) nothrow @system;
2306 * Set the callback for the `conference_namelist_change` event. Pass NULL to unset.
2308 * This event is triggered when the peer list changes (name change, peer join, peer exit).
2310 void tox_callback_conference_namelist_change(Tox* tox, tox_conference_namelist_change_cb callback) @nogc;
2313 alias TOX_ERR_CONFERENCE_NEW = int;
2314 enum : int {
2316 * The function returned successfully.
2318 TOX_ERR_CONFERENCE_NEW_OK,
2321 * The conference instance failed to initialize.
2323 TOX_ERR_CONFERENCE_NEW_INIT,
2328 * Creates a new conference.
2330 * This function creates a new text conference.
2332 * @return conference number on success, or UINT32_MAX on failure.
2334 uint tox_conference_new(Tox* tox, TOX_ERR_CONFERENCE_NEW* error=null) @nogc;
2337 alias TOX_ERR_CONFERENCE_DELETE = int;
2338 enum : int {
2340 * The function returned successfully.
2342 TOX_ERR_CONFERENCE_DELETE_OK,
2345 * The conference number passed did not designate a valid conference.
2347 TOX_ERR_CONFERENCE_DELETE_CONFERENCE_NOT_FOUND,
2352 * This function deletes a conference.
2354 * @param conference_number The conference number of the conference to be deleted.
2356 * @return true on success.
2358 bool tox_conference_delete(Tox* tox, uint conference_number, TOX_ERR_CONFERENCE_DELETE* error=null) @nogc;
2362 * Error codes for peer info queries.
2364 alias TOX_ERR_CONFERENCE_PEER_QUERY = int;
2365 enum : int {
2367 * The function returned successfully.
2369 TOX_ERR_CONFERENCE_PEER_QUERY_OK,
2372 * The conference number passed did not designate a valid conference.
2374 TOX_ERR_CONFERENCE_PEER_QUERY_CONFERENCE_NOT_FOUND,
2377 * The peer number passed did not designate a valid peer.
2379 TOX_ERR_CONFERENCE_PEER_QUERY_PEER_NOT_FOUND,
2382 * The client is not connected to the conference.
2384 TOX_ERR_CONFERENCE_PEER_QUERY_NO_CONNECTION,
2389 * Return the number of peers in the conference. Return value is unspecified on failure.
2391 uint tox_conference_peer_count(const(Tox)* tox, uint conference_number, TOX_ERR_CONFERENCE_PEER_QUERY* error=null) @nogc;
2394 * Return the length of the peer's name. Return value is unspecified on failure.
2396 usize tox_conference_peer_get_name_size(const(Tox)* tox, uint conference_number, uint peer_number, TOX_ERR_CONFERENCE_PEER_QUERY* error=null) @nogc;
2399 * Copy the name of peer_number who is in conference_number to name.
2400 * name must be at least TOX_MAX_NAME_LENGTH long.
2402 * @return true on success.
2404 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;
2407 * Copy the public key of peer_number who is in conference_number to public_key.
2408 * public_key must be TOX_PUBLIC_KEY_SIZE long.
2410 * @return true on success.
2412 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;
2415 * Return true if passed peer_number corresponds to our own.
2417 bool tox_conference_peer_number_is_ours(const(Tox)* tox, uint conference_number, uint peer_number, TOX_ERR_CONFERENCE_PEER_QUERY* error=null) @nogc;
2420 alias TOX_ERR_CONFERENCE_INVITE = int;
2421 enum : int {
2423 * The function returned successfully.
2425 TOX_ERR_CONFERENCE_INVITE_OK,
2428 * The conference number passed did not designate a valid conference.
2430 TOX_ERR_CONFERENCE_INVITE_CONFERENCE_NOT_FOUND,
2433 * The invite packet failed to send.
2435 TOX_ERR_CONFERENCE_INVITE_FAIL_SEND,
2440 * Invites a friend to a conference.
2442 * @param friend_number The friend number of the friend we want to invite.
2443 * @param conference_number The conference number of the conference we want to invite the friend to.
2445 * @return true on success.
2447 bool tox_conference_invite(Tox* tox, uint friend_number, uint conference_number, TOX_ERR_CONFERENCE_INVITE* error=null) @nogc;
2450 alias TOX_ERR_CONFERENCE_JOIN = int;
2451 enum : int {
2453 * The function returned successfully.
2455 TOX_ERR_CONFERENCE_JOIN_OK,
2458 * The cookie passed has an invalid length.
2460 TOX_ERR_CONFERENCE_JOIN_INVALID_LENGTH,
2463 * The conference is not the expected type. This indicates an invalid cookie.
2465 TOX_ERR_CONFERENCE_JOIN_WRONG_TYPE,
2468 * The friend number passed does not designate a valid friend.
2470 TOX_ERR_CONFERENCE_JOIN_FRIEND_NOT_FOUND,
2473 * Client is already in this conference.
2475 TOX_ERR_CONFERENCE_JOIN_DUPLICATE,
2478 * Conference instance failed to initialize.
2480 TOX_ERR_CONFERENCE_JOIN_INIT_FAIL,
2483 * The join packet failed to send.
2485 TOX_ERR_CONFERENCE_JOIN_FAIL_SEND,
2490 * Joins a conference that the client has been invited to.
2492 * @param friend_number The friend number of the friend who sent the invite.
2493 * @param cookie Received via the `conference_invite` event.
2494 * @param length The size of cookie.
2496 * @return conference number on success, UINT32_MAX on failure.
2498 uint tox_conference_join(Tox* tox, uint friend_number, const(void)* cookie, usize length, TOX_ERR_CONFERENCE_JOIN* error=null) @nogc;
2501 alias TOX_ERR_CONFERENCE_SEND_MESSAGE = int;
2502 enum : int {
2504 * The function returned successfully.
2506 TOX_ERR_CONFERENCE_SEND_MESSAGE_OK,
2509 * The conference number passed did not designate a valid conference.
2511 TOX_ERR_CONFERENCE_SEND_MESSAGE_CONFERENCE_NOT_FOUND,
2514 * The message is too long.
2516 TOX_ERR_CONFERENCE_SEND_MESSAGE_TOO_LONG,
2519 * The client is not connected to the conference.
2521 TOX_ERR_CONFERENCE_SEND_MESSAGE_NO_CONNECTION,
2524 * The message packet failed to send.
2526 TOX_ERR_CONFERENCE_SEND_MESSAGE_FAIL_SEND,
2531 * Send a text chat message to the conference.
2533 * This function creates a conference message packet and pushes it into the send
2534 * queue.
2536 * The message length may not exceed TOX_MAX_MESSAGE_LENGTH. Larger messages
2537 * must be split by the client and sent as separate messages. Other clients can
2538 * then reassemble the fragments.
2540 * @param conference_number The conference number of the conference the message is intended for.
2541 * @param type Message type (normal, action, ...).
2542 * @param message A non-NULL pointer to the first element of a byte array
2543 * containing the message text.
2544 * @param length Length of the message to be sent.
2546 * @return true on success.
2548 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;
2551 alias TOX_ERR_CONFERENCE_TITLE = int;
2552 enum : int {
2554 * The function returned successfully.
2556 TOX_ERR_CONFERENCE_TITLE_OK,
2559 * The conference number passed did not designate a valid conference.
2561 TOX_ERR_CONFERENCE_TITLE_CONFERENCE_NOT_FOUND,
2564 * The title is too long or empty.
2566 TOX_ERR_CONFERENCE_TITLE_INVALID_LENGTH,
2569 * The title packet failed to send.
2571 TOX_ERR_CONFERENCE_TITLE_FAIL_SEND,
2576 * Return the length of the conference title. Return value is unspecified on failure.
2578 * The return value is equal to the `length` argument received by the last
2579 * `conference_title` callback.
2581 usize tox_conference_get_title_size(const(Tox)* tox, uint conference_number, TOX_ERR_CONFERENCE_TITLE* error=null) @nogc;
2584 * Write the title designated by the given conference number to a byte array.
2586 * Call tox_conference_get_title_size to determine the allocation size for the `title` parameter.
2588 * The data written to `title` is equal to the data received by the last
2589 * `conference_title` callback.
2591 * @param title A valid memory region large enough to store the title.
2592 * If this parameter is NULL, this function has no effect.
2594 * @return true on success.
2596 bool tox_conference_get_title(const(Tox)* tox, uint conference_number, void* title, TOX_ERR_CONFERENCE_TITLE* error=null) @nogc;
2599 * Set the conference title and broadcast it to the rest of the conference.
2601 * Title length cannot be longer than TOX_MAX_NAME_LENGTH.
2603 * @return true on success.
2605 bool tox_conference_set_title(Tox* tox, uint conference_number, const(void)* title, usize length, TOX_ERR_CONFERENCE_TITLE* error=null) @nogc;
2608 * Return the number of conferences in the Tox instance.
2609 * This should be used to determine how much memory to allocate for `tox_conference_get_chatlist`.
2611 usize tox_conference_get_chatlist_size(const(Tox)* tox) @nogc;
2614 * Copy a list of valid conference IDs into the array chatlist. Determine how much space
2615 * to allocate for the array with the `tox_conference_get_chatlist_size` function.
2617 void tox_conference_get_chatlist(const(Tox)* tox, uint *chatlist) @nogc;
2621 * Returns the type of conference (TOX_CONFERENCE_TYPE) that conference_number is. Return value is
2622 * unspecified on failure.
2624 alias TOX_ERR_CONFERENCE_GET_TYPE = int;
2625 enum : int {
2627 * The function returned successfully.
2629 TOX_ERR_CONFERENCE_GET_TYPE_OK,
2632 * The conference number passed did not designate a valid conference.
2634 TOX_ERR_CONFERENCE_GET_TYPE_CONFERENCE_NOT_FOUND,
2638 TOX_CONFERENCE_TYPE tox_conference_get_type(const(Tox)* tox, uint conference_number, TOX_ERR_CONFERENCE_GET_TYPE* error=null) @nogc;
2641 /*******************************************************************************
2643 * :: Low-level custom packet sending and receiving
2645 ******************************************************************************/
2648 alias TOX_ERR_FRIEND_CUSTOM_PACKET = int;
2649 enum : int {
2651 * The function returned successfully.
2653 TOX_ERR_FRIEND_CUSTOM_PACKET_OK,
2656 * One of the arguments to the function was NULL when it was not expected.
2658 TOX_ERR_FRIEND_CUSTOM_PACKET_NULL,
2661 * The friend number did not designate a valid friend.
2663 TOX_ERR_FRIEND_CUSTOM_PACKET_FRIEND_NOT_FOUND,
2666 * This client is currently not connected to the friend.
2668 TOX_ERR_FRIEND_CUSTOM_PACKET_FRIEND_NOT_CONNECTED,
2671 * The first byte of data was not in the specified range for the packet type.
2672 * This range is 200-254 for lossy, and 160-191 for lossless packets.
2674 TOX_ERR_FRIEND_CUSTOM_PACKET_INVALID,
2677 * Attempted to send an empty packet.
2679 TOX_ERR_FRIEND_CUSTOM_PACKET_EMPTY,
2682 * Packet data length exceeded TOX_MAX_CUSTOM_PACKET_SIZE.
2684 TOX_ERR_FRIEND_CUSTOM_PACKET_TOO_LONG,
2687 * Packet queue is full.
2689 TOX_ERR_FRIEND_CUSTOM_PACKET_SENDQ,
2694 * Send a custom lossy packet to a friend.
2696 * The first byte of data must be in the range 200-254. Maximum length of a
2697 * custom packet is TOX_MAX_CUSTOM_PACKET_SIZE.
2699 * Lossy packets behave like UDP packets, meaning they might never reach the
2700 * other side or might arrive more than once (if someone is messing with the
2701 * connection) or might arrive in the wrong order.
2703 * Unless latency is an issue, it is recommended that you use lossless custom
2704 * packets instead.
2706 * @param friend_number The friend number of the friend this lossy packet
2707 * should be sent to.
2708 * @param data A byte array containing the packet data.
2709 * @param length The length of the packet data byte array.
2711 * @return true on success.
2713 bool tox_friend_send_lossy_packet(Tox* tox, uint friend_number, const(void)* data, usize length, TOX_ERR_FRIEND_CUSTOM_PACKET* error=null) @nogc;
2716 * Send a custom lossless packet to a friend.
2718 * The first byte of data must be in the range 160-191. Maximum length of a
2719 * custom packet is TOX_MAX_CUSTOM_PACKET_SIZE.
2721 * Lossless packet behaviour is comparable to TCP (reliability, arrive in order)
2722 * but with packets instead of a stream.
2724 * @param friend_number The friend number of the friend this lossless packet
2725 * should be sent to.
2726 * @param data A byte array containing the packet data.
2727 * @param length The length of the packet data byte array.
2729 * @return true on success.
2731 bool tox_friend_send_lossless_packet(Tox* tox, uint friend_number, const(void)* data, usize length, TOX_ERR_FRIEND_CUSTOM_PACKET* error=null) @nogc;
2734 * @param friend_number The friend number of the friend who sent a lossy packet.
2735 * @param data A byte array containing the received packet data.
2736 * @param length The length of the packet data byte array.
2738 alias tox_friend_lossy_packet_cb = void function (Tox* tox, uint friend_number, const(void)* data, usize length, void* user_data) nothrow @system;
2742 * Set the callback for the `friend_lossy_packet` event. Pass NULL to unset.
2745 void tox_callback_friend_lossy_packet(Tox* tox, tox_friend_lossy_packet_cb callback) @nogc;
2748 * @param friend_number The friend number of the friend who sent the packet.
2749 * @param data A byte array containing the received packet data.
2750 * @param length The length of the packet data byte array.
2752 alias tox_friend_lossless_packet_cb = void function (Tox* tox, uint friend_number, const(void)* data, usize length, void* user_data) nothrow @system;
2756 * Set the callback for the `friend_lossless_packet` event. Pass NULL to unset.
2759 void tox_callback_friend_lossless_packet(Tox* tox, tox_friend_lossless_packet_cb callback) @nogc;
2762 /*******************************************************************************
2764 * :: Low-level network information
2766 ******************************************************************************/
2770 * Writes the temporary DHT public key of this instance to a byte array.
2772 * This can be used in combination with an externally accessible IP address and
2773 * the bound port (from tox_self_get_udp_port) to run a temporary bootstrap node.
2775 * Be aware that every time a new instance is created, the DHT public key
2776 * changes, meaning this cannot be used to run a permanent bootstrap node.
2778 * @param dht_id A memory region of at least TOX_PUBLIC_KEY_SIZE bytes. If this
2779 * parameter is NULL, this function has no effect.
2781 void tox_self_get_dht_id(const(Tox)* tox, void* dht_id) @nogc;
2784 alias TOX_ERR_GET_PORT = int;
2785 enum : int {
2787 * The function returned successfully.
2789 TOX_ERR_GET_PORT_OK,
2792 * The instance was not bound to any port.
2794 TOX_ERR_GET_PORT_NOT_BOUND,
2799 * Return the UDP port this Tox instance is bound to.
2801 ushort tox_self_get_udp_port(const(Tox)* tox, TOX_ERR_GET_PORT* error=null) @nogc;
2804 * Return the TCP port this Tox instance is bound to. This is only relevant if
2805 * the instance is acting as a TCP relay.
2807 ushort tox_self_get_tcp_port(const(Tox)* tox, TOX_ERR_GET_PORT* error=null) @nogc;
2810 // ////////////////////////////////////////////////////////////////////////// //
2812 * Batch encryption functions.
2816 * Copyright © 2016-2017 The TokTok team.
2817 * Copyright © 2013-2016 Tox Developers.
2819 * This file is part of Tox, the free peer to peer instant messenger.
2821 * Tox is free software: you can redistribute it and/or modify
2822 * it under the terms of the GNU General Public License as published by
2823 * the Free Software Foundation, either version 3 of the License, or
2824 * (at your option) any later version.
2826 * Tox is distributed in the hope that it will be useful,
2827 * but WITHOUT ANY WARRANTY; without even the implied warranty of
2828 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2829 * GNU General Public License for more details.
2831 * You should have received a copy of the GNU General Public License
2832 * along with Tox. If not, see <http://www.gnu.org/licenses/>.
2836 /*******************************************************************************
2838 * This module is organized into two parts.
2840 * 1. A simple API operating on plain text/cipher text data and a password to
2841 * encrypt or decrypt it.
2842 * 2. A more advanced API that splits key derivation and encryption into two
2843 * separate function calls.
2845 * The first part is implemented in terms of the second part and simply calls
2846 * the separate functions in sequence. Since key derivation is very expensive
2847 * compared to the actual encryption, clients that do a lot of crypto should
2848 * prefer the advanced API and reuse pass-key objects.
2850 * To use the second part, first derive an encryption key from a password with
2851 * tox_pass_key_derive, then use the derived key to encrypt the data.
2853 * The encrypted data is prepended with a magic number, to aid validity
2854 * checking (no guarantees are made of course). Any data to be decrypted must
2855 * start with the magic number.
2857 * Clients should consider alerting their users that, unlike plain data, if
2858 * even one bit becomes corrupted, the data will be entirely unrecoverable.
2859 * Ditto if they forget their password, there is no way to recover the data.
2861 ******************************************************************************/
2866 * The size of the salt part of a pass-key.
2868 enum TOX_PASS_SALT_LENGTH = 32;
2870 uint tox_pass_salt_length() @nogc;
2873 * The size of the key part of a pass-key.
2875 enum TOX_PASS_KEY_LENGTH = 32;
2877 uint tox_pass_key_length() @nogc;
2880 * The amount of additional data required to store any encrypted byte array.
2881 * Encrypting an array of N bytes requires N + TOX_PASS_ENCRYPTION_EXTRA_LENGTH
2882 * bytes in the encrypted byte array.
2884 enum TOX_PASS_ENCRYPTION_EXTRA_LENGTH = 80;
2886 uint tox_pass_encryption_extra_length() @nogc;
2889 alias TOX_ERR_KEY_DERIVATION = int;
2890 enum : int {
2892 * The function returned successfully.
2894 TOX_ERR_KEY_DERIVATION_OK,
2897 * One of the arguments to the function was NULL when it was not expected.
2899 TOX_ERR_KEY_DERIVATION_NULL,
2902 * The crypto lib was unable to derive a key from the given passphrase,
2903 * which is usually a lack of memory issue.
2905 TOX_ERR_KEY_DERIVATION_FAILED,
2909 alias TOX_ERR_ENCRYPTION = int;
2910 enum : int {
2912 * The function returned successfully.
2914 TOX_ERR_ENCRYPTION_OK,
2917 * One of the arguments to the function was NULL when it was not expected.
2919 TOX_ERR_ENCRYPTION_NULL,
2922 * The crypto lib was unable to derive a key from the given passphrase,
2923 * which is usually a lack of memory issue. The functions accepting keys
2924 * do not produce this error.
2926 TOX_ERR_ENCRYPTION_KEY_DERIVATION_FAILED,
2929 * The encryption itself failed.
2931 TOX_ERR_ENCRYPTION_FAILED,
2935 alias TOX_ERR_DECRYPTION = int;
2936 enum : int {
2938 * The function returned successfully.
2940 TOX_ERR_DECRYPTION_OK,
2943 * One of the arguments to the function was NULL when it was not expected.
2945 TOX_ERR_DECRYPTION_NULL,
2948 * The input data was shorter than TOX_PASS_ENCRYPTION_EXTRA_LENGTH bytes
2950 TOX_ERR_DECRYPTION_INVALID_LENGTH,
2953 * The input data is missing the magic number (i.e. wasn't created by this
2954 * module, or is corrupted).
2956 TOX_ERR_DECRYPTION_BAD_FORMAT,
2959 * The crypto lib was unable to derive a key from the given passphrase,
2960 * which is usually a lack of memory issue. The functions accepting keys
2961 * do not produce this error.
2963 TOX_ERR_DECRYPTION_KEY_DERIVATION_FAILED,
2966 * The encrypted byte array could not be decrypted. Either the data was
2967 * corrupted or the password/key was incorrect.
2969 TOX_ERR_DECRYPTION_FAILED,
2974 /*******************************************************************************
2976 * BEGIN PART 1
2978 * The simple API is presented first. If your code spends too much time using
2979 * these functions, consider using the advanced functions instead and caching
2980 * the generated pass-key.
2982 ******************************************************************************/
2986 * Encrypts the given data with the given passphrase.
2988 * The output array must be at least `plaintext_len + TOX_PASS_ENCRYPTION_EXTRA_LENGTH`
2989 * bytes long. This delegates to tox_pass_key_derive and
2990 * tox_pass_key_encrypt.
2992 * @param plaintext A byte array of length `plaintext_len`.
2993 * @param plaintext_len The length of the plain text array. Bigger than 0.
2994 * @param passphrase The user-provided password. Can be empty.
2995 * @param passphrase_len The length of the password.
2996 * @param ciphertext The cipher text array to write the encrypted data to.
2998 * @return true on success.
3000 bool tox_pass_encrypt(const(void)* plaintext, usize plaintext_len, const(void)* passphrase, usize passphrase_len, void* ciphertext, TOX_ERR_ENCRYPTION* error=null) @nogc;
3003 * Decrypts the given data with the given passphrase.
3005 * The output array must be at least `ciphertext_len - TOX_PASS_ENCRYPTION_EXTRA_LENGTH`
3006 * bytes long. This delegates to tox_pass_key_decrypt.
3008 * @param ciphertext A byte array of length `ciphertext_len`.
3009 * @param ciphertext_len The length of the cipher text array. At least TOX_PASS_ENCRYPTION_EXTRA_LENGTH.
3010 * @param passphrase The user-provided password. Can be empty.
3011 * @param passphrase_len The length of the password.
3012 * @param plaintext The plain text array to write the decrypted data to.
3014 * @return true on success.
3016 bool tox_pass_decrypt(const(void)* ciphertext, usize ciphertext_len, const(void)* passphrase, usize passphrase_len, void* plaintext, TOX_ERR_DECRYPTION* error=null) @nogc;
3019 /*******************************************************************************
3021 * BEGIN PART 2
3023 * And now part 2, which does the actual encryption, and can be used to write
3024 * less CPU intensive client code than part one.
3026 ******************************************************************************/
3030 * This type represents a pass-key.
3032 * A pass-key and a password are two different concepts: a password is given
3033 * by the user in plain text. A pass-key is the generated symmetric key used
3034 * for encryption and decryption. It is derived from a salt and the user-
3035 * provided password.
3037 * The Tox_Pass_Key structure is hidden in the implementation. It can be created
3038 * using tox_pass_key_derive or tox_pass_key_derive_with_salt and must be deallocated using tox_pass_key_free.
3040 struct Tox_Pass_Key {
3041 // disable construction and postblit
3042 @disable this ();
3043 @disable this (this);
3047 * Deallocate a Tox_Pass_Key. This function behaves like free(), so NULL is an
3048 * acceptable argument value.
3050 void tox_pass_key_free(Tox_Pass_Key* _key) @nogc;
3053 * Generates a secret symmetric key from the given passphrase.
3055 * Be sure to not compromise the key! Only keep it in memory, do not write
3056 * it to disk.
3058 * Note that this function is not deterministic; to derive the same key from
3059 * a password, you also must know the random salt that was used. A
3060 * deterministic version of this function is tox_pass_key_derive_with_salt.
3062 * @param passphrase The user-provided password. Can be empty.
3063 * @param passphrase_len The length of the password.
3065 * @return non-NULL on success.
3067 Tox_Pass_Key* tox_pass_key_derive(const(void)* passphrase, usize passphrase_len, TOX_ERR_KEY_DERIVATION* error=null) @nogc;
3070 * Same as above, except use the given salt for deterministic key derivation.
3072 * @param passphrase The user-provided password. Can be empty.
3073 * @param passphrase_len The length of the password.
3074 * @param salt An array of at least TOX_PASS_SALT_LENGTH bytes.
3076 * @return non-NULL on success.
3078 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;
3081 * Encrypt a plain text with a key produced by tox_pass_key_derive or tox_pass_key_derive_with_salt.
3083 * The output array must be at least `plaintext_len + TOX_PASS_ENCRYPTION_EXTRA_LENGTH`
3084 * bytes long.
3086 * @param plaintext A byte array of length `plaintext_len`.
3087 * @param plaintext_len The length of the plain text array. Bigger than 0.
3088 * @param ciphertext The cipher text array to write the encrypted data to.
3090 * @return true on success.
3092 bool tox_pass_key_encrypt(const(Tox_Pass_Key)* _key, const(void)* plaintext, usize plaintext_len, void* ciphertext, TOX_ERR_ENCRYPTION* error=null) @nogc;
3095 * This is the inverse of tox_pass_key_encrypt, also using only keys produced by
3096 * tox_pass_key_derive or tox_pass_key_derive_with_salt.
3098 * @param ciphertext A byte array of length `ciphertext_len`.
3099 * @param ciphertext_len The length of the cipher text array. At least TOX_PASS_ENCRYPTION_EXTRA_LENGTH.
3100 * @param plaintext The plain text array to write the decrypted data to.
3102 * @return true on success.
3104 bool tox_pass_key_decrypt(const(Tox_Pass_Key)* _key, const(void)* ciphertext, usize ciphertext_len, void* plaintext, TOX_ERR_DECRYPTION* error=null) @nogc;
3107 alias TOX_ERR_GET_SALT = int;
3108 enum : int {
3110 * The function returned successfully.
3112 TOX_ERR_GET_SALT_OK,
3115 * One of the arguments to the function was NULL when it was not expected.
3117 TOX_ERR_GET_SALT_NULL,
3120 * The input data is missing the magic number (i.e. wasn't created by this
3121 * module, or is corrupted).
3123 TOX_ERR_GET_SALT_BAD_FORMAT,
3128 * Retrieves the salt used to encrypt the given data.
3130 * The retrieved salt can then be passed to tox_pass_key_derive_with_salt to
3131 * produce the same key as was previously used. Any data encrypted with this
3132 * module can be used as input.
3134 * The cipher text must be at least TOX_PASS_ENCRYPTION_EXTRA_LENGTH bytes in length.
3135 * The salt must be TOX_PASS_SALT_LENGTH bytes in length.
3136 * If the passed byte arrays are smaller than required, the behaviour is
3137 * undefined.
3139 * If the cipher text pointer or the salt is NULL, this function returns false.
3141 * Success does not say anything about the validity of the data, only that
3142 * data of the appropriate size was copied.
3144 * @return true on success.
3146 bool tox_get_salt(const(void)* ciphertext, void* salt, TOX_ERR_GET_SALT* error=null) @nogc;
3149 * Determines whether or not the given data is encrypted by this module.
3151 * It does this check by verifying that the magic number is the one put in
3152 * place by the encryption functions.
3154 * The data must be at least TOX_PASS_ENCRYPTION_EXTRA_LENGTH bytes in length.
3155 * If the passed byte array is smaller than required, the behaviour is
3156 * undefined.
3158 * If the data pointer is NULL, the behaviour is undefined
3160 * @return true if the data is encrypted by this module.
3162 bool tox_is_data_encrypted(const(void)* data) @nogc;
3165 } // extern(C) nothrow @trusted
3168 // ////////////////////////////////////////////////////////////////////////// //
3169 // Ketmar's added utilities
3170 //import std.net.curl;
3173 bool tox_unhex() (ubyte[] dest, const(char)[] str) nothrow @trusted @nogc {
3174 if (dest.length == 0) return false;
3175 typeof(dest.length) dpos = 0;
3176 foreach (char ch; str) {
3177 if (ch <= ' ' || ch == '_') continue;
3178 if (dpos >= dest.length*2) return false;
3179 int n;
3180 if (ch >= '0' && ch <= '9') n = ch-'0';
3181 else if (ch >= 'A' && ch <= 'F') n = ch-'A'+10;
3182 else if (ch >= 'a' && ch <= 'f') n = ch-'a'+10;
3183 else return false;
3184 if (dpos%2 == 0) dest[dpos/2] = cast(ubyte)(n*16); else dest[dpos/2] |= cast(ubyte)n;
3185 ++dpos;
3187 return (dpos == dest.length*2);
3191 string tox_hex() (const(void)[] src) nothrow @trusted {
3192 assert(src.length < int.max/2);
3193 if (src.length == 0) return null;
3194 auto res = new char[](src.length*2);
3195 int dpos = 0;
3196 foreach (ubyte b; cast(const(ubyte)[])src) {
3197 immutable hb = (b>>4);
3198 b &= 0x0f;
3199 res[dpos++] = cast(char)(hb+'0'+(hb > 9 ? 7 : 0));
3200 res[dpos++] = cast(char)(b+'0'+(b > 9 ? 7 : 0));
3202 return cast(string)res; // it is safe to cast here
3206 struct ToxBootstrapServer {
3207 string maintainer;
3208 ubyte[TOX_PUBLIC_KEY_SIZE] pubkey;
3209 string ipv4; // always contains trailing zero
3210 ushort port;
3211 bool tcp;
3212 bool udp;
3213 ushort[] tcpports;
3217 ToxBootstrapServer[] tox_download_bootstrap_list() (string url=null) {
3218 import std.net.curl : get;
3219 import std.json;
3220 if (url.length == 0) url = "https://nodes.tox.chat/json";
3221 auto lst = get(url);
3222 auto js = parseJSON(lst);
3223 ToxBootstrapServer[] res;
3224 foreach (ref svx; js["nodes"].array) {
3225 import std.conv : to;
3226 auto sv = ToxBootstrapServer();
3227 sv.maintainer = svx["maintainer"].str;
3228 if (!tox_unhex(sv.pubkey, svx["public_key"].str)) continue;
3229 //if (tox_hex(sv.pubkey) != svx["public_key"].str) assert(0, "wtf?!");
3230 auto loc = svx["location"].str;
3231 if (loc.length == 2 && (loc[0] == 'R' || loc[0] == 'r') && (loc[1] == 'U' || loc[1] == 'u')) continue;
3232 auto v4 = svx["ipv4"].str;
3233 if (v4.length < 1) continue;
3234 auto tv4 = new char[](v4.length+1);
3235 tv4[] = 0;
3236 tv4[0..$-1] = v4[];
3237 sv.ipv4 = cast(string)tv4; // it is safe to cast here
3238 if (!sv.ipv4.length) continue;
3239 if (svx["port"].type == JSON_TYPE.INTEGER) {
3240 auto pp = svx["port"].integer;
3241 if (pp < 1 || pp > 65535) continue;
3242 sv.port = cast(ushort)pp;
3243 } else {
3244 continue;
3246 if (svx["status_udp"].type == JSON_TYPE.TRUE) sv.udp = true;
3247 if (svx["status_tcp"].type == JSON_TYPE.TRUE) sv.tcp = true;
3248 if (!sv.tcp && !sv.udp) continue;
3249 if (sv.tcp) {
3250 tcpportloop: foreach (ref tpp; svx["tcp_ports"].array) {
3251 if (tpp.type != JSON_TYPE.INTEGER) continue;
3252 auto pp = svx["port"].integer;
3253 if (pp < 1 || pp > 65535) continue;
3254 foreach (immutable ushort t; sv.tcpports) if (t == pp) continue tcpportloop;
3255 sv.tcpports ~= cast(ushort)pp;
3257 if (sv.tcpports.length == 0) {
3258 sv.tcp = false;
3259 if (!sv.udp) continue;
3262 res ~= sv;
3264 return res;