summaryrefslogtreecommitdiff
path: root/core/Messenger.c
diff options
context:
space:
mode:
authorMaxim Biro <nurupo.contributions@gmail.com>2013-07-19 22:40:23 -0400
committerMaxim Biro <nurupo.contributions@gmail.com>2013-07-19 23:17:15 -0400
commit48e2a80720ae438a0addc561674f13133b31de54 (patch)
tree6427db364a02636f0cc8cec6e611fb961a02e188 /core/Messenger.c
parentac6003e932219bd15cbe50808c7ad13ea01aad8f (diff)
Don't assume that all callbacks are set.
If one of callbacks is not set, the application crashes. This is the case when you update the library and the update adds more callbacks, or when you simply don't want to receive some of the updates (callbacks). In any case, defining all callbacks shouldn't be mandatory.
Diffstat (limited to 'core/Messenger.c')
-rw-r--r--core/Messenger.c28
1 files changed, 24 insertions, 4 deletions
diff --git a/core/Messenger.c b/core/Messenger.c
index 5a10c1da..bb581a45 100644
--- a/core/Messenger.c
+++ b/core/Messenger.c
@@ -355,33 +355,41 @@ static int set_friend_userstatus(int friendnumber, uint8_t * status, uint16_t le
355} 355}
356 356
357static void (*friend_request)(uint8_t *, uint8_t *, uint16_t); 357static void (*friend_request)(uint8_t *, uint8_t *, uint16_t);
358static uint8_t friend_request_isset = 0;
358 359
359//set the function that will be executed when a friend request is received. 360//set the function that will be executed when a friend request is received.
360void m_callback_friendrequest(void (*function)(uint8_t *, uint8_t *, uint16_t)) 361void m_callback_friendrequest(void (*function)(uint8_t *, uint8_t *, uint16_t))
361{ 362{
362 friend_request = function; 363 friend_request = function;
364 friend_request_isset = 1;
363} 365}
364 366
365 367
366static void (*friend_message)(int, uint8_t *, uint16_t); 368static void (*friend_message)(int, uint8_t *, uint16_t);
369static uint8_t friend_message_isset = 0;
367 370
368//set the function that will be executed when a message from a friend is received. 371//set the function that will be executed when a message from a friend is received.
369void m_callback_friendmessage(void (*function)(int, uint8_t *, uint16_t)) 372void m_callback_friendmessage(void (*function)(int, uint8_t *, uint16_t))
370{ 373{
371 friend_message = function; 374 friend_message = function;
375 friend_message_isset = 1;
372} 376}
373 377
374 378
375static void (*friend_namechange)(int, uint8_t *, uint16_t); 379static void (*friend_namechange)(int, uint8_t *, uint16_t);
380static uint8_t friend_namechange_isset = 0;
376void m_callback_namechange(void (*function)(int, uint8_t *, uint16_t)) 381void m_callback_namechange(void (*function)(int, uint8_t *, uint16_t))
377{ 382{
378 friend_namechange = function; 383 friend_namechange = function;
384 friend_namechange_isset = 1;
379} 385}
380 386
381static void (*friend_statuschange)(int, uint8_t *, uint16_t); 387static void (*friend_statuschange)(int, uint8_t *, uint16_t);
388static uint8_t friend_statuschange_isset = 0;
382void m_callback_userstatus(void (*function)(int, uint8_t *, uint16_t)) 389void m_callback_userstatus(void (*function)(int, uint8_t *, uint16_t))
383{ 390{
384 friend_statuschange = function; 391 friend_statuschange = function;
392 friend_statuschange_isset = 1;
385} 393}
386 394
387#define PORT 33445 395#define PORT 33445
@@ -458,7 +466,10 @@ static void doFriends()
458 switch(temp[0]) { 466 switch(temp[0]) {
459 case PACKET_ID_NICKNAME: { 467 case PACKET_ID_NICKNAME: {
460 if (len != MAX_NAME_LENGTH + 1) break; 468 if (len != MAX_NAME_LENGTH + 1) break;
461 friend_namechange(i, temp + 1, MAX_NAME_LENGTH); // todo: use the actual length 469 if(friend_namechange_isset)
470 {
471 friend_namechange(i, temp + 1, MAX_NAME_LENGTH); // todo: use the actual length
472 }
462 memcpy(friendlist[i].name, temp + 1, MAX_NAME_LENGTH); 473 memcpy(friendlist[i].name, temp + 1, MAX_NAME_LENGTH);
463 friendlist[i].name[MAX_NAME_LENGTH - 1] = 0;//make sure the NULL terminator is present. 474 friendlist[i].name[MAX_NAME_LENGTH - 1] = 0;//make sure the NULL terminator is present.
464 break; 475 break;
@@ -466,13 +477,19 @@ static void doFriends()
466 case PACKET_ID_USERSTATUS: { 477 case PACKET_ID_USERSTATUS: {
467 uint8_t *status = calloc(MIN(len - 1, MAX_USERSTATUS_LENGTH), 1); 478 uint8_t *status = calloc(MIN(len - 1, MAX_USERSTATUS_LENGTH), 1);
468 memcpy(status, temp + 1, MIN(len - 1, MAX_USERSTATUS_LENGTH)); 479 memcpy(status, temp + 1, MIN(len - 1, MAX_USERSTATUS_LENGTH));
469 friend_statuschange(i, status, MIN(len - 1, MAX_USERSTATUS_LENGTH)); 480 if(friend_statuschange_isset)
481 {
482 friend_statuschange(i, status, MIN(len - 1, MAX_USERSTATUS_LENGTH));
483 }
470 set_friend_userstatus(i, status, MIN(len - 1, MAX_USERSTATUS_LENGTH)); 484 set_friend_userstatus(i, status, MIN(len - 1, MAX_USERSTATUS_LENGTH));
471 free(status); 485 free(status);
472 break; 486 break;
473 } 487 }
474 case PACKET_ID_MESSAGE: { 488 case PACKET_ID_MESSAGE: {
475 (*friend_message)(i, temp + 1, len - 1); 489 if(friend_message_isset)
490 {
491 (*friend_message)(i, temp + 1, len - 1);
492 }
476 break; 493 break;
477 } 494 }
478 } 495 }
@@ -499,7 +516,10 @@ static void doFriendRequest()
499 int len = handle_friendrequest(public_key, temp); 516 int len = handle_friendrequest(public_key, temp);
500 if(len >= 0) 517 if(len >= 0)
501 { 518 {
502 (*friend_request)(public_key, temp, len); 519 if(friend_request_isset)
520 {
521 (*friend_request)(public_key, temp, len);
522 }
503 } 523 }
504} 524}
505 525