comparison nnchat.c @ 27:da721f94c60f

Use ANSI colours.
author Matti Hamalainen <ccr@tnsp.org>
date Sat, 02 Aug 2008 04:04:58 +0300
parents b84fc46c6035
children 512775f6b081
comparison
equal deleted inserted replaced
26:b84fc46c6035 27:da721f94c60f
19 19
20 20
21 #define SET_ALLOC_SIZE (128) 21 #define SET_ALLOC_SIZE (128)
22 #define SET_SELECT_USEC (100000) 22 #define SET_SELECT_USEC (100000)
23 23
24 #define ANSI_BLACK "\x1b[0;30m"
25 #define ANSI_RED "\x1b[0;31m"
26 #define ANSI_GREEN "\x1b[0;32m"
27 #define ANSI_YELLOW "\x1b[0;33m"
28 #define ANSI_BLUE "\x1b[0;34m"
29 #define ANSI_MAGENTA "\x1b[0;35m"
30 #define ANSI_CYAN "\x1b[0;36m"
31 #define ANSI_WHITE "\x1b[0;37m"
32
33 #define ANSI_L_BLACK "\x1b[0;1;30m"
34 #define ANSI_L_RED "\x1b[0;1;31m"
35 #define ANSI_L_GREEN "\x1b[0;1;32m"
36 #define ANSI_L_YELLOW "\x1b[0;1;33m"
37 #define ANSI_L_BLUE "\x1b[0;1;34m"
38 #define ANSI_L_MAGENTA "\x1b[0;1;35m"
39 #define ANSI_L_CYAN "\x1b[0;1;36m"
40 #define ANSI_L_WHITE "\x1b[0;1;37m"
41
42 #define ANSI_END "\x1b[0m"
43
24 44
25 /* Options 45 /* Options
26 */ 46 */
27 int optPort = 8005; 47 int optPort = 8005;
28 int optUserColor = 0x408060; 48 int optUserColor = 0x408060;
29 char *optServer = "www11.servemedata.com", 49 char *optServer = "www11.servemedata.com",
30 *optUserName = NULL, 50 *optUserName = NULL,
31 *optUserName2 = NULL, 51 *optUserName2 = NULL,
32 *optPassword = NULL, 52 *optPassword = NULL,
33 *optLogFilename = NULL, 53 *optLogFilename = NULL,
34 *setTarget = NULL; 54 *setTarget = NULL;
35 BOOL optDaemon = FALSE; 55 BOOL optDaemon = FALSE;
36 FILE *optLogFile = NULL; 56 FILE *optLogFile = NULL;
37 57
38 58
39 /* Arguments 59 /* Arguments
40 */ 60 */
41 optarg_t optList[] = { 61 optarg_t optList[] = {
42 { 0, '?', "help", "Show this help", OPT_NONE }, 62 { 0, '?', "help", "Show this help", OPT_NONE },
43 { 1, 'v', "verbose", "Be more verbose", OPT_NONE }, 63 { 1, 'v', "verbose", "Be more verbose", OPT_NONE },
44 { 2, 'p', "port", "Connect to port", OPT_ARGREQ }, 64 { 2, 'p', "port", "Connect to port", OPT_ARGREQ },
45 { 3, 's', "server", "Server to connect to", OPT_ARGREQ }, 65 { 3, 's', "server", "Server to connect to", OPT_ARGREQ },
46 { 4, 'C', "color", "Initial color in RGB hex 000000", OPT_ARGREQ }, 66 { 4, 'C', "color", "Initial color in RGB hex 000000", OPT_ARGREQ },
47 { 5, 'l', "logfile", "Log filename", OPT_ARGREQ }, 67 { 5, 'l', "logfile", "Log filename", OPT_ARGREQ },
48 { 6, 'D', "daemon", "A pseudo-daemon mode for logging", OPT_NONE }, 68 { 6, 'D', "daemon", "A pseudo-daemon mode for logging", OPT_NONE },
49 }; 69 };
50 70
51 const int optListN = (sizeof(optList) / sizeof(optarg_t)); 71 const int optListN = (sizeof(optList) / sizeof(optarg_t));
52 72
53 73
66 (void) err; 86 (void) err;
67 87
68 return "???"; 88 return "???";
69 } 89 }
70 #endif 90 #endif
91
71 92
72 int getColor(char *str) 93 int getColor(char *str)
73 { 94 {
74 char *p = str; 95 char *p = str;
75 int len, val = 0; 96 int len, val = 0;
204 } 225 }
205 return TRUE; 226 return TRUE;
206 } 227 }
207 228
208 229
209 void printMsg(char *fmt, ...) 230 void printMsg(char *fmt, char *fmt2, ...)
210 { 231 {
211 char tmpStr[64] = ""; 232 char tmpStr[64] = "";
212 va_list ap; 233 va_list ap;
213 time_t timeStamp; 234 time_t timeStamp;
214 struct tm *tmpTime;; 235 struct tm *tmpTime;;
215 236
216 timeStamp = time(NULL); 237 timeStamp = time(NULL);
217 if ((tmpTime = localtime(&timeStamp)) != NULL) { 238 if ((tmpTime = localtime(&timeStamp)) != NULL) {
218 strftime(tmpStr, sizeof(tmpStr), "[%H:%M:%S] ", tmpTime); 239 strftime(tmpStr, sizeof(tmpStr), "%H:%M:%S", tmpTime);
219 } 240 }
220 241
221 if (optLogFile) { 242 if (optLogFile) {
222 fputs(tmpStr, optLogFile); 243 fprintf(optLogFile, "[%s] ", tmpStr);
223 va_start(ap, fmt); 244 va_start(ap, fmt2);
224 vfprintf(optLogFile, fmt, ap); 245 vfprintf(optLogFile, fmt, ap);
225 va_end(ap); 246 va_end(ap);
226 fflush(optLogFile); 247 fflush(optLogFile);
227 } 248 }
228 249
229 if (!optDaemon) { 250 if (!optDaemon) {
230 fputs(tmpStr, stdout); 251 fprintf(stdout, ANSI_L_BLACK "[" ANSI_L_GREEN "%s" ANSI_L_BLACK "]" ANSI_END " ", tmpStr);
231 va_start(ap, fmt); 252 va_start(ap, fmt2);
232 vfprintf(stdout, fmt, ap); 253 vfprintf(stdout, fmt2, ap);
233 va_end(ap); 254 va_end(ap);
234 fflush(stdout); 255 fflush(stdout);
235 } 256 }
236 } 257 }
237 258
339 case '+': 360 case '+':
340 PUSHCHAR(' '); 361 PUSHCHAR(' ');
341 s++; 362 s++;
342 break; 363 break;
343 364
365 case '\r':
366 PUSHCHAR(' ');
367 s++;
368 break;
369
344 case '%': 370 case '%':
345 s++; 371 s++;
346 if (*s == '%') 372 if (*s == '%')
347 PUSHCHAR('%'); 373 PUSHCHAR('%');
348 else if ((c = getxdigit(*s, 4)) >= 0) { 374 else if ((c = getxdigit(*s, 4)) >= 0) {
522 548
523 if (*s == '/') { 549 if (*s == '/') {
524 if (!strncmp(s, "/BPRV", 5)) { 550 if (!strncmp(s, "/BPRV", 5)) {
525 t = stripTags(s + 2); 551 t = stripTags(s + 2);
526 h = decodeStr2(t); 552 h = decodeStr2(t);
527 printMsg("%s\n", h); 553 printMsg("%s\n", ANSI_YELLOW "%s" ANSI_END "\n", h);
528 } else { 554 } else {
529 t = stripTags(s + 1); 555 t = stripTags(s + 1);
530 h = decodeStr2(t); 556 h = decodeStr2(t);
531 printMsg("* %s\n", h); 557 printMsg("* %s\n", ANSI_L_YELLOW "* %s" ANSI_END "\n", h);
532 } 558 }
533 th_free(h); 559 th_free(h);
534 th_free(t); 560 th_free(t);
535 } else { 561 } else {
536 t = stripTags(s); 562 t = stripTags(s);
537 h = decodeStr2(t); 563 h = decodeStr2(t);
538 printMsg("<%s> %s\n", p, h); 564 printMsg("<%s> %s\n", ANSI_MAGENTA "<" ANSI_L_CYAN "%s" ANSI_MAGENTA ">" ANSI_END " %s\n", p, h);
539 th_free(h); 565 th_free(h);
540 th_free(t); 566 th_free(t);
541 } 567 }
542 568
543 th_free(s); 569 th_free(s);
556 if ((tmpTime = localtime(&timeStamp)) != NULL) { 582 if ((tmpTime = localtime(&timeStamp)) != NULL) {
557 strftime(tmpStr, sizeof(tmpStr), "%c", tmpTime); 583 strftime(tmpStr, sizeof(tmpStr), "%c", tmpTime);
558 } 584 }
559 585
560 if (!strncmp(str, "FAILURE", 7)) { 586 if (!strncmp(str, "FAILURE", 7)) {
561 printMsg("Login failure - %s\n", tmpStr); 587 printMsg("Login failure - %s\n", "Login failure - %s\n", tmpStr);
562 return -2; 588 return -2;
563 } else if (!strncmp(str, "SUCCESS", 7)) { 589 } else if (!strncmp(str, "SUCCESS", 7)) {
564 printMsg("Login success - %s\n", tmpStr); 590 printMsg("Login success - %s\n", "Login success - %s\n", tmpStr);
565 sendUserMsg(sock, optUserName2, "%%2FRequestUserList"); 591 sendUserMsg(sock, optUserName2, "%%2FRequestUserList");
566 return 0; 592 return 0;
567 } else 593 } else
568 return 1; 594 return 1;
569 } 595 }
579 *s = 0; 605 *s = 0;
580 606
581 p = decodeStr1(str); 607 p = decodeStr1(str);
582 if (!p) return -1; 608 if (!p) return -1;
583 609
584 printMsg("! %s ADDED.\n", p); 610 printMsg("! %s ADDED.\n", "! " ANSI_GREEN "%s" ANSI_END " ADDED.\n", p);
585 th_free(p); 611 th_free(p);
586 return 0; 612 return 0;
587 } 613 }
588 614
589 615
597 *s = 0; 623 *s = 0;
598 624
599 p = decodeStr1(str); 625 p = decodeStr1(str);
600 if (!p) return -1; 626 if (!p) return -1;
601 627
602 printMsg("! %s DELETED.\n", p); 628 printMsg("! %s DELETED.\n", "! " ANSI_RED "%s" ANSI_END " DELETED.\n", p);
603 th_free(p); 629 th_free(p);
604 return 0; 630 return 0;
605 } 631 }
606 632
607 633
660 /* Check command */ 686 /* Check command */
661 if (*buf == 0) { 687 if (*buf == 0) {
662 return 1; 688 return 1;
663 } else if (!strncmp(buf, "/color ", 7)) { 689 } else if (!strncmp(buf, "/color ", 7)) {
664 if ((optUserColor = getColor(buf+7)) < 0) { 690 if ((optUserColor = getColor(buf+7)) < 0) {
665 printMsg("Invalid color value '%s'\n", buf+7); 691 printMsg("Invalid color value '%s'\n", "Invalid color value '%s'\n", buf+7);
666 return 1; 692 return 1;
667 } 693 }
668 printMsg("Setting color to #%06x\n", optUserColor); 694 printMsg("Setting color to #%06x\n", "Setting color to #%06x\n", optUserColor);
669 sendUserMsg(sock, optUserName2, "%%2FSetFontColor%%20%%2Dcolor%%20%06X", optUserColor); 695 sendUserMsg(sock, optUserName2, "%%2FSetFontColor%%20%%2Dcolor%%20%06X", optUserColor);
696 return 0;
697 } else if (!strncmp(buf, "/fake ", 6)) {
698 printMsg("Sending /%s\n", "Sending /%s\n", buf+6);
699 tmpStr = encodeStr2(tmpBuf);
700 if (!tmpStr) return -2;
701 tmpStr2 = encodeStr1(tmpStr);
702 if (!tmpStr2) {
703 th_free(tmpStr);
704 return -3;
705 }
706 sendUserMsg(sock ,optUserName2, "%%2F%s", tmpStr2);
707
708 th_free(tmpStr);
709 th_free(tmpStr2);
710 return 0;
711 } else if (!strncmp(buf, "/flood ", 7)) {
712 int i;
713
714 snprintf(tmpBuf, sizeof(tmpBuf), "/prv -to %s -msg . .",
715 buf+7);
716
717 tmpStr = encodeStr2(tmpBuf);
718 if (!tmpStr) return -2;
719 tmpStr2 = encodeStr1(tmpStr);
720 if (!tmpStr2) {
721 th_free(tmpStr);
722 return -3;
723 }
724
725 result = TRUE;
726 for (i = 0; i < 50 && result; i++) {
727 result = sendUserMsg(sock, optUserName2, "%s", tmpStr2);
728 usleep(250);
729 }
730
731 th_free(tmpStr);
732 th_free(tmpStr2);
670 return 0; 733 return 0;
671 } else if (!strncmp(buf, "/msg ", 5)) { 734 } else if (!strncmp(buf, "/msg ", 5)) {
672 if (setTarget) { 735 if (setTarget) {
673 snprintf(tmpBuf, sizeof(tmpBuf), "/prv -to %s -msg %s", setTarget, buf+5); 736 snprintf(tmpBuf, sizeof(tmpBuf), "/prv -to %s -msg %s", setTarget, buf+5);
674 buf = tmpBuf; 737 buf = tmpBuf;
675 } else { 738 } else {
676 printMsg("No target set!\n"); 739 printMsg("No target set!\n", ANSI_L_RED "No target set!" ANSI_END "\n");
677 return 1; 740 return 1;
678 } 741 }
679 } else if (!strncmp(buf, "/to ", 4)) { 742 } else if (!strncmp(buf, "/to ", 4)) {
680 buf += 4; 743 buf += 4;
681 th_free(setTarget); 744 th_free(setTarget);
682 setTarget = th_strdup(buf); 745 setTarget = th_strdup(buf);
683 printMsg("Set prv target to '%s'\n", setTarget); 746 printMsg("Set prv target to '%s'\n",
747 "Set prv target to '" ANSI_L_GREEN "%s" ANSI_END "'\n", setTarget);
684 return 0; 748 return 0;
685 } 749 }
686 750
687 { 751 {
688 /* Send double-encoded */ 752 /* Send double-encoded */
812 /* Check for incoming data from the server */ 876 /* Check for incoming data from the server */
813 tv.tv_sec = 0; 877 tv.tv_sec = 0;
814 tv.tv_usec = SET_SELECT_USEC; 878 tv.tv_usec = SET_SELECT_USEC;
815 tmpfds = sockfds; 879 tmpfds = sockfds;
816 if ((result = select(tmpSocket+1, &tmpfds, NULL, NULL, &tv)) == -1) { 880 if ((result = select(tmpSocket+1, &tmpfds, NULL, NULL, &tv)) == -1) {
817 printMsg("Error occured in select(sockfds): %s\n", strerror(errno)); 881 printMsg("Error occured in select(sockfds): %s\n",
882 "Error occured in select(sockfds): %s\n",
883 strerror(errno));
818 exitProg = TRUE; 884 exitProg = TRUE;
819 } else if (FD_ISSET(tmpSocket, &tmpfds)) { 885 } else if (FD_ISSET(tmpSocket, &tmpfds)) {
820 gotBuf = recv(tmpSocket, tmpBuf, sizeof(tmpBuf), 0); 886 gotBuf = recv(tmpSocket, tmpBuf, sizeof(tmpBuf), 0);
821 887
822 if (gotBuf < 0) { 888 if (gotBuf < 0) {
823 printMsg("Error in recv: %s\n", strerror(errno)); 889 printMsg("Error in recv: %s\n", strerror(errno));
824 exitProg = TRUE; 890 exitProg = TRUE;
825 } else if (gotBuf == 0) { 891 } else if (gotBuf == 0) {
826 printMsg("Server closed connection.\n"); 892 printMsg("Server closed connection.\n", "Server closed connection.\n");
827 exitProg = TRUE; 893 exitProg = TRUE;
828 } else { 894 } else {
829 /* Handle protocol data */ 895 /* Handle protocol data */
830 tmpBuf[gotBuf] = 0; 896 tmpBuf[gotBuf] = 0;
831 result = handleProtocol(tmpSocket, tmpBuf, gotBuf); 897 result = handleProtocol(tmpSocket, tmpBuf, gotBuf);
832 898
833 if (result > 0) { 899 if (result > 0) {
834 /* Couldn't handle the message for some reason */ 900 /* Couldn't handle the message for some reason */
835 printMsg("Could not handle: %s\n", tmpBuf); 901 printMsg("Could not handle: %s\n", "Could not handle: %s\n", tmpBuf);
836 } else if (result < 0) { 902 } else if (result < 0) {
837 /* Fatal error, quit */ 903 /* Fatal error, quit */
838 printMsg("Fatal error with message: %s\n", tmpBuf); 904 printMsg("Fatal error with message: %s\n", tmpBuf);
839 exitProg = TRUE; 905 exitProg = TRUE;
840 } 906 }
851 exitProg = TRUE; 917 exitProg = TRUE;
852 } else if (FD_ISSET(0, &tmpfds)) { 918 } else if (FD_ISSET(0, &tmpfds)) {
853 gotBuf = read(0, tmpBuf, sizeof(tmpBuf)); 919 gotBuf = read(0, tmpBuf, sizeof(tmpBuf));
854 920
855 if (gotBuf < 0) { 921 if (gotBuf < 0) {
856 printMsg("Error in reading stdio.\n"); 922 printMsg("Error in reading stdio.\n", "Error in reading stdio.\n");
857 exitProg = TRUE; 923 exitProg = TRUE;
858 } else { 924 } else {
859 /* Call the user input handler */ 925 /* Call the user input handler */
860 result = handleUserInput(tmpSocket, tmpBuf, gotBuf); 926 result = handleUserInput(tmpSocket, tmpBuf, gotBuf);
861 if (result < 0) { 927 if (result < 0) {
867 } 933 }
868 } /* !optDaemon */ 934 } /* !optDaemon */
869 935
870 if (!colorSet) { 936 if (!colorSet) {
871 colorSet = TRUE; 937 colorSet = TRUE;
872 //sendUserMsg(tmpSocket, optUserName2, "%%2FSetFontColor%%20%%2Dcolor%%20%06X", optUserColor); 938 sendUserMsg(tmpSocket, optUserName2, "%%2FSetFontColor%%20%%2Dcolor%%20%06X", optUserColor);
873 } 939 }
874 940
875 fflush(stdout); 941 fflush(stdout);
876 fflush(stderr); 942 fflush(stderr);
877 } 943 }