# HG changeset patch # User Matti Hamalainen # Date 1216218223 -10800 # Node ID b84fc46c6035c25782fe0013c9b3ff56a3f6d851 # Parent 3b67a9a806a74ba9a709efd41a41987f62a50b16 Improved color hex triplet parsing. diff -r 3b67a9a806a7 -r b84fc46c6035 nnchat.c --- a/nnchat.c Wed Jul 09 17:47:40 2008 +0300 +++ b/nnchat.c Wed Jul 16 17:23:43 2008 +0300 @@ -69,6 +69,25 @@ } #endif +int getColor(char *str) +{ + char *p = str; + int len, val = 0; + + for (len = 0; *p && len < 6; p++, len++) { + if (*p >= '0' && *p <= '9') { + val *= 16; val += (*p - '0'); + } else if (*p >= 'A' && *p <= 'F') { + val *= 16; val += (*p - 'A') + 10; + } else if (*p >= 'a' && *p <= 'f') { + val *= 16; val += (*p - 'a') + 10; + } else + return -1; + } + + return (len == 6) ? val : -1; +} + BOOL argHandleOpt(const int optN, char *optArg, char *currArg) { switch (optN) { @@ -90,7 +109,7 @@ break; case 4: - if (sscanf(optArg, "%06x", &optUserColor) != 1) { + if ((optUserColor = getColor(optArg)) < 0) { THERR("Invalid color argument '%s', should be a RGB hex triplet '000000'.\n", optArg); return FALSE; @@ -642,11 +661,11 @@ if (*buf == 0) { return 1; } else if (!strncmp(buf, "/color ", 7)) { - if (sscanf(buf+7, "%6x", &optUserColor) != 1) { + if ((optUserColor = getColor(buf+7)) < 0) { printMsg("Invalid color value '%s'\n", buf+7); return 1; } - printMsg("Setting color to %06X", optUserColor); + printMsg("Setting color to #%06x\n", optUserColor); sendUserMsg(sock, optUserName2, "%%2FSetFontColor%%20%%2Dcolor%%20%06X", optUserColor); return 0; } else if (!strncmp(buf, "/msg ", 5)) { @@ -693,7 +712,7 @@ { int tmpSocket; struct hostent *tmpHost; - BOOL exitProg = FALSE, colorSet = FALSE; + BOOL argsOK, exitProg = FALSE, colorSet = FALSE; struct timeval tv; fd_set sockfds; fd_set inputfds; @@ -706,15 +725,18 @@ th_verbosityLevel = 0; /* Parse arguments */ - th_args_process(argc, argv, optList, optListN, + argsOK = th_args_process(argc, argv, optList, optListN, argHandleOpt, argHandleFile, FALSE); - + /* Check the mode and arguments */ if (optUserName == NULL || optPassword == NULL) { THERR("User/pass not specified, get some --help\n"); return -1; } + if (!argsOK) + return -2; + /* Open logfile */ if (optLogFilename) { THMSG(1, "Opening logfile '%s'\n", optLogFilename); @@ -744,7 +766,7 @@ return -3; } THMSG(2, "True hostname: %s\n", tmpHost->h_name); - + /* To emulate the official client, we first make a fake connection ... */ if ((tmpSocket = openConnection((struct in_addr *) tmpHost->h_addr, optPort)) < 0) { THERR("Fakeprobe connection setup failed!\n"); @@ -763,12 +785,13 @@ THMSG(2, "Probe got: %s\n", tmpBuf); closeConnection(tmpSocket); } - + /* Okay, now do the proper connection ... */ if ((tmpSocket = openConnection((struct in_addr *) tmpHost->h_addr, optPort)) < 0) { THERR("Main connection setup failed!\n"); goto err_exit; } + THMSG(1, "Connected, logging in as '%s'.\n", optUserName); optUserName2 = encodeStr1(optUserName); @@ -846,7 +869,7 @@ if (!colorSet) { colorSet = TRUE; - sendUserMsg(tmpSocket, optUserName2, "%%2FSetFontColor%%20%%2Dcolor%%20%06X", optUserColor); + //sendUserMsg(tmpSocket, optUserName2, "%%2FSetFontColor%%20%%2Dcolor%%20%06X", optUserColor); } fflush(stdout); @@ -855,6 +878,7 @@ /* Shotdiwn */ err_exit: + THMSG(1, "Error exit.\n"); th_free(optUserName2); closeConnection(tmpSocket);