summaryrefslogtreecommitdiff
path: root/vendor/github.com/cretz/bine/control/cmd_misc.go
blob: 6c3fa4919c5cd947490d00d1ce6685d98dddc989 (plain)
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package control

import (
	"strings"

	"github.com/cretz/bine/torutil"
)

// Signal invokes SIGNAL.
func (c *Conn) Signal(signal string) error {
	return c.sendRequestIgnoreResponse("SIGNAL %v", signal)
}

// Quit invokes QUIT.
func (c *Conn) Quit() error {
	return c.sendRequestIgnoreResponse("QUIT")
}

// MapAddresses invokes MAPADDRESS and returns mapped addresses.
func (c *Conn) MapAddresses(addresses ...*KeyVal) ([]*KeyVal, error) {
	cmd := "MAPADDRESS"
	for _, address := range addresses {
		cmd += " " + address.Key + "=" + address.Val
	}
	resp, err := c.SendRequest(cmd)
	if err != nil {
		return nil, err
	}
	data := resp.DataWithReply()
	ret := make([]*KeyVal, 0, len(data))
	for _, address := range data {
		mappedAddress := &KeyVal{}
		mappedAddress.Key, mappedAddress.Val, _ = torutil.PartitionString(address, '=')
		ret = append(ret, mappedAddress)
	}
	return ret, nil
}

// GetInfo invokes GETINTO and returns values for requested keys.
func (c *Conn) GetInfo(keys ...string) ([]*KeyVal, error) {
	resp, err := c.SendRequest("GETINFO %v", strings.Join(keys, " "))
	if err != nil {
		return nil, err
	}
	ret := make([]*KeyVal, 0, len(resp.Data))
	for _, val := range resp.Data {
		infoVal := &KeyVal{}
		infoVal.Key, infoVal.Val, _ = torutil.PartitionString(val, '=')
		if infoVal.Val, err = torutil.UnescapeSimpleQuotedStringIfNeeded(infoVal.Val); err != nil {
			return nil, err
		}
		ret = append(ret, infoVal)
	}
	return ret, nil
}

// PostDescriptor invokes POSTDESCRIPTOR.
func (c *Conn) PostDescriptor(descriptor string, purpose string, cache string) error {
	cmd := "+POSTDESCRIPTOR"
	if purpose != "" {
		cmd += " purpose=" + purpose
	}
	if cache != "" {
		cmd += " cache=" + cache
	}
	cmd += "\r\n" + descriptor + "\r\n."
	return c.sendRequestIgnoreResponse(cmd)
}

// UseFeatures invokes USEFEATURE.
func (c *Conn) UseFeatures(features ...string) error {
	return c.sendRequestIgnoreResponse("USEFEATURE " + strings.Join(features, " "))
}

// ResolveAsync invokes RESOLVE.
func (c *Conn) ResolveAsync(address string, reverse bool) error {
	cmd := "RESOLVE "
	if reverse {
		cmd += "mode=reverse "
	}
	return c.sendRequestIgnoreResponse(cmd + address)
}

// TakeOwnership invokes TAKEOWNERSHIP.
func (c *Conn) TakeOwnership() error {
	return c.sendRequestIgnoreResponse("TAKEOWNERSHIP")
}

// DropGuards invokes DROPGUARDS.
func (c *Conn) DropGuards() error {
	return c.sendRequestIgnoreResponse("DROPGUARDS")
}