From 151141f288e51e427168cee1340a7163bf58309a Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Thu, 10 May 2018 15:47:24 +0300 Subject: Add command to log out --- config/config.go | 11 +++++++++++ interface/matrix.go | 1 + matrix/matrix.go | 22 +++++++++++++++------- ui/ui.go | 2 ++ ui/view-main.go | 4 ++++ 5 files changed, 33 insertions(+), 7 deletions(-) diff --git a/config/config.go b/config/config.go index b025115..cf080ff 100644 --- a/config/config.go +++ b/config/config.go @@ -55,6 +55,17 @@ func (config *Config) Clear() { os.RemoveAll(config.MediaDir) } +func (config *Config) DeleteSession() { + if config.Session != nil { + os.Remove(config.Session.path) + config.Session = nil + } + os.RemoveAll(config.HistoryDir) + os.RemoveAll(config.MediaDir) + os.MkdirAll(config.HistoryDir, 0700) + os.MkdirAll(config.MediaDir, 0700) +} + // Load loads the config from config.yaml in the directory given to the config struct. func (config *Config) Load() { os.MkdirAll(config.Dir, 0700) diff --git a/interface/matrix.go b/interface/matrix.go index 40710ac..73d9ec9 100644 --- a/interface/matrix.go +++ b/interface/matrix.go @@ -30,6 +30,7 @@ type MatrixContainer interface { Stop() Login(user, password string) error + Logout() SendMessage(roomID, msgtype, message string) (string, error) SendMarkdownMessage(roomID, msgtype, message string) (string, error) diff --git a/matrix/matrix.go b/matrix/matrix.go index b43a858..4609876 100644 --- a/matrix/matrix.go +++ b/matrix/matrix.go @@ -131,6 +131,15 @@ func (c *Container) Login(user, password string) error { return nil } +// Logout revokes the access token, stops the syncer and calls the OnLogout() method of the UI. +func (c *Container) Logout() { + c.client.Logout() + c.config.DeleteSession() + c.Stop() + c.client = nil + c.ui.OnLogout() +} + // Stop stops the Matrix syncer. func (c *Container) Stop() { if c.running { @@ -158,12 +167,6 @@ func (c *Container) PushRules() *pushrules.PushRuleset { return c.config.Session.PushRules } -// OnLogout stops the syncer and moves the UI back to the login view. -func (c *Container) OnLogout() { - c.ui.OnLogout() - c.Stop() -} - // OnLogin initializes the syncer and updates the room list. func (c *Container) OnLogin() { c.ui.OnLogin() @@ -209,7 +212,12 @@ func (c *Container) Start() { return default: if err := c.client.Sync(); err != nil { - debug.Print("Sync() errored", err) + if httpErr, ok := err.(gomatrix.HTTPError); ok && httpErr.Code == http.StatusUnauthorized { + debug.Print("Sync() errored with ", err, " -> logging out") + c.Logout() + } else { + debug.Print("Sync() errored", err) + } } else { debug.Print("Sync() returned without error") } diff --git a/ui/ui.go b/ui/ui.go index 2f45f27..5e06d16 100644 --- a/ui/ui.go +++ b/ui/ui.go @@ -78,10 +78,12 @@ func (ui *GomuksUI) Render() { func (ui *GomuksUI) OnLogin() { ui.SetView(ViewMain) + ui.app.SetFocus(ui.mainView) } func (ui *GomuksUI) OnLogout() { ui.SetView(ViewLogin) + ui.app.SetFocus(ui.loginView) } func (ui *GomuksUI) SetView(name View) { diff --git a/ui/view-main.go b/ui/view-main.go index 7a94194..ac7f3d2 100644 --- a/ui/view-main.go +++ b/ui/view-main.go @@ -119,6 +119,7 @@ func (view *MainView) SendMessage(roomView *RoomView, text string) { func (view *MainView) sendTempMessage(roomView *RoomView, tempMessage ifc.Message, text string) { defer debug.Recover() + debug.Print("Sending message", tempMessage.Type(), text) eventID, err := view.matrix.SendMarkdownMessage(roomView.Room.ID, tempMessage.Type(), text) if err != nil { tempMessage.SetState(ifc.MessageStateFailed) @@ -131,6 +132,7 @@ func (view *MainView) sendTempMessage(roomView *RoomView, tempMessage ifc.Messag roomView.AddServiceMessage(fmt.Sprintf("Failed to send message: %v", err)) view.parent.Render() } else { + debug.Print("Event ID received:", eventID) roomView.MessageView().UpdateMessageID(tempMessage, eventID) } } @@ -167,6 +169,8 @@ func (view *MainView) HandleCommand(roomView *RoomView, command string, args []s if err == nil { view.AddRoom(room) } + case "/logout": + view.matrix.Logout() default: roomView.AddServiceMessage("Unknown command.") } -- cgit v1.2.3