2018-03-13 20:58:43 +01:00
|
|
|
// gomuks - A terminal Matrix client written in Go.
|
2019-01-17 13:13:25 +01:00
|
|
|
// Copyright (C) 2019 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 (
|
2019-03-25 23:37:35 +01:00
|
|
|
"maunium.net/go/tcell"
|
|
|
|
|
2019-01-17 13:13:25 +01:00
|
|
|
"maunium.net/go/mautrix"
|
2019-03-25 23:37:35 +01:00
|
|
|
"maunium.net/go/mauview"
|
2019-01-17 13:13:25 +01:00
|
|
|
|
2018-03-22 22:03:45 +01:00
|
|
|
"maunium.net/go/gomuks/config"
|
2018-04-09 22:45:54 +02:00
|
|
|
"maunium.net/go/gomuks/debug"
|
2018-04-19 10:10:34 +02:00
|
|
|
"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
|
|
|
|
error *mauview.TextField
|
|
|
|
|
|
|
|
loginButton *mauview.Button
|
|
|
|
quitButton *mauview.Button
|
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
|
2019-03-25 23:37:35 +01:00
|
|
|
view.homeserver.SetText(hs)
|
|
|
|
view.username.SetText(ui.gmx.Config().UserID)
|
|
|
|
view.password.SetMaskCharacter('*')
|
2018-03-14 21:19:26 +01:00
|
|
|
|
2019-06-15 00:11:51 +02:00
|
|
|
view.quitButton.SetOnClick(func() { ui.gmx.Stop(true) }).SetBackgroundColor(tcell.ColorDarkCyan)
|
2019-04-07 02:22:51 +02:00
|
|
|
view.loginButton.SetOnClick(view.Login).SetBackgroundColor(tcell.ColorDarkCyan)
|
2018-03-22 22:03:45 +01:00
|
|
|
|
2019-03-25 23:37:35 +01:00
|
|
|
view.SetColumns([]int{1, 10, 1, 9, 1, 9, 1, 10, 1})
|
|
|
|
view.SetRows([]int{1, 1, 1, 1, 1, 1, 1, 1, 1, 1})
|
|
|
|
view.AddFormItem(view.username, 3, 1, 5, 1).
|
|
|
|
AddFormItem(view.password, 3, 3, 5, 1).
|
|
|
|
AddFormItem(view.homeserver, 3, 5, 5, 1).
|
|
|
|
AddFormItem(view.loginButton, 5, 7, 3, 1).
|
|
|
|
AddFormItem(view.quitButton, 1, 7, 3, 1).
|
|
|
|
AddComponent(view.usernameLabel, 1, 1, 1, 1).
|
|
|
|
AddComponent(view.passwordLabel, 1, 3, 1, 1).
|
|
|
|
AddComponent(view.homeserverLabel, 1, 5, 1, 1)
|
2019-04-06 17:03:37 +02:00
|
|
|
view.FocusNextItem()
|
2018-03-22 22:03:45 +01:00
|
|
|
ui.loginView = view
|
|
|
|
|
2019-03-25 23:37:35 +01:00
|
|
|
view.container = mauview.Center(mauview.NewBox(view).SetTitle("Log in to Matrix"), 45, 13)
|
2019-04-06 17:03:37 +02:00
|
|
|
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) Error(err string) {
|
2019-03-25 23:37:35 +01:00
|
|
|
if len(err) == 0 {
|
|
|
|
debug.Print("Hiding error")
|
|
|
|
view.RemoveComponent(view.error)
|
|
|
|
view.error = nil
|
|
|
|
return
|
|
|
|
}
|
|
|
|
debug.Print("Showing error", err)
|
2018-04-23 11:55:38 +02:00
|
|
|
if view.error == nil {
|
2019-03-25 23:37:35 +01:00
|
|
|
view.error = mauview.NewTextField().SetTextColor(tcell.ColorRed)
|
|
|
|
view.AddComponent(view.error, 1, 9, 7, 1)
|
2018-04-23 11:55:38 +02:00
|
|
|
}
|
|
|
|
view.error.SetText(err)
|
2019-03-25 23:37:35 +01:00
|
|
|
|
|
|
|
view.parent.Render()
|
2018-03-13 20:58:43 +01:00
|
|
|
}
|
|
|
|
|
2018-03-22 22:03:45 +01:00
|
|
|
func (view *LoginView) Login() {
|
|
|
|
hs := view.homeserver.GetText()
|
|
|
|
mxid := view.username.GetText()
|
|
|
|
password := view.password.GetText()
|
2018-03-13 20:58:43 +01:00
|
|
|
|
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
|
2018-04-23 11:55:38 +02:00
|
|
|
err := view.matrix.InitClient()
|
|
|
|
debug.Print("Init error:", err)
|
|
|
|
err = view.matrix.Login(mxid, password)
|
|
|
|
if 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("Failed to connect to server.")
|
|
|
|
}
|
|
|
|
debug.Print("Login error:", err)
|
|
|
|
}
|
2018-03-13 20:58:43 +01:00
|
|
|
}
|