Debian Codegraph @

commands/msg/envelope.go

raw
1 package msg
2
3 import (
4 "errors"
5 "fmt"
6 "strings"
7
8 "git.sr.ht/~rjarry/aerc/app"
9 "git.sr.ht/~rjarry/aerc/commands"
10 "git.sr.ht/~rjarry/aerc/lib/format"
11 "git.sr.ht/~rjarry/aerc/lib/log"
12 "git.sr.ht/~rjarry/aerc/models"
13 "github.com/emersion/go-message/mail"
14 )
15
16 type Envelope struct {
17 Header bool `opt:"-h" desc:"Show all header fields."`
18 Format string `opt:"-s" default:"%-20.20s: %s" desc:"Format specifier."`
19 }
20
21 func init() {
22 commands.Register(Envelope{})
23 }
24
25 func (Envelope) Description() string {
26 return "Open the message envelope in a dialog popup."
27 }
28
29 func (Envelope) Context() commands.CommandContext {
30 return commands.MESSAGE_LIST | commands.MESSAGE_VIEWER
31 }
32
33 func (Envelope) Aliases() []string {
34 return []string{"envelope"}
35 }
36
37 func (e Envelope) Execute(args []string) error {
38 provider, ok := app.SelectedTabContent().(app.ProvidesMessages)
39 if !ok {
40 return fmt.Errorf("current tab does not implement app.ProvidesMessage interface")
41 }
42
43 acct := provider.SelectedAccount()
44 if acct == nil {
45 return errors.New("No account selected")
46 }
47
48 var list []string
49 if msg, err := provider.SelectedMessage(); err != nil {
50 return err
51 } else {
52 if msg != nil {
53 if e.Header {
54 list = parseHeader(msg, e.Format)
55 } else {
56 list = parseEnvelope(msg, e.Format,
57 acct.UiConfig().TimestampFormat)
58 }
59 } else {
60 return fmt.Errorf("Selected message is empty.")
61 }
62 }
63
64 app.AddDialog(app.DefaultDialog(
65 app.NewListBox(
66 "Message Envelope. Press <Esc> or <Enter> to close. "+
67 "Start typing to filter.",
68 list,
69 app.SelectedAccountUiConfig(),
70 func(_ string) {
71 app.CloseDialog()
72 },
73 ),
74 ))
75
76 return nil
77 }
78
79 func parseEnvelope(msg *models.MessageInfo, fmtStr, fmtTime string,
80 ) (result []string) {
81 if envlp := msg.Envelope; envlp != nil {
82 addStr := func(key, text string) {
83 result = append(result, fmt.Sprintf(fmtStr, key, text))
84 }
85 addAddr := func(key string, ls []*mail.Address) {
86 for _, l := range ls {
87 result = append(result,
88 fmt.Sprintf(fmtStr, key,
89 format.AddressForHumans(l)))
90 }
91 }
92
93 addStr("Date", envlp.Date.Format(fmtTime))
94 addStr("Subject", envlp.Subject)
95 addStr("Message-Id", envlp.MessageId)
96
97 addAddr("From", envlp.From)
98 addAddr("To", envlp.To)
99 addAddr("ReplyTo", envlp.ReplyTo)
100 addAddr("Cc", envlp.Cc)
101 addAddr("Bcc", envlp.Bcc)
102 }
103 return
104 }
105
106 func parseHeader(msg *models.MessageInfo, fmtStr string) (result []string) {
107 if h := msg.RFC822Headers; h != nil {
108 hf := h.Fields()
109 for hf.Next() {
110 text, err := hf.Text()
111 if err != nil {
112 log.Errorf(err.Error())
113 text = hf.Value()
114 }
115 result = append(result,
116 headerExpand(fmtStr, hf.Key(), text)...)
117 }
118 }
119 return
120 }
121
122 func headerExpand(fmtStr, key, text string) []string {
123 var result []string
124 switch strings.ToLower(key) {
125 case "to", "from", "bcc", "cc":
126 for _, item := range strings.Split(text, ",") {
127 result = append(result, fmt.Sprintf(fmtStr, key,
128 strings.TrimSpace(item)))
129 }
130 default:
131 result = append(result, fmt.Sprintf(fmtStr, key, text))
132 }
133 return result
134 }