| 1 |
package send |
| 2 |
|
| 3 |
import ( |
| 4 |
"bufio" |
| 5 |
"bytes" |
| 6 |
"fmt" |
| 7 |
"io" |
| 8 |
"net/url" |
| 9 |
|
| 10 |
"github.com/emersion/go-message/mail" |
| 11 |
|
| 12 |
"git.sr.ht/~rjarry/aerc/lib/auth" |
| 13 |
"git.sr.ht/~rjarry/aerc/worker/types" |
| 14 |
) |
| 15 |
|
| 16 |
// NewSender returns an io.WriterCloser into which the caller can write |
| 17 |
// contents of a message. The caller must invoke the Close() method on the |
| 18 |
// sender when finished. |
| 19 |
func NewSender( |
| 20 |
worker *types.Worker, uri *url.URL, domain string, |
| 21 |
from *mail.Address, rcpts []*mail.Address, account string, |
| 22 |
copyTo []string, requestDSN bool, |
| 23 |
) (io.WriteCloser, error) { |
| 24 |
protocol, mech, err := auth.ParseScheme(uri) |
| 25 |
if err != nil { |
| 26 |
return nil, err |
| 27 |
} |
| 28 |
|
| 29 |
var w io.WriteCloser |
| 30 |
|
| 31 |
switch protocol { |
| 32 |
case "smtp", "smtp+insecure", "smtps", "smtps+insecure": |
| 33 |
w, err = newSmtpSender(protocol, mech, uri, domain, from, rcpts, account, requestDSN) |
| 34 |
case "jmap": |
| 35 |
w, err = newJmapSender(worker, from, rcpts, copyTo) |
| 36 |
case "": |
| 37 |
w, err = newSendmailSender(uri, rcpts) |
| 38 |
default: |
| 39 |
err = fmt.Errorf("unsupported protocol %s", protocol) |
| 40 |
} |
| 41 |
if err != nil { |
| 42 |
return nil, err |
| 43 |
} |
| 44 |
return &crlfWriter{w: w}, nil |
| 45 |
} |
| 46 |
|
| 47 |
type crlfWriter struct { |
| 48 |
w io.WriteCloser |
| 49 |
buf bytes.Buffer |
| 50 |
} |
| 51 |
|
| 52 |
func (w *crlfWriter) Write(p []byte) (int, error) { |
| 53 |
return w.buf.Write(p) |
| 54 |
} |
| 55 |
|
| 56 |
func (w *crlfWriter) Close() error { |
| 57 |
scan := bufio.NewScanner(&w.buf) |
| 58 |
for scan.Scan() { |
| 59 |
if _, err := w.w.Write(scan.Bytes()); err != nil { |
| 60 |
w.w.Close() |
| 61 |
return err |
| 62 |
} |
| 63 |
if _, err := w.w.Write([]byte{'\r', '\n'}); err != nil { |
| 64 |
w.w.Close() |
| 65 |
return err |
| 66 |
} |
| 67 |
} |
| 68 |
if scan.Err() != nil { |
| 69 |
w.w.Close() |
| 70 |
return scan.Err() |
| 71 |
} |
| 72 |
return w.w.Close() |
| 73 |
} |