Add command to delete session information to force logout when the bridge gets stuck

This commit is contained in:
Tulir Asokan 2019-05-16 18:00:46 +03:00
parent c1e1964fc5
commit 5d0edda04a
3 changed files with 25 additions and 3 deletions

View file

@ -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,

View file

@ -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)
}

View file

@ -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)