Implement IMAP keepalive.
authorLars Magne Ingebrigtsen <larsi@quimbies.gnus.org>
Fri, 24 Sep 2010 16:33:58 +0000 (18:33 +0200)
committerLars Magne Ingebrigtsen <larsi@quimbies.gnus.org>
Fri, 24 Sep 2010 16:33:58 +0000 (18:33 +0200)
lisp/ChangeLog
lisp/nnimap.el

index b4080cc..9733760 100644 (file)
@@ -2,6 +2,9 @@
 
        * nnimap.el (nnimap-command): Register the last command time so
        that we can use it for idling NOOPs.
+       (nnimap-open-connection): Start the keeplive timer.
+       (nnimap-make-process-buffer): Store all the process buffers.
+       (nnimap-keepalive): New function.
 
        * starttls.el: (starttls-open-stream): Add autoload cookie.
 
index afc0c3f..7f88685 100644 (file)
@@ -90,6 +90,9 @@ not done by default on servers that doesn't support that command.")
 (defvar nnimap-split-download-body-default nil
   "Internal variable with default value for `nnimap-split-download-body'.")
 
+(defvar nnimap-keepalive-timer nil)
+(defvar nnimap-process-buffers nil)
+
 (defstruct nnimap
   group process commands capabilities select-result newlinep server
   last-command-time)
@@ -224,6 +227,7 @@ not done by default on servers that doesn't support that command.")
     (set (make-local-variable 'nnimap-object)
         (make-nnimap :server (nnoo-current-server 'nnimap)))
     (push (list buffer (current-buffer)) nnimap-connection-alist)
+    (push (current-buffer) nnimap-process-buffers)
     (current-buffer)))
 
 (defun nnimap-open-shell-stream (name buffer host port)
@@ -247,7 +251,25 @@ not done by default on servers that doesn't support that command.")
             '("login" "password") address port nil (null ports))))
     credentials))
 
+(defun nnimap-keepalive ()
+  (let ((now (current-time)))
+    (dolist (buffer nnimap-process-buffers)
+      (when (buffer-name buffer)
+       (with-current-buffer buffer
+         (when (and nnimap-object
+                    (nnimap-last-command-time nnimap-object)
+                    (> (time-to-seconds
+                        (time-subtract
+                         now
+                         (nnimap-last-command-time nnimap-object)))
+                       ;; More than five minutes since the last command.
+                       (* 5 60)))
+           (nnimap-send-command "NOOP")))))))
+
 (defun nnimap-open-connection (buffer)
+  (unless nnimap-keepalive-timer
+    (setq nnimap-keepalive-timer (run-at-time (* 60 15) (* 60 15)
+                                             'nnimap-keepalive)))
   (with-current-buffer (nnimap-make-process-buffer buffer)
     (let* ((coding-system-for-read 'binary)
           (coding-system-for-write 'binary)