summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--core/Messenger.c35
-rw-r--r--testing/nTox.c34
-rw-r--r--testing/nTox_win32.c10
-rw-r--r--testing/toxic/prompt.c24
4 files changed, 67 insertions, 36 deletions
diff --git a/core/Messenger.c b/core/Messenger.c
index eb59b81a..f8a794ce 100644
--- a/core/Messenger.c
+++ b/core/Messenger.c
@@ -94,24 +94,31 @@ int getclient_id(int friend_id, uint8_t *client_id)
94 return -1; 94 return -1;
95} 95}
96 96
97/* add a friend 97/*
98 set the data that will be sent along with friend request 98 * add a friend
99 client_id is the client id of the friend 99 * set the data that will be sent along with friend request
100 data is the data and length is the length 100 * client_id is the client id of the friend
101 returns the friend number if success 101 * data is the data and length is the length
102 return -1 if key length is wrong. 102 * returns the friend number if success
103 return -2 if user's own key 103 * return -1 if message length is too long
104 return -3 if already a friend 104 * return -2 if no message (message length must be >= 1 byte)
105 return -4 for other*/ 105 * return -3 if user's own key
106 * return -4 if friend request already sent or already a friend
107 * return -5 for unknown error
108 */
106int m_addfriend(uint8_t *client_id, uint8_t *data, uint16_t length) 109int m_addfriend(uint8_t *client_id, uint8_t *data, uint16_t length)
107{ 110{
108 if (length == 0 || length >= 111 if (length >= (MAX_DATA_SIZE - crypto_box_PUBLICKEYBYTES
109 (MAX_DATA_SIZE - crypto_box_PUBLICKEYBYTES - crypto_box_NONCEBYTES - crypto_box_BOXZEROBYTES + crypto_box_ZEROBYTES)) 112 - crypto_box_NONCEBYTES - crypto_box_BOXZEROBYTES
113 + crypto_box_ZEROBYTES))
110 return -1; 114 return -1;
111 if (memcmp(client_id, self_public_key, crypto_box_PUBLICKEYBYTES) == 0) 115 if (length < 1)
112 return -2; 116 return -2;
113 if (getfriend_id(client_id) != -1) 117 if (memcmp(client_id, self_public_key, crypto_box_PUBLICKEYBYTES) == 0)
114 return -3; 118 return -3;
119 if (getfriend_id(client_id) != -1)
120 return -4;
121
115 uint32_t i; 122 uint32_t i;
116 for (i = 0; i <= numfriends; ++i) { /*TODO: dynamic memory allocation, this will segfault if there are more than MAX_NUM_FRIENDS*/ 123 for (i = 0; i <= numfriends; ++i) { /*TODO: dynamic memory allocation, this will segfault if there are more than MAX_NUM_FRIENDS*/
117 if(friendlist[i].status == 0) { 124 if(friendlist[i].status == 0) {
@@ -129,7 +136,7 @@ int m_addfriend(uint8_t *client_id, uint8_t *data, uint16_t length)
129 return i; 136 return i;
130 } 137 }
131 } 138 }
132 return -4; 139 return -5;
133} 140}
134 141
135int m_addfriend_norequest(uint8_t * client_id) 142int m_addfriend_norequest(uint8_t * client_id)
diff --git a/testing/nTox.c b/testing/nTox.c
index 34d74d5a..81cac0b6 100644
--- a/testing/nTox.c
+++ b/testing/nTox.c
@@ -111,24 +111,28 @@ void line_eval(char lines[HISTORY][STRING_LENGTH], char *line)
111 char temp_id[128]; 111 char temp_id[128];
112 for (i = 0; i < 128; i++) 112 for (i = 0; i < 128; i++)
113 temp_id[i] = line[i+prompt_offset]; 113 temp_id[i] = line[i+prompt_offset];
114
114 int num = m_addfriend(hex_string_to_bin(temp_id), (uint8_t*)"Install Gentoo", sizeof("Install Gentoo")); 115 int num = m_addfriend(hex_string_to_bin(temp_id), (uint8_t*)"Install Gentoo", sizeof("Install Gentoo"));
115 char numstring[100]; 116 char numstring[100];
116 switch (num) { 117 switch (num) {
117 case -1: 118 case -1:
118 sprintf(numstring, "[i] Incorrect key length"); 119 sprintf(numstring, "[i] Message is too long.");
119 break; 120 break;
120 case -2: 121 case -2:
121 sprintf(numstring, "[i] That appears to be your own key"); 122 sprintf(numstring, "[i] Please add a message to your request.");
122 break; 123 break;
123 case -3: 124 case -3:
124 sprintf(numstring, "[i] Friend request already sent"); 125 sprintf(numstring, "[i] That appears to be your own ID.");
125 break; 126 break;
126 case -4: 127 case -4:
127 sprintf(numstring, "[i] Could not add friend"); 128 sprintf(numstring, "[i] Friend request already sent.");
128 break; 129 break;
129 default: 130 case -5:
130 sprintf(numstring, "[i] Added friend %d", num); 131 sprintf(numstring, "[i] Undefined error when adding friend.");
131 break; 132 break;
133 default:
134 sprintf(numstring, "[i] Added friend as %d.", num);
135 break;
132 } 136 }
133 new_lines(numstring); 137 new_lines(numstring);
134 do_refresh(); 138 do_refresh();
diff --git a/testing/nTox_win32.c b/testing/nTox_win32.c
index 2394877f..42780923 100644
--- a/testing/nTox_win32.c
+++ b/testing/nTox_win32.c
@@ -132,13 +132,15 @@ void line_eval(char* line)
132 printf(numstring); 132 printf(numstring);
133 } 133 }
134 else if (num == -1) 134 else if (num == -1)
135 printf("\nWrong key size\n\n"); 135 printf("\n[i] Message is too long.\n\n");
136 else if (num == -2) 136 else if (num == -2)
137 printf("\nYou can't add yourself\n\n"); 137 printf("\n[i] Please add a message to your friend request.\n\n");
138 else if (num == -3) 138 else if (num == -3)
139 printf("\nYou already have this person added\n\n"); 139 printf("\n[i] That appears to be your own ID.\n\n");
140 else if (num == -4) 140 else if (num == -4)
141 printf("\nUndefined error when adding friend"); 141 printf("\n[i] Friend request already sent.\n\n");
142 else if (num == -5)
143 printf("\n[i] Undefined error when adding friend\n\n");
142 } 144 }
143 145
144 else if (inpt_command == 'r') { 146 else if (inpt_command == 'r') {
diff --git a/testing/toxic/prompt.c b/testing/toxic/prompt.c
index a3cf2d94..b0f83811 100644
--- a/testing/toxic/prompt.c
+++ b/testing/toxic/prompt.c
@@ -134,10 +134,28 @@ static void execute(ToxWindow* self, char* cmd) {
134 } 134 }
135 135
136 num = m_addfriend(id_bin, (uint8_t*) msg, strlen(msg)+1); 136 num = m_addfriend(id_bin, (uint8_t*) msg, strlen(msg)+1);
137 137 switch (num) {
138 wprintw(self->window, "Friend added as %d.\n", num); 138 case -1:
139 on_friendadded(num); 139 wprintw(self->window, "Message is too long.\n");
140 break;
141 case -2:
142 wprintw(self->window, "Please add a message to your request.\n");
143 case -3:
144 wprintw(self->window, "That appears to be your own ID.\n");
145 break;
146 case -4:
147 wprintw(self->window, "Friend request already sent.\n");
148 break;
149 case -5:
150 wprintw(self->window, "[i] Undefined error when adding friend.\n");
151 break;
152 default:
153 wprintw(self->window, "Friend added as %d.\n", num);
154 on_friendadded(num);
155 break;
156 }
140 } 157 }
158
141 else if(!strcmp(cmd, "help")) { 159 else if(!strcmp(cmd, "help")) {
142 print_usage(self); 160 print_usage(self);
143 } 161 }