diff --git a/commands.go b/commands.go index 6b845a0..b006e99 100644 --- a/commands.go +++ b/commands.go @@ -83,6 +83,8 @@ func (handler *CommandHandler) Handle(roomID types.MatrixRoomID, user *User, mes handler.CommandHelp(ce) case "reconnect": handler.CommandReconnect(ce) + case "delete-session": + handler.CommandDeleteSession(ce) case "logout", "disconnect", "sync", "list", "open", "pm": if ce.User.Conn == nil { ce.Reply("You are not logged in. Use the `login` command to log into WhatsApp.") @@ -141,11 +143,26 @@ func (handler *CommandHandler) CommandLogout(ce *CommandEvent) { } ce.User.Connected = false ce.User.Conn = nil - ce.User.Session = nil - ce.User.Update() + ce.User.SetSession(nil) ce.Reply("Logged out successfully.") } +const cmdDeleteSessionHelp = `delete-session - Delete session information and disconnect from WhatsApp without sending a logout request` + +func (handler *CommandHandler) CommandDeleteSession(ce *CommandEvent) { + if ce.User.Session == nil && !ce.User.Connected && ce.User.Conn == nil { + ce.Reply("Nothing to purge: no session information stored and no active connection.") + return + } + ce.User.SetSession(nil) + ce.User.Connected = false + if ce.User.Conn != nil { + _, _ = ce.User.Conn.Disconnect() + ce.User.Conn = nil + } + ce.Reply("Session information purged") +} + const cmdReconnectHelp = `reconnect - Reconnect to WhatsApp` func (handler *CommandHandler) CommandReconnect(ce *CommandEvent) { @@ -213,6 +230,7 @@ func (handler *CommandHandler) CommandHelp(ce *CommandEvent) { cmdPrefix + cmdHelpHelp, cmdPrefix + cmdLoginHelp, cmdPrefix + cmdLogoutHelp, + cmdPrefix + cmdDeleteSessionHelp, cmdPrefix + cmdReconnectHelp, cmdPrefix + cmdDisconnectHelp, cmdPrefix + cmdSyncHelp, diff --git a/user.go b/user.go index 30d0e9e..8891eb3 100644 --- a/user.go +++ b/user.go @@ -219,7 +219,7 @@ func (user *User) Login(ce *CommandEvent) { ce.Reply("QR code scan timed out. Please try again.") } else { user.log.Warnln("Failed to log in:", err) - ce.Reply("Failed to log in: %v", err) + ce.Reply("Unknown error while logging in: %v", err) } return } @@ -355,6 +355,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" + "Use the `reconnect` command to disconnect the other client and resume bridging." } else { + user.log.Warnln("Unknown kind of disconnect:", string(cmd.Raw)) 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) } diff --git a/whatsapp-ext/cmd.go b/whatsapp-ext/cmd.go index 15d64ee..debc31b 100644 --- a/whatsapp-ext/cmd.go +++ b/whatsapp-ext/cmd.go @@ -36,6 +36,8 @@ type Command struct { *ProfilePicInfo Kind string `json:"kind"` + + Raw json.RawMessage `json:"-"` } type CommandHandler interface { @@ -50,6 +52,7 @@ func (ext *ExtendedConn) handleMessageCommand(message []byte) { ext.jsonParseError(err) return } + event.Raw = message event.JID = strings.Replace(event.JID, OldUserSuffix, NewUserSuffix, 1) for _, handler := range ext.handlers { commandHandler, ok := handler.(CommandHandler)