summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--toxcore/Messenger.c147
1 files changed, 61 insertions, 86 deletions
diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c
index c310412d..b3663fe6 100644
--- a/toxcore/Messenger.c
+++ b/toxcore/Messenger.c
@@ -183,6 +183,54 @@ static int handle_status(void *object, int i, uint8_t status);
183static int handle_packet(void *object, int i, uint8_t *temp, uint16_t len); 183static int handle_packet(void *object, int i, uint8_t *temp, uint16_t len);
184static int handle_custom_lossy_packet(void *object, int friend_num, const uint8_t *packet, uint16_t length); 184static int handle_custom_lossy_packet(void *object, int friend_num, const uint8_t *packet, uint16_t length);
185 185
186static int32_t init_new_friend(Messenger *m, const uint8_t *client_id, uint8_t status)
187{
188 /* Resize the friend list if necessary. */
189 if (realloc_friendlist(m, m->numfriends + 1) != 0)
190 return FAERR_NOMEM;
191
192 memset(&(m->friendlist[m->numfriends]), 0, sizeof(Friend));
193
194 int friendcon_id = new_friend_connection(m->fr_c, client_id);
195
196 if (friendcon_id == -1)
197 return FAERR_UNKNOWN;
198
199 uint32_t i;
200
201 for (i = 0; i <= m->numfriends; ++i) {
202 if (m->friendlist[i].status == NOFRIEND) {
203 m->friendlist[i].status = status;
204 m->friendlist[i].friendcon_id = friendcon_id;
205 m->friendlist[i].friendrequest_lastsent = 0;
206 id_copy(m->friendlist[i].client_id, client_id);
207 m->friendlist[i].statusmessage = calloc(1, 1);
208 m->friendlist[i].statusmessage_length = 1;
209 m->friendlist[i].userstatus = USERSTATUS_NONE;
210 m->friendlist[i].avatar_info_sent = 0;
211 m->friendlist[i].avatar_recv_data = NULL;
212 m->friendlist[i].avatar_send_data.bytes_sent = 0;
213 m->friendlist[i].avatar_send_data.last_reset = 0;
214 m->friendlist[i].is_typing = 0;
215 m->friendlist[i].message_id = 0;
216 m->friendlist[i].receives_read_receipts = 1; /* Default: YES. */
217 friend_connection_callbacks(m->fr_c, friendcon_id, MESSENGER_CALLBACK_INDEX, &handle_status, &handle_packet,
218 &handle_custom_lossy_packet, m, i);
219
220 if (m->numfriends == i)
221 ++m->numfriends;
222
223 if (friend_con_connected(m->fr_c, friendcon_id) == FRIENDCONN_STATUS_CONNECTED) {
224 send_online_packet(m, i);
225 }
226
227 return i;
228 }
229 }
230
231 return FAERR_UNKNOWN;
232}
233
186/* 234/*
187 * Add a friend. 235 * Add a friend.
188 * Set the data that will be sent along with friend request. 236 * Set the data that will be sent along with friend request.
@@ -239,54 +287,18 @@ int32_t m_addfriend(Messenger *m, const uint8_t *address, const uint8_t *data, u
239 return FAERR_SETNEWNOSPAM; 287 return FAERR_SETNEWNOSPAM;
240 } 288 }
241 289
242 /* Resize the friend list if necessary. */ 290 int32_t ret = init_new_friend(m, client_id, FRIEND_ADDED);
243 if (realloc_friendlist(m, m->numfriends + 1) != 0)
244 return FAERR_NOMEM;
245
246 memset(&(m->friendlist[m->numfriends]), 0, sizeof(Friend));
247
248 int friendcon_id = new_friend_connection(m->fr_c, client_id);
249
250 if (friendcon_id == -1)
251 return -1;
252
253 uint32_t i;
254 291
255 for (i = 0; i <= m->numfriends; ++i) { 292 if (ret < 0) {
256 if (m->friendlist[i].status == NOFRIEND) { 293 return ret;
257 m->friendlist[i].status = FRIEND_ADDED;
258 m->friendlist[i].friendcon_id = friendcon_id;
259 m->friendlist[i].friendrequest_lastsent = 0;
260 m->friendlist[i].friendrequest_timeout = FRIENDREQUEST_TIMEOUT;
261 id_copy(m->friendlist[i].client_id, client_id);
262 m->friendlist[i].statusmessage = calloc(1, 1);
263 m->friendlist[i].statusmessage_length = 1;
264 m->friendlist[i].userstatus = USERSTATUS_NONE;
265 m->friendlist[i].avatar_info_sent = 0;
266 m->friendlist[i].avatar_recv_data = NULL;
267 m->friendlist[i].avatar_send_data.bytes_sent = 0;
268 m->friendlist[i].avatar_send_data.last_reset = 0;
269 m->friendlist[i].is_typing = 0;
270 memcpy(m->friendlist[i].info, data, length);
271 m->friendlist[i].info_size = length;
272 m->friendlist[i].message_id = 0;
273 m->friendlist[i].receives_read_receipts = 1; /* Default: YES. */
274 memcpy(&(m->friendlist[i].friendrequest_nospam), address + crypto_box_PUBLICKEYBYTES, sizeof(uint32_t));
275 friend_connection_callbacks(m->fr_c, friendcon_id, MESSENGER_CALLBACK_INDEX, &handle_status, &handle_packet,
276 &handle_custom_lossy_packet, m, i);
277
278 if (m->numfriends == i)
279 ++m->numfriends;
280
281 if (friend_con_connected(m->fr_c, friendcon_id) == FRIENDCONN_STATUS_CONNECTED) {
282 send_online_packet(m, i);
283 }
284
285 return i;
286 }
287 } 294 }
288 295
289 return FAERR_UNKNOWN; 296 m->friendlist[ret].friendrequest_timeout = FRIENDREQUEST_TIMEOUT;
297 memcpy(m->friendlist[ret].info, data, length);
298 m->friendlist[ret].info_size = length;
299 memcpy(&(m->friendlist[ret].friendrequest_nospam), address + crypto_box_PUBLICKEYBYTES, sizeof(uint32_t));
300
301 return ret;
290} 302}
291 303
292int32_t m_addfriend_norequest(Messenger *m, const uint8_t *client_id) 304int32_t m_addfriend_norequest(Messenger *m, const uint8_t *client_id)
@@ -297,53 +309,16 @@ int32_t m_addfriend_norequest(Messenger *m, const uint8_t *client_id)
297 if (!public_key_valid(client_id)) 309 if (!public_key_valid(client_id))
298 return -1; 310 return -1;
299 311
300 /* Resize the friend list if necessary. */
301 if (realloc_friendlist(m, m->numfriends + 1) != 0)
302 return -1;
303
304 if (id_equal(client_id, m->net_crypto->self_public_key)) 312 if (id_equal(client_id, m->net_crypto->self_public_key))
305 return -1; 313 return -1;
306 314
307 memset(&(m->friendlist[m->numfriends]), 0, sizeof(Friend)); 315 int32_t ret = init_new_friend(m, client_id, FRIEND_CONFIRMED);
308
309 int friendcon_id = new_friend_connection(m->fr_c, client_id);
310 316
311 if (friendcon_id == -1) 317 if (ret < 0) {
312 return -1; 318 return -1;
313 319 } else {
314 uint32_t i; 320 return ret;
315
316 for (i = 0; i <= m->numfriends; ++i) {
317 if (m->friendlist[i].status == NOFRIEND) {
318 m->friendlist[i].status = FRIEND_CONFIRMED;
319 m->friendlist[i].friendcon_id = friendcon_id;
320 m->friendlist[i].friendrequest_lastsent = 0;
321 id_copy(m->friendlist[i].client_id, client_id);
322 m->friendlist[i].statusmessage = calloc(1, 1);
323 m->friendlist[i].statusmessage_length = 1;
324 m->friendlist[i].userstatus = USERSTATUS_NONE;
325 m->friendlist[i].avatar_info_sent = 0;
326 m->friendlist[i].avatar_recv_data = NULL;
327 m->friendlist[i].avatar_send_data.bytes_sent = 0;
328 m->friendlist[i].avatar_send_data.last_reset = 0;
329 m->friendlist[i].is_typing = 0;
330 m->friendlist[i].message_id = 0;
331 m->friendlist[i].receives_read_receipts = 1; /* Default: YES. */
332 friend_connection_callbacks(m->fr_c, friendcon_id, MESSENGER_CALLBACK_INDEX, &handle_status, &handle_packet,
333 &handle_custom_lossy_packet, m, i);
334
335 if (m->numfriends == i)
336 ++m->numfriends;
337
338 if (friend_con_connected(m->fr_c, friendcon_id) == FRIENDCONN_STATUS_CONNECTED) {
339 send_online_packet(m, i);
340 }
341
342 return i;
343 }
344 } 321 }
345
346 return -1;
347} 322}
348 323
349/* Remove a friend. 324/* Remove a friend.