| 1 |
package account |
| 2 |
|
| 3 |
import ( |
| 4 |
"errors" |
| 5 |
"fmt" |
| 6 |
"io" |
| 7 |
gomail "net/mail" |
| 8 |
"regexp" |
| 9 |
"strings" |
| 10 |
|
| 11 |
"github.com/emersion/go-message/mail" |
| 12 |
|
| 13 |
"git.sr.ht/~rjarry/aerc/app" |
| 14 |
"git.sr.ht/~rjarry/aerc/commands" |
| 15 |
"git.sr.ht/~rjarry/aerc/config" |
| 16 |
) |
| 17 |
|
| 18 |
type Compose struct { |
| 19 |
Headers string `opt:"-H" action:"ParseHeader" desc:"Add the specified header to the message."` |
| 20 |
Template string `opt:"-T" complete:"CompleteTemplate" desc:"Template name."` |
| 21 |
Edit bool `opt:"-e" desc:"Force [compose].edit-headers = true."` |
| 22 |
NoEdit bool `opt:"-E" desc:"Force [compose].edit-headers = false."` |
| 23 |
SkipEditor bool `opt:"-s" desc:"Skip the editor and go directly to the review screen."` |
| 24 |
Body string `opt:"..." required:"false"` |
| 25 |
} |
| 26 |
|
| 27 |
func init() { |
| 28 |
commands.Register(Compose{}) |
| 29 |
} |
| 30 |
|
| 31 |
func (Compose) Description() string { |
| 32 |
return "Open the compose window to write a new email." |
| 33 |
} |
| 34 |
|
| 35 |
func (Compose) Context() commands.CommandContext { |
| 36 |
return commands.MESSAGE_LIST |
| 37 |
} |
| 38 |
|
| 39 |
func (c *Compose) ParseHeader(arg string) error { |
| 40 |
if strings.Contains(arg, ":") { |
| 41 |
// ensure first colon is followed by a single space |
| 42 |
re := regexp.MustCompile(`^(.*?):\s*(.*)`) |
| 43 |
c.Headers += re.ReplaceAllString(arg, "$1: $2\r\n") |
| 44 |
} else { |
| 45 |
c.Headers += arg + ":\r\n" |
| 46 |
} |
| 47 |
return nil |
| 48 |
} |
| 49 |
|
| 50 |
func (*Compose) CompleteTemplate(arg string) []string { |
| 51 |
return commands.GetTemplates(arg) |
| 52 |
} |
| 53 |
|
| 54 |
func (Compose) Aliases() []string { |
| 55 |
return []string{"compose"} |
| 56 |
} |
| 57 |
|
| 58 |
func (c Compose) Execute(args []string) error { |
| 59 |
if c.Headers != "" { |
| 60 |
if c.Body != "" { |
| 61 |
c.Body = c.Headers + "\r\n" + c.Body |
| 62 |
} else { |
| 63 |
c.Body = c.Headers + "\r\n\r\n" |
| 64 |
} |
| 65 |
} |
| 66 |
if c.Template == "" { |
| 67 |
c.Template = config.Templates().NewMessage |
| 68 |
} |
| 69 |
editHeaders := (config.Compose().EditHeaders || c.Edit) && !c.NoEdit |
| 70 |
|
| 71 |
acct := app.SelectedAccount() |
| 72 |
if acct == nil { |
| 73 |
return errors.New("No account selected") |
| 74 |
} |
| 75 |
|
| 76 |
msg, err := gomail.ReadMessage(strings.NewReader(c.Body)) |
| 77 |
if errors.Is(err, io.EOF) { // completely empty |
| 78 |
msg = &gomail.Message{Body: strings.NewReader("")} |
| 79 |
} else if err != nil { |
| 80 |
return fmt.Errorf("mail.ReadMessage: %w", err) |
| 81 |
} |
| 82 |
headers := mail.HeaderFromMap(msg.Header) |
| 83 |
|
| 84 |
composer, err := app.NewComposer(acct, |
| 85 |
acct.AccountConfig(), acct.Worker(), editHeaders, |
| 86 |
c.Template, &headers, nil, msg.Body) |
| 87 |
if err != nil { |
| 88 |
return err |
| 89 |
} |
| 90 |
composer.Tab = app.NewTab(composer, "New email") |
| 91 |
if c.SkipEditor { |
| 92 |
composer.Terminal().Close() |
| 93 |
} |
| 94 |
return nil |
| 95 |
} |