diff options
author | irungentoo <irungentoo@gmail.com> | 2013-08-12 03:39:18 -0700 |
---|---|---|
committer | irungentoo <irungentoo@gmail.com> | 2013-08-12 03:39:18 -0700 |
commit | 33e104f32ff2852da31dbcff1441ea4bdc47fe95 (patch) | |
tree | 035448a709db7cab74d9d40fcfec641694f375ec /testing/nTox.c | |
parent | 139d915482c82f2a4aa87b444008afffef728561 (diff) | |
parent | 4293c4b1e66e9547f88c86bd580b9a4c79ca7ace (diff) |
Merge pull request #438 from mkfifo/master
Messenger refactor - redid work from pull request 79
Diffstat (limited to 'testing/nTox.c')
-rw-r--r-- | testing/nTox.c | 71 |
1 files changed, 39 insertions, 32 deletions
diff --git a/testing/nTox.c b/testing/nTox.c index ee4d7de4..1322067e 100644 --- a/testing/nTox.c +++ b/testing/nTox.c | |||
@@ -85,12 +85,12 @@ void new_lines(char *line) | |||
85 | } | 85 | } |
86 | 86 | ||
87 | 87 | ||
88 | void print_friendlist() | 88 | void print_friendlist(Messenger *m) |
89 | { | 89 | { |
90 | char name[MAX_NAME_LENGTH]; | 90 | char name[MAX_NAME_LENGTH]; |
91 | int i = 0; | 91 | int i = 0; |
92 | new_lines("[i] Friend List:"); | 92 | new_lines("[i] Friend List:"); |
93 | while(getname(i, (uint8_t *)name) != -1) { | 93 | while(getname(m, i, (uint8_t *)name) != -1) { |
94 | /* account for the longest name and the longest "base" string */ | 94 | /* account for the longest name and the longest "base" string */ |
95 | char fstring[MAX_NAME_LENGTH + strlen("[i] Friend: NULL\n\tid: ")]; | 95 | char fstring[MAX_NAME_LENGTH + strlen("[i] Friend: NULL\n\tid: ")]; |
96 | 96 | ||
@@ -107,13 +107,13 @@ void print_friendlist() | |||
107 | new_lines("\tno friends! D:"); | 107 | new_lines("\tno friends! D:"); |
108 | } | 108 | } |
109 | 109 | ||
110 | char *format_message(char *message, int friendnum) | 110 | char *format_message(Messenger *m, char *message, int friendnum) |
111 | { | 111 | { |
112 | char name[MAX_NAME_LENGTH]; | 112 | char name[MAX_NAME_LENGTH]; |
113 | if (friendnum != -1) { | 113 | if (friendnum != -1) { |
114 | getname(friendnum, (uint8_t*)name); | 114 | getname(m, friendnum, (uint8_t*)name); |
115 | } else { | 115 | } else { |
116 | getself_name((uint8_t*)name); | 116 | getself_name(m, (uint8_t*)name); |
117 | } | 117 | } |
118 | char *msg = malloc(100+strlen(message)+strlen(name)+1); | 118 | char *msg = malloc(100+strlen(message)+strlen(name)+1); |
119 | 119 | ||
@@ -133,7 +133,7 @@ char *format_message(char *message, int friendnum) | |||
133 | return msg; | 133 | return msg; |
134 | } | 134 | } |
135 | 135 | ||
136 | void line_eval(char *line) | 136 | void line_eval(Messenger *m, char *line) |
137 | { | 137 | { |
138 | if (line[0] == '/') { | 138 | if (line[0] == '/') { |
139 | char inpt_command = line[1]; | 139 | char inpt_command = line[1]; |
@@ -148,7 +148,7 @@ void line_eval(char *line) | |||
148 | temp_id[i] = line[i+prompt_offset]; | 148 | temp_id[i] = line[i+prompt_offset]; |
149 | 149 | ||
150 | unsigned char *bin_string = hex_string_to_bin(temp_id); | 150 | unsigned char *bin_string = hex_string_to_bin(temp_id); |
151 | int num = m_addfriend(bin_string, (uint8_t*)"Install Gentoo", sizeof("Install Gentoo")); | 151 | int num = m_addfriend(m, bin_string, (uint8_t*)"Install Gentoo", sizeof("Install Gentoo")); |
152 | free(bin_string); | 152 | free(bin_string); |
153 | char numstring[100]; | 153 | char numstring[100]; |
154 | switch (num) { | 154 | switch (num) { |
@@ -175,7 +175,7 @@ void line_eval(char *line) | |||
175 | do_refresh(); | 175 | do_refresh(); |
176 | } | 176 | } |
177 | else if (inpt_command == 'd') { | 177 | else if (inpt_command == 'd') { |
178 | doMessenger(); | 178 | doMessenger(m); |
179 | } | 179 | } |
180 | else if (inpt_command == 'm') { //message command: /m friendnumber messsage | 180 | else if (inpt_command == 'm') { //message command: /m friendnumber messsage |
181 | size_t len = strlen(line); | 181 | size_t len = strlen(line); |
@@ -196,10 +196,10 @@ void line_eval(char *line) | |||
196 | } | 196 | } |
197 | } | 197 | } |
198 | int num = atoi(numstring); | 198 | int num = atoi(numstring); |
199 | if (m_sendmessage(num, (uint8_t*) message, strlen(message) + 1) != 1) { | 199 | if (m_sendmessage(m, num, (uint8_t*) message, strlen(message) + 1) != 1) { |
200 | new_lines("[i] could not send message"); | 200 | new_lines("[i] could not send message"); |
201 | } else { | 201 | } else { |
202 | new_lines(format_message(message, -1)); | 202 | new_lines(format_message(m, message, -1)); |
203 | } | 203 | } |
204 | } | 204 | } |
205 | else if (inpt_command == 'n') { | 205 | else if (inpt_command == 'n') { |
@@ -211,13 +211,13 @@ void line_eval(char *line) | |||
211 | name[i-3] = line[i]; | 211 | name[i-3] = line[i]; |
212 | } | 212 | } |
213 | name[i-3] = 0; | 213 | name[i-3] = 0; |
214 | setname(name, i - 2); | 214 | setname(m, name, i - 2); |
215 | char numstring[100]; | 215 | char numstring[100]; |
216 | sprintf(numstring, "[i] changed nick to %s", (char*)name); | 216 | sprintf(numstring, "[i] changed nick to %s", (char*)name); |
217 | new_lines(numstring); | 217 | new_lines(numstring); |
218 | } | 218 | } |
219 | else if (inpt_command == 'l') { | 219 | else if (inpt_command == 'l') { |
220 | print_friendlist(); | 220 | print_friendlist(m); |
221 | } | 221 | } |
222 | else if (inpt_command == 's') { | 222 | else if (inpt_command == 's') { |
223 | uint8_t status[MAX_STATUSMESSAGE_LENGTH]; | 223 | uint8_t status[MAX_STATUSMESSAGE_LENGTH]; |
@@ -228,7 +228,7 @@ void line_eval(char *line) | |||
228 | status[i-3] = line[i]; | 228 | status[i-3] = line[i]; |
229 | } | 229 | } |
230 | status[i-3] = 0; | 230 | status[i-3] = 0; |
231 | m_set_statusmessage(status, strlen((char*)status) + 1); | 231 | m_set_statusmessage(m, status, strlen((char*)status) + 1); |
232 | char numstring[100]; | 232 | char numstring[100]; |
233 | sprintf(numstring, "[i] changed status to %s", (char*)status); | 233 | sprintf(numstring, "[i] changed status to %s", (char*)status); |
234 | new_lines(numstring); | 234 | new_lines(numstring); |
@@ -240,7 +240,7 @@ void line_eval(char *line) | |||
240 | sprintf(numchar,"[i] you either didn't receive that request or you already accepted it"); | 240 | sprintf(numchar,"[i] you either didn't receive that request or you already accepted it"); |
241 | new_lines(numchar); | 241 | new_lines(numchar); |
242 | } else { | 242 | } else { |
243 | int num = m_addfriend_norequest(pending_requests[numf].id); | 243 | int num = m_addfriend_norequest(m, pending_requests[numf].id); |
244 | if (num != -1) { | 244 | if (num != -1) { |
245 | pending_requests[numf].accepted = 1; | 245 | pending_requests[numf].accepted = 1; |
246 | sprintf(numchar, "[i] friend request %u accepted", numf); | 246 | sprintf(numchar, "[i] friend request %u accepted", numf); |
@@ -349,32 +349,32 @@ void print_request(uint8_t *public_key, uint8_t *data, uint16_t length) | |||
349 | do_refresh(); | 349 | do_refresh(); |
350 | } | 350 | } |
351 | 351 | ||
352 | void print_message(int friendnumber, uint8_t * string, uint16_t length) | 352 | void print_message(Messenger *m, int friendnumber, uint8_t * string, uint16_t length) |
353 | { | 353 | { |
354 | new_lines(format_message((char*)string, friendnumber)); | 354 | new_lines(format_message(m, (char*)string, friendnumber)); |
355 | } | 355 | } |
356 | 356 | ||
357 | void print_nickchange(int friendnumber, uint8_t *string, uint16_t length) | 357 | void print_nickchange(Messenger *m, int friendnumber, uint8_t *string, uint16_t length) |
358 | { | 358 | { |
359 | char name[MAX_NAME_LENGTH]; | 359 | char name[MAX_NAME_LENGTH]; |
360 | if(getname(friendnumber, (uint8_t*)name) != -1) { | 360 | if(getname(m, friendnumber, (uint8_t*)name) != -1) { |
361 | char msg[100+length]; | 361 | char msg[100+length]; |
362 | sprintf(msg, "[i] [%d] %s is now known as %s.", friendnumber, name, string); | 362 | sprintf(msg, "[i] [%d] %s is now known as %s.", friendnumber, name, string); |
363 | new_lines(msg); | 363 | new_lines(msg); |
364 | } | 364 | } |
365 | } | 365 | } |
366 | 366 | ||
367 | void print_statuschange(int friendnumber, uint8_t *string, uint16_t length) | 367 | void print_statuschange(Messenger *m, int friendnumber, uint8_t *string, uint16_t length) |
368 | { | 368 | { |
369 | char name[MAX_NAME_LENGTH]; | 369 | char name[MAX_NAME_LENGTH]; |
370 | if(getname(friendnumber, (uint8_t*)name) != -1) { | 370 | if(getname(m, friendnumber, (uint8_t*)name) != -1) { |
371 | char msg[100+length+strlen(name)+1]; | 371 | char msg[100+length+strlen(name)+1]; |
372 | sprintf(msg, "[i] [%d] %s's status changed to %s.", friendnumber, name, string); | 372 | sprintf(msg, "[i] [%d] %s's status changed to %s.", friendnumber, name, string); |
373 | new_lines(msg); | 373 | new_lines(msg); |
374 | } | 374 | } |
375 | } | 375 | } |
376 | 376 | ||
377 | void load_key(char *path) | 377 | void load_key(Messenger *m, char *path) |
378 | { | 378 | { |
379 | FILE *data_file = fopen(path, "r"); | 379 | FILE *data_file = fopen(path, "r"); |
380 | int size = 0; | 380 | int size = 0; |
@@ -390,13 +390,13 @@ void load_key(char *path) | |||
390 | fputs("[!] could not read data file! exiting...\n", stderr); | 390 | fputs("[!] could not read data file! exiting...\n", stderr); |
391 | goto FILE_ERROR; | 391 | goto FILE_ERROR; |
392 | } | 392 | } |
393 | Messenger_load(data, size); | 393 | Messenger_load(m, data, size); |
394 | 394 | ||
395 | } else { | 395 | } else { |
396 | //else save new keys | 396 | //else save new keys |
397 | int size = Messenger_size(); | 397 | int size = Messenger_size(m); |
398 | uint8_t data[size]; | 398 | uint8_t data[size]; |
399 | Messenger_save(data); | 399 | Messenger_save(m, data); |
400 | data_file = fopen(path, "w"); | 400 | data_file = fopen(path, "w"); |
401 | 401 | ||
402 | if(!data_file) { | 402 | if(!data_file) { |
@@ -435,6 +435,7 @@ int main(int argc, char *argv[]) | |||
435 | int i = 0; | 435 | int i = 0; |
436 | char *filename = "data"; | 436 | char *filename = "data"; |
437 | char idstring[200] = {0}; | 437 | char idstring[200] = {0}; |
438 | Messenger *m; | ||
438 | 439 | ||
439 | if (argc < 4) { | 440 | if (argc < 4) { |
440 | printf("[!] Usage: %s [IP] [port] [public_key] <keyfile>\n", argv[0]); | 441 | printf("[!] Usage: %s [IP] [port] [public_key] <keyfile>\n", argv[0]); |
@@ -458,13 +459,18 @@ int main(int argc, char *argv[]) | |||
458 | } | 459 | } |
459 | } | 460 | } |
460 | 461 | ||
461 | initMessenger(); | 462 | m = initMessenger(); |
462 | load_key(filename); | 463 | if( !m ){ |
464 | fputs("Failed to allocate Messenger datastructure", stderr); | ||
465 | exit(0); | ||
466 | } | ||
467 | |||
468 | load_key(m, filename); | ||
463 | 469 | ||
464 | m_callback_friendrequest(print_request); | 470 | m_callback_friendrequest(m, print_request); |
465 | m_callback_friendmessage(print_message); | 471 | m_callback_friendmessage(m, print_message); |
466 | m_callback_namechange(print_nickchange); | 472 | m_callback_namechange(m, print_nickchange); |
467 | m_callback_statusmessage(print_statuschange); | 473 | m_callback_statusmessage(m, print_statuschange); |
468 | 474 | ||
469 | initscr(); | 475 | initscr(); |
470 | noecho(); | 476 | noecho(); |
@@ -494,7 +500,7 @@ int main(int argc, char *argv[]) | |||
494 | on = 1; | 500 | on = 1; |
495 | } | 501 | } |
496 | 502 | ||
497 | doMessenger(); | 503 | doMessenger(m); |
498 | c_sleep(1); | 504 | c_sleep(1); |
499 | do_refresh(); | 505 | do_refresh(); |
500 | 506 | ||
@@ -504,7 +510,7 @@ int main(int argc, char *argv[]) | |||
504 | 510 | ||
505 | getmaxyx(stdscr, y, x); | 511 | getmaxyx(stdscr, y, x); |
506 | if (c == '\n') { | 512 | if (c == '\n') { |
507 | line_eval(line); | 513 | line_eval(m, line); |
508 | strcpy(line, ""); | 514 | strcpy(line, ""); |
509 | } else if (c == 8 || c == 127) { | 515 | } else if (c == 8 || c == 127) { |
510 | line[strlen(line)-1] = '\0'; | 516 | line[strlen(line)-1] = '\0'; |
@@ -512,6 +518,7 @@ int main(int argc, char *argv[]) | |||
512 | strcpy(line, appender(line, (char) c)); | 518 | strcpy(line, appender(line, (char) c)); |
513 | } | 519 | } |
514 | } | 520 | } |
521 | cleanupMessenger(m); | ||
515 | endwin(); | 522 | endwin(); |
516 | return 0; | 523 | return 0; |
517 | } | 524 | } |