diff options
Diffstat (limited to 'core')
-rw-r--r-- | core/Messenger.c | 516 | ||||
-rw-r--r-- | core/Messenger.h | 126 |
2 files changed, 331 insertions, 311 deletions
diff --git a/core/Messenger.c b/core/Messenger.c index 92c83b9b..a6189941 100644 --- a/core/Messenger.c +++ b/core/Messenger.c | |||
@@ -24,40 +24,8 @@ | |||
24 | #include "Messenger.h" | 24 | #include "Messenger.h" |
25 | #define MIN(a,b) (((a)<(b))?(a):(b)) | 25 | #define MIN(a,b) (((a)<(b))?(a):(b)) |
26 | 26 | ||
27 | typedef struct { | 27 | static void set_friend_status(Messenger *m, int friendnumber, uint8_t status); |
28 | uint8_t client_id[CLIENT_ID_SIZE]; | 28 | static int write_cryptpacket_id(Messenger *m, int friendnumber, uint8_t packet_id, uint8_t *data, uint32_t length); |
29 | int crypt_connection_id; | ||
30 | uint64_t friend_request_id; /* id of the friend request corresponding to the current friend request to the current friend. */ | ||
31 | uint8_t status; /* 0 if no friend, 1 if added, 2 if friend request sent, 3 if confirmed friend, 4 if online. */ | ||
32 | uint8_t info[MAX_DATA_SIZE]; /* the data that is sent during the friend requests we do */ | ||
33 | uint8_t name[MAX_NAME_LENGTH]; | ||
34 | uint8_t name_sent; /* 0 if we didn't send our name to this friend 1 if we have. */ | ||
35 | uint8_t *statusmessage; | ||
36 | uint16_t statusmessage_length; | ||
37 | uint8_t statusmessage_sent; | ||
38 | USERSTATUS userstatus; | ||
39 | uint8_t userstatus_sent; | ||
40 | uint16_t info_size; /* length of the info */ | ||
41 | uint32_t message_id; /* a semi-unique id used in read receipts */ | ||
42 | uint8_t receives_read_receipts; /* shall we send read receipts to this person? */ | ||
43 | } Friend; | ||
44 | |||
45 | uint8_t self_public_key[crypto_box_PUBLICKEYBYTES]; | ||
46 | |||
47 | static uint8_t self_name[MAX_NAME_LENGTH]; | ||
48 | static uint16_t self_name_length; | ||
49 | |||
50 | static uint8_t self_statusmessage[MAX_STATUSMESSAGE_LENGTH]; | ||
51 | static uint16_t self_statusmessage_length; | ||
52 | |||
53 | static USERSTATUS self_userstatus; | ||
54 | |||
55 | static Friend *friendlist; | ||
56 | static uint32_t numfriends; | ||
57 | |||
58 | |||
59 | static void set_friend_status(int friendnumber, uint8_t status); | ||
60 | static int write_cryptpacket_id(int friendnumber, uint8_t packet_id, uint8_t *data, uint32_t length); | ||
61 | 29 | ||
62 | /* 1 if we are online | 30 | /* 1 if we are online |
63 | 0 if we are offline | 31 | 0 if we are offline |
@@ -65,24 +33,24 @@ static int write_cryptpacket_id(int friendnumber, uint8_t packet_id, uint8_t *da | |||
65 | 33 | ||
66 | /* set the size of the friend list to numfriends | 34 | /* set the size of the friend list to numfriends |
67 | return -1 if realloc fails */ | 35 | return -1 if realloc fails */ |
68 | int realloc_friendlist(uint32_t num) { | 36 | int realloc_friendlist(Messenger *m, uint32_t num) { |
69 | Friend *newfriendlist = realloc(friendlist, num*sizeof(Friend)); | 37 | Friend *newfriendlist = realloc(m->friendlist, num*sizeof(Friend)); |
70 | if (newfriendlist == NULL) | 38 | if (newfriendlist == NULL) |
71 | return -1; | 39 | return -1; |
72 | memset(&newfriendlist[num-1], 0, sizeof(Friend)); | 40 | memset(&newfriendlist[num-1], 0, sizeof(Friend)); |
73 | friendlist = newfriendlist; | 41 | m->friendlist = newfriendlist; |
74 | return 0; | 42 | return 0; |
75 | } | 43 | } |
76 | 44 | ||
77 | /* return the friend id associated to that public key. | 45 | /* return the friend id associated to that public key. |
78 | return -1 if no such friend */ | 46 | return -1 if no such friend */ |
79 | int getfriend_id(uint8_t *client_id) | 47 | int getfriend_id(Messenger *m, uint8_t *client_id) |
80 | { | 48 | { |
81 | uint32_t i; | 49 | uint32_t i; |
82 | 50 | ||
83 | for (i = 0; i < numfriends; ++i) { | 51 | for (i = 0; i < m->numfriends; ++i) { |
84 | if (friendlist[i].status > 0) | 52 | if (m->friendlist[i].status > 0) |
85 | if (memcmp(client_id, friendlist[i].client_id, crypto_box_PUBLICKEYBYTES) == 0) | 53 | if (memcmp(client_id, m->friendlist[i].client_id, crypto_box_PUBLICKEYBYTES) == 0) |
86 | return i; | 54 | return i; |
87 | } | 55 | } |
88 | 56 | ||
@@ -93,13 +61,13 @@ int getfriend_id(uint8_t *client_id) | |||
93 | make sure that client_id is of size CLIENT_ID_SIZE. | 61 | make sure that client_id is of size CLIENT_ID_SIZE. |
94 | return 0 if success | 62 | return 0 if success |
95 | return -1 if failure. */ | 63 | return -1 if failure. */ |
96 | int getclient_id(int friend_id, uint8_t *client_id) | 64 | int getclient_id(Messenger *m, int friend_id, uint8_t *client_id) |
97 | { | 65 | { |
98 | if (friend_id >= numfriends || friend_id < 0) | 66 | if (friend_id >= m->numfriends || friend_id < 0) |
99 | return -1; | 67 | return -1; |
100 | 68 | ||
101 | if (friendlist[friend_id].status > 0) { | 69 | if (m->friendlist[friend_id].status > 0) { |
102 | memcpy(client_id, friendlist[friend_id].client_id, CLIENT_ID_SIZE); | 70 | memcpy(client_id, m->friendlist[friend_id].client_id, CLIENT_ID_SIZE); |
103 | return 0; | 71 | return 0; |
104 | } | 72 | } |
105 | 73 | ||
@@ -118,7 +86,7 @@ int getclient_id(int friend_id, uint8_t *client_id) | |||
118 | * return FAERR_ALREADYSENT if friend request already sent or already a friend | 86 | * return FAERR_ALREADYSENT if friend request already sent or already a friend |
119 | * return FAERR_UNKNOWN for unknown error | 87 | * return FAERR_UNKNOWN for unknown error |
120 | */ | 88 | */ |
121 | int m_addfriend(uint8_t *client_id, uint8_t *data, uint16_t length) | 89 | int m_addfriend(Messenger *m, uint8_t *client_id, uint8_t *data, uint16_t length) |
122 | { | 90 | { |
123 | if (length >= (MAX_DATA_SIZE - crypto_box_PUBLICKEYBYTES | 91 | if (length >= (MAX_DATA_SIZE - crypto_box_PUBLICKEYBYTES |
124 | - crypto_box_NONCEBYTES - crypto_box_BOXZEROBYTES | 92 | - crypto_box_NONCEBYTES - crypto_box_BOXZEROBYTES |
@@ -128,57 +96,57 @@ int m_addfriend(uint8_t *client_id, uint8_t *data, uint16_t length) | |||
128 | return FAERR_NOMESSAGE; | 96 | return FAERR_NOMESSAGE; |
129 | if (memcmp(client_id, self_public_key, crypto_box_PUBLICKEYBYTES) == 0) | 97 | if (memcmp(client_id, self_public_key, crypto_box_PUBLICKEYBYTES) == 0) |
130 | return FAERR_OWNKEY; | 98 | return FAERR_OWNKEY; |
131 | if (getfriend_id(client_id) != -1) | 99 | if (getfriend_id(m, client_id) != -1) |
132 | return FAERR_ALREADYSENT; | 100 | return FAERR_ALREADYSENT; |
133 | 101 | ||
134 | /* resize the friend list if necessary */ | 102 | /* resize the friend list if necessary */ |
135 | realloc_friendlist(numfriends + 1); | 103 | realloc_friendlist(m, m->numfriends + 1); |
136 | 104 | ||
137 | uint32_t i; | 105 | uint32_t i; |
138 | for (i = 0; i <= numfriends; ++i) { | 106 | for (i = 0; i <= m->numfriends; ++i) { |
139 | if (friendlist[i].status == NOFRIEND) { | 107 | if (m->friendlist[i].status == NOFRIEND) { |
140 | DHT_addfriend(client_id); | 108 | DHT_addfriend(client_id); |
141 | friendlist[i].status = FRIEND_ADDED; | 109 | m->friendlist[i].status = FRIEND_ADDED; |
142 | friendlist[i].crypt_connection_id = -1; | 110 | m->friendlist[i].crypt_connection_id = -1; |
143 | friendlist[i].friend_request_id = -1; | 111 | m->friendlist[i].friend_request_id = -1; |
144 | memcpy(friendlist[i].client_id, client_id, CLIENT_ID_SIZE); | 112 | memcpy(m->friendlist[i].client_id, client_id, CLIENT_ID_SIZE); |
145 | friendlist[i].statusmessage = calloc(1, 1); | 113 | m->friendlist[i].statusmessage = calloc(1, 1); |
146 | friendlist[i].statusmessage_length = 1; | 114 | m->friendlist[i].statusmessage_length = 1; |
147 | friendlist[i].userstatus = USERSTATUS_NONE; | 115 | m->friendlist[i].userstatus = USERSTATUS_NONE; |
148 | memcpy(friendlist[i].info, data, length); | 116 | memcpy(m->friendlist[i].info, data, length); |
149 | friendlist[i].info_size = length; | 117 | m->friendlist[i].info_size = length; |
150 | friendlist[i].message_id = 0; | 118 | m->friendlist[i].message_id = 0; |
151 | friendlist[i].receives_read_receipts = 1; /* default: YES */ | 119 | m->friendlist[i].receives_read_receipts = 1; /* default: YES */ |
152 | 120 | ||
153 | ++numfriends; | 121 | ++ m->numfriends; |
154 | return i; | 122 | return i; |
155 | } | 123 | } |
156 | } | 124 | } |
157 | return FAERR_UNKNOWN; | 125 | return FAERR_UNKNOWN; |
158 | } | 126 | } |
159 | 127 | ||
160 | int m_addfriend_norequest(uint8_t * client_id) | 128 | int m_addfriend_norequest(Messenger *m, uint8_t * client_id) |
161 | { | 129 | { |
162 | if (getfriend_id(client_id) != -1) | 130 | if (getfriend_id(m, client_id) != -1) |
163 | return -1; | 131 | return -1; |
164 | 132 | ||
165 | /* resize the friend list if necessary */ | 133 | /* resize the friend list if necessary */ |
166 | realloc_friendlist(numfriends + 1); | 134 | realloc_friendlist(m, m->numfriends + 1); |
167 | 135 | ||
168 | uint32_t i; | 136 | uint32_t i; |
169 | for (i = 0; i <= numfriends; ++i) { | 137 | for (i = 0; i <= m->numfriends; ++i) { |
170 | if(friendlist[i].status == NOFRIEND) { | 138 | if(m->friendlist[i].status == NOFRIEND) { |
171 | DHT_addfriend(client_id); | 139 | DHT_addfriend(client_id); |
172 | friendlist[i].status = FRIEND_REQUESTED; | 140 | m->friendlist[i].status = FRIEND_REQUESTED; |
173 | friendlist[i].crypt_connection_id = -1; | 141 | m->friendlist[i].crypt_connection_id = -1; |
174 | friendlist[i].friend_request_id = -1; | 142 | m->friendlist[i].friend_request_id = -1; |
175 | memcpy(friendlist[i].client_id, client_id, CLIENT_ID_SIZE); | 143 | memcpy(m->friendlist[i].client_id, client_id, CLIENT_ID_SIZE); |
176 | friendlist[i].statusmessage = calloc(1, 1); | 144 | m->friendlist[i].statusmessage = calloc(1, 1); |
177 | friendlist[i].statusmessage_length = 1; | 145 | m->friendlist[i].statusmessage_length = 1; |
178 | friendlist[i].userstatus = USERSTATUS_NONE; | 146 | m->friendlist[i].userstatus = USERSTATUS_NONE; |
179 | friendlist[i].message_id = 0; | 147 | m->friendlist[i].message_id = 0; |
180 | friendlist[i].receives_read_receipts = 1; /* default: YES */ | 148 | m->friendlist[i].receives_read_receipts = 1; /* default: YES */ |
181 | ++numfriends; | 149 | ++ m->numfriends; |
182 | return i; | 150 | return i; |
183 | } | 151 | } |
184 | } | 152 | } |
@@ -188,23 +156,23 @@ int m_addfriend_norequest(uint8_t * client_id) | |||
188 | /* remove a friend | 156 | /* remove a friend |
189 | return 0 if success | 157 | return 0 if success |
190 | return -1 if failure */ | 158 | return -1 if failure */ |
191 | int m_delfriend(int friendnumber) | 159 | int m_delfriend(Messenger *m, int friendnumber) |
192 | { | 160 | { |
193 | if (friendnumber >= numfriends || friendnumber < 0) | 161 | if (friendnumber >= m->numfriends || friendnumber < 0) |
194 | return -1; | 162 | return -1; |
195 | 163 | ||
196 | DHT_delfriend(friendlist[friendnumber].client_id); | 164 | DHT_delfriend(m->friendlist[friendnumber].client_id); |
197 | crypto_kill(friendlist[friendnumber].crypt_connection_id); | 165 | crypto_kill(m->friendlist[friendnumber].crypt_connection_id); |
198 | free(friendlist[friendnumber].statusmessage); | 166 | free(m->friendlist[friendnumber].statusmessage); |
199 | memset(&friendlist[friendnumber], 0, sizeof(Friend)); | 167 | memset(&(m->friendlist[friendnumber]), 0, sizeof(Friend)); |
200 | uint32_t i; | 168 | uint32_t i; |
201 | 169 | ||
202 | for (i = numfriends; i != 0; --i) { | 170 | for (i = m->numfriends; i != 0; --i) { |
203 | if (friendlist[i-1].status != NOFRIEND) | 171 | if (m->friendlist[i-1].status != NOFRIEND) |
204 | break; | 172 | break; |
205 | } | 173 | } |
206 | numfriends = i; | 174 | m->numfriends = i; |
207 | realloc_friendlist(numfriends + 1); | 175 | realloc_friendlist(m, m->numfriends + 1); |
208 | 176 | ||
209 | return 0; | 177 | return 0; |
210 | } | 178 | } |
@@ -214,31 +182,31 @@ int m_delfriend(int friendnumber) | |||
214 | return FRIEND_REQUESTED if the friend request was sent | 182 | return FRIEND_REQUESTED if the friend request was sent |
215 | return FRIEND_ADDED if the friend was added | 183 | return FRIEND_ADDED if the friend was added |
216 | return NOFRIEND if there is no friend with that number */ | 184 | return NOFRIEND if there is no friend with that number */ |
217 | int m_friendstatus(int friendnumber) | 185 | int m_friendstatus(Messenger *m, int friendnumber) |
218 | { | 186 | { |
219 | if (friendnumber < 0 || friendnumber >= numfriends) | 187 | if (friendnumber < 0 || friendnumber >= m->numfriends) |
220 | return NOFRIEND; | 188 | return NOFRIEND; |
221 | return friendlist[friendnumber].status; | 189 | return m->friendlist[friendnumber].status; |
222 | } | 190 | } |
223 | 191 | ||
224 | /* send a text chat message to an online friend | 192 | /* send a text chat message to an online friend |
225 | return the message id if packet was successfully put into the send queue | 193 | return the message id if packet was successfully put into the send queue |
226 | return 0 if it was not */ | 194 | return 0 if it was not */ |
227 | uint32_t m_sendmessage(int friendnumber, uint8_t *message, uint32_t length) | 195 | uint32_t m_sendmessage(Messenger *m, int friendnumber, uint8_t *message, uint32_t length) |
228 | { | 196 | { |
229 | if (friendnumber < 0 || friendnumber >= numfriends) | 197 | if (friendnumber < 0 || friendnumber >= m->numfriends) |
230 | return 0; | 198 | return 0; |
231 | uint32_t msgid = ++friendlist[friendnumber].message_id; | 199 | uint32_t msgid = ++m->friendlist[friendnumber].message_id; |
232 | if (msgid == 0) | 200 | if (msgid == 0) |
233 | msgid = 1; /* otherwise, false error */ | 201 | msgid = 1; /* otherwise, false error */ |
234 | if(m_sendmessage_withid(friendnumber, msgid, message, length)) { | 202 | if(m_sendmessage_withid(m, friendnumber, msgid, message, length)) { |
235 | return msgid; | 203 | return msgid; |
236 | } | 204 | } |
237 | 205 | ||
238 | return 0; | 206 | return 0; |
239 | } | 207 | } |
240 | 208 | ||
241 | uint32_t m_sendmessage_withid(int friendnumber, uint32_t theid, uint8_t *message, uint32_t length) | 209 | uint32_t m_sendmessage_withid(Messenger *m, int friendnumber, uint32_t theid, uint8_t *message, uint32_t length) |
242 | { | 210 | { |
243 | if (length >= (MAX_DATA_SIZE - sizeof(theid))) | 211 | if (length >= (MAX_DATA_SIZE - sizeof(theid))) |
244 | return 0; | 212 | return 0; |
@@ -246,34 +214,34 @@ uint32_t m_sendmessage_withid(int friendnumber, uint32_t theid, uint8_t *message | |||
246 | theid = htonl(theid); | 214 | theid = htonl(theid); |
247 | memcpy(temp, &theid, sizeof(theid)); | 215 | memcpy(temp, &theid, sizeof(theid)); |
248 | memcpy(temp + sizeof(theid), message, length); | 216 | memcpy(temp + sizeof(theid), message, length); |
249 | return write_cryptpacket_id(friendnumber, PACKET_ID_MESSAGE, temp, length + sizeof(theid)); | 217 | return write_cryptpacket_id(m, friendnumber, PACKET_ID_MESSAGE, temp, length + sizeof(theid)); |
250 | } | 218 | } |
251 | 219 | ||
252 | /* send an action to an online friend | 220 | /* send an action to an online friend |
253 | return 1 if packet was successfully put into the send queue | 221 | return 1 if packet was successfully put into the send queue |
254 | return 0 if it was not */ | 222 | return 0 if it was not */ |
255 | int m_sendaction(int friendnumber, uint8_t *action, uint32_t length) | 223 | int m_sendaction(Messenger *m, int friendnumber, uint8_t *action, uint32_t length) |
256 | { | 224 | { |
257 | return write_cryptpacket_id(friendnumber, PACKET_ID_ACTION, action, length); | 225 | return write_cryptpacket_id(m, friendnumber, PACKET_ID_ACTION, action, length); |
258 | } | 226 | } |
259 | 227 | ||
260 | /* send a name packet to friendnumber | 228 | /* send a name packet to friendnumber |
261 | length is the length with the NULL terminator*/ | 229 | length is the length with the NULL terminator*/ |
262 | static int m_sendname(int friendnumber, uint8_t * name, uint16_t length) | 230 | static int m_sendname(Messenger *m, int friendnumber, uint8_t * name, uint16_t length) |
263 | { | 231 | { |
264 | if(length > MAX_NAME_LENGTH || length == 0) | 232 | if(length > MAX_NAME_LENGTH || length == 0) |
265 | return 0; | 233 | return 0; |
266 | return write_cryptpacket_id(friendnumber, PACKET_ID_NICKNAME, name, length); | 234 | return write_cryptpacket_id(m, friendnumber, PACKET_ID_NICKNAME, name, length); |
267 | } | 235 | } |
268 | 236 | ||
269 | /* set the name of a friend | 237 | /* set the name of a friend |
270 | return 0 if success | 238 | return 0 if success |
271 | return -1 if failure */ | 239 | return -1 if failure */ |
272 | static int setfriendname(int friendnumber, uint8_t * name) | 240 | static int setfriendname(Messenger *m, int friendnumber, uint8_t * name) |
273 | { | 241 | { |
274 | if (friendnumber >= numfriends || friendnumber < 0) | 242 | if (friendnumber >= m->numfriends || friendnumber < 0) |
275 | return -1; | 243 | return -1; |
276 | memcpy(friendlist[friendnumber].name, name, MAX_NAME_LENGTH); | 244 | memcpy(m->friendlist[friendnumber].name, name, MAX_NAME_LENGTH); |
277 | return 0; | 245 | return 0; |
278 | } | 246 | } |
279 | 247 | ||
@@ -283,15 +251,15 @@ static int setfriendname(int friendnumber, uint8_t * name) | |||
283 | length is the length of name with the NULL terminator | 251 | length is the length of name with the NULL terminator |
284 | return 0 if success | 252 | return 0 if success |
285 | return -1 if failure */ | 253 | return -1 if failure */ |
286 | int setname(uint8_t * name, uint16_t length) | 254 | int setname(Messenger *m, uint8_t * name, uint16_t length) |
287 | { | 255 | { |
288 | if (length > MAX_NAME_LENGTH || length == 0) | 256 | if (length > MAX_NAME_LENGTH || length == 0) |
289 | return -1; | 257 | return -1; |
290 | memcpy(self_name, name, length); | 258 | memcpy(m->name, name, length); |
291 | self_name_length = length; | 259 | m->name_length = length; |
292 | uint32_t i; | 260 | uint32_t i; |
293 | for (i = 0; i < numfriends; ++i) | 261 | for (i = 0; i < m->numfriends; ++i) |
294 | friendlist[i].name_sent = 0; | 262 | m->friendlist[i].name_sent = 0; |
295 | return 0; | 263 | return 0; |
296 | } | 264 | } |
297 | 265 | ||
@@ -299,10 +267,10 @@ int setname(uint8_t * name, uint16_t length) | |||
299 | put it in name | 267 | put it in name |
300 | name needs to be a valid memory location with a size of at least MAX_NAME_LENGTH bytes. | 268 | name needs to be a valid memory location with a size of at least MAX_NAME_LENGTH bytes. |
301 | return the length of the name */ | 269 | return the length of the name */ |
302 | uint16_t getself_name(uint8_t *name) | 270 | uint16_t getself_name(Messenger *m, uint8_t *name) |
303 | { | 271 | { |
304 | memcpy(name, self_name, self_name_length); | 272 | memcpy(name, m->name, m->name_length); |
305 | return self_name_length; | 273 | return m->name_length; |
306 | } | 274 | } |
307 | 275 | ||
308 | /* get name of friendnumber | 276 | /* get name of friendnumber |
@@ -310,293 +278,288 @@ uint16_t getself_name(uint8_t *name) | |||
310 | name needs to be a valid memory location with a size of at least MAX_NAME_LENGTH bytes. | 278 | name needs to be a valid memory location with a size of at least MAX_NAME_LENGTH bytes. |
311 | return 0 if success | 279 | return 0 if success |
312 | return -1 if failure */ | 280 | return -1 if failure */ |
313 | int getname(int friendnumber, uint8_t * name) | 281 | int getname(Messenger *m, int friendnumber, uint8_t * name) |
314 | { | 282 | { |
315 | if (friendnumber >= numfriends || friendnumber < 0) | 283 | if (friendnumber >= m->numfriends || friendnumber < 0) |
316 | return -1; | 284 | return -1; |
317 | memcpy(name, friendlist[friendnumber].name, MAX_NAME_LENGTH); | 285 | memcpy(name, m->friendlist[friendnumber].name, MAX_NAME_LENGTH); |
318 | return 0; | 286 | return 0; |
319 | } | 287 | } |
320 | 288 | ||
321 | int m_set_statusmessage(uint8_t *status, uint16_t length) | 289 | int m_set_statusmessage(Messenger *m, uint8_t *status, uint16_t length) |
322 | { | 290 | { |
323 | if (length > MAX_STATUSMESSAGE_LENGTH) | 291 | if (length > MAX_STATUSMESSAGE_LENGTH) |
324 | return -1; | 292 | return -1; |
325 | memcpy(self_statusmessage, status, length); | 293 | memcpy(m->statusmessage, status, length); |
326 | self_statusmessage_length = length; | 294 | m->statusmessage_length = length; |
327 | 295 | ||
328 | uint32_t i; | 296 | uint32_t i; |
329 | for (i = 0; i < numfriends; ++i) | 297 | for (i = 0; i < m->numfriends; ++i) |
330 | friendlist[i].statusmessage_sent = 0; | 298 | m->friendlist[i].statusmessage_sent = 0; |
331 | return 0; | 299 | return 0; |
332 | } | 300 | } |
333 | 301 | ||
334 | int m_set_userstatus(USERSTATUS status) | 302 | int m_set_userstatus(Messenger *m, USERSTATUS status) |
335 | { | 303 | { |
336 | if (status >= USERSTATUS_INVALID) { | 304 | if (status >= USERSTATUS_INVALID) { |
337 | return -1; | 305 | return -1; |
338 | } | 306 | } |
339 | self_userstatus = status; | 307 | m->userstatus = status; |
340 | uint32_t i; | 308 | uint32_t i; |
341 | for (i = 0; i < numfriends; ++i) | 309 | for (i = 0; i < m->numfriends; ++i) |
342 | friendlist[i].userstatus_sent = 0; | 310 | m->friendlist[i].userstatus_sent = 0; |
343 | return 0; | 311 | return 0; |
344 | } | 312 | } |
345 | 313 | ||
346 | /* return the size of friendnumber's user status | 314 | /* return the size of friendnumber's user status |
347 | guaranteed to be at most MAX_STATUSMESSAGE_LENGTH */ | 315 | guaranteed to be at most MAX_STATUSMESSAGE_LENGTH */ |
348 | int m_get_statusmessage_size(int friendnumber) | 316 | int m_get_statusmessage_size(Messenger *m, int friendnumber) |
349 | { | 317 | { |
350 | if (friendnumber >= numfriends || friendnumber < 0) | 318 | if (friendnumber >= m->numfriends || friendnumber < 0) |
351 | return -1; | 319 | return -1; |
352 | return friendlist[friendnumber].statusmessage_length; | 320 | return m->friendlist[friendnumber].statusmessage_length; |
353 | } | 321 | } |
354 | 322 | ||
355 | /* copy the user status of friendnumber into buf, truncating if needed to maxlen | 323 | /* copy the user status of friendnumber into buf, truncating if needed to maxlen |
356 | bytes, use m_get_statusmessage_size to find out how much you need to allocate */ | 324 | bytes, use m_get_statusmessage_size to find out how much you need to allocate */ |
357 | int m_copy_statusmessage(int friendnumber, uint8_t * buf, uint32_t maxlen) | 325 | int m_copy_statusmessage(Messenger *m, int friendnumber, uint8_t * buf, uint32_t maxlen) |
358 | { | 326 | { |
359 | if (friendnumber >= numfriends || friendnumber < 0) | 327 | if (friendnumber >= m->numfriends || friendnumber < 0) |
360 | return -1; | 328 | return -1; |
361 | memset(buf, 0, maxlen); | 329 | memset(buf, 0, maxlen); |
362 | memcpy(buf, friendlist[friendnumber].statusmessage, MIN(maxlen, MAX_STATUSMESSAGE_LENGTH) - 1); | 330 | memcpy(buf, m->friendlist[friendnumber].statusmessage, MIN(maxlen, MAX_STATUSMESSAGE_LENGTH) - 1); |
363 | return 0; | 331 | return 0; |
364 | } | 332 | } |
365 | 333 | ||
366 | int m_copy_self_statusmessage(uint8_t * buf, uint32_t maxlen) | 334 | int m_copy_self_statusmessage(Messenger *m, uint8_t * buf, uint32_t maxlen) |
367 | { | 335 | { |
368 | memset(buf, 0, maxlen); | 336 | memset(buf, 0, maxlen); |
369 | memcpy(buf, self_statusmessage, MIN(maxlen, MAX_STATUSMESSAGE_LENGTH) - 1); | 337 | memcpy(buf, m->statusmessage, MIN(maxlen, MAX_STATUSMESSAGE_LENGTH) - 1); |
370 | return 0; | 338 | return 0; |
371 | } | 339 | } |
372 | 340 | ||
373 | USERSTATUS m_get_userstatus(int friendnumber) | 341 | USERSTATUS m_get_userstatus(Messenger *m, int friendnumber) |
374 | { | 342 | { |
375 | if (friendnumber >= numfriends || friendnumber < 0) | 343 | if (friendnumber >= m->numfriends || friendnumber < 0) |
376 | return USERSTATUS_INVALID; | 344 | return USERSTATUS_INVALID; |
377 | USERSTATUS status = friendlist[friendnumber].userstatus; | 345 | USERSTATUS status = m->friendlist[friendnumber].userstatus; |
378 | if (status >= USERSTATUS_INVALID) { | 346 | if (status >= USERSTATUS_INVALID) { |
379 | status = USERSTATUS_NONE; | 347 | status = USERSTATUS_NONE; |
380 | } | 348 | } |
381 | return status; | 349 | return status; |
382 | } | 350 | } |
383 | 351 | ||
384 | USERSTATUS m_get_self_userstatus(void) | 352 | USERSTATUS m_get_self_userstatus(Messenger *m) |
385 | { | 353 | { |
386 | return self_userstatus; | 354 | return m->userstatus; |
387 | } | 355 | } |
388 | 356 | ||
389 | static int send_statusmessage(int friendnumber, uint8_t * status, uint16_t length) | 357 | static int send_statusmessage(Messenger *m, int friendnumber, uint8_t * status, uint16_t length) |
390 | { | 358 | { |
391 | return write_cryptpacket_id(friendnumber, PACKET_ID_STATUSMESSAGE, status, length); | 359 | return write_cryptpacket_id(m, friendnumber, PACKET_ID_STATUSMESSAGE, status, length); |
392 | } | 360 | } |
393 | 361 | ||
394 | static int send_userstatus(int friendnumber, USERSTATUS status) | 362 | static int send_userstatus(Messenger *m, int friendnumber, USERSTATUS status) |
395 | { | 363 | { |
396 | uint8_t stat = status; | 364 | uint8_t stat = status; |
397 | return write_cryptpacket_id(friendnumber, PACKET_ID_USERSTATUS, &stat, sizeof(stat)); | 365 | return write_cryptpacket_id(m, friendnumber, PACKET_ID_USERSTATUS, &stat, sizeof(stat)); |
398 | } | 366 | } |
399 | 367 | ||
400 | static int set_friend_statusmessage(int friendnumber, uint8_t * status, uint16_t length) | 368 | static int set_friend_statusmessage(Messenger *m, int friendnumber, uint8_t * status, uint16_t length) |
401 | { | 369 | { |
402 | if (friendnumber >= numfriends || friendnumber < 0) | 370 | if (friendnumber >= m->numfriends || friendnumber < 0) |
403 | return -1; | 371 | return -1; |
404 | uint8_t *newstatus = calloc(length, 1); | 372 | uint8_t *newstatus = calloc(length, 1); |
405 | memcpy(newstatus, status, length); | 373 | memcpy(newstatus, status, length); |
406 | free(friendlist[friendnumber].statusmessage); | 374 | free(m->friendlist[friendnumber].statusmessage); |
407 | friendlist[friendnumber].statusmessage = newstatus; | 375 | m->friendlist[friendnumber].statusmessage = newstatus; |
408 | friendlist[friendnumber].statusmessage_length = length; | 376 | m->friendlist[friendnumber].statusmessage_length = length; |
409 | return 0; | 377 | return 0; |
410 | } | 378 | } |
411 | 379 | ||
412 | static void set_friend_userstatus(int friendnumber, USERSTATUS status) | 380 | static void set_friend_userstatus(Messenger *m, int friendnumber, USERSTATUS status) |
413 | { | 381 | { |
414 | friendlist[friendnumber].userstatus = status; | 382 | m->friendlist[friendnumber].userstatus = status; |
415 | } | 383 | } |
416 | 384 | ||
417 | /* Sets whether we send read receipts for friendnumber. */ | 385 | /* Sets whether we send read receipts for friendnumber. */ |
418 | void m_set_sends_receipts(int friendnumber, int yesno) | 386 | void m_set_sends_receipts(Messenger *m, int friendnumber, int yesno) |
419 | { | 387 | { |
420 | if (yesno != 0 || yesno != 1) | 388 | if (yesno != 0 || yesno != 1) |
421 | return; | 389 | return; |
422 | if (friendnumber >= numfriends || friendnumber < 0) | 390 | if (friendnumber >= m->numfriends || friendnumber < 0) |
423 | return; | 391 | return; |
424 | friendlist[friendnumber].receives_read_receipts = yesno; | 392 | m->friendlist[friendnumber].receives_read_receipts = yesno; |
425 | } | 393 | } |
426 | 394 | ||
427 | /* static void (*friend_request)(uint8_t *, uint8_t *, uint16_t); | 395 | /* static void (*friend_request)(uint8_t *, uint8_t *, uint16_t); |
428 | static uint8_t friend_request_isset = 0; */ | 396 | static uint8_t friend_request_isset = 0; */ |
429 | /* set the function that will be executed when a friend request is received. */ | 397 | /* set the function that will be executed when a friend request is received. */ |
430 | void m_callback_friendrequest(void (*function)(uint8_t *, uint8_t *, uint16_t)) | 398 | void m_callback_friendrequest(Messenger *m, void (*function)(uint8_t *, uint8_t *, uint16_t)) |
431 | { | 399 | { |
432 | callback_friendrequest(function); | 400 | callback_friendrequest(function); |
433 | } | 401 | } |
434 | 402 | ||
435 | static void (*friend_message)(int, uint8_t *, uint16_t); | ||
436 | static uint8_t friend_message_isset = 0; | ||
437 | |||
438 | /* set the function that will be executed when a message from a friend is received. */ | 403 | /* set the function that will be executed when a message from a friend is received. */ |
439 | void m_callback_friendmessage(void (*function)(int, uint8_t *, uint16_t)) | 404 | void m_callback_friendmessage(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t)) |
440 | { | 405 | { |
441 | friend_message = function; | 406 | m->friend_message = function; |
442 | friend_message_isset = 1; | 407 | m->friend_message_isset = 1; |
443 | } | 408 | } |
444 | 409 | ||
445 | static void (*friend_action)(int, uint8_t *, uint16_t); | 410 | void m_callback_action(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t)) |
446 | static uint8_t friend_action_isset = 0; | ||
447 | void m_callback_action(void (*function)(int, uint8_t *, uint16_t)) | ||
448 | { | 411 | { |
449 | friend_action = function; | 412 | m->friend_action = function; |
450 | friend_action_isset = 1; | 413 | m->friend_action_isset = 1; |
451 | } | 414 | } |
452 | 415 | ||
453 | static void (*friend_namechange)(int, uint8_t *, uint16_t); | 416 | void m_callback_namechange(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t)) |
454 | static uint8_t friend_namechange_isset = 0; | ||
455 | void m_callback_namechange(void (*function)(int, uint8_t *, uint16_t)) | ||
456 | { | 417 | { |
457 | friend_namechange = function; | 418 | m->friend_namechange = function; |
458 | friend_namechange_isset = 1; | 419 | m->friend_namechange_isset = 1; |
459 | } | 420 | } |
460 | 421 | ||
461 | static void (*friend_statusmessagechange)(int, uint8_t *, uint16_t); | 422 | void m_callback_statusmessage(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t)) |
462 | static uint8_t friend_statusmessagechange_isset = 0; | ||
463 | void m_callback_statusmessage(void (*function)(int, uint8_t *, uint16_t)) | ||
464 | { | 423 | { |
465 | friend_statusmessagechange = function; | 424 | m->friend_statusmessagechange = function; |
466 | friend_statusmessagechange_isset = 1; | 425 | m->friend_statusmessagechange_isset = 1; |
467 | } | 426 | } |
468 | 427 | ||
469 | static void (*friend_userstatuschange)(int, USERSTATUS); | 428 | void m_callback_userstatus(Messenger *m, void (*function)(Messenger *m, int, USERSTATUS)) |
470 | static uint8_t friend_userstatuschange_isset = 0; | ||
471 | void m_callback_userstatus(void (*function)(int, USERSTATUS)) | ||
472 | { | 429 | { |
473 | friend_userstatuschange = function; | 430 | m->friend_userstatuschange = function; |
474 | friend_userstatuschange_isset = 1; | 431 | m->friend_userstatuschange_isset = 1; |
475 | } | 432 | } |
476 | 433 | ||
477 | static void (*read_receipt)(int, uint32_t); | 434 | void m_callback_read_receipt(Messenger *m, void (*function)(Messenger *m, int, uint32_t)) |
478 | static uint8_t read_receipt_isset = 0; | ||
479 | void m_callback_read_receipt(void (*function)(int, uint32_t)) | ||
480 | { | 435 | { |
481 | read_receipt = function; | 436 | m->read_receipt = function; |
482 | read_receipt_isset = 1; | 437 | m->read_receipt_isset = 1; |
483 | } | 438 | } |
484 | 439 | ||
485 | static void (*friend_connectionstatuschange)(int, uint8_t); | 440 | void m_callback_connectionstatus(Messenger *m, void (*function)(Messenger *m, int, uint8_t)) |
486 | static uint8_t friend_connectionstatuschange_isset = 0; | ||
487 | void m_callback_connectionstatus(void (*function)(int, uint8_t)) | ||
488 | { | 441 | { |
489 | friend_connectionstatuschange = function; | 442 | m->friend_connectionstatuschange = function; |
490 | friend_connectionstatuschange_isset = 1; | 443 | m->friend_connectionstatuschange_isset = 1; |
491 | } | 444 | } |
492 | 445 | ||
493 | static void check_friend_connectionstatus(int friendnumber, uint8_t status) | 446 | static void check_friend_connectionstatus(Messenger *m, int friendnumber, uint8_t status) |
494 | { | 447 | { |
495 | if (!friend_connectionstatuschange_isset) | 448 | if (!m->friend_connectionstatuschange_isset) |
496 | return; | 449 | return; |
497 | if (status == NOFRIEND) | 450 | if (status == NOFRIEND) |
498 | return; | 451 | return; |
499 | const uint8_t was_connected = friendlist[friendnumber].status == FRIEND_ONLINE; | 452 | const uint8_t was_connected = m->friendlist[friendnumber].status == FRIEND_ONLINE; |
500 | const uint8_t is_connected = status == FRIEND_ONLINE; | 453 | const uint8_t is_connected = status == FRIEND_ONLINE; |
501 | if (is_connected != was_connected) | 454 | if (is_connected != was_connected) |
502 | friend_connectionstatuschange(friendnumber, is_connected); | 455 | m->friend_connectionstatuschange(m, friendnumber, is_connected); |
503 | } | 456 | } |
504 | 457 | ||
505 | static void set_friend_status(int friendnumber, uint8_t status) | 458 | void set_friend_status(Messenger *m, int friendnumber, uint8_t status) |
506 | { | 459 | { |
507 | check_friend_connectionstatus(friendnumber, status); | 460 | check_friend_connectionstatus(m, friendnumber, status); |
508 | friendlist[friendnumber].status = status; | 461 | m->friendlist[friendnumber].status = status; |
509 | } | 462 | } |
510 | 463 | ||
511 | static int write_cryptpacket_id(int friendnumber, uint8_t packet_id, uint8_t *data, uint32_t length) | 464 | int write_cryptpacket_id(Messenger *m, int friendnumber, uint8_t packet_id, uint8_t *data, uint32_t length) |
512 | { | 465 | { |
513 | if (friendnumber < 0 || friendnumber >= numfriends) | 466 | if (friendnumber < 0 || friendnumber >= m->numfriends) |
514 | return 0; | 467 | return 0; |
515 | if (length >= MAX_DATA_SIZE || friendlist[friendnumber].status != FRIEND_ONLINE) | 468 | if (length >= MAX_DATA_SIZE || m->friendlist[friendnumber].status != FRIEND_ONLINE) |
516 | return 0; | 469 | return 0; |
517 | uint8_t packet[length + 1]; | 470 | uint8_t packet[length + 1]; |
518 | packet[0] = packet_id; | 471 | packet[0] = packet_id; |
519 | memcpy(packet + 1, data, length); | 472 | memcpy(packet + 1, data, length); |
520 | return write_cryptpacket(friendlist[friendnumber].crypt_connection_id, packet, length + 1); | 473 | return write_cryptpacket(m->friendlist[friendnumber].crypt_connection_id, packet, length + 1); |
521 | } | 474 | } |
522 | 475 | ||
523 | #define PORT 33445 | 476 | #define PORT 33445 |
524 | /* run this at startup */ | 477 | /* run this at startup */ |
525 | int initMessenger(void) | 478 | Messenger * initMessenger(void) |
526 | { | 479 | { |
480 | Messenger *m = calloc(1, sizeof(Messenger)); | ||
481 | if( ! m ) | ||
482 | return 0; | ||
483 | |||
527 | new_keys(); | 484 | new_keys(); |
528 | m_set_statusmessage((uint8_t*)"Online", sizeof("Online")); | 485 | m_set_statusmessage(m, (uint8_t*)"Online", sizeof("Online")); |
529 | initNetCrypto(); | 486 | initNetCrypto(); |
530 | IP ip; | 487 | IP ip; |
531 | ip.i = 0; | 488 | ip.i = 0; |
532 | 489 | ||
533 | if(init_networking(ip,PORT) == -1) | 490 | if(init_networking(ip,PORT) == -1) |
534 | return -1; | 491 | return 0; |
535 | 492 | ||
536 | DHT_init(); | 493 | DHT_init(); |
537 | LosslessUDP_init(); | 494 | LosslessUDP_init(); |
538 | friendreq_init(); | 495 | friendreq_init(); |
539 | LANdiscovery_init(); | 496 | LANdiscovery_init(); |
540 | 497 | ||
541 | return 0; | 498 | return m; |
499 | } | ||
500 | |||
501 | /* run this before closing shop */ | ||
502 | void cleanupMessenger(Messenger *m){ | ||
503 | /* FIXME TODO it seems no one frees friendlist or all the elements status */ | ||
504 | free(m); | ||
542 | } | 505 | } |
543 | 506 | ||
544 | //TODO: make this function not suck. | 507 | //TODO: make this function not suck. |
545 | static void doFriends(void) | 508 | void doFriends(Messenger *m) |
546 | { | 509 | { |
547 | /* TODO: add incoming connections and some other stuff. */ | 510 | /* TODO: add incoming connections and some other stuff. */ |
548 | uint32_t i; | 511 | uint32_t i; |
549 | int len; | 512 | int len; |
550 | uint8_t temp[MAX_DATA_SIZE]; | 513 | uint8_t temp[MAX_DATA_SIZE]; |
551 | for (i = 0; i < numfriends; ++i) { | 514 | for (i = 0; i < m->numfriends; ++i) { |
552 | if (friendlist[i].status == FRIEND_ADDED) { | 515 | if (m->friendlist[i].status == FRIEND_ADDED) { |
553 | int fr = send_friendrequest(friendlist[i].client_id, friendlist[i].info, friendlist[i].info_size); | 516 | int fr = send_friendrequest(m->friendlist[i].client_id, m->friendlist[i].info, m->friendlist[i].info_size); |
554 | if (fr == 0) /* TODO: This needs to be fixed so that it sends the friend requests a couple of times in case of packet loss */ | 517 | if (fr == 0) /* TODO: This needs to be fixed so that it sends the friend requests a couple of times in case of packet loss */ |
555 | set_friend_status(i, FRIEND_REQUESTED); | 518 | set_friend_status(m, i, FRIEND_REQUESTED); |
556 | else if (fr > 0) | 519 | else if (fr > 0) |
557 | set_friend_status(i, FRIEND_REQUESTED); | 520 | set_friend_status(m, i, FRIEND_REQUESTED); |
558 | } | 521 | } |
559 | if (friendlist[i].status == FRIEND_REQUESTED || friendlist[i].status == FRIEND_CONFIRMED) { /* friend is not online */ | 522 | if (m->friendlist[i].status == FRIEND_REQUESTED || m->friendlist[i].status == FRIEND_CONFIRMED) { /* friend is not online */ |
560 | if (friendlist[i].status == FRIEND_REQUESTED) { | 523 | if (m->friendlist[i].status == FRIEND_REQUESTED) { |
561 | if (friendlist[i].friend_request_id + 10 < unix_time()) { /*I know this is hackish but it should work.*/ | 524 | if (m->friendlist[i].friend_request_id + 10 < unix_time()) { /*I know this is hackish but it should work.*/ |
562 | send_friendrequest(friendlist[i].client_id, friendlist[i].info, friendlist[i].info_size); | 525 | send_friendrequest(m->friendlist[i].client_id, m->friendlist[i].info, m->friendlist[i].info_size); |
563 | friendlist[i].friend_request_id = unix_time(); | 526 | m->friendlist[i].friend_request_id = unix_time(); |
564 | } | 527 | } |
565 | } | 528 | } |
566 | IP_Port friendip = DHT_getfriendip(friendlist[i].client_id); | 529 | IP_Port friendip = DHT_getfriendip(m->friendlist[i].client_id); |
567 | switch (is_cryptoconnected(friendlist[i].crypt_connection_id)) { | 530 | switch (is_cryptoconnected(m->friendlist[i].crypt_connection_id)) { |
568 | case 0: | 531 | case 0: |
569 | if (friendip.ip.i > 1) | 532 | if (friendip.ip.i > 1) |
570 | friendlist[i].crypt_connection_id = crypto_connect(friendlist[i].client_id, friendip); | 533 | m->friendlist[i].crypt_connection_id = crypto_connect(m->friendlist[i].client_id, friendip); |
571 | break; | 534 | break; |
572 | case 3: /* Connection is established */ | 535 | case 3: /* Connection is established */ |
573 | set_friend_status(i, FRIEND_ONLINE); | 536 | set_friend_status(m, i, FRIEND_ONLINE); |
574 | friendlist[i].name_sent = 0; | 537 | m->friendlist[i].name_sent = 0; |
575 | friendlist[i].userstatus_sent = 0; | 538 | m->friendlist[i].userstatus_sent = 0; |
576 | friendlist[i].statusmessage_sent = 0; | 539 | m->friendlist[i].statusmessage_sent = 0; |
577 | break; | 540 | break; |
578 | case 4: | 541 | case 4: |
579 | crypto_kill(friendlist[i].crypt_connection_id); | 542 | crypto_kill(m->friendlist[i].crypt_connection_id); |
580 | friendlist[i].crypt_connection_id = -1; | 543 | m->friendlist[i].crypt_connection_id = -1; |
581 | break; | 544 | break; |
582 | default: | 545 | default: |
583 | break; | 546 | break; |
584 | } | 547 | } |
585 | } | 548 | } |
586 | while (friendlist[i].status == FRIEND_ONLINE) { /* friend is online */ | 549 | while (m->friendlist[i].status == FRIEND_ONLINE) { /* friend is online */ |
587 | if (friendlist[i].name_sent == 0) { | 550 | if (m->friendlist[i].name_sent == 0) { |
588 | if (m_sendname(i, self_name, self_name_length)) | 551 | if (m_sendname(m, i, m->name, m->name_length)) |
589 | friendlist[i].name_sent = 1; | 552 | m->friendlist[i].name_sent = 1; |
590 | } | 553 | } |
591 | if (friendlist[i].statusmessage_sent == 0) { | 554 | if (m->friendlist[i].statusmessage_sent == 0) { |
592 | if (send_statusmessage(i, self_statusmessage, self_statusmessage_length)) | 555 | if (send_statusmessage(m, i, m->statusmessage, m->statusmessage_length)) |
593 | friendlist[i].statusmessage_sent = 1; | 556 | m->friendlist[i].statusmessage_sent = 1; |
594 | } | 557 | } |
595 | if (friendlist[i].userstatus_sent == 0) { | 558 | if (m->friendlist[i].userstatus_sent == 0) { |
596 | if (send_userstatus(i, self_userstatus)) | 559 | if (send_userstatus(m, i, m->userstatus)) |
597 | friendlist[i].userstatus_sent = 1; | 560 | m->friendlist[i].userstatus_sent = 1; |
598 | } | 561 | } |
599 | len = read_cryptpacket(friendlist[i].crypt_connection_id, temp); | 562 | len = read_cryptpacket(m->friendlist[i].crypt_connection_id, temp); |
600 | uint8_t packet_id = temp[0]; | 563 | uint8_t packet_id = temp[0]; |
601 | uint8_t* data = temp + 1; | 564 | uint8_t* data = temp + 1; |
602 | int data_length = len - 1; | 565 | int data_length = len - 1; |
@@ -605,10 +568,10 @@ static void doFriends(void) | |||
605 | case PACKET_ID_NICKNAME: { | 568 | case PACKET_ID_NICKNAME: { |
606 | if (data_length >= MAX_NAME_LENGTH || data_length == 0) | 569 | if (data_length >= MAX_NAME_LENGTH || data_length == 0) |
607 | break; | 570 | break; |
608 | if(friend_namechange_isset) | 571 | if(m->friend_namechange_isset) |
609 | friend_namechange(i, data, data_length); | 572 | m->friend_namechange(m, i, data, data_length); |
610 | memcpy(friendlist[i].name, data, data_length); | 573 | memcpy(m->friendlist[i].name, data, data_length); |
611 | friendlist[i].name[data_length - 1] = 0; /* make sure the NULL terminator is present. */ | 574 | m->friendlist[i].name[data_length - 1] = 0; /* make sure the NULL terminator is present. */ |
612 | break; | 575 | break; |
613 | } | 576 | } |
614 | case PACKET_ID_STATUSMESSAGE: { | 577 | case PACKET_ID_STATUSMESSAGE: { |
@@ -616,9 +579,9 @@ static void doFriends(void) | |||
616 | break; | 579 | break; |
617 | uint8_t *status = calloc(MIN(data_length, MAX_STATUSMESSAGE_LENGTH), 1); | 580 | uint8_t *status = calloc(MIN(data_length, MAX_STATUSMESSAGE_LENGTH), 1); |
618 | memcpy(status, data, MIN(data_length, MAX_STATUSMESSAGE_LENGTH)); | 581 | memcpy(status, data, MIN(data_length, MAX_STATUSMESSAGE_LENGTH)); |
619 | if (friend_statusmessagechange_isset) | 582 | if (m->friend_statusmessagechange_isset) |
620 | friend_statusmessagechange(i, status, MIN(data_length, MAX_STATUSMESSAGE_LENGTH)); | 583 | m->friend_statusmessagechange(m, i, status, MIN(data_length, MAX_STATUSMESSAGE_LENGTH)); |
621 | set_friend_statusmessage(i, status, MIN(data_length, MAX_STATUSMESSAGE_LENGTH)); | 584 | set_friend_statusmessage(m, i, status, MIN(data_length, MAX_STATUSMESSAGE_LENGTH)); |
622 | free(status); | 585 | free(status); |
623 | break; | 586 | break; |
624 | } | 587 | } |
@@ -626,9 +589,9 @@ static void doFriends(void) | |||
626 | if (data_length != 1) | 589 | if (data_length != 1) |
627 | break; | 590 | break; |
628 | USERSTATUS status = data[0]; | 591 | USERSTATUS status = data[0]; |
629 | if (friend_userstatuschange_isset) | 592 | if (m->friend_userstatuschange_isset) |
630 | friend_userstatuschange(i, status); | 593 | m->friend_userstatuschange(m, i, status); |
631 | set_friend_userstatus(i, status); | 594 | set_friend_userstatus(m, i, status); |
632 | break; | 595 | break; |
633 | } | 596 | } |
634 | case PACKET_ID_MESSAGE: { | 597 | case PACKET_ID_MESSAGE: { |
@@ -636,16 +599,16 @@ static void doFriends(void) | |||
636 | uint8_t message_id_length = 4; | 599 | uint8_t message_id_length = 4; |
637 | uint8_t *message = data + message_id_length; | 600 | uint8_t *message = data + message_id_length; |
638 | uint16_t message_length = data_length - message_id_length; | 601 | uint16_t message_length = data_length - message_id_length; |
639 | if (friendlist[i].receives_read_receipts) { | 602 | if (m->friendlist[i].receives_read_receipts) { |
640 | write_cryptpacket_id(i, PACKET_ID_RECEIPT, message_id, message_id_length); | 603 | write_cryptpacket_id(m, i, PACKET_ID_RECEIPT, message_id, message_id_length); |
641 | } | 604 | } |
642 | if (friend_message_isset) | 605 | if (m->friend_message_isset) |
643 | (*friend_message)(i, message, message_length); | 606 | (*m->friend_message)(m, i, message, message_length); |
644 | break; | 607 | break; |
645 | } | 608 | } |
646 | case PACKET_ID_ACTION: { | 609 | case PACKET_ID_ACTION: { |
647 | if (friend_action_isset) | 610 | if (m->friend_action_isset) |
648 | (*friend_action)(i, data, data_length); | 611 | (*m->friend_action)(m, i, data, data_length); |
649 | break; | 612 | break; |
650 | } | 613 | } |
651 | case PACKET_ID_RECEIPT: { | 614 | case PACKET_ID_RECEIPT: { |
@@ -654,16 +617,16 @@ static void doFriends(void) | |||
654 | break; | 617 | break; |
655 | memcpy(&msgid, data, sizeof(msgid)); | 618 | memcpy(&msgid, data, sizeof(msgid)); |
656 | msgid = ntohl(msgid); | 619 | msgid = ntohl(msgid); |
657 | if (read_receipt_isset) | 620 | if (m->read_receipt_isset) |
658 | (*read_receipt)(i, msgid); | 621 | (*m->read_receipt)(m, i, msgid); |
659 | break; | 622 | break; |
660 | } | 623 | } |
661 | } | 624 | } |
662 | } else { | 625 | } else { |
663 | if (is_cryptoconnected(friendlist[i].crypt_connection_id) == 4) { /* if the connection timed out, kill it */ | 626 | if (is_cryptoconnected(m->friendlist[i].crypt_connection_id) == 4) { /* if the connection timed out, kill it */ |
664 | crypto_kill(friendlist[i].crypt_connection_id); | 627 | crypto_kill(m->friendlist[i].crypt_connection_id); |
665 | friendlist[i].crypt_connection_id = -1; | 628 | m->friendlist[i].crypt_connection_id = -1; |
666 | set_friend_status(i, FRIEND_CONFIRMED); | 629 | set_friend_status(m, i, FRIEND_CONFIRMED); |
667 | } | 630 | } |
668 | break; | 631 | break; |
669 | } | 632 | } |
@@ -671,20 +634,20 @@ static void doFriends(void) | |||
671 | } | 634 | } |
672 | } | 635 | } |
673 | 636 | ||
674 | static void doInbound(void) | 637 | void doInbound(Messenger *m) |
675 | { | 638 | { |
676 | uint8_t secret_nonce[crypto_box_NONCEBYTES]; | 639 | uint8_t secret_nonce[crypto_box_NONCEBYTES]; |
677 | uint8_t public_key[crypto_box_PUBLICKEYBYTES]; | 640 | uint8_t public_key[crypto_box_PUBLICKEYBYTES]; |
678 | uint8_t session_key[crypto_box_PUBLICKEYBYTES]; | 641 | uint8_t session_key[crypto_box_PUBLICKEYBYTES]; |
679 | int inconnection = crypto_inbound(public_key, secret_nonce, session_key); | 642 | int inconnection = crypto_inbound(public_key, secret_nonce, session_key); |
680 | if (inconnection != -1) { | 643 | if (inconnection != -1) { |
681 | int friend_id = getfriend_id(public_key); | 644 | int friend_id = getfriend_id(m, public_key); |
682 | if (friend_id != -1) { | 645 | if (friend_id != -1) { |
683 | crypto_kill(friendlist[friend_id].crypt_connection_id); | 646 | crypto_kill(m->friendlist[friend_id].crypt_connection_id); |
684 | friendlist[friend_id].crypt_connection_id = | 647 | m->friendlist[friend_id].crypt_connection_id = |
685 | accept_crypto_inbound(inconnection, public_key, secret_nonce, session_key); | 648 | accept_crypto_inbound(inconnection, public_key, secret_nonce, session_key); |
686 | 649 | ||
687 | set_friend_status(friend_id, FRIEND_CONFIRMED); | 650 | set_friend_status(m, friend_id, FRIEND_CONFIRMED); |
688 | } | 651 | } |
689 | } | 652 | } |
690 | } | 653 | } |
@@ -695,7 +658,7 @@ static void doInbound(void) | |||
695 | static uint64_t last_LANdiscovery; | 658 | static uint64_t last_LANdiscovery; |
696 | 659 | ||
697 | /*Send a LAN discovery packet every LAN_DISCOVERY_INTERVAL seconds*/ | 660 | /*Send a LAN discovery packet every LAN_DISCOVERY_INTERVAL seconds*/ |
698 | static void LANdiscovery(void) | 661 | void LANdiscovery(Messenger *m) |
699 | { | 662 | { |
700 | if (last_LANdiscovery + LAN_DISCOVERY_INTERVAL < unix_time()) { | 663 | if (last_LANdiscovery + LAN_DISCOVERY_INTERVAL < unix_time()) { |
701 | send_LANdiscovery(htons(PORT)); | 664 | send_LANdiscovery(htons(PORT)); |
@@ -705,27 +668,27 @@ static void LANdiscovery(void) | |||
705 | 668 | ||
706 | 669 | ||
707 | /* the main loop that needs to be run at least 200 times per second. */ | 670 | /* the main loop that needs to be run at least 200 times per second. */ |
708 | void doMessenger(void) | 671 | void doMessenger(Messenger *m) |
709 | { | 672 | { |
710 | networking_poll(); | 673 | networking_poll(); |
711 | 674 | ||
712 | doDHT(); | 675 | doDHT(); |
713 | doLossless_UDP(); | 676 | doLossless_UDP(); |
714 | doNetCrypto(); | 677 | doNetCrypto(); |
715 | doInbound(); | 678 | doInbound(m); |
716 | doFriends(); | 679 | doFriends(m); |
717 | LANdiscovery(); | 680 | LANdiscovery(m); |
718 | } | 681 | } |
719 | 682 | ||
720 | /* returns the size of the messenger data (for saving) */ | 683 | /* returns the size of the messenger data (for saving) */ |
721 | uint32_t Messenger_size(void) | 684 | uint32_t Messenger_size(Messenger *m) |
722 | { | 685 | { |
723 | return crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES | 686 | return crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES |
724 | + sizeof(uint32_t) + DHT_size() + sizeof(uint32_t) + sizeof(Friend) * numfriends; | 687 | + sizeof(uint32_t) + DHT_size() + sizeof(uint32_t) + sizeof(Friend) * m->numfriends; |
725 | } | 688 | } |
726 | 689 | ||
727 | /* save the messenger in data of size Messenger_size() */ | 690 | /* save the messenger in data of size Messenger_size() */ |
728 | void Messenger_save(uint8_t *data) | 691 | void Messenger_save(Messenger *m, uint8_t *data) |
729 | { | 692 | { |
730 | save_keys(data); | 693 | save_keys(data); |
731 | data += crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES; | 694 | data += crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES; |
@@ -734,14 +697,14 @@ void Messenger_save(uint8_t *data) | |||
734 | data += sizeof(size); | 697 | data += sizeof(size); |
735 | DHT_save(data); | 698 | DHT_save(data); |
736 | data += size; | 699 | data += size; |
737 | size = sizeof(Friend) * numfriends; | 700 | size = sizeof(Friend) * m->numfriends; |
738 | memcpy(data, &size, sizeof(size)); | 701 | memcpy(data, &size, sizeof(size)); |
739 | data += sizeof(size); | 702 | data += sizeof(size); |
740 | memcpy(data, friendlist, sizeof(Friend) * numfriends); | 703 | memcpy(data, m->friendlist, sizeof(Friend) * m->numfriends); |
741 | } | 704 | } |
742 | 705 | ||
743 | /* load the messenger from data of size length. */ | 706 | /* load the messenger from data of size length. */ |
744 | int Messenger_load(uint8_t * data, uint32_t length) | 707 | int Messenger_load(Messenger *m, uint8_t * data, uint32_t length) |
745 | { | 708 | { |
746 | if (length == ~0) | 709 | if (length == ~0) |
747 | return -1; | 710 | return -1; |
@@ -773,11 +736,12 @@ int Messenger_load(uint8_t * data, uint32_t length) | |||
773 | uint32_t i; | 736 | uint32_t i; |
774 | for (i = 0; i < num; ++i) { | 737 | for (i = 0; i < num; ++i) { |
775 | if(temp[i].status != 0) { | 738 | if(temp[i].status != 0) { |
776 | int fnum = m_addfriend_norequest(temp[i].client_id); | 739 | int fnum = m_addfriend_norequest(m, temp[i].client_id); |
777 | setfriendname(fnum, temp[i].name); | 740 | setfriendname(m, fnum, temp[i].name); |
778 | /* set_friend_statusmessage(fnum, temp[i].statusmessage, temp[i].statusmessage_length); */ | 741 | /* set_friend_statusmessage(fnum, temp[i].statusmessage, temp[i].statusmessage_length); */ |
779 | } | 742 | } |
780 | } | 743 | } |
781 | free(temp); | 744 | free(temp); |
782 | return 0; | 745 | return 0; |
783 | } | 746 | } |
747 | |||
diff --git a/core/Messenger.h b/core/Messenger.h index 9352cfbb..36e24596 100644 --- a/core/Messenger.h +++ b/core/Messenger.h | |||
@@ -73,6 +73,58 @@ typedef enum { | |||
73 | USERSTATUS_INVALID | 73 | USERSTATUS_INVALID |
74 | } USERSTATUS; | 74 | } USERSTATUS; |
75 | 75 | ||
76 | typedef struct { | ||
77 | uint8_t client_id[CLIENT_ID_SIZE]; | ||
78 | int crypt_connection_id; | ||
79 | uint64_t friend_request_id; /* id of the friend request corresponding to the current friend request to the current friend. */ | ||
80 | uint8_t status; /* 0 if no friend, 1 if added, 2 if friend request sent, 3 if confirmed friend, 4 if online. */ | ||
81 | uint8_t info[MAX_DATA_SIZE]; /* the data that is sent during the friend requests we do */ | ||
82 | uint8_t name[MAX_NAME_LENGTH]; | ||
83 | uint8_t name_sent; /* 0 if we didn't send our name to this friend 1 if we have. */ | ||
84 | uint8_t *statusmessage; | ||
85 | uint16_t statusmessage_length; | ||
86 | uint8_t statusmessage_sent; | ||
87 | USERSTATUS userstatus; | ||
88 | uint8_t userstatus_sent; | ||
89 | uint16_t info_size; /* length of the info */ | ||
90 | uint32_t message_id; /* a semi-unique id used in read receipts */ | ||
91 | uint8_t receives_read_receipts; /* shall we send read receipts to this person? */ | ||
92 | } Friend; | ||
93 | |||
94 | typedef struct Messenger { | ||
95 | uint8_t public_key[crypto_box_PUBLICKEYBYTES]; | ||
96 | |||
97 | uint8_t name[MAX_NAME_LENGTH]; | ||
98 | uint16_t name_length; | ||
99 | |||
100 | uint8_t statusmessage[MAX_STATUSMESSAGE_LENGTH]; | ||
101 | uint16_t statusmessage_length; | ||
102 | |||
103 | USERSTATUS userstatus; | ||
104 | |||
105 | Friend *friendlist; | ||
106 | uint32_t numfriends; | ||
107 | |||
108 | void (*friend_message)(struct Messenger *m, int, uint8_t *, uint16_t); | ||
109 | uint8_t friend_message_isset; | ||
110 | void (*friend_action)(struct Messenger *m, int, uint8_t *, uint16_t); | ||
111 | uint8_t friend_action_isset; | ||
112 | void (*friend_namechange)(struct Messenger *m, int, uint8_t *, uint16_t); | ||
113 | uint8_t friend_namechange_isset; | ||
114 | void (*friend_statusmessagechange)(struct Messenger *m, int, uint8_t *, uint16_t); | ||
115 | uint8_t friend_statusmessagechange_isset; | ||
116 | void (*friend_userstatuschange)(struct Messenger *m, int, USERSTATUS); | ||
117 | uint8_t friend_userstatuschange_isset; | ||
118 | void (*read_receipt)(struct Messenger *m, int, uint32_t); | ||
119 | uint8_t read_receipt_isset; | ||
120 | void (*friend_statuschange)(struct Messenger *m, int, uint8_t); | ||
121 | uint8_t friend_statuschange_isset; | ||
122 | void (*friend_connectionstatuschange)(struct Messenger *m, int, uint8_t); | ||
123 | uint8_t friend_connectionstatuschange_isset; | ||
124 | |||
125 | |||
126 | } Messenger; | ||
127 | |||
76 | /* | 128 | /* |
77 | * add a friend | 129 | * add a friend |
78 | * set the data that will be sent along with friend request | 130 | * set the data that will be sent along with friend request |
@@ -85,33 +137,33 @@ typedef enum { | |||
85 | * return -4 if friend request already sent or already a friend | 137 | * return -4 if friend request already sent or already a friend |
86 | * return -5 for unknown error | 138 | * return -5 for unknown error |
87 | */ | 139 | */ |
88 | int m_addfriend(uint8_t *client_id, uint8_t *data, uint16_t length); | 140 | int m_addfriend(Messenger *m, uint8_t *client_id, uint8_t *data, uint16_t length); |
89 | 141 | ||
90 | 142 | ||
91 | /* add a friend without sending a friendrequest. | 143 | /* add a friend without sending a friendrequest. |
92 | returns the friend number if success | 144 | returns the friend number if success |
93 | return -1 if failure. */ | 145 | return -1 if failure. */ |
94 | int m_addfriend_norequest(uint8_t *client_id); | 146 | int m_addfriend_norequest(Messenger *m, uint8_t *client_id); |
95 | 147 | ||
96 | /* return the friend id associated to that client id. | 148 | /* return the friend id associated to that client id. |
97 | return -1 if no such friend */ | 149 | return -1 if no such friend */ |
98 | int getfriend_id(uint8_t *client_id); | 150 | int getfriend_id(Messenger *m, uint8_t *client_id); |
99 | 151 | ||
100 | /* copies the public key associated to that friend id into client_id buffer. | 152 | /* copies the public key associated to that friend id into client_id buffer. |
101 | make sure that client_id is of size CLIENT_ID_SIZE. | 153 | make sure that client_id is of size CLIENT_ID_SIZE. |
102 | return 0 if success | 154 | return 0 if success |
103 | return -1 if failure */ | 155 | return -1 if failure */ |
104 | int getclient_id(int friend_id, uint8_t *client_id); | 156 | int getclient_id(Messenger *m, int friend_id, uint8_t *client_id); |
105 | 157 | ||
106 | /* remove a friend */ | 158 | /* remove a friend */ |
107 | int m_delfriend(int friendnumber); | 159 | int m_delfriend(Messenger *m, int friendnumber); |
108 | 160 | ||
109 | /* return 4 if friend is online | 161 | /* return 4 if friend is online |
110 | return 3 if friend is confirmed | 162 | return 3 if friend is confirmed |
111 | return 2 if the friend request was sent | 163 | return 2 if the friend request was sent |
112 | return 1 if the friend was added | 164 | return 1 if the friend was added |
113 | return 0 if there is no friend with that number */ | 165 | return 0 if there is no friend with that number */ |
114 | int m_friendstatus(int friendnumber); | 166 | int m_friendstatus(Messenger *m, int friendnumber); |
115 | 167 | ||
116 | /* send a text chat message to an online friend | 168 | /* send a text chat message to an online friend |
117 | returns the message id if packet was successfully put into the send queue | 169 | returns the message id if packet was successfully put into the send queue |
@@ -120,13 +172,13 @@ int m_friendstatus(int friendnumber); | |||
120 | if one is received. | 172 | if one is received. |
121 | m_sendmessage_withid will send a message with the id of your choosing, | 173 | m_sendmessage_withid will send a message with the id of your choosing, |
122 | however we can generate an id for you by calling plain m_sendmessage. */ | 174 | however we can generate an id for you by calling plain m_sendmessage. */ |
123 | uint32_t m_sendmessage(int friendnumber, uint8_t *message, uint32_t length); | 175 | uint32_t m_sendmessage(Messenger *m, int friendnumber, uint8_t *message, uint32_t length); |
124 | uint32_t m_sendmessage_withid(int friendnumber, uint32_t theid, uint8_t *message, uint32_t length); | 176 | uint32_t m_sendmessage_withid(Messenger *m, int friendnumber, uint32_t theid, uint8_t *message, uint32_t length); |
125 | 177 | ||
126 | /* send an action to an online friend | 178 | /* send an action to an online friend |
127 | returns 1 if packet was successfully put into the send queue | 179 | returns 1 if packet was successfully put into the send queue |
128 | return 0 if it was not */ | 180 | return 0 if it was not */ |
129 | int m_sendaction(int friendnumber, uint8_t *action, uint32_t length); | 181 | int m_sendaction(Messenger *m, int friendnumber, uint8_t *action, uint32_t length); |
130 | 182 | ||
131 | /* Set our nickname | 183 | /* Set our nickname |
132 | name must be a string of maximum MAX_NAME_LENGTH length. | 184 | name must be a string of maximum MAX_NAME_LENGTH length. |
@@ -134,73 +186,73 @@ int m_sendaction(int friendnumber, uint8_t *action, uint32_t length); | |||
134 | length is the length of name with the NULL terminator | 186 | length is the length of name with the NULL terminator |
135 | return 0 if success | 187 | return 0 if success |
136 | return -1 if failure */ | 188 | return -1 if failure */ |
137 | int setname(uint8_t *name, uint16_t length); | 189 | int setname(Messenger *m, uint8_t *name, uint16_t length); |
138 | 190 | ||
139 | /* get our nickname | 191 | /* get our nickname |
140 | put it in name | 192 | put it in name |
141 | return the length of the name*/ | 193 | return the length of the name*/ |
142 | uint16_t getself_name(uint8_t *name); | 194 | uint16_t getself_name(Messenger *m, uint8_t *name); |
143 | 195 | ||
144 | /* get name of friendnumber | 196 | /* get name of friendnumber |
145 | put it in name | 197 | put it in name |
146 | name needs to be a valid memory location with a size of at least MAX_NAME_LENGTH (128) bytes. | 198 | name needs to be a valid memory location with a size of at least MAX_NAME_LENGTH (128) bytes. |
147 | return 0 if success | 199 | return 0 if success |
148 | return -1 if failure */ | 200 | return -1 if failure */ |
149 | int getname(int friendnumber, uint8_t *name); | 201 | int getname(Messenger *m, int friendnumber, uint8_t *name); |
150 | 202 | ||
151 | /* set our user status | 203 | /* set our user status |
152 | you are responsible for freeing status after | 204 | you are responsible for freeing status after |
153 | returns 0 on success, -1 on failure */ | 205 | returns 0 on success, -1 on failure */ |
154 | int m_set_statusmessage(uint8_t *status, uint16_t length); | 206 | int m_set_statusmessage(Messenger *m, uint8_t *status, uint16_t length); |
155 | int m_set_userstatus(USERSTATUS status); | 207 | int m_set_userstatus(Messenger *m, USERSTATUS status); |
156 | 208 | ||
157 | /* return the length of friendnumber's status message, | 209 | /* return the length of friendnumber's status message, |
158 | including null | 210 | including null |
159 | pass it into malloc */ | 211 | pass it into malloc */ |
160 | int m_get_statusmessage_size(int friendnumber); | 212 | int m_get_statusmessage_size(Messenger *m, int friendnumber); |
161 | 213 | ||
162 | /* copy friendnumber's status message into buf, truncating if size is over maxlen | 214 | /* copy friendnumber's status message into buf, truncating if size is over maxlen |
163 | get the size you need to allocate from m_get_statusmessage_size | 215 | get the size you need to allocate from m_get_statusmessage_size |
164 | The self variant will copy our own status message. */ | 216 | The self variant will copy our own status message. */ |
165 | int m_copy_statusmessage(int friendnumber, uint8_t *buf, uint32_t maxlen); | 217 | int m_copy_statusmessage(Messenger *m, int friendnumber, uint8_t *buf, uint32_t maxlen); |
166 | int m_copy_self_statusmessage(uint8_t *buf, uint32_t maxlen); | 218 | int m_copy_self_statusmessage(Messenger *m, uint8_t *buf, uint32_t maxlen); |
167 | 219 | ||
168 | /* Return one of USERSTATUS values. | 220 | /* Return one of USERSTATUS values. |
169 | * Values unknown to your application should be represented as USERSTATUS_NONE. | 221 | * Values unknown to your application should be represented as USERSTATUS_NONE. |
170 | * As above, the self variant will return our own USERSTATUS. | 222 | * As above, the self variant will return our own USERSTATUS. |
171 | * If friendnumber is invalid, this shall return USERSTATUS_INVALID. */ | 223 | * If friendnumber is invalid, this shall return USERSTATUS_INVALID. */ |
172 | USERSTATUS m_get_userstatus(int friendnumber); | 224 | USERSTATUS m_get_userstatus(Messenger *m, int friendnumber); |
173 | USERSTATUS m_get_self_userstatus(void); | 225 | USERSTATUS m_get_self_userstatus(Messenger *m); |
174 | 226 | ||
175 | /* Sets whether we send read receipts for friendnumber. | 227 | /* Sets whether we send read receipts for friendnumber. |
176 | * This function is not lazy, and it will fail if yesno is not (0 or 1).*/ | 228 | * This function is not lazy, and it will fail if yesno is not (0 or 1).*/ |
177 | void m_set_sends_receipts(int friendnumber, int yesno); | 229 | void m_set_sends_receipts(Messenger *m, int friendnumber, int yesno); |
178 | 230 | ||
179 | /* set the function that will be executed when a friend request is received. | 231 | /* set the function that will be executed when a friend request is received. |
180 | function format is function(uint8_t * public_key, uint8_t * data, uint16_t length) */ | 232 | function format is function(uint8_t * public_key, uint8_t * data, uint16_t length) */ |
181 | void m_callback_friendrequest(void (*function)(uint8_t *, uint8_t *, uint16_t)); | 233 | void m_callback_friendrequest(Messenger *m, void (*function)(uint8_t *, uint8_t *, uint16_t)); |
182 | 234 | ||
183 | /* set the function that will be executed when a message from a friend is received. | 235 | /* set the function that will be executed when a message from a friend is received. |
184 | function format is: function(int friendnumber, uint8_t * message, uint32_t length) */ | 236 | function format is: function(int friendnumber, uint8_t * message, uint32_t length) */ |
185 | void m_callback_friendmessage(void (*function)(int, uint8_t *, uint16_t)); | 237 | void m_callback_friendmessage(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t)); |
186 | 238 | ||
187 | /* set the function that will be executed when an action from a friend is received. | 239 | /* set the function that will be executed when an action from a friend is received. |
188 | function format is: function(int friendnumber, uint8_t * action, uint32_t length) */ | 240 | function format is: function(int friendnumber, uint8_t * action, uint32_t length) */ |
189 | void m_callback_action(void (*function)(int, uint8_t *, uint16_t)); | 241 | void m_callback_action(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t)); |
190 | 242 | ||
191 | /* set the callback for name changes | 243 | /* set the callback for name changes |
192 | function(int friendnumber, uint8_t *newname, uint16_t length) | 244 | function(int friendnumber, uint8_t *newname, uint16_t length) |
193 | you are not responsible for freeing newname */ | 245 | you are not responsible for freeing newname */ |
194 | void m_callback_namechange(void (*function)(int, uint8_t *, uint16_t)); | 246 | void m_callback_namechange(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t)); |
195 | 247 | ||
196 | /* set the callback for status message changes | 248 | /* set the callback for status message changes |
197 | function(int friendnumber, uint8_t *newstatus, uint16_t length) | 249 | function(int friendnumber, uint8_t *newstatus, uint16_t length) |
198 | you are not responsible for freeing newstatus */ | 250 | you are not responsible for freeing newstatus */ |
199 | void m_callback_statusmessage(void (*function)(int, uint8_t *, uint16_t)); | 251 | void m_callback_statusmessage(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t)); |
200 | 252 | ||
201 | /* set the callback for status type changes | 253 | /* set the callback for status type changes |
202 | function(int friendnumber, USERSTATUS kind) */ | 254 | function(int friendnumber, USERSTATUS kind) */ |
203 | void m_callback_userstatus(void (*function)(int, USERSTATUS)); | 255 | void m_callback_userstatus(Messenger *m, void (*function)(Messenger *m, int, USERSTATUS)); |
204 | 256 | ||
205 | /* set the callback for read receipts | 257 | /* set the callback for read receipts |
206 | function(int friendnumber, uint32_t receipt) | 258 | function(int friendnumber, uint32_t receipt) |
@@ -209,7 +261,7 @@ void m_callback_userstatus(void (*function)(int, USERSTATUS)); | |||
209 | has been received on the other side. since core doesn't | 261 | has been received on the other side. since core doesn't |
210 | track ids for you, receipt may not correspond to any message | 262 | track ids for you, receipt may not correspond to any message |
211 | in that case, you should discard it. */ | 263 | in that case, you should discard it. */ |
212 | void m_callback_read_receipt(void (*function)(int, uint32_t)); | 264 | void m_callback_read_receipt(Messenger *m, void (*function)(Messenger *m, int, uint32_t)); |
213 | 265 | ||
214 | /* set the callback for connection status changes | 266 | /* set the callback for connection status changes |
215 | function(int friendnumber, uint8_t status) | 267 | function(int friendnumber, uint8_t status) |
@@ -219,26 +271,30 @@ void m_callback_read_receipt(void (*function)(int, uint32_t)); | |||
219 | note that this callback is not called when adding friends, thus the "after | 271 | note that this callback is not called when adding friends, thus the "after |
220 | being previously online" part. it's assumed that when adding friends, | 272 | being previously online" part. it's assumed that when adding friends, |
221 | their connection status is offline. */ | 273 | their connection status is offline. */ |
222 | void m_callback_connectionstatus(void (*function)(int, uint8_t)); | 274 | void m_callback_connectionstatus(Messenger *m, void (*function)(Messenger *m, int, uint8_t)); |
223 | 275 | ||
224 | /* run this at startup | 276 | /* run this at startup |
225 | returns 0 if no connection problems | 277 | * returns allocated instance of Messenger on success |
226 | returns -1 if there are problems */ | 278 | * returns 0 if there are problems */ |
227 | int initMessenger(void); | 279 | Messenger * initMessenger(void); |
280 | |||
281 | /* run this before closing shop | ||
282 | * free all datastructures */ | ||
283 | void cleanupMessenger(Messenger *M); | ||
228 | 284 | ||
229 | /* the main loop that needs to be run at least 200 times per second */ | 285 | /* the main loop that needs to be run at least 200 times per second */ |
230 | void doMessenger(void); | 286 | void doMessenger(Messenger *m); |
231 | 287 | ||
232 | /* SAVING AND LOADING FUNCTIONS: */ | 288 | /* SAVING AND LOADING FUNCTIONS: */ |
233 | 289 | ||
234 | /* returns the size of the messenger data (for saving) */ | 290 | /* returns the size of the messenger data (for saving) */ |
235 | uint32_t Messenger_size(void); | 291 | uint32_t Messenger_size(Messenger *m); |
236 | 292 | ||
237 | /* save the messenger in data (must be allocated memory of size Messenger_size()) */ | 293 | /* save the messenger in data (must be allocated memory of size Messenger_size()) */ |
238 | void Messenger_save(uint8_t *data); | 294 | void Messenger_save(Messenger *m, uint8_t *data); |
239 | 295 | ||
240 | /* load the messenger from data of size length */ | 296 | /* load the messenger from data of size length */ |
241 | int Messenger_load(uint8_t *data, uint32_t length); | 297 | int Messenger_load(Messenger *m, uint8_t *data, uint32_t length); |
242 | 298 | ||
243 | #ifdef __cplusplus | 299 | #ifdef __cplusplus |
244 | } | 300 | } |