-
Notifications
You must be signed in to change notification settings - Fork 297
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge support for COMPRESS extension
- Loading branch information
Showing
9 changed files
with
184 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package commands | ||
|
||
import ( | ||
"errors" | ||
|
||
"github.com/emersion/go-imap" | ||
) | ||
|
||
// A COMPRESS command. | ||
type Compress struct { | ||
// Name of the compression mechanism. | ||
Mechanism string | ||
} | ||
|
||
func (cmd *Compress) Command() *imap.Command { | ||
return &imap.Command{ | ||
Name: "COMPRESS", | ||
Arguments: []interface{}{cmd.Mechanism}, | ||
} | ||
} | ||
|
||
func (cmd *Compress) Parse(fields []interface{}) (err error) { | ||
if len(fields) < 1 { | ||
return errors.New("No enough arguments") | ||
} | ||
|
||
var ok bool | ||
if cmd.Mechanism, ok = fields[0].(string); !ok { | ||
return errors.New("Compression mechanism name must be a string") | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package imap | ||
|
||
// A CompressUnsuppportedError is returned by Client.Compress when the provided | ||
// compression mechanism is not supported. | ||
type CompressUnsupportedError struct { | ||
Mechanism string | ||
} | ||
|
||
func (err CompressUnsupportedError) Error() string { | ||
return "COMPRESS mechanism " + err.Mechanism + " not supported" | ||
} | ||
|
||
// Compression algorithms for use with COMPRESS extension (RFC 4978). | ||
const ( | ||
// The DEFLATE algorithm, defined in RFC 1951. | ||
CompressDeflate = "DEFLATE" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package internal | ||
|
||
import ( | ||
"compress/flate" | ||
"io" | ||
"net" | ||
) | ||
|
||
type deflateConn struct { | ||
net.Conn | ||
|
||
r io.ReadCloser | ||
w *flate.Writer | ||
} | ||
|
||
func (c *deflateConn) Read(b []byte) (int, error) { | ||
return c.r.Read(b) | ||
} | ||
|
||
func (c *deflateConn) Write(b []byte) (int, error) { | ||
return c.w.Write(b) | ||
} | ||
|
||
type flusher interface { | ||
Flush() error | ||
} | ||
|
||
func (c *deflateConn) Flush() error { | ||
if f, ok := c.Conn.(flusher); ok { | ||
if err := f.Flush(); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return c.w.Flush() | ||
} | ||
|
||
func (c *deflateConn) Close() error { | ||
if err := c.r.Close(); err != nil { | ||
return err | ||
} | ||
|
||
if err := c.w.Close(); err != nil { | ||
return err | ||
} | ||
|
||
return c.Conn.Close() | ||
} | ||
|
||
func CreateDeflateConn(c net.Conn, level int) (net.Conn, error) { | ||
r := flate.NewReader(c) | ||
w, err := flate.NewWriter(c, level) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &deflateConn{ | ||
Conn: c, | ||
r: r, | ||
w: w, | ||
}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters