summary refs log tree commit diff stats
path: root/widgets
diff options
context:
space:
mode:
authorDrew DeVault <sir@cmpwn.com>2019-05-14 14:05:29 -0400
committerDrew DeVault <sir@cmpwn.com>2019-05-14 14:07:27 -0400
commit29de3297a157c0ac109121152fe2b5737fc81d95 (patch)
tree875fed0384d689392a7454c7a71fa2017d28d15c /widgets
parent6c36e04c1f7f7e222c71c5c8e7e7337744fe9c34 (diff)
downloadaerc-29de3297a157c0ac109121152fe2b5737fc81d95.tar.gz
Implement sending emails /o/
Diffstat (limited to 'widgets')
-rw-r--r--widgets/compose.go60
1 files changed, 42 insertions, 18 deletions
diff --git a/widgets/compose.go b/widgets/compose.go
index 318bfc4..38c33fc 100644
--- a/widgets/compose.go
+++ b/widgets/compose.go
@@ -107,6 +107,19 @@ func (c *Composer) OnInvalidate(fn func(d ui.Drawable)) {
 	})
 }
 
+func (c *Composer) Close() {
+	if c.email != nil {
+		path := c.email.Name()
+		c.email.Close()
+		os.Remove(path)
+		c.email = nil
+	}
+	if c.editor != nil {
+		c.editor.Destroy()
+		c.editor = nil
+	}
+}
+
 func (c *Composer) Event(event tcell.Event) bool {
 	return c.focusable[c.focused].Event(event)
 }
@@ -119,29 +132,19 @@ func (c *Composer) Config() *config.AccountConfig {
 	return c.config
 }
 
-// Writes the email to the given writer, and returns a list of recipients
-func (c *Composer) Message(writeto io.Writer) ([]string, error) {
+func (c *Composer) Header() (*mail.Header, []string, error) {
 	// Extract headers from the email, if present
 	c.email.Seek(0, os.SEEK_SET)
 	var (
 		rcpts  []string
 		header mail.Header
-		body   io.Reader
 	)
 	reader, err := mail.CreateReader(c.email)
 	if err == nil {
 		header = reader.Header
-		// TODO: Do we want to let users write a full blown multipart email
-		// into the editor? If so this needs to change
-		part, err := reader.NextPart()
-		if err != nil {
-			return nil, err
-		}
-		body = part.Body
 		defer reader.Close()
 	} else {
 		c.email.Seek(0, os.SEEK_SET)
-		body = c.email
 	}
 	// Update headers
 	// TODO: Custom header fields
@@ -161,11 +164,11 @@ func (c *Composer) Message(writeto io.Writer) ([]string, error) {
 		// your types aren't compatible enough with each other
 		to_rcpts, err := gomail.ParseAddressList(to)
 		if err != nil {
-			return nil, err
+			return nil, nil, err
 		}
 		ed_rcpts, err := header.AddressList("To")
 		if err != nil {
-			return nil, err
+			return nil, nil, err
 		}
 		for _, addr := range to_rcpts {
 			ed_rcpts = append(ed_rcpts, (*mail.Address)(addr))
@@ -176,14 +179,34 @@ func (c *Composer) Message(writeto io.Writer) ([]string, error) {
 		}
 	}
 	// TODO: Add cc, bcc to rcpts
+	return &header, rcpts, nil
+}
+
+func (c *Composer) WriteMessage(header *mail.Header, writer io.Writer) error {
+	c.email.Seek(0, os.SEEK_SET)
+	var body io.Reader
+	reader, err := mail.CreateReader(c.email)
+	if err == nil {
+		// TODO: Do we want to let users write a full blown multipart email
+		// into the editor? If so this needs to change
+		part, err := reader.NextPart()
+		if err != nil {
+			return err
+		}
+		body = part.Body
+		defer reader.Close()
+	} else {
+		c.email.Seek(0, os.SEEK_SET)
+		body = c.email
+	}
 	// TODO: attachments
-	writer, err := mail.CreateSingleInlineWriter(writeto, header)
+	w, err := mail.CreateSingleInlineWriter(writer, *header)
 	if err != nil {
-		return nil, err
+		return err
 	}
-	defer writer.Close()
-	io.Copy(writer, body)
-	return rcpts, nil
+	defer w.Close()
+	_, err = io.Copy(w, body)
+	return err
 }
 
 func (c *Composer) termClosed(err error) {
@@ -191,6 +214,7 @@ func (c *Composer) termClosed(err error) {
 	c.grid.RemoveChild(c.editor)
 	c.grid.AddChild(newReviewMessage(c)).At(1, 0)
 	c.editor.Destroy()
+	c.editor = nil
 }
 
 func (c *Composer) PrevField() {