aboutsummaryrefslogtreecommitdiff
path: root/plugins/base
diff options
context:
space:
mode:
authorSimon Ser <contact@emersion.fr>2020-01-24 18:01:01 +0100
committerSimon Ser <contact@emersion.fr>2020-01-24 18:01:01 +0100
commit2e367efe58ea62ddf85552d68c757f2b6cba25ab (patch)
treea447290599b83d174bd0a72c315c47adb343dcc1 /plugins/base
parentbfc617b70267f299e3117eae33b5fd8f23e25500 (diff)
downloadalps-2e367efe58ea62ddf85552d68c757f2b6cba25ab.tar.gz
alps-2e367efe58ea62ddf85552d68c757f2b6cba25ab.zip
plugins/base: add fallback if SPECIAL-USE is unsupported
Diffstat (limited to 'plugins/base')
-rwxr-xr-xplugins/base/imap.go56
1 files changed, 36 insertions, 20 deletions
diff --git a/plugins/base/imap.go b/plugins/base/imap.go
index a9de217..980a8aa 100755
--- a/plugins/base/imap.go
+++ b/plugins/base/imap.go
@@ -38,18 +38,49 @@ func listMailboxes(conn *imapclient.Client) ([]*imap.MailboxInfo, error) {
return mailboxes, nil
}
-func getMailboxByAttribute(conn *imapclient.Client, attr string) (*imap.MailboxInfo, error) {
+type mailboxType int
+
+const (
+ mailboxSent mailboxType = iota
+ mailboxDrafts
+)
+
+func getMailboxByType(conn *imapclient.Client, mboxType mailboxType) (*imap.MailboxInfo, error) {
ch := make(chan *imap.MailboxInfo, 10)
done := make(chan error, 1)
go func() {
done <- conn.List("", "%", ch)
}()
- var mailbox *imap.MailboxInfo
+ // TODO: configurable fallback names?
+ var attr string
+ var fallbackNames []string
+ switch mboxType {
+ case mailboxSent:
+ attr = imapspecialuse.Sent
+ fallbackNames = []string{"Sent"}
+ case mailboxDrafts:
+ attr = imapspecialuse.Drafts
+ fallbackNames = []string{"Draft", "Drafts"}
+ }
+
+ var attrMatched bool
+ var best *imap.MailboxInfo
for mbox := range ch {
for _, a := range mbox.Attributes {
if attr == a {
- mailbox = mbox
+ best = mbox
+ attrMatched = true
+ break
+ }
+ }
+ if attrMatched {
+ break
+ }
+
+ for _, fallback := range fallbackNames {
+ if strings.EqualFold(fallback, mbox.Name) {
+ best = mbox
break
}
}
@@ -59,7 +90,7 @@ func getMailboxByAttribute(conn *imapclient.Client, attr string) (*imap.MailboxI
return nil, fmt.Errorf("failed to get mailbox with attribute %q: %v", attr, err)
}
- return mailbox, nil
+ return best, nil
}
func ensureMailboxSelected(conn *imapclient.Client, mboxName string) error {
@@ -379,23 +410,8 @@ func markMessageAnswered(conn *imapclient.Client, mboxName string, uid uint32) e
return conn.UidStore(seqSet, item, flags, nil)
}
-type mailboxType int
-
-const (
- mailboxSent mailboxType = iota
- mailboxDrafts
-)
-
func appendMessage(c *imapclient.Client, msg *OutgoingMessage, mboxType mailboxType) (saved bool, err error) {
- var mboxAttr string
- switch mboxType {
- case mailboxSent:
- mboxAttr = imapspecialuse.Sent
- case mailboxDrafts:
- mboxAttr = imapspecialuse.Drafts
- }
-
- mbox, err := getMailboxByAttribute(c, mboxAttr)
+ mbox, err := getMailboxByType(c, mboxType)
if err != nil {
return false, err
}