/
message.go
265 lines (244 loc) · 6.24 KB
/
message.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
package libsignal
import (
"encoding/base64"
"encoding/json"
"fmt"
"io"
"time"
"github.com/golang/protobuf/proto"
"github.com/jckimble/golibsignal/axolotl"
"github.com/jckimble/golibsignal/signalservice"
"log"
)
type messageHandler struct {
f func(*Message)
}
func (mh messageHandler) Message(m *Message) {
mh.f(m)
}
func MessageHandlerFunc(f func(*Message)) MessageHandler {
return messageHandler{f}
}
// MessageHandler is an interface for recieving signal messages
type MessageHandler interface {
Message(*Message)
}
// Message holds incoming message information
type Message struct {
source string
message string
attachments []*Attachment
group *Group
timestamp uint64
flags uint32
contact *Contact
}
// Contact returns Contact from incoming Message
func (m Message) Contact() *Contact {
return m.contact
}
// Source returns Message Source Phone Number
func (m Message) Source() string {
return m.source
}
// Source returns Message Text
func (m Message) Message() string {
return m.message
}
// Attachments returns Message Attachments
func (m Message) Attachments() []*Attachment {
return m.attachments
}
// Group returns Group message was sent to, nil if direct
func (m Message) Group() *Group {
return m.group
}
// Timestamp returns Message Timestamp
func (m Message) Timestamp() uint64 {
return m.timestamp
}
// Flags returns Message Flags
func (m Message) Flags() uint32 {
return m.flags
}
type sendResponse struct {
NeedsSync bool `json:"needsSync"`
Timestamp uint64
}
type mismatchedResponse struct {
MissingDevices []uint32 `json:"missingDevices"`
ExtraDevices []uint32 `json:"extraDevices"`
}
type staleResponse struct {
StaleDevices []uint32 `json:"staleDevices"`
}
type jsonMessage struct {
Type int32 `json:"type"`
DestDeviceID uint32 `json:"destinationDeviceId"`
DestRegistrationID uint32 `json:"destinationRegistrationId"`
Content string `json:"content"`
Relay string `json:"relay,omitempty"`
}
type groupMessage struct {
id []byte
name string
members []string
typ signalservice.GroupContext_Type
}
type outgoingMessage struct {
tel string
msg string
group *groupMessage
attachments []*signalservice.AttachmentPointer
flags uint32
}
// SendMessage sends the given text message to the given number
func (s *Signal) SendMessage(tel, msg string) (uint64, error) {
omsg := &outgoingMessage{
tel: tel,
msg: msg,
}
return s.sendMessage(omsg)
}
func (s *Signal) sendMessage(msg *outgoingMessage) (uint64, error) {
dm := &signalservice.DataMessage{}
if msg.msg != "" {
dm.Body = &msg.msg
}
if msg.attachments != nil {
dm.Attachments = msg.attachments
}
if msg.group != nil {
dm.Group = &signalservice.GroupContext{
Id: msg.group.id,
Type: &msg.group.typ,
Name: &msg.group.name,
Members: msg.group.members,
}
}
dm.Flags = &msg.flags
content := &signalservice.Content{
DataMessage: dm,
}
b, err := proto.Marshal(content)
if err != nil {
return 0, err
}
resp, err := s.buildAndSendMessage(msg.tel, padMessage(b), false)
if err != nil {
return 0, err
}
if resp.NeedsSync {
sm := &signalservice.SyncMessage{
Sent: &signalservice.SyncMessage_Sent{
Destination: &msg.tel,
Timestamp: &resp.Timestamp,
Message: dm,
},
}
s.sendSyncMessage(sm)
}
return resp.Timestamp, err
}
func (s *Signal) buildMessage(tel string, paddedMessage []byte, isSync bool) ([]jsonMessage, error) {
recid := tel[1:]
messages := []jsonMessage{}
contact, err := s.ContactStore.Get(tel)
if err != nil {
log.Printf("Unknown Contact: %s %s", tel, err)
contact = &Contact{
Tel: tel,
Devices: []uint32{1},
}
}
for _, devid := range contact.Devices {
if !s.SessionStore.ContainsSession(recid, devid) {
pkb, err := s.makePreKeyBundle(tel, devid)
if err != nil {
return nil, err
}
sb := axolotl.NewSessionBuilder(s.IdentityStore, s.PreKeyStore, s.SignedPreKeyStore, s.SessionStore, recid, pkb.DeviceID)
if err := sb.BuildSenderSession(pkb); err != nil {
return nil, err
}
}
sc := axolotl.NewSessionCipher(s.IdentityStore, s.PreKeyStore, s.SignedPreKeyStore, s.SessionStore, recid, devid)
encryptedMessage, messageType, err := sc.SessionEncryptMessage(paddedMessage)
if err != nil {
return nil, err
}
rrID, err := sc.GetRemoteRegistrationID()
if err != nil {
return nil, err
}
jmsg := jsonMessage{
Type: messageType,
DestDeviceID: devid,
DestRegistrationID: rrID,
Content: base64.StdEncoding.EncodeToString(encryptedMessage),
}
messages = append(messages, jmsg)
}
return messages, nil
}
func (s *Signal) buildAndSendMessage(tel string, paddedMessage []byte, isSync bool) (*sendResponse, error) {
bm, err := s.buildMessage(tel, paddedMessage, isSync)
if err != nil {
return nil, err
}
m := make(map[string]interface{})
m["messages"] = bm
m["timestamp"] = uint64(time.Now().UnixNano() / 1000000)
m["destination"] = tel
r, w := io.Pipe()
go func() {
defer w.Close()
json.NewEncoder(w).Encode(m)
}()
var rsp sendResponse
var stale staleResponse
var mismatched mismatchedResponse
if err := s.serverRequest("PUT", fmt.Sprintf("/v1/messages/%s", tel), r, "application/json", map[int]interface{}{
200: &rsp,
410: &stale,
409: &mismatched,
}); err != nil {
return nil, err
}
if stale.StaleDevices != nil {
for _, id := range stale.StaleDevices {
s.SessionStore.DeleteSession(tel[1:], id)
}
return s.buildAndSendMessage(tel, paddedMessage, isSync)
}
if mismatched.MissingDevices != nil || mismatched.ExtraDevices != nil {
contact, err := s.ContactStore.Get(tel)
if err != nil {
log.Printf("Unknown Contact: %s %s", tel, err)
contact = &Contact{
Tel: tel,
Devices: []uint32{1},
}
}
devs := []uint32{}
for _, id := range contact.Devices {
in := true
for _, eid := range mismatched.ExtraDevices {
if id == eid {
in = false
break
}
}
if in {
devs = append(devs, id)
}
}
contact.Devices = append(devs, mismatched.MissingDevices...)
if err := s.ContactStore.Save(contact); err != nil {
return nil, err
}
return s.buildAndSendMessage(tel, paddedMessage, isSync)
}
rsp.Timestamp = m["timestamp"].(uint64)
return &rsp, nil
}