| 1 | package control |
| 2 | |
| 3 | import ( |
| 4 | "strings" |
| 5 | |
| 6 | "github.com/cretz/bine/torutil" |
| 7 | ) |
| 8 | |
| 9 | // SetConf invokes SETCONF. |
| 10 | func (c *Conn) SetConf(entries ...*KeyVal) error { |
| 11 | return c.sendSetConf("SETCONF", entries) |
| 12 | } |
| 13 | |
| 14 | // ResetConf invokes RESETCONF. |
| 15 | func (c *Conn) ResetConf(entries ...*KeyVal) error { |
| 16 | return c.sendSetConf("RESETCONF", entries) |
| 17 | } |
| 18 | |
| 19 | func (c *Conn) sendSetConf(cmd string, entries []*KeyVal) error { |
| 20 | for _, entry := range entries { |
| 21 | cmd += " " + entry.Key |
| 22 | if entry.ValSet() { |
| 23 | cmd += "=" + torutil.EscapeSimpleQuotedStringIfNeeded(entry.Val) |
| 24 | } |
| 25 | } |
| 26 | return c.sendRequestIgnoreResponse(cmd) |
| 27 | } |
| 28 | |
| 29 | // GetConf invokes GETCONF and returns the values for the requested keys. |
| 30 | func (c *Conn) GetConf(keys ...string) ([]*KeyVal, error) { |
| 31 | resp, err := c.SendRequest("GETCONF %v", strings.Join(keys, " ")) |
| 32 | if err != nil { |
| 33 | return nil, err |
| 34 | } |
| 35 | data := resp.DataWithReply() |
| 36 | ret := make([]*KeyVal, 0, len(data)) |
| 37 | for _, data := range data { |
| 38 | key, val, ok := torutil.PartitionString(data, '=') |
| 39 | entry := &KeyVal{Key: key} |
| 40 | if ok { |
| 41 | if entry.Val, err = torutil.UnescapeSimpleQuotedStringIfNeeded(val); err != nil { |
| 42 | return nil, err |
| 43 | } |
| 44 | if len(entry.Val) == 0 { |
| 45 | entry.ValSetAndEmpty = true |
| 46 | } |
| 47 | } |
| 48 | ret = append(ret, entry) |
| 49 | } |
| 50 | return ret, nil |
| 51 | } |
| 52 | |
| 53 | // SaveConf invokes SAVECONF. |
| 54 | func (c *Conn) SaveConf(force bool) error { |
| 55 | cmd := "SAVECONF" |
| 56 | if force { |
| 57 | cmd += " FORCE" |
| 58 | } |
| 59 | return c.sendRequestIgnoreResponse(cmd) |
| 60 | } |
| 61 | |
| 62 | // LoadConf invokes LOADCONF. |
| 63 | func (c *Conn) LoadConf(conf string) error { |
| 64 | return c.sendRequestIgnoreResponse("+LOADCONF\r\n%v\r\n.", conf) |
| 65 | } |