comparison nnchat.c @ 267:5175ed15ffa4

Merged.
author Matti Hamalainen <ccr@tnsp.org>
date Fri, 03 Jun 2011 11:47:11 +0300
parents 6d48fc4fd421 b9c650db8a6a
children d04ea4395e9e
comparison
equal deleted inserted replaced
266:9bf3e5620eb5 267:5175ed15ffa4
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 *setIgnoreList = 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;
435 } 437 }
436 438
437 439
438 BOOL checkIgnoreList(const char *name) 440 BOOL checkIgnoreList(const char *name)
439 { 441 {
440 qlist_t *node = nnIgnoreList; 442 qlist_t *node = setIgnoreList;
441 while (node != NULL) { 443 while (node != NULL) {
442 if (strcasecmp(name, (char *) node->data) == 0) 444 if (strcasecmp(name, (char *) node->data) == 0)
443 return TRUE; 445 return TRUE;
444 node = node->next; 446 node = node->next;
445 } 447 }
683 } 685 }
684 else if (!strncasecmp(buf, "/ignore", 7)) { 686 else if (!strncasecmp(buf, "/ignore", 7)) {
685 char *name = trimLeft(buf + 7); 687 char *name = trimLeft(buf + 7);
686 if (strlen(name) > 0) { 688 if (strlen(name) > 0) {
687 /* Add or remove someone to/from ignore */ 689 /* Add or remove someone to/from ignore */
688 qlist_t *user = th_llist_find_func(nnIgnoreList, name, compareUsername); 690 qlist_t *user = th_llist_find_func(setIgnoreList, name, compareUsername);
689 if (user != NULL) { 691 if (user != NULL) {
690 printMsg("Removed user '%s' from ignore.\n", name); 692 printMsg("Removed user '%s' from ignore.\n", name);
691 th_llist_delete_node(&nnIgnoreList, user); 693 th_llist_delete_node(&setIgnoreList, user);
692 } else { 694 } else {
693 printMsg("Now ignoring '%s'.\n", name); 695 printMsg("Now ignoring '%s'.\n", name);
694 th_llist_append(&nnIgnoreList, th_strdup(name)); 696 th_llist_append(&setIgnoreList, th_strdup(name));
695 } 697 }
696 } else { 698 } else {
697 /* Just list whomever is in ignore now */ 699 /* Just list whomever is in ignore now */
698 qlist_t *user = nnIgnoreList; 700 qlist_t *user = setIgnoreList;
699 ssize_t nuser = th_llist_length(nnIgnoreList); 701 ssize_t nuser = th_llist_length(setIgnoreList);
700 printMsg("Users ignored (%d): ", nuser); 702 printMsg("Users ignored (%d): ", nuser);
701 while (user != NULL) { 703 while (user != NULL) {
702 if (user->data != NULL) { 704 if (user->data != NULL) {
703 printMsgC("'%s'", (char *) user->data); 705 printMsgC("'%s'", (char *) user->data);
704 if (--nuser > 0) 706 if (--nuser > 0)
1080 th_cfg_add_hexvalue(&tmpcfg, "color", &optUserColor, optUserColor); 1082 th_cfg_add_hexvalue(&tmpcfg, "color", &optUserColor, optUserColor);
1081 1083
1082 th_cfg_add_comment(&tmpcfg, "Default setting of ignore mode"); 1084 th_cfg_add_comment(&tmpcfg, "Default setting of ignore mode");
1083 th_cfg_add_bool(&tmpcfg, "ignore", &setIgnoreMode, setIgnoreMode); 1085 th_cfg_add_bool(&tmpcfg, "ignore", &setIgnoreMode, setIgnoreMode);
1084 th_cfg_add_comment(&tmpcfg, "People to be ignored when ignore mode is enabled"); 1086 th_cfg_add_comment(&tmpcfg, "People to be ignored when ignore mode is enabled");
1085 th_cfg_add_string_list(&tmpcfg, "ignore_list", &nnIgnoreList); 1087 th_cfg_add_string_list(&tmpcfg, "ignore_list", &setIgnoreList);
1088
1089 th_cfg_add_comment(&tmpcfg, "Random messages for idle timeout protection. If none are set, plain '.' is used.");
1090 th_cfg_add_string_list(&tmpcfg, "idle_messages", &setIdleMessages);
1091
1086 th_cfg_add_section(&cfg, "general", tmpcfg); 1092 th_cfg_add_section(&cfg, "general", tmpcfg);
1093
1087 1094
1088 tmpcfg = NULL; 1095 tmpcfg = NULL;
1089 th_cfg_add_comment(&tmpcfg, "Chat server hostname or IP address"); 1096 th_cfg_add_comment(&tmpcfg, "Chat server hostname or IP address");
1090 th_cfg_add_string(&tmpcfg, "host", &optServer, optServer); 1097 th_cfg_add_string(&tmpcfg, "host", &optServer, optServer);
1091 th_cfg_add_comment(&tmpcfg, "Default port to connect to (8002 = public room, 8003 = passion pit, 8005 = members only)"); 1098 th_cfg_add_comment(&tmpcfg, "Default port to connect to (8002 = public room, 8003 = passion pit, 8005 = members only)");
1152 if ((nnUsers = nn_userhash_new()) == NULL) { 1159 if ((nnUsers = nn_userhash_new()) == NULL) {
1153 THERR("Could not allocate userhash. Fatal error.\n"); 1160 THERR("Could not allocate userhash. Fatal error.\n");
1154 return -105; 1161 return -105;
1155 } 1162 }
1156 1163
1164 /* If no idle messages are set, add default */
1165 if (setIdleMessages == NULL) {
1166 th_llist_append(&setIdleMessages, th_strdup("."));
1167 }
1168
1157 /* Open logfile */ 1169 /* Open logfile */
1158 logFileOpen(); 1170 logFileOpen();
1159 1171
1160 1172 /* Initialize network */
1161 if (!nn_network_init()) { 1173 if (!nn_network_init()) {
1162 THERR("Could not initialize network subsystem.\n"); 1174 THERR("Could not initialize network subsystem.\n");
1163 goto err_exit; 1175 goto err_exit;
1164 } else 1176 } else
1165 networkInit = TRUE; 1177 networkInit = TRUE;
1253 nn_editbuf_clear(editBuf); 1265 nn_editbuf_clear(editBuf);
1254 printEditBuf("", editBuf); 1266 printEditBuf("", editBuf);
1255 updateStatus(insertMode); 1267 updateStatus(insertMode);
1256 } 1268 }
1257 1269
1270 /* Initialize random numbers */
1271 prevTime = time(NULL);
1272 srandom((int) prevTime);
1273
1258 /* Enter mainloop */ 1274 /* Enter mainloop */
1259 prevTime = time(NULL);
1260
1261 while (!isError && !exitProg) { 1275 while (!isError && !exitProg) {
1262 int cres = nn_conn_pull(conn); 1276 int cres = nn_conn_pull(conn);
1263 if (cres == 0) { 1277 if (cres == 0) {
1264 do { 1278 do {
1265 size_t bufLen = strlen(conn->ptr) + 1; 1279 size_t bufLen = strlen(conn->ptr) + 1;
1376 nn_editbuf_clear(editBuf); 1390 nn_editbuf_clear(editBuf);
1377 1391
1378 if (result < 0) { 1392 if (result < 0) {
1379 errorMsg("Fatal error handling user input: %s\n", editBuf->data); 1393 errorMsg("Fatal error handling user input: %s\n", editBuf->data);
1380 isError = TRUE; 1394 isError = TRUE;
1395 } else {
1396 /* Update time value of last sent message for unidle timeouts */
1397 prevTime = time(NULL);
1381 } 1398 }
1382 1399
1383 update = TRUE; 1400 update = TRUE;
1384 } 1401 }
1385 break; 1402 break;
1543 } /* !optDaemon */ 1560 } /* !optDaemon */
1544 1561
1545 if (++updateCount > 10) { 1562 if (++updateCount > 10) {
1546 time_t tmpTime = time(NULL); 1563 time_t tmpTime = time(NULL);
1547 if (tmpTime - prevTime > SET_KEEPALIVE) { 1564 if (tmpTime - prevTime > SET_KEEPALIVE) {
1548 nn_conn_send_msg(conn, optUserNameEnc, "/listallusers"); 1565 int n = random() % th_llist_length(setIdleMessages);
1566 qlist_t *node = th_llist_get_nth(setIdleMessages, n);
1567 nn_conn_send_msg(conn, optUserNameEnc, node->data);
1549 prevTime = tmpTime; 1568 prevTime = tmpTime;
1550 } 1569 }
1551 1570
1552 if (!colorSet) { 1571 if (!colorSet) {
1553 colorSet = TRUE; 1572 colorSet = TRUE;