aerc: Refactor getpasswd dialog
Previously there's a hack for showing and hiding the dialog. Change it to use channels to emulate async/await
This commit is contained in:
parent
94e8d8f7bf
commit
a31d184ba5
2 changed files with 56 additions and 40 deletions
|
@ -36,7 +36,7 @@ type Aerc struct {
|
||||||
tabs *ui.Tabs
|
tabs *ui.Tabs
|
||||||
ui *ui.UI
|
ui *ui.UI
|
||||||
beep func() error
|
beep func() error
|
||||||
getpasswd *GetPasswd
|
dialog ui.DrawableInteractive
|
||||||
}
|
}
|
||||||
|
|
||||||
type Choice struct {
|
type Choice struct {
|
||||||
|
@ -170,8 +170,8 @@ func (aerc *Aerc) Focus(focus bool) {
|
||||||
|
|
||||||
func (aerc *Aerc) Draw(ctx *ui.Context) {
|
func (aerc *Aerc) Draw(ctx *ui.Context) {
|
||||||
aerc.grid.Draw(ctx)
|
aerc.grid.Draw(ctx)
|
||||||
if aerc.getpasswd != nil {
|
if aerc.dialog != nil {
|
||||||
aerc.getpasswd.Draw(ctx.Subcontext(4, ctx.Height()/2-2,
|
aerc.dialog.Draw(ctx.Subcontext(4, ctx.Height()/2-2,
|
||||||
ctx.Width()-8, 4))
|
ctx.Width()-8, 4))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -212,8 +212,8 @@ func (aerc *Aerc) simulate(strokes []config.KeyStroke) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (aerc *Aerc) Event(event tcell.Event) bool {
|
func (aerc *Aerc) Event(event tcell.Event) bool {
|
||||||
if aerc.getpasswd != nil {
|
if aerc.dialog != nil {
|
||||||
return aerc.getpasswd.Event(event)
|
return aerc.dialog.Event(event)
|
||||||
}
|
}
|
||||||
|
|
||||||
if aerc.focused != nil {
|
if aerc.focused != nil {
|
||||||
|
@ -537,16 +537,42 @@ func (aerc *Aerc) CloseBackends() error {
|
||||||
return returnErr
|
return returnErr
|
||||||
}
|
}
|
||||||
|
|
||||||
func (aerc *Aerc) GetPassword(title string, prompt string, cb func(string, error)) {
|
func (aerc *Aerc) AddDialog(d ui.DrawableInteractive) {
|
||||||
aerc.getpasswd = NewGetPasswd(title, prompt, func(pw string, err error) {
|
aerc.dialog = d
|
||||||
aerc.getpasswd = nil
|
aerc.dialog.OnInvalidate(func(_ ui.Drawable) {
|
||||||
aerc.Invalidate()
|
|
||||||
cb(pw, err)
|
|
||||||
})
|
|
||||||
aerc.getpasswd.OnInvalidate(func(_ ui.Drawable) {
|
|
||||||
aerc.Invalidate()
|
aerc.Invalidate()
|
||||||
})
|
})
|
||||||
aerc.Invalidate()
|
aerc.Invalidate()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (aerc *Aerc) CloseDialog() {
|
||||||
|
aerc.dialog = nil
|
||||||
|
aerc.Invalidate()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
func (aerc *Aerc) GetPassword(title string, prompt string) (chText chan string, chErr chan error) {
|
||||||
|
chText = make(chan string, 1)
|
||||||
|
chErr = make(chan error, 1)
|
||||||
|
getPasswd := NewGetPasswd(title, prompt, func(pw string, err error) {
|
||||||
|
defer func() {
|
||||||
|
close(chErr)
|
||||||
|
close(chText)
|
||||||
|
aerc.CloseDialog()
|
||||||
|
}()
|
||||||
|
if err != nil {
|
||||||
|
chErr <- err
|
||||||
|
return
|
||||||
|
}
|
||||||
|
chErr <- nil
|
||||||
|
chText <- pw
|
||||||
|
return
|
||||||
|
})
|
||||||
|
aerc.AddDialog(getPasswd)
|
||||||
|
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (aerc *Aerc) Initialize(ui *ui.UI) {
|
func (aerc *Aerc) Initialize(ui *ui.UI) {
|
||||||
|
@ -554,28 +580,25 @@ func (aerc *Aerc) Initialize(ui *ui.UI) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (aerc *Aerc) DecryptKeys(keys []openpgp.Key, symmetric bool) (b []byte, err error) {
|
func (aerc *Aerc) DecryptKeys(keys []openpgp.Key, symmetric bool) (b []byte, err error) {
|
||||||
// HACK HACK HACK
|
|
||||||
for _, key := range keys {
|
for _, key := range keys {
|
||||||
var ident *openpgp.Identity
|
ident := key.Entity.PrimaryIdentity()
|
||||||
for _, ident = range key.Entity.Identities {
|
chPass, chErr := aerc.GetPassword("Decrypt PGP private key",
|
||||||
break
|
|
||||||
}
|
|
||||||
aerc.GetPassword("Decrypt PGP private key",
|
|
||||||
fmt.Sprintf("Enter password for %s (%8X)\nPress <ESC> to cancel",
|
fmt.Sprintf("Enter password for %s (%8X)\nPress <ESC> to cancel",
|
||||||
ident.Name, key.PublicKey.KeyId),
|
ident.Name, key.PublicKey.KeyId))
|
||||||
func(pass string, e error) {
|
|
||||||
if e != nil {
|
for {
|
||||||
err = e
|
select {
|
||||||
return
|
case err = <-chErr:
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
}
|
}
|
||||||
e = key.PrivateKey.Decrypt([]byte(pass))
|
pass := <-chPass
|
||||||
if e != nil {
|
err = key.PrivateKey.Decrypt([]byte(pass))
|
||||||
err = e
|
return nil, err
|
||||||
}
|
default:
|
||||||
})
|
|
||||||
for aerc.getpasswd != nil {
|
|
||||||
aerc.ui.Tick()
|
aerc.ui.Tick()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
|
@ -41,11 +41,8 @@ func (p *PGPInfo) DrawSignature(ctx *ui.Context) {
|
||||||
p.details.SignatureError.Error())
|
p.details.SignatureError.Error())
|
||||||
} else {
|
} else {
|
||||||
entity := p.details.SignedBy.Entity
|
entity := p.details.SignedBy.Entity
|
||||||
var ident *openpgp.Identity
|
ident := entity.PrimaryIdentity()
|
||||||
// TODO: Pick identity more intelligently
|
|
||||||
for _, ident = range entity.Identities {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
x := ctx.Printf(0, 0, validStyle, "✓ Authentic ")
|
x := ctx.Printf(0, 0, validStyle, "✓ Authentic ")
|
||||||
x += ctx.Printf(x, 0, tcell.StyleDefault,
|
x += ctx.Printf(x, 0, tcell.StyleDefault,
|
||||||
"Signature from %s (%8X)",
|
"Signature from %s (%8X)",
|
||||||
|
@ -56,11 +53,7 @@ func (p *PGPInfo) DrawSignature(ctx *ui.Context) {
|
||||||
func (p *PGPInfo) DrawEncryption(ctx *ui.Context, y int) {
|
func (p *PGPInfo) DrawEncryption(ctx *ui.Context, y int) {
|
||||||
validStyle := tcell.StyleDefault.Foreground(tcell.ColorGreen).Bold(true)
|
validStyle := tcell.StyleDefault.Foreground(tcell.ColorGreen).Bold(true)
|
||||||
entity := p.details.DecryptedWith.Entity
|
entity := p.details.DecryptedWith.Entity
|
||||||
var ident *openpgp.Identity
|
ident := entity.PrimaryIdentity()
|
||||||
// TODO: Pick identity more intelligently
|
|
||||||
for _, ident = range entity.Identities {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
|
|
||||||
x := ctx.Printf(0, y, validStyle, "✓ Encrypted ")
|
x := ctx.Printf(0, y, validStyle, "✓ Encrypted ")
|
||||||
x += ctx.Printf(x, y, tcell.StyleDefault,
|
x += ctx.Printf(x, y, tcell.StyleDefault,
|
||||||
|
|
Loading…
Reference in a new issue