summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--testing/nTox.c97
1 files changed, 65 insertions, 32 deletions
diff --git a/testing/nTox.c b/testing/nTox.c
index 73b55427..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 */
212static int save_data(Tox *m);
213
211void line_eval(Tox *m, char *line) 214void 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
@@ -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
500void load_key(Tox *m, char *path) 509static char *data_file_name = NULL;
510
511static 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; 540static 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
543FILE_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
563static 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
551void print_help(void) 575void print_help(void)
@@ -610,7 +634,7 @@ int main(int argc, char *argv[])
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);
@@ -642,9 +666,18 @@ int main(int argc, char *argv[])
642 666
643 nodelay(stdscr, TRUE); 667 nodelay(stdscr, TRUE);
644 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
645 while (1) { 678 while (1) {
646 if (on == 0 && tox_isconnected(m)) { 679 if (on == 0 && tox_isconnected(m)) {
647 new_lines("[i] connected to DHT\n[i] define username with /n"); 680 new_lines("[i] connected to DHT");
648 on = 1; 681 on = 1;
649 } 682 }
650 683