changeset 267:5175ed15ffa4

Merged.
author Matti Hamalainen <ccr@tnsp.org>
date Fri, 03 Jun 2011 11:47:11 +0300
parents 9bf3e5620eb5 (current diff) b9c650db8a6a (diff)
children d04ea4395e9e
files nnchat.c
diffstat 1 files changed, 30 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/nnchat.c	Fri Jun 03 11:24:36 2011 +0300
+++ b/nnchat.c	Fri Jun 03 11:47:11 2011 +0300
@@ -44,6 +44,7 @@
 #define SET_KEEPALIVE   (15*60)     /* Ping/keepalive period in seconds */
 
 
+
 /* Options
  */
 int     optPort = 8005;
@@ -68,7 +69,8 @@
 BOOL    optDebug = FALSE;
 BOOL    optLogEnable = FALSE;
 
-qlist_t *nnIgnoreList = NULL;
+qlist_t *setIgnoreList = NULL,
+        *setIdleMessages = NULL;
 nn_userhash_t *nnUsers = NULL;
 char    *setConfigFile = NULL,
         *setBrowser = NULL;
@@ -437,7 +439,7 @@
 
 BOOL checkIgnoreList(const char *name)
 {
-    qlist_t *node = nnIgnoreList;
+    qlist_t *node = setIgnoreList;
     while (node != NULL) {
         if (strcasecmp(name, (char *) node->data) == 0)
             return TRUE;
@@ -685,18 +687,18 @@
         char *name = trimLeft(buf + 7);
         if (strlen(name) > 0) {
             /* Add or remove someone to/from ignore */
-            qlist_t *user = th_llist_find_func(nnIgnoreList, name, compareUsername);
+            qlist_t *user = th_llist_find_func(setIgnoreList, name, compareUsername);
             if (user != NULL) {
                 printMsg("Removed user '%s' from ignore.\n", name);
-                th_llist_delete_node(&nnIgnoreList, user);
+                th_llist_delete_node(&setIgnoreList, user);
             } else {
                 printMsg("Now ignoring '%s'.\n", name);
-                th_llist_append(&nnIgnoreList, th_strdup(name));
+                th_llist_append(&setIgnoreList, th_strdup(name));
             }
         } else {
             /* Just list whomever is in ignore now */
-            qlist_t *user = nnIgnoreList;
-            ssize_t nuser = th_llist_length(nnIgnoreList);
+            qlist_t *user = setIgnoreList;
+            ssize_t nuser = th_llist_length(setIgnoreList);
             printMsg("Users ignored (%d): ", nuser);
             while (user != NULL) {
                 if (user->data != NULL) {
@@ -1082,9 +1084,14 @@
     th_cfg_add_comment(&tmpcfg, "Default setting of ignore mode");
     th_cfg_add_bool(&tmpcfg, "ignore", &setIgnoreMode, setIgnoreMode);
     th_cfg_add_comment(&tmpcfg, "People to be ignored when ignore mode is enabled");
-    th_cfg_add_string_list(&tmpcfg, "ignore_list", &nnIgnoreList);
+    th_cfg_add_string_list(&tmpcfg, "ignore_list", &setIgnoreList);
+
+    th_cfg_add_comment(&tmpcfg, "Random messages for idle timeout protection. If none are set, plain '.' is used.");
+    th_cfg_add_string_list(&tmpcfg, "idle_messages", &setIdleMessages);
+
     th_cfg_add_section(&cfg, "general", tmpcfg);
 
+
     tmpcfg = NULL;
     th_cfg_add_comment(&tmpcfg, "Chat server hostname or IP address");
     th_cfg_add_string(&tmpcfg, "host", &optServer, optServer);
@@ -1154,10 +1161,15 @@
         return -105;
     }
 
+    /* If no idle messages are set, add default */
+    if (setIdleMessages == NULL) {
+        th_llist_append(&setIdleMessages, th_strdup("."));
+    }
+
     /* Open logfile */
     logFileOpen();
         
-    
+    /* Initialize network */    
     if (!nn_network_init()) {
         THERR("Could not initialize network subsystem.\n");
         goto err_exit;
@@ -1255,9 +1267,11 @@
         updateStatus(insertMode);
     }
 
-    /* Enter mainloop */
+    /* Initialize random numbers */
     prevTime = time(NULL);
+    srandom((int) prevTime);
 
+    /* Enter mainloop */
     while (!isError && !exitProg) {
         int cres = nn_conn_pull(conn);
         if (cres == 0) {
@@ -1378,6 +1392,9 @@
                     if (result < 0) {
                         errorMsg("Fatal error handling user input: %s\n", editBuf->data);
                         isError = TRUE;
+                    } else {
+                        /* Update time value of last sent message for unidle timeouts */
+                        prevTime = time(NULL);
                     }
                     
                     update = TRUE;
@@ -1545,7 +1562,9 @@
         if (++updateCount > 10) {
             time_t tmpTime = time(NULL);
             if (tmpTime - prevTime > SET_KEEPALIVE) {
-                nn_conn_send_msg(conn, optUserNameEnc, "/listallusers"); 
+                int n = random() % th_llist_length(setIdleMessages);
+                qlist_t *node = th_llist_get_nth(setIdleMessages, n);
+                nn_conn_send_msg(conn, optUserNameEnc, node->data);
                 prevTime = tmpTime;
             }