-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathquery.go
96 lines (76 loc) · 2.15 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package otr3
import (
"bytes"
"strconv"
"time"
)
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 {
r := rune(c)
if r == '?' {
break
}
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
}
var timeoutLength = time.Duration(1) * time.Minute
func isWithinTimeToIgnoreQueryMessage(t time.Time) bool {
return t.Add(timeoutLength).After(time.Now())
}
func (c *Conversation) receiveQueryMessage(msg ValidMessage) ([]messageWithHeader, error) {
versions := extractVersionsFromQueryMessage(c.Policies, msg)
err := c.commitToVersionFrom(versions)
if err != nil {
return nil, err
}
if dontIgnoreFastRepeatQueryMessage != "true" && ((c.msgState == encrypted && isWithinTimeToIgnoreQueryMessage(c.lastMessageStateChange)) ||
(c.ake != nil && isWithinTimeToIgnoreQueryMessage(c.ake.lastStateChange))) {
return nil, nil
}
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...)
}
//SetFriendlyQueryMessage will set a new message as query message
func (c *Conversation) SetFriendlyQueryMessage(msg string) {
c.friendlyQueryMessage = msg
}