Skip to content
Open
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
25 changes: 12 additions & 13 deletions imap/imap.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@ package imap
import (
"fmt"
"io"
"os"
"strings"
"sync"
)

func check(err os.Error) {
func check(err error) {
if err != nil {
panic(err)
}
Expand All @@ -31,12 +30,12 @@ type IMAP struct {

func New(r io.Reader, w io.Writer) *IMAP {
return &IMAP{
r: &reader{newParser(r)},
w: w,
r: &reader{newParser(r)},
w: w,
}
}

func (imap *IMAP) Start() (string, os.Error) {
func (imap *IMAP) Start() (string, error) {
tag, r, err := imap.r.readResponse()
if err != nil {
return "", err
Expand All @@ -58,7 +57,7 @@ func (imap *IMAP) Start() (string, os.Error) {
return resp.text, nil
}

func (imap *IMAP) Send(ch chan interface{}, format string, args ...interface{}) os.Error {
func (imap *IMAP) Send(ch chan interface{}, format string, args ...interface{}) error {
tag := tag(imap.nextTag)
imap.nextTag++

Expand All @@ -75,7 +74,7 @@ func (imap *IMAP) Send(ch chan interface{}, format string, args ...interface{})
return err
}

func (imap *IMAP) SendSync(format string, args ...interface{}) (*ResponseStatus, os.Error) {
func (imap *IMAP) SendSync(format string, args ...interface{}) (*ResponseStatus, error) {
ch := make(chan interface{}, 1)
err := imap.Send(ch, format, args...)
if err != nil {
Expand Down Expand Up @@ -106,7 +105,7 @@ L:
return response, nil
}

func (imap *IMAP) Auth(user string, pass string) (string, []string, os.Error) {
func (imap *IMAP) Auth(user string, pass string) (string, []string, error) {
resp, err := imap.SendSync("LOGIN %s %s", user, pass)
if err != nil {
return "", nil, err
Expand All @@ -131,7 +130,7 @@ func quote(in string) string {
return "\"" + in + "\""
}

func (imap *IMAP) List(reference string, name string) ([]*ResponseList, os.Error) {
func (imap *IMAP) List(reference string, name string) ([]*ResponseList, error) {
/* Responses: untagged responses: LIST */
response, err := imap.SendSync("LIST %s %s", quote(reference), quote(name))
if err != nil {
Expand Down Expand Up @@ -160,7 +159,7 @@ type ResponseExamine struct {
UIDNext int
}

func (imap *IMAP) Examine(mailbox string) (*ResponseExamine, os.Error) {
func (imap *IMAP) Examine(mailbox string) (*ResponseExamine, error) {
/*
Responses: REQUIRED untagged responses: FLAGS, EXISTS, RECENT
REQUIRED OK untagged responses: UNSEEN, PERMANENTFLAGS,
Expand Down Expand Up @@ -207,7 +206,7 @@ func formatFetch(sequence string, fields []string) string {
return fmt.Sprintf("FETCH %s %s", sequence, fieldsStr)
}

func (imap *IMAP) Fetch(sequence string, fields []string) ([]*ResponseFetch, os.Error) {
func (imap *IMAP) Fetch(sequence string, fields []string) ([]*ResponseFetch, error) {
resp, err := imap.SendSync("%s", formatFetch(sequence, fields))
if err != nil {
return nil, err
Expand All @@ -224,7 +223,7 @@ func (imap *IMAP) Fetch(sequence string, fields []string) ([]*ResponseFetch, os.
return lists, nil
}

func (imap *IMAP) FetchAsync(sequence string, fields []string) (chan interface{}, os.Error) {
func (imap *IMAP) FetchAsync(sequence string, fields []string) (chan interface{}, error) {
ch := make(chan interface{})
err := imap.Send(ch, formatFetch(sequence, fields))
if err != nil {
Expand Down Expand Up @@ -252,7 +251,7 @@ func (imap *IMAP) FetchAsync(sequence string, fields []string) (chan interface{}
}

// Repeatedly reads messages off the connection and dispatches them.
func (imap *IMAP) readLoop() os.Error {
func (imap *IMAP) readLoop() error {
var msgChan chan interface{}
for {
tag, r, err := imap.r.readResponse()
Expand Down
35 changes: 18 additions & 17 deletions imap/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@ package imap
import (
"bufio"
"bytes"
"errors"
"fmt"
"io"
"log"
"os"
"strconv"
)

func init() {
log.SetFlags(log.Ltime | log.Lshortfile)
}

func recoverError(err *os.Error) {
func recoverError(err *error) {
if e := recover(); e != nil {
if osErr, ok := e.(os.Error); ok {
if osErr, ok := e.(error); ok {
*err = osErr
return
}
Expand All @@ -25,6 +25,7 @@ func recoverError(err *os.Error) {
}

type sexp interface{}

// One of:
// string
// []sexp
Expand All @@ -45,7 +46,7 @@ func newParser(r io.Reader) *parser {
return &parser{bufio.NewReader(r)}
}

func (p *parser) expect(text string) os.Error {
func (p *parser) expect(text string) error {
buf := make([]byte, len(text))

_, err := io.ReadFull(p, buf)
Expand All @@ -60,11 +61,11 @@ func (p *parser) expect(text string) os.Error {
return nil
}

func (p *parser) expectEOL() os.Error {
func (p *parser) expectEOL() error {
return p.expect("\r\n")
}

func (p *parser) readToken() (token string, outErr os.Error) {
func (p *parser) readToken() (token string, outErr error) {
defer recoverError(&outErr)

buf := bytes.NewBuffer(make([]byte, 0, 16))
Expand All @@ -84,15 +85,15 @@ func (p *parser) readToken() (token string, outErr os.Error) {
panic("not reached")
}

func (p *parser) readNumber() (num int, outErr os.Error) {
func (p *parser) readNumber() (num int, outErr error) {
defer recoverError(&outErr)

num = 0
for {
c, err := p.ReadByte()
check(err)
if c >= '0' && c <= '9' {
num = num * 10 + int(c - '0')
num = num*10 + int(c-'0')
} else {
check(p.UnreadByte())
return num, nil
Expand All @@ -102,7 +103,7 @@ func (p *parser) readNumber() (num int, outErr os.Error) {
panic("not reached")
}

func (p *parser) readAtom() (outStr string, outErr os.Error) {
func (p *parser) readAtom() (outStr string, outErr error) {
/*
ATOM-CHAR = <any CHAR except atom-specials>

Expand Down Expand Up @@ -135,7 +136,7 @@ func (p *parser) readAtom() (outStr string, outErr os.Error) {
panic("not reached")
}

func (p *parser) readQuoted() (outStr string, outErr os.Error) {
func (p *parser) readQuoted() (outStr string, outErr error) {
defer recoverError(&outErr)

err := p.expect("\"")
Expand All @@ -162,7 +163,7 @@ func (p *parser) readQuoted() (outStr string, outErr os.Error) {
panic("not reached")
}

func (p *parser) readLiteral() (literal []byte, outErr os.Error) {
func (p *parser) readLiteral() (literal []byte, outErr error) {
/*
literal = "{" number "}" CRLF *CHAR8
*/
Expand All @@ -186,18 +187,18 @@ func (p *parser) readLiteral() (literal []byte, outErr os.Error) {
return
}

func (p *parser) readBracketed() (text string, outErr os.Error) {
func (p *parser) readBracketed() (text string, outErr error) {
defer recoverError(&outErr)

check(p.expect("["))
text, err := p.ReadString(']')
check(err)
text = text[0:len(text)-1]
text = text[0 : len(text)-1]

return text, nil
}

func (p *parser) readSexp() (s []sexp, outErr os.Error) {
func (p *parser) readSexp() (s []sexp, outErr error) {
defer recoverError(&outErr)

err := p.expect("(")
Expand Down Expand Up @@ -244,7 +245,7 @@ func (p *parser) readSexp() (s []sexp, outErr os.Error) {
panic("not reached")
}

func (p *parser) readParenStringList() ([]string, os.Error) {
func (p *parser) readParenStringList() ([]string, error) {
sexp, err := p.readSexp()
if err != nil {
return nil, err
Expand All @@ -260,13 +261,13 @@ func (p *parser) readParenStringList() ([]string, os.Error) {
return strs, nil
}

func (p *parser) readToEOL() (string, os.Error) {
func (p *parser) readToEOL() (string, error) {
line, prefix, err := p.ReadLine()
if err != nil {
return "", err
}
if prefix {
return "", os.NewError("got line prefix, buffer too small")
return "", errors.New("got line prefix, buffer too small")
}
return string(line), nil
}
22 changes: 11 additions & 11 deletions imap/protocol.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package imap

import (
"os"
"strconv"
"errors"
"fmt"
"strconv"
)

// Status represents server status codes which are returned by
Expand Down Expand Up @@ -45,7 +45,7 @@ type IMAPError struct {
Text string
}

func (e *IMAPError) String() string {
func (e *IMAPError) Error() string {
return fmt.Sprintf("imap: %s %s", e.Status, e.Text)
}

Expand All @@ -63,7 +63,7 @@ type reader struct {
}

// Read a full response (e.g. "* OK foobar\r\n").
func (r *reader) readResponse() (tag, interface{}, os.Error) {
func (r *reader) readResponse() (tag, interface{}, error) {
tag, err := r.readTag()
if err != nil {
return untagged, nil, err
Expand All @@ -88,13 +88,13 @@ func (r *reader) readResponse() (tag, interface{}, os.Error) {

// Read the tag, the first part of the response.
// Expects either "*" or "a123".
func (r *reader) readTag() (tag, os.Error) {
func (r *reader) readTag() (tag, error) {
str, err := r.readToken()
if err != nil {
return untagged, err
}
if len(str) == 0 {
return untagged, os.NewError("read empty tag")
return untagged, errors.New("read empty tag")
}

switch str[0] {
Expand Down Expand Up @@ -130,10 +130,10 @@ type ResponseUIDNext struct {
}

// Read a status response, one starting with OK/NO/BAD.
func (r *reader) readStatus(statusStr string) (resp *ResponseStatus, outErr os.Error) {
func (r *reader) readStatus(statusStr string) (resp *ResponseStatus, outErr error) {
defer func() {
if e := recover(); e != nil {
if osErr, ok := e.(os.Error); ok {
if osErr, ok := e.(error); ok {
outErr = osErr
return
}
Expand All @@ -142,7 +142,7 @@ func (r *reader) readStatus(statusStr string) (resp *ResponseStatus, outErr os.E
}()

if len(statusStr) == 0 {
var err os.Error
var err error
statusStr, err = r.readToken()
check(err)
}
Expand Down Expand Up @@ -373,10 +373,10 @@ type ResponseRecent struct {
Count int
}

func (r *reader) readUntagged() (resp interface{}, outErr os.Error) {
func (r *reader) readUntagged() (resp interface{}, outErr error) {
defer func() {
if e := recover(); e != nil {
if osErr, ok := e.(os.Error); ok {
if osErr, ok := e.(error); ok {
outErr = osErr
return
}
Expand Down
4 changes: 2 additions & 2 deletions imapsync/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ import (
var logger *log.Logger

type LoggingReader struct {
r io.Reader
r io.Reader
max int
}

func newLoggingReader(r io.Reader, max int) *LoggingReader {
return &LoggingReader{r, max}
}

func (r *LoggingReader) Read(p []byte) (int, os.Error) {
func (r *LoggingReader) Read(p []byte) (int, error) {
if logger == nil {
logger = log.New(os.Stderr, "", log.Ltime)
}
Expand Down
Loading