diff options
author | Ben Burwell <ben@benburwell.com> | 2019-07-07 22:43:58 -0400 |
---|---|---|
committer | Drew DeVault <sir@cmpwn.com> | 2019-07-08 16:06:28 -0400 |
commit | c610c3cd9dd47c400e52c1858e987f5f32a7a45b (patch) | |
tree | 6e521ba706d87ea4a03ce81d57ff84317506f3df /lib | |
parent | 88c379dcbaaf9fd549cd271817e79fe634b1dd84 (diff) | |
download | aerc-c610c3cd9dd47c400e52c1858e987f5f32a7a45b.tar.gz |
Factor IMAP-specific structs out of UI models
Before, we were using several IMAP-specific concepts to represent information being displayed in the UI. Factor these structures out of the IMAP package to make it easier for other backends to provide the required information.
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 |