Debian Codegraph @

app/pgpinfo.go

raw
1 package app
2
3 import (
4 "fmt"
5 "strings"
6 "unicode/utf8"
7
8 "git.sr.ht/~rjarry/aerc/config"
9 "git.sr.ht/~rjarry/aerc/lib/ui"
10 "git.sr.ht/~rjarry/aerc/models"
11 "go.rockorager.dev/vaxis"
12 )
13
14 type PGPInfo struct {
15 details *models.MessageDetails
16 uiConfig *config.UIConfig
17 }
18
19 func NewPGPInfo(details *models.MessageDetails, uiConfig *config.UIConfig) *PGPInfo {
20 return &PGPInfo{details: details, uiConfig: uiConfig}
21 }
22
23 func (p *PGPInfo) DrawSignature(ctx *ui.Context) {
24 errorStyle := p.uiConfig.GetStyle(config.STYLE_ERROR)
25 warningStyle := p.uiConfig.GetStyle(config.STYLE_WARNING)
26 validStyle := p.uiConfig.GetStyle(config.STYLE_SUCCESS)
27 defaultStyle := p.uiConfig.GetStyle(config.STYLE_DEFAULT)
28
29 var icon string
30 var indicatorStyle, textstyle vaxis.Style
31 textstyle = defaultStyle
32 var indicatorText, messageText string
33 // TODO: Nicer prompt for TOFU, fetch from keyserver, etc
34 switch p.details.SignatureValidity {
35 case models.UnknownEntity:
36 icon = p.uiConfig.IconUnknown
37 indicatorStyle = warningStyle
38 indicatorText = "Unknown"
39 messageText = fmt.Sprintf("Signed with unknown key (%8X); authenticity unknown", p.details.SignedByKeyId)
40 case models.Valid:
41 icon = p.uiConfig.IconSigned
42 if p.details.IsEncrypted && p.uiConfig.IconSignedEncrypted != "" {
43 icon = p.uiConfig.IconSignedEncrypted
44 }
45 indicatorStyle = validStyle
46 indicatorText = "Authentic"
47 messageText = fmt.Sprintf("Signature from %s (%8X)", p.details.SignedBy, p.details.SignedByKeyId)
48 default:
49 icon = p.uiConfig.IconInvalid
50 indicatorStyle = errorStyle
51 indicatorText = "Invalid signature!"
52 messageText = fmt.Sprintf("This message may have been tampered with! (%s)", p.details.SignatureError)
53 }
54
55 x := ctx.Printf(0, 0, indicatorStyle, "%s %s ", icon, indicatorText)
56 ctx.Printf(x, 0, textstyle, "%s", messageText)
57 }
58
59 func (p *PGPInfo) DrawEncryption(ctx *ui.Context, y int) {
60 warningStyle := p.uiConfig.GetStyle(config.STYLE_WARNING)
61 validStyle := p.uiConfig.GetStyle(config.STYLE_SUCCESS)
62 defaultStyle := p.uiConfig.GetStyle(config.STYLE_DEFAULT)
63
64 // if a sign-encrypt combination icon is set, use that
65 icon := p.uiConfig.IconEncrypted
66 if p.details.IsSigned && p.details.SignatureValidity == models.Valid && p.uiConfig.IconSignedEncrypted != "" {
67 icon = strings.Repeat(" ", utf8.RuneCountInString(p.uiConfig.IconSignedEncrypted))
68 }
69
70 x := ctx.Printf(0, y, validStyle, "%s Encrypted", icon)
71 x += ctx.Printf(x+1, y, defaultStyle, "To %s (%8X) ", p.details.DecryptedWith, p.details.DecryptedWithKeyId)
72 if !p.details.IsSigned {
73 ctx.Printf(x, y, warningStyle, "(message not signed!)")
74 }
75 }
76
77 func (p *PGPInfo) Draw(ctx *ui.Context) {
78 warningStyle := p.uiConfig.GetStyle(config.STYLE_WARNING)
79 defaultStyle := p.uiConfig.GetStyle(config.STYLE_DEFAULT)
80 ctx.Fill(0, 0, ctx.Width(), ctx.Height(), ' ', defaultStyle)
81
82 switch {
83 case p.details == nil && p.uiConfig.IconUnencrypted != "":
84 x := ctx.Printf(0, 0, warningStyle, "%s ", p.uiConfig.IconUnencrypted)
85 ctx.Printf(x, 0, defaultStyle, "message unencrypted and unsigned")
86 case p.details.IsSigned && p.details.IsEncrypted:
87 p.DrawSignature(ctx)
88 p.DrawEncryption(ctx, 1)
89 case p.details.IsSigned:
90 p.DrawSignature(ctx)
91 case p.details.IsEncrypted:
92 p.DrawEncryption(ctx, 0)
93 }
94 }
95
96 func (p *PGPInfo) Invalidate() {
97 ui.Invalidate()
98 }