| 1 |
// gomuks - A terminal Matrix client written in Go. |
| 2 |
// Copyright (C) 2020 Tulir Asokan |
| 3 |
// |
| 4 |
// This program is free software: you can redistribute it and/or modify |
| 5 |
// it under the terms of the GNU Affero General Public License as published by |
| 6 |
// the Free Software Foundation, either version 3 of the License, or |
| 7 |
// (at your option) any later version. |
| 8 |
// |
| 9 |
// This program is distributed in the hope that it will be useful, |
| 10 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
// GNU Affero General Public License for more details. |
| 13 |
// |
| 14 |
// You should have received a copy of the GNU Affero General Public License |
| 15 |
// along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 16 |
|
| 17 |
package ui |
| 18 |
|
| 19 |
import ( |
| 20 |
"math" |
| 21 |
|
| 22 |
"github.com/mattn/go-runewidth" |
| 23 |
|
| 24 |
"go.mau.fi/mauview" |
| •25 |
"github.com/gdamore/tcell/v2" |
| 26 |
|
| 27 |
"maunium.net/go/mautrix" |
| 28 |
"maunium.net/go/mautrix/id" |
| 29 |
|
| 30 |
"maunium.net/go/gomuks/config" |
| 31 |
"maunium.net/go/gomuks/debug" |
| 32 |
ifc "maunium.net/go/gomuks/interface" |
| 33 |
) |
| 34 |
|
| 35 |
type LoginView struct { |
| 36 |
*mauview.Form |
| 37 |
|
| 38 |
container *mauview.Centerer |
| 39 |
|
| 40 |
homeserverLabel *mauview.TextField |
| 41 |
usernameLabel *mauview.TextField |
| 42 |
passwordLabel *mauview.TextField |
| 43 |
|
| 44 |
homeserver *mauview.InputField |
| 45 |
username *mauview.InputField |
| 46 |
password *mauview.InputField |
| 47 |
error *mauview.TextView |
| 48 |
|
| 49 |
loginButton *mauview.Button |
| 50 |
quitButton *mauview.Button |
| 51 |
|
| 52 |
loading bool |
| 53 |
|
| 54 |
matrix ifc.MatrixContainer |
| 55 |
config *config.Config |
| 56 |
parent *GomuksUI |
| 57 |
} |
| 58 |
|
| 59 |
func (ui *GomuksUI) NewLoginView() mauview.Component { |
| 60 |
view := &LoginView{ |
| 61 |
Form: mauview.NewForm(), |
| 62 |
|
| 63 |
usernameLabel: mauview.NewTextField().SetText("Username"), |
| 64 |
passwordLabel: mauview.NewTextField().SetText("Password"), |
| 65 |
homeserverLabel: mauview.NewTextField().SetText("Homeserver"), |
| 66 |
|
| 67 |
username: mauview.NewInputField(), |
| 68 |
password: mauview.NewInputField(), |
| 69 |
homeserver: mauview.NewInputField(), |
| 70 |
|
| 71 |
loginButton: mauview.NewButton("Login"), |
| 72 |
quitButton: mauview.NewButton("Quit"), |
| 73 |
|
| 74 |
matrix: ui.gmx.Matrix(), |
| 75 |
config: ui.gmx.Config(), |
| 76 |
parent: ui, |
| 77 |
} |
| 78 |
|
| 79 |
hs := ui.gmx.Config().HS |
| 80 |
view.homeserver.SetPlaceholder("https://example.com").SetText(hs).SetTextColor(tcell.ColorWhite) |
| 81 |
view.username.SetPlaceholder("@user:example.com").SetText(string(ui.gmx.Config().UserID)).SetTextColor(tcell.ColorWhite) |
| 82 |
view.password.SetPlaceholder("correct horse battery staple").SetMaskCharacter('*').SetTextColor(tcell.ColorWhite) |
| 83 |
|
| 84 |
view.quitButton. |
| 85 |
SetOnClick(func() { ui.gmx.Stop(true) }). |
| 86 |
SetBackgroundColor(tcell.ColorDarkCyan). |
| 87 |
SetForegroundColor(tcell.ColorWhite). |
| 88 |
SetFocusedForegroundColor(tcell.ColorWhite) |
| 89 |
view.loginButton. |
| 90 |
SetOnClick(view.Login). |
| 91 |
SetBackgroundColor(tcell.ColorDarkCyan). |
| 92 |
SetForegroundColor(tcell.ColorWhite). |
| 93 |
SetFocusedForegroundColor(tcell.ColorWhite) |
| 94 |
|
| 95 |
view. |
| 96 |
SetColumns([]int{1, 10, 1, 30, 1}). |
| 97 |
SetRows([]int{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}) |
| 98 |
view. |
| 99 |
AddFormItem(view.username, 3, 1, 1, 1). |
| 100 |
AddFormItem(view.password, 3, 3, 1, 1). |
| 101 |
AddFormItem(view.homeserver, 3, 5, 1, 1). |
| 102 |
AddFormItem(view.loginButton, 1, 7, 3, 1). |
| 103 |
AddFormItem(view.quitButton, 1, 9, 3, 1). |
| 104 |
AddComponent(view.usernameLabel, 1, 1, 1, 1). |
| 105 |
AddComponent(view.passwordLabel, 1, 3, 1, 1). |
| 106 |
AddComponent(view.homeserverLabel, 1, 5, 1, 1) |
| 107 |
view.SetOnFocusChanged(view.focusChanged) |
| 108 |
view.FocusNextItem() |
| 109 |
ui.loginView = view |
| 110 |
|
| 111 |
view.container = mauview.Center(mauview.NewBox(view).SetTitle("Log in to Matrix"), 45, 13) |
| 112 |
view.container.SetAlwaysFocusChild(true) |
| 113 |
return view.container |
| 114 |
} |
| 115 |
|
| 116 |
func (view *LoginView) resolveWellKnown() { |
| 117 |
_, homeserver, err := id.UserID(view.username.GetText()).Parse() |
| 118 |
if err != nil { |
| 119 |
return |
| 120 |
} |
| 121 |
view.homeserver.SetText("Resolving...") |
| 122 |
resp, err := mautrix.DiscoverClientAPI(homeserver) |
| 123 |
if err != nil { |
| 124 |
view.homeserver.SetText("") |
| 125 |
view.Error(err.Error()) |
| 126 |
} else if resp != nil { |
| 127 |
view.homeserver.SetText(resp.Homeserver.BaseURL) |
| 128 |
view.parent.Render() |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
func (view *LoginView) focusChanged(from, to mauview.Component) { |
| 133 |
if from == view.username && view.homeserver.GetText() == "" { |
| 134 |
go view.resolveWellKnown() |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
func (view *LoginView) Error(err string) { |
| 139 |
if len(err) == 0 && view.error != nil { |
| 140 |
debug.Print("Hiding error") |
| 141 |
view.RemoveComponent(view.error) |
| 142 |
view.container.SetHeight(13) |
| 143 |
view.SetRows([]int{1, 1, 1, 1, 1, 1, 1, 1, 1}) |
| 144 |
view.error = nil |
| 145 |
} else if len(err) > 0 { |
| 146 |
debug.Print("Showing error", err) |
| 147 |
if view.error == nil { |
| 148 |
view.error = mauview.NewTextView().SetTextColor(tcell.ColorRed) |
| 149 |
view.AddComponent(view.error, 1, 11, 3, 1) |
| 150 |
} |
| 151 |
view.error.SetText(err) |
| 152 |
errorHeight := int(math.Ceil(float64(runewidth.StringWidth(err)) / 41)) |
| 153 |
view.container.SetHeight(14 + errorHeight) |
| 154 |
view.SetRow(11, errorHeight) |
| 155 |
} |
| 156 |
|
| 157 |
view.parent.Render() |
| 158 |
} |
| 159 |
|
| 160 |
func (view *LoginView) actuallyLogin(hs, mxid, password string) { |
| 161 |
debug.Printf("Logging into %s as %s...", hs, mxid) |
| 162 |
view.config.HS = hs |
| 163 |
|
| 164 |
if err := view.matrix.InitClient(false); err != nil { |
| 165 |
debug.Print("Init error:", err) |
| 166 |
view.Error(err.Error()) |
| 167 |
} else if err = view.matrix.Login(mxid, password); err != nil { |
| 168 |
if httpErr, ok := err.(mautrix.HTTPError); ok { |
| 169 |
if httpErr.RespError != nil && len(httpErr.RespError.Err) > 0 { |
| 170 |
view.Error(httpErr.RespError.Err) |
| 171 |
} else if len(httpErr.Message) > 0 { |
| 172 |
view.Error(httpErr.Message) |
| 173 |
} else { |
| 174 |
view.Error(err.Error()) |
| 175 |
} |
| 176 |
} else { |
| 177 |
view.Error(err.Error()) |
| 178 |
} |
| 179 |
debug.Print("Login error:", err) |
| 180 |
} |
| 181 |
view.loading = false |
| 182 |
view.loginButton.SetText("Login") |
| 183 |
} |
| 184 |
|
| 185 |
func (view *LoginView) Login() { |
| 186 |
if view.loading { |
| 187 |
return |
| 188 |
} |
| 189 |
hs := view.homeserver.GetText() |
| 190 |
mxid := view.username.GetText() |
| 191 |
password := view.password.GetText() |
| 192 |
|
| 193 |
view.loading = true |
| 194 |
view.loginButton.SetText("Logging in...") |
| 195 |
go view.actuallyLogin(hs, mxid, password) |
| 196 |
} |