Debian Codegraph @

ui/verification-modal.go

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
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 //go:build cgo
18
19 package ui
20
21 import (
22 "fmt"
23 "strconv"
24 "strings"
25 "time"
26
27 "go.mau.fi/mauview"
•28 "github.com/gdamore/tcell/v2"
29
30 "maunium.net/go/mautrix/crypto"
31 "maunium.net/go/mautrix/event"
32
33 "maunium.net/go/gomuks/config"
34 "maunium.net/go/gomuks/debug"
35 )
36
37 type EmojiView struct {
38 mauview.SimpleEventHandler
39 Data crypto.SASData
40 }
41
42 func (e *EmojiView) Draw(screen mauview.Screen) {
43 if e.Data == nil {
44 return
45 }
46 switch e.Data.Type() {
47 case event.SASEmoji:
48 width := 10
49 for i, emoji := range e.Data.(crypto.EmojiSASData) {
50 x := i*width + i
51 y := 0
52 if i >= 4 {
53 x = (i-4)*width + i
54 y = 2
55 }
56 mauview.Print(screen, string(emoji.Emoji), x, y, width, mauview.AlignCenter, tcell.ColorDefault)
57 mauview.Print(screen, emoji.Description, x, y+1, width, mauview.AlignCenter, tcell.ColorDefault)
58 }
59 case event.SASDecimal:
60 maxWidth := 43
61 for i, number := range e.Data.(crypto.DecimalSASData) {
62 mauview.Print(screen, strconv.FormatUint(uint64(number), 10), 0, i, maxWidth, mauview.AlignCenter, tcell.ColorDefault)
63 }
64 }
65 }
66
67 type VerificationModal struct {
68 mauview.Component
69
70 device *crypto.DeviceIdentity
71
72 container *mauview.Box
73
74 waitingBar *mauview.ProgressBar
75 infoText *mauview.TextView
76 emojiText *EmojiView
77 inputBar *mauview.InputField
78
79 progress int
80 progressMax int
81 stopWaiting chan struct{}
82 confirmChan chan bool
83 done bool
84
85 parent *MainView
86 }
87
88 func NewVerificationModal(mainView *MainView, device *crypto.DeviceIdentity, timeout time.Duration) *VerificationModal {
89 vm := &VerificationModal{
90 parent: mainView,
91 device: device,
92 stopWaiting: make(chan struct{}),
93 confirmChan: make(chan bool),
94 done: false,
95 }
96
97 vm.progressMax = int(timeout.Seconds())
98 vm.progress = vm.progressMax
99 vm.waitingBar = mauview.NewProgressBar().
100 SetMax(vm.progressMax).
101 SetProgress(vm.progress).
102 SetIndeterminate(false)
103
104 vm.infoText = mauview.NewTextView()
105 vm.infoText.SetText(fmt.Sprintf("Waiting for %s\nto accept", device.UserID))
106
107 vm.emojiText = &EmojiView{}
108
109 vm.inputBar = mauview.NewInputField().
110 SetBackgroundColor(tcell.ColorDefault).
111 SetPlaceholderTextColor(tcell.ColorDefault)
112
113 flex := mauview.NewFlex().
114 SetDirection(mauview.FlexRow).
115 AddFixedComponent(vm.waitingBar, 1).
116 AddFixedComponent(vm.infoText, 4).
117 AddFixedComponent(vm.emojiText, 4).
118 AddFixedComponent(vm.inputBar, 1)
119
120 vm.container = mauview.NewBox(flex).
121 SetBorder(true).
122 SetTitle("Interactive verification")
123
124 vm.Component = mauview.Center(vm.container, 45, 12).SetAlwaysFocusChild(true)
125
126 go vm.decrementWaitingBar()
127
128 return vm
129 }
130
131 func (vm *VerificationModal) decrementWaitingBar() {
132 for {
133 select {
134 case <-time.Tick(time.Second):
135 if vm.progress <= 0 {
136 vm.waitingBar.SetIndeterminate(true)
137 vm.parent.parent.app.SetRedrawTicker(100 * time.Millisecond)
138 return
139 }
140 vm.progress--
141 vm.waitingBar.SetProgress(vm.progress)
142 vm.parent.parent.Render()
143 case <-vm.stopWaiting:
144 return
145 }
146 }
147 }
148
149 func (vm *VerificationModal) VerificationMethods() []crypto.VerificationMethod {
150 return []crypto.VerificationMethod{crypto.VerificationMethodEmoji{}, crypto.VerificationMethodDecimal{}}
151 }
152
153 func (vm *VerificationModal) VerifySASMatch(device *crypto.DeviceIdentity, data crypto.SASData) bool {
154 vm.device = device
155 var typeName string
156 if data.Type() == event.SASDecimal {
157 typeName = "numbers"
158 } else if data.Type() == event.SASEmoji {
159 typeName = "emojis"
160 } else {
161 return false
162 }
163 vm.infoText.SetText(fmt.Sprintf(
164 "Check if the other device is showing the\n"+
165 "same %s as below, then type \"yes\" to\n"+
166 "accept, or \"no\" to reject", typeName))
167 vm.inputBar.
168 SetTextColor(tcell.ColorDefault).
169 SetBackgroundColor(tcell.ColorDarkCyan).
170 SetPlaceholder("Type \"yes\" or \"no\"").
171 Focus()
172 vm.emojiText.Data = data
173 vm.parent.parent.Render()
174 vm.progress = vm.progressMax
175 confirm := <-vm.confirmChan
176 vm.progress = vm.progressMax
177 vm.emojiText.Data = nil
178 vm.infoText.SetText(fmt.Sprintf("Waiting for %s\nto confirm", vm.device.UserID))
179 vm.parent.parent.Render()
180 return confirm
181 }
182
183 func (vm *VerificationModal) OnCancel(cancelledByUs bool, reason string, _ event.VerificationCancelCode) {
184 vm.waitingBar.SetIndeterminate(false).SetMax(100).SetProgress(100)
185 vm.parent.parent.app.SetRedrawTicker(1 * time.Minute)
186 if cancelledByUs {
187 vm.infoText.SetText(fmt.Sprintf("Verification failed: %s", reason))
188 } else {
189 vm.infoText.SetText(fmt.Sprintf("Verification cancelled by %s: %s", vm.device.UserID, reason))
190 }
191 vm.inputBar.SetPlaceholder("Press enter to close the dialog")
192 vm.stopWaiting <- struct{}{}
193 vm.done = true
194 vm.parent.parent.Render()
195 }
196
197 func (vm *VerificationModal) OnSuccess() {
198 vm.waitingBar.SetIndeterminate(false).SetMax(100).SetProgress(100)
199 vm.parent.parent.app.SetRedrawTicker(1 * time.Minute)
200 vm.infoText.SetText(fmt.Sprintf("Successfully verified %s (%s) of %s", vm.device.Name, vm.device.DeviceID, vm.device.UserID))
201 vm.inputBar.SetPlaceholder("Press enter to close the dialog")
202 vm.stopWaiting <- struct{}{}
203 vm.done = true
204 vm.parent.parent.Render()
205 if vm.parent.config.SendToVerifiedOnly {
206 // Hacky way to make new group sessions after verified
207 vm.parent.matrix.Crypto().(*crypto.OlmMachine).OnDevicesChanged(vm.device.UserID)
208 }
209 }
210
211 func (vm *VerificationModal) OnKeyEvent(event mauview.KeyEvent) bool {
212 kb := config.Keybind{
213 Key: event.Key(),
214 Ch: event.Rune(),
215 Mod: event.Modifiers(),
216 }
217 if vm.done {
218 if vm.parent.config.Keybindings.Modal[kb] == "cancel" || vm.parent.config.Keybindings.Modal[kb] == "confirm" {
219 vm.parent.HideModal()
220 return true
221 }
222 return false
223 } else if vm.emojiText.Data == nil {
224 debug.Print("Ignoring pre-emoji key event")
225 return false
226 }
227 if vm.parent.config.Keybindings.Modal[kb] == "confirm" {
228 text := strings.ToLower(strings.TrimSpace(vm.inputBar.GetText()))
229 if text == "yes" {
230 debug.Print("Confirming verification")
231 vm.confirmChan <- true
232 } else if text == "no" {
233 debug.Print("Rejecting verification")
234 vm.confirmChan <- false
235 }
236 vm.inputBar.
237 SetPlaceholder("").
238 SetTextAndMoveCursor("").
239 SetBackgroundColor(tcell.ColorDefault).
240 SetTextColor(tcell.ColorDefault)
241 return true
242 } else {
243 return vm.inputBar.OnKeyEvent(event)
244 }
245 }
246
247 func (vm *VerificationModal) Focus() {
248 vm.container.Focus()
249 }
250
251 func (vm *VerificationModal) Blur() {
252 vm.container.Blur()
253 }