-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclient.go
59 lines (47 loc) · 1.25 KB
/
client.go
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
package compress
import (
"compress/flate"
"net"
"github.com/emersion/go-imap/client"
)
// Client is a COMPRESS client.
type Client struct {
client *client.Client
isCompressed bool
}
// Compress instructs the server to use the named compression mechanism for all
// commands and/or responses.
func (c *Client) Compress(mech string) error {
if c.isCompressed {
return ErrAlreadyEnabled
}
if mech != Deflate {
return NotSupportedError{mech}
}
cmd := &Command{Mechanism: mech}
err := c.client.Upgrade(func(conn net.Conn) (net.Conn, error) {
if status, err := c.client.Execute(cmd, nil); err != nil {
return nil, err
} else if err := status.Err(); err != nil {
return nil, err
}
return createDeflateConn(conn, flate.DefaultCompression)
})
if err != nil {
return err
}
c.isCompressed = true
return nil
}
// IsCompressed checks if this client's connection is compressed.
func (c *Client) IsCompress() bool {
return c.isCompressed
}
// SupportCompress checks if the server supports a compression mechanism.
func (c *Client) SupportCompress(mech string) (bool, error) {
return c.client.Support(Capability + "=" + mech)
}
// NewClient creates a new client.
func NewClient(c *client.Client) *Client {
return &Client{client: c}
}