comparison nnchat.c @ 97:218efd2f0641

Rename functions for clarity.
author Matti Hamalainen <ccr@tnsp.org>
date Tue, 17 Nov 2009 19:20:25 +0200
parents 7c9538e71c89
children b7c981e27b66
comparison
equal deleted inserted replaced
96:7c9538e71c89 97:218efd2f0641
340 340
341 q = strstr(s, "</MESSAGE>"); 341 q = strstr(s, "</MESSAGE>");
342 if (!q) return 3; 342 if (!q) return 3;
343 *q = 0; 343 *q = 0;
344 344
345 s = decodeStr1(s); 345 s = nn_decode_str1(s);
346 if (!s) return -1; 346 if (!s) return -1;
347 347
348 p = decodeStr1(p); 348 p = nn_decode_str1(p);
349 if (!p) { 349 if (!p) {
350 th_free(s); 350 th_free(s);
351 return -2; 351 return -2;
352 } 352 }
353 353
354 354
355 if (*s == '/') { 355 if (*s == '/') {
356 t = stripXMLTags(s + 1); 356 t = nn_strip_tags(s + 1);
357 if (!strncmp(t, "BPRV", 4)) { 357 if (!strncmp(t, "BPRV", 4)) {
358 h = decodeStr2(t + 1); 358 h = nn_decode_str2(t + 1);
359 printMsg("½11½%s½0½\n", h); 359 printMsg("½11½%s½0½\n", h);
360 } else { 360 } else {
361 h = decodeStr2(t); 361 h = nn_decode_str2(t);
362 printMsg("½9½* %s½0½\n", h); 362 printMsg("½9½* %s½0½\n", h);
363 } 363 }
364 th_free(h); 364 th_free(h);
365 th_free(t); 365 th_free(t);
366 } else { 366 } else {
367 BOOL isMine = strcmp(p, optUserName) == 0; 367 BOOL isMine = strcmp(p, optUserName) == 0;
368 t = stripXMLTags(s); 368 t = nn_strip_tags(s);
369 h = decodeStr2(t); 369 h = nn_decode_str2(t);
370 printMsg("½5½<½%d½%s½5½>½0½ %s\n", isMine ? 14 : 15, p, h); 370 printMsg("½5½<½%d½%s½5½>½0½ %s\n", isMine ? 14 : 15, p, h);
371 th_free(h); 371 th_free(h);
372 th_free(t); 372 th_free(t);
373 } 373 }
374 374
392 if (!strncmp(str, "FAILURE", 7)) { 392 if (!strncmp(str, "FAILURE", 7)) {
393 printMsg("½1½Login failure½0½ - ½3½%s½0½\n", tmpStr); 393 printMsg("½1½Login failure½0½ - ½3½%s½0½\n", tmpStr);
394 return -2; 394 return -2;
395 } else if (!strncmp(str, "SUCCESS", 7)) { 395 } else if (!strncmp(str, "SUCCESS", 7)) {
396 printMsg("½2½Login success½0½ - ½3½%s½0½\n", tmpStr); 396 printMsg("½2½Login success½0½ - ½3½%s½0½\n", tmpStr);
397 sendUserMsg(sock, optUserName2, "%%2FRequestUserList"); 397 nn_send_msg(sock, optUserName2, "%%2FRequestUserList");
398 return 0; 398 return 0;
399 } else 399 } else
400 return 1; 400 return 1;
401 } 401 }
402 402
408 (void) sock; 408 (void) sock;
409 409
410 if (!s) return 1; 410 if (!s) return 1;
411 *s = 0; 411 *s = 0;
412 412
413 p = doubleDecodeStr(str); 413 p = nn_dbldecode_str(str);
414 if (!p) return -1; 414 if (!p) return -1;
415 415
416 printMsg("! ½3½%s½0½ ½2½ADDED.½0½\n", p); 416 printMsg("! ½3½%s½0½ ½2½ADDED.½0½\n", p);
417 th_free(p); 417 th_free(p);
418 return 0; 418 return 0;
426 (void) sock; 426 (void) sock;
427 427
428 if (!s) return 1; 428 if (!s) return 1;
429 *s = 0; 429 *s = 0;
430 430
431 p = doubleDecodeStr(str); 431 p = nn_dbldecode_str(str);
432 if (!p) return -1; 432 if (!p) return -1;
433 433
434 printMsg("! ½3½%s½0½ ½1½DELETED.½0½\n", p); 434 printMsg("! ½3½%s½0½ ½1½DELETED.½0½\n", p);
435 th_free(p); 435 th_free(p);
436 return 0; 436 return 0;
505 printMsg("Invalid color value '%s'\n", buf+7); 505 printMsg("Invalid color value '%s'\n", buf+7);
506 return 1; 506 return 1;
507 } 507 }
508 optUserColor = tmpInt; 508 optUserColor = tmpInt;
509 printMsg("Setting color to #%06x\n", optUserColor); 509 printMsg("Setting color to #%06x\n", optUserColor);
510 sendUserMsg(sock, optUserName2, "%%2FSetFontColor%%20%%2Dcolor%%20%06X", optUserColor); 510 nn_send_msg(sock, optUserName2, "%%2FSetFontColor%%20%%2Dcolor%%20%06X", optUserColor);
511 return 0; 511 return 0;
512 } else if (!strncmp(buf, "/flood ", 7)) { 512 } else if (!strncmp(buf, "/flood ", 7)) {
513 int i; 513 int i;
514 514
515 snprintf(tmpBuf, sizeof(tmpBuf), "/prv -to %s -msg . .", 515 snprintf(tmpBuf, sizeof(tmpBuf), "/prv -to %s -msg . .",
516 buf+7); 516 buf+7);
517 517
518 tmpStr = doubleEncodeStr(tmpBuf); 518 tmpStr = nn_dblencode_str(tmpBuf);
519 if (!tmpStr) return -2; 519 if (!tmpStr) return -2;
520 520
521 result = TRUE; 521 result = TRUE;
522 for (i = 0; i < 50 && result; i++) { 522 for (i = 0; i < 50 && result; i++) {
523 result = sendUserMsg(sock, optUserName2, "%s", tmpStr); 523 result = nn_send_msg(sock, optUserName2, "%s", tmpStr);
524 usleep(250); 524 usleep(250);
525 } 525 }
526 526
527 th_free(tmpStr); 527 th_free(tmpStr);
528 return 0; 528 return 0;
545 } 545 }
546 } 546 }
547 547
548 548
549 /* Send double-encoded */ 549 /* Send double-encoded */
550 tmpStr = doubleEncodeStr(buf); 550 tmpStr = nn_dblencode_str(buf);
551 if (!tmpStr) return -2; 551 if (!tmpStr) return -2;
552 552
553 result = sendUserMsg(sock, optUserName2, "%s", tmpStr); 553 result = nn_send_msg(sock, optUserName2, "%s", tmpStr);
554 th_free(tmpStr); 554 th_free(tmpStr);
555 if (result) 555 if (result)
556 return 0; 556 return 0;
557 else 557 else
558 return -1; 558 return -1;
591 insertMode = TRUE; 591 insertMode = TRUE;
592 time_t prevTime; 592 time_t prevTime;
593 struct timeval socktv; 593 struct timeval socktv;
594 fd_set sockfds; 594 fd_set sockfds;
595 char *tmpStr; 595 char *tmpStr;
596 nn_editbuf_t *editBuf = newBuf(SET_BUFSIZE); 596 nn_editbuf_t *editBuf = nn_editbuf_new(SET_BUFSIZE);
597 nn_editbuf_t *histBuf[SET_MAX_HISTORY+2]; 597 nn_editbuf_t *histBuf[SET_MAX_HISTORY+2];
598 int histPos = 0, histMax = 0; 598 int histPos = 0, histMax = 0;
599 599
600 memset(histBuf, 0, sizeof(histBuf)); 600 memset(histBuf, 0, sizeof(histBuf));
601 601
627 THERR("Could not open logfile for appending!\n"); 627 THERR("Could not open logfile for appending!\n");
628 return -9; 628 return -9;
629 } 629 }
630 } 630 }
631 631
632 if (!initNetwork()) { 632 if (!nn_network_init()) {
633 THERR("Could not initialize network subsystem.\n"); 633 THERR("Could not initialize network subsystem.\n");
634 goto err_exit; 634 goto err_exit;
635 } else 635 } else
636 networkInit = TRUE; 636 networkInit = TRUE;
637 637
648 648
649 #if 1 649 #if 1
650 /* To emulate the official client, we first make a request for 650 /* To emulate the official client, we first make a request for
651 * policy file, even though we don't use it for anything... 651 * policy file, even though we don't use it for anything...
652 */ 652 */
653 if ((tmpSocket = openConnection((struct in_addr *) tmpHost->h_addr, 843)) < 0) { 653 if ((tmpSocket = nn_open_connection((struct in_addr *) tmpHost->h_addr, 843)) < 0) {
654 THERR("Policy file request connection setup failed!\n"); 654 THERR("Policy file request connection setup failed!\n");
655 goto err_exit; 655 goto err_exit;
656 } 656 }
657 657
658 tmpStr = "<policy-file-request/>"; 658 tmpStr = "<policy-file-request/>";
659 if (sendToSocket(tmpSocket, tmpStr, strlen(tmpStr) + 1) == FALSE) { 659 if (nn_send_to_socket(tmpSocket, tmpStr, strlen(tmpStr) + 1) == FALSE) {
660 THERR("Failed to send policy file request.\n"); 660 THERR("Failed to send policy file request.\n");
661 goto err_exit; 661 goto err_exit;
662 } else { 662 } else {
663 ssize_t gotBuf; 663 ssize_t gotBuf;
664 char tmpBuf[SET_BUFSIZE]; 664 char tmpBuf[SET_BUFSIZE];
665 gotBuf = recv(tmpSocket, tmpBuf, sizeof(tmpBuf), 0); 665 gotBuf = recv(tmpSocket, tmpBuf, sizeof(tmpBuf), 0);
666 tmpBuf[gotBuf-1] = 0; 666 tmpBuf[gotBuf-1] = 0;
667 THMSG(2, "Probe got: %s\n", tmpBuf); 667 THMSG(2, "Probe got: %s\n", tmpBuf);
668 closeConnection(tmpSocket); 668 nn_close_connection(tmpSocket);
669 } 669 }
670 #endif 670 #endif
671 671
672 /* Okay, now do the proper connection ... */ 672 /* Okay, now do the proper connection ... */
673 if ((tmpSocket = openConnection((struct in_addr *) tmpHost->h_addr, optPort)) < 0) { 673 if ((tmpSocket = nn_open_connection((struct in_addr *) tmpHost->h_addr, optPort)) < 0) {
674 THERR("Main connection setup failed!\n"); 674 THERR("Main connection setup failed!\n");
675 goto err_exit; 675 goto err_exit;
676 } 676 }
677 677
678 THMSG(1, "Connected, logging in as '%s', site '%s'.\n", optUserName, optSite); 678 THMSG(1, "Connected, logging in as '%s', site '%s'.\n", optUserName, optSite);
679 optUserName2 = doubleEncodeStr(optUserName); 679 optUserName2 = nn_dblencode_str(optUserName);
680 tmpStr = doubleEncodeStr(optSite); 680 tmpStr = nn_dblencode_str(optSite);
681 sendUserMsg(tmpSocket, optUserName2, "%%2Flogin%%20%%2Dsite%%20%s%%20%%2Dpassword%%20%s", tmpStr, optPassword); 681 nn_send_msg(tmpSocket, optUserName2, "%%2Flogin%%20%%2Dsite%%20%s%%20%%2Dpassword%%20%s", tmpStr, optPassword);
682 th_free(tmpStr); 682 th_free(tmpStr);
683 683
684 /* Initialize NCurses */ 684 /* Initialize NCurses */
685 if (!optDaemon) { 685 if (!optDaemon) {
686 if (LINES < 0 || LINES > 1000) LINES = 24; 686 if (LINES < 0 || LINES > 1000) LINES = 24;
718 cursesInit = TRUE; 718 cursesInit = TRUE;
719 719
720 if (!initializeWindows()) 720 if (!initializeWindows())
721 goto err_exit; 721 goto err_exit;
722 722
723 clearBuf(editBuf); 723 nn_editbuf_clear(editBuf);
724 printEditBuf("", editBuf); 724 printEditBuf("", editBuf);
725 updateStatus(insertMode); 725 updateStatus(insertMode);
726 } 726 }
727 727
728 /* Enter mainloop */ 728 /* Enter mainloop */
737 /* Check for incoming data from the server */ 737 /* Check for incoming data from the server */
738 socktv.tv_sec = 0; 738 socktv.tv_sec = 0;
739 socktv.tv_usec = SET_DELAY_USEC; 739 socktv.tv_usec = SET_DELAY_USEC;
740 tmpfds = sockfds; 740 tmpfds = sockfds;
741 if ((result = select(tmpSocket+1, &tmpfds, NULL, NULL, &socktv)) == -1) { 741 if ((result = select(tmpSocket+1, &tmpfds, NULL, NULL, &socktv)) == -1) {
742 int res = getSocketErrno(); 742 int res = nn_get_socket_errno();
743 if (res != EINTR) { 743 if (res != EINTR) {
744 errorMsg("Error occured in select(sockfds): %d, %s\n", 744 errorMsg("Error occured in select(sockfds): %d, %s\n",
745 res, getSocketErrStr(res)); 745 res, nn_get_socket_errstr(res));
746 isError = TRUE; 746 isError = TRUE;
747 } 747 }
748 } else if (FD_ISSET(tmpSocket, &tmpfds)) { 748 } else if (FD_ISSET(tmpSocket, &tmpfds)) {
749 ssize_t gotBuf; 749 ssize_t gotBuf;
750 char tmpBuf[8192]; 750 char tmpBuf[8192];
751 char *bufPtr = tmpBuf; 751 char *bufPtr = tmpBuf;
752 gotBuf = recv(tmpSocket, tmpBuf, sizeof(tmpBuf), 0); 752 gotBuf = recv(tmpSocket, tmpBuf, sizeof(tmpBuf), 0);
753 753
754 if (gotBuf < 0) { 754 if (gotBuf < 0) {
755 int res = getSocketErrno(); 755 int res = nn_get_socket_errno();
756 errorMsg("Error in recv: %d, %s\n", res, getSocketErrStr(res)); 756 errorMsg("Error in recv: %d, %s\n", res, nn_get_socket_errstr(res));
757 isError = TRUE; 757 isError = TRUE;
758 } else if (gotBuf == 0) { 758 } else if (gotBuf == 0) {
759 errorMsg("Server closed connection.\n"); 759 errorMsg("Server closed connection.\n");
760 isError = TRUE; 760 isError = TRUE;
761 } else { 761 } else {
822 case '\r': 822 case '\r':
823 /* Call the user input handler */ 823 /* Call the user input handler */
824 if (editBuf->len > 0) { 824 if (editBuf->len > 0) {
825 825
826 if (histMax > 0) { 826 if (histMax > 0) {
827 freeBuf(histBuf[SET_MAX_HISTORY+1]); 827 nn_editbuf_free(histBuf[SET_MAX_HISTORY+1]);
828 histBuf[SET_MAX_HISTORY+1] = NULL; 828 histBuf[SET_MAX_HISTORY+1] = NULL;
829 memmove(&histBuf[2], &histBuf[1], histMax * sizeof(histBuf[0])); 829 memmove(&histBuf[2], &histBuf[1], histMax * sizeof(histBuf[0]));
830 } 830 }
831 831
832 histPos = 0; 832 histPos = 0;
833 histBuf[1] = copyBuf(editBuf); 833 histBuf[1] = nn_editbuf_copy(editBuf);
834 if (histMax < SET_MAX_HISTORY) histMax++; 834 if (histMax < SET_MAX_HISTORY) histMax++;
835 835
836 insertBuf(editBuf, editBuf->len, 0); 836 nn_editbuf_insert(editBuf, editBuf->len, 0);
837 result = handleUserInput(tmpSocket, editBuf->data, editBuf->len); 837 result = handleUserInput(tmpSocket, editBuf->data, editBuf->len);
838 838
839 clearBuf(editBuf); 839 nn_editbuf_clear(editBuf);
840 840
841 if (result < 0) { 841 if (result < 0) {
842 errorMsg("Fatal error handling user input: %s\n", editBuf->data); 842 errorMsg("Fatal error handling user input: %s\n", editBuf->data);
843 isError = TRUE; 843 isError = TRUE;
844 } 844 }
857 update = TRUE; 857 update = TRUE;
858 break; 858 break;
859 859
860 case KEY_UP: /* Backwards in input history */ 860 case KEY_UP: /* Backwards in input history */
861 if (histPos == 0) { 861 if (histPos == 0) {
862 freeBuf(histBuf[0]); 862 nn_editbuf_free(histBuf[0]);
863 histBuf[0] = copyBuf(editBuf); 863 histBuf[0] = nn_editbuf_copy(editBuf);
864 } 864 }
865 if (histPos < histMax) { 865 if (histPos < histMax) {
866 histPos++; 866 histPos++;
867 freeBuf(editBuf); 867 nn_editbuf_free(editBuf);
868 editBuf = copyBuf(histBuf[histPos]); 868 editBuf = nn_editbuf_copy(histBuf[histPos]);
869 update = TRUE; 869 update = TRUE;
870 } 870 }
871 break; 871 break;
872 872
873 case KEY_DOWN: /* Forwards in input history */ 873 case KEY_DOWN: /* Forwards in input history */
874 if (histPos > 0) { 874 if (histPos > 0) {
875 histPos--; 875 histPos--;
876 freeBuf(editBuf); 876 nn_editbuf_free(editBuf);
877 editBuf = copyBuf(histBuf[histPos]); 877 editBuf = nn_editbuf_copy(histBuf[histPos]);
878 update = TRUE; 878 update = TRUE;
879 } 879 }
880 break; 880 break;
881 881
882 case 0x204: /* ctrl+left = Skip words left */ 882 case 0x204: /* ctrl+left = Skip words left */
906 insertMode = !insertMode; 906 insertMode = !insertMode;
907 update = TRUE; 907 update = TRUE;
908 break; 908 break;
909 909
910 case 0x10a: /* F2 = Clear editbuffer */ 910 case 0x10a: /* F2 = Clear editbuffer */
911 clearBuf(editBuf); 911 nn_editbuf_clear(editBuf);
912 update = TRUE; 912 update = TRUE;
913 break; 913 break;
914 914
915 case KEY_HOME: setBufPos(editBuf, 0); update = TRUE; break; 915 case KEY_HOME: nn_editbuf_setpos(editBuf, 0); update = TRUE; break;
916 case KEY_END: setBufPos(editBuf, editBuf->len); update = TRUE; break; 916 case KEY_END: nn_editbuf_setpos(editBuf, editBuf->len); update = TRUE; break;
917 case KEY_LEFT: setBufPos(editBuf, editBuf->pos - 1); update = TRUE; break; 917 case KEY_LEFT: nn_editbuf_setpos(editBuf, editBuf->pos - 1); update = TRUE; break;
918 case KEY_RIGHT: setBufPos(editBuf, editBuf->pos + 1); update = TRUE; break; 918 case KEY_RIGHT: nn_editbuf_setpos(editBuf, editBuf->pos + 1); update = TRUE; break;
919 919
920 case KEY_BACKSPACE: 920 case KEY_BACKSPACE:
921 case 0x08: 921 case 0x08:
922 deleteBuf(editBuf, editBuf->pos - 1); 922 nn_editbuf_delete(editBuf, editBuf->pos - 1);
923 setBufPos(editBuf, editBuf->pos - 1); 923 nn_editbuf_setpos(editBuf, editBuf->pos - 1);
924 update = TRUE; 924 update = TRUE;
925 break; 925 break;
926 926
927 case 0x14a: 927 case 0x14a:
928 /* Delete */ 928 /* Delete */
929 deleteBuf(editBuf, editBuf->pos); 929 nn_editbuf_delete(editBuf, editBuf->pos);
930 update = TRUE; 930 update = TRUE;
931 break; 931 break;
932 932
933 case 0x0c: 933 case 0x0c:
934 /* ctrl+l */ 934 /* ctrl+l */
942 break; 942 break;
943 943
944 default: 944 default:
945 if (isprint(c) || c == 0xe4 || c == 0xf6 || c == 0xc4 || c == 0xd6) { 945 if (isprint(c) || c == 0xe4 || c == 0xf6 || c == 0xc4 || c == 0xd6) {
946 if (insertMode) 946 if (insertMode)
947 insertBuf(editBuf, editBuf->pos, c); 947 nn_editbuf_insert(editBuf, editBuf->pos, c);
948 else 948 else
949 writeBuf(editBuf, editBuf->pos, c); 949 nn_editbuf_write(editBuf, editBuf->pos, c);
950 setBufPos(editBuf, editBuf->pos + 1); 950 nn_editbuf_setpos(editBuf, editBuf->pos + 1);
951 update = TRUE; 951 update = TRUE;
952 } else { 952 } else {
953 printMsg("Unhandled key: 0x%02x\n", c); 953 printMsg("Unhandled key: 0x%02x\n", c);
954 } 954 }
955 break; 955 break;
964 } /* !optDaemon */ 964 } /* !optDaemon */
965 965
966 if (++updateCount > 10) { 966 if (++updateCount > 10) {
967 time_t tmpTime = time(NULL); 967 time_t tmpTime = time(NULL);
968 if (tmpTime - prevTime > SET_KEEPALIVE) { 968 if (tmpTime - prevTime > SET_KEEPALIVE) {
969 sendUserMsg(tmpSocket, optUserName2, "/listallusers"); 969 nn_send_msg(tmpSocket, optUserName2, "/listallusers");
970 prevTime = tmpTime; 970 prevTime = tmpTime;
971 } 971 }
972 972
973 if (!colorSet) { 973 if (!colorSet) {
974 colorSet = TRUE; 974 colorSet = TRUE;
975 printMsg("%s v%s - %s\n", th_prog_name, th_prog_version, th_prog_fullname); 975 printMsg("%s v%s - %s\n", th_prog_name, th_prog_version, th_prog_fullname);
976 printMsg("%s\n", th_prog_author); 976 printMsg("%s\n", th_prog_author);
977 printMsg("%s\n", th_prog_license); 977 printMsg("%s\n", th_prog_license);
978 sendUserMsg(tmpSocket, optUserName2, "%%2FSetFontColor%%20%%2Dcolor%%20%06X", optUserColor); 978 nn_send_msg(tmpSocket, optUserName2, "%%2FSetFontColor%%20%%2Dcolor%%20%06X", optUserColor);
979 } 979 }
980 980
981 updateStatus(insertMode); 981 updateStatus(insertMode);
982 updateCount = 0; 982 updateCount = 0;
983 } 983 }
984 984
985 } 985 }
986 986
987 /* Shutdown */ 987 /* Shutdown */
988 err_exit: 988 err_exit:
989 freeBuf(editBuf); 989 nn_editbuf_free(editBuf);
990 for (histPos = 0; histPos <= SET_MAX_HISTORY; histPos++) 990 for (histPos = 0; histPos <= SET_MAX_HISTORY; histPos++)
991 freeBuf(histBuf[histPos]); 991 nn_editbuf_free(histBuf[histPos]);
992 992
993 if (cursesInit) { 993 if (cursesInit) {
994 if (curVis != ERR) 994 if (curVis != ERR)
995 curs_set(curVis); 995 curs_set(curVis);
996 endwin(); 996 endwin();
1001 THMSG(1, "Error exit.\n"); 1001 THMSG(1, "Error exit.\n");
1002 } 1002 }
1003 1003
1004 th_free(optUserName2); 1004 th_free(optUserName2);
1005 1005
1006 closeConnection(tmpSocket); 1006 nn_close_connection(tmpSocket);
1007 1007
1008 if (networkInit) 1008 if (networkInit)
1009 closeNetwork(); 1009 nn_network_close();
1010 1010
1011 THMSG(1, "Connection terminated.\n"); 1011 THMSG(1, "Connection terminated.\n");
1012 1012
1013 if (optLogFile) { 1013 if (optLogFile) {
1014 THMSG(1, "Closing logfile.\n"); 1014 THMSG(1, "Closing logfile.\n");