diff options
Diffstat (limited to 'core/tox.c')
-rw-r--r-- | core/tox.c | 374 |
1 files changed, 0 insertions, 374 deletions
diff --git a/core/tox.c b/core/tox.c deleted file mode 100644 index a97e52bc..00000000 --- a/core/tox.c +++ /dev/null | |||
@@ -1,374 +0,0 @@ | |||
1 | /* tox.c | ||
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 | #include "Messenger.h" | ||
25 | /* | ||
26 | * returns a FRIEND_ADDRESS_SIZE byte address to give to others. | ||
27 | * format: [client_id (32 bytes)][nospam number (4 bytes)][checksum (2 bytes)] | ||
28 | * | ||
29 | */ | ||
30 | void tox_getaddress(void *tox, uint8_t *address) | ||
31 | { | ||
32 | Messenger *m = tox; | ||
33 | getaddress(m, address); | ||
34 | } | ||
35 | |||
36 | /* | ||
37 | * add a friend | ||
38 | * set the data that will be sent along with friend request | ||
39 | * 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. | ||
40 | * data is the data and length is the length | ||
41 | * returns the friend number if success | ||
42 | * return FA_TOOLONG if message length is too long | ||
43 | * return FAERR_NOMESSAGE if no message (message length must be >= 1 byte) | ||
44 | * return FAERR_OWNKEY if user's own key | ||
45 | * return FAERR_ALREADYSENT if friend request already sent or already a friend | ||
46 | * return FAERR_UNKNOWN for unknown error | ||
47 | * return FAERR_BADCHECKSUM if bad checksum in address | ||
48 | * return FAERR_SETNEWNOSPAM if the friend was already there but the nospam was different | ||
49 | * (the nospam for that friend was set to the new one) | ||
50 | * return FAERR_NOMEM if increasing the friend list size fails | ||
51 | */ | ||
52 | int tox_addfriend(void *tox, uint8_t *address, uint8_t *data, uint16_t length) | ||
53 | { | ||
54 | Messenger *m = tox; | ||
55 | return m_addfriend(m, address, data, length); | ||
56 | } | ||
57 | |||
58 | /* add a friend without sending a friendrequest. | ||
59 | returns the friend number if success | ||
60 | return -1 if failure. */ | ||
61 | int tox_addfriend_norequest(void *tox, uint8_t *client_id) | ||
62 | { | ||
63 | Messenger *m = tox; | ||
64 | return m_addfriend_norequest(m, client_id); | ||
65 | } | ||
66 | |||
67 | /* return the friend id associated to that client id. | ||
68 | return -1 if no such friend */ | ||
69 | int tox_getfriend_id(void *tox, uint8_t *client_id) | ||
70 | { | ||
71 | Messenger *m = tox; | ||
72 | return getfriend_id(m, client_id); | ||
73 | } | ||
74 | |||
75 | /* copies the public key associated to that friend id into client_id buffer. | ||
76 | make sure that client_id is of size CLIENT_ID_SIZE. | ||
77 | return 0 if success | ||
78 | return -1 if failure */ | ||
79 | int tox_getclient_id(void *tox, int friend_id, uint8_t *client_id) | ||
80 | { | ||
81 | Messenger *m = tox; | ||
82 | return getclient_id(m, friend_id, client_id); | ||
83 | } | ||
84 | |||
85 | /* remove a friend */ | ||
86 | int tox_delfriend(void *tox, int friendnumber) | ||
87 | { | ||
88 | Messenger *m = tox; | ||
89 | return m_delfriend(m, friendnumber); | ||
90 | } | ||
91 | |||
92 | /* return 4 if friend is online | ||
93 | return 3 if friend is confirmed | ||
94 | return 2 if the friend request was sent | ||
95 | return 1 if the friend was added | ||
96 | return 0 if there is no friend with that number */ | ||
97 | int tox_friendstatus(void *tox, int friendnumber) | ||
98 | { | ||
99 | Messenger *m = tox; | ||
100 | return m_friendstatus(m, friendnumber); | ||
101 | } | ||
102 | |||
103 | /* send a text chat message to an online friend | ||
104 | returns the message id if packet was successfully put into the send queue | ||
105 | return 0 if it was not | ||
106 | you will want to retain the return value, it will be passed to your read receipt callback | ||
107 | if one is received. | ||
108 | m_sendmessage_withid will send a message with the id of your choosing, | ||
109 | however we can generate an id for you by calling plain m_sendmessage. */ | ||
110 | uint32_t tox_sendmessage(void *tox, int friendnumber, uint8_t *message, uint32_t length) | ||
111 | { | ||
112 | Messenger *m = tox; | ||
113 | return m_sendmessage(m, friendnumber, message, length); | ||
114 | } | ||
115 | |||
116 | uint32_t tox_sendmessage_withid(void *tox, int friendnumber, uint32_t theid, uint8_t *message, uint32_t length) | ||
117 | { | ||
118 | Messenger *m = tox; | ||
119 | return m_sendmessage_withid(m, friendnumber, theid, message, length); | ||
120 | } | ||
121 | |||
122 | /* send an action to an online friend | ||
123 | returns 1 if packet was successfully put into the send queue | ||
124 | return 0 if it was not */ | ||
125 | int tox_sendaction(void *tox, int friendnumber, uint8_t *action, uint32_t length) | ||
126 | { | ||
127 | Messenger *m = tox; | ||
128 | return m_sendaction(m, friendnumber, action, length); | ||
129 | } | ||
130 | |||
131 | /* Set our nickname | ||
132 | name must be a string of maximum MAX_NAME_LENGTH length. | ||
133 | length must be at least 1 byte | ||
134 | length is the length of name with the NULL terminator | ||
135 | return 0 if success | ||
136 | return -1 if failure */ | ||
137 | int tox_setname(void *tox, uint8_t *name, uint16_t length) | ||
138 | { | ||
139 | Messenger *m = tox; | ||
140 | return setname(m, name, length); | ||
141 | } | ||
142 | |||
143 | /* | ||
144 | Get your nickname. | ||
145 | m The messanger context to use. | ||
146 | name Pointer to a string for the name. | ||
147 | nlen The length of the string buffer. | ||
148 | returns Return the length of the name, 0 on error. | ||
149 | */ | ||
150 | uint16_t tox_getselfname(void *tox, uint8_t *name, uint16_t nlen) | ||
151 | { | ||
152 | Messenger *m = tox; | ||
153 | return getself_name(m, name, nlen); | ||
154 | } | ||
155 | |||
156 | /* get name of friendnumber | ||
157 | put it in name | ||
158 | name needs to be a valid memory location with a size of at least MAX_NAME_LENGTH (128) bytes. | ||
159 | return 0 if success | ||
160 | return -1 if failure */ | ||
161 | int tox_getname(void *tox, int friendnumber, uint8_t *name) | ||
162 | { | ||
163 | Messenger *m = tox; | ||
164 | return getname(m, friendnumber, name); | ||
165 | } | ||
166 | |||
167 | /* set our user status | ||
168 | you are responsible for freeing status after | ||
169 | returns 0 on success, -1 on failure */ | ||
170 | int tox_set_statusmessage(void *tox, uint8_t *status, uint16_t length) | ||
171 | { | ||
172 | Messenger *m = tox; | ||
173 | return m_set_statusmessage(m, status, length); | ||
174 | } | ||
175 | |||
176 | int tox_set_userstatus(void *tox, USERSTATUS status) | ||
177 | { | ||
178 | Messenger *m = tox; | ||
179 | return m_set_userstatus(m, status); | ||
180 | } | ||
181 | |||
182 | /* return the length of friendnumber's status message, | ||
183 | including null | ||
184 | pass it into malloc */ | ||
185 | int tox_get_statusmessage_size(void *tox, int friendnumber) | ||
186 | { | ||
187 | Messenger *m = tox; | ||
188 | return m_get_statusmessage_size(m, friendnumber); | ||
189 | } | ||
190 | |||
191 | /* copy friendnumber's status message into buf, truncating if size is over maxlen | ||
192 | get the size you need to allocate from m_get_statusmessage_size | ||
193 | The self variant will copy our own status message. */ | ||
194 | int tox_copy_statusmessage(void *tox, int friendnumber, uint8_t *buf, uint32_t maxlen) | ||
195 | { | ||
196 | Messenger *m = tox; | ||
197 | return m_copy_statusmessage(m, friendnumber, buf, maxlen); | ||
198 | } | ||
199 | |||
200 | int tox_copy_self_statusmessage(void *tox, uint8_t *buf, uint32_t maxlen) | ||
201 | { | ||
202 | Messenger *m = tox; | ||
203 | return m_copy_self_statusmessage(m, buf, maxlen); | ||
204 | } | ||
205 | |||
206 | /* Return one of USERSTATUS values. | ||
207 | * Values unknown to your application should be represented as USERSTATUS_NONE. | ||
208 | * As above, the self variant will return our own USERSTATUS. | ||
209 | * If friendnumber is invalid, this shall return USERSTATUS_INVALID. */ | ||
210 | USERSTATUS tox_get_userstatus(void *tox, int friendnumber) | ||
211 | { | ||
212 | Messenger *m = tox; | ||
213 | return m_get_userstatus(m, friendnumber); | ||
214 | } | ||
215 | |||
216 | USERSTATUS tox_get_selfuserstatus(void *tox) | ||
217 | { | ||
218 | Messenger *m = tox; | ||
219 | return m_get_self_userstatus(m); | ||
220 | } | ||
221 | |||
222 | |||
223 | /* Sets whether we send read receipts for friendnumber. | ||
224 | * This function is not lazy, and it will fail if yesno is not (0 or 1).*/ | ||
225 | void tox_set_sends_receipts(void *tox, int friendnumber, int yesno) | ||
226 | { | ||
227 | Messenger *m = tox; | ||
228 | m_set_sends_receipts(m, friendnumber, yesno); | ||
229 | } | ||
230 | |||
231 | |||
232 | /* set the function that will be executed when a friend request is received. | ||
233 | function format is function(uint8_t * public_key, uint8_t * data, uint16_t length) */ | ||
234 | void tox_callback_friendrequest(void *tox, void (*function)(uint8_t *, uint8_t *, uint16_t, void *), void *userdata) | ||
235 | { | ||
236 | Messenger *m = tox; | ||
237 | m_callback_friendrequest(m, function, userdata); | ||
238 | } | ||
239 | |||
240 | |||
241 | /* set the function that will be executed when a message from a friend is received. | ||
242 | function format is: function(int friendnumber, uint8_t * message, uint32_t length) */ | ||
243 | void tox_callback_friendmessage(void *tox, void (*function)(Messenger *tox, int, uint8_t *, uint16_t, void *), | ||
244 | void *userdata) | ||
245 | { | ||
246 | Messenger *m = tox; | ||
247 | m_callback_friendmessage(m, function, userdata); | ||
248 | } | ||
249 | |||
250 | /* set the function that will be executed when an action from a friend is received. | ||
251 | function format is: function(int friendnumber, uint8_t * action, uint32_t length) */ | ||
252 | void tox_callback_action(void *tox, void (*function)(Messenger *tox, int, uint8_t *, uint16_t, void *), void *userdata) | ||
253 | { | ||
254 | Messenger *m = tox; | ||
255 | m_callback_action(m, function, userdata); | ||
256 | } | ||
257 | |||
258 | /* set the callback for name changes | ||
259 | function(int friendnumber, uint8_t *newname, uint16_t length) | ||
260 | you are not responsible for freeing newname */ | ||
261 | void tox_callback_namechange(void *tox, void (*function)(Messenger *tox, int, uint8_t *, uint16_t, void *), | ||
262 | void *userdata) | ||
263 | { | ||
264 | Messenger *m = tox; | ||
265 | m_callback_namechange(m, function, userdata); | ||
266 | } | ||
267 | |||
268 | /* set the callback for status message changes | ||
269 | function(int friendnumber, uint8_t *newstatus, uint16_t length) | ||
270 | you are not responsible for freeing newstatus */ | ||
271 | void tox_callback_statusmessage(void *tox, void (*function)(Messenger *tox, int, uint8_t *, uint16_t, void *), | ||
272 | void *userdata) | ||
273 | { | ||
274 | Messenger *m = tox; | ||
275 | m_callback_statusmessage(m, function, userdata); | ||
276 | } | ||
277 | |||
278 | /* set the callback for status type changes | ||
279 | function(int friendnumber, USERSTATUS kind) */ | ||
280 | void tox_callback_userstatus(void *tox, void (*function)(Messenger *tox, int, USERSTATUS, void *), void *userdata) | ||
281 | { | ||
282 | Messenger *m = tox; | ||
283 | m_callback_userstatus(m, function, userdata); | ||
284 | } | ||
285 | |||
286 | /* set the callback for read receipts | ||
287 | function(int friendnumber, uint32_t receipt) | ||
288 | if you are keeping a record of returns from m_sendmessage, | ||
289 | receipt might be one of those values, and that means the message | ||
290 | has been received on the other side. since core doesn't | ||
291 | track ids for you, receipt may not correspond to any message | ||
292 | in that case, you should discard it. */ | ||
293 | void tox_callback_read_receipt(void *tox, void (*function)(Messenger *tox, int, uint32_t, void *), void *userdata) | ||
294 | { | ||
295 | Messenger *m = tox; | ||
296 | m_callback_read_receipt(m, function, userdata); | ||
297 | } | ||
298 | |||
299 | /* set the callback for connection status changes | ||
300 | function(int friendnumber, uint8_t status) | ||
301 | status: | ||
302 | 0 -- friend went offline after being previously online | ||
303 | 1 -- friend went online | ||
304 | note that this callback is not called when adding friends, thus the "after | ||
305 | being previously online" part. it's assumed that when adding friends, | ||
306 | their connection status is offline. */ | ||
307 | void tox_callback_connectionstatus(void *tox, void (*function)(Messenger *tox, int, uint8_t, void *), void *userdata) | ||
308 | { | ||
309 | Messenger *m = tox; | ||
310 | m_callback_connectionstatus(m, function, userdata); | ||
311 | } | ||
312 | |||
313 | /* Use this function to bootstrap the client | ||
314 | Sends a get nodes request to the given node with ip port and public_key */ | ||
315 | void tox_bootstrap(void *tox, IP_Port ip_port, uint8_t *public_key) | ||
316 | { | ||
317 | Messenger *m = tox; | ||
318 | DHT_bootstrap(m->dht, ip_port, public_key); | ||
319 | } | ||
320 | |||
321 | /* returns 0 if we are not connected to the DHT | ||
322 | returns 1 if we are */ | ||
323 | int tox_isconnected(void *tox) | ||
324 | { | ||
325 | Messenger *m = tox; | ||
326 | return DHT_isconnected(m->dht); | ||
327 | } | ||
328 | |||
329 | /* run this at startup | ||
330 | * returns allocated instance of tox on success | ||
331 | * returns 0 if there are problems */ | ||
332 | void *tox_new(void) | ||
333 | { | ||
334 | return initMessenger(); | ||
335 | } | ||
336 | |||
337 | /* run this before closing shop | ||
338 | * free all datastructures */ | ||
339 | void tox_kill(void *tox) | ||
340 | { | ||
341 | Messenger *m = tox; | ||
342 | cleanupMessenger(m); | ||
343 | } | ||
344 | |||
345 | /* the main loop that needs to be run at least 20 times per second */ | ||
346 | void tox_do(void *tox) | ||
347 | { | ||
348 | Messenger *m = tox; | ||
349 | doMessenger(m); | ||
350 | } | ||
351 | |||
352 | /* SAVING AND LOADING FUNCTIONS: */ | ||
353 | |||
354 | /* returns the size of the messenger data (for saving) */ | ||
355 | uint32_t tox_size(void *tox) | ||
356 | { | ||
357 | Messenger *m = tox; | ||
358 | return Messenger_size(m); | ||
359 | } | ||
360 | |||
361 | /* save the messenger in data (must be allocated memory of size Messenger_size()) */ | ||
362 | void tox_save(void *tox, uint8_t *data) | ||
363 | { | ||
364 | Messenger *m = tox; | ||
365 | Messenger_save(m, data); | ||
366 | } | ||
367 | |||
368 | /* load the messenger from data of size length */ | ||
369 | int tox_load(void *tox, uint8_t *data, uint32_t length) | ||
370 | { | ||
371 | Messenger *m = tox; | ||
372 | return Messenger_load(m, data, length); | ||
373 | } | ||
374 | |||