From a5c64e4361f38a50c859a3f644100c077eeddd4c Mon Sep 17 00:00:00 2001 From: Mechiel Lukkien Date: Wed, 5 Mar 2025 17:34:57 +0100 Subject: [PATCH] make code less indented --- imapserver/server.go | 98 +++++++++++++++++++------------------- webaccount/import.go | 111 ++++++++++++++++++++++--------------------- 2 files changed, 106 insertions(+), 103 deletions(-) diff --git a/imapserver/server.go b/imapserver/server.go index 6647279..a1052ba 100644 --- a/imapserver/server.go +++ b/imapserver/server.go @@ -2943,63 +2943,63 @@ func (c *conn) cmdRename(tag, cmd string, p *parser) { c.xdbwrite(func(tx *bstore.Tx) { srcMB := c.xmailbox(tx, src, "NONEXISTENT") + // Handle common/simple case first. + if src != "Inbox" { + var modseq store.ModSeq + var alreadyExists bool + var err error + changes, _, alreadyExists, err = c.account.MailboxRename(tx, &srcMB, dst, &modseq) + if alreadyExists { + xusercodeErrorf("ALREADYEXISTS", "%s", err) + } + xcheckf(err, "renaming mailbox") + return + } + // Inbox is very special. Unlike other mailboxes, its children are not moved. And // unlike a regular move, its messages are moved to a newly created mailbox. We do // indeed create a new destination mailbox and actually move the messages. // ../rfc/9051:2101 - if src == "Inbox" { - exists, err := c.account.MailboxExists(tx, dst) - xcheckf(err, "checking if destination mailbox exists") - if exists { - xusercodeErrorf("ALREADYEXISTS", "destination mailbox %q already exists", dst) - } - if dst == src { - xuserErrorf("cannot move inbox to itself") - } - - var modseq store.ModSeq - dstMB, chl, err := c.account.MailboxEnsure(tx, dst, false, store.SpecialUse{}, &modseq) - xcheckf(err, "creating destination mailbox") - changes = chl - - // Copy mailbox annotations. ../rfc/5464:368 - qa := bstore.QueryTx[store.Annotation](tx) - qa.FilterNonzero(store.Annotation{MailboxID: srcMB.ID}) - qa.FilterEqual("Expunged", false) - annotations, err := qa.List() - xcheckf(err, "get annotations to copy for inbox") - for _, a := range annotations { - a.ID = 0 - a.MailboxID = dstMB.ID - a.ModSeq = modseq - a.CreateSeq = modseq - err := tx.Insert(&a) - xcheckf(err, "copy annotation to destination mailbox") - changes = append(changes, a.Change(dstMB.Name)) - } - c.xcheckMetadataSize(tx) - - // Build query that selects messages to move. - q := bstore.QueryTx[store.Message](tx) - q.FilterNonzero(store.Message{MailboxID: srcMB.ID}) - q.FilterEqual("Expunged", false) - q.SortAsc("UID") - - newIDs, chl := c.xmoveMessages(tx, q, 0, modseq, &srcMB, &dstMB) - changes = append(changes, chl...) - cleanupIDs = newIDs - - return + exists, err := c.account.MailboxExists(tx, dst) + xcheckf(err, "checking if destination mailbox exists") + if exists { + xusercodeErrorf("ALREADYEXISTS", "destination mailbox %q already exists", dst) + } + if dst == src { + xuserErrorf("cannot move inbox to itself") } var modseq store.ModSeq - var alreadyExists bool - var err error - changes, _, alreadyExists, err = c.account.MailboxRename(tx, &srcMB, dst, &modseq) - if alreadyExists { - xusercodeErrorf("ALREADYEXISTS", "%s", err) + dstMB, chl, err := c.account.MailboxEnsure(tx, dst, false, store.SpecialUse{}, &modseq) + xcheckf(err, "creating destination mailbox") + changes = chl + + // Copy mailbox annotations. ../rfc/5464:368 + qa := bstore.QueryTx[store.Annotation](tx) + qa.FilterNonzero(store.Annotation{MailboxID: srcMB.ID}) + qa.FilterEqual("Expunged", false) + annotations, err := qa.List() + xcheckf(err, "get annotations to copy for inbox") + for _, a := range annotations { + a.ID = 0 + a.MailboxID = dstMB.ID + a.ModSeq = modseq + a.CreateSeq = modseq + err := tx.Insert(&a) + xcheckf(err, "copy annotation to destination mailbox") + changes = append(changes, a.Change(dstMB.Name)) } - xcheckf(err, "renaming mailbox") + c.xcheckMetadataSize(tx) + + // Build query that selects messages to move. + q := bstore.QueryTx[store.Message](tx) + q.FilterNonzero(store.Message{MailboxID: srcMB.ID}) + q.FilterEqual("Expunged", false) + q.SortAsc("UID") + + newIDs, chl := c.xmoveMessages(tx, q, 0, modseq, &srcMB, &dstMB) + changes = append(changes, chl...) + cleanupIDs = newIDs }) cleanupIDs = nil diff --git a/webaccount/import.go b/webaccount/import.go index a8fcd3f..52fbb4e 100644 --- a/webaccount/import.go +++ b/webaccount/import.go @@ -713,61 +713,64 @@ func importMessages(ctx context.Context, log mlog.Log, token string, acc *store. case "new", "cur", "tmp": mailbox := path.Dir(dir) ximportMaildir(mailbox, origName, r) - default: - if path.Base(name) == "dovecot-keywords" { - mailbox := path.Dir(name) - dovecotKeywords := map[rune]string{} - words, err := store.ParseDovecotKeywordsFlags(r, log) - log.Check(err, "parsing dovecot keywords for mailbox", slog.String("mailbox", mailbox)) - for i, kw := range words { - dovecotKeywords['a'+rune(i)] = kw - } - mailboxKeywords[mailbox] = dovecotKeywords - - for id, chars := range mailboxMissingKeywordMessages[mailbox] { - var flags, zeroflags store.Flags - keywords := map[string]bool{} - for _, c := range chars { - kw, ok := dovecotKeywords[c] - if !ok { - problemf("unspecified dovecot message flag %c for message id %d (continuing)", c, id) - continue - } - flagSet(&flags, keywords, kw) - } - if flags == zeroflags && len(keywords) == 0 { - continue - } - - m := store.Message{ID: id} - err := tx.Get(&m) - ximportcheckf(err, "get imported message for flag update") - - mb := mailboxIDs[m.MailboxID] - mb.Sub(m.MailboxCounts()) - - oflags := m.Flags - m.Flags = m.Flags.Set(flags, flags) - m.Keywords = maps.Keys(keywords) - sort.Strings(m.Keywords) - - mb.Add(m.MailboxCounts()) - - mb.Keywords, _ = store.MergeKeywords(mb.Keywords, m.Keywords) - - // We train before updating, training may set m.TrainedJunk. - if jf != nil && m.NeedsTraining() { - openTrainMessage(&m) - } - err = tx.Update(&m) - ximportcheckf(err, "updating message after flag update") - changes = append(changes, m.ChangeFlags(oflags)) - } - delete(mailboxMissingKeywordMessages, mailbox) - } else { - problemf("unrecognized file %s (skipping)", origName) - } + return } + + if path.Base(name) != "dovecot-keywords" { + problemf("unrecognized file %s (skipping)", origName) + return + } + + // Handle dovecot-keywords. + mailbox := path.Dir(name) + dovecotKeywords := map[rune]string{} + words, err := store.ParseDovecotKeywordsFlags(r, log) + log.Check(err, "parsing dovecot keywords for mailbox", slog.String("mailbox", mailbox)) + for i, kw := range words { + dovecotKeywords['a'+rune(i)] = kw + } + mailboxKeywords[mailbox] = dovecotKeywords + + for id, chars := range mailboxMissingKeywordMessages[mailbox] { + var flags, zeroflags store.Flags + keywords := map[string]bool{} + for _, c := range chars { + kw, ok := dovecotKeywords[c] + if !ok { + problemf("unspecified dovecot message flag %c for message id %d (continuing)", c, id) + continue + } + flagSet(&flags, keywords, kw) + } + if flags == zeroflags && len(keywords) == 0 { + continue + } + + m := store.Message{ID: id} + err := tx.Get(&m) + ximportcheckf(err, "get imported message for flag update") + + mb := mailboxIDs[m.MailboxID] + mb.Sub(m.MailboxCounts()) + + oflags := m.Flags + m.Flags = m.Flags.Set(flags, flags) + m.Keywords = maps.Keys(keywords) + sort.Strings(m.Keywords) + + mb.Add(m.MailboxCounts()) + + mb.Keywords, _ = store.MergeKeywords(mb.Keywords, m.Keywords) + + // We train before updating, training may set m.TrainedJunk. + if jf != nil && m.NeedsTraining() { + openTrainMessage(&m) + } + err = tx.Update(&m) + ximportcheckf(err, "updating message after flag update") + changes = append(changes, m.ChangeFlags(oflags)) + } + delete(mailboxMissingKeywordMessages, mailbox) } if zr != nil {