forked from coyim/coyim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
query.go
82 lines (65 loc) · 1.72 KB
/
query.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
73
74
75
76
77
78
79
80
81
82
package otr3
import (
"bytes"
"strconv"
)
func isQueryMessage(msg ValidMessage) bool {
return bytes.HasPrefix(msg, queryMarker)
}
func parseOTRQueryMessage(msg ValidMessage) []int {
ret := []int{}
if bytes.HasPrefix(msg, queryMarker) && len(msg) > len(queryMarker) {
versions := msg[len(queryMarker):]
if versions[0] == '?' {
ret = append(ret, 1)
versions = versions[1:]
}
if len(versions) > 0 && versions[0] == 'v' {
for _, c := range versions {
if v, err := strconv.Atoi(string(c)); err == nil {
ret = append(ret, v)
}
}
}
}
return ret
}
func extractVersionsFromQueryMessage(p policies, msg ValidMessage) int {
versions := 0
for _, v := range parseOTRQueryMessage(msg) {
switch {
case v == 3 && p.has(allowV3):
versions |= (1 << 3)
case v == 2 && p.has(allowV2):
versions |= (1 << 2)
}
}
return versions
}
func (c *Conversation) receiveQueryMessage(msg ValidMessage) ([]messageWithHeader, error) {
versions := extractVersionsFromQueryMessage(c.Policies, msg)
err := c.commitToVersionFrom(versions)
if err != nil {
return nil, err
}
ts, err := c.sendDHCommit()
return c.potentialAuthError(compactMessagesWithHeader(ts), err)
}
//QueryMessage will return a QueryMessage determined by Conversation Policies
func (c Conversation) QueryMessage() ValidMessage {
queryMessage := []byte("?OTRv")
if c.Policies.has(allowV2) {
queryMessage = append(queryMessage, '2')
}
if c.Policies.has(allowV3) {
queryMessage = append(queryMessage, '3')
}
suffix := "?"
if c.friendlyQueryMessage != "" {
suffix = "? " + c.friendlyQueryMessage
}
return append(queryMessage, suffix...)
}
func (c *Conversation) SetFriendlyQueryMessage(msg string) {
c.friendlyQueryMessage = msg
}