From 37c1db2ab22b892cc41c8d663a359fc6ebe12e31 Mon Sep 17 00:00:00 2001
From: Moritz Poldrack <git@moritz.sh>
Date: Tue, 13 Sep 2022 13:50:35 +0200
Subject: [PATCH] socket: use existing socket if it is connected
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

When using ephemeral aerc sessions – for example while writing patches
for it – the mailto: functionality breaks if the socket is removed.

Try to send a ping (an empty message) to the socket and remove it, if
the send fails or no noop reply is received.

Signed-off-by: Moritz Poldrack <git@moritz.sh>
Acked-by: Tim Culverhouse <tim@timculverhouse.com>
---
 lib/socket.go | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/lib/socket.go b/lib/socket.go
index c83cc8a..4da3485 100644
--- a/lib/socket.go
+++ b/lib/socket.go
@@ -24,8 +24,10 @@ type AercServer struct {
 
 func StartServer() (*AercServer, error) {
 	sockpath := path.Join(xdg.RuntimeDir(), "aerc.sock")
-	// remove the socket if it already exists
-	os.Remove(sockpath)
+	// remove the socket if it is not connected to a session
+	if err := ConnectAndExec(""); err != nil {
+		os.Remove(sockpath)
+	}
 	logging.Infof("Starting Unix server: %s", sockpath)
 	l, err := net.Listen("unix", sockpath)
 	if err != nil {