changeset 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
files nnchat.c
diffstat 1 files changed, 72 insertions(+), 33 deletions(-) [+]
line wrap: on
line diff
--- a/nnchat.c	Sat Jun 11 07:39:23 2011 +0300
+++ b/nnchat.c	Sat Jun 11 09:48:26 2011 +0300
@@ -311,7 +311,10 @@
 
     wattrset(statusWin, A_BOLD | COLOR_PAIR(13));
     waddstr(statusWin, " | WIN: ");
-    snprintf(tmpStr, sizeof(tmpStr), "%d: %s", currWin->num + 1, currWin->id ? currWin->id : "MAIN");
+    snprintf(tmpStr, sizeof(tmpStr), "%d: %s / %d",
+        currWin->num + 1,
+        currWin->id != NULL ? currWin->id : "MAIN",
+        currWin->pos);
     waddstr(statusWin, tmpStr);
 
     wattrset(statusWin, A_BOLD | COLOR_PAIR(13));
@@ -487,7 +490,6 @@
         nn_window_t *tmp = win != NULL ? win : chatWindows[0];
         if (flags & LOG_STAMP) nn_window_print(tmp, tmpStr);
         nn_window_print(tmp, buf);
-        updateMainWin(FALSE);
     }
     
     th_free(buf);
@@ -502,21 +504,21 @@
     va_end(ap);
 }
 
-void printMsgC(nn_window_t *win, const char *fmt, ...)
+void printMsgF(nn_window_t *win, int flags, const char *fmt, ...)
 {
     va_list ap;
     
     va_start(ap, fmt);
-    printMsgV(win, LOG_WINDOW | LOG_FILE, fmt, ap);
+    printMsgV(win, flags | LOG_STAMP, fmt, ap);
     va_end(ap);
 }
 
-void printMsgQ(nn_window_t *win, BOOL logOnly, const char *fmt, ...)
+void printMsgQ(nn_window_t *win, const char *fmt, ...)
 {
     va_list ap;
     
     va_start(ap, fmt);
-    printMsgV(win, logOnly ? (LOG_STAMP | LOG_FILE) : (LOG_STAMP | LOG_WINDOW | LOG_FILE), fmt, ap);
+    printMsgV(win, LOG_STAMP | LOG_WINDOW, fmt, ap);
     va_end(ap);
 }
 
@@ -642,17 +644,21 @@
             isIgnored = setIgnoreMode && checkIgnoreList(name);
             win = nn_find_window(name);
             
-            if (win != NULL)
-                printMsgQ(win, isIgnored, "5<%d%s5>0 %s\n", isMine ? 14 : 15, isMine ? optUserName : name, msg);
-            else
-                printMsgQ(NULL, isIgnored, "11%s0\n", h);
-            
+            if (win != NULL) {
+                printMsgF(win, isIgnored ? LOG_FILE : (LOG_WINDOW | LOG_FILE),
+                    "5<%d%s5>0 %s\n",
+                    isMine ? 14 : 15, isMine ? optUserName : name, msg);
+            } else {
+                printMsgF(NULL, isIgnored ? LOG_FILE : (LOG_WINDOW | LOG_FILE),
+                    "11%s0\n", h);
+            }
             th_free(name);
             th_free(h);
         } else {
             /* It's an action (/me) */
             char *h = nn_decode_str2(t);
-            printMsgQ(NULL, isIgnored, "9* %s0\n", h);
+            printMsgF(NULL, isIgnored ? LOG_FILE : (LOG_WINDOW | LOG_FILE),
+                "9* %s0\n", h);
             th_free(h);
         }
         th_free(t);
@@ -661,7 +667,8 @@
         char *h;
         t = nn_strip_tags(s);
         h = nn_decode_str2(t);
-        printMsgQ(NULL, isIgnored, "5<%d%s5>0 %s\n", isMine ? 14 : 15, userName, h);
+        printMsgF(NULL, isIgnored ? LOG_FILE : (LOG_WINDOW | LOG_FILE),
+            "5<%d%s5>0 %s\n", isMine ? 14 : 15, userName, h);
         th_free(h);
         th_free(t);
     }
@@ -833,11 +840,11 @@
         /* Change color */
         int tmpInt;
         if ((tmpInt = th_get_hex_triplet(trimLeft(buf + 7))) < 0) {
-            printMsg(currWin, "Invalid color value '%s'\n", buf+7);
+            printMsgQ(currWin, "Invalid color value '%s'\n", buf+7);
             return 1;
         }
         optUserColor = tmpInt;
-        printMsg(currWin, "Setting color to #%06x\n", optUserColor);
+        printMsgQ(currWin, "Setting color to #%06x\n", optUserColor);
         nn_conn_send_msg(conn, optUserNameEnc, "%%2FSetFontColor%%20%%2Dcolor%%20%06X", optUserColor);
         return 0;
     }
@@ -847,10 +854,10 @@
             /* Add or remove someone to/from ignore */
             qlist_t *user = th_llist_find_func(setIgnoreList, name, compareUsername);
             if (user != NULL) {
-                printMsg(currWin, "Removed user '%s' from ignore.\n", name);
+                printMsgQ(currWin, "Removed user '%s' from ignore.\n", name);
                 th_llist_delete_node(&setIgnoreList, user);
             } else {
-                printMsg(currWin, "Now ignoring '%s'.\n", name);
+                printMsgQ(currWin, "Now ignoring '%s'.\n", name);
                 th_llist_append(&setIgnoreList, th_strdup(name));
             }
         } else {
@@ -866,7 +873,7 @@
                 }
                 user = user->next;
             }
