summaryrefslogtreecommitdiff
path: root/core/Messenger.h
diff options
context:
space:
mode:
authorjin-eld <jin at mediatomb dot cc>2013-08-04 15:10:37 +0300
committerjin-eld <jin at mediatomb dot cc>2013-08-24 03:25:07 +0300
commite658892793c42b2d058eed0937025ef2ddaaa372 (patch)
tree2a022cab057f2c16ca95860ed980092880052f6e /core/Messenger.h
parente2aa8161adc85795fe4d63d4642f47e90937ddc2 (diff)
Rename core directory because of autoconf name clash
While doing the checks configure might generate "core" files and will then try to remove them. Having a "core" directory generates an error while runing the configure script. There's no workaround but to rename the core directory.
Diffstat (limited to 'core/Messenger.h')
-rw-r--r--core/Messenger.h351
1 files changed, 0 insertions, 351 deletions
diff --git a/core/Messenger.h b/core/Messenger.h
deleted file mode 100644
index e808529f..00000000
--- a/core/Messenger.h
+++ /dev/null
@@ -1,351 +0,0 @@
1/* Messenger.h
2 *
3 * An implementation of a simple text chat only messenger on the tox network core.
4 *
5 * NOTE: All the text in the messages must be encoded using UTF-8
6 *
7 * Copyright (C) 2013 Tox project All Rights Reserved.
8 *
9 * This file is part of Tox.
10 *
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.
15 *
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.
20 *
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/>.
23 *
24 */
25
26#ifndef MESSENGER_H
27#define MESSENGER_H
28
29#include "net_crypto.h"
30#include "DHT.h"
31#include "friend_requests.h"
32#include "LAN_discovery.h"
33
34#ifdef __cplusplus
35extern "C" {
36#endif
37
38#define MAX_NAME_LENGTH 128
39#define MAX_STATUSMESSAGE_LENGTH 128
40
41#define FRIEND_ADDRESS_SIZE (crypto_box_PUBLICKEYBYTES + sizeof(uint32_t) + sizeof(uint16_t))
42
43#define PACKET_ID_PING 0
44#define PACKET_ID_NICKNAME 48
45#define PACKET_ID_STATUSMESSAGE 49
46#define PACKET_ID_USERSTATUS 50
47#define PACKET_ID_RECEIPT 65
48#define PACKET_ID_MESSAGE 64
49#define PACKET_ID_ACTION 63
50
51
52/* status definitions */
53enum {
54 NOFRIEND,
55 FRIEND_ADDED,
56 FRIEND_REQUESTED,
57 FRIEND_CONFIRMED,
58 FRIEND_ONLINE,
59};
60
61/* errors for m_addfriend
62 * FAERR - Friend Add Error */
63enum {
64 FAERR_TOOLONG = -1,
65 FAERR_NOMESSAGE = -2,
66 FAERR_OWNKEY = -3,
67 FAERR_ALREADYSENT = -4,
68 FAERR_UNKNOWN = -5,
69 FAERR_BADCHECKSUM = -6,
70 FAERR_SETNEWNOSPAM = -7,
71 FAERR_NOMEM = -8
72};
73
74/* don't assume MAX_STATUSMESSAGE_LENGTH will stay at 128, it may be increased
75 to an absurdly large number later */
76
77/* Default start timeout in seconds between friend requests */
78#define FRIENDREQUEST_TIMEOUT 5;
79
80/* interval between the sending of ping packets.*/
81#define FRIEND_PING_INTERVAL 5
82
83/* If no packets are recieved from friend in this time interval, kill the connection.*/
84#define FRIEND_CONNECTION_TIMEOUT (FRIEND_PING_INTERVAL * 2)
85
86/* USERSTATUS
87 * Represents userstatuses someone can have. */
88
89typedef enum {
90 USERSTATUS_NONE,
91 USERSTATUS_AWAY,
92 USERSTATUS_BUSY,
93 USERSTATUS_INVALID
94}
95USERSTATUS;
96
97typedef struct {
98 uint8_t client_id[CLIENT_ID_SIZE];
99 int crypt_connection_id;
100 uint64_t friendrequest_lastsent; /* time at which the last friend request was sent. */
101 uint32_t friendrequest_timeout; /* The timeout between successful friendrequest sending attempts */
102 uint8_t status; /* 0 if no friend, 1 if added, 2 if friend request sent, 3 if confirmed friend, 4 if online. */
103 uint8_t info[MAX_DATA_SIZE]; /* the data that is sent during the friend requests we do */
104 uint8_t name[MAX_NAME_LENGTH];
105 uint8_t name_sent; /* 0 if we didn't send our name to this friend 1 if we have. */
106 uint8_t *statusmessage;
107 uint16_t statusmessage_length;
108 uint8_t statusmessage_sent;
109 USERSTATUS userstatus;
110 uint8_t userstatus_sent;
111 uint16_t info_size; /* length of the info */
112 uint32_t message_id; /* a semi-unique id used in read receipts */
113 uint8_t receives_read_receipts; /* shall we send read receipts to this person? */
114 uint32_t friendrequest_nospam; /*The nospam number used in the friend request*/
115 uint64_t ping_lastrecv;
116 uint64_t ping_lastsent;
117} Friend;
118
119typedef struct Messenger {
120
121 Networking_Core *net;
122 Net_Crypto *net_crypto;
123 DHT *dht;
124 Friend_Requests fr;
125 uint8_t name[MAX_NAME_LENGTH];
126 uint16_t name_length;
127
128 uint8_t statusmessage[MAX_STATUSMESSAGE_LENGTH];
129 uint16_t statusmessage_length;
130
131 USERSTATUS userstatus;
132
133 Friend *friendlist;
134 uint32_t numfriends;
135
136 uint64_t last_LANdiscovery;
137
138 void (*friend_message)(struct Messenger *m, int, uint8_t *, uint16_t, void *);
139 void *friend_message_userdata;
140 void (*friend_action)(struct Messenger *m, int, uint8_t *, uint16_t, void *);
141 void *friend_action_userdata;
142 void (*friend_namechange)(struct Messenger *m, int, uint8_t *, uint16_t, void *);
143 void *friend_namechange_userdata;
144 void (*friend_statusmessagechange)(struct Messenger *m, int, uint8_t *, uint16_t, void *);
145 void *friend_statusmessagechange_userdata;
146 void (*friend_userstatuschange)(struct Messenger *m, int, USERSTATUS, void *);
147 void *friend_userstatuschange_userdata;
148 void (*read_receipt)(struct Messenger *m, int, uint32_t, void *);
149 void *read_receipt_userdata;
150 void (*friend_statuschange)(struct Messenger *m, int, uint8_t, void *);
151 void *friend_statuschange_userdata;
152 void (*friend_connectionstatuschange)(struct Messenger *m, int, uint8_t, void *);
153 void *friend_connectionstatuschange_userdata;
154
155
156} Messenger;
157
158/*
159 * returns a FRIEND_ADDRESS_SIZE byte address to give to others.
160 * format: [client_id (32 bytes)][nospam number (4 bytes)][checksum (2 bytes)]
161 *
162 */
163void getaddress(Messenger *m, uint8_t *address);
164
165/*
166 * add a friend
167 * set the data that will be sent along with friend request
168 * 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.
169 * data is the data and length is the length
170 * returns the friend number if success
171 * return -1 if message length is too long
172 * return -2 if no message (message length must be >= 1 byte)
173 * return -3 if user's own key
174 * return -4 if friend request already sent or already a friend
175 * return -5 for unknown error
176 * return -6 if bad checksum in address
177 * return -7 if the friend was already there but the nospam was different
178 * (the nospam for that friend was set to the new one)
179 * return -8 if increasing the friend list size fails
180 */
181int m_addfriend(Messenger *m, uint8_t *address, uint8_t *data, uint16_t length);
182
183
184/* add a friend without sending a friendrequest.
185 returns the friend number if success
186 return -1 if failure. */
187int m_addfriend_norequest(Messenger *m, uint8_t *client_id);
188
189/* return the friend id associated to that client id.
190 return -1 if no such friend */
191int getfriend_id(Messenger *m, uint8_t *client_id);
192
193/* copies the public key associated to that friend id into client_id buffer.
194 make sure that client_id is of size CLIENT_ID_SIZE.
195 return 0 if success
196 return -1 if failure */
197int getclient_id(Messenger *m, int friend_id, uint8_t *client_id);
198
199/* remove a friend */
200int m_delfriend(Messenger *m, int friendnumber);
201
202/* return 4 if friend is online
203 return 3 if friend is confirmed
204 return 2 if the friend request was sent
205 return 1 if the friend was added
206 return 0 if there is no friend with that number */
207int m_friendstatus(Messenger *m, int friendnumber);
208
209/* send a text chat message to an online friend
210 returns the message id if packet was successfully put into the send queue
211 return 0 if it was not
212 you will want to retain the return value, it will be passed to your read receipt callback
213 if one is received.
214 m_sendmessage_withid will send a message with the id of your choosing,
215 however we can generate an id for you by calling plain m_sendmessage. */
216uint32_t m_sendmessage(Messenger *m, int friendnumber, uint8_t *message, uint32_t length);
217uint32_t m_sendmessage_withid(Messenger *m, int friendnumber, uint32_t theid, uint8_t *message, uint32_t length);
218
219/* send an action to an online friend
220 returns 1 if packet was successfully put into the send queue
221 return 0 if it was not */
222int m_sendaction(Messenger *m, int friendnumber, uint8_t *action, uint32_t length);
223
224/* Set our nickname
225 name must be a string of maximum MAX_NAME_LENGTH length.
226 length must be at least 1 byte
227 length is the length of name with the NULL terminator
228 return 0 if success
229 return -1 if failure */
230int setname(Messenger *m, uint8_t *name, uint16_t length);
231
232/*
233 Get your nickname.
234 m The messanger context to use.
235 name Pointer to a string for the name.
236 nlen The length of the string buffer.
237 returns Return the length of the name, 0 on error.
238*/
239uint16_t getself_name(Messenger *m, uint8_t *name, uint16_t nlen);
240
241/* get name of friendnumber
242 put it in name
243 name needs to be a valid memory location with a size of at least MAX_NAME_LENGTH (128) bytes.
244 return 0 if success
245 return -1 if failure */
246int getname(Messenger *m, int friendnumber, uint8_t *name);
247
248/* set our user status
249 you are responsible for freeing status after
250 returns 0 on success, -1 on failure */
251int m_set_statusmessage(Messenger *m, uint8_t *status, uint16_t length);
252int m_set_userstatus(Messenger *m, USERSTATUS status);
253
254/* return the length of friendnumber's status message,
255 including null
256 pass it into malloc */
257int m_get_statusmessage_size(Messenger *m, int friendnumber);
258
259/* copy friendnumber's status message into buf, truncating if size is over maxlen
260 get the size you need to allocate from m_get_statusmessage_size
261 The self variant will copy our own status message. */
262int m_copy_statusmessage(Messenger *m, int friendnumber, uint8_t *buf, uint32_t maxlen);
263int m_copy_self_statusmessage(Messenger *m, uint8_t *buf, uint32_t maxlen);
264
265/* Return one of USERSTATUS values.
266 * Values unknown to your application should be represented as USERSTATUS_NONE.
267 * As above, the self variant will return our own USERSTATUS.
268 * If friendnumber is invalid, this shall return USERSTATUS_INVALID. */
269USERSTATUS m_get_userstatus(Messenger *m, int friendnumber);
270USERSTATUS m_get_self_userstatus(Messenger *m);
271
272/* Sets whether we send read receipts for friendnumber.
273 * This function is not lazy, and it will fail if yesno is not (0 or 1).*/
274void m_set_sends_receipts(Messenger *m, int friendnumber, int yesno);
275
276/* set the function that will be executed when a friend request is received.
277 function format is function(uint8_t * public_key, uint8_t * data, uint16_t length) */
278void m_callback_friendrequest(Messenger *m, void (*function)(uint8_t *, uint8_t *, uint16_t, void *), void *userdata);
279
280/* set the function that will be executed when a message from a friend is received.
281 function format is: function(int friendnumber, uint8_t * message, uint32_t length) */
282void m_callback_friendmessage(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t, void *),
283 void *userdata);
284
285/* set the function that will be executed when an action from a friend is received.
286 function format is: function(int friendnumber, uint8_t * action, uint32_t length) */
287void m_callback_action(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t, void *), void *userdata);
288
289/* set the callback for name changes
290 function(int friendnumber, uint8_t *newname, uint16_t length)
291 you are not responsible for freeing newname */
292void m_callback_namechange(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t, void *),
293 void *userdata);
294
295/* set the callback for status message changes
296 function(int friendnumber, uint8_t *newstatus, uint16_t length)
297 you are not responsible for freeing newstatus */
298void m_callback_statusmessage(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t, void *),
299 void *userdata);
300
301/* set the callback for status type changes
302 function(int friendnumber, USERSTATUS kind) */
303void m_callback_userstatus(Messenger *m, void (*function)(Messenger *m, int, USERSTATUS, void *), void *userdata);
304
305/* set the callback for read receipts
306 function(int friendnumber, uint32_t receipt)
307 if you are keeping a record of returns from m_sendmessage,
308 receipt might be one of those values, and that means the message
309 has been received on the other side. since core doesn't
310 track ids for you, receipt may not correspond to any message
311 in that case, you should discard it. */
312void m_callback_read_receipt(Messenger *m, void (*function)(Messenger *m, int, uint32_t, void *), void *userdata);
313
314/* set the callback for connection status changes
315 function(int friendnumber, uint8_t status)
316 status:
317 0 -- friend went offline after being previously online
318 1 -- friend went online
319 note that this callback is not called when adding friends, thus the "after
320 being previously online" part. it's assumed that when adding friends,
321 their connection status is offline. */
322void m_callback_connectionstatus(Messenger *m, void (*function)(Messenger *m, int, uint8_t, void *), void *userdata);
323
324/* run this at startup
325 * returns allocated instance of Messenger on success
326 * returns 0 if there are problems */
327Messenger *initMessenger(void);
328
329/* run this before closing shop
330 * free all datastructures */
331void cleanupMessenger(Messenger *M);
332
333/* the main loop that needs to be run at least 200 times per second */
334void doMessenger(Messenger *m);
335
336/* SAVING AND LOADING FUNCTIONS: */
337
338/* returns the size of the messenger data (for saving) */
339uint32_t Messenger_size(Messenger *m);
340
341/* save the messenger in data (must be allocated memory of size Messenger_size()) */
342void Messenger_save(Messenger *m, uint8_t *data);
343
344/* load the messenger from data of size length */
345int Messenger_load(Messenger *m, uint8_t *data, uint32_t length);
346
347#ifdef __cplusplus
348}
349#endif
350
351#endif