mirror of
https://github.com/mjl-/mox.git
synced 2025-07-16 07:14:40 +03:00
rename variables, struct fields and functions to include an "x" when they can panic for handling errors
and document the convention in develop.txt. spurred by running errcheck again (it has been a while). it still has too many false to enable by default.
This commit is contained in:
@ -30,17 +30,19 @@ import (
|
||||
// Conn is an IMAP connection to a server.
|
||||
type Conn struct {
|
||||
// Connection, may be original TCP or TLS connection. Reads go through c.br, and
|
||||
// writes through c.bw. It wraps a tracing reading/writer and may wrap flate
|
||||
// compression.
|
||||
conn net.Conn
|
||||
connBroken bool // If connection is broken, we won't flush (and write) again.
|
||||
br *bufio.Reader
|
||||
bw *bufio.Writer
|
||||
compress bool // If compression is enabled, we must flush flateWriter and its target original bufio writer.
|
||||
flateWriter *moxio.FlateWriter
|
||||
flateBW *bufio.Writer
|
||||
tr *moxio.TraceReader
|
||||
tw *moxio.TraceWriter
|
||||
// writes through c.xbw. The "x" for the writes indicate that failed writes cause
|
||||
// an i/o panic, which is either turned into a returned error, or passed on (see
|
||||
// boolean panic). The reader and writer wrap a tracing reading/writer and may wrap
|
||||
// flate compression.
|
||||
conn net.Conn
|
||||
connBroken bool // If connection is broken, we won't flush (and write) again.
|
||||
br *bufio.Reader
|
||||
tr *moxio.TraceReader
|
||||
xbw *bufio.Writer
|
||||
compress bool // If compression is enabled, we must flush flateWriter and its target original bufio writer.
|
||||
xflateWriter *moxio.FlateWriter
|
||||
xflateBW *bufio.Writer
|
||||
xtw *moxio.TraceWriter
|
||||
|
||||
log mlog.Log
|
||||
panic bool
|
||||
@ -86,8 +88,8 @@ func New(cid int64, conn net.Conn, xpanic bool) (client *Conn, rerr error) {
|
||||
c.br = bufio.NewReader(c.tr)
|
||||
|
||||
// Writes are buffered and write to Conn, which may panic.
|
||||
c.tw = moxio.NewTraceWriter(log, "CW: ", &c)
|
||||
c.bw = bufio.NewWriter(c.tw)
|
||||
c.xtw = moxio.NewTraceWriter(log, "CW: ", &c)
|
||||
c.xbw = bufio.NewWriter(c.xtw)
|
||||
|
||||
defer c.recover(&rerr)
|
||||
tag := c.xnonspace()
|
||||
@ -171,14 +173,14 @@ func (c *Conn) xflush() {
|
||||
return
|
||||
}
|
||||
|
||||
err := c.bw.Flush()
|
||||
err := c.xbw.Flush()
|
||||
c.xcheckf(err, "flush")
|
||||
|
||||
// If compression is active, we need to flush the deflate stream.
|
||||
if c.compress {
|
||||
err := c.flateWriter.Flush()
|
||||
err := c.xflateWriter.Flush()
|
||||
c.xcheckf(err, "flush deflate")
|
||||
err = c.flateBW.Flush()
|
||||
err = c.xflateBW.Flush()
|
||||
c.xcheckf(err, "flush deflate buffer")
|
||||
}
|
||||
}
|
||||
@ -186,11 +188,11 @@ func (c *Conn) xflush() {
|
||||
func (c *Conn) xtrace(level slog.Level) func() {
|
||||
c.xflush()
|
||||
c.tr.SetTrace(level)
|
||||
c.tw.SetTrace(level)
|
||||
c.xtw.SetTrace(level)
|
||||
return func() {
|
||||
c.xflush()
|
||||
c.tr.SetTrace(mlog.LevelTrace)
|
||||
c.tw.SetTrace(mlog.LevelTrace)
|
||||
c.xtw.SetTrace(mlog.LevelTrace)
|
||||
}
|
||||
}
|
||||
|
||||
@ -214,13 +216,13 @@ func (c *Conn) Close() (rerr error) {
|
||||
if c.conn == nil {
|
||||
return nil
|
||||
}
|
||||
if !c.connBroken && c.flateWriter != nil {
|
||||
err := c.flateWriter.Close()
|
||||
if !c.connBroken && c.xflateWriter != nil {
|
||||
err := c.xflateWriter.Close()
|
||||
c.xcheckf(err, "close deflate writer")
|
||||
err = c.flateBW.Flush()
|
||||
err = c.xflateBW.Flush()
|
||||
c.xcheckf(err, "flush deflate buffer")
|
||||
c.flateWriter = nil
|
||||
c.flateBW = nil
|
||||
c.xflateWriter = nil
|
||||
c.xflateBW = nil
|
||||
}
|
||||
err := c.conn.Close()
|
||||
c.xcheckf(err, "close connection")
|
||||
@ -248,8 +250,7 @@ func (c *Conn) Commandf(tag string, format string, args ...any) (rerr error) {
|
||||
}
|
||||
c.LastTag = tag
|
||||
|
||||
_, err := fmt.Fprintf(c.bw, "%s %s\r\n", tag, fmt.Sprintf(format, args...))
|
||||
c.xcheckf(err, "write command")
|
||||
fmt.Fprintf(c.xbw, "%s %s\r\n", tag, fmt.Sprintf(format, args...))
|
||||
c.xflush()
|
||||
return
|
||||
}
|
||||
@ -337,8 +338,7 @@ func (c *Conn) Writelinef(format string, args ...any) (rerr error) {
|
||||
defer c.recover(&rerr)
|
||||
|
||||
s := fmt.Sprintf(format, args...)
|
||||
_, err := fmt.Fprintf(c.bw, "%s\r\n", s)
|
||||
c.xcheckf(err, "writeline")
|
||||
fmt.Fprintf(c.xbw, "%s\r\n", s)
|
||||
c.xflush()
|
||||
return nil
|
||||
}
|
||||
@ -348,8 +348,7 @@ func (c *Conn) Writelinef(format string, args ...any) (rerr error) {
|
||||
func (c *Conn) WriteSyncLiteral(s string) (untagged []Untagged, rerr error) {
|
||||
defer c.recover(&rerr)
|
||||
|
||||
_, err := fmt.Fprintf(c.bw, "{%d}\r\n", len(s))
|
||||
c.xcheckf(err, "write sync literal size")
|
||||
fmt.Fprintf(c.xbw, "{%d}\r\n", len(s))
|
||||
c.xflush()
|
||||
|
||||
plus, err := c.br.Peek(1)
|
||||
@ -358,7 +357,7 @@ func (c *Conn) WriteSyncLiteral(s string) (untagged []Untagged, rerr error) {
|
||||
_, err = c.Readline()
|
||||
c.xcheckf(err, "read continuation line")
|
||||
|
||||
_, err = c.bw.Write([]byte(s))
|
||||
_, err = c.xbw.Write([]byte(s))
|
||||
c.xcheckf(err, "write literal data")
|
||||
c.xflush()
|
||||
return nil, nil
|
||||
|
@ -58,9 +58,9 @@ func (c *Conn) Login(username, password string) (untagged []Untagged, result Res
|
||||
defer c.recover(&rerr)
|
||||
|
||||
c.LastTag = c.nextTag()
|
||||
fmt.Fprintf(c.bw, "%s login %s ", c.LastTag, astring(username))
|
||||
fmt.Fprintf(c.xbw, "%s login %s ", c.LastTag, astring(username))
|
||||
defer c.xtrace(mlog.LevelTraceauth)()
|
||||
fmt.Fprintf(c.bw, "%s\r\n", astring(password))
|
||||
fmt.Fprintf(c.xbw, "%s\r\n", astring(password))
|
||||
c.xtrace(mlog.LevelTrace) // Restore.
|
||||
return c.Response()
|
||||
}
|
||||
@ -69,18 +69,19 @@ func (c *Conn) Login(username, password string) (untagged []Untagged, result Res
|
||||
func (c *Conn) AuthenticatePlain(username, password string) (untagged []Untagged, result Result, rerr error) {
|
||||
defer c.recover(&rerr)
|
||||
|
||||
c.Commandf("", "authenticate plain")
|
||||
err := c.Commandf("", "authenticate plain")
|
||||
c.xcheckf(err, "writing authenticate command")
|
||||
_, untagged, result, rerr = c.ReadContinuation()
|
||||
c.xcheckf(rerr, "reading continuation")
|
||||
if result.Status != "" {
|
||||
c.xerrorf("got result status %q, expected continuation", result.Status)
|
||||
}
|
||||
defer c.xtrace(mlog.LevelTraceauth)()
|
||||
xw := base64.NewEncoder(base64.StdEncoding, c.bw)
|
||||
xw := base64.NewEncoder(base64.StdEncoding, c.xbw)
|
||||
fmt.Fprintf(xw, "\u0000%s\u0000%s", username, password)
|
||||
xw.Close()
|
||||
c.xtrace(mlog.LevelTrace) // Restore.
|
||||
fmt.Fprintf(c.bw, "\r\n")
|
||||
fmt.Fprintf(c.xbw, "\r\n")
|
||||
c.xflush()
|
||||
return c.Response()
|
||||
}
|
||||
@ -153,15 +154,15 @@ func (c *Conn) CompressDeflate() (untagged []Untagged, result Result, rerr error
|
||||
untagged, result, rerr = c.Transactf("compress deflate")
|
||||
c.xcheck(rerr)
|
||||
|
||||
c.flateBW = bufio.NewWriter(c)
|
||||
fw0, err := flate.NewWriter(c.flateBW, flate.DefaultCompression)
|
||||
c.xflateBW = bufio.NewWriter(c)
|
||||
fw0, err := flate.NewWriter(c.xflateBW, flate.DefaultCompression)
|
||||
c.xcheckf(err, "deflate") // Cannot happen.
|
||||
fw := moxio.NewFlateWriter(fw0)
|
||||
|
||||
c.compress = true
|
||||
c.flateWriter = fw
|
||||
c.tw = moxio.NewTraceWriter(mlog.New("imapclient", nil), "CW: ", fw)
|
||||
c.bw = bufio.NewWriter(c.tw)
|
||||
c.xflateWriter = fw
|
||||
c.xtw = moxio.NewTraceWriter(mlog.New("imapclient", nil), "CW: ", fw)
|
||||
c.xbw = bufio.NewWriter(c.xtw)
|
||||
|
||||
rc := c.xprefixConn()
|
||||
fr := flate.NewReaderPartial(rc)
|
||||
@ -303,8 +304,7 @@ func (c *Conn) Append(mailbox string, message Append, more ...Append) (untagged
|
||||
tag := c.nextTag()
|
||||
c.LastTag = tag
|
||||
|
||||
_, err := fmt.Fprintf(c.bw, "%s append %s", tag, astring(mailbox))
|
||||
c.xcheckf(err, "write command")
|
||||
fmt.Fprintf(c.xbw, "%s append %s", tag, astring(mailbox))
|
||||
|
||||
msgs := append([]Append{message}, more...)
|
||||
for _, m := range msgs {
|
||||
@ -316,14 +316,14 @@ func (c *Conn) Append(mailbox string, message Append, more ...Append) (untagged
|
||||
// todo: use literal8 if needed, with "UTF8()" if required.
|
||||
// todo: for larger messages, use a synchronizing literal.
|
||||
|
||||
fmt.Fprintf(c.bw, " (%s)%s {%d+}\r\n", strings.Join(m.Flags, " "), date, m.Size)
|
||||
fmt.Fprintf(c.xbw, " (%s)%s {%d+}\r\n", strings.Join(m.Flags, " "), date, m.Size)
|
||||
defer c.xtrace(mlog.LevelTracedata)()
|
||||
_, err := io.Copy(c.bw, m.Data)
|
||||
_, err := io.Copy(c.xbw, m.Data)
|
||||
c.xcheckf(err, "write message data")
|
||||
c.xtrace(mlog.LevelTrace) // Restore
|
||||
}
|
||||
|
||||
fmt.Fprintf(c.bw, "\r\n")
|
||||
fmt.Fprintf(c.xbw, "\r\n")
|
||||
c.xflush()
|
||||
return c.Response()
|
||||
}
|
||||
@ -441,14 +441,15 @@ func (c *Conn) replace(cmd string, num string, mailbox string, msg Append) (unta
|
||||
}
|
||||
// todo: only use literal8 if needed, possibly with "UTF8()"
|
||||
// todo: encode mailbox
|
||||
c.Commandf("", "%s %s %s (%s)%s ~{%d+}", cmd, num, astring(mailbox), strings.Join(msg.Flags, " "), date, msg.Size)
|
||||
err := c.Commandf("", "%s %s %s (%s)%s ~{%d+}", cmd, num, astring(mailbox), strings.Join(msg.Flags, " "), date, msg.Size)
|
||||
c.xcheckf(err, "writing replace command")
|
||||
|
||||
defer c.xtrace(mlog.LevelTracedata)()
|
||||
_, err := io.Copy(c.bw, msg.Data)
|
||||
_, err = io.Copy(c.xbw, msg.Data)
|
||||
c.xcheckf(err, "write message data")
|
||||
c.xtrace(mlog.LevelTrace)
|
||||
|
||||
fmt.Fprintf(c.bw, "\r\n")
|
||||
fmt.Fprintf(c.xbw, "\r\n")
|
||||
c.xflush()
|
||||
|
||||
return c.Response()
|
||||
|
@ -856,8 +856,7 @@ func (c *Conn) xliteral() []byte {
|
||||
c.xerrorf("refusing to read more than 1MB: %d", size)
|
||||
}
|
||||
if sync {
|
||||
_, err := fmt.Fprintf(c.bw, "+ ok\r\n")
|
||||
c.xcheckf(err, "write continuation")
|
||||
fmt.Fprintf(c.xbw, "+ ok\r\n")
|
||||
c.xflush()
|
||||
}
|
||||
buf := make([]byte, int(size))
|
||||
|
Reference in New Issue
Block a user