-
Notifications
You must be signed in to change notification settings - Fork 1
/
conn.go
72 lines (63 loc) · 1.31 KB
/
conn.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
60
61
62
63
64
65
66
67
68
69
70
71
72
package dovecotsasl
import (
"bufio"
"fmt"
"io"
"net"
"strings"
)
type conn struct {
C net.Conn
W *bufio.Writer
R *bufio.Scanner
}
func (c *conn) Writeln(cmd string, params ...string) error {
if _, err := c.W.WriteString(cmd); err != nil {
return err
}
if len(params) != 0 {
if _, err := c.W.WriteRune('\t'); err != nil {
return err
}
}
for i, p := range params {
if _, err := c.W.WriteString(p); err != nil {
return err
}
if i != len(params)-1 {
if _, err := c.W.WriteRune('\t'); err != nil {
return err
}
}
}
if _, err := c.W.WriteRune('\n'); err != nil {
return err
}
return c.W.Flush()
}
func (c *conn) Readln() (string, []string, error) {
if !c.R.Scan() {
if err := c.R.Err(); err != nil {
return "", nil, err
}
return "", nil, io.EOF
}
parts := strings.Split(c.R.Text(), "\t")
return parts[0], parts[1:], nil
}
func (c *conn) ReadlnExpect(expectCmd string, atleastParams int) ([]string, error) {
cmd, params, err := c.Readln()
if err != nil {
return nil, err
}
if cmd != expectCmd {
return nil, fmt.Errorf("dovecotsasl: unexpected command: %v", cmd)
}
if len(params) < atleastParams {
return nil, fmt.Errorf("dovecotsasl: not enough params for %s: %v", cmd, len(params))
}
return params, nil
}
func (c *conn) Close() error {
return c.C.Close()
}