1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
|
package sortthread
import (
"errors"
"github.com/emersion/go-imap"
"github.com/emersion/go-imap/backend"
"github.com/emersion/go-imap/server"
)
var ErrUnsupportedBackend = errors.New("sortthread: backend not supported")
type SortMailbox interface {
backend.Mailbox
Sort(uid bool, sortCrit []SortCriterion, searchCrit *imap.SearchCriteria) ([]uint32, error)
}
type ThreadBackend interface {
backend.Backend
SupportedThreadAlgorithms() []ThreadAlgorithm
}
type ThreadMailbox interface {
backend.Mailbox
Thread(uid bool, threading ThreadAlgorithm, searchCrit *imap.SearchCriteria) ([]*Thread, error)
}
type SortHandler struct {
SortCommand
}
func (h *SortHandler) handle(uid bool, conn server.Conn) error {
if conn.Context().Mailbox == nil {
return server.ErrNoMailboxSelected
}
mbox, ok := conn.Context().Mailbox.(SortMailbox)
if !ok {
return ErrUnsupportedBackend
}
ids, err := mbox.Sort(uid, h.SortCriteria, h.SearchCriteria)
if err != nil {
return err
}
return conn.WriteResp(&SortResponse{Ids: ids})
}
func (h *SortHandler) Handle(conn server.Conn) error {
return h.handle(false, conn)
}
func (h *SortHandler) UidHandle(conn server.Conn) error {
return h.handle(true, conn)
}
type ThreadHandler struct {
ThreadCommand
}
func (h *ThreadHandler) handle(uid bool, conn server.Conn) error {
if conn.Context().Mailbox == nil {
return server.ErrNoMailboxSelected
}
mbox, ok := conn.Context().Mailbox.(ThreadMailbox)
if !ok {
return ErrUnsupportedBackend
}
thr, err := mbox.Thread(uid, h.Algorithm, h.SearchCriteria)
if err != nil {
return err
}
return conn.WriteResp(&ThreadResponse{Threads: thr})
}
func (h *ThreadHandler) Handle(conn server.Conn) error {
return h.handle(false, conn)
}
func (h *ThreadHandler) UidHandle(conn server.Conn) error {
return h.handle(true, conn)
}
type sortExtension struct{}
func NewSortExtension() server.Extension {
return &sortExtension{}
}
func (s *sortExtension) Capabilities(c server.Conn) []string {
if c.Context().State&imap.AuthenticatedState != 0 {
return []string{SortCapability}
}
return nil
}
func (s *sortExtension) Command(name string) server.HandlerFactory {
if name == "SORT" {
return func() server.Handler {
return &SortHandler{}
}
}
return nil
}
type threadExtension struct{}
func NewThreadExtension() server.Extension {
return &threadExtension{}
}
func (s *threadExtension) Capabilities(c server.Conn) []string {
if c.Context().State&imap.AuthenticatedState == 0 {
return nil
}
be, ok := c.Server().Backend.(ThreadBackend)
if !ok {
// No backend support, no-op.
return nil
}
var caps []string
for _, algo := range be.SupportedThreadAlgorithms() {
caps = append(caps, string("THREAD="+algo))
}
return caps
}
func (s *threadExtension) Command(name string) server.HandlerFactory {
if name == "THREAD" {
return func() server.Handler {
return &ThreadHandler{}
}
}
return nil
}
|