-            printMsg(currWin, "%s\n", result);
+            printMsgQ(currWin, "%s\n", result);
             th_free(result);
         }
         return 0;
@@ -877,15 +884,33 @@
             nn_user_t *user = nn_user_find(nnUsers, nn_username_encode(name));
             if (user != NULL) {
                 name = nn_username_decode(th_strdup(user->name));
-                printMsg(currWin, "Opening PRV query for '%s'.\n", name);
+                printMsgQ(currWin, "Opening PRV query for '%s'.\n", name);
                 if (openWindow(name, TRUE))
-                    printMsg(currWin, "In PRV query with '%s'.\n", name);
+                    printMsgQ(currWin, "In PRV query with '%s'.\n", name);
                 th_free(name);
             }
         } else {
-            printMsg(currWin, "Usage: /query username\n");
-            printMsg(currWin, "To close a PRV query, use /close [username]\n");
-            printMsg(currWin, "/close without username will close the current PRV window.\n");
+            printMsgQ(currWin, "Usage: /query username\n");
+            printMsgQ(currWin, "To close a PRV query, use /close [username]\n");
+            printMsgQ(currWin, "/close without username will close the current PRV window.\n");
+        }
+        return 0;
+    }
+    else if (!strncasecmp(buf, "/win", 4)) {
+        /* Change color */
+        char *tmp = trimLeft(buf + 4);
+        if (strlen(tmp) > 0) {
+            int val = atoi(tmp);
+            if (val >= 1 && val < SET_MAX_WINDOWS) {
+                if (chatWindows[val - 1] != NULL)
+                    currWin = chatWindows[val - 1];
+            } else {
+                printMsgQ(currWin, "Invalid window number '%s'\n", tmp);
+                return 1;
+            }
+        } else {
+            printMsgQ(currWin, "Window   : #%d\n", currWin->num);
+            printMsgQ(currWin, "ID       : %s\n", currWin->id);
         }
         return 0;
     }
@@ -896,9 +921,9 @@
             win = nn_find_window(name);
             if (win != NULL) {
                 closeWindow(win);
-                printMsg(currWin, "Closed PRV query to '%s'.\n", name);
+                printMsgQ(currWin, "Closed PRV query to '%s'.\n", name);
             } else {
-                printMsg(currWin, "No PRV query by name '%s'.\n", name);
+                printMsgQ(currWin, "No PRV query by name '%s'.\n", name);
             }
         } else {
             if (currWin != chatWindows[0]) {
@@ -912,11 +937,11 @@
         /* Save configuration */
         FILE *cfgfile = fopen(setConfigFile, "w");
         if (cfgfile == NULL) {
-            printMsg(currWin, "Could not create configuration to file '%s': %s\n",
+            printMsgQ(currWin, "Could not create configuration to file '%s': %s\n",
                 setConfigFile, strerror(errno));
             return 0;
         }
-        printMsg(currWin, "Configuration saved in file '%s', res=%d\n",
+        printMsgQ(currWin, "Configuration saved in file '%s', res=%d\n",
             setConfigFile,
             th_cfg_write(cfgfile, setConfigFile, cfg));
 
@@ -937,7 +962,7 @@
         th_free(tmpStr);
         status = ShellExecute(NULL, "open", tmpBuf, NULL, NULL, SW_SHOWNA);
         if (status <= 32)
-            printMsg(currWin, "Could not launch default web browser: %d\n", status);
+            printMsgQ(currWin, "Could not launch default web browser: %d\n", status);
         }
 #else
         {
@@ -949,12 +974,12 @@
 
         if (pipe(fds) == -1) {
             int ret = errno;
-            printMsg(currWin, "Could not open process communication pipe! (%d, %s)\n", ret, strerror(ret));
+            printMsgQ(currWin, "Could not open process communication pipe! (%d, %s)\n", ret, strerror(ret));
             return 0;
         }
 
         if ((pid = fork()) < 0) {
-            printMsg(currWin, "Could not create sub-process!\n");
+            printMsgQ(currWin, "Could not create sub-process!\n");
         } else if (pid == 0) {
             dup2(fds[1], STDOUT_FILENO);
             dup2(fds[0], STDERR_FILENO);
@@ -977,7 +1002,7 @@
             snprintf(tmpBuf, sizeof(tmpBuf), "/prv -to %s -msg %s", currWin->id, buf);
             buf = tmpBuf;
         } else {
-            printMsg(NULL, "No target set, exiting prv mode.\n");
+            printMsgQ(NULL, "No target set, exiting prv mode.\n");
             return 1;
         }
     }
@@ -1228,6 +1253,16 @@
         return NULL;
 }
 
+void printHelp(void)
+{
+    printMsgQ(currWin,
+    "NNChat Help\n"
+    "===========\n"
+    "\n"
+    "F1                This help.\n"
+    );
+}
+
 int main(int argc, char *argv[])
 {
     nn_conn_t *conn = NULL;
@@ -1574,7 +1609,6 @@
                     errorMsg("Error resizing curses chatWindows\n");
                     isError = TRUE;
                 }
-
                 update = updateMain = TRUE;
                 break;
 #endif
@@ -1609,7 +1643,7 @@
                         prevTime = time(NULL);
                     }
                     
-                    update = TRUE;
+                    updateMain = update = TRUE;
                 }
                 break;
             
@@ -1677,6 +1711,11 @@
                 update = TRUE;
                 break;
             
+            case KEY_F(1): /* F1 = Print help */
+                printHelp();
+                updateMain = TRUE;
+                break;
+
             case KEY_F(2): /* F2 = Clear editbuffer */
                 nn_editbuf_clear(editBuf);
                 update = TRUE;