File: client.go

package info (click to toggle)
golang-github-emersion-go-imap-sortthread 1.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 116 kB
  • sloc: makefile: 2
file content (112 lines) | stat: -rw-r--r-- 2,785 bytes parent folder | download
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
package sortthread

import (
	"github.com/emersion/go-imap"
	"github.com/emersion/go-imap/client"
	"github.com/emersion/go-imap/commands"
)

// SortClient is a SORT client.
type SortClient struct {
	c *client.Client
}

// ThreadClient is a THREAD client.
type ThreadClient struct {
	c *client.Client
}

// NewClient creates a new SORT client.
func NewSortClient(c *client.Client) *SortClient {
	return &SortClient{c: c}
}

// SupportSort returns true if the remote server supports the extension.
func (c *SortClient) SupportSort() (bool, error) {
	return c.c.Support(SortCapability)
}

func (c *SortClient) sort(uid bool, sortCriteria []SortCriterion, searchCriteria *imap.SearchCriteria) ([]uint32, error) {
	if c.c.State() != imap.SelectedState {
		return nil, client.ErrNoMailboxSelected
	}

	var cmd imap.Commander
	cmd = &SortCommand{
		SortCriteria:   sortCriteria,
		Charset:        "UTF-8",
		SearchCriteria: searchCriteria,
	}
	if uid {
		cmd = &commands.Uid{Cmd: cmd}
	}

	res := new(SortResponse)

	status, err := c.c.Execute(cmd, res)
	if err != nil {
		return nil, err
	}

	return res.Ids, status.Err()
}

func (c *SortClient) Sort(sortCriteria []SortCriterion, searchCriteria *imap.SearchCriteria) ([]uint32, error) {
	return c.sort(false, sortCriteria, searchCriteria)
}

func (c *SortClient) UidSort(sortCriteria []SortCriterion, searchCriteria *imap.SearchCriteria) ([]uint32, error) {
	return c.sort(true, sortCriteria, searchCriteria)
}

// NewClient creates a new THREAD client
func NewThreadClient(c *client.Client) *ThreadClient {
	return &ThreadClient{c: c}
}

// SupportThread returns true if the remote server supports the extension.
func (c *ThreadClient) SupportThread() (bool, error) {
	for _, capability := range ThreadCapabilities {
		ok, err := c.c.Support(capability)
		if err != nil {
			return false, err
		} else if ok {
			return true, nil
		}
	}
	return false, nil
}

func (c *ThreadClient) thread(uid bool, algorithm ThreadAlgorithm, searchCriteria *imap.SearchCriteria) ([]*Thread, error) {
	if c.c.State() != imap.SelectedState {
		return nil, client.ErrNoMailboxSelected
	}

	var cmd imap.Commander
	cmd = &ThreadCommand{
		Algorithm:      algorithm,
		Charset:        "UTF-8",
		SearchCriteria: searchCriteria,
	}

	if uid {
		cmd = &commands.Uid{Cmd: cmd}
	}

	res := new(ThreadResponse)

	status, err := c.c.Execute(cmd, res)
	if err != nil {
		return nil, err
	}

	return res.Threads, status.Err()
}

func (c *ThreadClient) Thread(algorithm ThreadAlgorithm, searchCriteria *imap.SearchCriteria) ([]*Thread, error) {
	return c.thread(false, algorithm, searchCriteria)
}

func (c *ThreadClient) UidThread(algorithm ThreadAlgorithm, searchCriteria *imap.SearchCriteria) ([]*Thread, error) {
	return c.thread(true, algorithm, searchCriteria)
}