changeset 43:40ba8cdcf03a

Cleanups, added a silly morse code conversion mode.
author Matti Hamalainen <ccr@tnsp.org>
date Mon, 27 Oct 2008 01:00:49 +0200
parents dd59868059d5
children dfe6b835a745
files nnchat.c
diffstat 1 files changed, 122 insertions(+), 39 deletions(-) [+]
line wrap: on
line diff
--- a/nnchat.c	Mon Oct 27 00:58:34 2008 +0200
+++ b/nnchat.c	Mon Oct 27 01:00:49 2008 +0200
@@ -41,7 +41,8 @@
 WINDOW  *mainWin = NULL,
 		*statusWin = NULL,
 		*editWin = NULL;
-BOOL    setPrvMode = FALSE;
+BOOL    setPrvMode = FALSE,
+		setMorseMode = FALSE;
 
 
 /* Arguments
@@ -57,7 +58,7 @@
 	{ 7, 'S', "site",       "Site (default: NN)", OPT_ARGREQ },
 };
 
-const int optListN = (sizeof(optList) / sizeof(optarg_t));
+const int optListN = (sizeof(optList) / sizeof(optList[0]));
 
 
 void argShowHelp()
@@ -151,7 +152,6 @@
 	return TRUE;
 }
 
-
 typedef struct {
 	ssize_t pos, len, size;
 	char *data;
@@ -497,6 +497,87 @@
 }
 
 
+typedef struct {
+	char c;
+	char *code;
+} conv_ent_t;
+
+conv_ent_t morseTab[] = {
+  { ' ', " / " },
+  { 'A', ".-" },
+  { 'B', "-..." },
+  { 'C', "-.-." },
+  { 'D', "-.." },
+  { 'E', "." },
+  { 'F', "..-." },
+  { 'G', "--." },
+  { 'H', "...." },
+  { 'I', ".." },
+  { 'J', ".---" },
+  { 'K', "-.-" },
+  { 'L', ".-.." },
+  { 'M', "--" },
+  { 'N', "-." },
+  { 'O', "---" },
+  { 'P', ".--." },
+  { 'Q', "--.-" },
+  { 'R', ".-." },
+  { 'S', "..." },
+  { 'T', "-" },
+  { 'U', "..-" },
+  { 'V', "...-" },
+  { 'W', ".--" },
+  { 'X', "-..-" },
+  { 'Y', "-.--" },
+  { 'Z', "--.." },
+  { '0', "-----" },
+  { '1', ".----" },
+  { '2', "..---" },
+  { '3', "...--" },
+  { '4', "....-" },
+  { '5', "....." },
+  { '6', "-...." },
+  { '7', "--..." },
+  { '8', "---.." },
+  { '9', "----." },
+  { '.', ".-.-.-" },
+  { ',', "--..--" },
+  { '?', "..--.." },
+};
+
+
+char * conv2tab(conv_ent_t *tab, size_t ntab, char *str)
+{
+	char *result, *s = str;
+	size_t resSize, resPos = 0;
+
+	if (!str) return NULL;
+	
+	resSize = (strlen(str) * 4) + SET_ALLOC_SIZE;
+	if ((result = th_malloc(resSize)) == NULL)
+		return NULL;
+	
+	while (*s) {
+		size_t i;
+		char c = toupper(*s);
+		BOOL found = FALSE;
+		for (i = 0; i < ntab; i++)
+		if (tab[i].c == c) {
+			PUSHSTR(tab[i].code);
+			PUSHCHAR(' ');
+			found = TRUE;
+			break;
+		}
+		if (!found) PUSHCHAR(*s);
+		
+		s++;
+	}
+	PUSHCHAR(0);
+
+	return result;	
+}
+
+
 char *encodeStr1(char *str)
 {
 	char *result, *s = str;
@@ -646,7 +727,7 @@
 	{ '>', "&gt;" },
 };
 
-const int numHTMLEntities = (sizeof(HTMLEntities) / sizeof(html_entity_t));
+const int numHTMLEntities = (sizeof(HTMLEntities) / sizeof(HTMLEntities[0]));
 
 
 char *encodeStr2(char *str)
@@ -866,7 +947,7 @@
 	{ "<NUMCLIENTS>", handleFoo },
 };
 
-const int nprotoCmds = (sizeof(protoCmds) / sizeof(protocmd_t));
+const int nprotoCmds = (sizeof(protoCmds) / sizeof(protoCmds[0]));
 
 
 int handleProtocol(int sock, char *buf, size_t bufLen)
@@ -931,19 +1012,10 @@
 		th_free(tmpStr2);
 		return 0;
 	} else if (!strncmp(buf, "/to ", 4)) {
-		buf += 4;
 		th_free(setTarget);
-		setTarget = th_strdup(buf);
+		setTarget = th_strdup(buf + 4);
 		printMsg("Set prv target to '%s'\n", setTarget);
 		return 0;
-	} else if (!strncmp(buf, "/msg ", 5)) {
-		if (setTarget != NULL) {
-			snprintf(tmpBuf, sizeof(tmpBuf), "/prv -to %s -msg %s", setTarget, buf+5);
-			buf = tmpBuf;
-		} else {
-			printMsg("No target set!\n");
-			return 1;
-		}
 	} else if (setPrvMode) {
 		if (setTarget != NULL) {
 			snprintf(tmpBuf, sizeof(tmpBuf), "/prv -to %s -msg %s", setTarget, buf);
@@ -953,26 +1025,34 @@
 			setPrvMode = FALSE;
 			return 1;
 		}
+	} else if (buf[0] != '/') {
+		if (setMorseMode) {
+			tmpStr = conv2tab(morseTab, sizeof(morseTab) / sizeof(morseTab[0]), buf);
+			strncpy(tmpBuf, tmpStr, sizeof(tmpBuf));
+			tmpBuf[sizeof(tmpBuf)-1] = 0;
+			th_free(tmpStr);
+			buf = tmpBuf;
+		}
 	}
 	
-	{
-		/* Send double-encoded */
-		tmpStr = encodeStr2(buf);
-		if (!tmpStr) return -2;
-		tmpStr2 = encodeStr1(tmpStr);
-		if (!tmpStr2) {
-			th_free(tmpStr);
-			return -3;
-		}
-		
-		result = sendUserMsg(sock, optUserName2, "%s", tmpStr2);
+	
+	
+	/* Send double-encoded */
+	tmpStr = encodeStr2(buf);
+	if (!tmpStr) return -2;
+	tmpStr2 = encodeStr1(tmpStr);
+	if (!tmpStr2) {
 		th_free(tmpStr);
-		th_free(tmpStr2);
-		if (result)
-			return 0;
-		else
-			return -1;
+		return -3;
 	}
