2020-03-03 21:20:07 +00:00
|
|
|
package widgets
|
|
|
|
|
|
|
|
import (
|
2022-06-22 10:19:41 +00:00
|
|
|
"fmt"
|
2022-06-22 10:19:39 +00:00
|
|
|
"strings"
|
|
|
|
"unicode/utf8"
|
|
|
|
|
2021-11-05 09:19:46 +00:00
|
|
|
"git.sr.ht/~rjarry/aerc/config"
|
|
|
|
"git.sr.ht/~rjarry/aerc/lib/ui"
|
2022-04-25 13:30:43 +00:00
|
|
|
"git.sr.ht/~rjarry/aerc/models"
|
2022-06-22 10:19:41 +00:00
|
|
|
"github.com/gdamore/tcell/v2"
|
2020-03-03 21:20:07 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type PGPInfo struct {
|
|
|
|
ui.Invalidatable
|
2022-04-25 13:30:43 +00:00
|
|
|
details *models.MessageDetails
|
2022-07-03 15:11:12 +00:00
|
|
|
uiConfig *config.UIConfig
|
2020-03-03 21:20:07 +00:00
|
|
|
}
|
|
|
|
|
2022-07-03 15:11:12 +00:00
|
|
|
func NewPGPInfo(details *models.MessageDetails, uiConfig *config.UIConfig) *PGPInfo {
|
2020-07-27 08:03:55 +00:00
|
|
|
return &PGPInfo{details: details, uiConfig: uiConfig}
|
2020-03-03 21:20:07 +00:00
|
|
|
}
|
|
|
|
|
2020-03-04 00:02:27 +00:00
|
|
|
func (p *PGPInfo) DrawSignature(ctx *ui.Context) {
|
2020-07-27 08:03:55 +00:00
|
|
|
errorStyle := p.uiConfig.GetStyle(config.STYLE_ERROR)
|
|
|
|
warningStyle := p.uiConfig.GetStyle(config.STYLE_WARNING)
|
|
|
|
validStyle := p.uiConfig.GetStyle(config.STYLE_SUCCESS)
|
|
|
|
defaultStyle := p.uiConfig.GetStyle(config.STYLE_DEFAULT)
|
2020-03-03 21:20:07 +00:00
|
|
|
|
2022-06-22 10:19:41 +00:00
|
|
|
var icon string
|
|
|
|
var indicatorStyle, textstyle tcell.Style
|
|
|
|
textstyle = defaultStyle
|
|
|
|
var indicatorText, messageText string
|
2020-03-03 21:20:07 +00:00
|
|
|
// TODO: Nicer prompt for TOFU, fetch from keyserver, etc
|
2022-06-22 10:19:41 +00:00
|
|
|
switch p.details.SignatureValidity {
|
|
|
|
case models.UnknownEntity:
|
|
|
|
icon = p.uiConfig.IconUnknown
|
|
|
|
indicatorStyle = warningStyle
|
|
|
|
indicatorText = "Unknown"
|
|
|
|
messageText = fmt.Sprintf("Signed with unknown key (%8X); authenticity unknown", p.details.SignedByKeyId)
|
|
|
|
case models.Valid:
|
|
|
|
icon = p.uiConfig.IconSigned
|
2022-06-22 10:19:40 +00:00
|
|
|
if p.details.IsEncrypted && p.uiConfig.IconSignedEncrypted != "" {
|
2022-06-22 10:19:39 +00:00
|
|
|
icon = p.uiConfig.IconSignedEncrypted
|
|
|
|
}
|
2022-06-22 10:19:41 +00:00
|
|
|
indicatorStyle = validStyle
|
|
|
|
indicatorText = "Authentic"
|
|
|
|
messageText = fmt.Sprintf("Signature from %s (%8X)", p.details.SignedBy, p.details.SignedByKeyId)
|
|
|
|
default:
|
|
|
|
icon = p.uiConfig.IconInvalid
|
|
|
|
indicatorStyle = errorStyle
|
|
|
|
indicatorText = "Invalid signature!"
|
|
|
|
messageText = fmt.Sprintf("This message may have been tampered with! (%s)", p.details.SignatureError)
|
2020-03-03 21:20:07 +00:00
|
|
|
}
|
2022-06-22 10:19:41 +00:00
|
|
|
|
|
|
|
x := ctx.Printf(0, 0, indicatorStyle, "%s %s ", icon, indicatorText)
|
|
|
|
ctx.Printf(x, 0, textstyle, messageText)
|
2020-03-03 21:20:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *PGPInfo) DrawEncryption(ctx *ui.Context, y int) {
|
2022-06-22 10:19:38 +00:00
|
|
|
warningStyle := p.uiConfig.GetStyle(config.STYLE_WARNING)
|
2020-07-27 08:03:55 +00:00
|
|
|
validStyle := p.uiConfig.GetStyle(config.STYLE_SUCCESS)
|
|
|
|
defaultStyle := p.uiConfig.GetStyle(config.STYLE_DEFAULT)
|
2020-03-03 21:20:07 +00:00
|
|
|
|
2022-06-22 10:19:40 +00:00
|
|
|
// if a sign-encrypt combination icon is set, use that
|
2022-06-22 10:19:39 +00:00
|
|
|
icon := p.uiConfig.IconEncrypted
|
2022-06-22 10:19:40 +00:00
|
|
|
if p.details.IsSigned && p.details.SignatureValidity == models.Valid && p.uiConfig.IconSignedEncrypted != "" {
|
2022-06-22 10:19:39 +00:00
|
|
|
icon = strings.Repeat(" ", utf8.RuneCountInString(p.uiConfig.IconSignedEncrypted))
|
|
|
|
}
|
|
|
|
|
2022-06-22 10:19:40 +00:00
|
|
|
x := ctx.Printf(0, y, validStyle, "%s Encrypted", icon)
|
|
|
|
x += ctx.Printf(x+1, y, defaultStyle, "To %s (%8X) ", p.details.DecryptedWith, p.details.DecryptedWithKeyId)
|
2022-06-22 10:19:38 +00:00
|
|
|
if !p.details.IsSigned {
|
2022-07-29 19:25:56 +00:00
|
|
|
ctx.Printf(x, y, warningStyle, "(message not signed!)")
|
2022-06-22 10:19:38 +00:00
|
|
|
}
|
2020-03-03 21:20:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *PGPInfo) Draw(ctx *ui.Context) {
|
2022-06-22 10:19:40 +00:00
|
|
|
warningStyle := p.uiConfig.GetStyle(config.STYLE_WARNING)
|
2020-07-27 08:03:55 +00:00
|
|
|
defaultStyle := p.uiConfig.GetStyle(config.STYLE_DEFAULT)
|
|
|
|
ctx.Fill(0, 0, ctx.Width(), ctx.Height(), ' ', defaultStyle)
|
2022-06-22 10:19:40 +00:00
|
|
|
|
|
|
|
switch {
|
|
|
|
case p.details == nil && p.uiConfig.IconUnencrypted != "":
|
|
|
|
x := ctx.Printf(0, 0, warningStyle, "%s ", p.uiConfig.IconUnencrypted)
|
|
|
|
ctx.Printf(x, 0, defaultStyle, "message unencrypted and unsigned")
|
|
|
|
case p.details.IsSigned && p.details.IsEncrypted:
|
2020-03-04 00:02:27 +00:00
|
|
|
p.DrawSignature(ctx)
|
2020-03-03 21:20:07 +00:00
|
|
|
p.DrawEncryption(ctx, 1)
|
2022-06-22 10:19:40 +00:00
|
|
|
case p.details.IsSigned:
|
2020-03-04 00:02:27 +00:00
|
|
|
p.DrawSignature(ctx)
|
2022-06-22 10:19:40 +00:00
|
|
|
case p.details.IsEncrypted:
|
2020-03-03 21:20:07 +00:00
|
|
|
p.DrawEncryption(ctx, 0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *PGPInfo) Invalidate() {
|
|
|
|
p.DoInvalidate(p)
|
|
|
|
}
|