1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
|
package msg
import (
"errors"
"fmt"
"strings"
"git.sr.ht/~rjarry/aerc/app"
"git.sr.ht/~rjarry/aerc/lib/format"
"git.sr.ht/~rjarry/aerc/log"
"git.sr.ht/~rjarry/aerc/models"
"git.sr.ht/~sircmpwn/getopt"
"github.com/emersion/go-message/mail"
)
type Envelope struct{}
func init() {
register(Envelope{})
}
func (Envelope) Aliases() []string {
return []string{"envelope"}
}
func (Envelope) Complete(aerc *app.Aerc, args []string) []string {
return nil
}
func (Envelope) Execute(aerc *app.Aerc, args []string) error {
header := false
fmtStr := "%-20.20s: %s"
opts, _, err := getopt.Getopts(args, "hs:")
if err != nil {
return err
}
for _, opt := range opts {
switch opt.Option {
case 's':
fmtStr = opt.Value
case 'h':
header = true
}
}
acct := aerc.SelectedAccount()
if acct == nil {
return errors.New("No account selected")
}
var list []string
if msg, err := acct.SelectedMessage(); err != nil {
return err
} else {
if msg != nil {
if header {
list = parseHeader(msg, fmtStr)
} else {
list = parseEnvelope(msg, fmtStr,
acct.UiConfig().TimestampFormat)
}
} else {
return fmt.Errorf("Selected message is empty.")
}
}
n := len(list)
aerc.AddDialog(app.NewDialog(
app.NewListBox(
"Message Envelope. Press <Esc> or <Enter> to close. "+
"Start typing to filter.",
list,
aerc.SelectedAccountUiConfig(),
func(_ string) {
aerc.CloseDialog()
},
),
// start pos on screen
func(h int) int {
if n < h/8*6 {
return h/2 - n/2 - 1
}
return h / 8
},
// dialog height
func(h int) int {
if n < h/8*6 {
return n + 2
}
return h / 8 * 6
},
))
return nil
}
func parseEnvelope(msg *models.MessageInfo, fmtStr, fmtTime string,
) (result []string) {
if envlp := msg.Envelope; envlp != nil {
addStr := func(key, text string) {
result = append(result, fmt.Sprintf(fmtStr, key, text))
}
addAddr := func(key string, ls []*mail.Address) {
for _, l := range ls {
result = append(result,
fmt.Sprintf(fmtStr, key,
format.AddressForHumans(l)))
}
}
addStr("Date", envlp.Date.Format(fmtTime))
addStr("Subject", envlp.Subject)
addStr("Message-Id", envlp.MessageId)
addAddr("From", envlp.From)
addAddr("To", envlp.To)
addAddr("ReplyTo", envlp.ReplyTo)
addAddr("Cc", envlp.Cc)
addAddr("Bcc", envlp.Bcc)
}
return
}
func parseHeader(msg *models.MessageInfo, fmtStr string) (result []string) {
if h := msg.RFC822Headers; h != nil {
hf := h.Fields()
for hf.Next() {
text, err := hf.Text()
if err != nil {
log.Errorf(err.Error())
text = hf.Value()
}
result = append(result,
headerExpand(fmtStr, hf.Key(), text)...)
}
}
return
}
func headerExpand(fmtStr, key, text string) []string {
var result []string
switch strings.ToLower(key) {
case "to", "from", "bcc", "cc":
for _, item := range strings.Split(text, ",") {
result = append(result, fmt.Sprintf(fmtStr, key,
strings.TrimSpace(item)))
}
default:
result = append(result, fmt.Sprintf(fmtStr, key, text))
}
return result
}
|