+	
+	result = sendUserMsg(sock, optUserName2, "%s", tmpStr2);
+	th_free(tmpStr);
+	th_free(tmpStr2);
+	if (result)
+		return 0;
+	else
+		return -1;
 }
 
 
@@ -1229,14 +1309,6 @@
 				}
 				break;
 				
-			case 0x109: /* F1 */
-				if (insertMode)
-					insertMode = FALSE;
-				else
-					insertMode = TRUE;
-				update = TRUE;
-				break;
-			
 			case 0x204: /* ctrl+left */
 				editBuf->pos--;
 				while (editBuf->pos > 0 && !isspace(editBuf->data[editBuf->pos]))
@@ -1260,11 +1332,22 @@
 				exitProg = TRUE;
 				break;
 			
+			case 0x109: /* F1 */
+				insertMode = !insertMode;
+				update = TRUE;
+				break;
+			
 			case 0x10a: /* F2 */
 				clearBuf(editBuf);
 				update = TRUE;
 				break;
 			
+			case 0x10b: /* F3 */
+				setMorseMode = !setMorseMode;
+				printMsg("Morse code mode = %d\n", setMorseMode);
+				update = TRUE;
+				break;
+			
 			case KEY_HOME: setBufPos(editBuf, 0); update = TRUE; break;
 			case KEY_END: setBufPos(editBuf, editBuf->len); update = TRUE; break;
 			case KEY_LEFT: setBufPos(editBuf, editBuf->pos - 1); update = TRUE; break;