diff options
author | Robin Jarry <robin@jarry.cc> | 2022-12-20 20:29:12 +0100 |
---|---|---|
committer | Robin Jarry <robin@jarry.cc> | 2023-01-04 22:57:31 +0100 |
commit | 37e9a924894db7e5f232e82066155a60827c339b (patch) | |
tree | 48deeeea33113bbc9e6fcbcb8973a3ba1d62a06d /widgets | |
parent | c56027b2e69ec198e41394e5cf906273d80baf79 (diff) | |
download | aerc-37e9a924894db7e5f232e82066155a60827c339b.tar.gz |
config: parse account from and aliases once
Instead of accepting any garbage for these configuration fields, parse
them when parsing accounts.conf and store mail.Address objects. Reuse
these objects everywhere.
Signed-off-by: Robin Jarry <robin@jarry.cc>
Acked-by: Tim Culverhouse <tim@timculverhouse.com>
Diffstat (limited to 'widgets')
-rw-r--r-- | widgets/account-wizard.go | 9 | ||||
-rw-r--r-- | widgets/aerc.go | 2 | ||||
-rw-r--r-- | widgets/compose.go | 35 | ||||
-rw-r--r-- | widgets/msglist.go | 2 |
4 files changed, 14 insertions, 34 deletions
diff --git a/widgets/account-wizard.go b/widgets/account-wizard.go index 2ff87241..4b948a30 100644 --- a/widgets/account-wizard.go +++ b/widgets/account-wizard.go @@ -13,6 +13,7 @@ import ( "sync" "time" + "github.com/emersion/go-message/mail" "github.com/gdamore/tcell/v2" "github.com/go-ini/ini" "github.com/kyoh86/xdg" @@ -531,10 +532,16 @@ func (wizard *AccountWizard) finish(tutorial bool) { } } + from, err := mail.ParseAddress(sec.Key("from").String()) + if err != nil { + wizard.errorFor(nil, err) + return + } + account := config.AccountConfig{ Name: sec.Name(), Default: "INBOX", - From: sec.Key("from").String(), + From: from, Source: sec.Key("source").String(), Outgoing: config.RemoteConfig{Value: sec.Key("outgoing").String()}, } diff --git a/widgets/aerc.go b/widgets/aerc.go index cf893a7d..f6e5c1ff 100644 --- a/widgets/aerc.go +++ b/widgets/aerc.go @@ -748,7 +748,7 @@ func (aerc *Aerc) Mbox(source string) error { aerc.PushStatus(info, 10*time.Second) log.Debugf(info) } else { - acctConf.From = "<user@localhost>" + acctConf.From = &mail.Address{Address: "user@localhost"} } acctConf.Name = "mbox" acctConf.Source = source diff --git a/widgets/compose.go b/widgets/compose.go index a625e7eb..782e7538 100644 --- a/widgets/compose.go +++ b/widgets/compose.go @@ -141,13 +141,7 @@ func (c *Composer) setupFor(view *AccountView) error { // Set from header if not already in header if fl, err := c.header.AddressList("from"); err != nil || fl == nil { - fl, err = mail.ParseAddressList(view.acct.From) - if err != nil { - return err - } - if fl != nil { - c.header.SetAddressList("from", fl) - } + c.header.SetAddressList("from", []*mail.Address{view.acct.From}) } // update completer @@ -294,10 +288,7 @@ func (c *Composer) SetAttachKey(attach bool) error { if c.acctConfig.PgpKeyId != "" { s = c.acctConfig.PgpKeyId } else { - s, err = getSenderEmail(c) - if err != nil { - return err - } + s = c.acctConfig.From.Address } c.crypto.signKey, err = c.aerc.Crypto.GetSignerKeyId(s) if err != nil { @@ -390,10 +381,7 @@ func (c *Composer) updateCrypto() error { if c.acctConfig.PgpKeyId != "" { s = c.acctConfig.PgpKeyId } else { - s, err = getSenderEmail(c) - if err != nil { - return err - } + s = c.acctConfig.From.Address } c.crypto.signKey, err = cp.GetSignerKeyId(s) if err != nil { @@ -717,18 +705,6 @@ func (c *Composer) PrepareHeader() (*mail.Header, error) { return c.header, nil } -func getSenderEmail(c *Composer) (string, error) { - // add the from: field also to the 'recipients' list - if c.acctConfig.From == "" { - return "", errors.New("No 'From' configured for this account") - } - from, err := mail.ParseAddress(c.acctConfig.From) - if err != nil { - return "", errors.Wrap(err, "ParseAddress(config.From)") - } - return from.Address, nil -} - func getRecipientsEmail(c *Composer) ([]string, error) { h, err := c.PrepareHeader() if err != nil { @@ -776,10 +752,7 @@ func (c *Composer) WriteMessage(header *mail.Header, writer io.Writer) error { if c.acctConfig.PgpKeyId != "" { signer = c.acctConfig.PgpKeyId } else { - signer, err = getSenderEmail(c) - if err != nil { - return err - } + signer = c.acctConfig.From.Address } } diff --git a/widgets/msglist.go b/widgets/msglist.go index c260da1c..839453f2 100644 --- a/widgets/msglist.go +++ b/widgets/msglist.go @@ -91,7 +91,7 @@ func (ml *MessageList) Draw(ctx *ui.Context) { createBaseCtx := func(uid uint32, row int) format.Ctx { return format.Ctx{ - FromAddress: acct.acct.From, + FromAddress: format.AddressForHumans(acct.acct.From), AccountName: acct.Name(), MsgInfo: store.Messages[uid], MsgNum: row, |