comparison nnchat.c @ 264:4c1c18a388d9

Add configuration setting and functionality for random "keepalive" messages to avoid idle timeout kicks from chat.
author Matti Hamalainen <ccr@tnsp.org>
date Fri, 03 Jun 2011 11:18:19 +0300
parents 8a0f7446318e
children b9c650db8a6a
comparison
equal deleted inserted replaced
260:e97c764927ce 264:4c1c18a388d9
42 #define SET_BACKBUF_LEN (512) /* Backbuffer size (in lines) */ 42 #define SET_BACKBUF_LEN (512) /* Backbuffer size (in lines) */
43 #define SET_MAX_HISTORY (16) /* Command history length */ 43 #define SET_MAX_HISTORY (16) /* Command history length */
44 #define SET_KEEPALIVE (15*60) /* Ping/keepalive period in seconds */ 44 #define SET_KEEPALIVE (15*60) /* Ping/keepalive period in seconds */
45 45
46 46
47
47 /* Options 48 /* Options
48 */ 49 */
49 int optPort = 8005; 50 int optPort = 8005;
50 int optUserColor = 0x000000; 51 int optUserColor = 0x000000;
51 char *optServer = "chat.newbienudes.com", 52 char *optServer = "chat.newbienudes.com",
66 BOOL setPrvMode = FALSE; 67 BOOL setPrvMode = FALSE;
67 BOOL setIgnoreMode = FALSE; 68 BOOL setIgnoreMode = FALSE;
68 BOOL optDebug = FALSE; 69 BOOL optDebug = FALSE;
69 BOOL optLogEnable = FALSE; 70 BOOL optLogEnable = FALSE;
70 71
71 qlist_t *nnIgnoreList = NULL; 72 qlist_t *nnIgnoreList = NULL,
73 *setIdleMessages = NULL;
72 nn_userhash_t *nnUsers = NULL; 74 nn_userhash_t *nnUsers = NULL;
73 char *setConfigFile = NULL, 75 char *setConfigFile = NULL,
74 *setBrowser = NULL; 76 *setBrowser = NULL;
75 cfgitem_t *cfg = NULL; 77 cfgitem_t *cfg = NULL;
76 nn_ringbuf_t *backBuf = NULL; 78 nn_ringbuf_t *backBuf = NULL;
1079 1081
1080 th_cfg_add_comment(&tmpcfg, "Default setting of ignore mode"); 1082 th_cfg_add_comment(&tmpcfg, "Default setting of ignore mode");
1081 th_cfg_add_bool(&tmpcfg, "ignore", &setIgnoreMode, setIgnoreMode); 1083 th_cfg_add_bool(&tmpcfg, "ignore", &setIgnoreMode, setIgnoreMode);
1082 th_cfg_add_comment(&tmpcfg, "People to be ignored when ignore mode is enabled"); 1084 th_cfg_add_comment(&tmpcfg, "People to be ignored when ignore mode is enabled");
1083 th_cfg_add_string_list(&tmpcfg, "ignore_list", &nnIgnoreList); 1085 th_cfg_add_string_list(&tmpcfg, "ignore_list", &nnIgnoreList);
1086
1087 th_cfg_add_comment(&tmpcfg, "Random messages for idle timeout protection. If none are set, plain '.' is used.");
1088 th_cfg_add_string_list(&tmpcfg, "idle_messages", &setIdleMessages);
1089
1084 th_cfg_add_section(&cfg, "general", tmpcfg); 1090 th_cfg_add_section(&cfg, "general", tmpcfg);
1091
1085 1092
1086 tmpcfg = NULL; 1093 tmpcfg = NULL;
1087 th_cfg_add_comment(&tmpcfg, "Chat server hostname or IP address"); 1094 th_cfg_add_comment(&tmpcfg, "Chat server hostname or IP address");
1088 th_cfg_add_string(&tmpcfg, "host", &optServer, optServer); 1095 th_cfg_add_string(&tmpcfg, "host", &optServer, optServer);
1089 th_cfg_add_comment(&tmpcfg, "Default port to connect to (8002 = public room, 8003 = passion pit, 8005 = members only)"); 1096 th_cfg_add_comment(&tmpcfg, "Default port to connect to (8002 = public room, 8003 = passion pit, 8005 = members only)");
1150 if ((nnUsers = nn_userhash_new()) == NULL) { 1157 if ((nnUsers = nn_userhash_new()) == NULL) {
1151 THERR("Could not allocate userhash. Fatal error.\n"); 1158 THERR("Could not allocate userhash. Fatal error.\n");
1152 return -105; 1159 return -105;
1153 } 1160 }
1154 1161
1162 /* If no idle messages are set, add default */
1163 if (setIdleMessages == NULL) {
1164 th_llist_append(&setIdleMessages, th_strdup("."));
1165 }
1166
1155 /* Open logfile */ 1167 /* Open logfile */
1156 logFileOpen(); 1168 logFileOpen();
1157 1169
1158 1170 /* Initialize network */
1159 if (!nn_network_init()) { 1171 if (!nn_network_init()) {
1160 THERR("Could not initialize network subsystem.\n"); 1172 THERR("Could not initialize network subsystem.\n");
1161 goto err_exit; 1173 goto err_exit;
1162 } else 1174 } else
1163 networkInit = TRUE; 1175 networkInit = TRUE;
1251 nn_editbuf_clear(editBuf); 1263 nn_editbuf_clear(editBuf);
1252 printEditBuf("", editBuf); 1264 printEditBuf("", editBuf);
1253 updateStatus(insertMode); 1265 updateStatus(insertMode);
1254 } 1266 }
1255 1267
1268 /* Initialize random numbers */
1269 prevTime = time(NULL);
1270 srandom((int) prevTime);
1271
1256 /* Enter mainloop */ 1272 /* Enter mainloop */
1257 prevTime = time(NULL);
1258
1259 while (!isError && !exitProg) { 1273 while (!isError && !exitProg) {
1260 int cres = nn_conn_pull(conn); 1274 int cres = nn_conn_pull(conn);
1261 if (cres == 0) { 1275 if (cres == 0) {
1262 do { 1276 do {
1263 size_t bufLen = strlen(conn->ptr) + 1; 1277 size_t bufLen = strlen(conn->ptr) + 1;
1374 nn_editbuf_clear(editBuf); 1388 nn_editbuf_clear(editBuf);
1375 1389
1376 if (result < 0) { 1390 if (result < 0) {
1377 errorMsg("Fatal error handling user input: %s\n", editBuf->data); 1391 errorMsg("Fatal error handling user input: %s\n", editBuf->data);
1378 isError = TRUE; 1392 isError = TRUE;
1393 } else {
1394 /* Update time value of last sent message for unidle timeouts */
1395 prevTime = time(NULL);
1379 } 1396 }
1380 1397
1381 update = TRUE; 1398 update = TRUE;
1382 } 1399 }
1383 break; 1400 break;
1541 } /* !optDaemon */ 1558 } /* !optDaemon */
1542 1559
1543 if (++updateCount > 10) { 1560 if (++updateCount > 10) {
1544 time_t tmpTime = time(NULL); 1561 time_t tmpTime = time(NULL);
1545 if (tmpTime - prevTime > SET_KEEPALIVE) { 1562 if (tmpTime - prevTime > SET_KEEPALIVE) {
1546 nn_conn_send_msg(conn, optUserNameEnc, "/listallusers"); 1563 int n = random() % th_llist_length(setIdleMessages);
1564 qlist_t *node = th_llist_get_nth(setIdleMessages, n);
1565 nn_conn_send_msg(conn, optUserNameEnc, node->data);
1547 prevTime = tmpTime; 1566 prevTime = tmpTime;
1548 } 1567 }
1549 1568
1550 if (!colorSet) { 1569 if (!colorSet) {
1551 colorSet = TRUE; 1570 colorSet = TRUE;