Robbert Gurdeep Singh: 1 reply: add aliases-regexp 3 files changed, 43 insertions(+), 5 deletions(-)
Copy & paste the following snippet into your terminal to import this patchset into git:
curl -s https://lists.sr.ht/~sircmpwn/aerc/patches/20829/mbox | git am -3Learn more about email & git
This is usefull for people who own a whole domain, or people using + in their email adresses. The config line I use is: aliases-regexp=^[^@]*@beardhatcode\.be$ The addrSet has been adapted to keep a pointer to the original address so that we can use it to get back a mail.Address. --- commands/msg/reply.go | 35 ++++++++++++++++++++++++++++++----- config/config.go | 3 +++ doc/aerc-config.5.scd | 10 ++++++++++ 3 files changed, 43 insertions(+), 5 deletions(-) diff --git a/commands/msg/reply.go b/commands/msg/reply.go index 2e4a21a..fbbbac5 100644 --- a/commands/msg/reply.go +++ b/commands/msg/reply.go @@ -5,6 +5,7 @@ import ( "errors" "fmt" "io" + "regexp" "strings" "git.sr.ht/~sircmpwn/getopt" @@ -72,6 +73,13 @@ func (reply) Execute(aerc *widgets.Aerc, args []string) error { return err } } + var aliasesRegexp *regexp.Regexp + if conf.AliasesRegexp != "" { + aliasesRegexp, err = regexp.Compile(conf.AliasesRegexp) + if err != nil { + return err + } + } store := widget.Store() if store == nil { @@ -83,7 +91,7 @@ func (reply) Execute(aerc *widgets.Aerc, args []string) error { } // figure out the sending from address if we have aliases - if len(aliases) != 0 { + if len(aliases) != 0 || aliasesRegexp != nil { rec := newAddrSet() rec.AddList(msg.Envelope.To) rec.AddList(msg.Envelope.Cc) @@ -97,6 +105,10 @@ func (reply) Execute(aerc *widgets.Aerc, args []string) error { break } } + regexpMatch , found := rec.ContainsRegexp(aliasesRegexp) + if found { + from = regexpMatch + } } } @@ -225,20 +237,20 @@ func (reply) Execute(aerc *widgets.Aerc, args []string) error { } } -type addrSet map[string]struct{} +type addrSet map[string]*mail.Address func newAddrSet() addrSet { - s := make(map[string]struct{}) + s := make(map[string]*mail.Address) return addrSet(s) } func (s addrSet) Add(a *mail.Address) { - s[a.Address] = struct{}{} + s[a.Address] = a } func (s addrSet) AddList(al []*mail.Address) { for _, a := range al { - s[a.Address] = struct{}{} + s[a.Address] = a } } @@ -247,6 +259,19 @@ func (s addrSet) Contains(a *mail.Address) bool { return ok } + +func (s addrSet) ContainsRegexp(a *regexp.Regexp) (*mail.Address, bool) { + var n *mail.Address + if a != nil { + for address, mailAdress := range s { + if a.MatchString(address) { + return mailAdress, true + } + } + } + return n, false +} + //setReferencesHeader adds the references header to target based on parent //according to RFC2822 func setReferencesHeader(target, parent *mail.Header) error { diff --git a/config/config.go b/config/config.go index 8b409fe..f770286 100644 --- a/config/config.go +++ b/config/config.go @@ -76,6 +76,7 @@ type AccountConfig struct { Postpone string From string Aliases string + AliasesRegexp string Name string Source string SourceCredCmd string @@ -205,6 +206,8 @@ func loadAccountConfig(path string) ([]AccountConfig, error) { account.From = val } else if key == "aliases" { account.Aliases = val + } else if key == "aliases-regexp" { + account.AliasesRegexp = val } else if key == "copy-to" { account.CopyTo = val } else if key == "archive" { diff --git a/doc/aerc-config.5.scd b/doc/aerc-config.5.scd index d4de883..f7d4490 100644 --- a/doc/aerc-config.5.scd +++ b/doc/aerc-config.5.scd @@ -418,6 +418,16 @@ Note that many of these configuration options are written for you, such as Default: none +*aliases-regexp* + A regular expression that matches aliases of the current account. Ensure + that the regular expression matches full adresses. Matching adresses will + be used to fill in the From: field when replying to mails where this + expression matches an adress in From, CC or BCC. Make sure that your email + server accepts this value, or for example use *aerc-sendmail*(5) in + combination with msmtp and --read-envelope-from. + + Default: none + *outgoing* Specifies the transport for sending outgoing emails on this account. It should be a connection string, and the specific meaning of each component -- 2.30.1