client.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. Package manageSieve implements a simple client for the protocol for remotely managing sieve scripts.
  3. RFC5804 - https://tools.ietf.org/html/rfc5804
  4. Done
  5. These commands are implemented:
  6. STARTSSL
  7. AUTHENTICATE (Plain)
  8. LISTSCRIPTS
  9. GETSCRIPT
  10. TODO
  11. These need to be done:
  12. CAPABILITY
  13. HAVESPACE
  14. PUTSCRIPT
  15. SETACTIVE
  16. DELETESCRIPT
  17. RENAMESCRIPT
  18. CHECKSCRIPT
  19. */
  20. package manageSieve
  21. import (
  22. "bufio"
  23. "crypto/tls"
  24. "encoding/base64"
  25. "fmt"
  26. "net"
  27. "strings"
  28. "sync"
  29. )
  30. type Client struct {
  31. sl sync.Mutex
  32. scanner *bufio.Scanner
  33. conn net.Conn
  34. serverName string
  35. }
  36. func Dial(addr string) (*Client, error) {
  37. conn, err := net.Dial("tcp", addr)
  38. if err != nil {
  39. return nil, err
  40. }
  41. // conn = debug.WrapConn(conn)
  42. host, _, _ := net.SplitHostPort(addr)
  43. return NewClient(conn, host)
  44. }
  45. func NewClient(conn net.Conn, host string) (*Client, error) {
  46. c := &Client{
  47. scanner: bufio.NewScanner(conn),
  48. conn: conn,
  49. serverName: host,
  50. }
  51. _, err := c.waitForOK() // TODO: parse capabilities
  52. if err != nil {
  53. return nil, err
  54. }
  55. return c, nil
  56. }
  57. func (c *Client) StartTLS(config *tls.Config) error {
  58. _, err := c.cmd(true, "STARTTLS")
  59. if err != nil {
  60. return err
  61. }
  62. if config == nil {
  63. config = &tls.Config{
  64. ServerName: c.serverName,
  65. }
  66. }
  67. c.conn = tls.Client(c.conn, config)
  68. // c.conn = debug.WrapConn(c.conn)
  69. c.scanner = bufio.NewScanner(c.conn)
  70. _, err = c.waitForOK() // TODO: parse capabilities
  71. return err
  72. }
  73. func (c *Client) Login(user, pass string) error {
  74. data := []byte("\x00" + user + "\x00" + pass)
  75. creds := base64.StdEncoding.EncodeToString(data)
  76. _, err := c.cmd(true, "AUTHENTICATE %q %q", "PLAIN", creds)
  77. return err
  78. }
  79. type Script struct {
  80. Name string
  81. Active bool
  82. }
  83. func (c *Client) ListScripts() ([]Script, error) {
  84. b, err := c.cmd(true, "LISTSCRIPTS")
  85. if err != nil {
  86. return nil, err
  87. }
  88. var scripts []Script
  89. for _, l := range b[:len(b)-1] {
  90. var s Script
  91. if l[0] != '"' {
  92. return nil, fmt.Errorf("Illegal Line: %q", l)
  93. }
  94. s.Name = l[1:strings.LastIndex(l, "\"")]
  95. s.Active = strings.HasSuffix(l, "ACTIVE")
  96. scripts = append(scripts, s)
  97. }
  98. return scripts, nil
  99. }
  100. func (c *Client) GetScript(name string) (string, error) {
  101. b, err := c.cmd(true, "GETSCRIPT %q", name)
  102. if err != nil {
  103. return "", err
  104. }
  105. // TODO: Validate return?
  106. // scriptLenStr := b[0] // {int}
  107. // scriptLen, err := strconv.Atoi(scriptLenStr[1 : len(scriptLenStr)-1])
  108. // if err != nil {
  109. // return "", err
  110. // }
  111. // log.Println("scriptLen:", scriptLen)
  112. return strings.Join(b[1:len(b)-1], "\n"), nil
  113. }
  114. func (c *Client) cmd(wait bool, format string, args ...interface{}) ([]string, error) {
  115. c.sl.Lock()
  116. defer c.sl.Unlock()
  117. _, err := fmt.Fprintf(c.conn, format+"\r\n", args...)
  118. if err != nil {
  119. return nil, err
  120. }
  121. if wait {
  122. return c.waitForOK()
  123. }
  124. // TODO: not actually used..
  125. return []string{c.scanner.Text()}, c.scanner.Err()
  126. // return nil, nil
  127. }
  128. // read lines until OK
  129. func (c *Client) waitForOK() ([]string, error) {
  130. var b []string
  131. for c.scanner.Scan() {
  132. l := c.scanner.Text()
  133. b = append(b, l)
  134. if strings.HasPrefix(l, "OK ") {
  135. break
  136. }
  137. }
  138. return b, c.scanner.Err()
  139. }