diff options
Diffstat (limited to 'testing')
-rw-r--r-- | testing/nTox.c | 84 |
1 files changed, 55 insertions, 29 deletions
diff --git a/testing/nTox.c b/testing/nTox.c index 22756957..61fca135 100644 --- a/testing/nTox.c +++ b/testing/nTox.c | |||
@@ -42,6 +42,7 @@ | |||
42 | 42 | ||
43 | #include <stdio.h> | 43 | #include <stdio.h> |
44 | #include <time.h> | 44 | #include <time.h> |
45 | #include <locale.h> | ||
45 | 46 | ||
46 | #ifdef __WIN32__ | 47 | #ifdef __WIN32__ |
47 | #define c_sleep(x) Sleep(1*x) | 48 | #define c_sleep(x) Sleep(1*x) |
@@ -237,6 +238,18 @@ void get_id(Tox *m, char *data) | |||
237 | } | 238 | } |
238 | } | 239 | } |
239 | 240 | ||
241 | int getfriendname_terminated(Tox *m, int friendnum, char *namebuf) | ||
242 | { | ||
243 | int res = tox_getname(m, friendnum, (uint8_t *)namebuf); | ||
244 | |||
245 | if (res >= 0) | ||
246 | namebuf[res] = 0; | ||
247 | else | ||
248 | namebuf[0] = 0; | ||
249 | |||
250 | return res; | ||
251 | } | ||
252 | |||
240 | void new_lines_mark(char *line, uint8_t special) | 253 | void new_lines_mark(char *line, uint8_t special) |
241 | { | 254 | { |
242 | int i = 0; | 255 | int i = 0; |
@@ -261,11 +274,11 @@ void new_lines(char *line) | |||
261 | const char ptrn_friend[] = "[i] Friend: %s\n+ id: %i"; | 274 | const char ptrn_friend[] = "[i] Friend: %s\n+ id: %i"; |
262 | void print_friendlist(Tox *m) | 275 | void print_friendlist(Tox *m) |
263 | { | 276 | { |
264 | char name[TOX_MAX_NAME_LENGTH]; | 277 | char name[TOX_MAX_NAME_LENGTH + 1]; |
265 | int i = 0; | 278 | int i = 0; |
266 | new_lines("[i] Friend List:"); | 279 | new_lines("[i] Friend List:"); |
267 | 280 | ||
268 | while (tox_getname(m, i, (uint8_t *)name) != -1) { | 281 | while (getfriendname_terminated(m, i, name) != -1) { |
269 | /* account for the longest name and the longest "base" string and number (int) */ | 282 | /* account for the longest name and the longest "base" string and number (int) */ |
270 | char fstring[TOX_MAX_NAME_LENGTH + strlen(ptrn_friend) + 21]; | 283 | char fstring[TOX_MAX_NAME_LENGTH + strlen(ptrn_friend) + 21]; |
271 | 284 | ||
@@ -283,34 +296,43 @@ void print_friendlist(Tox *m) | |||
283 | new_lines("+ no friends! D:"); | 296 | new_lines("+ no friends! D:"); |
284 | } | 297 | } |
285 | 298 | ||
286 | char *format_message(Tox *m, char *message, int friendnum) | 299 | static int fmtmsg_tm_mday = -1; |
287 | { | ||
288 | char name[TOX_MAX_NAME_LENGTH]; | ||
289 | 300 | ||
290 | if (friendnum != -1) { | 301 | static void print_formatted_message(Tox *m, char *message, int friendnum, uint8_t outgoing) |
291 | tox_getname(m, friendnum, (uint8_t *)name); | 302 | { |
292 | } else { | 303 | char name[TOX_MAX_NAME_LENGTH + 1]; |
293 | tox_getselfname(m, (uint8_t *)name, sizeof(name)); | 304 | getfriendname_terminated(m, friendnum, name); |
294 | } | ||
295 | 305 | ||
296 | char *msg = malloc(100 + strlen(message) + strlen(name) + 1); | 306 | char msg[100 + strlen(message) + strlen(name) + 1]; |
297 | 307 | ||
298 | time_t rawtime; | 308 | time_t rawtime; |
299 | struct tm *timeinfo; | 309 | struct tm *timeinfo; |
300 | time ( &rawtime ); | 310 | time ( &rawtime ); |
301 | timeinfo = localtime ( &rawtime ); | 311 | timeinfo = localtime ( &rawtime ); |
302 | char *time = asctime(timeinfo); | ||
303 | size_t len = strlen(time); | ||
304 | time[len - 1] = '\0'; | ||
305 | 312 | ||
306 | if (friendnum != -1) { | 313 | /* assume that printing the date once a day is enough */ |
307 | sprintf(msg, "[%d] %s <%s> %s", friendnum, time, name, message); | 314 | if (fmtmsg_tm_mday != timeinfo->tm_mday) { |
315 | fmtmsg_tm_mday = timeinfo->tm_mday; | ||
316 | /* strftime(msg, 100, "Today is %a %b %d %Y.", timeinfo); */ | ||
317 | /* %x is the locale's preferred date format */ | ||
318 | strftime(msg, 100, "Today is %x.", timeinfo); | ||
319 | new_lines(msg); | ||
320 | } | ||
321 | |||
322 | char time[64]; | ||
323 | /* strftime(time, 64, "%I:%M:%S %p", timeinfo); */ | ||
324 | /* %X is the locale's preferred time format */ | ||
325 | strftime(time, 64, "%X", timeinfo); | ||
326 | |||
327 | if (outgoing) { | ||
328 | /* tgt: friend */ | ||
329 | sprintf(msg, "[%d] %s =>{%s} %s", friendnum, time, name, message); | ||
308 | } else { | 330 | } else { |
309 | // This message came from ourselves | 331 | /* src: friend */ |
310 | sprintf(msg, "%s <%s> %s", time, name, message); | 332 | sprintf(msg, "[%d] %s <%s>: %s", friendnum, time, name, message); |
311 | } | 333 | } |
312 | 334 | ||
313 | return msg; | 335 | new_lines(msg); |
314 | } | 336 | } |
315 | 337 | ||
316 | /* forward declaration */ | 338 | /* forward declaration */ |
@@ -381,7 +403,7 @@ void line_eval(Tox *m, char *line) | |||
381 | sprintf(sss, "[i] could not send message to friend num %u", num); | 403 | sprintf(sss, "[i] could not send message to friend num %u", num); |
382 | new_lines(sss); | 404 | new_lines(sss); |
383 | } else { | 405 | } else { |
384 | new_lines(format_message(m, *posi + 1, -1)); | 406 | print_formatted_message(m, *posi + 1, num, 1); |
385 | } | 407 | } |
386 | } else | 408 | } else |
387 | new_lines("Error, bad input."); | 409 | new_lines("Error, bad input."); |
@@ -429,9 +451,7 @@ void line_eval(Tox *m, char *line) | |||
429 | 451 | ||
430 | if (num != -1) { | 452 | if (num != -1) { |
431 | pending_requests[numf].accepted = 1; | 453 | pending_requests[numf].accepted = 1; |
432 | sprintf(numchar, "[i] friend request %u accepted", numf); | 454 | sprintf(numchar, "[i] friend request %u accepted as friend no. %d", numf, num); |
433 | new_lines(numchar); | ||
434 | sprintf(numchar, "[i] added friendnumber %d", num); | ||
435 | new_lines(numchar); | 455 | new_lines(numchar); |
436 | save_data(m); | 456 | save_data(m); |
437 | } else { | 457 | } else { |
@@ -699,14 +719,16 @@ void print_request(uint8_t *public_key, uint8_t *data, uint16_t length, void *us | |||
699 | 719 | ||
700 | void print_message(Tox *m, int friendnumber, uint8_t *string, uint16_t length, void *userdata) | 720 | void print_message(Tox *m, int friendnumber, uint8_t *string, uint16_t length, void *userdata) |
701 | { | 721 | { |
702 | new_lines(format_message(m, (char *)string, friendnumber)); | 722 | /* ensure null termination */ |
723 | string[length - 1] = 0; | ||
724 | print_formatted_message(m, (char *)string, friendnumber, 0); | ||
703 | } | 725 | } |
704 | 726 | ||
705 | void print_nickchange(Tox *m, int friendnumber, uint8_t *string, uint16_t length, void *userdata) | 727 | void print_nickchange(Tox *m, int friendnumber, uint8_t *string, uint16_t length, void *userdata) |
706 | { | 728 | { |
707 | char name[TOX_MAX_NAME_LENGTH]; | 729 | char name[TOX_MAX_NAME_LENGTH + 1]; |
708 | 730 | ||
709 | if (tox_getname(m, friendnumber, (uint8_t *)name) != -1) { | 731 | if (getfriendname_terminated(m, friendnumber, name) != -1) { |
710 | char msg[100 + length]; | 732 | char msg[100 + length]; |
711 | sprintf(msg, "[i] [%d] %s is now known as %s.", friendnumber, name, string); | 733 | sprintf(msg, "[i] [%d] %s is now known as %s.", friendnumber, name, string); |
712 | new_lines(msg); | 734 | new_lines(msg); |
@@ -715,9 +737,9 @@ void print_nickchange(Tox *m, int friendnumber, uint8_t *string, uint16_t length | |||
715 | 737 | ||
716 | void print_statuschange(Tox *m, int friendnumber, uint8_t *string, uint16_t length, void *userdata) | 738 | void print_statuschange(Tox *m, int friendnumber, uint8_t *string, uint16_t length, void *userdata) |
717 | { | 739 | { |
718 | char name[TOX_MAX_NAME_LENGTH]; | 740 | char name[TOX_MAX_NAME_LENGTH + 1]; |
719 | 741 | ||
720 | if (tox_getname(m, friendnumber, (uint8_t *)name) != -1) { | 742 | if (getfriendname_terminated(m, friendnumber, name) != -1) { |
721 | char msg[100 + length + strlen(name) + 1]; | 743 | char msg[100 + length + strlen(name) + 1]; |
722 | sprintf(msg, "[i] [%d] %s's status changed to %s.", friendnumber, name, string); | 744 | sprintf(msg, "[i] [%d] %s's status changed to %s.", friendnumber, name, string); |
723 | new_lines(msg); | 745 | new_lines(msg); |
@@ -878,6 +900,9 @@ void write_file(Tox *m, int friendnumber, uint8_t filenumber, uint8_t *data, uin | |||
878 | 900 | ||
879 | int main(int argc, char *argv[]) | 901 | int main(int argc, char *argv[]) |
880 | { | 902 | { |
903 | /* minimalistic locale support (i.e. when printing dates) */ | ||
904 | setlocale(LC_ALL, ""); | ||
905 | |||
881 | if (argc < 4) { | 906 | if (argc < 4) { |
882 | if ((argc == 2) && !strcmp(argv[1], "-h")) { | 907 | if ((argc == 2) && !strcmp(argv[1], "-h")) { |
883 | print_help(argv[0]); | 908 | print_help(argv[0]); |
@@ -950,8 +975,9 @@ int main(int argc, char *argv[]) | |||
950 | nodelay(stdscr, TRUE); | 975 | nodelay(stdscr, TRUE); |
951 | 976 | ||
952 | new_lines("[i] change username with /n"); | 977 | new_lines("[i] change username with /n"); |
953 | uint8_t name[TOX_MAX_NAME_LENGTH]; | 978 | uint8_t name[TOX_MAX_NAME_LENGTH + 1]; |
954 | uint16_t namelen = tox_getselfname(m, name, sizeof(name)); | 979 | uint16_t namelen = tox_getselfname(m, name, sizeof(name)); |
980 | name[namelen] = 0; | ||
955 | 981 | ||
956 | if (namelen > 0) { | 982 | if (namelen > 0) { |
957 | char whoami[128 + TOX_MAX_NAME_LENGTH]; | 983 | char whoami[128 + TOX_MAX_NAME_LENGTH]; |