diff options
author | Jfreegman <Jfreegman@gmail.com> | 2013-08-04 02:40:57 -0400 |
---|---|---|
committer | Jfreegman <Jfreegman@gmail.com> | 2013-08-04 02:40:57 -0400 |
commit | 357c02ac2231d1f7844ff74427c6f6bbb7adc6ca (patch) | |
tree | cf5de376f1ec65ce3f307ddbed7b4815c8b0e0e7 /testing | |
parent | c13de2d2859f09a4b8f0f88d122e9fbd2ecc6b80 (diff) | |
parent | 6f0ae0dbb483f7cb11ed735eac235e3367ccb019 (diff) |
Merge branch 'master' of https://github.com/irungentoo/ProjectTox-Core
Diffstat (limited to 'testing')
-rw-r--r-- | testing/nTox.c | 97 | ||||
-rw-r--r-- | testing/nTox.h | 3 | ||||
-rw-r--r-- | testing/toxic/chat.c | 63 | ||||
-rw-r--r-- | testing/toxic/main.c | 31 |
4 files changed, 141 insertions, 53 deletions
diff --git a/testing/nTox.c b/testing/nTox.c index 13db58d7..15e209a9 100644 --- a/testing/nTox.c +++ b/testing/nTox.c | |||
@@ -36,11 +36,12 @@ | |||
36 | char lines[HISTORY][STRING_LENGTH]; | 36 | char lines[HISTORY][STRING_LENGTH]; |
37 | char line[STRING_LENGTH]; | 37 | char line[STRING_LENGTH]; |
38 | 38 | ||
39 | char *help = "[i] commands: /f ID (to add friend), /m friendnumber message (to send message), /s status (to change status)\n" | 39 | char *help = "[i] commands:\n/f ID (to add friend)\n/m friendnumber message " |
40 | "[i] /l list (list friends), /h for help, /i for info, /n nick (to change nickname), /q (to quit)"; | 40 | "(to send message)\n/s status (to change status)\n[i] /l list (l" |
41 | "ist friends)\n/h for help\n/i for info\n/n nick (to change nick" | ||
42 | "name)\n/q (to quit)"; | ||
41 | int x, y; | 43 | int x, y; |
42 | 44 | ||
43 | |||
44 | uint8_t pending_requests[256][CLIENT_ID_SIZE]; | 45 | uint8_t pending_requests[256][CLIENT_ID_SIZE]; |
45 | uint8_t num_requests = 0; | 46 | uint8_t num_requests = 0; |
46 | 47 | ||
@@ -82,18 +83,23 @@ void new_lines(char *line) | |||
82 | void print_friendlist() | 83 | void print_friendlist() |
83 | { | 84 | { |
84 | char name[MAX_NAME_LENGTH]; | 85 | char name[MAX_NAME_LENGTH]; |
86 | int i = 0; | ||
85 | new_lines("[i] Friend List:"); | 87 | new_lines("[i] Friend List:"); |
86 | uint32_t i; | 88 | while(getname(i, (uint8_t *)name) != -1) { |
87 | for (i = 0; i <= num_requests; i++) { | 89 | /* account for the longest name and the longest "base" string */ |
88 | char fstring[128]; | 90 | char fstring[MAX_NAME_LENGTH + strlen("[i] Friend: NULL\n\tid: ")]; |
89 | getname(i, (uint8_t*)name); | 91 | |
90 | if (strlen(name) <= 0) { | 92 | if (strlen(name) <= 0) { |
91 | sprintf(fstring, "[i] Friend: NULL\n\tid: %i", i); | 93 | sprintf(fstring, "[i] Friend: No Friend!\n\tid: %i", i); |
92 | } else { | 94 | } else { |
93 | sprintf(fstring, "[i] Friend: %s\n\tid: %i", (uint8_t*)name, i); | 95 | sprintf(fstring, "[i] Friend: %s\n\tid: %i", (uint8_t*)name, i); |
94 | } | 96 | } |
97 | i++; | ||
95 | new_lines(fstring); | 98 | new_lines(fstring); |
96 | } | 99 | } |
100 | |||
101 | if(i == 0) | ||
102 | new_lines("\tno friends! D:"); | ||
97 | } | 103 | } |
98 | 104 | ||
99 | char *format_message(char *message, int friendnum) | 105 | char *format_message(char *message, int friendnum) |
@@ -122,7 +128,7 @@ char *format_message(char *message, int friendnum) | |||
122 | return msg; | 128 | return msg; |
123 | } | 129 | } |
124 | 130 | ||
125 | void line_eval(char lines[HISTORY][STRING_LENGTH], char *line) | 131 | void line_eval(char *line) |
126 | { | 132 | { |
127 | if (line[0] == '/') { | 133 | if (line[0] == '/') { |
128 | char inpt_command = line[1]; | 134 | char inpt_command = line[1]; |
@@ -236,8 +242,7 @@ void line_eval(char lines[HISTORY][STRING_LENGTH], char *line) | |||
236 | do_refresh(); | 242 | do_refresh(); |
237 | } | 243 | } |
238 | else if (inpt_command == 'h') { //help | 244 | else if (inpt_command == 'h') { //help |
239 | new_lines("[i] commands: /f ID (to add friend), /m friendnumber message (to send message), /s status (to change status)"); | 245 | new_lines(help); |
240 | new_lines("[i] /l list (list friends), /h for help, /i for info, /n nick (to change nickname), /q (to quit)"); | ||
241 | } | 246 | } |
242 | else if (inpt_command == 'i') { //info | 247 | else if (inpt_command == 'i') { //info |
243 | char idstring[200]; | 248 | char idstring[200]; |
@@ -353,10 +358,10 @@ void print_statuschange(int friendnumber, uint8_t *string, uint16_t length) | |||
353 | new_lines(msg); | 358 | new_lines(msg); |
354 | } | 359 | } |
355 | 360 | ||
356 | void load_key() | 361 | void load_key(char *path) |
357 | { | 362 | { |
358 | FILE *data_file = NULL; | 363 | FILE *data_file = fopen(path, "r"); |
359 | data_file = fopen("data","r"); | 364 | |
360 | if (data_file) { | 365 | if (data_file) { |
361 | //load keys | 366 | //load keys |
362 | fseek(data_file, 0, SEEK_END); | 367 | fseek(data_file, 0, SEEK_END); |
@@ -368,51 +373,81 @@ void load_key() | |||
368 | exit(1); | 373 | exit(1); |
369 | } | 374 | } |
370 | Messenger_load(data, size); | 375 | Messenger_load(data, size); |
371 | } else { | 376 | |
377 | } else { | ||
372 | //else save new keys | 378 | //else save new keys |
373 | int size = Messenger_size(); | 379 | int size = Messenger_size(); |
374 | uint8_t data[size]; | 380 | uint8_t data[size]; |
375 | Messenger_save(data); | 381 | Messenger_save(data); |
376 | data_file = fopen("data","w"); | 382 | data_file = fopen(path, "w"); |
383 | |||
384 | if(!data_file) { | ||
385 | perror("[!] load_key"); | ||
386 | exit(1); | ||
387 | } | ||
388 | |||
377 | if (fwrite(data, sizeof(uint8_t), size, data_file) != size){ | 389 | if (fwrite(data, sizeof(uint8_t), size, data_file) != size){ |
378 | printf("[i] could not write data file\n[i] exiting\n"); | 390 | puts("[i] could not write data file! exiting..."); |
379 | exit(1); | 391 | exit(1); |
380 | } | 392 | } |
381 | } | 393 | } |
382 | fclose(data_file); | 394 | fclose(data_file); |
383 | } | 395 | } |
384 | 396 | ||
397 | void print_help(void) | ||
398 | { | ||
399 | printf("nTox %.1f - Command-line tox-core client\n", 0.1); | ||
400 | puts("Options:"); | ||
401 | puts("\t-h\t-\tPrint this help and exit."); | ||
402 | puts("\t-f\t-\tSpecify a keyfile to read (or write to) from."); | ||
403 | } | ||
404 | |||
385 | int main(int argc, char *argv[]) | 405 | int main(int argc, char *argv[]) |
386 | { | 406 | { |
407 | int on = 0; | ||
408 | int c = 0; | ||
409 | int i = 0; | ||
410 | char *filename = "data"; | ||
411 | char idstring[200] = {0}; | ||
412 | |||
387 | if (argc < 4) { | 413 | if (argc < 4) { |
388 | printf("[!] Usage: %s [IP] [port] [public_key] <nokey>\n", argv[0]); | 414 | printf("[!] Usage: %s [IP] [port] [public_key] <keyfile>\n", argv[0]); |
389 | exit(0); | 415 | exit(0); |
390 | } | 416 | } |
391 | int c; | 417 | |
392 | int on = 0; | 418 | for(i = 0; i < argc; i++) { |
393 | initMessenger(); | 419 | if(argv[i][0] == '-') { |
394 | //if keyfiles exist | 420 | if(argv[i][1] == 'h') { |
395 | if(argc > 4){ | 421 | print_help(); |
396 | if(strncmp(argv[4], "nokey", 6) < 0){ | 422 | exit(0); |
397 | //load_key(); | 423 | } else if(argv[i][1] == 'f') { |
424 | if(argv[i + 1] != NULL) | ||
425 | filename = argv[i + 1]; | ||
426 | else { | ||
427 | fputs("[!] you passed '-f' without giving an argument!\n", stderr); | ||
428 | } | ||
429 | } | ||
398 | } | 430 | } |
399 | } else { | ||
400 | load_key(); | ||
401 | } | 431 | } |
432 | |||
433 | initMessenger(); | ||
434 | load_key(filename); | ||
435 | |||
402 | m_callback_friendrequest(print_request); | 436 | m_callback_friendrequest(print_request); |
403 | m_callback_friendmessage(print_message); | 437 | m_callback_friendmessage(print_message); |
404 | m_callback_namechange(print_nickchange); | 438 | m_callback_namechange(print_nickchange); |
405 | m_callback_userstatus(print_statuschange); | 439 | m_callback_userstatus(print_statuschange); |
406 | 440 | ||
407 | char idstring[200]; | ||
408 | get_id(idstring); | ||
409 | initscr(); | 441 | initscr(); |
410 | noecho(); | 442 | noecho(); |
411 | raw(); | 443 | raw(); |
412 | getmaxyx(stdscr, y, x); | 444 | getmaxyx(stdscr, y, x); |
445 | |||
446 | new_lines("/h for list of commands"); | ||
447 | get_id(idstring); | ||
413 | new_lines(idstring); | 448 | new_lines(idstring); |
414 | new_lines(help); | ||
415 | strcpy(line, ""); | 449 | strcpy(line, ""); |
450 | |||
416 | IP_Port bootstrap_ip_port; | 451 | IP_Port bootstrap_ip_port; |
417 | bootstrap_ip_port.port = htons(atoi(argv[2])); | 452 | bootstrap_ip_port.port = htons(atoi(argv[2])); |
418 | int resolved_address = resolve_addr(argv[1]); | 453 | int resolved_address = resolve_addr(argv[1]); |
@@ -439,7 +474,7 @@ int main(int argc, char *argv[]) | |||
439 | 474 | ||
440 | getmaxyx(stdscr, y, x); | 475 | getmaxyx(stdscr, y, x); |
441 | if (c == '\n') { | 476 | if (c == '\n') { |
442 | line_eval(lines, line); | 477 | line_eval(line); |
443 | strcpy(line, ""); | 478 | strcpy(line, ""); |
444 | } else if (c == 8 || c == 127) { | 479 | } else if (c == 8 || c == 127) { |
445 | line[strlen(line)-1] = '\0'; | 480 | 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 @@ | |||
37 | #include <netdb.h> | 37 | #include <netdb.h> |
38 | #include "../core/Messenger.h" | 38 | #include "../core/Messenger.h" |
39 | #include "../core/network.h" | 39 | #include "../core/network.h" |
40 | |||
40 | #define STRING_LENGTH 256 | 41 | #define STRING_LENGTH 256 |
41 | #define HISTORY 50 | 42 | #define HISTORY 50 |
42 | #define PUB_KEY_BYTES 32 | 43 | #define PUB_KEY_BYTES 32 |
43 | 44 | ||
44 | void new_lines(char *line); | 45 | void new_lines(char *line); |
45 | void line_eval(char lines[HISTORY][STRING_LENGTH], char *line); | 46 | void line_eval(char *line); |
46 | void wrap(char output[STRING_LENGTH], char input[STRING_LENGTH], int line_width) ; | 47 | void wrap(char output[STRING_LENGTH], char input[STRING_LENGTH], int line_width) ; |
47 | int count_lines(char *string) ; | 48 | int count_lines(char *string) ; |
48 | char *appender(char *str, const char c); | 49 | char *appender(char *str, const char c); |
diff --git a/testing/toxic/chat.c b/testing/toxic/chat.c index 1bfd94f5..a90bb2aa 100644 --- a/testing/toxic/chat.c +++ b/testing/toxic/chat.c | |||
@@ -7,6 +7,7 @@ | |||
7 | #include <string.h> | 7 | #include <string.h> |
8 | #include <stdint.h> | 8 | #include <stdint.h> |
9 | #include <ctype.h> | 9 | #include <ctype.h> |
10 | #include <time.h> | ||
10 | 11 | ||
11 | #include "../../core/Messenger.h" | 12 | #include "../../core/Messenger.h" |
12 | #include "../../core/network.h" | 13 | #include "../../core/network.h" |
@@ -26,11 +27,15 @@ typedef struct { | |||
26 | 27 | ||
27 | extern void fix_name(uint8_t* name); | 28 | extern void fix_name(uint8_t* name); |
28 | 29 | ||
29 | |||
30 | static void chat_onMessage(ToxWindow* self, int num, uint8_t* msg, uint16_t len) { | 30 | static void chat_onMessage(ToxWindow* self, int num, uint8_t* msg, uint16_t len) { |
31 | ChatContext* ctx = (ChatContext*) self->x; | 31 | ChatContext* ctx = (ChatContext*) self->x; |
32 | uint8_t nick[MAX_NAME_LENGTH] = {0}; | 32 | uint8_t nick[MAX_NAME_LENGTH] = {0}; |
33 | 33 | ||
34 | time_t now; | ||
35 | time(&now); | ||
36 | struct tm * timeinfo; | ||
37 | timeinfo = localtime(&now); | ||
38 | |||
34 | if(ctx->friendnum != num) | 39 | if(ctx->friendnum != num) |
35 | return; | 40 | return; |
36 | 41 | ||
@@ -42,10 +47,11 @@ static void chat_onMessage(ToxWindow* self, int num, uint8_t* msg, uint16_t len) | |||
42 | fix_name(msg); | 47 | fix_name(msg); |
43 | fix_name(nick); | 48 | fix_name(nick); |
44 | 49 | ||
50 | wattron(ctx->history, COLOR_PAIR(2)); | ||
51 | wprintw(ctx->history, "%02d:%02d:%02d ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec); | ||
45 | wattron(ctx->history, COLOR_PAIR(4)); | 52 | wattron(ctx->history, COLOR_PAIR(4)); |
46 | wprintw(ctx->history, "%s: ", nick); | 53 | wprintw(ctx->history, "%s: ", nick); |
47 | wattroff(ctx->history, COLOR_PAIR(4)); | 54 | wattroff(ctx->history, COLOR_PAIR(4)); |
48 | |||
49 | wprintw(ctx->history, "%s\n", msg); | 55 | wprintw(ctx->history, "%s\n", msg); |
50 | 56 | ||
51 | self->blink = true; | 57 | self->blink = true; |
@@ -71,9 +77,26 @@ static void chat_onStatusChange(ToxWindow* self, int num, uint8_t* status, uint1 | |||
71 | 77 | ||
72 | } | 78 | } |
73 | 79 | ||
80 | /* check that the string has one non-space character */ | ||
81 | int string_is_empty(char *string) | ||
82 | { | ||
83 | int rc = 0; | ||
84 | char *copy = strdup(string); | ||
85 | |||
86 | rc = ((strtok(copy, " ") == NULL) ? 1:0); | ||
87 | free(copy); | ||
88 | |||
89 | return rc; | ||
90 | } | ||
91 | |||
74 | static void chat_onKey(ToxWindow* self, int key) { | 92 | static void chat_onKey(ToxWindow* self, int key) { |
75 | ChatContext* ctx = (ChatContext*) self->x; | 93 | ChatContext* ctx = (ChatContext*) self->x; |
76 | 94 | ||
95 | time_t now; | ||
96 | time(&now); | ||
97 | struct tm * timeinfo; | ||
98 | timeinfo = localtime(&now); | ||
99 | |||
77 | if(isprint(key)) { | 100 | if(isprint(key)) { |
78 | 101 | ||
79 | if(ctx->pos != sizeof(ctx->line)-1) { | 102 | if(ctx->pos != sizeof(ctx->line)-1) { |
@@ -81,28 +104,34 @@ static void chat_onKey(ToxWindow* self, int key) { | |||
81 | ctx->line[ctx->pos] = '\0'; | 104 | ctx->line[ctx->pos] = '\0'; |
82 | } | 105 | } |
83 | } | 106 | } |
84 | else if(key == '\n') { | ||
85 | wattron(ctx->history, COLOR_PAIR(1)); | ||
86 | wprintw(ctx->history, "you: ", ctx->line); | ||
87 | wattroff(ctx->history, COLOR_PAIR(1)); | ||
88 | |||
89 | wprintw(ctx->history, "%s\n", ctx->line); | ||
90 | 107 | ||
91 | if(m_sendmessage(ctx->friendnum, (uint8_t*) ctx->line, strlen(ctx->line)+1) < 0) { | 108 | else if(key == '\n') { |
92 | wattron(ctx->history, COLOR_PAIR(3)); | 109 | if(!string_is_empty(ctx->line)) { |
93 | wprintw(ctx->history, " * Failed to send message.\n"); | 110 | /* make sure the string has at least non-space character */ |
94 | wattroff(ctx->history, COLOR_PAIR(3)); | 111 | wattron(ctx->history, COLOR_PAIR(2)); |
112 | wprintw(ctx->history, "%02d:%02d:%02d ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec); | ||
113 | wattron(ctx->history, COLOR_PAIR(1)); | ||
114 | wprintw(ctx->history, "you: ", ctx->line); | ||
115 | wattroff(ctx->history, COLOR_PAIR(1)); | ||
116 | wprintw(ctx->history, "%s\n", ctx->line); | ||
117 | |||
118 | if(m_sendmessage(ctx->friendnum, (uint8_t*) ctx->line, strlen(ctx->line)+1) < 0) { | ||
119 | wattron(ctx->history, COLOR_PAIR(3)); | ||
120 | wprintw(ctx->history, " * Failed to send message.\n"); | ||
121 | wattroff(ctx->history, COLOR_PAIR(3)); | ||
122 | } | ||
123 | |||
124 | ctx->line[0] = '\0'; | ||
125 | ctx->pos = 0; | ||
95 | } | 126 | } |
96 | |||
97 | ctx->line[0] = '\0'; | ||
98 | ctx->pos = 0; | ||
99 | } | 127 | } |
128 | |||
100 | else if(key == 0x107 || key == 0x8 || key == 0x7f) { | 129 | else if(key == 0x107 || key == 0x8 || key == 0x7f) { |
101 | if(ctx->pos != 0) { | 130 | if(ctx->pos != 0) { |
102 | ctx->line[--ctx->pos] = '\0'; | 131 | ctx->line[--ctx->pos] = '\0'; |
103 | } | 132 | } |
104 | } | 133 | } |
105 | 134 | ||
106 | } | 135 | } |
107 | 136 | ||
108 | static void chat_onDraw(ToxWindow* self) { | 137 | static void chat_onDraw(ToxWindow* self) { |
@@ -150,7 +179,7 @@ ToxWindow new_chat(int friendnum) { | |||
150 | uint8_t nick[MAX_NAME_LENGTH] = {0}; | 179 | uint8_t nick[MAX_NAME_LENGTH] = {0}; |
151 | getname(friendnum, (uint8_t*) &nick); | 180 | getname(friendnum, (uint8_t*) &nick); |
152 | fix_name(nick); | 181 | fix_name(nick); |
153 | 182 | ||
154 | snprintf(ret.title, sizeof(ret.title), "[%s (%d)]", nick, friendnum); | 183 | snprintf(ret.title, sizeof(ret.title), "[%s (%d)]", nick, friendnum); |
155 | 184 | ||
156 | ChatContext* x = calloc(1, sizeof(ChatContext)); | 185 | ChatContext* x = calloc(1, sizeof(ChatContext)); |
diff --git a/testing/toxic/main.c b/testing/toxic/main.c index 391b0b39..3b45a89f 100644 --- a/testing/toxic/main.c +++ b/testing/toxic/main.c | |||
@@ -170,12 +170,12 @@ static void do_tox() { | |||
170 | doMessenger(); | 170 | doMessenger(); |
171 | } | 171 | } |
172 | 172 | ||
173 | static void load_data() { | 173 | static void load_data(char *path) { |
174 | FILE* fd; | 174 | FILE* fd; |
175 | size_t len; | 175 | size_t len; |
176 | uint8_t* buf; | 176 | uint8_t* buf; |
177 | 177 | ||
178 | if((fd = fopen("data", "r")) != NULL) { | 178 | if((fd = fopen(path, "r")) != NULL) { |
179 | fseek(fd, 0, SEEK_END); | 179 | fseek(fd, 0, SEEK_END); |
180 | len = ftell(fd); | 180 | len = ftell(fd); |
181 | fseek(fd, 0, SEEK_SET); | 181 | fseek(fd, 0, SEEK_SET); |
@@ -213,7 +213,7 @@ static void load_data() { | |||
213 | 213 | ||
214 | Messenger_save(buf); | 214 | Messenger_save(buf); |
215 | 215 | ||
216 | fd = fopen("data", "w"); | 216 | fd = fopen(path, "w"); |
217 | if(fd == NULL) { | 217 | if(fd == NULL) { |
218 | fprintf(stderr, "fopen() failed.\n"); | 218 | fprintf(stderr, "fopen() failed.\n"); |
219 | 219 | ||
@@ -282,13 +282,36 @@ void prepare_window(WINDOW* w) { | |||
282 | 282 | ||
283 | int main(int argc, char* argv[]) { | 283 | int main(int argc, char* argv[]) { |
284 | int ch; | 284 | int ch; |
285 | int i = 0; | ||
286 | int f_flag = 0; | ||
287 | char *filename = "data"; | ||
285 | ToxWindow* a; | 288 | ToxWindow* a; |
286 | 289 | ||
290 | for(i = 0; i < argc; i++) { | ||
291 | if(argv[i][0] == '-') { | ||
292 | if(argv[i][1] == 'f') { | ||
293 | if(argv[i + 1] != NULL) | ||
294 | filename = argv[i + 1]; | ||
295 | else { | ||
296 | f_flag = -1; | ||
297 | } | ||
298 | } | ||
299 | } | ||
300 | } | ||
301 | |||
287 | init_term(); | 302 | init_term(); |
288 | init_tox(); | 303 | init_tox(); |
289 | load_data(); | 304 | load_data(filename); |
290 | init_windows(); | 305 | init_windows(); |
291 | 306 | ||
307 | if(f_flag == -1) { | ||
308 | attron(COLOR_PAIR(3) | A_BOLD); | ||
309 | wprintw(prompt->window, "You passed '-f' without giving an argument!\n" | ||
310 | "defaulting to 'data' for a keyfile...\n"); | ||
311 | attroff(COLOR_PAIR(3) | A_BOLD); | ||
312 | } | ||
313 | |||
314 | |||
292 | while(true) { | 315 | while(true) { |
293 | // Update tox. | 316 | // Update tox. |
294 | do_tox(); | 317 | do_tox(); |