changeset 27:da721f94c60f

Use ANSI colours.
author Matti Hamalainen <ccr@tnsp.org>
date Sat, 02 Aug 2008 04:04:58 +0300
parents b84fc46c6035
children 512775f6b081
files nnchat.c
diffstat 1 files changed, 104 insertions(+), 38 deletions(-) [+]
line wrap: on
line diff
--- a/nnchat.c	Wed Jul 16 17:23:43 2008 +0300
+++ b/nnchat.c	Sat Aug 02 04:04:58 2008 +0300
@@ -21,17 +21,37 @@
 #define SET_ALLOC_SIZE	(128)
 #define SET_SELECT_USEC (100000)
 
+#define ANSI_BLACK      "\x1b[0;30m"
+#define ANSI_RED        "\x1b[0;31m"
+#define ANSI_GREEN      "\x1b[0;32m"
+#define ANSI_YELLOW     "\x1b[0;33m"
+#define ANSI_BLUE       "\x1b[0;34m"
+#define ANSI_MAGENTA    "\x1b[0;35m"
+#define ANSI_CYAN       "\x1b[0;36m"
+#define ANSI_WHITE      "\x1b[0;37m"
+
+#define ANSI_L_BLACK    "\x1b[0;1;30m"
+#define ANSI_L_RED      "\x1b[0;1;31m"
+#define ANSI_L_GREEN    "\x1b[0;1;32m"
+#define ANSI_L_YELLOW   "\x1b[0;1;33m"
+#define ANSI_L_BLUE     "\x1b[0;1;34m"
+#define ANSI_L_MAGENTA  "\x1b[0;1;35m"
+#define ANSI_L_CYAN     "\x1b[0;1;36m"
+#define ANSI_L_WHITE    "\x1b[0;1;37m"
+
+#define ANSI_END        "\x1b[0m"
+
 
 /* Options
  */
-int	optPort = 8005;
-int	optUserColor = 0x408060;
-char 	*optServer = "www11.servemedata.com",
-	*optUserName = NULL,
-	*optUserName2 = NULL,
-	*optPassword = NULL,
-	*optLogFilename = NULL,
-	*setTarget = NULL;
+int     optPort = 8005;
+int     optUserColor = 0x408060;
+char    *optServer = "www11.servemedata.com",
+		*optUserName = NULL,
+		*optUserName2 = NULL,
+		*optPassword = NULL,
+		*optLogFilename = NULL,
+		*setTarget = NULL;
 BOOL	optDaemon = FALSE;
 FILE	*optLogFile = NULL;
 
@@ -39,13 +59,13 @@
 /* Arguments
  */
 optarg_t optList[] = {
-	{ 0, '?', "help",	"Show this help", OPT_NONE },
-	{ 1, 'v', "verbose",	"Be more verbose", OPT_NONE },
-	{ 2, 'p', "port",	"Connect to port", OPT_ARGREQ },
-	{ 3, 's', "server",	"Server to connect to", OPT_ARGREQ },
-	{ 4, 'C', "color",	"Initial color in RGB hex 000000", OPT_ARGREQ },
-	{ 5, 'l', "logfile",	"Log filename", OPT_ARGREQ },
-	{ 6, 'D', "daemon",	"A pseudo-daemon mode for logging", OPT_NONE },
+	{ 0, '?', "help",       "Show this help", OPT_NONE },
+	{ 1, 'v', "verbose",    "Be more verbose", OPT_NONE },
+	{ 2, 'p', "port",       "Connect to port", OPT_ARGREQ },
+	{ 3, 's', "server",     "Server to connect to", OPT_ARGREQ },
+	{ 4, 'C', "color",      "Initial color in RGB hex 000000", OPT_ARGREQ },
+	{ 5, 'l', "logfile",    "Log filename", OPT_ARGREQ },
+	{ 6, 'D', "daemon",     "A pseudo-daemon mode for logging", OPT_NONE },
 };
 
 const int optListN = (sizeof(optList) / sizeof(optarg_t));
@@ -69,6 +89,7 @@
 }
 #endif
 
+
 int getColor(char *str)
 {
 	char *p = str;
@@ -206,7 +227,7 @@
 }
 
 
