diff options
author | Ben Burwell <ben@benburwell.com> | 2019-07-04 22:34:52 -0400 |
---|---|---|
committer | Ben Burwell <ben@benburwell.com> | 2019-07-04 22:37:29 -0400 |
commit | b46b497f99d7f4dcba8936ce0ebfe8cf982cec1f (patch) | |
tree | b9212d4b196706d3c0e2d93b8dfd8cc39cbacf3b /lib | |
parent | 6574dedd8a4afdaedd3677283955a866214bd99a (diff) |
Factor UI models out of the worker message packagemdps1
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.
Diffstat (limited to 'lib')
-rw-r--r-- | lib/address.go | 40 | ||||
-rw-r--r-- | lib/indexformat.go | 46 |
2 files changed, 20 insertions, 66 deletions
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 |