diff options
Diffstat (limited to 'core')
-rw-r--r-- | core/tox.h | 267 |
1 files changed, 267 insertions, 0 deletions
diff --git a/core/tox.h b/core/tox.h new file mode 100644 index 00000000..3dc86855 --- /dev/null +++ b/core/tox.h | |||
@@ -0,0 +1,267 @@ | |||
1 | /* tox.h | ||
2 | * | ||
3 | * The Tox public API. | ||
4 | * | ||
5 | * Copyright (C) 2013 Tox project All Rights Reserved. | ||
6 | * | ||
7 | * This file is part of Tox. | ||
8 | * | ||
9 | * Tox is free software: you can redistribute it and/or modify | ||
10 | * it under the terms of the GNU General Public License as published by | ||
11 | * the Free Software Foundation, either version 3 of the License, or | ||
12 | * (at your option) any later version. | ||
13 | * | ||
14 | * Tox is distributed in the hope that it will be useful, | ||
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
17 | * GNU General Public License for more details. | ||
18 | * | ||
19 | * You should have received a copy of the GNU General Public License | ||
20 | * along with Tox. If not, see <http://www.gnu.org/licenses/>. | ||
21 | * | ||
22 | */ | ||
23 | |||
24 | #ifndef TOX_H | ||
25 | #define TOX_H | ||
26 | |||
27 | |||
28 | #ifdef __cplusplus | ||
29 | extern "C" { | ||
30 | #endif | ||
31 | |||
32 | #define MAX_NAME_LENGTH 128 | ||
33 | #define MAX_STATUSMESSAGE_LENGTH 128 | ||
34 | |||
35 | #define FRIEND_ADDRESS_SIZE (crypto_box_PUBLICKEYBYTES + sizeof(uint32_t) + sizeof(uint16_t)) | ||
36 | |||
37 | /* status definitions */ | ||
38 | #define FRIEND_ONLINE 4 | ||
39 | #define FRIEND_CONFIRMED 3 | ||
40 | #define FRIEND_REQUESTED 2 | ||
41 | #define FRIEND_ADDED 1 | ||
42 | #define NOFRIEND 0 | ||
43 | |||
44 | /* errors for m_addfriend | ||
45 | * FAERR - Friend Add Error */ | ||
46 | #define FAERR_TOOLONG -1 | ||
47 | #define FAERR_NOMESSAGE -2 | ||
48 | #define FAERR_OWNKEY -3 | ||
49 | #define FAERR_ALREADYSENT -4 | ||
50 | #define FAERR_UNKNOWN -5 | ||
51 | #define FAERR_BADCHECKSUM -6 | ||
52 | #define FAERR_SETNEWNOSPAM -7 | ||
53 | #define FAERR_NOMEM -8 | ||
54 | |||
55 | /* USERSTATUS | ||
56 | * Represents userstatuses someone can have. */ | ||
57 | |||
58 | typedef enum { | ||
59 | TOX_USERSTATUS_NONE, | ||
60 | TOX_USERSTATUS_AWAY, | ||
61 | TOX_USERSTATUS_BUSY, | ||
62 | TOX_USERSTATUS_INVALID | ||
63 | } | ||
64 | TOX_USERSTATUS; | ||
65 | |||
66 | /* | ||
67 | * returns a FRIEND_ADDRESS_SIZE byte address to give to others. | ||
68 | * format: [client_id (32 bytes)][nospam number (4 bytes)][checksum (2 bytes)] | ||
69 | * | ||
70 | */ | ||
71 | void tox_getaddress(void *tox, uint8_t *address); | ||
72 | |||
73 | /* | ||
74 | * add a friend | ||
75 | * set the data that will be sent along with friend request | ||
76 | * address is the address of the friend (returned by getaddress of the friend you wish to add) it must be FRIEND_ADDRESS_SIZE bytes. TODO: add checksum. | ||
77 | * data is the data and length is the length | ||
78 | * returns the friend number if success | ||
79 | * return -1 if message length is too long | ||
80 | * return -2 if no message (message length must be >= 1 byte) | ||
81 | * return -3 if user's own key | ||
82 | * return -4 if friend request already sent or already a friend | ||
83 | * return -5 for unknown error | ||
84 | * return -6 if bad checksum in address | ||
85 | * return -7 if the friend was already there but the nospam was different | ||
86 | * (the nospam for that friend was set to the new one) | ||
87 | * return -8 if increasing the friend list size fails | ||
88 | */ | ||
89 | int tox_addfriend(void *tox, uint8_t *address, uint8_t *data, uint16_t length); | ||
90 | |||
91 | |||
92 | /* add a friend without sending a friendrequest. | ||
93 | returns the friend number if success | ||
94 | return -1 if failure. */ | ||
95 | int tox_addfriend_norequest(void *tox, uint8_t *client_id); | ||
96 | |||
97 | /* return the friend id associated to that client id. | ||
98 | return -1 if no such friend */ | ||
99 | int tox_getfriend_id(void *tox, uint8_t *client_id); | ||
100 | |||
101 | /* copies the public key associated to that friend id into client_id buffer. | ||
102 | make sure that client_id is of size CLIENT_ID_SIZE. | ||
103 | return 0 if success | ||
104 | return -1 if failure */ | ||
105 | int tox_getclient_id(void *tox, int friend_id, uint8_t *client_id); | ||
106 | |||
107 | /* remove a friend */ | ||
108 | int tox_delfriend(void *tox, int friendnumber); | ||
109 | |||
110 | /* return 4 if friend is online | ||
111 | return 3 if friend is confirmed | ||
112 | return 2 if the friend request was sent | ||
113 | return 1 if the friend was added | ||
114 | return 0 if there is no friend with that number */ | ||
115 | int tox_friendstatus(void *tox, int friendnumber); | ||
116 | |||
117 | /* send a text chat message to an online friend | ||
118 | returns the message id if packet was successfully put into the send queue | ||
119 | return 0 if it was not | ||
120 | you will want to retain the return value, it will be passed to your read receipt callback | ||
121 | if one is received. | ||
122 | m_sendmessage_withid will send a message with the id of your choosing, | ||
123 | however we can generate an id for you by calling plain m_sendmessage. */ | ||
124 | uint32_t tox_sendmessage(void *tox, int friendnumber, uint8_t *message, uint32_t length); | ||
125 | uint32_t tox_sendmessage_withid(void *tox, int friendnumber, uint32_t theid, uint8_t *message, uint32_t length); | ||
126 | |||
127 | /* send an action to an online friend | ||
128 | returns 1 if packet was successfully put into the send queue | ||
129 | return 0 if it was not */ | ||
130 | int tox_sendaction(void *tox, int friendnumber, uint8_t *action, uint32_t length); | ||
131 | |||
132 | /* Set our nickname | ||
133 | name must be a string of maximum MAX_NAME_LENGTH length. | ||
134 | length must be at least 1 byte | ||
135 | length is the length of name with the NULL terminator | ||
136 | return 0 if success | ||
137 | return -1 if failure */ | ||
138 | int tox_setname(void *tox, uint8_t *name, uint16_t length); | ||
139 | |||
140 | /* | ||
141 | Get your nickname. | ||
142 | m The messanger context to use. | ||
143 | name Pointer to a string for the name. | ||
144 | nlen The length of the string buffer. | ||
145 | returns Return the length of the name, 0 on error. | ||
146 | */ | ||
147 | uint16_t tox_getselfname(void *tox, uint8_t *name, uint16_t nlen); | ||
148 | |||
149 | /* get name of friendnumber | ||
150 | put it in name | ||
151 | name needs to be a valid memory location with a size of at least MAX_NAME_LENGTH (128) bytes. | ||
152 | return 0 if success | ||
153 | return -1 if failure */ | ||
154 | int tox_getname(void *tox, int friendnumber, uint8_t *name); | ||
155 | |||
156 | /* set our user status | ||
157 | you are responsible for freeing status after | ||
158 | returns 0 on success, -1 on failure */ | ||
159 | int tox_set_statusmessage(void *tox, uint8_t *status, uint16_t length); | ||
160 | int tox_set_userstatus(void *tox, USERSTATUS status); | ||
161 | |||
162 | /* return the length of friendnumber's status message, | ||
163 | including null | ||
164 | pass it into malloc */ | ||
165 | int tox_get_statusmessage_size(void *tox, int friendnumber); | ||
166 | |||
167 | /* copy friendnumber's status message into buf, truncating if size is over maxlen | ||
168 | get the size you need to allocate from m_get_statusmessage_size | ||
169 | The self variant will copy our own status message. */ | ||
170 | int tox_copy_statusmessage(void *tox, int friendnumber, uint8_t *buf, uint32_t maxlen); | ||
171 | int tox_copy_self_statusmessage(void *tox, uint8_t *buf, uint32_t maxlen); | ||
172 | |||
173 | /* Return one of USERSTATUS values. | ||
174 | * Values unknown to your application should be represented as USERSTATUS_NONE. | ||
175 | * As above, the self variant will return our own USERSTATUS. | ||
176 | * If friendnumber is invalid, this shall return USERSTATUS_INVALID. */ | ||
177 | TOX_USERSTATUS tox_get_userstatus(void *tox, int friendnumber); | ||
178 | TOX_USERSTATUS tox_get_selfuserstatus(void *tox); | ||
179 | |||
180 | /* Sets whether we send read receipts for friendnumber. | ||
181 | * This function is not lazy, and it will fail if yesno is not (0 or 1).*/ | ||
182 | void tox_set_sends_receipts(void *tox, int friendnumber, int yesno); | ||
183 | |||
184 | /* set the function that will be executed when a friend request is received. | ||
185 | function format is function(uint8_t * public_key, uint8_t * data, uint16_t length) */ | ||
186 | void tox_callback_friendrequest(void *tox, void (*function)(uint8_t *, uint8_t *, uint16_t, void *), void *userdata); | ||
187 | |||
188 | /* set the function that will be executed when a message from a friend is received. | ||
189 | function format is: function(int friendnumber, uint8_t * message, uint32_t length) */ | ||
190 | void tox_callback_friendmessage(void *tox, void (*function)(void *tox, int, uint8_t *, uint16_t, void *), | ||
191 | void *userdata); | ||
192 | |||
193 | /* set the function that will be executed when an action from a friend is received. | ||
194 | function format is: function(int friendnumber, uint8_t * action, uint32_t length) */ | ||
195 | void tox_callback_action(void *tox, void (*function)(void *tox, int, uint8_t *, uint16_t, void *), void *userdata); | ||
196 | |||
197 | /* set the callback for name changes | ||
198 | function(int friendnumber, uint8_t *newname, uint16_t length) | ||
199 | you are not responsible for freeing newname */ | ||
200 | void tox_callback_namechange(void *tox, void (*function)(void *tox, int, uint8_t *, uint16_t, void *), | ||
201 | void *userdata); | ||
202 | |||
203 | /* set the callback for status message changes | ||
204 | function(int friendnumber, uint8_t *newstatus, uint16_t length) | ||
205 | you are not responsible for freeing newstatus */ | ||
206 | void tox_callback_statusmessage(void *tox, void (*function)(void *tox, int, uint8_t *, uint16_t, void *), | ||
207 | void *userdata); | ||
208 | |||
209 | /* set the callback for status type changes | ||
210 | function(int friendnumber, USERSTATUS kind) */ | ||
211 | void tox_callback_userstatus(void *tox, void (*function)(void *tox, int, USERSTATUS, void *), void *userdata); | ||
212 | |||
213 | /* set the callback for read receipts | ||
214 | function(int friendnumber, uint32_t receipt) | ||
215 | if you are keeping a record of returns from m_sendmessage, | ||
216 | receipt might be one of those values, and that means the message | ||
217 | has been received on the other side. since core doesn't | ||
218 | track ids for you, receipt may not correspond to any message | ||
219 | in that case, you should discard it. */ | ||
220 | void tox_callback_read_receipt(void *tox, void (*function)(void *tox, int, uint32_t, void *), void *userdata); | ||
221 | |||
222 | /* set the callback for connection status changes | ||
223 | function(int friendnumber, uint8_t status) | ||
224 | status: | ||
225 | 0 -- friend went offline after being previously online | ||
226 | 1 -- friend went online | ||
227 | note that this callback is not called when adding friends, thus the "after | ||
228 | being previously online" part. it's assumed that when adding friends, | ||
229 | their connection status is offline. */ | ||
230 | void tox_callback_connectionstatus(void *tox, void (*function)(void *tox, int, uint8_t, void *), void *userdata); | ||
231 | |||
232 | /* Use this function to bootstrap the client | ||
233 | Sends a get nodes request to the given node with ip port and public_key */ | ||
234 | void tox_bootstrap(void *tox, IP_Port ip_port, uint8_t *public_key); | ||
235 | |||
236 | /* returns 0 if we are not connected to the DHT | ||
237 | returns 1 if we are */ | ||
238 | int tox_isconnected(void *tox); | ||
239 | |||
240 | /* run this at startup | ||
241 | * returns allocated instance of tox on success | ||
242 | * returns 0 if there are problems */ | ||
243 | void *tox_new(void); | ||
244 | |||
245 | /* run this before closing shop | ||
246 | * free all datastructures */ | ||
247 | void tox_kill(void *tox); | ||
248 | |||
249 | /* the main loop that needs to be run at least 20 times per second */ | ||
250 | void tox_do(void *tox); | ||
251 | |||
252 | /* SAVING AND LOADING FUNCTIONS: */ | ||
253 | |||
254 | /* returns the size of the messenger data (for saving) */ | ||
255 | uint32_t tox_size(void *tox); | ||
256 | |||
257 | /* save the messenger in data (must be allocated memory of size Messenger_size()) */ | ||
258 | void tox_save(void *tox, uint8_t *data); | ||
259 | |||
260 | /* load the messenger from data of size length */ | ||
261 | int tox_load(void *tox, uint8_t *data, uint32_t length); | ||
262 | |||
263 | #ifdef __cplusplus | ||
264 | } | ||
265 | #endif | ||
266 | |||
267 | #endif | ||