summaryrefslogtreecommitdiff
path: root/core/Messenger.c
diff options
context:
space:
mode:
authorSebastian Stal <stal@pyboard.net>2013-08-05 12:30:07 -0700
committerSebastian Stal <stal@pyboard.net>2013-08-05 12:30:07 -0700
commitb7f989266df854fcd7772bec58e408c7359087ff (patch)
treebed83b3eb43c9a8f156cf2dc6c863b23c30b459c /core/Messenger.c
parentdc3fe775fca468ace65165fa1178b8d35397e20a (diff)
Add functions for dealing with userstatus kinds.
Diffstat (limited to 'core/Messenger.c')
-rw-r--r--core/Messenger.c88
1 files changed, 70 insertions, 18 deletions
diff --git a/core/Messenger.c b/core/Messenger.c
index 57d485bb..d8bf3413 100644
--- a/core/Messenger.c
+++ b/core/Messenger.c
@@ -35,6 +35,7 @@ typedef struct {
35 uint8_t *userstatus; 35 uint8_t *userstatus;
36 uint16_t userstatus_length; 36 uint16_t userstatus_length;
37 uint8_t userstatus_sent; 37 uint8_t userstatus_sent;
38 USERSTATUS_KIND userstatus_kind;
38 uint16_t info_size; /* length of the info */ 39 uint16_t info_size; /* length of the info */
39} Friend; 40} Friend;
40 41
@@ -45,6 +46,7 @@ static uint16_t self_name_length;
45 46
46static uint8_t *self_userstatus; 47static uint8_t *self_userstatus;
47static uint16_t self_userstatus_len; 48static uint16_t self_userstatus_len;
49static USERSTATUS_KIND self_userstatus_kind;
48 50
49#define MAX_NUM_FRIENDS 256 51#define MAX_NUM_FRIENDS 256
50 52
@@ -98,12 +100,12 @@ int getclient_id(int friend_id, uint8_t *client_id)
98 * return FAERR_NOMESSAGE if no message (message length must be >= 1 byte) 100 * return FAERR_NOMESSAGE if no message (message length must be >= 1 byte)
99 * return FAERR_OWNKEY if user's own key 101 * return FAERR_OWNKEY if user's own key
100 * return FAERR_ALREADYSENT if friend request already sent or already a friend 102 * return FAERR_ALREADYSENT if friend request already sent or already a friend
101 * return FAERR_UNKNOWN for unknown error 103 * return FAERR_UNKNOWN for unknown error
102 */ 104 */
103int m_addfriend(uint8_t *client_id, uint8_t *data, uint16_t length) 105int m_addfriend(uint8_t *client_id, uint8_t *data, uint16_t length)
104{ 106{
105 if (length >= (MAX_DATA_SIZE - crypto_box_PUBLICKEYBYTES 107 if (length >= (MAX_DATA_SIZE - crypto_box_PUBLICKEYBYTES
106 - crypto_box_NONCEBYTES - crypto_box_BOXZEROBYTES 108 - crypto_box_NONCEBYTES - crypto_box_BOXZEROBYTES
107 + crypto_box_ZEROBYTES)) 109 + crypto_box_ZEROBYTES))
108 return FAERR_TOOLONG; 110 return FAERR_TOOLONG;
109 if (length < 1) 111 if (length < 1)
@@ -123,6 +125,7 @@ int m_addfriend(uint8_t *client_id, uint8_t *data, uint16_t length)
123 memcpy(friendlist[i].client_id, client_id, CLIENT_ID_SIZE); 125 memcpy(friendlist[i].client_id, client_id, CLIENT_ID_SIZE);
124 friendlist[i].userstatus = calloc(1, 1); 126 friendlist[i].userstatus = calloc(1, 1);
125 friendlist[i].userstatus_length = 1; 127 friendlist[i].userstatus_length = 1;
128 friendlist[i].userstatus_kind = USERSTATUS_KIND_OFFLINE;
126 memcpy(friendlist[i].info, data, length); 129 memcpy(friendlist[i].info, data, length);
127 friendlist[i].info_size = length; 130 friendlist[i].info_size = length;
128 131
@@ -205,7 +208,7 @@ int m_sendmessage(int friendnumber, uint8_t *message, uint32_t length)
205 return write_cryptpacket(friendlist[friendnumber].crypt_connection_id, temp, length + 1); 208 return write_cryptpacket(friendlist[friendnumber].crypt_connection_id, temp, length + 1);
206} 209}
207 210
208/* send a name packet to friendnumber 211/* send a name packet to friendnumber
209 length is the length with the NULL terminator*/ 212 length is the length with the NULL terminator*/
210static int m_sendname(int friendnumber, uint8_t * name, uint16_t length) 213static int m_sendname(int friendnumber, uint8_t * name, uint16_t length)
211{ 214{
@@ -247,14 +250,14 @@ int setname(uint8_t * name, uint16_t length)
247} 250}
248 251
249/* get our nickname 252/* get our nickname
250 put it in name 253 put it in name
251 name needs to be a valid memory location with a size of at least MAX_NAME_LENGTH bytes. 254 name needs to be a valid memory location with a size of at least MAX_NAME_LENGTH bytes.
252 return the length of the name */ 255 return the length of the name */
253uint16_t getself_name(uint8_t *name) 256uint16_t getself_name(uint8_t *name)
254{ 257{
255 memcpy(name, self_name, self_name_length); 258 memcpy(name, self_name, self_name_length);
256 return self_name_length; 259 return self_name_length;
257} 260}
258 261
259/* get name of friendnumber 262/* get name of friendnumber
260 put it in name 263 put it in name
@@ -269,10 +272,13 @@ int getname(int friendnumber, uint8_t * name)
269 return 0; 272 return 0;
270} 273}
271 274
272int m_set_userstatus(uint8_t *status, uint16_t length) 275int m_set_userstatus(USERSTATUS_KIND kind, uint8_t *status, uint16_t length)
273{ 276{
274 if (length > MAX_USERSTATUS_LENGTH) 277 if (length > MAX_USERSTATUS_LENGTH)
275 return -1; 278 return -1;
279 if (kind != USERSTATUS_KIND_RETAIN) {
280 self_userstatus_kind = kind;
281 }
276 uint8_t *newstatus = calloc(length, 1); 282 uint8_t *newstatus = calloc(length, 1);
277 memcpy(newstatus, status, length); 283 memcpy(newstatus, status, length);
278 free(self_userstatus); 284 free(self_userstatus);
@@ -285,6 +291,20 @@ int m_set_userstatus(uint8_t *status, uint16_t length)
285 return 0; 291 return 0;
286} 292}
287 293
294int m_set_userstatus_kind(USERSTATUS_KIND kind) {
295 if (kind >= USERSTATUS_KIND_INVALID) {
296 return -1;
297 }
298 if (kind == USERSTATUS_KIND_RETAIN) {
299 return 0;
300 }
301 self_userstatus_kind = kind;
302 uint32_t i;
303 for (i = 0; i < numfriends; ++i)
304 friendlist[i].userstatus_sent = 0;
305 return 0;
306}
307
288/* return the size of friendnumber's user status 308/* return the size of friendnumber's user status
289 guaranteed to be at most MAX_USERSTATUS_LENGTH */ 309 guaranteed to be at most MAX_USERSTATUS_LENGTH */
290int m_get_userstatus_size(int friendnumber) 310int m_get_userstatus_size(int friendnumber)
@@ -305,11 +325,33 @@ int m_copy_userstatus(int friendnumber, uint8_t * buf, uint32_t maxlen)
305 return 0; 325 return 0;
306} 326}
307 327
328int m_copy_self_userstatus(uint8_t * buf, uint32_t maxlen)
329{
330 memset(buf, 0, maxlen);
331 memcpy(buf, self_userstatus, MIN(maxlen, MAX_USERSTATUS_LENGTH) - 1);
332 return 0;
333}
334
335USERSTATUS_KIND m_get_userstatus_kind(int friendnumber) {
336 if (friendnumber >= numfriends || friendnumber < 0)
337 return USERSTATUS_KIND_INVALID;
338 USERSTATUS_KIND uk = friendlist[friendnumber].userstatus_kind;
339 if (uk >= USERSTATUS_KIND_INVALID) {
340 uk = USERSTATUS_KIND_ONLINE;
341 }
342 return uk;
343}
344
345USERSTATUS_KIND m_get_self_userstatus_kind(void) {
346 return self_userstatus_kind;
347}
348
308static int send_userstatus(int friendnumber, uint8_t * status, uint16_t length) 349static int send_userstatus(int friendnumber, uint8_t * status, uint16_t length)
309{ 350{
310 uint8_t *thepacket = malloc(length + 1); 351 uint8_t *thepacket = malloc(length + 2);
311 memcpy(thepacket + 1, status, length); 352 memcpy(thepacket + 2, status, length);
312 thepacket[0] = PACKET_ID_USERSTATUS; 353 thepacket[0] = PACKET_ID_USERSTATUS;
354 thepacket[1] = self_userstatus_kind;
313 int written = write_cryptpacket(friendlist[friendnumber].crypt_connection_id, thepacket, length + 1); 355 int written = write_cryptpacket(friendlist[friendnumber].crypt_connection_id, thepacket, length + 1);
314 free(thepacket); 356 free(thepacket);
315 return written; 357 return written;
@@ -327,6 +369,11 @@ static int set_friend_userstatus(int friendnumber, uint8_t * status, uint16_t le
327 return 0; 369 return 0;
328} 370}
329 371
372static void set_friend_userstatus_kind(int friendnumber, USERSTATUS_KIND k)
373{
374 friendlist[friendnumber].userstatus_kind = k;
375}
376
330/* static void (*friend_request)(uint8_t *, uint8_t *, uint16_t); 377/* static void (*friend_request)(uint8_t *, uint8_t *, uint16_t);
331static uint8_t friend_request_isset = 0; */ 378static uint8_t friend_request_isset = 0; */
332/* set the function that will be executed when a friend request is received. */ 379/* set the function that will be executed when a friend request is received. */
@@ -353,9 +400,9 @@ void m_callback_namechange(void (*function)(int, uint8_t *, uint16_t))
353 friend_namechange_isset = 1; 400 friend_namechange_isset = 1;
354} 401}
355 402
356static void (*friend_statuschange)(int, uint8_t *, uint16_t); 403static void (*friend_statuschange)(int, USERSTATUS_KIND, uint8_t *, uint16_t);
357static uint8_t friend_statuschange_isset = 0; 404static uint8_t friend_statuschange_isset = 0;
358void m_callback_userstatus(void (*function)(int, uint8_t *, uint16_t)) 405void m_callback_userstatus(void (*function)(int, USERSTATUS_KIND, uint8_t *, uint16_t))
359{ 406{
360 friend_statuschange = function; 407 friend_statuschange = function;
361 friend_statuschange_isset = 1; 408 friend_statuschange_isset = 1;
@@ -366,7 +413,7 @@ void m_callback_userstatus(void (*function)(int, uint8_t *, uint16_t))
366int initMessenger(void) 413int initMessenger(void)
367{ 414{
368 new_keys(); 415 new_keys();
369 m_set_userstatus((uint8_t*)"Online", sizeof("Online")); 416 m_set_userstatus(USERSTATUS_KIND_ONLINE, (uint8_t*)"Online", sizeof("Online"));
370 initNetCrypto(); 417 initNetCrypto();
371 IP ip; 418 IP ip;
372 ip.i = 0; 419 ip.i = 0;
@@ -438,12 +485,17 @@ static void doFriends(void)
438 break; 485 break;
439 } 486 }
440 case PACKET_ID_USERSTATUS: { 487 case PACKET_ID_USERSTATUS: {
441 uint8_t *status = calloc(MIN(len - 1, MAX_USERSTATUS_LENGTH), 1); 488 if (len > 2) {
442 memcpy(status, temp + 1, MIN(len - 1, MAX_USERSTATUS_LENGTH)); 489 uint8_t *status = calloc(MIN(len - 2, MAX_USERSTATUS_LENGTH), 1);
443 if (friend_statuschange_isset) 490 memcpy(status, temp + 2, MIN(len - 2, MAX_USERSTATUS_LENGTH));
444 friend_statuschange(i, status, MIN(len - 1, MAX_USERSTATUS_LENGTH)); 491 if (friend_statuschange_isset)
445 set_friend_userstatus(i, status, MIN(len - 1, MAX_USERSTATUS_LENGTH)); 492 friend_statuschange(i, temp[1], status, MIN(len - 2, MAX_USERSTATUS_LENGTH));
446 free(status); 493 set_friend_userstatus(i, status, MIN(len - 2, MAX_USERSTATUS_LENGTH));
494 free(status);
495 } else if (friend_statuschange_isset) {
496 friend_statuschange(i, temp[1], friendlist[i].userstatus, friendlist[i].userstatus_length);
497 }
498 set_friend_userstatus_kind(i, temp[1]);
447 break; 499 break;
448 } 500 }
449 case PACKET_ID_MESSAGE: { 501 case PACKET_ID_MESSAGE: {