Mercurial > hg > nnchat
comparison nnchat.c @ 275:66b90d71548b
Improve interactive prompts, etc.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Fri, 03 Jun 2011 15:16:52 +0300 |
parents | 4d106ad65f26 |
children | 2d8967497e21 |
comparison
equal
deleted
inserted
replaced
274:1211b740fbda | 275:66b90d71548b |
---|---|
73 *setIdleMessages = NULL; | 73 *setIdleMessages = NULL; |
74 nn_userhash_t *nnUsers = NULL; | 74 nn_userhash_t *nnUsers = NULL; |
75 char *setConfigFile = NULL, | 75 char *setConfigFile = NULL, |
76 *setBrowser = NULL; | 76 *setBrowser = NULL; |
77 cfgitem_t *cfg = NULL; | 77 cfgitem_t *cfg = NULL; |
78 nn_ringbuf_t *backBuf = NULL; | |
79 | 78 |
80 | 79 |
81 /* Logging mode flags | 80 /* Logging mode flags |
82 */ | 81 */ |
83 enum { | 82 enum { |
1035 | 1034 |
1036 char *promptRequester(WINDOW *win, const char *info, BOOL allowEmpty) | 1035 char *promptRequester(WINDOW *win, const char *info, BOOL allowEmpty) |
1037 { | 1036 { |
1038 char tmpBuf[512], *ptr; | 1037 char tmpBuf[512], *ptr; |
1039 ssize_t pos; | 1038 ssize_t pos; |
1040 | 1039 int curVis = curs_set(1); |
1040 | |
1041 echo(); | |
1041 waddstr(win, info); | 1042 waddstr(win, info); |
1042 wgetnstr(win, tmpBuf, sizeof(tmpBuf) - 1); | 1043 wgetnstr(win, tmpBuf, sizeof(tmpBuf) - 1); |
1044 noecho(); | |
1045 if (curVis != ERR) | |
1046 curs_set(curVis); | |
1043 | 1047 |
1044 for (pos = strlen(tmpBuf) - 1; pos > 0 && (tmpBuf[pos] == '\n' || tmpBuf[pos] == '\r' || th_isspace(tmpBuf[pos])); pos--) | 1048 for (pos = strlen(tmpBuf) - 1; pos > 0 && (tmpBuf[pos] == '\n' || tmpBuf[pos] == '\r' || th_isspace(tmpBuf[pos])); pos--) |
1045 tmpBuf[pos] = 0; | 1049 tmpBuf[pos] = 0; |
1046 | 1050 |
1047 ptr = trimLeft(tmpBuf); | 1051 ptr = trimLeft(tmpBuf); |
1053 } | 1057 } |
1054 | 1058 |
1055 int main(int argc, char *argv[]) | 1059 int main(int argc, char *argv[]) |
1056 { | 1060 { |
1057 nn_conn_t *conn = NULL; | 1061 nn_conn_t *conn = NULL; |
1062 struct hostent *tmpHost; | |
1058 int curVis = ERR, updateCount = 0; | 1063 int curVis = ERR, updateCount = 0; |
1059 struct hostent *tmpHost; | |
1060 BOOL argsOK, isError = FALSE, | 1064 BOOL argsOK, isError = FALSE, |
1061 exitProg = FALSE, | 1065 exitProg = FALSE, |
1062 colorSet = FALSE, | 1066 colorSet = FALSE, |
1063 cursesInit = FALSE, | 1067 cursesInit = FALSE, |
1064 networkInit = FALSE, | 1068 networkInit = FALSE, |
1065 insertMode = TRUE; | 1069 insertMode = TRUE, |
1070 firstUpdate = TRUE; | |
1066 time_t prevTime; | 1071 time_t prevTime; |
1067 char *tmpStr; | 1072 char *tmpStr; |
1068 nn_editbuf_t *editBuf = nn_editbuf_new(NN_TMPBUF_SIZE); | 1073 nn_editbuf_t *editBuf = nn_editbuf_new(NN_TMPBUF_SIZE); |
1069 nn_editbuf_t *histBuf[SET_MAX_HISTORY+2]; | 1074 nn_editbuf_t *histBuf[SET_MAX_HISTORY+2]; |
1070 int histPos = 0, histMax = 0; | 1075 int histPos = 0, histMax = 0; |
1071 BOOL firstUpdate = TRUE; | 1076 |
1077 nn_ringbuf_t *backBuf; | |
1072 | 1078 |
1073 cfgitem_t *tmpcfg; | 1079 cfgitem_t *tmpcfg; |
1074 char *homeDir = NULL; | 1080 char *homeDir = NULL; |
1075 | 1081 |
1076 memset(histBuf, 0, sizeof(histBuf)); | |
1077 backBuf = nn_ringbuf_new(SET_BACKBUF_LEN); | |
1078 | 1082 |
1079 /* Initialize */ | 1083 /* Initialize */ |
1080 th_init("NNChat", "Newbie Nudes chat client", NN_VERSION, | 1084 th_init("NNChat", "Newbie Nudes chat client", NN_VERSION, |
1081 "Written and designed by Anonymous Finnish Guy (C) 2008-2011", | 1085 "Written and designed by Anonymous Finnish Guy (C) 2008-2011", |
1082 "This software is freeware, use and distribute as you wish."); | 1086 "This software is freeware, use and distribute as you wish."); |
1083 th_verbosityLevel = 0; | 1087 th_verbosityLevel = 0; |
1084 | 1088 |
1085 /* Read config */ | 1089 /* Read configuration file */ |
1086 tmpcfg = NULL; | 1090 tmpcfg = NULL; |
1087 th_cfg_add_comment(&tmpcfg, "General settings"); | 1091 th_cfg_add_comment(&tmpcfg, "General settings"); |
1088 th_cfg_add_string(&tmpcfg, "username", &optUserName, NULL); | 1092 th_cfg_add_string(&tmpcfg, "username", &optUserName, NULL); |
1089 th_cfg_add_string(&tmpcfg, "password", &optPassword, NULL); | 1093 th_cfg_add_string(&tmpcfg, "password", &optPassword, NULL); |
1090 th_cfg_add_comment(&tmpcfg, "Default color as a hex-triplet"); | 1094 th_cfg_add_comment(&tmpcfg, "Default color as a hex-triplet"); |
1179 if (LINES < 0 || LINES > 1000) LINES = 24; | 1183 if (LINES < 0 || LINES > 1000) LINES = 24; |
1180 if (COLS < 0 || COLS > 1000) COLS = 80; | 1184 if (COLS < 0 || COLS > 1000) COLS = 80; |
1181 initscr(); | 1185 initscr(); |
1182 raw(); | 1186 raw(); |
1183 keypad(stdscr, TRUE); | 1187 keypad(stdscr, TRUE); |
1184 echo(); | 1188 noecho(); |
1185 meta(stdscr, TRUE); | 1189 meta(stdscr, TRUE); |
1186 timeout(SET_DELAY); | 1190 timeout(SET_DELAY); |
1187 curVis = curs_set(1); | 1191 curVis = curs_set(0); |
1188 | 1192 |
1189 if (has_colors()) { | 1193 if (has_colors()) { |
1190 start_color(); | 1194 start_color(); |
1191 | 1195 |
1192 init_pair( 1, COLOR_RED, COLOR_BLACK); | 1196 init_pair( 1, COLOR_RED, COLOR_BLACK); |
1267 } | 1271 } |
1268 | 1272 |
1269 conn->errfunc = errorFunc; | 1273 conn->errfunc = errorFunc; |
1270 conn->msgfunc = messageFunc; | 1274 conn->msgfunc = messageFunc; |
1271 | 1275 |
1272 /* Log in */ | 1276 /* Send login command */ |
1273 optUserNameEnc = nn_dblencode_str(optUserName); | 1277 optUserNameEnc = nn_dblencode_str(optUserName); |
1274 tmpStr = nn_dblencode_str(optSite); | 1278 tmpStr = nn_dblencode_str(optSite); |
1275 nn_conn_send_msg(conn, optUserNameEnc, "%%2Flogin%%20%%2Dsite%%20%s%%20%%2Dpassword%%20%s", tmpStr, optPassword); | 1279 nn_conn_send_msg(conn, optUserNameEnc, "%%2Flogin%%20%%2Dsite%%20%s%%20%%2Dpassword%%20%s", tmpStr, optPassword); |
1276 th_free(tmpStr); | 1280 th_free(tmpStr); |
1277 | 1281 |
1278 /* Initialize random numbers */ | 1282 /* Initialize random numbers */ |
1279 prevTime = time(NULL); | 1283 prevTime = time(NULL); |
1280 srandom((int) prevTime); | 1284 srandom((int) prevTime); |
1281 | 1285 |
1282 if (cursesInit) { | 1286 if (cursesInit) { |
1283 noecho(); | 1287 /* Initialize rest of interactive UI code */ |
1284 curVis = curs_set(0); | 1288 memset(histBuf, 0, sizeof(histBuf)); |
1289 backBuf = nn_ringbuf_new(SET_BACKBUF_LEN); | |
1285 nn_editbuf_clear(editBuf); | 1290 nn_editbuf_clear(editBuf); |
1291 | |
1292 /* First update of screen */ | |
1286 printEditBuf("", editBuf); | 1293 printEditBuf("", editBuf); |
1287 updateStatus(insertMode); | 1294 updateStatus(insertMode); |
1288 } | 1295 } |
1289 | 1296 |
1290 /* Enter mainloop */ | 1297 /* Enter mainloop */ |
1310 } | 1317 } |
1311 if (!nn_conn_check(conn)) | 1318 if (!nn_conn_check(conn)) |
1312 isError = TRUE; | 1319 isError = TRUE; |
1313 | 1320 |
1314 /* Handle user input */ | 1321 /* Handle user input */ |
1315 if (!optDaemon) { | 1322 if (cursesInit) { |
1316 int c, cnt = 0; | 1323 int c, cnt = 0; |
1317 BOOL update = FALSE; | 1324 BOOL update = FALSE; |
1318 | 1325 |
1319 /* Handle several buffered keypresses at once */ | 1326 /* Handle several buffered keypresses at once */ |
1320 do { | 1327 do { |
1571 /* Update edit line */ | 1578 /* Update edit line */ |
1572 printEditBuf(setPrvMode ? setTarget : "", editBuf); | 1579 printEditBuf(setPrvMode ? setTarget : "", editBuf); |
1573 updateStatus(insertMode); | 1580 updateStatus(insertMode); |
1574 firstUpdate = FALSE; /* a nasty hack ... */ | 1581 firstUpdate = FALSE; /* a nasty hack ... */ |
1575 } | 1582 } |
1576 } /* !optDaemon */ | 1583 } /* cursesInit */ |
1577 | 1584 |
1578 if (++updateCount > 10) { | 1585 if (++updateCount > 10) { |
1579 time_t tmpTime = time(NULL); | 1586 time_t tmpTime = time(NULL); |
1580 if (tmpTime - prevTime > SET_KEEPALIVE) { | 1587 if (tmpTime - prevTime > SET_KEEPALIVE) { |
1581 int n = random() % th_llist_length(setIdleMessages); | 1588 int n = random() % th_llist_length(setIdleMessages); |
1606 for (histPos = 0; histPos <= SET_MAX_HISTORY; histPos++) | 1613 for (histPos = 0; histPos <= SET_MAX_HISTORY; histPos++) |
1607 nn_editbuf_free(histBuf[histPos]); | 1614 nn_editbuf_free(histBuf[histPos]); |
1608 | 1615 |
1609 #ifdef __WIN32 | 1616 #ifdef __WIN32 |
1610 if (errorMessages || isError) { | 1617 if (errorMessages || isError) { |
1611 char *tmp = promptRequester(editWin, "Press enter to quit.\n", FALSE); | 1618 char *tmp; |
1619 wclear(editWin); | |
1620 tmp = promptRequester(editWin, "Press enter to quit.\n", FALSE); | |
1612 th_free(tmp); | 1621 th_free(tmp); |
1613 } | 1622 } |
1614 #endif | 1623 #endif |
1615 | 1624 |
1616 if (cursesInit) { | 1625 if (cursesInit) { |