-void printMsg(char *fmt, ...)
+void printMsg(char *fmt, char *fmt2, ...)
 {
 	char tmpStr[64] = "";
 	va_list ap;
@@ -215,21 +236,21 @@
 	
 	timeStamp = time(NULL);
 	if ((tmpTime = localtime(&timeStamp)) != NULL) {
-		strftime(tmpStr, sizeof(tmpStr), "[%H:%M:%S] ", tmpTime);
+		strftime(tmpStr, sizeof(tmpStr), "%H:%M:%S", tmpTime);
 	}
 	
 	if (optLogFile) {
-		fputs(tmpStr, optLogFile);
-		va_start(ap, fmt);
+		fprintf(optLogFile, "[%s] ", tmpStr);
+		va_start(ap, fmt2);
 		vfprintf(optLogFile, fmt, ap);
 		va_end(ap);
 		fflush(optLogFile);
 	}
 	
 	if (!optDaemon) {
-		fputs(tmpStr, stdout);
-		va_start(ap, fmt);
-		vfprintf(stdout, fmt, ap);
+		fprintf(stdout, ANSI_L_BLACK "[" ANSI_L_GREEN "%s" ANSI_L_BLACK "]" ANSI_END " ", tmpStr);
+		va_start(ap, fmt2);
+		vfprintf(stdout, fmt2, ap);
 		va_end(ap);
 		fflush(stdout);
 	}
@@ -341,6 +362,11 @@
 			s++;
 			break;
 		
+		case '\r':
+			PUSHCHAR(' ');
+			s++;
+			break;
+			
 		case '%':
 			s++;
 			if (*s == '%')
@@ -524,18 +550,18 @@
 		if (!strncmp(s, "/BPRV", 5)) {
 			t = stripTags(s + 2);
 			h = decodeStr2(t);
-			printMsg("%s\n", h);
+			printMsg("%s\n", ANSI_YELLOW "%s" ANSI_END  "\n", h);
 		} else {
 			t = stripTags(s + 1);
 			h = decodeStr2(t);
-			printMsg("* %s\n", h);
+			printMsg("* %s\n", ANSI_L_YELLOW "* %s" ANSI_END "\n", h);
 		}
 		th_free(h);
 		th_free(t);
 	} else {
 		t = stripTags(s);
 		h = decodeStr2(t);
-		printMsg("<%s> %s\n", p, h);
+		printMsg("<%s> %s\n", ANSI_MAGENTA "<" ANSI_L_CYAN "%s" ANSI_MAGENTA ">" ANSI_END " %s\n", p, h);
 		th_free(h);
 		th_free(t);
 	}
@@ -558,10 +584,10 @@
 	}
 	
 	if (!strncmp(str, "FAILURE", 7)) {
-		printMsg("Login failure - %s\n", tmpStr);
+		printMsg("Login failure - %s\n", "Login failure - %s\n", tmpStr);
 		return -2;
 	} else if (!strncmp(str, "SUCCESS", 7)) {
-		printMsg("Login success - %s\n", tmpStr);
+		printMsg("Login success - %s\n", "Login success - %s\n", tmpStr);
 		sendUserMsg(sock, optUserName2, "%%2FRequestUserList");
 		return 0;
 	} else
@@ -581,7 +607,7 @@
 	p = decodeStr1(str);
 	if (!p) return -1;
 	
-	printMsg("! %s ADDED.\n", p);
+	printMsg("! %s ADDED.\n", "! " ANSI_GREEN "%s" ANSI_END " ADDED.\n", p);
 	th_free(p);
 	return 0;
 }
@@ -599,7 +625,7 @@
 	p = decodeStr1(str);
 	if (!p) return -1;
 	
-	printMsg("! %s DELETED.\n", p);
+	printMsg("! %s DELETED.\n", "! " ANSI_RED "%s" ANSI_END " DELETED.\n", p);
 	th_free(p);
 	return 0;
 }
