From 87e5c5d332185415dfedd296af82f85eb64efac8 Mon Sep 17 00:00:00 2001 From: Szymon Sobczak Date: Sun, 21 Jul 2013 23:43:14 +0200 Subject: [PATCH] fix compilation --- imap/imap.go | 25 ++++++++++++------------- imap/parser.go | 35 ++++++++++++++++++----------------- imap/protocol.go | 22 +++++++++++----------- imapsync/debug.go | 4 ++-- imapsync/main.go | 18 +++++++++--------- imapsync/mbox.go | 9 ++++----- imapsync/netmon.go | 7 +++---- 7 files changed, 59 insertions(+), 61 deletions(-) diff --git a/imap/imap.go b/imap/imap.go index a15ead2..b8690b7 100644 --- a/imap/imap.go +++ b/imap/imap.go @@ -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) } @@ -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 @@ -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++ @@ -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 { @@ -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 @@ -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 { @@ -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, @@ -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 @@ -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 { @@ -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() diff --git a/imap/parser.go b/imap/parser.go index 6a6b225..6f3d63b 100644 --- a/imap/parser.go +++ b/imap/parser.go @@ -3,10 +3,10 @@ package imap import ( "bufio" "bytes" + "errors" "fmt" "io" "log" - "os" "strconv" ) @@ -14,9 +14,9 @@ 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 } @@ -25,6 +25,7 @@ func recoverError(err *os.Error) { } type sexp interface{} + // One of: // string // []sexp @@ -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) @@ -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)) @@ -84,7 +85,7 @@ 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 @@ -92,7 +93,7 @@ func (p *parser) readNumber() (num int, outErr os.Error) { 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 @@ -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 = @@ -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("\"") @@ -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 */ @@ -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("(") @@ -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 @@ -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 } diff --git a/imap/protocol.go b/imap/protocol.go index 8a69069..965bda1 100644 --- a/imap/protocol.go +++ b/imap/protocol.go @@ -1,9 +1,9 @@ package imap import ( - "os" - "strconv" + "errors" "fmt" + "strconv" ) // Status represents server status codes which are returned by @@ -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) } @@ -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 @@ -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] { @@ -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 } @@ -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) } @@ -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 } diff --git a/imapsync/debug.go b/imapsync/debug.go index 6ce0487..04e1515 100644 --- a/imapsync/debug.go +++ b/imapsync/debug.go @@ -9,7 +9,7 @@ import ( var logger *log.Logger type LoggingReader struct { - r io.Reader + r io.Reader max int } @@ -17,7 +17,7 @@ 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) } diff --git a/imapsync/main.go b/imapsync/main.go index 0ebfa1b..bd6942a 100644 --- a/imapsync/main.go +++ b/imapsync/main.go @@ -1,20 +1,20 @@ package main import ( - "crypto/tls" - "io" - "os" "bufio" - "imap" - "log" + "crypto/tls" "flag" "fmt" + "github.com/martine/go-imap/imap" + "io" + "log" + "os" "time" ) var dumpProtocol *bool = flag.Bool("dumpprotocol", false, "dump imap stream") -func check(err os.Error) { +func check(err error) { if err != nil { panic(err) } @@ -118,7 +118,7 @@ func (ui *UI) fetch(im *imap.IMAP, mailbox string) { ch, err := im.FetchAsync(query, []string{"RFC822"}) check(err) - envelopeDate := time.LocalTime().Format(time.ANSIC) + envelopeDate := time.Now().Local().Format(time.ANSIC) i := 0 total := examine.Exists @@ -168,7 +168,7 @@ func (ui *UI) runFetch(mailbox string) { overprint = true default: if s != nil { - status = s.(os.Error).String() + status = s.(error).Error() ui.statusChan = nil ticker.Stop() } @@ -191,7 +191,7 @@ func (ui *UI) runFetch(mailbox string) { overprintLast = overprint fmt.Printf("%s", status) if overprint && ui.netmon != nil { - fmt.Printf(" [%.1fk/s]", ui.netmon.Bandwidth() / 1000.0) + fmt.Printf(" [%.1fk/s]", ui.netmon.Bandwidth()/1000.0) } } fmt.Printf("\n") diff --git a/imapsync/mbox.go b/imapsync/mbox.go index abb98de..a81262d 100644 --- a/imapsync/mbox.go +++ b/imapsync/mbox.go @@ -2,16 +2,15 @@ package main import ( "bytes" - "io" - "os" "fmt" + "io" ) type fromEncodingWriter struct { w io.Writer } -func (w *fromEncodingWriter) Write(buf []byte) (int, os.Error) { +func (w *fromEncodingWriter) Write(buf []byte) (int, error) { total := 0 for len(buf) > 0 { // Insert a quote for the current line, if needed. @@ -20,7 +19,7 @@ func (w *fromEncodingWriter) Write(buf []byte) (int, os.Error) { // iterate } magicFrom := []byte("From ") - if ofs + len(magicFrom) <= len(buf) && + if ofs+len(magicFrom) <= len(buf) && bytes.Equal(buf[ofs:ofs+len(magicFrom)], magicFrom) { _, err := w.w.Write([]byte(">")) if err != nil { @@ -55,7 +54,7 @@ func newMbox(w io.Writer) *mbox { return &mbox{w} } -func (m *mbox) writeMessage(envelopeFrom string, envelopeDate string, rfc822 []byte) os.Error { +func (m *mbox) writeMessage(envelopeFrom string, envelopeDate string, rfc822 []byte) error { _, err := m.Write([]byte(fmt.Sprintf("From %s %s\r\n", envelopeFrom, envelopeDate))) if err != nil { return err diff --git a/imapsync/netmon.go b/imapsync/netmon.go index bb7e337..2166413 100644 --- a/imapsync/netmon.go +++ b/imapsync/netmon.go @@ -1,7 +1,6 @@ package main import ( - "os" "io" "sync" ) @@ -17,14 +16,14 @@ type netmonReader struct { } func newNetmonReader(r io.Reader) *netmonReader { - return &netmonReader{r:r} + return &netmonReader{r: r} } func (n *netmonReader) Tick() int { n.lock.Lock() var alpha float32 = 0.9 bucket := n.bucket - n.estimate = (alpha * float32(n.bucket)) + ((1-alpha) * n.estimate) + n.estimate = (alpha * float32(n.bucket)) + ((1 - alpha) * n.estimate) n.bucket = 0 n.lock.Unlock() return bucket @@ -37,7 +36,7 @@ func (n *netmonReader) Bandwidth() float32 { return val } -func (n *netmonReader) Read(buf []byte) (int, os.Error) { +func (n *netmonReader) Read(buf []byte) (int, error) { nb, err := n.r.Read(buf) n.lock.Lock() n.bucket += nb