summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--testing/nTox.c140
-rw-r--r--toxcore/network.c9
-rw-r--r--toxcore/tox.h7
3 files changed, 126 insertions, 30 deletions
diff --git a/testing/nTox.c b/testing/nTox.c
index 27875a96..2dd8b874 100644
--- a/testing/nTox.c
+++ b/testing/nTox.c
@@ -64,7 +64,7 @@ const char wrap_cont_str[] = "\n+ ";
64/* documented: fdmnlsahxgiztq(c[rfg]) */ 64/* documented: fdmnlsahxgiztq(c[rfg]) */
65/* undocumented: d (tox_do()) */ 65/* undocumented: d (tox_do()) */
66 66
67/* 249 characters */ 67/* 251+1 characters */
68char *help_main = 68char *help_main =
69 "[i] Available main commands:\n+ " 69 "[i] Available main commands:\n+ "
70 "/x (to print one's own id)|" 70 "/x (to print one's own id)|"
@@ -75,21 +75,22 @@ char *help_main =
75 "/h friend (for friend related commands)|" 75 "/h friend (for friend related commands)|"
76 "/h group (for group related commands)"; 76 "/h group (for group related commands)";
77 77
78/* 141 characters */ 78/* 190+1 characters */
79char *help_friend1 = 79char *help_friend1 =
80 "[i] Available friend commands (1/2):\n+ " 80 "[i] Available friend commands (1/2):\n+ "
81 "/l list (to list friends)|" 81 "/l list (to list friends)|"
82 "/r friend no. (to remove from the friend list)|"
82 "/f ID (to send a friend request)|" 83 "/f ID (to send a friend request)|"
83 "/a request no. (to accept a friend request)"; 84 "/a request no. (to accept a friend request)";
84 85
85/* 184 characters */ 86/* 187+1 characters */
86char *help_friend2 = 87char *help_friend2 =
87 "[i] Available friend commands (2/2):\n+ " 88 "[i] Available friend commands (2/2):\n+ "
88 "/m friend no. message (to send a message)|" 89 "/m friend no. message (to send a message)|"
89 "/t friend no. filename (to send a file to a friend)|" 90 "/t friend no. filename (to send a file to a friend)|"
90 "/cf friend no. (to talk to that friend per default)"; 91 "/cf friend no. (to talk to that friend per default)";
91 92
92/* 216 characters */ 93/* 253+1 characters */
93char *help_group = 94char *help_group =
94 "[i] Available group commands:\n+ " 95 "[i] Available group commands:\n+ "
95 "/g (to create a group)|" 96 "/g (to create a group)|"
@@ -236,18 +237,41 @@ uint32_t resolve_addr(const char *address)
236 return addr; 237 return addr;
237} 238}
238 239
240#define FRADDR_TOSTR_CHUNK_LEN 8
241#define FRADDR_TOSTR_BUFSIZE (TOX_FRIEND_ADDRESS_SIZE * 2 + TOX_FRIEND_ADDRESS_SIZE / FRADDR_TOSTR_CHUNK_LEN + 1)
242
243static void fraddr_to_str(uint8_t *id_bin, char *id_str)
244{
245 uint i, delta = 0, pos_extra, sum_extra = 0;
246
247 for (i = 0; i < TOX_FRIEND_ADDRESS_SIZE; i++) {
248 sprintf(&id_str[2 * i + delta], "%02hhX", id_bin[i]);
249
250 if ((i + 1) == TOX_CLIENT_ID_SIZE)
251 pos_extra = 2 * (i + 1) + delta;
252
253 if (i >= TOX_CLIENT_ID_SIZE)
254 sum_extra |= id_bin[i];
255
256 if (!((i + 1) % FRADDR_TOSTR_CHUNK_LEN)) {
257 id_str[2 * (i + 1) + delta] = ' ';
258 delta++;
259 }
260 }
261
262 id_str[2 * i + delta] = 0;
263
264 if (!sum_extra)
265 id_str[pos_extra] = 0;
266}
267
239void get_id(Tox *m, char *data) 268void get_id(Tox *m, char *data)
240{ 269{
241 sprintf(data, "[i] ID: "); 270 sprintf(data, "[i] ID: ");
242 int offset = strlen(data); 271 int offset = strlen(data);
243 uint8_t address[TOX_FRIEND_ADDRESS_SIZE]; 272 uint8_t address[TOX_FRIEND_ADDRESS_SIZE];
244 tox_getaddress(m, address); 273 tox_getaddress(m, address);
245 274 fraddr_to_str(address, data + offset);
246 uint32_t i = 0;
247
248 for (; i < TOX_FRIEND_ADDRESS_SIZE; i++) {
249 sprintf(data + 2 * i + offset, "%02X ", address[i]);
250 }
251} 275}
252 276
253int getfriendname_terminated(Tox *m, int friendnum, char *namebuf) 277int getfriendname_terminated(Tox *m, int friendnum, char *namebuf)
@@ -283,21 +307,31 @@ void new_lines(char *line)
283} 307}
284 308
285 309
286const char ptrn_friend[] = "[i] Friend: %s\n+ id: %i"; 310const char ptrn_friend[] = "[i] Friend %i: %s\n+ id: %s";
311const int id_str_len = TOX_FRIEND_ADDRESS_SIZE * 2 + 3;
287void print_friendlist(Tox *m) 312void print_friendlist(Tox *m)
288{ 313{
289 char name[TOX_MAX_NAME_LENGTH + 1];
290 int i = 0;
291 new_lines("[i] Friend List:"); 314 new_lines("[i] Friend List:");
292 315
316 char name[TOX_MAX_NAME_LENGTH + 1];
317 uint8_t fraddr_bin[TOX_FRIEND_ADDRESS_SIZE];
318 char fraddr_str[FRADDR_TOSTR_BUFSIZE];
319
320 /* account for the longest name and the longest "base" string and number (int) and id_str */
321 char fstring[TOX_MAX_NAME_LENGTH + strlen(ptrn_friend) + 21 + id_str_len];
322
323 uint i = 0;
324
293 while (getfriendname_terminated(m, i, name) != -1) { 325 while (getfriendname_terminated(m, i, name) != -1) {
294 /* account for the longest name and the longest "base" string and number (int) */ 326 if (!tox_getclient_id(m, i, fraddr_bin))
295 char fstring[TOX_MAX_NAME_LENGTH + strlen(ptrn_friend) + 21]; 327 fraddr_to_str(fraddr_bin, fraddr_str);
328 else
329 sprintf(fraddr_str, "???");
296 330
297 if (strlen(name) <= 0) { 331 if (strlen(name) <= 0) {
298 sprintf(fstring, ptrn_friend, "No name?", i); 332 sprintf(fstring, ptrn_friend, i, "No name?", fraddr_str);
299 } else { 333 } else {
300 sprintf(fstring, ptrn_friend, (uint8_t *)name, i); 334 sprintf(fstring, ptrn_friend, i, (uint8_t *)name, fraddr_str);
301 } 335 }
302 336
303 i++; 337 i++;
@@ -361,11 +395,15 @@ void line_eval(Tox *m, char *line)
361 new_lines(prompt); 395 new_lines(prompt);
362 396
363 if (inpt_command == 'f') { // add friend command: /f ID 397 if (inpt_command == 'f') { // add friend command: /f ID
364 int i; 398 int i, delta = 0;
365 char temp_id[128]; 399 char temp_id[128];
366 400
367 for (i = 0; i < 128; i++) 401 for (i = 0; i < 128; i++) {
368 temp_id[i] = line[i + prompt_offset]; 402 temp_id[i - delta] = line[i + prompt_offset];
403
404 if ((temp_id[i - delta] == ' ') || (temp_id[i - delta] == '+'))
405 delta++;
406 }
369 407
370 unsigned char *bin_string = hex_string_to_bin(temp_id); 408 unsigned char *bin_string = hex_string_to_bin(temp_id);
371 int num = tox_addfriend(m, bin_string, (uint8_t *)"Install Gentoo", sizeof("Install Gentoo")); 409 int num = tox_addfriend(m, bin_string, (uint8_t *)"Install Gentoo", sizeof("Install Gentoo"));
@@ -452,7 +490,7 @@ void line_eval(Tox *m, char *line)
452 char numstring[100]; 490 char numstring[100];
453 sprintf(numstring, "[i] changed status to %s", (char *)status); 491 sprintf(numstring, "[i] changed status to %s", (char *)status);
454 new_lines(numstring); 492 new_lines(numstring);
455 } else if (inpt_command == 'a') { 493 } else if (inpt_command == 'a') { // /a #: accept
456 uint8_t numf = atoi(line + 3); 494 uint8_t numf = atoi(line + 3);
457 char numchar[100]; 495 char numchar[100];
458 496
@@ -472,6 +510,39 @@ void line_eval(Tox *m, char *line)
472 new_lines(numchar); 510 new_lines(numchar);
473 } 511 }
474 } 512 }
513 } else if (inpt_command == 'r') { // /r #: remove friend
514 uint8_t numf = atoi(line + 3);
515
516 if (!tox_friend_exists(m, numf)) {
517 char err[64];
518 sprintf(err, "You don't have a friend %i.", numf);
519 new_lines(err);
520 return;
521 }
522
523 char msg[128 + TOX_MAX_NAME_LENGTH];
524 char fname[TOX_MAX_NAME_LENGTH ];
525 getfriendname_terminated(m, numf, fname);
526 sprintf(msg, "Are you sure you want to delete friend %i: %s? (y/n)", numf, fname);
527 input_line[0] = 0;
528 new_lines(msg);
529
530 int c;
531
532 do {
533 c = getchar();
534 } while ((c != 'y') && (c != 'n') && (c != EOF));
535
536 if (c == 'y') {
537 int res = tox_delfriend(m, numf);
538
539 if (res == 0)
540 sprintf(msg, "[i] [%i: %s] is no longer your friend", numf, fname);
541 else
542 sprintf(msg, "[i] failed to remove friend");
543
544 new_lines(msg);
545 }
475 } else if (inpt_command == 'h') { //help 546 } else if (inpt_command == 'h') { //help
476 if (line[2] == ' ') { 547 if (line[2] == ' ') {
477 if (line[3] == 'f') { 548 if (line[3] == 'f') {
@@ -838,7 +909,7 @@ void do_refresh()
838 909
839 if (count < y) { 910 if (count < y) {
840 move(y - 1 - count, 0); 911 move(y - 1 - count, 0);
841 printw(wrap_output); 912 printw("%s", wrap_output);
842 clrtoeol(); 913 clrtoeol();
843 } 914 }
844 } 915 }
@@ -846,7 +917,7 @@ void do_refresh()
846 move(y - 1, 0); 917 move(y - 1, 0);
847 clrtoeol(); 918 clrtoeol();
848 printw(">> "); 919 printw(">> ");
849 printw(input_line); 920 printw("%s", input_line);
850 clrtoeol(); 921 clrtoeol();
851 refresh(); 922 refresh();
852} 923}
@@ -1243,6 +1314,16 @@ int main(int argc, char *argv[])
1243 1314
1244 time_t timestamp0 = time(NULL); 1315 time_t timestamp0 = time(NULL);
1245 1316
1317 uint8_t pollok = 0;
1318 uint16_t len = 0;
1319
1320 if (!tox_wait_prepare(m, NULL, &len))
1321 pollok = 1;
1322 else
1323 new_lines("[i] failed to setup for low cpu consumption");
1324
1325 uint8_t data[len];
1326
1246 while (1) { 1327 while (1) {
1247 if (on == 0) { 1328 if (on == 0) {
1248 if (tox_isconnected(m)) { 1329 if (tox_isconnected(m)) {
@@ -1258,9 +1339,20 @@ int main(int argc, char *argv[])
1258 } 1339 }
1259 } 1340 }
1260 1341
1342 if (numfilesenders > 0)
1343 // during file transfer wasting cpu cycles is almost unavoidable
1344 c_sleep(1);
1345 else {
1346 if (pollok && (tox_wait_prepare(m, data, &len) == 1)) {
1347 /* 250ms is more than fast enough in "regular" mode */
1348 tox_wait_execute(m, data, len, 100);
1349 tox_wait_cleanup(m, data, len);
1350 } else
1351 c_sleep(25);
1352 }
1353
1261 send_filesenders(m); 1354 send_filesenders(m);
1262 tox_do(m); 1355 tox_do(m);
1263 c_sleep(1);
1264 do_refresh(); 1356 do_refresh();
1265 1357
1266 c = getch(); 1358 c = getch();
diff --git a/toxcore/network.c b/toxcore/network.c
index e5a80254..82f29aef 100644
--- a/toxcore/network.c
+++ b/toxcore/network.c
@@ -310,9 +310,12 @@ typedef struct {
310 310
311int networking_wait_prepare(Networking_Core *net, uint32_t sendqueue_length, uint8_t *data, uint16_t *lenptr) 311int networking_wait_prepare(Networking_Core *net, uint32_t sendqueue_length, uint8_t *data, uint16_t *lenptr)
312{ 312{
313 if ((data == NULL) || (*lenptr < sizeof(select_info))) { 313 if ((data == NULL) || !lenptr || (*lenptr < sizeof(select_info))) {
314 *lenptr = sizeof(select_info); 314 if (lenptr) {
315 return 0; 315 *lenptr = sizeof(select_info);
316 return 0;
317 } else
318 return -1;
316 } 319 }
317 320
318 *lenptr = sizeof(select_info); 321 *lenptr = sizeof(select_info);
diff --git a/toxcore/tox.h b/toxcore/tox.h
index 08727d2a..469a1f78 100644
--- a/toxcore/tox.h
+++ b/toxcore/tox.h
@@ -606,10 +606,11 @@ void tox_do(Tox *tox);
606 * Prepares the data required to call tox_wait_execute() asynchronously 606 * Prepares the data required to call tox_wait_execute() asynchronously
607 * 607 *
608 * data[] is reserved and kept by the caller 608 * data[] is reserved and kept by the caller
609 * len is in/out: in = reserved data[], out = required data[] 609 * *lenptr is in/out: in = reserved data[], out = required data[]
610 * 610 *
611 * returns 1 on success 611 * returns 1 on success
612 * returns 0 on failure (length is insufficient) 612 * returns 0 if *lenptr is insufficient
613 * returns -1 if lenptr is NULL
613 * 614 *
614 * 615 *
615 * tox_wait_execute(): function can be called asynchronously 616 * tox_wait_execute(): function can be called asynchronously