comparison nnchat.c @ 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 fe3140cec818
children ceec75116aa7
comparison
equal deleted inserted replaced
42:dd59868059d5 43:40ba8cdcf03a
39 BOOL optDaemon = FALSE; 39 BOOL optDaemon = FALSE;
40 FILE *optLogFile = NULL; 40 FILE *optLogFile = NULL;
41 WINDOW *mainWin = NULL, 41 WINDOW *mainWin = NULL,
42 *statusWin = NULL, 42 *statusWin = NULL,
43 *editWin = NULL; 43 *editWin = NULL;
44 BOOL setPrvMode = FALSE; 44 BOOL setPrvMode = FALSE,
45 setMorseMode = FALSE;
45 46
46 47
47 /* Arguments 48 /* Arguments
48 */ 49 */
49 optarg_t optList[] = { 50 optarg_t optList[] = {
55 { 5, 'l', "logfile", "Log filename", OPT_ARGREQ }, 56 { 5, 'l', "logfile", "Log filename", OPT_ARGREQ },
56 { 6, 'D', "daemon", "A pseudo-daemon mode for logging", OPT_NONE }, 57 { 6, 'D', "daemon", "A pseudo-daemon mode for logging", OPT_NONE },
57 { 7, 'S', "site", "Site (default: NN)", OPT_ARGREQ }, 58 { 7, 'S', "site", "Site (default: NN)", OPT_ARGREQ },
58 }; 59 };
59 60
60 const int optListN = (sizeof(optList) / sizeof(optarg_t)); 61 const int optListN = (sizeof(optList) / sizeof(optList[0]));
61 62
62 63
63 void argShowHelp() 64 void argShowHelp()
64 { 65 {
65 th_args_help(stdout, optList, optListN, th_prog_name, 66 th_args_help(stdout, optList, optListN, th_prog_name,
148 return FALSE; 149 return FALSE;
149 } 150 }
150 151
151 return TRUE; 152 return TRUE;
152 } 153 }
153
154 154
155 typedef struct { 155 typedef struct {
156 ssize_t pos, len, size; 156 ssize_t pos, len, size;
157 char *data; 157 char *data;
158 } editbuf_t; 158 } editbuf_t;
492 return FALSE; 492 return FALSE;
493 493
494 strcpy(*buf + *pos, str); 494 strcpy(*buf + *pos, str);
495 (*pos) += tmpLen; 495 (*pos) += tmpLen;
496 return TRUE; 496 return TRUE;
497 }
498
499
500 typedef struct {
501 char c;
502 char *code;
503 } conv_ent_t;
504
505 conv_ent_t morseTab[] = {
506 { ' ', " / " },
507 { 'A', ".-" },
508 { 'B', "-..." },
509 { 'C', "-.-." },
510 { 'D', "-.." },
511 { 'E', "." },
512 { 'F', "..-." },
513 { 'G', "--." },
514 { 'H', "...." },
515 { 'I', ".." },
516 { 'J', ".---" },
517 { 'K', "-.-" },
518 { 'L', ".-.." },
519 { 'M', "--" },
520 { 'N', "-." },
521 { 'O', "---" },
522 { 'P', ".--." },
523 { 'Q', "--.-" },
524 { 'R', ".-." },
525 { 'S', "..." },
526 { 'T', "-" },
527 { 'U', "..-" },
528 { 'V', "...-" },
529 { 'W', ".--" },
530 { 'X', "-..-" },
531 { 'Y', "-.--" },
532 { 'Z', "--.." },
533 { '0', "-----" },
534 { '1', ".----" },
535 { '2', "..---" },
536 { '3', "...--" },
537 { '4', "....-" },
538 { '5', "....." },
539 { '6', "-...." },
540 { '7', "--..." },
541 { '8', "---.." },
542 { '9', "----." },
543 { '.', ".-.-.-" },
544 { ',', "--..--" },
545 { '?', "..--.." },
546 };
547
548
549 char * conv2tab(conv_ent_t *tab, size_t ntab, char *str)
550 {
551 char *result, *s = str;
552 size_t resSize, resPos = 0;
553
554 if (!str) return NULL;
555
556 resSize = (strlen(str) * 4) + SET_ALLOC_SIZE;
557 if ((result = th_malloc(resSize)) == NULL)
558 return NULL;
559
560 while (*s) {
561 size_t i;
562 char c = toupper(*s);
563 BOOL found = FALSE;
564 for (i = 0; i < ntab; i++)
565 if (tab[i].c == c) {
566 PUSHSTR(tab[i].code);
567 PUSHCHAR(' ');
568 found = TRUE;
569 break;
570 }
571 if (!found) PUSHCHAR(*s);
572
573 s++;
574 }
575 PUSHCHAR(0);
576
577 return result;
497 } 578 }
498 579
499 580
500 char *encodeStr1(char *str) 581 char *encodeStr1(char *str)
501 { 582 {
644 html_entity_t HTMLEntities[] = { 725 html_entity_t HTMLEntities[] = {
645 { '<', "&lt;" }, 726 { '<', "&lt;" },
646 { '>', "&gt;" }, 727 { '>', "&gt;" },
647 }; 728 };
648 729
649 const int numHTMLEntities = (sizeof(HTMLEntities) / sizeof(html_entity_t)); 730 const int numHTMLEntities = (sizeof(HTMLEntities) / sizeof(HTMLEntities[0]));
650 731
651 732
652 char *encodeStr2(char *str) 733 char *encodeStr2(char *str)
653 { 734 {
654 char *result, *s = str; 735 char *result, *s = str;
864 { "<DELETE_USER>", handleDeleteUser }, 945 { "<DELETE_USER>", handleDeleteUser },
865 { "<ADD_USER>", handleAddUser }, 946 { "<ADD_USER>", handleAddUser },
866 { "<NUMCLIENTS>", handleFoo }, 947 { "<NUMCLIENTS>", handleFoo },
867 }; 948 };
868 949
869 const int nprotoCmds = (sizeof(protoCmds) / sizeof(protocmd_t)); 950 const int nprotoCmds = (sizeof(protoCmds) / sizeof(protoCmds[0]));
870 951
871 952
872 int handleProtocol(int sock, char *buf, size_t bufLen) 953 int handleProtocol(int sock, char *buf, size_t bufLen)
873 { 954 {
874 int i; 955 int i;
929 1010
930 th_free(tmpStr); 1011 th_free(tmpStr);
931 th_free(tmpStr2); 1012 th_free(tmpStr2);
932 return 0; 1013 return 0;
933 } else if (!strncmp(buf, "/to ", 4)) { 1014 } else if (!strncmp(buf, "/to ", 4)) {
934 buf += 4;
935 th_free(setTarget); 1015 th_free(setTarget);
936 setTarget = th_strdup(buf); 1016 setTarget = th_strdup(buf + 4);
937 printMsg("Set prv target to '%s'\n", setTarget); 1017 printMsg("Set prv target to '%s'\n", setTarget);
938 return 0; 1018 return 0;
939 } else if (!strncmp(buf, "/msg ", 5)) {
940 if (setTarget != NULL) {
941 snprintf(tmpBuf, sizeof(tmpBuf), "/prv -to %s -msg %s", setTarget, buf+5);
942 buf = tmpBuf;
943 } else {
944 printMsg("No target set!\n");
945 return 1;
946 }
947 } else if (setPrvMode) { 1019 } else if (setPrvMode) {
948 if (setTarget != NULL) { 1020 if (setTarget != NULL) {
949 snprintf(tmpBuf, sizeof(tmpBuf), "/prv -to %s -msg %s", setTarget, buf); 1021 snprintf(tmpBuf, sizeof(tmpBuf), "/prv -to %s -msg %s", setTarget, buf);
950 buf = tmpBuf; 1022 buf = tmpBuf;
951 } else { 1023 } else {
952 printMsg("No target set, exiting prv mode.\n"); 1024 printMsg("No target set, exiting prv mode.\n");
953 setPrvMode = FALSE; 1025 setPrvMode = FALSE;
954 return 1; 1026 return 1;
955 } 1027 }
956 } 1028 } else if (buf[0] != '/') {
957 1029 if (setMorseMode) {
958 { 1030 tmpStr = conv2tab(morseTab, sizeof(morseTab) / sizeof(morseTab[0]), buf);
959 /* Send double-encoded */ 1031 strncpy(tmpBuf, tmpStr, sizeof(tmpBuf));
960 tmpStr = encodeStr2(buf); 1032 tmpBuf[sizeof(tmpBuf)-1] = 0;
961 if (!tmpStr) return -2;
962 tmpStr2 = encodeStr1(tmpStr);
963 if (!tmpStr2) {
964 th_free(tmpStr); 1033 th_free(tmpStr);
965 return -3; 1034 buf = tmpBuf;
966 } 1035 }
967 1036 }
968 result = sendUserMsg(sock, optUserName2, "%s", tmpStr2); 1037
1038
1039
1040 /* Send double-encoded */
1041 tmpStr = encodeStr2(buf);
1042 if (!tmpStr) return -2;
1043 tmpStr2 = encodeStr1(tmpStr);
1044 if (!tmpStr2) {
969 th_free(tmpStr); 1045 th_free(tmpStr);
970 th_free(tmpStr2); 1046 return -3;
971 if (result) 1047 }
972 return 0; 1048
973 else 1049 result = sendUserMsg(sock, optUserName2, "%s", tmpStr2);
974 return -1; 1050 th_free(tmpStr);
975 } 1051 th_free(tmpStr2);
1052 if (result)
1053 return 0;
1054 else
1055 return -1;
976 } 1056 }
977 1057
978 1058
979 int main(int argc, char *argv[]) 1059 int main(int argc, char *argv[])
980 { 1060 {
1227 editBuf = copyBuf(histBuf[histPos]); 1307 editBuf = copyBuf(histBuf[histPos]);
1228 update = TRUE; 1308 update = TRUE;
1229 } 1309 }
1230 break; 1310 break;
1231 1311
1232 case 0x109: /* F1 */
1233 if (insertMode)
1234 insertMode = FALSE;
1235 else
1236 insertMode = TRUE;
1237 update = TRUE;
1238 break;
1239
1240 case 0x204: /* ctrl+left */ 1312 case 0x204: /* ctrl+left */
1241 editBuf->pos--; 1313 editBuf->pos--;
1242 while (editBuf->pos > 0 && !isspace(editBuf->data[editBuf->pos])) 1314 while (editBuf->pos > 0 && !isspace(editBuf->data[editBuf->pos]))
1243 editBuf->pos--; 1315 editBuf->pos--;
1244 if (editBuf->pos < 0) 1316 if (editBuf->pos < 0)
1258 case 0x111: /* F9 */ 1330 case 0x111: /* F9 */
1259 printMsg("Quitting per user request.\n"); 1331 printMsg("Quitting per user request.\n");
1260 exitProg = TRUE; 1332 exitProg = TRUE;
1261 break; 1333 break;
1262 1334
1335 case 0x109: /* F1 */
1336 insertMode = !insertMode;
1337 update = TRUE;
1338 break;
1339
1263 case 0x10a: /* F2 */ 1340 case 0x10a: /* F2 */
1264 clearBuf(editBuf); 1341 clearBuf(editBuf);
1342 update = TRUE;
1343 break;
1344
1345 case 0x10b: /* F3 */
1346 setMorseMode = !setMorseMode;
1347 printMsg("Morse code mode = %d\n", setMorseMode);
1265 update = TRUE; 1348 update = TRUE;
1266 break; 1349 break;
1267 1350
1268 case KEY_HOME: setBufPos(editBuf, 0); update = TRUE; break; 1351 case KEY_HOME: setBufPos(editBuf, 0); update = TRUE; break;
1269 case KEY_END: setBufPos(editBuf, editBuf->len); update = TRUE; break; 1352 case KEY_END: setBufPos(editBuf, editBuf->len); update = TRUE; break;