From 3678b302dbf2b202afe9e53b46153da69ecbb28e Mon Sep 17 00:00:00 2001 From: charmlesscoin Date: Fri, 2 Aug 2013 21:22:02 -0400 Subject: [PATCH 1/5] cleaned up print_friendlist(), it now prints a message if you have _no_ friends --- testing/nTox.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/testing/nTox.c b/testing/nTox.c index 24d40ead..4989f88a 100644 --- a/testing/nTox.c +++ b/testing/nTox.c @@ -82,18 +82,22 @@ void new_lines(char *line) void print_friendlist() { char name[MAX_NAME_LENGTH]; + int i = 0; new_lines("[i] Friend List:"); - uint32_t i; - for (i = 0; i <= num_requests; i++) { - char fstring[128]; - getname(i, (uint8_t*)name); + while(getname(i++, (uint8_t *)name) != -1) { + /* account for the longest name and the longest "base" string */ + char fstring[MAX_NAME_LENGTH + strlen("[i] Friend: NULL\n\tid: ")]; + if (strlen(name) <= 0) { - sprintf(fstring, "[i] Friend: NULL\n\tid: %i", i); + sprintf(fstring, "[i] Friend: No Friend!\n\tid: %i", i); } else { sprintf(fstring, "[i] Friend: %s\n\tid: %i", (uint8_t*)name, i); } new_lines(fstring); } + + if(i == 1) + new_lines("\tno friends! D:"); } char *format_message(char *message, int friendnum) From b9e3bf1fa61aac975abe54586e1928f25767ff0a Mon Sep 17 00:00:00 2001 From: charmlesscoin Date: Fri, 2 Aug 2013 22:34:45 -0400 Subject: [PATCH 2/5] changed some formatting, fixed the removal of x and y --- testing/nTox.c | 16 ++++++++++------ testing/nTox.h | 3 ++- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/testing/nTox.c b/testing/nTox.c index 4989f88a..a498e9a1 100644 --- a/testing/nTox.c +++ b/testing/nTox.c @@ -36,11 +36,12 @@ char lines[HISTORY][STRING_LENGTH]; char line[STRING_LENGTH]; -char *help = "[i] commands: /f ID (to add friend), /m friendnumber message (to send message), /s status (to change status)\n" - "[i] /l list (list friends), /h for help, /i for info, /n nick (to change nickname), /q (to quit)"; +char *help = "[i] commands:\n/f ID (to add friend)\n/m friendnumber message " + "(to send message)\n/s status (to change status)\n[i] /l list (l" + "ist friends)\n/h for help\n/i for info\n/n nick (to change nick" + "name)\n/q (to quit)"; int x, y; - uint8_t pending_requests[256][CLIENT_ID_SIZE]; uint8_t num_requests = 0; @@ -126,7 +127,7 @@ char *format_message(char *message, int friendnum) return msg; } -void line_eval(char lines[HISTORY][STRING_LENGTH], char *line) +void line_eval(char *line) { if (line[0] == '/') { char inpt_command = line[1]; @@ -410,13 +411,16 @@ int main(int argc, char *argv[]) char idstring[200]; get_id(idstring); + initscr(); noecho(); raw(); getmaxyx(stdscr, y, x); + + new_lines("/h for list of commands"); new_lines(idstring); - new_lines(help); strcpy(line, ""); + IP_Port bootstrap_ip_port; bootstrap_ip_port.port = htons(atoi(argv[2])); int resolved_address = resolve_addr(argv[1]); @@ -443,7 +447,7 @@ int main(int argc, char *argv[]) getmaxyx(stdscr, y, x); if (c == '\n') { - line_eval(lines, line); + line_eval(line); strcpy(line, ""); } else if (c == 127) { line[strlen(line)-1] = '\0'; diff --git a/testing/nTox.h b/testing/nTox.h index 9d82556c..47c73513 100644 --- a/testing/nTox.h +++ b/testing/nTox.h @@ -37,12 +37,13 @@ #include #include "../core/Messenger.h" #include "../core/network.h" + #define STRING_LENGTH 256 #define HISTORY 50 #define PUB_KEY_BYTES 32 void new_lines(char *line); -void line_eval(char lines[HISTORY][STRING_LENGTH], char *line); +void line_eval(char *line); void wrap(char output[STRING_LENGTH], char input[STRING_LENGTH], int line_width) ; int count_lines(char *string) ; char *appender(char *str, const char c); From 6b8f12e33cb30380dd0d60943955b2d40118bb11 Mon Sep 17 00:00:00 2001 From: charmlesscoin Date: Sat, 3 Aug 2013 00:45:34 -0400 Subject: [PATCH 3/5] added a getopt system, with -f and -h flags --- testing/nTox.c | 73 +++++++++++++++++++++++++++++++++++--------------- 1 file changed, 51 insertions(+), 22 deletions(-) diff --git a/testing/nTox.c b/testing/nTox.c index a498e9a1..319002fd 100644 --- a/testing/nTox.c +++ b/testing/nTox.c @@ -85,7 +85,7 @@ void print_friendlist() char name[MAX_NAME_LENGTH]; int i = 0; new_lines("[i] Friend List:"); - while(getname(i++, (uint8_t *)name) != -1) { + while(getname(i, (uint8_t *)name) != -1) { /* account for the longest name and the longest "base" string */ char fstring[MAX_NAME_LENGTH + strlen("[i] Friend: NULL\n\tid: ")]; @@ -94,10 +94,11 @@ void print_friendlist() } else { sprintf(fstring, "[i] Friend: %s\n\tid: %i", (uint8_t*)name, i); } + i++; new_lines(fstring); } - if(i == 1) + if(i == 0) new_lines("\tno friends! D:"); } @@ -241,8 +242,7 @@ void line_eval(char *line) do_refresh(); } else if (inpt_command == 'h') { //help - new_lines("[i] commands: /f ID (to add friend), /m friendnumber message (to send message), /s status (to change status)"); - new_lines("[i] /l list (list friends), /h for help, /i for info, /n nick (to change nickname), /q (to quit)"); + new_lines(help); } else if (inpt_command == 'i') { //info char idstring[200]; @@ -358,10 +358,10 @@ void print_statuschange(int friendnumber, uint8_t *string, uint16_t length) new_lines(msg); } -void load_key() +void load_key(char *path) { - FILE *data_file = NULL; - data_file = fopen("data","r"); + FILE *data_file = fopen(path, "r"); + if (data_file) { //load keys fseek(data_file, 0, SEEK_END); @@ -373,12 +373,21 @@ void load_key() exit(1); } Messenger_load(data, size); + } else { + fputs("(saving new keys now)\n", stderr); + //else save new keys int size = Messenger_size(); uint8_t data[size]; Messenger_save(data); - data_file = fopen("data","w"); + data_file = fopen(path, "w"); + + if(!data_file) { + perror("[!] load_key"); + exit(1); + } + if (fwrite(data, sizeof(uint8_t), size, data_file) != size){ printf("[i] could not write data file\n[i] exiting\n"); exit(1); @@ -387,38 +396,58 @@ void load_key() fclose(data_file); } +void print_help(void) +{ + printf("nTox %.1f - Command-line tox-core client\n", 0.1); + puts("Options:"); + puts("\t-h\t-\tPrint this help and exit."); + puts("\t-f\t-\tSpecify a keyfile to read from."); +} + int main(int argc, char *argv[]) { + int on = 0; + int c = 0; + int i = 0; + char *filename = "data"; + char idstring[200] = {0}; + if (argc < 4) { - printf("[!] Usage: %s [IP] [port] [public_key] \n", argv[0]); + printf("[!] Usage: %s [IP] [port] [public_key] \n", argv[0]); exit(0); } - int c; - int on = 0; - initMessenger(); - //if keyfiles exist - if(argc > 4){ - if(strncmp(argv[4], "nokey", 6) < 0){ - //load_key(); + + for(i = 0; i < argc; i++) { + if(argv[i][0] == '-') { + if(argv[i][1] == 'h') { + print_help(); + exit(0); + } else if(argv[i][1] == 'f') { + if(argv[i + 1] != NULL) + filename = argv[i + 1]; + else { + fputs("[!] you passed '-f' without giving an argument!\n", stderr); + } + } } - } else { - load_key(); } + + initMessenger(); + load_key(filename); + m_callback_friendrequest(print_request); m_callback_friendmessage(print_message); m_callback_namechange(print_nickchange); m_callback_userstatus(print_statuschange); - char idstring[200]; - get_id(idstring); - initscr(); noecho(); raw(); getmaxyx(stdscr, y, x); new_lines("/h for list of commands"); - new_lines(idstring); + get_id(idstring); + puts(idstring); strcpy(line, ""); IP_Port bootstrap_ip_port; From bb2ea0e5b40012dc722727fe8e5274cd6ec9c563 Mon Sep 17 00:00:00 2001 From: charmlesscoin Date: Sat, 3 Aug 2013 01:14:30 -0400 Subject: [PATCH 4/5] Fixed the printing of a few strings, moved some code around. --- testing/nTox.c | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/testing/nTox.c b/testing/nTox.c index 319002fd..dec7a12d 100644 --- a/testing/nTox.c +++ b/testing/nTox.c @@ -90,7 +90,7 @@ void print_friendlist() char fstring[MAX_NAME_LENGTH + strlen("[i] Friend: NULL\n\tid: ")]; if (strlen(name) <= 0) { - sprintf(fstring, "[i] Friend: No Friend!\n\tid: %i", i); + sprintf(fstring, "[i] Friend: !\n\tid: %i", i); } else { sprintf(fstring, "[i] Friend: %s\n\tid: %i", (uint8_t*)name, i); } @@ -375,8 +375,6 @@ void load_key(char *path) Messenger_load(data, size); } else { - fputs("(saving new keys now)\n", stderr); - //else save new keys int size = Messenger_size(); uint8_t data[size]; @@ -389,7 +387,7 @@ void load_key(char *path) } if (fwrite(data, sizeof(uint8_t), size, data_file) != size){ - printf("[i] could not write data file\n[i] exiting\n"); + puts("[i] could not write data file! exiting..."); exit(1); } } @@ -401,7 +399,7 @@ void print_help(void) printf("nTox %.1f - Command-line tox-core client\n", 0.1); puts("Options:"); puts("\t-h\t-\tPrint this help and exit."); - puts("\t-f\t-\tSpecify a keyfile to read from."); + puts("\t-f\t-\tSpecify a keyfile to read (or write to) from."); } int main(int argc, char *argv[]) @@ -447,7 +445,7 @@ int main(int argc, char *argv[]) new_lines("/h for list of commands"); get_id(idstring); - puts(idstring); + new_lines(idstring); strcpy(line, ""); IP_Port bootstrap_ip_port; @@ -457,7 +455,7 @@ int main(int argc, char *argv[]) bootstrap_ip_port.ip.i = resolved_address; else exit(1); - + DHT_bootstrap(bootstrap_ip_port, hex_string_to_bin(argv[3])); nodelay(stdscr, TRUE); while(true) { From 9b6283c084d1c109be1319d8d325c01322b2cfc5 Mon Sep 17 00:00:00 2001 From: charmlesscoin Date: Sat, 3 Aug 2013 01:27:20 -0400 Subject: [PATCH 5/5] whoops, removed part of a string by accident. --- testing/nTox.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/testing/nTox.c b/testing/nTox.c index dec7a12d..312484d6 100644 --- a/testing/nTox.c +++ b/testing/nTox.c @@ -90,7 +90,7 @@ void print_friendlist() char fstring[MAX_NAME_LENGTH + strlen("[i] Friend: NULL\n\tid: ")]; if (strlen(name) <= 0) { - sprintf(fstring, "[i] Friend: !\n\tid: %i", i); + sprintf(fstring, "[i] Friend: No Friend!\n\tid: %i", i); } else { sprintf(fstring, "[i] Friend: %s\n\tid: %i", (uint8_t*)name, i); }