changeset 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 e97c764927ce
children b9c650db8a6a
files nnchat.c
diffstat 1 files changed, 23 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/nnchat.c	Fri May 06 03:34:52 2011 +0300
+++ b/nnchat.c	Fri Jun 03 11:18:19 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 *nnIgnoreList = NULL,
+        *setIdleMessages = NULL;
 nn_userhash_t *nnUsers = NULL;
 char    *setConfigFile = NULL,
         *setBrowser = NULL;
@@ -1081,8 +1083,13 @@
     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_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);
@@ -1152,10 +1159,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;
@@ -1253,9 +1265,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) {
@@ -1376,6 +1390,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;
@@ -1543,7 +1560,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;
             }