comparison nnchat.c @ 26:b84fc46c6035

Improved color hex triplet parsing.
author Matti Hamalainen <ccr@tnsp.org>
date Wed, 16 Jul 2008 17:23:43 +0300
parents 3b67a9a806a7
children da721f94c60f
comparison
equal deleted inserted replaced
25:3b67a9a806a7 26:b84fc46c6035
67 67
68 return "???"; 68 return "???";
69 } 69 }
70 #endif 70 #endif
71 71
72 int getColor(char *str)
73 {
74 char *p = str;
75 int len, val = 0;
76
77 for (len = 0; *p && len < 6; p++, len++) {
78 if (*p >= '0' && *p <= '9') {
79 val *= 16; val += (*p - '0');
80 } else if (*p >= 'A' && *p <= 'F') {
81 val *= 16; val += (*p - 'A') + 10;
82 } else if (*p >= 'a' && *p <= 'f') {
83 val *= 16; val += (*p - 'a') + 10;
84 } else
85 return -1;
86 }
87
88 return (len == 6) ? val : -1;
89 }
90
72 BOOL argHandleOpt(const int optN, char *optArg, char *currArg) 91 BOOL argHandleOpt(const int optN, char *optArg, char *currArg)
73 { 92 {
74 switch (optN) { 93 switch (optN) {
75 case 0: 94 case 0:
76 argShowHelp(); 95 argShowHelp();
88 case 3: 107 case 3:
89 optServer = optArg; 108 optServer = optArg;
90 break; 109 break;
91 110
92 case 4: 111 case 4:
93 if (sscanf(optArg, "%06x", &optUserColor) != 1) { 112 if ((optUserColor = getColor(optArg)) < 0) {
94 THERR("Invalid color argument '%s', should be a RGB hex triplet '000000'.\n", 113 THERR("Invalid color argument '%s', should be a RGB hex triplet '000000'.\n",
95 optArg); 114 optArg);
96 return FALSE; 115 return FALSE;
97 } 116 }
98 THMSG(1, "Using color #%06x\n", optUserColor); 117 THMSG(1, "Using color #%06x\n", optUserColor);
640 659
641 /* Check command */ 660 /* Check command */
642 if (*buf == 0) { 661 if (*buf == 0) {
643 return 1; 662 return 1;
644 } else if (!strncmp(buf, "/color ", 7)) { 663 } else if (!strncmp(buf, "/color ", 7)) {
645 if (sscanf(buf+7, "%6x", &optUserColor) != 1) { 664 if ((optUserColor = getColor(buf+7)) < 0) {
646 printMsg("Invalid color value '%s'\n", buf+7); 665 printMsg("Invalid color value '%s'\n", buf+7);
647 return 1; 666 return 1;
648 } 667 }
649 printMsg("Setting color to %06X", optUserColor); 668 printMsg("Setting color to #%06x\n", optUserColor);
650 sendUserMsg(sock, optUserName2, "%%2FSetFontColor%%20%%2Dcolor%%20%06X", optUserColor); 669 sendUserMsg(sock, optUserName2, "%%2FSetFontColor%%20%%2Dcolor%%20%06X", optUserColor);
651 return 0; 670 return 0;
652 } else if (!strncmp(buf, "/msg ", 5)) { 671 } else if (!strncmp(buf, "/msg ", 5)) {
653 if (setTarget) { 672 if (setTarget) {
654 snprintf(tmpBuf, sizeof(tmpBuf), "/prv -to %s -msg %s", setTarget, buf+5); 673 snprintf(tmpBuf, sizeof(tmpBuf), "/prv -to %s -msg %s", setTarget, buf+5);
691 710
692 int main(int argc, char *argv[]) 711 int main(int argc, char *argv[])
693 { 712 {
694 int tmpSocket; 713 int tmpSocket;
695 struct hostent *tmpHost; 714 struct hostent *tmpHost;
696 BOOL exitProg = FALSE, colorSet = FALSE; 715 BOOL argsOK, exitProg = FALSE, colorSet = FALSE;
697 struct timeval tv; 716 struct timeval tv;
698 fd_set sockfds; 717 fd_set sockfds;
699 fd_set inputfds; 718 fd_set inputfds;
700 char *tmpStr; 719 char *tmpStr;
701 720
704 "Written and designed by Anonymous Finnish Guy (C) 2008", 723 "Written and designed by Anonymous Finnish Guy (C) 2008",
705 "This software is freeware, use and distribute as you wish."); 724 "This software is freeware, use and distribute as you wish.");
706 th_verbosityLevel = 0; 725 th_verbosityLevel = 0;
707 726
708 /* Parse arguments */ 727 /* Parse arguments */
709 th_args_process(argc, argv, optList, optListN, 728 argsOK = th_args_process(argc, argv, optList, optListN,
710 argHandleOpt, argHandleFile, FALSE); 729 argHandleOpt, argHandleFile, FALSE);
711 730
712 /* Check the mode and arguments */ 731 /* Check the mode and arguments */
713 if (optUserName == NULL || optPassword == NULL) { 732 if (optUserName == NULL || optPassword == NULL) {
714 THERR("User/pass not specified, get some --help\n"); 733 THERR("User/pass not specified, get some --help\n");
715 return -1; 734 return -1;
716 } 735 }
736
737 if (!argsOK)
738 return -2;
717 739
718 /* Open logfile */ 740 /* Open logfile */
719 if (optLogFilename) { 741 if (optLogFilename) {
720 THMSG(1, "Opening logfile '%s'\n", optLogFilename); 742 THMSG(1, "Opening logfile '%s'\n", optLogFilename);
721 743
742 THERR("Could not resolve hostname: %s.\n", 764 THERR("Could not resolve hostname: %s.\n",
743 hstrerror(h_errno)); 765 hstrerror(h_errno));
744 return -3; 766 return -3;
745 } 767 }
746 THMSG(2, "True hostname: %s\n", tmpHost->h_name); 768 THMSG(2, "True hostname: %s\n", tmpHost->h_name);
747 769
748 /* To emulate the official client, we first make a fake connection ... */ 770 /* To emulate the official client, we first make a fake connection ... */
749 if ((tmpSocket = openConnection((struct in_addr *) tmpHost->h_addr, optPort)) < 0) { 771 if ((tmpSocket = openConnection((struct in_addr *) tmpHost->h_addr, optPort)) < 0) {
750 THERR("Fakeprobe connection setup failed!\n"); 772 THERR("Fakeprobe connection setup failed!\n");
751 goto err_exit; 773 goto err_exit;
752 } 774 }
761 gotBuf = recv(tmpSocket, tmpBuf, sizeof(tmpBuf), 0); 783 gotBuf = recv(tmpSocket, tmpBuf, sizeof(tmpBuf), 0);
762 tmpBuf[gotBuf-1] = 0; 784 tmpBuf[gotBuf-1] = 0;
763 THMSG(2, "Probe got: %s\n", tmpBuf); 785 THMSG(2, "Probe got: %s\n", tmpBuf);
764 closeConnection(tmpSocket); 786 closeConnection(tmpSocket);
765 } 787 }
766 788
767 /* Okay, now do the proper connection ... */ 789 /* Okay, now do the proper connection ... */
768 if ((tmpSocket = openConnection((struct in_addr *) tmpHost->h_addr, optPort)) < 0) { 790 if ((tmpSocket = openConnection((struct in_addr *) tmpHost->h_addr, optPort)) < 0) {
769 THERR("Main connection setup failed!\n"); 791 THERR("Main connection setup failed!\n");
770 goto err_exit; 792 goto err_exit;
771 } 793 }
794
772 795
773 THMSG(1, "Connected, logging in as '%s'.\n", optUserName); 796 THMSG(1, "Connected, logging in as '%s'.\n", optUserName);
774 optUserName2 = encodeStr1(optUserName); 797 optUserName2 = encodeStr1(optUserName);
775 798
776 sendUserMsg(tmpSocket, optUserName2, "%%2Flogin%%20%%2Dsite%%20NN%%20%%2Dpassword%%20%s", optPassword); 799 sendUserMsg(tmpSocket, optUserName2, "%%2Flogin%%20%%2Dsite%%20NN%%20%%2Dpassword%%20%s", optPassword);
844 } 867 }
845 } /* !optDaemon */ 868 } /* !optDaemon */
846 869
847 if (!colorSet) { 870 if (!colorSet) {
848 colorSet = TRUE; 871 colorSet = TRUE;
849 sendUserMsg(tmpSocket, optUserName2, "%%2FSetFontColor%%20%%2Dcolor%%20%06X", optUserColor); 872 //sendUserMsg(tmpSocket, optUserName2, "%%2FSetFontColor%%20%%2Dcolor%%20%06X", optUserColor);
850 } 873 }
851 874
852 fflush(stdout); 875 fflush(stdout);
853 fflush(stderr); 876 fflush(stderr);
854 } 877 }
855 878
856 /* Shotdiwn */ 879 /* Shotdiwn */
857 err_exit: 880 err_exit:
881 THMSG(1, "Error exit.\n");
858 th_free(optUserName2); 882 th_free(optUserName2);
859 883
860 closeConnection(tmpSocket); 884 closeConnection(tmpSocket);
861 885
862 #ifdef __WIN32 886 #ifdef __WIN32