| 1 |
package compose |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"context" |
| 6 |
"fmt" |
| 7 |
"io" |
| 8 |
"net/url" |
| 9 |
"slices" |
| 10 |
"strings" |
| 11 |
"time" |
| 12 |
|
| 13 |
"github.com/pkg/errors" |
| 14 |
|
| 15 |
"git.sr.ht/~rjarry/aerc/app" |
| 16 |
"git.sr.ht/~rjarry/aerc/commands" |
| 17 |
"git.sr.ht/~rjarry/aerc/commands/mode" |
| 18 |
"git.sr.ht/~rjarry/aerc/commands/msg" |
| 19 |
"git.sr.ht/~rjarry/aerc/lib/hooks" |
| 20 |
"git.sr.ht/~rjarry/aerc/lib/log" |
| 21 |
"git.sr.ht/~rjarry/aerc/lib/send" |
| 22 |
"git.sr.ht/~rjarry/aerc/models" |
| 23 |
"git.sr.ht/~rjarry/aerc/worker/types" |
| 24 |
"git.sr.ht/~rjarry/go-opt/v2" |
| 25 |
"github.com/emersion/go-message/mail" |
| 26 |
) |
| 27 |
|
| 28 |
type Send struct { |
| 29 |
Archive string `opt:"-a" action:"ParseArchive" metavar:"flat|year|month" complete:"CompleteArchive" desc:"Archive the message being replied to."` |
| 30 |
CopyTo []string `opt:"-t" complete:"CompleteFolders" action:"ParseCopyTo" desc:"Override the Copy-To folders."` |
| 31 |
|
| 32 |
CopyToReplied bool `opt:"-r" desc:"Save sent message to current folder."` |
| 33 |
NoCopyToReplied bool `opt:"-R" desc:"Do not save sent message to current folder."` |
| 34 |
|
| 35 |
RequestDSN bool `opt:"--dsn" desc:"Request full delivery status notification (including success)."` |
| 36 |
} |
| 37 |
|
| 38 |
func init() { |
| 39 |
commands.Register(Send{}) |
| 40 |
} |
| 41 |
|
| 42 |
func (Send) Description() string { |
| 43 |
return "Send the message using the configured outgoing transport." |
| 44 |
} |
| 45 |
|
| 46 |
func (Send) Context() commands.CommandContext { |
| 47 |
return commands.COMPOSE_REVIEW |
| 48 |
} |
| 49 |
|
| 50 |
func (Send) Aliases() []string { |
| 51 |
return []string{"send"} |
| 52 |
} |
| 53 |
|
| 54 |
func (*Send) CompleteArchive(arg string) []string { |
| 55 |
return commands.FilterList(msg.ARCHIVE_TYPES, arg, nil) |
| 56 |
} |
| 57 |
|
| 58 |
func (*Send) CompleteFolders(arg string) []string { |
| 59 |
return commands.GetFolders(arg) |
| 60 |
} |
| 61 |
|
| 62 |
func (s *Send) ParseArchive(arg string) error { |
| 63 |
if slices.Contains(msg.ARCHIVE_TYPES, arg) { |
| 64 |
s.Archive = arg |
| 65 |
return nil |
| 66 |
} |
| 67 |
return errors.New("unsupported archive type") |
| 68 |
} |
| 69 |
|
| 70 |
func (o *Send) ParseCopyTo(arg string) error { |
| 71 |
o.CopyTo = append(o.CopyTo, strings.Split(arg, ",")...) |
| 72 |
return nil |
| 73 |
} |
| 74 |
|
| 75 |
func (s Send) Execute(args []string) error { |
| 76 |
tab := app.SelectedTab() |
| 77 |
if tab == nil { |
| 78 |
return errors.New("No selected tab") |
| 79 |
} |
| 80 |
composer, _ := tab.Content.(*app.Composer) |
| 81 |
|
| 82 |
err := composer.CheckForMultipartErrors() |
| 83 |
if err != nil { |
| 84 |
return err |
| 85 |
} |
| 86 |
|
| 87 |
config := composer.Config() |
| 88 |
|
| 89 |
if len(s.CopyTo) == 0 { |
| 90 |
s.CopyTo = config.CopyTo |
| 91 |
} |
| 92 |
copyToReplied := config.CopyToReplied || (s.CopyToReplied && !s.NoCopyToReplied) |
| 93 |
|
| 94 |
outgoing, err := config.Outgoing.ConnectionString() |
| 95 |
if err != nil { |
| 96 |
return errors.Wrap(err, "ReadCredentials(outgoing)") |
| 97 |
} |
| 98 |
if outgoing == "" { |
| 99 |
return errors.New( |
| 100 |
"No outgoing mail transport configured for this account") |
| 101 |
} |
| 102 |
|
| 103 |
header, err := composer.PrepareHeader() |
| 104 |
if err != nil { |
| 105 |
return errors.Wrap(err, "PrepareHeader") |
| 106 |
} |
| 107 |
rcpts, err := listRecipients(header) |
| 108 |
if err != nil { |
| 109 |
return errors.Wrap(err, "listRecipients") |
| 110 |
} |
| 111 |
if len(rcpts) == 0 { |
| 112 |
return errors.New("Cannot send message with no recipients") |
| 113 |
} |
| 114 |
|
| 115 |
if config.StripBcc { |
| 116 |
// Do NOT leak Bcc addresses to all recipients. |
| 117 |
header.Del("Bcc") |
| 118 |
} |
| 119 |
|
| 120 |
uri, err := url.Parse(outgoing) |
| 121 |
if err != nil { |
| 122 |
return errors.Wrap(err, "url.Parse(outgoing)") |
| 123 |
} |
| 124 |
|
| 125 |
var domain string |
| 126 |
if domain_, ok := config.Params["smtp-domain"]; ok { |
| 127 |
domain = domain_ |
| 128 |
} |
| 129 |
from := config.From |
| 130 |
if config.UseEnvelopeFrom { |
| 131 |
if fl, _ := header.AddressList("from"); len(fl) != 0 { |
| 132 |
from = fl[0] |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
log.Debugf("send config uri: %s", uri.Redacted()) |
| 137 |
log.Debugf("send config from: %s", from) |
| 138 |
log.Debugf("send config rcpts: %s", rcpts) |
| 139 |
log.Debugf("send config domain: %s", domain) |
| 140 |
|
| 141 |
warnSubject := composer.ShouldWarnSubject() |
| 142 |
warnAttachment := composer.ShouldWarnAttachment() |
| 143 |
if warnSubject || warnAttachment { |
| 144 |
var msg string |
| 145 |
switch { |
| 146 |
case warnSubject && warnAttachment: |
| 147 |
msg = "The subject is empty, and you may have forgotten an attachment." |
| 148 |
case warnSubject: |
| 149 |
msg = "The subject is empty." |
| 150 |
default: |
| 151 |
msg = "You may have forgotten an attachment." |
| 152 |
} |
| 153 |
|
| 154 |
prompt := app.NewPrompt( |
| 155 |
msg+" Abort send? [Y/n] ", |
| 156 |
func(text string) { |
| 157 |
if text == "n" || text == "N" { |
| 158 |
sendHelper(composer, header, uri, domain, |
| 159 |
from, rcpts, tab.Name, s.CopyTo, |
| 160 |
s.Archive, copyToReplied, |
| 161 |
s.RequestDSN) |
| 162 |
} |
| 163 |
}, func(ctx context.Context, cmd string) ([]opt.Completion, string) { |
| 164 |
var comps []opt.Completion |
| 165 |
if cmd == "" { |
| 166 |
comps = append(comps, opt.Completion{Value: "y"}) |
| 167 |
comps = append(comps, opt.Completion{Value: "n"}) |
| 168 |
} |
| 169 |
return comps, "" |
| 170 |
}, |
| 171 |
) |
| 172 |
|
| 173 |
app.PushPrompt(prompt) |
| 174 |
} else { |
| 175 |
sendHelper(composer, header, uri, domain, from, rcpts, tab.Name, |
| 176 |
s.CopyTo, s.Archive, copyToReplied, s.RequestDSN) |
| 177 |
} |
| 178 |
|
| 179 |
return nil |
| 180 |
} |
| 181 |
|
| 182 |
func sendHelper(composer *app.Composer, header *mail.Header, uri *url.URL, domain string, |
| 183 |
from *mail.Address, rcpts []*mail.Address, tabName string, copyTo []string, |
| 184 |
archive string, copyToReplied bool, requestDSN bool, |
| 185 |
) { |
| 186 |
// we don't want to block the UI thread while we are sending |
| 187 |
// so we do everything in a goroutine and hide the composer from the user |
| 188 |
app.RemoveTab(composer, false) |
| 189 |
app.PushStatus("Sending...", 10*time.Second) |
| 190 |
|
| 191 |
// enter no-quit mode |
| 192 |
mode.NoQuit() |
| 193 |
|
| 194 |
var shouldCopy bool = (len(copyTo) > 0 || copyToReplied) && !strings.HasPrefix(uri.Scheme, "jmap") |
| 195 |
var copyBuf bytes.Buffer |
| 196 |
|
| 197 |
failCh := make(chan error) |
| 198 |
// writer |
| 199 |
go func() { |
| 200 |
defer log.PanicHandler() |
| 201 |
|
| 202 |
var msgBuf bytes.Buffer |
| 203 |
|
| 204 |
err := composer.WriteMessage(header, &msgBuf) |
| 205 |
if err != nil { |
| 206 |
failCh <- err |
| 207 |
return |
| 208 |
} |
| 209 |
|
| 210 |
var folders []string |
| 211 |
folders = append(folders, copyTo...) |
| 212 |
if copyToReplied && composer.Parent() != nil { |
| 213 |
folders = append(folders, composer.Parent().Folder) |
| 214 |
} |
| 215 |
|
| 216 |
var sender io.WriteCloser |
| 217 |
sender, err = send.NewSender( |
| 218 |
composer.Worker(), uri, domain, |
| 219 |
from, rcpts, composer.Account().Name(), |
| 220 |
folders, requestDSN, |
| 221 |
) |
| 222 |
if err != nil { |
| 223 |
failCh <- errors.Wrap(err, "send:") |
| 224 |
return |
| 225 |
} |
| 226 |
|
| 227 |
if shouldCopy { |
| 228 |
_, err = copyBuf.Write(msgBuf.Bytes()) |
| 229 |
if err != nil { |
| 230 |
failCh <- err |
| 231 |
return |
| 232 |
} |
| 233 |
} |
| 234 |
_, err = io.Copy(sender, &msgBuf) |
| 235 |
if err != nil { |
| 236 |
failCh <- err |
| 237 |
return |
| 238 |
} |
| 239 |
|
| 240 |
failCh <- sender.Close() |
| 241 |
}() |
| 242 |
|
| 243 |
// cleanup + copy to sent |
| 244 |
go func() { |
| 245 |
defer log.PanicHandler() |
| 246 |
|
| 247 |
// leave no-quit mode |
| 248 |
defer mode.NoQuitDone() |
| 249 |
|
| 250 |
err := <-failCh |
| 251 |
if err != nil { |
| 252 |
app.PushError(strings.ReplaceAll(err.Error(), "\n", " ")) |
| 253 |
app.NewTab(composer, tabName) |
| 254 |
return |
| 255 |
} |
| 256 |
if shouldCopy { |
| 257 |
app.PushStatus("Copying to copy-to folders", 10*time.Second) |
| 258 |
errch := copyToSent(copyTo, copyToReplied, copyBuf.Len(), |
| 259 |
©Buf, composer) |
| 260 |
err = <-errch |
| 261 |
if err != nil { |
| 262 |
errmsg := fmt.Sprintf( |
| 263 |
"message sent, but copying to %v failed: %v", |
| 264 |
copyTo, err.Error()) |
| 265 |
app.PushError(errmsg) |
| 266 |
composer.SetSent(archive) |
| 267 |
composer.Close() |
| 268 |
return |
| 269 |
} |
| 270 |
} |
| 271 |
app.PushStatus("Message sent.", 10*time.Second) |
| 272 |
composer.SetSent(archive) |
| 273 |
err = hooks.RunHook(&hooks.MailSent{ |
| 274 |
Account: composer.Account().Name(), |
| 275 |
Backend: composer.Account().AccountConfig().Backend, |
| 276 |
Header: header, |
| 277 |
}) |
| 278 |
if err != nil { |
| 279 |
log.Errorf("failed to trigger mail-sent hook: %v", err) |
| 280 |
composer.Account().PushError(fmt.Errorf("[hook.mail-sent] failed: %w", err)) |
| 281 |
} |
| 282 |
composer.Close() |
| 283 |
}() |
| 284 |
} |
| 285 |
|
| 286 |
func listRecipients(h *mail.Header) ([]*mail.Address, error) { |
| 287 |
var rcpts []*mail.Address |
| 288 |
for _, key := range []string{"to", "cc", "bcc"} { |
| 289 |
list, err := h.AddressList(key) |
| 290 |
if err != nil { |
| 291 |
return nil, err |
| 292 |
} |
| 293 |
rcpts = append(rcpts, list...) |
| 294 |
} |
| 295 |
return rcpts, nil |
| 296 |
} |
| 297 |
|
| 298 |
func copyToSent(dests []string, copyToReplied bool, n int, msg *bytes.Buffer, composer *app.Composer) <-chan error { |
| 299 |
errCh := make(chan error, 1) |
| 300 |
acct := composer.Account() |
| 301 |
if acct == nil { |
| 302 |
errCh <- errors.New("No account selected") |
| 303 |
return errCh |
| 304 |
} |
| 305 |
store := acct.Store() |
| 306 |
if store == nil { |
| 307 |
errCh <- errors.New("No message store selected") |
| 308 |
return errCh |
| 309 |
} |
| 310 |
for _, dest := range dests { |
| 311 |
store.Append( |
| 312 |
dest, |
| 313 |
models.SeenFlag, |
| 314 |
time.Now(), |
| 315 |
bytes.NewReader(msg.Bytes()), |
| 316 |
n, |
| 317 |
func(msg types.WorkerMessage) { |
| 318 |
switch msg := msg.(type) { |
| 319 |
case *types.Done: |
| 320 |
errCh <- nil |
| 321 |
case *types.Error: |
| 322 |
errCh <- msg.Error |
| 323 |
} |
| 324 |
}, |
| 325 |
) |
| 326 |
} |
| 327 |
if copyToReplied && composer.Parent() != nil { |
| 328 |
store.Append( |
| 329 |
composer.Parent().Folder, |
| 330 |
models.SeenFlag, |
| 331 |
time.Now(), |
| 332 |
bytes.NewReader(msg.Bytes()), |
| 333 |
n, |
| 334 |
func(msg types.WorkerMessage) { |
| 335 |
switch msg := msg.(type) { |
| 336 |
case *types.Done: |
| 337 |
errCh <- nil |
| 338 |
case *types.Error: |
| 339 |
errCh <- msg.Error |
| 340 |
} |
| 341 |
}, |
| 342 |
) |
| 343 |
} |
| 344 |
return errCh |
| 345 |
} |