|
@@ -81,18 +81,20 @@ func (handler *CommandHandler) Handle(roomID types.MatrixRoomID, user *User, mes
|
|
handler.CommandLogin(ce)
|
|
handler.CommandLogin(ce)
|
|
case "help":
|
|
case "help":
|
|
handler.CommandHelp(ce)
|
|
handler.CommandHelp(ce)
|
|
- case "logout", "reconnect", "disconnect", "sync", "list", "open", "pm":
|
|
|
|
|
|
+ case "reconnect":
|
|
|
|
+ handler.CommandReconnect(ce)
|
|
|
|
+ case "logout", "disconnect", "sync", "list", "open", "pm":
|
|
if ce.User.Conn == nil {
|
|
if ce.User.Conn == nil {
|
|
- ce.Reply("You are not logged in.")
|
|
|
|
- ce.Reply("Please use the login command to log into WhatsApp.")
|
|
|
|
|
|
+ ce.Reply("You are not logged in. Use the `login` command to log into WhatsApp.")
|
|
|
|
+ return
|
|
|
|
+ } else if !ce.User.Connected {
|
|
|
|
+ ce.Reply("You are not connected to WhatsApp. Use the `reconnect` command to reconnect.")
|
|
return
|
|
return
|
|
}
|
|
}
|
|
|
|
|
|
switch cmd {
|
|
switch cmd {
|
|
case "logout":
|
|
case "logout":
|
|
handler.CommandLogout(ce)
|
|
handler.CommandLogout(ce)
|
|
- case "reconnect":
|
|
|
|
- handler.CommandReconnect(ce)
|
|
|
|
case "disconnect":
|
|
case "disconnect":
|
|
handler.CommandDisconnect(ce)
|
|
handler.CommandDisconnect(ce)
|
|
case "sync":
|
|
case "sync":
|
|
@@ -130,7 +132,7 @@ func (handler *CommandHandler) CommandLogout(ce *CommandEvent) {
|
|
err := ce.User.Conn.Logout()
|
|
err := ce.User.Conn.Logout()
|
|
if err != nil {
|
|
if err != nil {
|
|
ce.User.log.Warnln("Error while logging out:", err)
|
|
ce.User.log.Warnln("Error while logging out:", err)
|
|
- ce.Reply("Error while logging out (see logs for details)")
|
|
|
|
|
|
+ ce.Reply("Unknown error while logging out: %v", err)
|
|
return
|
|
return
|
|
}
|
|
}
|
|
_, err = ce.User.Conn.Disconnect()
|
|
_, err = ce.User.Conn.Disconnect()
|
|
@@ -148,16 +150,33 @@ const cmdReconnectHelp = `reconnect - Reconnect to WhatsApp`
|
|
|
|
|
|
func (handler *CommandHandler) CommandReconnect(ce *CommandEvent) {
|
|
func (handler *CommandHandler) CommandReconnect(ce *CommandEvent) {
|
|
err := ce.User.Conn.Restore()
|
|
err := ce.User.Conn.Restore()
|
|
- if err == whatsapp.ErrAlreadyLoggedIn {
|
|
|
|
- if ce.User.Connected {
|
|
|
|
- ce.Reply("You were already connected.")
|
|
|
|
|
|
+ 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()...")
|
|
|
|
+ var sess whatsapp.Session
|
|
|
|
+ sess, err = ce.User.Conn.RestoreWithSession(*ce.User.Session)
|
|
|
|
+ if err == nil {
|
|
|
|
+ ce.User.SetSession(&sess)
|
|
|
|
+ }
|
|
} else {
|
|
} else {
|
|
- ce.User.Connected = true
|
|
|
|
- ce.Reply("You were already connected, but the bridge hadn't noticed. Fixed that now.")
|
|
|
|
|
|
+ ce.Reply("You are not logged in.")
|
|
|
|
+ return
|
|
}
|
|
}
|
|
- } else if err != nil {
|
|
|
|
|
|
+ }
|
|
|
|
+ if err != nil {
|
|
ce.User.log.Warnln("Error while reconnecting:", err)
|
|
ce.User.log.Warnln("Error while reconnecting:", err)
|
|
- ce.Reply("Error while reconnecting (see logs for details)")
|
|
|
|
|
|
+ if err == whatsapp.ErrAlreadyLoggedIn {
|
|
|
|
+ if ce.User.Connected {
|
|
|
|
+ ce.Reply("You were already connected.")
|
|
|
|
+ } else {
|
|
|
|
+ ce.User.Connected = true
|
|
|
|
+ ce.Reply("You were already connected, but the bridge hadn't noticed. Fixed that now.")
|
|
|
|
+ }
|
|
|
|
+ } else 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)
|
|
|
|
+ }
|
|
return
|
|
return
|
|
}
|
|
}
|
|
ce.User.Connected = true
|
|
ce.User.Connected = true
|
|
@@ -174,7 +193,7 @@ func (handler *CommandHandler) CommandDisconnect(ce *CommandEvent) {
|
|
return
|
|
return
|
|
} else if err != nil {
|
|
} else if err != nil {
|
|
ce.User.log.Warnln("Error while disconnecting:", err)
|
|
ce.User.log.Warnln("Error while disconnecting:", err)
|
|
- ce.Reply("Error while disconnecting (see logs for details)")
|
|
|
|
|
|
+ ce.Reply("Unknown error while disconnecting: %v", err)
|
|
return
|
|
return
|
|
}
|
|
}
|
|
ce.User.SetSession(&sess)
|
|
ce.User.SetSession(&sess)
|