2020-03-03 22:20:07 +01:00
|
|
|
package widgets
|
|
|
|
|
|
|
|
import (
|
2022-06-22 12:19:41 +02:00
|
|
|
"fmt"
|
2022-06-22 12:19:39 +02:00
|
|
|
"strings"
|
|
|
|
"unicode/utf8"
|
|
|
|
|
2021-11-05 10:19:46 +01:00
|
|
|
"git.sr.ht/~rjarry/aerc/config"
|
|
|
|
"git.sr.ht/~rjarry/aerc/lib/ui"
|
2022-04-25 15:30:43 +02:00
|
|
|
"git.sr.ht/~rjarry/aerc/models"
|
2022-06-22 12:19:41 +02:00
|
|
|
"github.com/gdamore/tcell/v2"
|
2020-03-03 22:20:07 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type PGPInfo struct {
|
2022-04-25 15:30:43 +02:00
|
|
|
details *models.MessageDetails
|
2022-07-03 17:11:12 +02:00
|
|
|
uiConfig *config.UIConfig
|
2020-03-03 22:20:07 +01:00
|
|
|
}
|
|
|
|
|
2022-07-03 17:11:12 +02:00
|
|
|
func NewPGPInfo(details *models.MessageDetails, uiConfig *config.UIConfig) *PGPInfo {
|
2020-07-27 10:03:55 +02:00
|
|
|
return &PGPInfo{details: details, uiConfig: uiConfig}
|
2020-03-03 22:20:07 +01:00
|
|
|
}
|
|
|
|
|
2020-03-04 01:02:27 +01:00
|
|
|
func (p *PGPInfo) DrawSignature(ctx *ui.Context) {
|
2020-07-27 10:03:55 +02: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 22:20:07 +01:00
|
|
|
|
2022-06-22 12:19:41 +02:00
|
|
|
var icon string
|
|
|
|
var indicatorStyle, textstyle tcell.Style
|
|
|
|
textstyle = defaultStyle
|
|
|
|
var indicatorText, messageText string
|
2020-03-03 22:20:07 +01:00
|
|
|
// TODO: Nicer prompt for TOFU, fetch from keyserver, etc
|
2022-06-22 12:19:41 +02: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 12:19:40 +02:00
|
|
|
if p.details.IsEncrypted && p.uiConfig.IconSignedEncrypted != "" {
|
2022-06-22 12:19:39 +02:00
|
|
|
icon = p.uiConfig.IconSignedEncrypted
|
|
|
|
}
|
2022-06-22 12:19:41 +02: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 22:20:07 +01:00
|
|
|
}
|
2022-06-22 12:19:41 +02:00
|
|
|
|
|
|
|
x := ctx.Printf(0, 0, indicatorStyle, "%s %s ", icon, indicatorText)
|
|
|
|
ctx.Printf(x, 0, textstyle, messageText)
|
2020-03-03 22:20:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *PGPInfo) DrawEncryption(ctx *ui.Context, y int) {
|
2022-06-22 12:19:38 +02:00
|
|
|
warningStyle := p.uiConfig.GetStyle(config.STYLE_WARNING)
|
2020-07-27 10:03:55 +02:00
|
|
|
validStyle := p.uiConfig.GetStyle(config.STYLE_SUCCESS)
|
|
|
|
defaultStyle := p.uiConfig.GetStyle(config.STYLE_DEFAULT)
|
2020-03-03 22:20:07 +01:00
|
|
|
|
2022-06-22 12:19:40 +02:00
|
|
|
// if a sign-encrypt combination icon is set, use that
|
2022-06-22 12:19:39 +02:00
|
|
|
icon := p.uiConfig.IconEncrypted
|
2022-06-22 12:19:40 +02:00
|
|
|
if p.details.IsSigned && p.details.SignatureValidity == models.Valid && p.uiConfig.IconSignedEncrypted != "" {
|
2022-06-22 12:19:39 +02:00
|
|
|
icon = strings.Repeat(" ", utf8.RuneCountInString(p.uiConfig.IconSignedEncrypted))
|
|
|
|
}
|
|
|
|
|
2022-06-22 12:19:40 +02: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 12:19:38 +02:00
|
|
|
if !p.details.IsSigned {
|
2022-07-29 21:25:56 +02:00
|
|
|
ctx.Printf(x, y, warningStyle, "(message not signed!)")
|
2022-06-22 12:19:38 +02:00
|
|
|
}
|
2020-03-03 22:20:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *PGPInfo) Draw(ctx *ui.Context) {
|
2022-06-22 12:19:40 +02:00
|
|
|
warningStyle := p.uiConfig.GetStyle(config.STYLE_WARNING)
|
2020-07-27 10:03:55 +02:00
|
|
|
defaultStyle := p.uiConfig.GetStyle(config.STYLE_DEFAULT)
|
|
|
|
ctx.Fill(0, 0, ctx.Width(), ctx.Height(), ' ', defaultStyle)
|
2022-06-22 12:19:40 +02: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 01:02:27 +01:00
|
|
|
p.DrawSignature(ctx)
|
2020-03-03 22:20:07 +01:00
|
|
|
p.DrawEncryption(ctx, 1)
|
2022-06-22 12:19:40 +02:00
|
|
|
case p.details.IsSigned:
|
2020-03-04 01:02:27 +01:00
|
|
|
p.DrawSignature(ctx)
|
2022-06-22 12:19:40 +02:00
|
|
|
case p.details.IsEncrypted:
|
2020-03-03 22:20:07 +01:00
|
|
|
p.DrawEncryption(ctx, 0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *PGPInfo) Invalidate() {
|
2022-10-07 18:00:31 +02:00
|
|
|
ui.Invalidate()
|
2020-03-03 22:20:07 +01:00
|
|
|
}
|