diff options
author | 2020-01-28 12:30:07 +0100 | |
---|---|---|
committer | 2020-01-28 12:30:07 +0100 | |
commit | 85c01b87a99233fcc273e810b4ad48bb0d33096f (patch) | |
tree | 18c2bf279602f684db34ae5a0180f1f6b07a94b0 /plugins/base/smtp.go | |
parent | 50046b62ac61a985f82bfc22b4e7b39b334d030c (diff) | |
download | alps-85c01b87a99233fcc273e810b4ad48bb0d33096f.tar.gz alps-85c01b87a99233fcc273e810b4ad48bb0d33096f.tar.bz2 alps-85c01b87a99233fcc273e810b4ad48bb0d33096f.zip |
plugins/base: support attachments in drafts
References: https://todo.sr.ht/~sircmpwn/koushin/16
Diffstat (limited to 'plugins/base/smtp.go')
-rw-r--r-- | plugins/base/smtp.go | 58 |
1 files changed, 54 insertions, 4 deletions
diff --git a/plugins/base/smtp.go b/plugins/base/smtp.go index 663283d..489a6a1 100644 --- a/plugins/base/smtp.go +++ b/plugins/base/smtp.go @@ -2,8 +2,11 @@ package koushinbase import ( "bufio" + "bytes" "fmt" "io" + "io/ioutil" + "mime" "mime/multipart" "strings" "time" @@ -26,23 +29,70 @@ func quote(r io.Reader) (string, error) { return builder.String(), nil } +type Attachment interface { + MIMEType() string + Filename() string + Open() (io.ReadCloser, error) +} + +type formAttachment struct { + *multipart.FileHeader +} + +func (att *formAttachment) Open() (io.ReadCloser, error) { + return att.FileHeader.Open() +} + +func (att *formAttachment) MIMEType() string { + // TODO: retain params, e.g. "charset"? + t, _, _ := mime.ParseMediaType(att.FileHeader.Header.Get("Content-Type")) + return t +} + +func (att *formAttachment) Filename() string { + return att.FileHeader.Filename +} + +type imapAttachment struct { + Mailbox string + Uid uint32 + Node *IMAPPartNode + + Body []byte +} + +func (att *imapAttachment) Open() (io.ReadCloser, error) { + if att.Body == nil { + return nil, fmt.Errorf("IMAP attachment has not been pre-fetched") + } + return ioutil.NopCloser(bytes.NewReader(att.Body)), nil +} + +func (att *imapAttachment) MIMEType() string { + return att.Node.MIMEType +} + +func (att *imapAttachment) Filename() string { + return att.Node.Filename +} + type OutgoingMessage struct { From string To []string Subject string InReplyTo string Text string - Attachments []*multipart.FileHeader + Attachments []Attachment } func (msg *OutgoingMessage) ToString() string { return strings.Join(msg.To, ", ") } -func writeAttachment(mw *mail.Writer, att *multipart.FileHeader) error { +func writeAttachment(mw *mail.Writer, att Attachment) error { var h mail.AttachmentHeader - h.Set("Content-Type", att.Header.Get("Content-Type")) - h.SetFilename(att.Filename) + h.SetContentType(att.MIMEType(), nil) + h.SetFilename(att.Filename()) aw, err := mw.CreateAttachment(h) if err != nil { |