diff options
author | Karel Balej <balejk@matfyz.cz> | 2024-01-30 20:11:27 +0100 |
---|---|---|
committer | Robin Jarry <robin@jarry.cc> | 2024-02-12 22:58:40 +0100 |
commit | 3553e4f27165b18be84123d0ca015a019d35e41c (patch) | |
tree | 7c007d4bc65e242d65f0bb2a8ba1a99f7a7bb4ad /lib/send/sender.go | |
parent | 324e620c5a62fee07970c436f792c7383a3fb1e5 (diff) | |
download | aerc-3553e4f27165b18be84123d0ca015a019d35e41c.tar.gz |
send: move code to lib for reuse
Move the code which handles the preparation of a sender into which the
message can be written into lib to allow for reuse. Also hide the
sending backend a bit more from the `:send` command code by introducing
a NewSender function which determines which backend should be used and
invokes the appropriate sender factory function.
Rename send() to sendHelper() to avoid collision.
Signed-off-by: Karel Balej <balejk@matfyz.cz>
Acked-by: Robin Jarry <robin@jarry.cc>
Diffstat (limited to 'lib/send/sender.go')
-rw-r--r-- | lib/send/sender.go | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/lib/send/sender.go b/lib/send/sender.go new file mode 100644 index 00000000..a1e03da5 --- /dev/null +++ b/lib/send/sender.go @@ -0,0 +1,35 @@ +package send + +import ( + "fmt" + "io" + "net/url" + + "github.com/emersion/go-message/mail" + + "git.sr.ht/~rjarry/aerc/worker/types" +) + +// NewSender returns an io.WriterCloser into which the caller can write +// contents of a message. The caller must invoke the Close() method on the +// sender when finished. +func NewSender( + worker *types.Worker, uri *url.URL, domain string, + from *mail.Address, rcpts []*mail.Address, +) (io.WriteCloser, error) { + protocol, auth, err := parseScheme(uri) + if err != nil { + return nil, err + } + + switch protocol { + case "smtp", "smtp+insecure", "smtps": + return newSmtpSender(protocol, auth, uri, domain, from, rcpts) + case "jmap": + return newJmapSender(worker, from, rcpts) + case "": + return newSendmailSender(uri, rcpts) + default: + return nil, fmt.Errorf("unsupported protocol %s", protocol) + } +} |