diff options
Diffstat (limited to 'testing/nTox.c')
-rw-r--r-- | testing/nTox.c | 156 |
1 files changed, 94 insertions, 62 deletions
diff --git a/testing/nTox.c b/testing/nTox.c index fa3b5b8c..029d62b4 100644 --- a/testing/nTox.c +++ b/testing/nTox.c | |||
@@ -208,6 +208,9 @@ char *format_message(Tox *m, char *message, int friendnum) | |||
208 | return msg; | 208 | return msg; |
209 | } | 209 | } |
210 | 210 | ||
211 | /* forward declaration */ | ||
212 | static int save_data(Tox *m); | ||
213 | |||
211 | void line_eval(Tox *m, char *line) | 214 | void line_eval(Tox *m, char *line) |
212 | { | 215 | { |
213 | if (line[0] == '/') { | 216 | if (line[0] == '/') { |
@@ -251,7 +254,12 @@ void line_eval(Tox *m, char *line) | |||
251 | break; | 254 | break; |
252 | 255 | ||
253 | default: | 256 | default: |
254 | sprintf(numstring, "[i] Added friend as %d.", num); | 257 | if (num >= 0) { |
258 | sprintf(numstring, "[i] Added friend as %d.", num); | ||
259 | save_data(m); | ||
260 | } | ||
261 | else | ||
262 | sprintf(numstring, "[i] Unknown error %i.", num); | ||
255 | break; | 263 | break; |
256 | } | 264 | } |
257 | 265 | ||
@@ -284,7 +292,7 @@ void line_eval(Tox *m, char *line) | |||
284 | 292 | ||
285 | int num = atoi(numstring); | 293 | int num = atoi(numstring); |
286 | 294 | ||
287 | if (tox_sendmessage(m, num, (uint8_t *) message, strlen(message) + 1) != 1) { | 295 | if (tox_sendmessage(m, num, (uint8_t *) message, strlen(message) + 1) < 1) { |
288 | new_lines("[i] could not send message"); | 296 | new_lines("[i] could not send message"); |
289 | } else { | 297 | } else { |
290 | new_lines(format_message(m, message, -1)); | 298 | new_lines(format_message(m, message, -1)); |
@@ -339,6 +347,7 @@ void line_eval(Tox *m, char *line) | |||
339 | new_lines(numchar); | 347 | new_lines(numchar); |
340 | sprintf(numchar, "[i] added friendnumber %d", num); | 348 | sprintf(numchar, "[i] added friendnumber %d", num); |
341 | new_lines(numchar); | 349 | new_lines(numchar); |
350 | save_data(m); | ||
342 | } else { | 351 | } else { |
343 | sprintf(numchar, "[i] failed to add friend"); | 352 | sprintf(numchar, "[i] failed to add friend"); |
344 | new_lines(numchar); | 353 | new_lines(numchar); |
@@ -374,8 +383,8 @@ void line_eval(Tox *m, char *line) | |||
374 | tox_group_message_send(m, groupnumber, (uint8_t *)*posi + 1, strlen(*posi + 1) + 1)); | 383 | tox_group_message_send(m, groupnumber, (uint8_t *)*posi + 1, strlen(*posi + 1) + 1)); |
375 | new_lines(msg); | 384 | new_lines(msg); |
376 | } | 385 | } |
377 | |||
378 | } else if (inpt_command == 'q') { //exit | 386 | } else if (inpt_command == 'q') { //exit |
387 | save_data(m); | ||
379 | endwin(); | 388 | endwin(); |
380 | exit(EXIT_SUCCESS); | 389 | exit(EXIT_SUCCESS); |
381 | } else { | 390 | } else { |
@@ -497,55 +506,70 @@ void print_statuschange(Tox *m, int friendnumber, uint8_t *string, uint16_t leng | |||
497 | } | 506 | } |
498 | } | 507 | } |
499 | 508 | ||
500 | void load_key(Tox *m, char *path) | 509 | static char *data_file_name = NULL; |
510 | |||
511 | static int load_data(Tox *m) | ||
501 | { | 512 | { |
502 | FILE *data_file = fopen(path, "r"); | 513 | FILE *data_file = fopen(data_file_name, "r"); |
503 | int size = 0; | 514 | int size = 0; |
504 | |||
505 | if (data_file) { | 515 | if (data_file) { |
506 | //load keys | ||
507 | fseek(data_file, 0, SEEK_END); | 516 | fseek(data_file, 0, SEEK_END); |
508 | size = ftell(data_file); | 517 | size = ftell(data_file); |
509 | rewind(data_file); | 518 | rewind(data_file); |
510 | 519 | ||
511 | uint8_t data[size]; | 520 | uint8_t data[size]; |
512 | |||
513 | if (fread(data, sizeof(uint8_t), size, data_file) != size) { | 521 | if (fread(data, sizeof(uint8_t), size, data_file) != size) { |
514 | fputs("[!] could not read data file! exiting...\n", stderr); | 522 | fputs("[!] could not read data file!\n", stderr); |
515 | goto FILE_ERROR; | 523 | return 0; |
516 | } | 524 | } |
517 | 525 | ||
518 | tox_load(m, data, size); | 526 | tox_load(m, data, size); |
519 | 527 | ||
520 | } else { | 528 | if (fclose(data_file) < 0) { |
521 | //else save new keys | 529 | perror("[!] fclose failed"); |
522 | int size = tox_size(m); | 530 | /* we got it open and the expected data read... let it be ok */ |
523 | uint8_t data[size]; | 531 | /* return 0; */ |
524 | tox_save(m, data); | ||
525 | data_file = fopen(path, "w"); | ||
526 | |||
527 | if (!data_file) { | ||
528 | perror("[!] load_key"); | ||
529 | exit(1); | ||
530 | } | 532 | } |
531 | 533 | ||
532 | if (fwrite(data, sizeof(uint8_t), size, data_file) != size) { | 534 | return 1; |
533 | fputs("[!] could not write data file! exiting...", stderr); | ||
534 | goto FILE_ERROR; | ||
535 | } | ||
536 | } | 535 | } |
537 | 536 | ||
538 | if (fclose(data_file) < 0) | 537 | return 0; |
539 | perror("[!] fclose failed"); | 538 | } |
540 | 539 | ||
541 | return; | 540 | static int save_data(Tox *m) |
541 | { | ||
542 | FILE *data_file = fopen(data_file_name, "w"); | ||
543 | if (!data_file) { | ||
544 | perror("[!] load_key"); | ||
545 | return 0; | ||
546 | } | ||
542 | 547 | ||
543 | FILE_ERROR: | 548 | int size = tox_size(m); |
549 | uint8_t data[size]; | ||
550 | tox_save(m, data); | ||
544 | 551 | ||
545 | if (fclose(data_file) < 0) | 552 | if (fwrite(data, sizeof(uint8_t), size, data_file) != size) { |
546 | perror("[!] fclose failed"); | 553 | fputs("[!] could not write data file (1)!", stderr); |
554 | return 0; | ||
555 | } | ||
547 | 556 | ||
548 | exit(1); | 557 | if (fclose(data_file) < 0) { |
558 | perror("[!] could not write data file (2)"); | ||
559 | return 0; | ||
560 | } | ||
561 | } | ||
562 | |||
563 | static int load_data_or_init(Tox *m, char *path) | ||
564 | { | ||
565 | data_file_name = path; | ||
566 | if (load_data(m)) | ||
567 | return 1; | ||
568 | |||
569 | if (save_data(m)) | ||
570 | return 1; | ||
571 | |||
572 | return 0; | ||
549 | } | 573 | } |
550 | 574 | ||
551 | void print_help(void) | 575 | void print_help(void) |
@@ -574,43 +598,43 @@ void print_groupmessage(Tox *m, int groupnumber, uint8_t *message, uint16_t leng | |||
574 | 598 | ||
575 | int main(int argc, char *argv[]) | 599 | int main(int argc, char *argv[]) |
576 | { | 600 | { |
601 | if (argc < 4) { | ||
602 | printf("Usage: %s [--ipv4|--ipv6] IP PORT KEY [-f keyfile]\n", argv[0]); | ||
603 | exit(0); | ||
604 | } | ||
605 | |||
606 | /* let user override default by cmdline */ | ||
607 | uint8_t ipv6enabled = TOX_ENABLE_IPV6_DEFAULT; /* x */ | ||
608 | int argvoffset = cmdline_parsefor_ipv46(argc, argv, &ipv6enabled); | ||
609 | |||
610 | if (argvoffset < 0) | ||
611 | exit(1); | ||
612 | |||
577 | int on = 0; | 613 | int on = 0; |
578 | int c = 0; | 614 | int c = 0; |
579 | int i = 0; | ||
580 | char *filename = "data"; | 615 | char *filename = "data"; |
581 | char idstring[200] = {0}; | 616 | char idstring[200] = {0}; |
582 | Tox *m; | 617 | Tox *m; |
583 | 618 | ||
584 | if (argc < 4) { | 619 | if ((argc == 2) && !strcmp(argv[1], "-h")) { |
585 | printf("[!] Usage: %s [IP] [port] [public_key] <keyfile>\n", argv[0]); | 620 | print_help(); |
586 | exit(0); | 621 | exit(0); |
587 | } | 622 | } |
588 | 623 | ||
589 | for (i = 0; i < argc; i++) { | 624 | /* [-f keyfile] MUST be last two arguments, no point in walking over the list |
590 | if (argv[i] == NULL) { | 625 | * especially not a good idea to accept it anywhere in the middle */ |
591 | break; | 626 | if (argc > argvoffset + 3) |
592 | } else if (argv[i][0] == '-') { | 627 | if (!strcmp(argv[argc - 2], "-f")) |
593 | if (argv[i][1] == 'h') { | 628 | filename = argv[argc - 1]; |
594 | print_help(); | ||
595 | exit(0); | ||
596 | } else if (argv[i][1] == 'f') { | ||
597 | if (argv[i + 1] != NULL) | ||
598 | filename = argv[i + 1]; | ||
599 | else { | ||
600 | fputs("[!] you passed '-f' without giving an argument!\n", stderr); | ||
601 | } | ||
602 | } | ||
603 | } | ||
604 | } | ||
605 | 629 | ||
606 | m = tox_new(); | 630 | m = tox_new(ipv6enabled); |
607 | 631 | ||
608 | if ( !m ) { | 632 | if ( !m ) { |
609 | fputs("Failed to allocate Messenger datastructure", stderr); | 633 | fputs("Failed to allocate Messenger datastructure", stderr); |
610 | exit(0); | 634 | exit(0); |
611 | } | 635 | } |
612 | 636 | ||
613 | load_key(m, filename); | 637 | load_data_or_init(m, filename); |
614 | 638 | ||
615 | tox_callback_friendrequest(m, print_request, NULL); | 639 | tox_callback_friendrequest(m, print_request, NULL); |
616 | tox_callback_friendmessage(m, print_message, NULL); | 640 | tox_callback_friendmessage(m, print_message, NULL); |
@@ -629,23 +653,31 @@ int main(int argc, char *argv[]) | |||
629 | new_lines(idstring); | 653 | new_lines(idstring); |
630 | strcpy(line, ""); | 654 | strcpy(line, ""); |
631 | 655 | ||
632 | tox_IP_Port bootstrap_ip_port; | 656 | uint16_t port = htons(atoi(argv[argvoffset + 2])); |
633 | bootstrap_ip_port.port = htons(atoi(argv[2])); | 657 | unsigned char *binary_string = hex_string_to_bin(argv[argvoffset + 3]); |
634 | int resolved_address = resolve_addr(argv[1]); | 658 | int res = tox_bootstrap_from_address(m, argv[argvoffset + 1], ipv6enabled, port, binary_string); |
659 | free(binary_string); | ||
635 | 660 | ||
636 | if (resolved_address != 0) | 661 | if (!res) { |
637 | bootstrap_ip_port.ip.i = resolved_address; | 662 | printf("Failed to convert \"%s\" into an IP address. Exiting...\n", argv[argvoffset + 1]); |
638 | else | 663 | endwin(); |
639 | exit(1); | 664 | exit(1); |
665 | } | ||
640 | 666 | ||
641 | unsigned char *binary_string = hex_string_to_bin(argv[3]); | ||
642 | tox_bootstrap(m, bootstrap_ip_port, binary_string); | ||
643 | free(binary_string); | ||
644 | nodelay(stdscr, TRUE); | 667 | nodelay(stdscr, TRUE); |
645 | 668 | ||
669 | new_lines("[i] change username with /n"); | ||
670 | char name[TOX_MAX_NAME_LENGTH]; | ||
671 | uint16_t namelen = tox_getselfname(m, name, sizeof(name)); | ||
672 | if (namelen > 0) { | ||
673 | char whoami[128 + TOX_MAX_NAME_LENGTH]; | ||
674 | snprintf(whoami, sizeof(whoami), "[i] your current username is: %s", name); | ||
675 | new_lines(whoami); | ||
676 | } | ||
677 | |||
646 | while (1) { | 678 | while (1) { |
647 | if (on == 0 && tox_isconnected(m)) { | 679 | if (on == 0 && tox_isconnected(m)) { |
648 | new_lines("[i] connected to DHT\n[i] define username with /n"); | 680 | new_lines("[i] connected to DHT"); |
649 | on = 1; | 681 | on = 1; |
650 | } | 682 | } |
651 | 683 | ||