Improve edge case handling in login command by not assuming connection status

This commit is contained in:
Tulir Asokan 2019-05-15 23:17:09 +03:00
parent 6b73c66e12
commit adbdf55926
2 changed files with 25 additions and 20 deletions

View file

@ -113,13 +113,10 @@ const cmdLoginHelp = `login - Authenticate this Bridge as WhatsApp Web Client`
// CommandLogin handles login command // CommandLogin handles login command
func (handler *CommandHandler) CommandLogin(ce *CommandEvent) { func (handler *CommandHandler) CommandLogin(ce *CommandEvent) {
if ce.User.Session != nil { if ce.User.Conn == nil {
ce.Reply("You're already logged in.") ce.User.Connect(true)
return
} }
ce.User.Login(ce)
ce.User.Connect(true)
ce.User.Login(ce.RoomID)
} }
const cmdLogoutHelp = `logout - Logout from WhatsApp` const cmdLogoutHelp = `logout - Logout from WhatsApp`
@ -136,6 +133,11 @@ func (handler *CommandHandler) CommandLogout(ce *CommandEvent) {
ce.Reply("Error while logging out (see logs for details)") ce.Reply("Error while logging out (see logs for details)")
return return
} }
_, err = ce.User.Conn.Disconnect()
if err != nil {
ce.User.log.Warnln("Error while disconnecting after logout:", err)
}
ce.User.Connected = false
ce.User.Conn = nil ce.User.Conn = nil
ce.User.Session = nil ce.User.Session = nil
ce.User.Update() ce.User.Update()

31
user.go
View file

@ -176,9 +176,7 @@ func (user *User) IsLoggedIn() bool {
return user.Conn != nil return user.Conn != nil
} }
func (user *User) Login(roomID types.MatrixRoomID) { func (user *User) Login(ce *CommandEvent) {
bot := user.bridge.AS.BotClient()
qrChan := make(chan string, 2) qrChan := make(chan string, 2)
go func() { go func() {
code := <-qrChan code := <-qrChan
@ -188,37 +186,42 @@ func (user *User) Login(roomID types.MatrixRoomID) {
qrCode, err := qrcode.Encode(code, qrcode.Low, 256) qrCode, err := qrcode.Encode(code, qrcode.Low, 256)
if err != nil { if err != nil {
user.log.Errorln("Failed to encode QR code:", err) user.log.Errorln("Failed to encode QR code:", err)
_, _ = bot.SendNotice(roomID, "Failed to encode QR code (see logs for details)") ce.Reply("Failed to encode QR code (see logs for details)")
return return
} }
bot := user.bridge.AS.BotClient()
resp, err := bot.UploadBytes(qrCode, "image/png") resp, err := bot.UploadBytes(qrCode, "image/png")
if err != nil { if err != nil {
user.log.Errorln("Failed to upload QR code:", err) user.log.Errorln("Failed to upload QR code:", err)
_, _ = bot.SendNotice(roomID, "Failed to upload QR code (see logs for details)") ce.Reply("Failed to upload QR code (see logs for details)")
return return
} }
_, err = bot.SendImage(roomID, string(code), resp.ContentURI) _, err = bot.SendImage(ce.RoomID, string(code), resp.ContentURI)
if err != nil { if err != nil {
user.log.Errorln("Failed to send QR code to user:", err) user.log.Errorln("Failed to send QR code to user:", err)
} }
}() }()
session, err := user.Conn.Login(qrChan) session, err := user.Conn.Login(qrChan)
if err != nil { if err != nil {
user.log.Warnln("Failed to log in:", err)
_, _ = bot.SendNotice(roomID, "Failed to log in: "+err.Error())
qrChan <- "error" qrChan <- "error"
if err == whatsapp.ErrAlreadyLoggedIn {
ce.Reply("You're already logged in.")
} else if err == whatsapp.ErrLoginInProgress {
ce.Reply("You have a login in progress already.")
} else {
user.log.Warnln("Failed to log in:", err)
ce.Reply("Failed to log in (see logs for details)")
}
return return
} }
user.Connected = true user.Connected = true
user.JID = strings.Replace(user.Conn.Info.Wid, whatsappExt.OldUserSuffix, whatsappExt.NewUserSuffix, 1) user.JID = strings.Replace(user.Conn.Info.Wid, whatsappExt.OldUserSuffix, whatsappExt.NewUserSuffix, 1)
user.Session = &session user.Session = &session
user.Update() user.Update()
_, err = bot.SendNotice(roomID, "Successfully logged in. Now, you may ask for `sync [--create]`.") ce.Reply("Successfully logged in. Now, you may ask for `sync [--create]`.")
if err != nil {
user.log.Warnln("Failed to send login confirmation to user:", err)
}
} }
func (user *User) HandleError(err error) { func (user *User) HandleError(err error) {
@ -226,7 +229,7 @@ func (user *User) HandleError(err error) {
closed, ok := err.(*whatsapp.ErrConnectionClosed) closed, ok := err.(*whatsapp.ErrConnectionClosed)
if ok { if ok {
if closed.Code != 1000 { if closed.Code != 1000 {
msg := fmt.Sprintf("⚠️\u26a0 Your WhatsApp connection failed with websocket status code %d.\n\n" + msg := fmt.Sprintf("\u26a0 Your WhatsApp connection failed with websocket status code %d.\n\n"+
"Use the `reconnect` command to reconnect.", closed.Code) "Use the `reconnect` command to reconnect.", closed.Code)
_, _ = user.bridge.Bot.SendMessageEvent(user.ManagementRoom, mautrix.EventMessage, format.RenderMarkdown(msg)) _, _ = user.bridge.Bot.SendMessageEvent(user.ManagementRoom, mautrix.EventMessage, format.RenderMarkdown(msg))
} }
@ -335,7 +338,7 @@ func (user *User) HandleCommand(cmd whatsappExt.Command) {
msg = "\u26a0 Your WhatsApp connection was closed by the server because you opened another WhatsApp Web client.\n\n" + msg = "\u26a0 Your WhatsApp connection was closed by the server because you opened another WhatsApp Web client.\n\n" +
"Use the `reconnect` command to disconnect the other client and resume bridging." "Use the `reconnect` command to disconnect the other client and resume bridging."
} else { } else {
msg = fmt.Sprintf("\u26a0 Your WhatsApp connection was closed by the server (reason code: %s).\n\n" + msg = fmt.Sprintf("\u26a0 Your WhatsApp connection was closed by the server (reason code: %s).\n\n"+
"Use the `reconnect` command to reconnect.", cmd.Kind) "Use the `reconnect` command to reconnect.", cmd.Kind)
} }
_, _ = user.bridge.Bot.SendMessageEvent(user.ManagementRoom, mautrix.EventMessage, format.RenderMarkdown(msg)) _, _ = user.bridge.Bot.SendMessageEvent(user.ManagementRoom, mautrix.EventMessage, format.RenderMarkdown(msg))