comparison nnchat.c @ 322:b9c15c57dc8f

Clean up message functions, add new printMsgQ() helper function for messages that should not go into the log file. Add skeleton help function, accessible via F1 key. And other cleanups.
author Matti Hamalainen <ccr@tnsp.org>
date Sat, 11 Jun 2011 09:48:26 +0300
parents 384d508d1df3
children 863e3a26974d
comparison
equal deleted inserted replaced
321:384d508d1df3 322:b9c15c57dc8f
309 snprintf(tmpStr, sizeof(tmpStr), "#%06x", optUserColor); 309 snprintf(tmpStr, sizeof(tmpStr), "#%06x", optUserColor);
310 waddstr(statusWin, tmpStr); 310 waddstr(statusWin, tmpStr);
311 311
312 wattrset(statusWin, A_BOLD | COLOR_PAIR(13)); 312 wattrset(statusWin, A_BOLD | COLOR_PAIR(13));
313 waddstr(statusWin, " | WIN: "); 313 waddstr(statusWin, " | WIN: ");
314 snprintf(tmpStr, sizeof(tmpStr), "%d: %s", currWin->num + 1, currWin->id ? currWin->id : "MAIN"); 314 snprintf(tmpStr, sizeof(tmpStr), "%d: %s / %d",
315 currWin->num + 1,
316 currWin->id != NULL ? currWin->id : "MAIN",
317 currWin->pos);
315 waddstr(statusWin, tmpStr); 318 waddstr(statusWin, tmpStr);
316 319
317 wattrset(statusWin, A_BOLD | COLOR_PAIR(13)); 320 wattrset(statusWin, A_BOLD | COLOR_PAIR(13));
318 waddstr(statusWin, " | "); 321 waddstr(statusWin, " | ");
319 wattrset(statusWin, A_BOLD | COLOR_PAIR(11)); 322 wattrset(statusWin, A_BOLD | COLOR_PAIR(11));
485 488
486 if (!optDaemon && (flags & LOG_WINDOW)) { 489 if (!optDaemon && (flags & LOG_WINDOW)) {
487 nn_window_t *tmp = win != NULL ? win : chatWindows[0]; 490 nn_window_t *tmp = win != NULL ? win : chatWindows[0];
488 if (flags & LOG_STAMP) nn_window_print(tmp, tmpStr); 491 if (flags & LOG_STAMP) nn_window_print(tmp, tmpStr);
489 nn_window_print(tmp, buf); 492 nn_window_print(tmp, buf);
490 updateMainWin(FALSE);
491 } 493 }
492 494
493 th_free(buf); 495 th_free(buf);
494 } 496 }
495 497
500 va_start(ap, fmt); 502 va_start(ap, fmt);
501 printMsgV(win, LOG_STAMP | LOG_WINDOW | LOG_FILE, fmt, ap); 503 printMsgV(win, LOG_STAMP | LOG_WINDOW | LOG_FILE, fmt, ap);
502 va_end(ap); 504 va_end(ap);
503 } 505 }
504 506
505 void printMsgC(nn_window_t *win, const char *fmt, ...) 507 void printMsgF(nn_window_t *win, int flags, const char *fmt, ...)
506 { 508 {
507 va_list ap; 509 va_list ap;
508 510
509 va_start(ap, fmt); 511 va_start(ap, fmt);
510 printMsgV(win, LOG_WINDOW | LOG_FILE, fmt, ap); 512 printMsgV(win, flags | LOG_STAMP, fmt, ap);
511 va_end(ap); 513 va_end(ap);
512 } 514 }
513 515
514 void printMsgQ(nn_window_t *win, BOOL logOnly, const char *fmt, ...) 516 void printMsgQ(nn_window_t *win, const char *fmt, ...)
515 { 517 {
516 va_list ap; 518 va_list ap;
517 519
518 va_start(ap, fmt); 520 va_start(ap, fmt);
519 printMsgV(win, logOnly ? (LOG_STAMP | LOG_FILE) : (LOG_STAMP | LOG_WINDOW | LOG_FILE), fmt, ap); 521 printMsgV(win, LOG_STAMP | LOG_WINDOW, fmt, ap);
520 va_end(ap); 522 va_end(ap);
521 } 523 }
522 524
523 525
524 char *errorMessages = NULL; 526 char *errorMessages = NULL;
640 *tmp = 0; 642 *tmp = 0;
641 643
642 isIgnored = setIgnoreMode && checkIgnoreList(name); 644 isIgnored = setIgnoreMode && checkIgnoreList(name);
643 win = nn_find_window(name); 645 win = nn_find_window(name);
644 646
645 if (win != NULL) 647 if (win != NULL) {
646 printMsgQ(win, isIgnored, "½5½<½%d½%s½5½>½0½ %s\n", isMine ? 14 : 15, isMine ? optUserName : name, msg); 648 printMsgF(win, isIgnored ? LOG_FILE : (LOG_WINDOW | LOG_FILE),
647 else 649 "½5½<½%d½%s½5½>½0½ %s\n",
648 printMsgQ(NULL, isIgnored, "½11½%s½0½\n", h); 650 isMine ? 14 : 15, isMine ? optUserName : name, msg);
649 651 } else {
652 printMsgF(NULL, isIgnored ? LOG_FILE : (LOG_WINDOW | LOG_FILE),
653 "½11½%s½0½\n", h);
654 }
650 th_free(name); 655 th_free(name);
651 th_free(h); 656 th_free(h);
652 } else { 657 } else {
653 /* It's an action (/me) */ 658 /* It's an action (/me) */
654 char *h = nn_decode_str2(t); 659 char *h = nn_decode_str2(t);
655 printMsgQ(NULL, isIgnored, "½9½* %s½0½\n", h); 660 printMsgF(NULL, isIgnored ? LOG_FILE : (LOG_WINDOW | LOG_FILE),
661 "½9½* %s½0½\n", h);
656 th_free(h); 662 th_free(h);
657 } 663 }
658 th_free(t); 664 th_free(t);
659 } else { 665 } else {
660 /* It's a normal message */ 666 /* It's a normal message */
661 char *h; 667 char *h;
662 t = nn_strip_tags(s); 668 t = nn_strip_tags(s);
663 h = nn_decode_str2(t); 669 h = nn_decode_str2(t);
664 printMsgQ(NULL, isIgnored, "½5½<½%d½%s½5½>½0½ %s\n", isMine ? 14 : 15, userName, h); 670 printMsgF(NULL, isIgnored ? LOG_FILE : (LOG_WINDOW | LOG_FILE),
671 "½5½<½%d½%s½5½>½0½ %s\n", isMine ? 14 : 15, userName, h);
665 th_free(h); 672 th_free(h);
666 th_free(t); 673 th_free(t);
667 } 674 }
668 675
669 done: 676 done:
831 } 838 }
832 else if (!strncasecmp(buf, "/color ", 7)) { 839 else if (!strncasecmp(buf, "/color ", 7)) {
833 /* Change color */ 840 /* Change color */
834 int tmpInt; 841 int tmpInt;
835 if ((tmpInt = th_get_hex_triplet(trimLeft(buf + 7))) < 0) { 842 if ((tmpInt = th_get_hex_triplet(trimLeft(buf + 7))) < 0) {
836 printMsg(currWin, "Invalid color value '%s'\n", buf+7); 843 printMsgQ(currWin, "Invalid color value '%s'\n", buf+7);
837 return 1; 844 return 1;
838 } 845 }
839 optUserColor = tmpInt; 846 optUserColor = tmpInt;
840 printMsg(currWin, "Setting color to #%06x\n", optUserColor); 847 printMsgQ(currWin, "Setting color to #%06x\n", optUserColor);
841 nn_conn_send_msg(conn, optUserNameEnc, "%%2FSetFontColor%%20%%2Dcolor%%20%06X", optUserColor); 848 nn_conn_send_msg(conn, optUserNameEnc, "%%2FSetFontColor%%20%%2Dcolor%%20%06X", optUserColor);
842 return 0; 849 return 0;
843 } 850 }
844 else if (!strncasecmp(buf, "/ignore", 7)) { 851 else if (!strncasecmp(buf, "/ignore", 7)) {
845 char *name = trimLeft(buf + 7); 852 char *name = trimLeft(buf + 7);
846 if (strlen(name) > 0) { 853 if (strlen(name) > 0) {
847 /* Add or remove someone to/from ignore */ 854 /* Add or remove someone to/from ignore */
848 qlist_t *user = th_llist_find_func(setIgnoreList, name, compareUsername); 855 qlist_t *user = th_llist_find_func(setIgnoreList, name, compareUsername);
849 if (user != NULL) { 856 if (user != NULL) {
850 printMsg(currWin, "Removed user '%s' from ignore.\n", name); 857 printMsgQ(currWin, "Removed user '%s' from ignore.\n", name);
851 th_llist_delete_node(&setIgnoreList, user); 858 th_llist_delete_node(&setIgnoreList, user);
852 } else { 859 } else {
853 printMsg(currWin, "Now ignoring '%s'.\n", name); 860 printMsgQ(currWin, "Now ignoring '%s'.\n", name);
854 th_llist_append(&setIgnoreList, th_strdup(name)); 861 th_llist_append(&setIgnoreList, th_strdup(name));
855 } 862 }
856 } else { 863 } else {
857 /* Just list whomever is in ignore now */ 864 /* Just list whomever is in ignore now */
858 qlist_t *user = setIgnoreList; 865 qlist_t *user = setIgnoreList;
864 if (--nuser > 0) 871 if (--nuser > 0)
865 th_pstr_printf(&result, "%s, ", result); 872 th_pstr_printf(&result, "%s, ", result);
866 } 873 }
867 user = user->next; 874 user = user->next;
868 } 875 }
869 printMsg(currWin, "%s\n", result); 876 printMsgQ(currWin, "%s\n", result);
870 th_free(result); 877 th_free(result);
871 } 878 }
872 return 0; 879 return 0;
873 } 880 }
874 else if (!strncasecmp(buf, "/query", 6)) { 881 else if (!strncasecmp(buf, "/query", 6)) {
875 char *name = trimLeft(buf + 6); 882 char *name = trimLeft(buf + 6);
876 if (strlen(name) > 0) { 883 if (strlen(name) > 0) {
877 nn_user_t *user = nn_user_find(nnUsers, nn_username_encode(name)); 884 nn_user_t *user = nn_user_find(nnUsers, nn_username_encode(name));
878 if (user != NULL) { 885 if (user != NULL) {
879 name = nn_username_decode(th_strdup(user->name)); 886 name = nn_username_decode(th_strdup(user->name));
880 printMsg(currWin, "Opening PRV query for '%s'.\n", name); 887 printMsgQ(currWin, "Opening PRV query for '%s'.\n", name);
881 if (openWindow(name, TRUE)) 888 if (openWindow(name, TRUE))
882 printMsg(currWin, "In PRV query with '%s'.\n", name); 889 printMsgQ(currWin, "In PRV query with '%s'.\n", name);
883 th_free(name); 890 th_free(name);
884 } 891 }
885 } else { 892 } else {
886 printMsg(currWin, "Usage: /query username\n"); 893 printMsgQ(currWin, "Usage: /query username\n");
887 printMsg(currWin, "To close a PRV query, use /close [username]\n"); 894 printMsgQ(currWin, "To close a PRV query, use /close [username]\n");
888 printMsg(currWin, "/close without username will close the current PRV window.\n"); 895 printMsgQ(currWin, "/close without username will close the current PRV window.\n");
896 }
897 return 0;
898 }
899 else if (!strncasecmp(buf, "/win", 4)) {
900 /* Change color */
901 char *tmp = trimLeft(buf + 4);
902 if (strlen(tmp) > 0) {
903 int val = atoi(tmp);
904 if (val >= 1 && val < SET_MAX_WINDOWS) {
905 if (chatWindows[val - 1] != NULL)
906 currWin = chatWindows[val - 1];
907 } else {
908 printMsgQ(currWin, "Invalid window number '%s'\n", tmp);
909 return 1;
910 }
911 } else {
912 printMsgQ(currWin, "Window : #%d\n", currWin->num);
913 printMsgQ(currWin, "ID : %s\n", currWin->id);
889 } 914 }
890 return 0; 915 return 0;
891 } 916 }
892 else if (!strncasecmp(buf, "/close", 6)) { 917 else if (!strncasecmp(buf, "/close", 6)) {
893 char *name = trimLeft(buf + 6); 918 char *name = trimLeft(buf + 6);
894 if (strlen(name) > 0) { 919 if (strlen(name) > 0) {
895 nn_window_t *win; 920 nn_window_t *win;
896 win = nn_find_window(name); 921 win = nn_find_window(name);
897 if (win != NULL) { 922 if (win != NULL) {
898 closeWindow(win); 923 closeWindow(win);
899 printMsg(currWin, "Closed PRV query to '%s'.\n", name); 924 printMsgQ(currWin, "Closed PRV query to '%s'.\n", name);
900 } else { 925 } else {
901 printMsg(currWin, "No PRV query by name '%s'.\n", name); 926 printMsgQ(currWin, "No PRV query by name '%s'.\n", name);
902 } 927 }
903 } else { 928 } else {
904 if (currWin != chatWindows[0]) { 929 if (currWin != chatWindows[0]) {
905 closeWindow(currWin); 930 closeWindow(currWin);
906 currWin = chatWindows[0]; 931 currWin = chatWindows[0];
910 } 935 }
911 else if (!strncasecmp(buf, "/save", 5)) { 936 else if (!strncasecmp(buf, "/save", 5)) {
912 /* Save configuration */ 937 /* Save configuration */
913 FILE *cfgfile = fopen(setConfigFile, "w"); 938 FILE *cfgfile = fopen(setConfigFile, "w");
914 if (cfgfile == NULL) { 939 if (cfgfile == NULL) {
915 printMsg(currWin, "Could not create configuration to file '%s': %s\n", 940 printMsgQ(currWin, "Could not create configuration to file '%s': %s\n",
916 setConfigFile, strerror(errno)); 941 setConfigFile, strerror(errno));
917 return 0; 942 return 0;
918 } 943 }
919 printMsg(currWin, "Configuration saved in file '%s', res=%d\n", 944 printMsgQ(currWin, "Configuration saved in file '%s', res=%d\n",
920 setConfigFile, 945 setConfigFile,
921 th_cfg_write(cfgfile, setConfigFile, cfg)); 946 th_cfg_write(cfgfile, setConfigFile, cfg));
922 947
923 fclose(cfgfile); 948 fclose(cfgfile);
924 return 0; 949 return 0;
935 HINSTANCE status; 960 HINSTANCE status;
936 snprintf(tmpBuf, sizeof(tmpBuf), "http://www.newbienudes.com/profile/%s/", tmpStr); 961 snprintf(tmpBuf, sizeof(tmpBuf), "http://www.newbienudes.com/profile/%s/", tmpStr);
937 th_free(tmpStr); 962 th_free(tmpStr);
938 status = ShellExecute(NULL, "open", tmpBuf, NULL, NULL, SW_SHOWNA); 963 status = ShellExecute(NULL, "open", tmpBuf, NULL, NULL, SW_SHOWNA);
939 if (status <= 32) 964 if (status <= 32)
940 printMsg(currWin, "Could not launch default web browser: %d\n", status); 965 printMsgQ(currWin, "Could not launch default web browser: %d\n", status);
941 } 966 }
942 #else 967 #else
943 { 968 {
944 int status; 969 int status;
945 int fds[2]; 970 int fds[2];
947 snprintf(tmpBuf, sizeof(tmpBuf), "openurl(http://www.newbienudes.com/profile/%s/,new-tab)", tmpStr); 972 snprintf(tmpBuf, sizeof(tmpBuf), "openurl(http://www.newbienudes.com/profile/%s/,new-tab)", tmpStr);
948 th_free(tmpStr); 973 th_free(tmpStr);
949 974
950 if (pipe(fds) == -1) { 975 if (pipe(fds) == -1) {
951 int ret = errno; 976 int ret = errno;
952 printMsg(currWin, "Could not open process communication pipe! (%d, %s)\n", ret, strerror(ret)); 977 printMsgQ(currWin, "Could not open process communication pipe! (%d, %s)\n", ret, strerror(ret));
953 return 0; 978 return 0;
954 } 979 }
955 980
956 if ((pid = fork()) < 0) { 981 if ((pid = fork()) < 0) {
957 printMsg(currWin, "Could not create sub-process!\n"); 982 printMsgQ(currWin, "Could not create sub-process!\n");
958 } else if (pid == 0) { 983 } else if (pid == 0) {
959 dup2(fds[1], STDOUT_FILENO); 984 dup2(fds[1], STDOUT_FILENO);
960 dup2(fds[0], STDERR_FILENO); 985 dup2(fds[0], STDERR_FILENO);
961 execlp(setBrowser, setBrowser, "-remote", tmpBuf, (void *)NULL); 986 execlp(setBrowser, setBrowser, "-remote", tmpBuf, (void *)NULL);
962 _exit(errno); 987 _exit(errno);
975 else if (currWin != chatWindows[0]) { 1000 else if (currWin != chatWindows[0]) {
976 if (currWin->id != NULL) { 1001 if (currWin->id != NULL) {
977 snprintf(tmpBuf, sizeof(tmpBuf), "/prv -to %s -msg %s", currWin->id, buf); 1002 snprintf(tmpBuf, sizeof(tmpBuf), "/prv -to %s -msg %s", currWin->id, buf);
978 buf = tmpBuf; 1003 buf = tmpBuf;
979 } else { 1004 } else {
980 printMsg(NULL, "No target set, exiting prv mode.\n"); 1005 printMsgQ(NULL, "No target set, exiting prv mode.\n");
981 return 1; 1006 return 1;
982 } 1007 }
983 } 1008 }
984 1009
985 /* Send double-encoded */ 1010 /* Send double-encoded */
1224 1249
1225 if (allowEmpty || strlen(ptr) > 0) 1250 if (allowEmpty || strlen(ptr) > 0)
1226 return th_strdup(ptr); 1251 return th_strdup(ptr);
1227 else 1252 else
1228 return NULL; 1253 return NULL;
1254 }
1255
1256 void printHelp(void)
1257 {
1258 printMsgQ(currWin,
1259 "NNChat Help\n"
1260 "===========\n"
1261 "\n"
1262 "F1 This help.\n"
1263 );
1229 } 1264 }
1230 1265
1231 int main(int argc, char *argv[]) 1266 int main(int argc, char *argv[])
1232 { 1267 {
1233 nn_conn_t *conn = NULL; 1268 nn_conn_t *conn = NULL;
1572 1607
1573 if (!initializeWindows()) { 1608 if (!initializeWindows()) {
1574 errorMsg("Error resizing curses chatWindows\n"); 1609 errorMsg("Error resizing curses chatWindows\n");
1575 isError = TRUE; 1610 isError = TRUE;
1576 } 1611 }
1577
1578 update = updateMain = TRUE; 1612 update = updateMain = TRUE;
1579 break; 1613 break;
1580 #endif 1614 #endif
1581 1615
1582 case KEY_ENTER: 1616 case KEY_ENTER:
1607 } else { 1641 } else {
1608 /* Update time value of last sent message for unidle timeouts */ 1642 /* Update time value of last sent message for unidle timeouts */
1609 prevTime = time(NULL); 1643 prevTime = time(NULL);
1610 } 1644 }
1611 1645
1612 update = TRUE; 1646 updateMain = update = TRUE;
1613 } 1647 }
1614 break; 1648 break;
1615 1649
1616 case KEY_UP: /* Backwards in input history */ 1650 case KEY_UP: /* Backwards in input history */
1617 if (histPos == 0) { 1651 if (histPos == 0) {
1675 case KEY_IC: /* Ins = Toggle insert / overwrite mode */ 1709 case KEY_IC: /* Ins = Toggle insert / overwrite mode */
1676 insertMode = !insertMode; 1710 insertMode = !insertMode;
1677 update = TRUE; 1711 update = TRUE;
1678 break; 1712 break;
1679 1713
1714 case KEY_F(1): /* F1 = Print help */
1715 printHelp();
1716 updateMain = TRUE;
1717 break;
1718
1680 case KEY_F(2): /* F2 = Clear editbuffer */ 1719 case KEY_F(2): /* F2 = Clear editbuffer */
1681 nn_editbuf_clear(editBuf); 1720 nn_editbuf_clear(editBuf);
1682 update = TRUE; 1721 update = TRUE;
1683 break; 1722 break;
1684 1723