forked from willscott/talexmpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
talekcontact.go
170 lines (147 loc) · 3.94 KB
/
talekcontact.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package main
import (
"encoding/json"
"github.com/privacylab/talek/libtalek"
)
type talekContactState int
const (
// A contact is generated with an offer to give to the other party.
offerGenerated talekContactState = iota
// A contact has been created from another offer. the initial response is sent.
answerSent = iota
// The handshake has been complete.
confirmed = iota
)
// TalekContact represents a bidirectional message channel with another user.
type TalekContact struct {
MyNick string
TheirName string
MyTopic *libtalek.Topic
TheirHandle *libtalek.Handle
state talekContactState
outgoing chan []byte //msgs from local user to the contact
incoming *func([]byte) //msgs from contact to local user
done chan byte
}
type talekOffer struct {
fromNick string
readHandle string
writeTopic string
}
// GetOffer makes a local contact / handle as text for a remote contact.
// the offer contains:
// * A handle for reading messages from me
// * A topic I'll read one msg off of with info on your handle.
func GetOffer(myName, theirName string) (*TalekContact, []byte) {
contact := new(TalekContact)
contact.MyNick = myName
contact.TheirName = theirName
contact.state = offerGenerated
var err error
contact.MyTopic, err = libtalek.NewTopic()
if err != nil {
panic(err)
}
toPoll, err := libtalek.NewTopic()
if err != nil {
panic(err)
}
myHandle, err := contact.MyTopic.Handle.MarshalText()
if err != nil {
panic(err)
}
contact.TheirHandle = &toPoll.Handle
theirTopic, err := toPoll.MarshalText()
if err != nil {
panic(err)
}
offer, err := json.Marshal(talekOffer{fromNick: myName, readHandle: string(myHandle), writeTopic: string(theirTopic)})
if err != nil {
panic(err)
}
return contact, offer
}
// AcceptOffer resolves a remote contact's stream.
func AcceptOffer(myName string, offer []byte, client *libtalek.Client) *TalekContact {
contact := new(TalekContact)
contact.MyNick = myName
contact.state = answerSent
var err error
contact.MyTopic, err = libtalek.NewTopic()
if err != nil {
panic(err)
}
// Deserialize
offerstruct := talekOffer{}
if err = json.Unmarshal(offer, &offerstruct); err != nil {
panic(err)
}
rendezvousTopic := libtalek.Topic{}
if err = rendezvousTopic.UnmarshalText([]byte(offerstruct.writeTopic)); err != nil {
panic(err)
}
contact.TheirHandle = &libtalek.Handle{}
if err = contact.TheirHandle.UnmarshalText([]byte(offerstruct.readHandle)); err != nil {
panic(err)
}
myHandle, err := contact.MyTopic.Handle.MarshalText()
if err != nil {
panic(err)
}
if err = client.Publish(&rendezvousTopic, myHandle); err != nil {
panic(err)
}
contact.TheirName = offerstruct.fromNick
return contact
}
func (t *TalekContact) onMessage(data []byte) bool {
if t.state == offerGenerated {
t.TheirHandle = &libtalek.Handle{}
if err := t.TheirHandle.UnmarshalText(data); err != nil {
panic(err)
}
t.state = confirmed
return true
} else if t.state == answerSent {
t.state = confirmed
return true
}
return false
}
// Channel connects ingoing / outgoing message streams for a running contact.
func (t *TalekContact) Channel(incoming *func([]byte)) chan<- []byte {
t.incoming = incoming
return t.outgoing
}
// Start begins polling a contact for messages, and watching for those to send
func (t *TalekContact) Start(c *libtalek.Client) {
curHandle := t.TheirHandle
incoming := c.Poll(curHandle)
t.outgoing = make(chan []byte)
t.done = make(chan byte, 1)
go (func() {
for {
select {
case msg := <-incoming:
if !t.onMessage(msg) && t.incoming != nil {
(*t.incoming)(msg)
} else if t.TheirHandle != curHandle {
c.Done(curHandle)
curHandle = t.TheirHandle
incoming = c.Poll(t.TheirHandle)
}
case msg := <-t.outgoing:
c.Publish(t.MyTopic, msg)
case <-t.done:
t.outgoing = nil
return
}
}
})()
}
// Done cleans up a running contact
func (t *TalekContact) Done() {
if t.outgoing != nil {
t.done <- 1
}
}