bine/control/cmd_conf.go

66 lines
1.5 KiB
Go
Raw Normal View History

2018-05-11 04:08:20 +00:00
package control
import (
"strings"
2020-10-15 21:38:02 +00:00
"git.openprivacy.ca/openprivacy/bine/torutil"
2018-05-11 04:08:20 +00:00
)
// SetConf invokes SETCONF.
2018-05-11 19:43:58 +00:00
func (c *Conn) SetConf(entries ...*KeyVal) error {
2018-05-11 04:08:20 +00:00
return c.sendSetConf("SETCONF", entries)
}
// ResetConf invokes RESETCONF.
2018-05-11 19:43:58 +00:00
func (c *Conn) ResetConf(entries ...*KeyVal) error {
2018-05-11 04:08:20 +00:00
return c.sendSetConf("RESETCONF", entries)
}
2018-05-11 19:43:58 +00:00
func (c *Conn) sendSetConf(cmd string, entries []*KeyVal) error {
2018-05-11 04:08:20 +00:00
for _, entry := range entries {
cmd += " " + entry.Key
2018-05-11 19:43:58 +00:00
if entry.ValSet() {
cmd += "=" + torutil.EscapeSimpleQuotedStringIfNeeded(entry.Val)
2018-05-11 04:08:20 +00:00
}
}
return c.sendRequestIgnoreResponse(cmd)
2018-05-11 04:08:20 +00:00
}
// GetConf invokes GETCONF and returns the values for the requested keys.
2018-05-11 19:43:58 +00:00
func (c *Conn) GetConf(keys ...string) ([]*KeyVal, error) {
2018-05-11 04:08:20 +00:00
resp, err := c.SendRequest("GETCONF %v", strings.Join(keys, " "))
if err != nil {
return nil, err
}
data := resp.DataWithReply()
2018-05-11 19:43:58 +00:00
ret := make([]*KeyVal, 0, len(data))
2018-05-11 04:08:20 +00:00
for _, data := range data {
key, val, ok := torutil.PartitionString(data, '=')
2018-05-11 19:43:58 +00:00
entry := &KeyVal{Key: key}
2018-05-11 04:08:20 +00:00
if ok {
if entry.Val, err = torutil.UnescapeSimpleQuotedStringIfNeeded(val); err != nil {
2018-05-11 04:08:20 +00:00
return nil, err
}
2018-05-11 19:43:58 +00:00
if len(entry.Val) == 0 {
entry.ValSetAndEmpty = true
}
2018-05-11 04:08:20 +00:00
}
ret = append(ret, entry)
}
return ret, nil
}
// SaveConf invokes SAVECONF.
2018-05-11 04:08:20 +00:00
func (c *Conn) SaveConf(force bool) error {
cmd := "SAVECONF"
if force {
cmd += " FORCE"
}
return c.sendRequestIgnoreResponse(cmd)
}
// LoadConf invokes LOADCONF.
func (c *Conn) LoadConf(conf string) error {
return c.sendRequestIgnoreResponse("+LOADCONF\r\n%v\r\n.", conf)
2018-05-11 04:08:20 +00:00
}