From b46b497f99d7f4dcba8936ce0ebfe8cf982cec1f Mon Sep 17 00:00:00 2001 From: Ben Burwell Date: Thu, 4 Jul 2019 22:34:52 -0400 Subject: Factor UI models out of the worker message package Before, the information needed to display different parts of the UI was tightly coupled to the specific messages being sent back and forth to the backend worker. Separating out a models package allows us to be more specific about exactly what a backend is able to and required to provide for the UI. --- lib/address.go | 40 ---------------------------------------- lib/indexformat.go | 46 ++++++++++++++++++++-------------------------- 2 files changed, 20 insertions(+), 66 deletions(-) delete mode 100644 lib/address.go (limited to 'lib') diff --git a/lib/address.go b/lib/address.go deleted file mode 100644 index b557195..0000000 --- a/lib/address.go +++ /dev/null @@ -1,40 +0,0 @@ -package lib - -import ( - "bytes" - "fmt" - "regexp" - "strings" - - "github.com/emersion/go-imap" -) - -var ( - atom *regexp.Regexp = regexp.MustCompile("^[a-z0-9!#$%7'*+-/=?^_`{}|~ ]+$") -) - -func FormatAddresses(addrs []*imap.Address) string { - val := bytes.Buffer{} - for i, addr := range addrs { - val.WriteString(FormatAddress(addr)) - if i != len(addrs)-1 { - val.WriteString(", ") - } - } - return val.String() -} - -func FormatAddress(addr *imap.Address) string { - if addr.PersonalName != "" { - if atom.MatchString(addr.PersonalName) { - return fmt.Sprintf("%s <%s@%s>", - addr.PersonalName, addr.MailboxName, addr.HostName) - } else { - return fmt.Sprintf("\"%s\" <%s@%s>", - strings.ReplaceAll(addr.PersonalName, "\"", "'"), - addr.MailboxName, addr.HostName) - } - } else { - return fmt.Sprintf("<%s@%s>", addr.MailboxName, addr.HostName) - } -} diff --git a/lib/indexformat.go b/lib/indexformat.go index 43d2ef8..fa39909 100644 --- a/lib/indexformat.go +++ b/lib/indexformat.go @@ -6,8 +6,6 @@ import ( "strings" "unicode" - "github.com/emersion/go-imap" - "git.sr.ht/~sircmpwn/aerc/config" "git.sr.ht/~sircmpwn/aerc/models" ) @@ -70,10 +68,9 @@ func ParseIndexFormat(conf *config.AercConfig, number int, } addr := msg.Envelope.From[0] retval = append(retval, 's') - args = append(args, fmt.Sprintf("%s@%s", addr.MailboxName, - addr.HostName)) + args = append(args, fmt.Sprintf("%s@%s", addr.Mailbox, addr.Host)) case 'A': - var addr *imap.Address + var addr *models.Address if len(msg.Envelope.ReplyTo) == 0 { if len(msg.Envelope.From) == 0 { return "", nil, @@ -85,8 +82,7 @@ func ParseIndexFormat(conf *config.AercConfig, number int, addr = msg.Envelope.ReplyTo[0] } retval = append(retval, 's') - args = append(args, fmt.Sprintf("%s@%s", addr.MailboxName, - addr.HostName)) + args = append(args, fmt.Sprintf("%s@%s", addr.Mailbox, addr.Host)) case 'C': retval = append(retval, 'd') args = append(args, number) @@ -100,7 +96,7 @@ func ParseIndexFormat(conf *config.AercConfig, number int, if len(msg.Envelope.From) == 0 { return "", nil, errors.New("found no address for sender") } - addr := FormatAddress(msg.Envelope.From[0]) + addr := msg.Envelope.From[0].Format() retval = append(retval, 's') args = append(args, addr) case 'F': @@ -111,11 +107,10 @@ func ParseIndexFormat(conf *config.AercConfig, number int, // TODO: handle case when sender is current user. Then // use recipient's name var val string - if addr.PersonalName != "" { - val = addr.PersonalName + if addr.Name != "" { + val = addr.Name } else { - val = fmt.Sprintf("%s@%s", - addr.MailboxName, addr.HostName) + val = fmt.Sprintf("%s@%s", addr.Mailbox, addr.Host) } retval = append(retval, 's') args = append(args, val) @@ -129,20 +124,19 @@ func ParseIndexFormat(conf *config.AercConfig, number int, } addr := msg.Envelope.From[0] var val string - if addr.PersonalName != "" { - val = addr.PersonalName + if addr.Name != "" { + val = addr.Name } else { - val = fmt.Sprintf("%s@%s", - addr.MailboxName, addr.HostName) + val = fmt.Sprintf("%s@%s", addr.Mailbox, addr.Host) } retval = append(retval, 's') args = append(args, val) case 'r': - addrs := FormatAddresses(msg.Envelope.To) + addrs := models.FormatAddresses(msg.Envelope.To) retval = append(retval, 's') args = append(args, addrs) case 'R': - addrs := FormatAddresses(msg.Envelope.Cc) + addrs := models.FormatAddresses(msg.Envelope.Cc) retval = append(retval, 's') args = append(args, addrs) case 's': @@ -154,16 +148,16 @@ func ParseIndexFormat(conf *config.AercConfig, number int, } addr := msg.Envelope.From[0] retval = append(retval, 's') - args = append(args, addr.MailboxName) + args = append(args, addr.Mailbox) case 'v': if len(msg.Envelope.From) == 0 { return "", nil, errors.New("found no address for sender") } addr := msg.Envelope.From[0] // check if message is from current user - if addr.PersonalName != "" { + if addr.Name != "" { retval = append(retval, 's') - args = append(args, strings.Split(addr.PersonalName, " ")[0]) + args = append(args, strings.Split(addr.Name, " ")[0]) } case 'Z': // calculate all flags @@ -171,18 +165,18 @@ func ParseIndexFormat(conf *config.AercConfig, number int, var delFlag = "" var flaggedFlag = "" for _, flag := range msg.Flags { - if flag == imap.SeenFlag { + if flag == models.SeenFlag { readFlag = "O" // message is old - } else if flag == imap.RecentFlag { + } else if flag == models.RecentFlag { readFlag = "N" // message is new - } else if flag == imap.AnsweredFlag { + } else if flag == models.AnsweredFlag { readFlag = "r" // message has been replied to } - if flag == imap.DeletedFlag { + if flag == models.DeletedFlag { delFlag = "D" // TODO: check if attachments } - if flag == imap.FlaggedFlag { + if flag == models.FlaggedFlag { flaggedFlag = "!" } // TODO: check gpg stuff -- cgit v1.2.3