added error code for no message on friend add & updated nTox.c/nTox_win32.c

This commit is contained in:
Jfreegman 2013-08-01 15:27:08 -04:00
parent a604de9017
commit 8abc0a3462
4 changed files with 48 additions and 34 deletions

View File

@ -94,25 +94,31 @@ int getclient_id(int friend_id, uint8_t *client_id)
return -1; return -1;
} }
/* add a friend /*
set the data that will be sent along with friend request * add a friend
client_id is the client id of the friend * set the data that will be sent along with friend request
data is the data and length is the length * client_id is the client id of the friend
returns the friend number if success * data is the data and length is the length
return -1 if message length is too long * returns the friend number if success
return -2 if user's own key * return -1 if message length is too long
return -3 if already a friend * return -2 if no message (message length must be >= 1 byte)
return -4 for other*/ * return -3 if user's own key
* return -4 if friend request already sent or already a friend
* return -5 for unknown error
*/
int m_addfriend(uint8_t *client_id, uint8_t *data, uint16_t length) int m_addfriend(uint8_t *client_id, uint8_t *data, uint16_t length)
{ {
if (length >= (MAX_DATA_SIZE - crypto_box_PUBLICKEYBYTES if (length >= (MAX_DATA_SIZE - crypto_box_PUBLICKEYBYTES
- crypto_box_NONCEBYTES - crypto_box_BOXZEROBYTES - crypto_box_NONCEBYTES - crypto_box_BOXZEROBYTES
+ crypto_box_ZEROBYTES)) + crypto_box_ZEROBYTES))
return -1; return -1;
if (memcmp(client_id, self_public_key, crypto_box_PUBLICKEYBYTES) == 0) if (length < 1)
return -2; return -2;
if (getfriend_id(client_id) != -1) if (memcmp(client_id, self_public_key, crypto_box_PUBLICKEYBYTES) == 0)
return -3; return -3;
if (getfriend_id(client_id) != -1)
return -4;
uint32_t i; uint32_t i;
for (i = 0; i <= numfriends; ++i) { /*TODO: dynamic memory allocation, this will segfault if there are more than MAX_NUM_FRIENDS*/ for (i = 0; i <= numfriends; ++i) { /*TODO: dynamic memory allocation, this will segfault if there are more than MAX_NUM_FRIENDS*/
if(friendlist[i].status == 0) { if(friendlist[i].status == 0) {
@ -130,7 +136,7 @@ int m_addfriend(uint8_t *client_id, uint8_t *data, uint16_t length)
return i; return i;
} }
} }
return -4; return -5;
} }
int m_addfriend_norequest(uint8_t * client_id) int m_addfriend_norequest(uint8_t * client_id)

View File

@ -106,23 +106,27 @@ void line_eval(char lines[HISTORY][STRING_LENGTH], char *line)
char temp_id[128]; char temp_id[128];
for (i = 0; i < 128; i++) for (i = 0; i < 128; i++)
temp_id[i] = line[i+prompt_offset]; temp_id[i] = line[i+prompt_offset];
int num = m_addfriend(hex_string_to_bin(temp_id), (uint8_t*)"Install Gentoo", sizeof("Install Gentoo")); int num = m_addfriend(hex_string_to_bin(temp_id), (uint8_t*)"Install Gentoo", sizeof("Install Gentoo"));
char numstring[100]; char numstring[100];
switch (num) { switch (num) {
case -1: case -1:
sprintf(numstring, "[i] Incorrect key length"); sprintf(numstring, "[i] Message is too long.");
break; break;
case -2: case -2:
sprintf(numstring, "[i] That appears to be your own key"); sprintf(numstring, "[i] Please add a message to your request.");
break; break;
case -3: case -3:
sprintf(numstring, "[i] Friend request already sent"); sprintf(numstring, "[i] That appears to be your own ID.");
break; break;
case -4: case -4:
sprintf(numstring, "[i] Could not add friend"); sprintf(numstring, "[i] Friend request already sent.");
break;
case -5:
sprintf(numstring, "[i] Undefined error when adding friend.");
break; break;
default: default:
sprintf(numstring, "[i] Added friend %d", num); sprintf(numstring, "[i] Added friend as %d.", num);
break; break;
} }
new_lines(numstring); new_lines(numstring);

View File

@ -132,13 +132,15 @@ void line_eval(char* line)
printf(numstring); printf(numstring);
} }
else if (num == -1) else if (num == -1)
printf("\nWrong key size\n\n"); printf("\n[i] Message is too long.\n\n");
else if (num == -2) else if (num == -2)
printf("\nYou can't add yourself\n\n"); printf("\n[i] Please add a message to your friend request.\n\n");
else if (num == -3) else if (num == -3)
printf("\nYou already have this person added\n\n"); printf("\n[i] That appears to be your own ID.\n\n");
else if (num == -4) else if (num == -4)
printf("\nUndefined error when adding friend"); printf("\n[i] Friend request already sent.\n\n");
else if (num == -5)
printf("\n[i] Undefined error when adding friend\n\n");
} }
else if (inpt_command == 'r') { else if (inpt_command == 'r') {

View File

@ -138,13 +138,15 @@ static void execute(ToxWindow* self, char* cmd) {
wprintw(self->window, "Message is too long.\n"); wprintw(self->window, "Message is too long.\n");
break; break;
case -2: case -2:
wprintw(self->window, "Please add a message to your request.\n");
case -3:
wprintw(self->window, "That appears to be your own ID.\n"); wprintw(self->window, "That appears to be your own ID.\n");
break; break;
case -3: case -4:
wprintw(self->window, "Friend request already sent.\n"); wprintw(self->window, "Friend request already sent.\n");
break; break;
case -4: case -5:
wprintw(self->window, "Invalid ID.\n"); wprintw(self->window, "[i] Undefined error when adding friend.\n");
break; break;
default: default:
wprintw(self->window, "Friend added as %d.\n", num); wprintw(self->window, "Friend added as %d.\n", num);