# HG changeset patch # User Matti Hamalainen # Date 1307094526 -10800 # Node ID d04ea4395e9e6b6f5988c5c6ec00b2686a8113a2 # Parent 5175ed15ffa4578204fc88ce94aa26b40ac2ec83 Move username and password prompting into the Curses interface, also move Curses initialization to earlier phase. diff -r 5175ed15ffa4 -r d04ea4395e9e nnchat.c --- a/nnchat.c Fri Jun 03 11:47:11 2011 +0300 +++ b/nnchat.c Fri Jun 03 12:48:46 2011 +0300 @@ -344,8 +344,13 @@ return 0; } +enum { + LOG_FILE = 1, + LOG_WINDOW = 2, + LOG_STAMP = 4 +}; -void printMsgV(BOOL addStamp, BOOL logOnly, const char *fmt, va_list ap) +void printMsgV(int flags, const char *fmt, va_list ap) { char tmpStr[128], buf[8192]; @@ -353,14 +358,14 @@ vsnprintf(buf, sizeof(buf), fmt, ap); - if (optLogFile) { - if (addStamp) printFile(optLogFile, tmpStr); + if (optLogFile && (flags & LOG_FILE)) { + if (flags & LOG_STAMP) printFile(optLogFile, tmpStr); printFile(optLogFile, buf); fflush(optLogFile); } - if (!optDaemon && !logOnly) { - if (addStamp) printWin(mainWin, tmpStr); + if (!optDaemon && (flags & LOG_WINDOW)) { + if (flags & LOG_STAMP) printWin(mainWin, tmpStr); printWin(mainWin, buf); wrefresh(mainWin); } @@ -371,7 +376,7 @@ va_list ap; va_start(ap, fmt); - printMsgV(TRUE, FALSE, fmt, ap); + printMsgV(LOG_STAMP | LOG_WINDOW | LOG_FILE, fmt, ap); va_end(ap); } @@ -380,7 +385,7 @@ va_list ap; va_start(ap, fmt); - printMsgV(FALSE, FALSE, fmt, ap); + printMsgV(LOG_WINDOW | LOG_FILE, fmt, ap); va_end(ap); } @@ -389,7 +394,7 @@ va_list ap; va_start(ap, fmt); - printMsgV(TRUE, logOnly, fmt, ap); + printMsgV(logOnly ? (LOG_STAMP | LOG_FILE) : (LOG_STAMP | LOG_WINDOW | LOG_FILE), fmt, ap); va_end(ap); } @@ -402,7 +407,7 @@ va_list ap2; va_copy(ap2, ap); - printMsgV(TRUE, FALSE, fmt, ap); + printMsgV(LOG_STAMP | LOG_WINDOW | LOG_FILE, fmt, ap); tmp = th_strdup_vprintf(fmt, ap2); @@ -433,7 +438,7 @@ void messageFunc(struct _nn_conn_t *conn, const char *fmt, va_list ap) { (void) conn; - printMsgV(TRUE, FALSE, fmt, ap); + printMsgV(LOG_STAMP | LOG_WINDOW | LOG_FILE, fmt, ap); } @@ -1024,13 +1029,13 @@ } } -char *promptRequester(const char *info, BOOL allowEmpty) +char *promptRequester(WINDOW *win, const char *info, BOOL allowEmpty) { char tmpBuf[512], *ptr; ssize_t pos; - fputs(info, stdout); - fgets(tmpBuf, sizeof(tmpBuf), stdin); + waddstr(win, info); + wgetnstr(win, tmpBuf, sizeof(tmpBuf) - 1); for (pos = strlen(tmpBuf) - 1; pos > 0 && (tmpBuf[pos] == '\n' || tmpBuf[pos] == '\r' || th_isspace(tmpBuf[pos])); pos--) tmpBuf[pos] = 0; @@ -1141,17 +1146,6 @@ optPassword = optPasswordCmd; } - /* Check if we have username and password */ - if (optUserName == NULL || optPassword == NULL) { - printf("\nYou can avoid this prompt by issuing '/save' after logging in.\n\n"); - optUserName = promptRequester("NN username: ", FALSE); - optPassword = promptRequester("NN password: ", TRUE); - if (optUserName == NULL || optPassword == NULL) { - THERR("User/pass not specified, get some --help\n"); - return -1; - } - } - if (!argsOK) return -2; @@ -1176,54 +1170,6 @@ } else networkInit = TRUE; - /* Okay ... */ - THMSG(1, "Trying to resolve host '%s' ...\n", optServer); - tmpHost = gethostbyname(optServer); - if (tmpHost == NULL) { - THERR("Could not resolve hostname: %s.\n", strerror(h_errno)); - goto err_exit; - } - THMSG(2, "True hostname: %s\n", tmpHost->h_name); - - /* To emulate the official client, we first make a request for - * policy file, even though we don't use it for anything... - */ - conn = nn_conn_open((struct in_addr *) tmpHost->h_addr, 843); - if (!nn_conn_check(conn)) { - THERR("Policy file request connection setup failed!\n"); - goto err_exit; - } - - tmpStr = ""; - if (nn_conn_send_buf(conn, tmpStr, strlen(tmpStr) + 1) == FALSE) { - THERR("Failed to send policy file request.\n"); - goto err_exit; - } else { - int cres = nn_conn_pull(conn); - if (cres == 0) { - THMSG(2, "Probe got: %s\n", conn->buf); - } else { - THMSG(2, "Could not get policy probe.\n"); - } - } - nn_conn_close(conn); - - /* Okay, now do the proper connection ... */ - conn = nn_conn_open((struct in_addr *) tmpHost->h_addr, optPort); - if (!nn_conn_check(conn)) { - THERR("Main connection setup failed!\n"); - goto err_exit; - } - - conn->errfunc = errorFunc; - conn->msgfunc = messageFunc; - - THMSG(1, "Connected, logging in as '%s', site '%s'.\n", optUserName, optSite); - optUserNameEnc = nn_dblencode_str(optUserName); - tmpStr = nn_dblencode_str(optSite); - nn_conn_send_msg(conn, optUserNameEnc, "%%2Flogin%%20%%2Dsite%%20%s%%20%%2Dpassword%%20%s", tmpStr, optPassword); - th_free(tmpStr); - /* Initialize NCurses */ if (!optDaemon) { if (LINES < 0 || LINES > 1000) LINES = 24; @@ -1231,10 +1177,10 @@ initscr(); raw(); keypad(stdscr, TRUE); - noecho(); + echo(); meta(stdscr, TRUE); timeout(SET_DELAY); - curVis = curs_set(0); + curVis = curs_set(1); if (has_colors()) { start_color(); @@ -1262,15 +1208,81 @@ if (!initializeWindows()) goto err_exit; + updateStatus(insertMode); + } + + /* Check if we have username and password */ + if (cursesInit && (optUserName == NULL || optPassword == NULL)) { + printWin(editWin, "You can avoid this prompt by issuing '/save' after logging in.\n"); + optUserName = promptRequester(editWin, "NN username: ", FALSE); + optPassword = promptRequester(editWin, "NN password: ", TRUE); + } + + if (optUserName == NULL || optPassword == NULL) { + errorMsg("Username and/or password not specified.\n"); + goto err_exit; + } + + /* Okay ... */ + printMsg("Trying to resolve host '%s' ...\n", optServer); + tmpHost = gethostbyname(optServer); + if (tmpHost == NULL) { + errorMsg("Could not resolve hostname: %s.\n", strerror(h_errno)); + goto err_exit; + } + printMsg("True hostname: %s\n", tmpHost->h_name); + + /* To emulate the official client, we first make a request for + * policy file, even though we don't use it for anything... + */ + conn = nn_conn_open((struct in_addr *) tmpHost->h_addr, 843); + if (!nn_conn_check(conn)) { + errorMsg("Policy file request connection setup failed!\n"); + goto err_exit; + } + + tmpStr = ""; + if (nn_conn_send_buf(conn, tmpStr, strlen(tmpStr) + 1) == FALSE) { + errorMsg("Failed to send policy file request.\n"); + goto err_exit; + } else { + int cres = nn_conn_pull(conn); + if (cres == 0) { + printMsg("Probe got: %s\n", conn->buf); + } else { + printMsg("Could not get policy probe.\n"); + } + } + nn_conn_close(conn); + + /* Okay, now do the proper connection ... */ + conn = nn_conn_open((struct in_addr *) tmpHost->h_addr, optPort); + if (!nn_conn_check(conn)) { + errorMsg("Main connection setup failed!\n"); + goto err_exit; + } + + conn->errfunc = errorFunc; + conn->msgfunc = messageFunc; + + /* Log in */ + optUserNameEnc = nn_dblencode_str(optUserName); + tmpStr = nn_dblencode_str(optSite); + nn_conn_send_msg(conn, optUserNameEnc, "%%2Flogin%%20%%2Dsite%%20%s%%20%%2Dpassword%%20%s", tmpStr, optPassword); + th_free(tmpStr); + + /* Initialize random numbers */ + prevTime = time(NULL); + srandom((int) prevTime); + + if (cursesInit) { + noecho(); + curVis = curs_set(0); nn_editbuf_clear(editBuf); printEditBuf("", editBuf); updateStatus(insertMode); } - /* Initialize random numbers */ - prevTime = time(NULL); - srandom((int) prevTime); - /* Enter mainloop */ while (!isError && !exitProg) { int cres = nn_conn_pull(conn); @@ -1590,6 +1602,13 @@ for (histPos = 0; histPos <= SET_MAX_HISTORY; histPos++) nn_editbuf_free(histBuf[histPos]); +#ifdef __WIN32 + if (errorMessages || isError) { + char *tmp = promptRequester(editWin, "Press enter to quit.\n", FALSE); + th_free(tmp); + } +#endif + if (cursesInit) { if (curVis != ERR) curs_set(curVis); @@ -1598,17 +1617,10 @@ THMSG(1, "NCurses deinitialized.\n"); } +#ifndef __WIN32 if (errorMessages) THERR("%s", errorMessages); - - if (isError) { -#ifdef __WIN32 - char *tmp = promptRequester("Press enter to quit.\n", FALSE); - th_free(tmp); -#else - THMSG(1, "Error exit.\n"); #endif - } th_free(optUserNameEnc);