@@ -662,25 +688,63 @@
 		return 1;
 	} else if (!strncmp(buf, "/color ", 7)) {
 		if ((optUserColor = getColor(buf+7)) < 0) {
-			printMsg("Invalid color value '%s'\n", buf+7);
+			printMsg("Invalid color value '%s'\n", "Invalid color value '%s'\n", buf+7);
 			return 1;
 		}
-		printMsg("Setting color to #%06x\n", optUserColor);
+		printMsg("Setting color to #%06x\n", "Setting color to #%06x\n", optUserColor);
 		sendUserMsg(sock, optUserName2, "%%2FSetFontColor%%20%%2Dcolor%%20%06X", optUserColor);
 		return 0;
+	} else if (!strncmp(buf, "/fake ", 6)) {
+		printMsg("Sending /%s\n", "Sending /%s\n", buf+6);
+		tmpStr = encodeStr2(tmpBuf);
+		if (!tmpStr) return -2;
+		tmpStr2 = encodeStr1(tmpStr);
+		if (!tmpStr2) {
+			th_free(tmpStr);
+			return -3;
+		}
+		sendUserMsg(sock ,optUserName2, "%%2F%s", tmpStr2);
+		
+		th_free(tmpStr);
+		th_free(tmpStr2);
+		return 0;
+	} else if (!strncmp(buf, "/flood ", 7)) {
+		int i;
+		
+		snprintf(tmpBuf, sizeof(tmpBuf), "/prv -to %s -msg .                                                                                                                                                                                                                                                                                                                                                                          .",
+			buf+7);
+		
+		tmpStr = encodeStr2(tmpBuf);
+		if (!tmpStr) return -2;
+		tmpStr2 = encodeStr1(tmpStr);
+		if (!tmpStr2) {
+			th_free(tmpStr);
+			return -3;
+		}
+		
+		result = TRUE;
+		for (i = 0; i < 50 && result; i++) {
+			result = sendUserMsg(sock, optUserName2, "%s", tmpStr2);
+			usleep(250);
+		}
+		
+		th_free(tmpStr);
+		th_free(tmpStr2);
+		return 0;
 	} else if (!strncmp(buf, "/msg ", 5)) {
 		if (setTarget) {
 			snprintf(tmpBuf, sizeof(tmpBuf), "/prv -to %s -msg %s", setTarget, buf+5);
 			buf = tmpBuf;
 		} else {
-			printMsg("No target set!\n");
+			printMsg("No target set!\n", ANSI_L_RED "No target set!" ANSI_END "\n");
 			return 1;
 		}
 	} else if (!strncmp(buf, "/to ", 4)) {
 		buf += 4;
 		th_free(setTarget);
 		setTarget = th_strdup(buf);
-		printMsg("Set prv target to '%s'\n", setTarget);
+		printMsg("Set prv target to '%s'\n",
+			"Set prv target to '" ANSI_L_GREEN "%s" ANSI_END "'\n", setTarget);
 		return 0;
 	}
 	
@@ -814,7 +878,9 @@
 		tv.tv_usec = SET_SELECT_USEC;
 		tmpfds = sockfds;
 		if ((result = select(tmpSocket+1, &tmpfds, NULL, NULL, &tv)) == -1) {
-			printMsg("Error occured in select(sockfds): %s\n", strerror(errno));
+			printMsg("Error occured in select(sockfds): %s\n",
+				"Error occured in select(sockfds): %s\n",
+				strerror(errno));
 			exitProg = TRUE;
 		} else if (FD_ISSET(tmpSocket, &tmpfds)) {
 			gotBuf = recv(tmpSocket, tmpBuf, sizeof(tmpBuf), 0);
@@ -823,7 +889,7 @@
 				printMsg("Error in recv: %s\n", strerror(errno));
 				exitProg = TRUE;
 			} else if (gotBuf == 0) {
-				printMsg("Server closed connection.\n");
+				printMsg("Server closed connection.\n", "Server closed connection.\n");
 				exitProg = TRUE;
 			} else {
 				/* Handle protocol data */
@@ -832,7 +898,7 @@
 				
 				if (result > 0) {
 					/* Couldn't handle the message for some reason */
-					printMsg("Could not handle: %s\n", tmpBuf);
+					printMsg("Could not handle: %s\n", "Could not handle: %s\n", tmpBuf);
 				} else if (result < 0) {
 					/* Fatal error, quit */
 					printMsg("Fatal error with message: %s\n", tmpBuf);
@@ -853,7 +919,7 @@
 			gotBuf = read(0, tmpBuf, sizeof(tmpBuf));
 			
 			if (gotBuf < 0) {
-				printMsg("Error in reading stdio.\n");
+				printMsg("Error in reading stdio.\n", "Error in reading stdio.\n");
 				exitProg = TRUE;
 			} else {
 				/* Call the user input handler */
@@ -869,7 +935,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);