gomuks/ui/view-login.go

195 lines
5.5 KiB
Go
Raw Normal View History

2018-03-13 20:58:43 +01:00
// gomuks - A terminal Matrix client written in Go.
2020-04-19 17:10:14 +02:00
// Copyright (C) 2020 Tulir Asokan
2018-03-13 20:58:43 +01:00
//
// This program is free software: you can redistribute it and/or modify
2019-01-17 13:13:25 +01:00
// it under the terms of the GNU Affero General Public License as published by
2018-03-13 20:58:43 +01:00
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2019-01-17 13:13:25 +01:00
// GNU Affero General Public License for more details.
2018-03-13 20:58:43 +01:00
//
2019-01-17 13:13:25 +01:00
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
2018-03-13 20:58:43 +01:00
2018-03-18 20:24:03 +01:00
package ui
2018-03-13 20:58:43 +01:00
import (
2020-02-19 21:13:02 +01:00
"math"
2022-04-17 22:54:40 +02:00
"github.com/mattn/go-runewidth"
2022-04-15 11:53:09 +02:00
"go.mau.fi/mauview"
"go.mau.fi/tcell"
2019-03-25 23:37:35 +01:00
2019-01-17 13:13:25 +01:00
"maunium.net/go/mautrix"
2022-04-15 11:53:09 +02:00
"maunium.net/go/mautrix/id"
2019-01-17 13:13:25 +01:00
2018-03-22 22:03:45 +01:00
"maunium.net/go/gomuks/config"
"maunium.net/go/gomuks/debug"
2022-04-15 11:53:09 +02:00
ifc "maunium.net/go/gomuks/interface"
2018-03-13 20:58:43 +01:00
)
2018-03-22 22:03:45 +01:00
type LoginView struct {
2019-03-25 23:37:35 +01:00
*mauview.Form
container *mauview.Centerer
homeserverLabel *mauview.TextField
usernameLabel *mauview.TextField
passwordLabel *mauview.TextField
2018-03-22 22:03:45 +01:00
2019-03-25 23:37:35 +01:00
homeserver *mauview.InputField
username *mauview.InputField
password *mauview.InputField
2020-02-19 21:13:02 +01:00
error *mauview.TextView
2019-03-25 23:37:35 +01:00
loginButton *mauview.Button
quitButton *mauview.Button
2018-03-22 22:03:45 +01:00
loading bool
2018-03-22 22:03:45 +01:00
matrix ifc.MatrixContainer
config *config.Config
parent *GomuksUI
}
2019-03-25 23:37:35 +01:00
func (ui *GomuksUI) NewLoginView() mauview.Component {
2018-03-22 22:03:45 +01:00
view := &LoginView{
2019-03-25 23:37:35 +01:00
Form: mauview.NewForm(),
usernameLabel: mauview.NewTextField().SetText("Username"),
passwordLabel: mauview.NewTextField().SetText("Password"),
homeserverLabel: mauview.NewTextField().SetText("Homeserver"),
2018-03-22 22:03:45 +01:00
2019-03-25 23:37:35 +01:00
username: mauview.NewInputField(),
password: mauview.NewInputField(),
homeserver: mauview.NewInputField(),
loginButton: mauview.NewButton("Login"),
quitButton: mauview.NewButton("Quit"),
2018-03-22 22:03:45 +01:00
2018-03-23 13:44:36 +01:00
matrix: ui.gmx.Matrix(),
2018-03-22 22:03:45 +01:00
config: ui.gmx.Config(),
parent: ui,
}
2019-03-25 23:37:35 +01:00
2018-03-18 20:24:03 +01:00
hs := ui.gmx.Config().HS
view.homeserver.SetPlaceholder("https://example.com").SetText(hs).SetTextColor(tcell.ColorWhite)
view.username.SetPlaceholder("@user:example.com").SetText(string(ui.gmx.Config().UserID)).SetTextColor(tcell.ColorWhite)
view.password.SetPlaceholder("correct horse battery staple").SetMaskCharacter('*').SetTextColor(tcell.ColorWhite)
view.quitButton.
SetOnClick(func() { ui.gmx.Stop(true) }).
SetBackgroundColor(tcell.ColorDarkCyan).
SetForegroundColor(tcell.ColorWhite).
SetFocusedForegroundColor(tcell.ColorWhite)
view.loginButton.
SetOnClick(view.Login).
SetBackgroundColor(tcell.ColorDarkCyan).
SetForegroundColor(tcell.ColorWhite).
SetFocusedForegroundColor(tcell.ColorWhite)
2018-03-22 22:03:45 +01:00
view.
SetColumns([]int{1, 10, 1, 30, 1}).
SetRows([]int{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1})
view.
AddFormItem(view.username, 3, 1, 1, 1).
AddFormItem(view.password, 3, 3, 1, 1).
AddFormItem(view.homeserver, 3, 5, 1, 1).
AddFormItem(view.loginButton, 1, 7, 3, 1).
AddFormItem(view.quitButton, 1, 9, 3, 1).
2019-03-25 23:37:35 +01:00
AddComponent(view.usernameLabel, 1, 1, 1, 1).
AddComponent(view.passwordLabel, 1, 3, 1, 1).
AddComponent(view.homeserverLabel, 1, 5, 1, 1)
view.SetOnFocusChanged(view.focusChanged)
view.FocusNextItem()
2018-03-22 22:03:45 +01:00
ui.loginView = view
view.container = mauview.Center(mauview.NewBox(view).SetTitle("Log in to Matrix"), 45, 13)
view.container.SetAlwaysFocusChild(true)
2019-03-25 23:37:35 +01:00
return view.container
2018-04-23 11:55:38 +02:00
}
func (view *LoginView) resolveWellKnown() {
_, homeserver, err := id.UserID(view.username.GetText()).Parse()
if err != nil {
return
}
view.homeserver.SetText("Resolving...")
resp, err := mautrix.DiscoverClientAPI(homeserver)
if err != nil {
view.homeserver.SetText("")
view.Error(err.Error())
} else if resp != nil {
view.homeserver.SetText(resp.Homeserver.BaseURL)
view.parent.Render()
}
}
func (view *LoginView) focusChanged(from, to mauview.Component) {
if from == view.username && view.homeserver.GetText() == "" {
go view.resolveWellKnown()
}
}
2018-04-23 11:55:38 +02:00
func (view *LoginView) Error(err string) {
2020-02-19 21:13:02 +01:00
if len(err) == 0 && view.error != nil {
2019-03-25 23:37:35 +01:00
debug.Print("Hiding error")
view.RemoveComponent(view.error)
view.container.SetHeight(13)
2020-02-19 21:13:02 +01:00
view.SetRows([]int{1, 1, 1, 1, 1, 1, 1, 1, 1})
2019-03-25 23:37:35 +01:00
view.error = nil
2020-02-19 21:13:02 +01:00
} else if len(err) > 0 {
debug.Print("Showing error", err)
if view.error == nil {
view.error = mauview.NewTextView().SetTextColor(tcell.ColorRed)
view.AddComponent(view.error, 1, 11, 3, 1)
2020-02-19 21:13:02 +01:00
}
view.error.SetText(err)
errorHeight := int(math.Ceil(float64(runewidth.StringWidth(err)) / 41))
view.container.SetHeight(14 + errorHeight)
view.SetRow(11, errorHeight)
2018-04-23 11:55:38 +02:00
}
2019-03-25 23:37:35 +01:00
view.parent.Render()
2018-03-13 20:58:43 +01:00
}
func (view *LoginView) actuallyLogin(hs, mxid, password string) {
2018-03-18 20:24:03 +01:00
debug.Printf("Logging into %s as %s...", hs, mxid)
2018-03-22 22:03:45 +01:00
view.config.HS = hs
if err := view.matrix.InitClient(false); err != nil {
2020-02-19 21:12:12 +01:00
debug.Print("Init error:", err)
view.Error(err.Error())
} else if err = view.matrix.Login(mxid, password); err != nil {
2018-11-13 23:00:35 +01:00
if httpErr, ok := err.(mautrix.HTTPError); ok {
2019-03-25 23:37:35 +01:00
if httpErr.RespError != nil {
view.Error(httpErr.RespError.Err)
2018-04-23 11:55:38 +02:00
} else {
view.Error(httpErr.Message)
}
} else {
view.Error(err.Error())
2018-04-23 11:55:38 +02:00
}
debug.Print("Login error:", err)
}
view.loading = false
view.loginButton.SetText("Login")
}
func (view *LoginView) Login() {
if view.loading {
return
}
hs := view.homeserver.GetText()
mxid := view.username.GetText()
password := view.password.GetText()
view.loading = true
view.loginButton.SetText("Logging in...")
go view.actuallyLogin(hs, mxid, password)
2018-03-13 20:58:43 +01:00
}