/
exemption.go
113 lines (95 loc) · 2.55 KB
/
exemption.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package unfurl
import (
"sync"
"github.com/keybase/client/go/chat/types"
"github.com/keybase/client/go/protocol/chat1"
)
type OneTimeWhitelistExemption struct {
sync.Mutex
used bool
convID chat1.ConversationID
msgID chat1.MessageID
domain string
}
var _ (types.WhitelistExemption) = (*OneTimeWhitelistExemption)(nil)
func NewOneTimeWhitelistExemption(convID chat1.ConversationID, msgID chat1.MessageID, domain string) *OneTimeWhitelistExemption {
return &OneTimeWhitelistExemption{
convID: convID,
msgID: msgID,
domain: domain,
}
}
func (o *OneTimeWhitelistExemption) Use() bool {
o.Lock()
defer o.Unlock()
res := !o.used
o.used = true
return res
}
func (o *OneTimeWhitelistExemption) Matches(convID chat1.ConversationID, msgID chat1.MessageID,
domain string) bool {
return o.convID.Eq(convID) && o.msgID == msgID && o.domain == domain
}
func (o *OneTimeWhitelistExemption) Domain() string {
o.Lock()
defer o.Unlock()
return o.domain
}
type SingleMessageWhitelistExemption struct {
sync.Mutex
convID chat1.ConversationID
msgID chat1.MessageID
domain string
}
var _ (types.WhitelistExemption) = (*SingleMessageWhitelistExemption)(nil)
func NewSingleMessageWhitelistExemption(convID chat1.ConversationID, msgID chat1.MessageID, domain string) *SingleMessageWhitelistExemption {
return &SingleMessageWhitelistExemption{
convID: convID,
msgID: msgID,
domain: domain,
}
}
func (o *SingleMessageWhitelistExemption) Use() bool {
o.Lock()
defer o.Unlock()
return true
}
func (o *SingleMessageWhitelistExemption) Matches(convID chat1.ConversationID, msgID chat1.MessageID,
domain string) bool {
return o.convID.Eq(convID) && o.msgID == msgID && o.domain == domain
}
func (o *SingleMessageWhitelistExemption) Domain() string {
o.Lock()
defer o.Unlock()
return o.domain
}
type WhitelistExemptionList struct {
sync.Mutex
exemptions []types.WhitelistExemption
}
func NewWhitelistExemptionList() *WhitelistExemptionList {
return &WhitelistExemptionList{}
}
func (l *WhitelistExemptionList) Add(e types.WhitelistExemption) {
l.Lock()
defer l.Unlock()
l.exemptions = append(l.exemptions, e)
}
func (l *WhitelistExemptionList) Use(convID chat1.ConversationID, msgID chat1.MessageID, domain string) bool {
l.Lock()
defer l.Unlock()
var nextlist []types.WhitelistExemption
exempted := false
for _, e := range l.exemptions {
if exempted || !e.Matches(convID, msgID, domain) {
nextlist = append(nextlist, e)
continue
}
if e.Use() {
exempted = true
nextlist = append(nextlist, e)
}
}
l.exemptions = nextlist
return exempted
}