2020-07-24 22:44:04 +02:00
|
|
|
// gomuks - A terminal Matrix client written in Go.
|
|
|
|
// Copyright (C) 2020 Tulir Asokan
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// 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
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// 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/>.
|
|
|
|
|
2020-07-30 13:32:59 +02:00
|
|
|
// +build cgo
|
|
|
|
|
2020-07-24 22:44:04 +02:00
|
|
|
package ui
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"maunium.net/go/mauview"
|
|
|
|
"maunium.net/go/tcell"
|
|
|
|
|
|
|
|
"maunium.net/go/gomuks/debug"
|
|
|
|
"maunium.net/go/mautrix/crypto"
|
2020-07-25 17:40:31 +02:00
|
|
|
"maunium.net/go/mautrix/event"
|
2020-07-24 22:44:04 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type EmojiView struct {
|
|
|
|
mauview.SimpleEventHandler
|
2020-07-30 13:32:59 +02:00
|
|
|
Data crypto.SASData
|
2020-07-24 22:44:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e *EmojiView) Draw(screen mauview.Screen) {
|
2020-07-30 13:32:59 +02:00
|
|
|
if e.Data == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
switch e.Data.Type() {
|
|
|
|
case event.SASEmoji:
|
2020-07-24 22:44:04 +02:00
|
|
|
width := 10
|
2020-07-30 13:32:59 +02:00
|
|
|
for i, emoji := range e.Data.(crypto.EmojiSASData) {
|
2020-07-24 22:44:04 +02:00
|
|
|
x := i*width + i
|
|
|
|
y := 0
|
|
|
|
if i >= 4 {
|
|
|
|
x = (i-4)*width + i
|
|
|
|
y = 2
|
|
|
|
}
|
|
|
|
mauview.Print(screen, string(emoji.Emoji), x, y, width, mauview.AlignCenter, tcell.ColorDefault)
|
|
|
|
mauview.Print(screen, emoji.Description, x, y+1, width, mauview.AlignCenter, tcell.ColorDefault)
|
|
|
|
}
|
2020-07-30 13:32:59 +02:00
|
|
|
case event.SASDecimal:
|
2020-07-24 22:44:04 +02:00
|
|
|
maxWidth := 43
|
2020-07-30 13:32:59 +02:00
|
|
|
for i, number := range e.Data.(crypto.DecimalSASData) {
|
2020-07-24 22:44:04 +02:00
|
|
|
mauview.Print(screen, strconv.FormatUint(uint64(number), 10), 0, i, maxWidth, mauview.AlignCenter, tcell.ColorDefault)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type VerificationModal struct {
|
|
|
|
mauview.Component
|
|
|
|
|
2020-07-25 17:40:31 +02:00
|
|
|
device *crypto.DeviceIdentity
|
|
|
|
|
2020-07-24 22:44:04 +02:00
|
|
|
container *mauview.Box
|
|
|
|
|
|
|
|
waitingBar *mauview.ProgressBar
|
|
|
|
infoText *mauview.TextView
|
|
|
|
emojiText *EmojiView
|
|
|
|
inputBar *mauview.InputField
|
|
|
|
|
2020-07-30 13:32:59 +02:00
|
|
|
progress int
|
|
|
|
progressMax int
|
2020-07-24 22:44:04 +02:00
|
|
|
stopWaiting chan struct{}
|
|
|
|
confirmChan chan bool
|
2020-07-25 17:40:31 +02:00
|
|
|
done bool
|
2020-07-24 22:44:04 +02:00
|
|
|
|
|
|
|
parent *MainView
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewVerificationModal(mainView *MainView, device *crypto.DeviceIdentity, timeout time.Duration) *VerificationModal {
|
|
|
|
vm := &VerificationModal{
|
|
|
|
parent: mainView,
|
2020-07-25 17:40:31 +02:00
|
|
|
device: device,
|
2020-07-24 22:44:04 +02:00
|
|
|
stopWaiting: make(chan struct{}),
|
|
|
|
confirmChan: make(chan bool),
|
2020-07-25 17:40:31 +02:00
|
|
|
done: false,
|
2020-07-24 22:44:04 +02:00
|
|
|
}
|
|
|
|
|
2020-07-30 13:32:59 +02:00
|
|
|
vm.progressMax = int(timeout.Seconds())
|
|
|
|
vm.progress = vm.progressMax
|
2020-07-24 22:44:04 +02:00
|
|
|
vm.waitingBar = mauview.NewProgressBar().
|
2020-07-30 13:32:59 +02:00
|
|
|
SetMax(vm.progressMax).
|
|
|
|
SetProgress(vm.progress).
|
2020-07-24 22:44:04 +02:00
|
|
|
SetIndeterminate(false)
|
|
|
|
|
|
|
|
vm.infoText = mauview.NewTextView()
|
2020-07-30 13:32:59 +02:00
|
|
|
vm.infoText.SetText(fmt.Sprintf("Waiting for %s\nto accept", device.UserID))
|
2020-07-24 22:44:04 +02:00
|
|
|
|
|
|
|
vm.emojiText = &EmojiView{}
|
|
|
|
|
2020-07-25 19:54:32 +02:00
|
|
|
vm.inputBar = mauview.NewInputField().
|
|
|
|
SetBackgroundColor(tcell.ColorDefault).
|
2021-04-04 00:09:27 +02:00
|
|
|
SetPlaceholderTextColor(tcell.ColorDefault)
|
2020-07-24 22:44:04 +02:00
|
|
|
|
|
|
|
flex := mauview.NewFlex().
|
|
|
|
SetDirection(mauview.FlexRow).
|
|
|
|
AddFixedComponent(vm.waitingBar, 1).
|
|
|
|
AddFixedComponent(vm.infoText, 4).
|
|
|
|
AddFixedComponent(vm.emojiText, 4).
|
|
|
|
AddFixedComponent(vm.inputBar, 1)
|
|
|
|
|
|
|
|
vm.container = mauview.NewBox(flex).
|
|
|
|
SetBorder(true).
|
|
|
|
SetTitle("Interactive verification")
|
|
|
|
|
|
|
|
vm.Component = mauview.Center(vm.container, 45, 12).SetAlwaysFocusChild(true)
|
|
|
|
|
2020-07-30 13:32:59 +02:00
|
|
|
go vm.decrementWaitingBar()
|
2020-07-24 22:44:04 +02:00
|
|
|
|
|
|
|
return vm
|
|
|
|
}
|
|
|
|
|
2020-07-30 13:32:59 +02:00
|
|
|
func (vm *VerificationModal) decrementWaitingBar() {
|
2020-07-24 22:44:04 +02:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-time.Tick(time.Second):
|
2020-07-30 13:32:59 +02:00
|
|
|
if vm.progress <= 0 {
|
|
|
|
vm.waitingBar.SetIndeterminate(true)
|
|
|
|
vm.parent.parent.app.SetRedrawTicker(100 * time.Millisecond)
|
2020-07-24 22:44:04 +02:00
|
|
|
return
|
|
|
|
}
|
2020-07-30 13:32:59 +02:00
|
|
|
vm.progress--
|
|
|
|
vm.waitingBar.SetProgress(vm.progress)
|
2020-07-24 22:44:04 +02:00
|
|
|
vm.parent.parent.Render()
|
|
|
|
case <-vm.stopWaiting:
|
2020-07-25 17:40:31 +02:00
|
|
|
return
|
2020-07-24 22:44:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-30 13:32:59 +02:00
|
|
|
func (vm *VerificationModal) VerificationMethods() []crypto.VerificationMethod {
|
|
|
|
return []crypto.VerificationMethod{crypto.VerificationMethodEmoji{}, crypto.VerificationMethodDecimal{}}
|
2020-07-24 22:44:04 +02:00
|
|
|
}
|
|
|
|
|
2020-09-12 23:13:08 +02:00
|
|
|
func (vm *VerificationModal) VerifySASMatch(device *crypto.DeviceIdentity, data crypto.SASData) bool {
|
|
|
|
vm.device = device
|
2020-07-30 13:32:59 +02:00
|
|
|
var typeName string
|
|
|
|
if data.Type() == event.SASDecimal {
|
|
|
|
typeName = "numbers"
|
|
|
|
} else if data.Type() == event.SASEmoji {
|
|
|
|
typeName = "emojis"
|
|
|
|
} else {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
vm.infoText.SetText(fmt.Sprintf(
|
|
|
|
"Check if the other device is showing the\n"+
|
|
|
|
"same %s as below, then type \"yes\" to\n"+
|
|
|
|
"accept, or \"no\" to reject", typeName))
|
2020-07-24 22:44:04 +02:00
|
|
|
vm.inputBar.
|
2021-04-04 00:09:27 +02:00
|
|
|
SetTextColor(tcell.ColorDefault).
|
2020-07-24 22:44:04 +02:00
|
|
|
SetBackgroundColor(tcell.ColorDarkCyan).
|
|
|
|
SetPlaceholder("Type \"yes\" or \"no\"").
|
|
|
|
Focus()
|
2020-07-30 13:32:59 +02:00
|
|
|
vm.emojiText.Data = data
|
2020-07-24 22:44:04 +02:00
|
|
|
vm.parent.parent.Render()
|
2020-07-30 13:32:59 +02:00
|
|
|
vm.progress = vm.progressMax
|
2020-07-24 22:44:04 +02:00
|
|
|
confirm := <-vm.confirmChan
|
2020-07-30 13:32:59 +02:00
|
|
|
vm.progress = vm.progressMax
|
|
|
|
vm.emojiText.Data = nil
|
|
|
|
vm.infoText.SetText(fmt.Sprintf("Waiting for %s\nto confirm", vm.device.UserID))
|
2020-07-24 22:44:04 +02:00
|
|
|
vm.parent.parent.Render()
|
|
|
|
return confirm
|
|
|
|
}
|
|
|
|
|
2020-07-25 17:40:31 +02:00
|
|
|
func (vm *VerificationModal) OnCancel(cancelledByUs bool, reason string, _ event.VerificationCancelCode) {
|
|
|
|
vm.waitingBar.SetIndeterminate(false).SetMax(100).SetProgress(100)
|
|
|
|
vm.parent.parent.app.SetRedrawTicker(1 * time.Minute)
|
|
|
|
if cancelledByUs {
|
|
|
|
vm.infoText.SetText(fmt.Sprintf("Verification failed: %s", reason))
|
|
|
|
} else {
|
|
|
|
vm.infoText.SetText(fmt.Sprintf("Verification cancelled by %s: %s", vm.device.UserID, reason))
|
|
|
|
}
|
2020-07-25 19:54:32 +02:00
|
|
|
vm.inputBar.SetPlaceholder("Press enter to close the dialog")
|
2020-07-30 13:32:59 +02:00
|
|
|
vm.stopWaiting <- struct{}{}
|
2020-07-25 17:40:31 +02:00
|
|
|
vm.done = true
|
|
|
|
vm.parent.parent.Render()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (vm *VerificationModal) OnSuccess() {
|
|
|
|
vm.waitingBar.SetIndeterminate(false).SetMax(100).SetProgress(100)
|
|
|
|
vm.parent.parent.app.SetRedrawTicker(1 * time.Minute)
|
|
|
|
vm.infoText.SetText(fmt.Sprintf("Successfully verified %s (%s) of %s", vm.device.Name, vm.device.DeviceID, vm.device.UserID))
|
2020-07-25 19:54:32 +02:00
|
|
|
vm.inputBar.SetPlaceholder("Press enter to close the dialog")
|
2020-07-30 13:32:59 +02:00
|
|
|
vm.stopWaiting <- struct{}{}
|
2020-07-25 17:40:31 +02:00
|
|
|
vm.done = true
|
|
|
|
vm.parent.parent.Render()
|
2020-07-25 19:54:32 +02:00
|
|
|
if vm.parent.config.SendToVerifiedOnly {
|
|
|
|
// Hacky way to make new group sessions after verified
|
|
|
|
vm.parent.matrix.Crypto().(*crypto.OlmMachine).OnDevicesChanged(vm.device.UserID)
|
|
|
|
}
|
2020-07-25 17:40:31 +02:00
|
|
|
}
|
|
|
|
|
2020-07-24 22:44:04 +02:00
|
|
|
func (vm *VerificationModal) OnKeyEvent(event mauview.KeyEvent) bool {
|
2020-07-25 17:40:31 +02:00
|
|
|
if vm.done {
|
|
|
|
if event.Key() == tcell.KeyEnter || event.Key() == tcell.KeyEsc {
|
|
|
|
vm.parent.HideModal()
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
2020-07-30 13:32:59 +02:00
|
|
|
} else if vm.emojiText.Data == nil {
|
2020-07-24 22:44:04 +02:00
|
|
|
debug.Print("Ignoring pre-emoji key event")
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if event.Key() == tcell.KeyEnter {
|
|
|
|
text := strings.ToLower(strings.TrimSpace(vm.inputBar.GetText()))
|
|
|
|
if text == "yes" {
|
|
|
|
debug.Print("Confirming verification")
|
|
|
|
vm.confirmChan <- true
|
|
|
|
} else if text == "no" {
|
|
|
|
debug.Print("Rejecting verification")
|
|
|
|
vm.confirmChan <- false
|
|
|
|
}
|
2020-07-30 13:32:59 +02:00
|
|
|
vm.inputBar.
|
|
|
|
SetPlaceholder("").
|
|
|
|
SetTextAndMoveCursor("").
|
|
|
|
SetBackgroundColor(tcell.ColorDefault).
|
|
|
|
SetTextColor(tcell.ColorDefault)
|
2020-07-24 22:44:04 +02:00
|
|
|
return true
|
|
|
|
} else {
|
|
|
|
return vm.inputBar.OnKeyEvent(event)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (vm *VerificationModal) Focus() {
|
|
|
|
vm.container.Focus()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (vm *VerificationModal) Blur() {
|
|
|
|
vm.container.Blur()
|
|
|
|
}
|