| 1 |
package msg |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"errors" |
| 6 |
"fmt" |
| 7 |
"io" |
| 8 |
"regexp" |
| 9 |
"strings" |
| 10 |
"time" |
| 11 |
|
| 12 |
"git.sr.ht/~rjarry/aerc/app" |
| 13 |
"git.sr.ht/~rjarry/aerc/commands" |
| 14 |
"git.sr.ht/~rjarry/aerc/commands/account" |
| 15 |
"git.sr.ht/~rjarry/aerc/config" |
| 16 |
"git.sr.ht/~rjarry/aerc/lib" |
| 17 |
"git.sr.ht/~rjarry/aerc/lib/crypto" |
| 18 |
"git.sr.ht/~rjarry/aerc/lib/format" |
| 19 |
"git.sr.ht/~rjarry/aerc/lib/log" |
| 20 |
"git.sr.ht/~rjarry/aerc/lib/parse" |
| 21 |
"git.sr.ht/~rjarry/aerc/models" |
| 22 |
"github.com/danwakefield/fnmatch" |
| 23 |
"github.com/emersion/go-message/mail" |
| 24 |
) |
| 25 |
|
| 26 |
type reply struct { |
| 27 |
All bool `opt:"-a" desc:"Reply to all recipients."` |
| 28 |
Close bool `opt:"-c" desc:"Close the view tab when replying."` |
| 29 |
From bool `opt:"-f" desc:"Reply to all addresses in From and Reply-To headers."` |
| 30 |
List bool `opt:"-l" desc:"Reply to the mailing list in List-Post header"` |
| 31 |
Quote bool `opt:"-q" desc:"Alias of -T quoted-reply."` |
| 32 |
Template string `opt:"-T" complete:"CompleteTemplate" desc:"Template name."` |
| 33 |
Edit bool `opt:"-e" desc:"Force [compose].edit-headers = true."` |
| 34 |
NoEdit bool `opt:"-E" desc:"Force [compose].edit-headers = false."` |
| 35 |
Account string `opt:"-A" complete:"CompleteAccount" desc:"Reply with the specified account."` |
| 36 |
SkipEditor bool `opt:"-s" desc:"Skip the editor and go directly to the review screen."` |
| 37 |
} |
| 38 |
|
| 39 |
func init() { |
| 40 |
commands.Register(reply{}) |
| 41 |
} |
| 42 |
|
| 43 |
func (reply) Description() string { |
| 44 |
return "Open the composer to reply to the selected message." |
| 45 |
} |
| 46 |
|
| 47 |
func (reply) Context() commands.CommandContext { |
| 48 |
return commands.MESSAGE_LIST | commands.MESSAGE_VIEWER |
| 49 |
} |
| 50 |
|
| 51 |
func (reply) Aliases() []string { |
| 52 |
return []string{"reply"} |
| 53 |
} |
| 54 |
|
| 55 |
func (*reply) CompleteTemplate(arg string) []string { |
| 56 |
return commands.GetTemplates(arg) |
| 57 |
} |
| 58 |
|
| 59 |
func (*reply) CompleteAccount(arg string) []string { |
| 60 |
return commands.FilterList(app.AccountNames(), arg, nil) |
| 61 |
} |
| 62 |
|
| 63 |
func (r reply) Execute(args []string) error { |
| 64 |
editHeaders := (config.Compose().EditHeaders || r.Edit) && !r.NoEdit |
| 65 |
|
| 66 |
widget := app.SelectedTabContent().(app.ProvidesMessage) |
| 67 |
|
| 68 |
var acct *app.AccountView |
| 69 |
var err error |
| 70 |
|
| 71 |
if r.Account == "" { |
| 72 |
acct = widget.SelectedAccount() |
| 73 |
if acct == nil { |
| 74 |
return errors.New("No account selected") |
| 75 |
} |
| 76 |
} else { |
| 77 |
acct, err = app.Account(r.Account) |
| 78 |
if err != nil { |
| 79 |
return err |
| 80 |
} |
| 81 |
} |
| 82 |
conf := acct.AccountConfig() |
| 83 |
|
| 84 |
msg, err := widget.SelectedMessage() |
| 85 |
if err != nil { |
| 86 |
return err |
| 87 |
} |
| 88 |
|
| 89 |
from, err := chooseFromAddr(conf, msg) |
| 90 |
if err != nil { |
| 91 |
return err |
| 92 |
} |
| 93 |
|
| 94 |
var ( |
| 95 |
to []*mail.Address |
| 96 |
cc []*mail.Address |
| 97 |
) |
| 98 |
|
| 99 |
recSet := newAddrSet() // used for de-duping |
| 100 |
dedupe := func(addrs []*mail.Address) []*mail.Address { |
| 101 |
deduped := make([]*mail.Address, 0, len(addrs)) |
| 102 |
for _, addr := range addrs { |
| 103 |
if recSet.Contains(addr) { |
| 104 |
continue |
| 105 |
} |
| 106 |
recSet.Add(addr) |
| 107 |
deduped = append(deduped, addr) |
| 108 |
} |
| 109 |
return deduped |
| 110 |
} |
| 111 |
|
| 112 |
if config.Compose().ReplyToSelf { |
| 113 |
// We accept to reply to ourselves, so don't exclude our own address |
| 114 |
// from the reply's recipients. |
| 115 |
} else { |
| 116 |
recSet.Add(from) |
| 117 |
} |
| 118 |
|
| 119 |
switch { |
| 120 |
case len(msg.Envelope.ReplyTo) != 0: |
| 121 |
to = dedupe(msg.Envelope.ReplyTo) |
| 122 |
case len(msg.Envelope.From) != 0: |
| 123 |
to = dedupe(msg.Envelope.From) |
| 124 |
default: |
| 125 |
to = dedupe(msg.Envelope.Sender) |
| 126 |
} |
| 127 |
|
| 128 |
if r.From { |
| 129 |
to = append(to, dedupe(msg.Envelope.From)...) |
| 130 |
} |
| 131 |
|
| 132 |
if r.List { |
| 133 |
post, _ := msg.RFC822Headers.Text("List-Post") |
| 134 |
if addrs, err := parse.Mailto(post); err == nil { |
| 135 |
to = addrs |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
if !config.Compose().ReplyToSelf && len(to) == 0 { |
| 140 |
recSet = newAddrSet() |
| 141 |
to = dedupe(msg.Envelope.To) |
| 142 |
} |
| 143 |
|
| 144 |
if r.All { |
| 145 |
// order matters, due to the deduping |
| 146 |
// in order of importance, first parse the To, then the Cc header |
| 147 |
|
| 148 |
to = append(to, dedupe(msg.Envelope.To)...) |
| 149 |
|
| 150 |
cc = append(cc, dedupe(msg.Envelope.Cc)...) |
| 151 |
cc = append(cc, dedupe(msg.Envelope.Sender)...) |
| 152 |
} |
| 153 |
|
| 154 |
subject := "Re: " + trimLocalizedRe(msg.Envelope.Subject, conf.LocalizedRe) |
| 155 |
|
| 156 |
h := &mail.Header{} |
| 157 |
h.SetAddressList("to", to) |
| 158 |
h.SetAddressList("cc", cc) |
| 159 |
h.SetAddressList("from", []*mail.Address{from}) |
| 160 |
h.SetSubject(subject) |
| 161 |
h.SetMsgIDList("in-reply-to", []string{msg.Envelope.MessageId}) |
| 162 |
err = setReferencesHeader(h, msg.RFC822Headers) |
| 163 |
if err != nil { |
| 164 |
app.PushError(fmt.Sprintf("could not set references: %v", err)) |
| 165 |
} |
| 166 |
original := models.OriginalMail{ |
| 167 |
From: format.FormatAddresses(msg.Envelope.From), |
| 168 |
Date: msg.Envelope.Date, |
| 169 |
RFC822Headers: msg.RFC822Headers, |
| 170 |
} |
| 171 |
|
| 172 |
mv, isMsgViewer := app.SelectedTabContent().(*app.MessageViewer) |
| 173 |
|
| 174 |
store := widget.Store() |
| 175 |
noStore := store == nil |
| 176 |
switch { |
| 177 |
case noStore && isMsgViewer: |
| 178 |
app.PushWarning("No message store found: answered flag cannot be set") |
| 179 |
case noStore: |
| 180 |
return errors.New("Cannot perform action. Messages still loading") |
| 181 |
default: |
| 182 |
original.Folder = store.Name |
| 183 |
} |
| 184 |
|
| 185 |
addTab := func() error { |
| 186 |
composer, err := app.NewComposer(acct, |
| 187 |
acct.AccountConfig(), acct.Worker(), editHeaders, |
| 188 |
r.Template, h, &original, nil) |
| 189 |
if err != nil { |
| 190 |
app.PushError("Error: " + err.Error()) |
| 191 |
return err |
| 192 |
} |
| 193 |
if mv != nil && r.Close { |
| 194 |
app.RemoveTab(mv, true) |
| 195 |
} |
| 196 |
|
| 197 |
if r.SkipEditor { |
| 198 |
composer.Terminal().Close() |
| 199 |
} else if args[0] == "reply" { |
| 200 |
composer.FocusTerminal() |
| 201 |
} |
| 202 |
|
| 203 |
composer.Tab = app.NewTab(composer, subject) |
| 204 |
|
| 205 |
composer.OnClose(func(c *app.Composer) { |
| 206 |
switch { |
| 207 |
case c.Sent() && c.Archive() != "" && !noStore: |
| 208 |
store.Answered([]models.UID{msg.Uid}, true, nil) |
| 209 |
err := archive([]*models.MessageInfo{msg}, nil, c.Archive(), acct, store) |
| 210 |
if err != nil { |
| 211 |
app.PushStatus("Archive failed", 10*time.Second) |
| 212 |
} |
| 213 |
case c.Sent() && !noStore: |
| 214 |
store.Answered([]models.UID{msg.Uid}, true, nil) |
| 215 |
case mv != nil && r.Close: |
| 216 |
view := account.ViewMessage{Peek: true} |
| 217 |
//nolint:errcheck // who cares? |
| 218 |
view.Execute([]string{"view", "-p"}) |
| 219 |
} |
| 220 |
}) |
| 221 |
|
| 222 |
return nil |
| 223 |
} |
| 224 |
|
| 225 |
if r.Quote && r.Template == "" { |
| 226 |
r.Template = config.Templates().QuotedReply |
| 227 |
} |
| 228 |
|
| 229 |
if r.Template != "" { |
| 230 |
var fetchBodyPart func([]int, func(io.Reader)) |
| 231 |
|
| 232 |
if isMsgViewer { |
| 233 |
fetchBodyPart = mv.MessageView().FetchBodyPart |
| 234 |
} else { |
| 235 |
fetchBodyPart = func(part []int, cb func(io.Reader)) { |
| 236 |
store.FetchBodyPart(context.TODO(), msg.Uid, part, cb) |
| 237 |
} |
| 238 |
} |
| 239 |
|
| 240 |
if crypto.IsEncrypted(msg.BodyStructure) && !isMsgViewer { |
| 241 |
return fmt.Errorf("message is encrypted. " + |
| 242 |
"can only include reply from the message viewer") |
| 243 |
} |
| 244 |
|
| 245 |
part := getMessagePart(msg, widget) |
| 246 |
if part == nil { |
| 247 |
// mkey... let's get the first thing that isn't a container |
| 248 |
// if that's still nil it's either not a multipart msg (ok) or |
| 249 |
// broken (containers only) |
| 250 |
part = lib.FindFirstNonMultipart(msg.BodyStructure, nil) |
| 251 |
} |
| 252 |
|
| 253 |
err = addMimeType(msg, part, &original) |
| 254 |
if err != nil { |
| 255 |
return err |
| 256 |
} |
| 257 |
|
| 258 |
fetchBodyPart(part, func(reader io.Reader) { |
| 259 |
data, err := io.ReadAll(reader) |
| 260 |
if err != nil { |
| 261 |
log.Warnf("failed to read bodypart: %v", err) |
| 262 |
} |
| 263 |
original.Text = string(data) |
| 264 |
err = addTab() |
| 265 |
if err != nil { |
| 266 |
log.Warnf("failed to add tab: %v", err) |
| 267 |
} |
| 268 |
}) |
| 269 |
|
| 270 |
return nil |
| 271 |
} else { |
| 272 |
r.Template = config.Templates().NewMessage |
| 273 |
return addTab() |
| 274 |
} |
| 275 |
} |
| 276 |
|
| 277 |
func chooseFromAddr(conf *config.AccountConfig, msg *models.MessageInfo) (*mail.Address, error) { |
| 278 |
if len(conf.Aliases) == 0 { |
| 279 |
return conf.From, nil |
| 280 |
} |
| 281 |
|
| 282 |
rec := newAddrSet() |
| 283 |
rec.AddList(msg.Envelope.From) |
| 284 |
rec.AddList(msg.Envelope.To) |
| 285 |
rec.AddList(msg.Envelope.Cc) |
| 286 |
if conf.OriginalToHeader != "" && msg.RFC822Headers.Has(conf.OriginalToHeader) { |
| 287 |
origTo, err := msg.RFC822Headers.Text(conf.OriginalToHeader) |
| 288 |
if err != nil { |
| 289 |
return nil, err |
| 290 |
} |
| 291 |
|
| 292 |
origToAddress, err := mail.ParseAddressList(origTo) |
| 293 |
if err != nil { |
| 294 |
return nil, err |
| 295 |
} |
| 296 |
rec.AddList(origToAddress) |
| 297 |
} |
| 298 |
// test the from first, it has priority over any present alias |
| 299 |
if rec.Contains(conf.From) { |
| 300 |
// do nothing |
| 301 |
} else { |
| 302 |
for _, a := range conf.Aliases { |
| 303 |
if match := rec.FindMatch(a); match != "" { |
| 304 |
return &mail.Address{Name: a.Name, Address: match}, nil |
| 305 |
} |
| 306 |
} |
| 307 |
} |
| 308 |
|
| 309 |
return conf.From, nil |
| 310 |
} |
| 311 |
|
| 312 |
type addrSet map[string]struct{} |
| 313 |
|
| 314 |
func newAddrSet() addrSet { |
| 315 |
s := make(map[string]struct{}) |
| 316 |
return addrSet(s) |
| 317 |
} |
| 318 |
|
| 319 |
func (s addrSet) Add(a *mail.Address) { |
| 320 |
s[a.Address] = struct{}{} |
| 321 |
} |
| 322 |
|
| 323 |
func (s addrSet) AddList(al []*mail.Address) { |
| 324 |
for _, a := range al { |
| 325 |
s[a.Address] = struct{}{} |
| 326 |
} |
| 327 |
} |
| 328 |
|
| 329 |
func (s addrSet) Contains(a *mail.Address) bool { |
| 330 |
_, ok := s[a.Address] |
| 331 |
return ok |
| 332 |
} |
| 333 |
|
| 334 |
func (s addrSet) FindMatch(a *mail.Address) string { |
| 335 |
for addr := range s { |
| 336 |
if fnmatch.Match(a.Address, addr, 0) { |
| 337 |
return addr |
| 338 |
} |
| 339 |
} |
| 340 |
|
| 341 |
return "" |
| 342 |
} |
| 343 |
|
| 344 |
// setReferencesHeader adds the references header to target based on parent |
| 345 |
// according to RFC2822 |
| 346 |
func setReferencesHeader(target, parent *mail.Header) error { |
| 347 |
refs := parse.MsgIDList(parent, "references") |
| 348 |
if len(refs) == 0 { |
| 349 |
// according to the RFC we need to fall back to in-reply-to only if |
| 350 |
// References is not set |
| 351 |
refs = parse.MsgIDList(parent, "in-reply-to") |
| 352 |
} |
| 353 |
msgID, err := parent.MessageID() |
| 354 |
if err != nil { |
| 355 |
return err |
| 356 |
} |
| 357 |
refs = append(refs, msgID) |
| 358 |
target.SetMsgIDList("references", refs) |
| 359 |
return nil |
| 360 |
} |
| 361 |
|
| 362 |
// addMimeType adds the proper mime type of the part to the originalMail struct |
| 363 |
func addMimeType(msg *models.MessageInfo, part []int, |
| 364 |
orig *models.OriginalMail, |
| 365 |
) error { |
| 366 |
// caution, :forward uses the code as well, keep that in mind when modifying |
| 367 |
bs, err := msg.BodyStructure.PartAtIndex(part) |
| 368 |
if err != nil { |
| 369 |
return err |
| 370 |
} |
| 371 |
orig.MIMEType = bs.FullMIMEType() |
| 372 |
return nil |
| 373 |
} |
| 374 |
|
| 375 |
// trimLocalizedRe removes known localizations of Re: commonly used by Outlook. |
| 376 |
func trimLocalizedRe(subject string, localizedRe *regexp.Regexp) string { |
| 377 |
return strings.TrimPrefix(subject, localizedRe.FindString(subject)) |
| 378 |
} |