Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,9 @@ Composer keys are fixed, not part of `[keys]`:
| `junk` | `!` | Move to the junk mailbox. |
| `move` | `m` | Move to a mailbox chosen from a picker; type to filter the folder list. |
| `empty_trash` | `X` | Delete everything in Trash, after a confirmation. Only works inside Trash. |
| `new_folder` | `N` | In the folder pane, create a folder under the one selected; type a nested name to add levels. |
| `move_folder` | `M` | In the folder pane, move the selected folder under another one, after a confirmation naming the destination. Subfolders move with it. |
| `delete_folder` | `D` | In the folder pane, delete the selected folder after a confirmation. Refused while it holds mail or has subfolders. |
| `collapse` | `z` | Fold or unfold the conversation under the cursor. |
| `expand_folder` | `right` | In the folder pane, expand the folder under the cursor. |
| `collapse_folder` | `left` | In the folder pane, fold the folder under the cursor. |
Expand Down
6 changes: 6 additions & 0 deletions config/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ const (
KeyJunk = "junk"
KeyMove = "move"
KeyEmptyTrash = "empty_trash"
KeyNewFolder = "new_folder"
KeyMoveFolder = "move_folder"
KeyDeleteFolder = "delete_folder"
KeyCollapse = "collapse"
KeyExpandFolder = "expand_folder"
KeyCollapseFolder = "collapse_folder"
Expand Down Expand Up @@ -88,6 +91,9 @@ var keyDefaults = map[string][]string{
KeyJunk: {"!"},
KeyMove: {"m"},
KeyEmptyTrash: {"X"},
KeyNewFolder: {"N"},
KeyMoveFolder: {"M"},
KeyDeleteFolder: {"D"},
KeyCollapse: {"z"},
KeyExpandFolder: {"right"},
KeyCollapseFolder: {"left"},
Expand Down
44 changes: 44 additions & 0 deletions mail/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,50 @@ func (c *Client) Folders() ([]Folder, error) {
return foldersFromList(data), nil
}

// CreateMailbox creates one mailbox. Any missing parent is the caller's job, because
// servers differ on whether CREATE invents them.
func (c *Client) CreateMailbox(name string) error {
if err := c.conn.Create(name, nil).Wait(); err != nil {
return fmt.Errorf("mail: account %q: creating %q: %w", c.account, name, err)
}

return nil
}

// RenameMailbox renames one mailbox and leaves its inferior names alone. Servers
// differ on whether a rename cascades, so the caller repairs what is left behind.
func (c *Client) RenameMailbox(name, newName string) error {
if err := c.conn.Rename(name, newName, nil).Wait(); err != nil {
return fmt.Errorf("mail: account %q: renaming %q to %q: %w", c.account, name, newName, err)
}

return nil
}

// DeleteMailbox deletes one mailbox. The server refuses a mailbox that still holds
// mail only sometimes, so the caller checks first.
func (c *Client) DeleteMailbox(name string) error {
if err := c.conn.Delete(name).Wait(); err != nil {
return fmt.Errorf("mail: account %q: deleting %q: %w", c.account, name, err)
}

return nil
}

// MailboxMessages counts the messages in a mailbox with one STATUS command. It does not
// SELECT, so it leaves the open mailbox and its selection alone.
func (c *Client) MailboxMessages(name string) (uint32, error) {
data, err := c.conn.Status(name, &imap.StatusOptions{NumMessages: true}).Wait()
if err != nil {
return 0, fmt.Errorf("mail: account %q: counting messages in %q: %w", c.account, name, err)
}
if data.NumMessages == nil {
return 0, nil
}

return *data.NumMessages, nil
}

// FetchEnvelopes opens a folder and returns every message's envelope, flags and
// size. No message body is fetched.
func (c *Client) FetchEnvelopes(folder string) ([]Message, error) {
Expand Down
70 changes: 69 additions & 1 deletion mail/errors.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package mail

import "errors"
import (
"errors"
"fmt"
)

// Sentinel errors the interface maps onto user-facing phrases. The wrapped text keeps
// the account, host, stage and original cause for logs; the TUI decides the wording.
Expand Down Expand Up @@ -34,4 +37,69 @@ var (
// permanent delete of a selection would need a folder-wide EXPUNGE. Refused rather
// than destroying mail the user did not select.
ErrNoSafePurge = errors.New("mail: server cannot delete permanently")

// ErrFolderExists reports that a new or moved folder would collide with one the
// listing already has. Matched case-insensitively, like every other name here.
ErrFolderExists = errors.New("mail: folder already exists")

// ErrFolderNotEmpty reports a delete of a mailbox that still holds mail. The
// refusal carries the count, so the interface can say how much is in the way.
ErrFolderNotEmpty = errors.New("mail: folder is not empty")

// ErrFolderHasChildren reports a delete of a mailbox that has folders below it.
// Removing it would either orphan them or destroy them, depending on the server.
ErrFolderHasChildren = errors.New("mail: folder has subfolders")

// ErrFolderProtected reports a mailbox a rename or delete must not touch: INBOX,
// or a folder an [accounts.imap] override names.
ErrFolderProtected = errors.New("mail: folder cannot be changed")

// ErrFolderCycle reports a move whose destination is the folder itself or one of
// its own descendants, which would make the tree a loop.
ErrFolderCycle = errors.New("mail: cannot move a folder into itself")

// ErrFolderNoOp reports a move the folder is already in.
ErrFolderNoOp = errors.New("mail: folder is already there")
)

// FolderNotEmptyError says how many messages block a delete. It matches
// ErrFolderNotEmpty, so a caller that only needs the reason can ignore the count.
type FolderNotEmptyError struct {
Folder string
Count uint32
}

func (e *FolderNotEmptyError) Error() string {
return fmt.Sprintf("%s: %q holds %s", ErrFolderNotEmpty, e.Folder, countNoun(int(e.Count), "message"))
}

// Is reports the sentinel this refusal belongs to.
func (e *FolderNotEmptyError) Is(target error) bool {
return target == ErrFolderNotEmpty
}

// FolderHasChildrenError says how many folders sit below the one a delete named. The
// count includes deeper descendants, not just immediate children.
type FolderHasChildrenError struct {
Folder string
Count int
}

func (e *FolderHasChildrenError) Error() string {
return fmt.Sprintf("%s: %q has %s below it", ErrFolderHasChildren, e.Folder, countNoun(e.Count, "folder"))
}

// Is reports the sentinel this refusal belongs to.
func (e *FolderHasChildrenError) Is(target error) bool {
return target == ErrFolderHasChildren
}

// countNoun renders "1 message" or "12 messages". It is the mail package's own copy
// because the interface's countLabel is not reachable from here.
func countNoun(n int, noun string) string {
if n == 1 {
return "1 " + noun
}

return fmt.Sprintf("%d %ss", n, noun)
}
Loading
Loading