From a932911c56346b6f75ac406f7d406110949867af Mon Sep 17 00:00:00 2001 From: Remi Reuvekamp Date: Wed, 11 Mar 2020 16:09:31 +0100 Subject: [PATCH 1/2] Fix #142 Don't disconnect when trying to reconnect and receiving a ErrAlreadyLoggedIn as a result. --- commands.go | 7 ++++--- provisioning.go | 13 +++++++------ 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/commands.go b/commands.go index 3d326ec..252248c 100644 --- a/commands.go +++ b/commands.go @@ -247,12 +247,13 @@ func (handler *CommandHandler) CommandReconnect(ce *CommandEvent) { } else if err == whatsapp.ErrLoginInProgress { ce.Reply("A login or reconnection is already in progress.") return + } else if err == whatsapp.ErrAlreadyLoggedIn { + ce.Reply("You were already connected.") + return } if err != nil { ce.User.log.Warnln("Error while reconnecting:", err) - if err == whatsapp.ErrAlreadyLoggedIn { - ce.Reply("You were already connected.") - } else if err.Error() == "restore session connection timed out" { + if err.Error() == "restore session connection timed out" { ce.Reply("Reconnection timed out. Is WhatsApp on your phone reachable?") } else { ce.Reply("Unknown error while reconnecting: %v", err) diff --git a/provisioning.go b/provisioning.go index 19d68ce..51fbb97 100644 --- a/provisioning.go +++ b/provisioning.go @@ -178,15 +178,16 @@ func (prov *ProvisioningAPI) Reconnect(w http.ResponseWriter, r *http.Request) { ErrCode: "login in progress", }) return + } else if err == whatsapp.ErrAlreadyLoggedIn { + jsonResponse(w, http.StatusConflict, Error{ + Error: "You were already connected.", + ErrCode: err.Error(), + }) + return } if err != nil { user.log.Warnln("Error while reconnecting:", err) - if err == whatsapp.ErrAlreadyLoggedIn { - jsonResponse(w, http.StatusConflict, Error{ - Error: "You were already connected.", - ErrCode: err.Error(), - }) - } else if err.Error() == "restore session connection timed out" { + if err.Error() == "restore session connection timed out" { jsonResponse(w, http.StatusForbidden, Error{ Error: "Reconnection timed out. Is WhatsApp on your phone reachable?", ErrCode: err.Error(), From 46b784ca3d48df6f209ed6d8f32b225a27806a70 Mon Sep 17 00:00:00 2001 From: Remi Reuvekamp Date: Wed, 11 Mar 2020 17:21:51 +0100 Subject: [PATCH 2/2] Disconnect before reconnecting --- commands.go | 22 ++++++++++++++++++++-- provisioning.go | 23 +++++++++++++++++++++-- 2 files changed, 41 insertions(+), 4 deletions(-) diff --git a/commands.go b/commands.go index 252248c..ac4ddbb 100644 --- a/commands.go +++ b/commands.go @@ -231,7 +231,18 @@ func (handler *CommandHandler) CommandReconnect(ce *CommandEvent) { } return } - err := ce.User.Conn.Restore() + + wasConnected := true + sess, err := ce.User.Conn.Disconnect() + if err == whatsapp.ErrNotConnected { + wasConnected = false + } else if err != nil { + ce.User.log.Warnln("Error while disconnecting:", err) + } else if len(sess.Wid) > 0 { + ce.User.SetSession(&sess) + } + + err = ce.User.Conn.Restore() if err == whatsapp.ErrInvalidSession { if ce.User.Session != nil { ce.User.log.Debugln("Got invalid session error when reconnecting, but user has session. Retrying using RestoreWithSession()...") @@ -268,7 +279,14 @@ func (handler *CommandHandler) CommandReconnect(ce *CommandEvent) { return } ce.User.ConnectionErrors = 0 - ce.Reply("Reconnected successfully.") + + var msg string + if wasConnected { + msg = "Reconnected successfully." + } else { + msg = "Connected successfully." + } + ce.Reply(msg) ce.User.PostLogin() } diff --git a/provisioning.go b/provisioning.go index 51fbb97..0b17666 100644 --- a/provisioning.go +++ b/provisioning.go @@ -156,7 +156,18 @@ func (prov *ProvisioningAPI) Reconnect(w http.ResponseWriter, r *http.Request) { } return } - err := user.Conn.Restore() + + wasConnected := true + sess, err := user.Conn.Disconnect() + if err == whatsapp.ErrNotConnected { + wasConnected = false + } else if err != nil { + user.log.Warnln("Error while disconnecting:", err) + } else if len(sess.Wid) > 0 { + user.SetSession(&sess) + } + + err = user.Conn.Restore() if err == whatsapp.ErrInvalidSession { if user.Session != nil { user.log.Debugln("Got invalid session error when reconnecting, but user has session. Retrying using RestoreWithSession()...") @@ -209,7 +220,15 @@ func (prov *ProvisioningAPI) Reconnect(w http.ResponseWriter, r *http.Request) { } user.ConnectionErrors = 0 user.PostLogin() - jsonResponse(w, http.StatusOK, Response{true, "Reconnected successfully."}) + + var msg string + if wasConnected { + msg = "Reconnected successfully." + } else { + msg = "Connected successfully." + } + + jsonResponse(w, http.StatusOK, Response{true, msg}) } func (prov *ProvisioningAPI) Ping(w http.ResponseWriter, r *http.Request) {