[PATCH] Allow ERC to re-connect in the background properly

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

[PATCH] Allow ERC to re-connect in the background properly

Victor Orlikowski
In my experience, the existing code for re-connecting to a
disconnected server forces ERC into the foreground.

The following patch changes ERC so that reconnection attempts
proceed in the background, without interrupting the user's workflow.

From 7a64c15406225a14dca6863342da3d90569baf49 Mon Sep 17 00:00:00 2001
From: "Victor J. Orlikowski" <[hidden email]>
Date: Sun, 3 Feb 2019 12:32:12 -0500
Subject: [PATCH] Handle reconnection attempts in the background, rather than
 forcing to the foreground.

---
 lisp/erc/erc-backend.el | 38 +++++++++++++++++++++-----------------
 1 file changed, 21 insertions(+), 17 deletions(-)

diff --git a/lisp/erc/erc-backend.el b/lisp/erc/erc-backend.el
index 2fd3d454bf..57f34073be 100644
--- a/lisp/erc/erc-backend.el
+++ b/lisp/erc/erc-backend.el
@@ -613,11 +613,6 @@ Make sure you are in an ERC buffer when running this."
         (erc-open erc-session-server erc-session-port erc-server-current-nick
                   erc-session-user-full-name t erc-session-password)))))
 
-(defun erc-server-delayed-reconnect (buffer)
-  (if (buffer-live-p buffer)
-    (with-current-buffer buffer
-      (erc-server-reconnect))))
-
 (defun erc-server-filter-function (process string)
   "The process filter for the ERC server."
   (with-current-buffer (process-buffer process)
@@ -667,6 +662,25 @@ EVENT is the message received from the closed connection process."
               ;; open-network-stream-nowait error for connection refused
               (if (string-match "^failed with code 111" ,event) 'nonblocking t))))))
 
+(defun erc-server-background-reconnect (buffer)
+  (let ((process nil))
+    (condition-case err
+        (progn
+          (setq erc-server-reconnecting nil
+                erc-server-reconnect-count (1+ erc-server-reconnect-count))
+          (if (buffer-live-p buffer)
+              (with-current-buffer buffer
+                (setq process (get-buffer-process (erc-server-buffer)))
+                (erc-server-reconnect))))
+      (error (unless (integerp erc-server-reconnect-attempts)
+               (message "%s ... %s"
+                        "Reconnecting until we succeed"
+                        "kill the ERC server buffer to stop"))
+             (run-at-time erc-server-reconnect-timeout nil
+                          #'erc-server-background-reconnect buffer)))
+    (if process
+        (delete-process process))))
+
 (defun erc-process-sentinel-2 (event buffer)
   "Called when `erc-process-sentinel-1' has detected an unexpected disconnect."
   (if (not (buffer-live-p buffer))
@@ -684,18 +698,8 @@ EVENT is the message received from the closed connection process."
               (erc-update-mode-line)
               (set-buffer-modified-p nil))
           ;; reconnect
-          (condition-case nil
-              (progn
-                (setq erc-server-reconnecting   nil
-                      erc-server-reconnect-count (1+ erc-server-reconnect-count))
-                (setq delay erc-server-reconnect-timeout)
-                (run-at-time delay nil
-                             #'erc-server-delayed-reconnect buffer))
-            (error (unless (integerp erc-server-reconnect-attempts)
-                     (message "%s ... %s"
-                              "Reconnecting until we succeed"
-                              "kill the ERC server buffer to stop"))
-                   (erc-server-delayed-reconnect buffer))))))))
+          (run-at-time erc-server-reconnect-timeout nil
+                       #'erc-server-background-reconnect buffer))))))
 
 (defun erc-process-sentinel-1 (event buffer)
   "Called when `erc-process-sentinel' has decided that we're disconnecting.

Best,
Victor
--
Victor J. Orlikowski <> vjo@(ee.|cs.)?duke.edu

Reply | Threaded
Open this post in threaded view
|

Re: [PATCH] Allow ERC to re-connect in the background properly

Eli Zaretskii
> From: Victor Orlikowski <[hidden email]>
> Date: Sun, 3 Feb 2019 17:43:30 +0000
>
> In my experience, the existing code for re-connecting to a
> disconnected server forces ERC into the foreground.
>
> The following patch changes ERC so that reconnection attempts
> proceed in the background, without interrupting the user's workflow.

Thanks.  I don't use ERC and am not familiar with its code.  Would
someone who does please review this patch and comment on it?