forked from signal-golang/textsecure
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sync.go
272 lines (233 loc) · 6.79 KB
/
sync.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
266
267
268
269
270
271
272
package textsecure
import (
"bytes"
"encoding/binary"
"fmt"
"regexp"
"github.com/golang/protobuf/proto"
"github.com/signal-golang/textsecure/config"
"github.com/signal-golang/textsecure/contacts"
signalservice "github.com/signal-golang/textsecure/protobuf"
"github.com/signal-golang/textsecure/unidentifiedAccess"
log "github.com/sirupsen/logrus"
)
// handleSyncMessage handles an incoming SyncMessage.
func handleSyncMessage(src string, srcUUID string, timestamp uint64, sm *signalservice.SyncMessage) error {
log.Debugf("[textsecure] SyncMessage recieved at %d", timestamp)
if sm.GetContacts() != nil {
log.Debugln("[textsecure] SyncMessage unhandled contacts")
return nil
} else if sm.GetGroups() != nil {
log.Debugln("[textsecure] SyncMessage groups")
return nil
} else if sm.GetRead() != nil {
log.Debugln("[textsecure] SyncMessage getRead")
return handleSyncRead(sm.GetRead())
} else if sm.GetViewed() != nil {
log.Debugln("[textsecure] SyncMessage unhandled getViewed")
return nil
} else if sm.GetViewOnceOpen() != nil {
log.Debugln("[textsecure] SyncMessage unhandled GetViewOnceOpen")
return nil
} else if sm.GetViewOnceOpen() != nil {
log.Debugln("[textsecure] SyncMessage unhandled GetBlockedList")
return nil
} else if sm.GetBlocked() != nil {
log.Debugln("[textsecure] SyncMessage blocked")
return nil
} else if sm.GetConfiguration() != nil {
log.Debugln("[textsecure] SyncMessage unahndled configuration")
return nil
} else if sm.GetSent() != nil {
log.Debugln("[textsecure] SyncMessage getSent")
return handleSyncSent(sm.GetSent(), timestamp)
} else if sm.GetStickerPackOperation() != nil {
log.Debugln("[textsecure] SyncMessage unhandled GetStickerPackOperation")
return nil
} else if sm.GetRequest() != nil {
log.Debugln("[textsecure] SyncMessage getRequest")
return handleSyncRequest(sm.GetRequest())
} else if sm.GetVerified() != nil {
log.Debugln("[textsecure] SyncMessage verified")
unidentifiedAccess, err := unidentifiedAccess.GetAccessForSync(config.ConfigFile.ProfileKey, config.ConfigFile.Certificate)
if err != nil {
return err
}
return sendVerifiedMessage(sm.GetVerified(), unidentifiedAccess)
} else if sm.GetPadding() != nil {
log.Debugln("[textsecure] SyncMessage padding")
return nil
} else if sm.GetFetchLatest() != nil {
log.Debugln("[textsecure] SyncMessage GetFetchLatest")
return nil
} else {
log.Errorf("[textsecure] SyncMessage contains no known sync types")
}
return nil
}
// handleSyncSent handles sync sent messages
func handleSyncSent(s *signalservice.SyncMessage_Sent, ts uint64) error {
dm := s.GetMessage()
dest := s.GetDestinationE164()
if dm == nil {
return fmt.Errorf("DataMessage was nil for SyncMessage_Sent")
}
flags, err := handleFlags(dest, dm)
if err != nil {
return err
}
atts, err := handleAttachments(dm)
if err != nil {
return err
}
gr, err := handleGroups(dest, dm)
if err != nil {
return err
}
grV2, err := handleGroupsV2(dest, dm)
if err != nil {
return err
}
cs, err := contacts.HandleContacts(dest, dm)
if err != nil {
return err
}
msg := &Message{
source: dest,
message: dm.GetBody(),
attachments: atts,
group: gr,
groupV2: grV2,
contact: cs,
flags: flags,
expireTimer: dm.GetExpireTimer(),
profileKey: dm.GetProfileKey(),
timestamp: *dm.Timestamp,
quote: dm.GetQuote(),
preview: dm.GetPreview(),
sticker: dm.GetSticker(),
reaction: dm.GetReaction(),
}
if client.SyncSentHandler != nil {
client.SyncSentHandler(msg, ts)
}
return nil
}
// handleSyncRequestMessage
func handleSyncRequest(request *signalservice.SyncMessage_Request) error {
if request.GetType() == signalservice.SyncMessage_Request_CONTACTS {
return sendContactUpdate()
} else if request.GetType() == signalservice.SyncMessage_Request_GROUPS {
return sendGroupUpdate()
} else {
log.Debugln("[textsecure] handle sync request unhandled type", request.GetType())
}
return nil
}
func isValidUUID(uuid string) bool {
r := regexp.MustCompile("^[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-4[a-fA-F0-9]{3}-[8|9|aA|bB][a-fA-F0-9]{3}-[a-fA-F0-9]{12}$")
return r.MatchString(uuid)
}
// sendContactUpdate
func sendContactUpdate() error {
log.Debugf("[textsecure] Sending contact SyncMessage")
lc, err := GetRegisteredContacts()
if err != nil {
return fmt.Errorf("could not get local contacts: %s", err)
}
var buf bytes.Buffer
for _, contact := range lc {
if isValidUUID(contact.UUID) {
cd := &signalservice.ContactDetails{
Number: &contact.Tel,
Uuid: &contact.UUID,
Name: &contact.Name,
Color: &contact.Color,
Verified: contact.Verified,
Blocked: &contact.Blocked,
ExpireTimer: &contact.ExpireTimer,
// TODO: handle avatars
}
b, err := proto.Marshal(cd)
if err != nil {
log.Errorf("[textsecure] Failed to marshal contact details")
continue
}
buf.Write(varint32(len(b)))
buf.Write(b)
}
}
a, err := uploadAttachment(&buf, "application/octet-stream")
if err != nil {
return err
}
sm := &signalservice.SyncMessage{
Contacts: &signalservice.SyncMessage_Contacts{
Blob: &signalservice.AttachmentPointer{
AttachmentIdentifier: &signalservice.AttachmentPointer_CdnId{
CdnId: a.id,
},
ContentType: &a.ct,
Key: a.keys[:],
Digest: a.digest[:],
Size: &a.size,
},
},
}
_, err = sendSyncMessage(sm, nil)
return err
}
// sendGroupUpdate
func sendGroupUpdate() error {
log.Debugf("Sending group SyncMessage")
var buf bytes.Buffer
for _, g := range groups {
gd := &signalservice.GroupDetails{
Id: g.ID,
Name: &g.Name,
MembersE164: g.Members,
// XXX add support for avatar
// XXX add support for active?
}
b, err := proto.Marshal(gd)
if err != nil {
log.Errorf("Failed to marshal group details")
continue
}
buf.Write(varint32(len(b)))
buf.Write(b)
}
a, err := uploadAttachment(&buf, "application/octet-stream")
if err != nil {
return err
}
sm := &signalservice.SyncMessage{
Groups: &signalservice.SyncMessage_Groups{
Blob: &signalservice.AttachmentPointer{
AttachmentIdentifier: &signalservice.AttachmentPointer_CdnId{
CdnId: a.id,
},
ContentType: &a.ct,
Key: a.keys[:],
Digest: a.digest[:],
Size: &a.size,
},
},
}
_, err = sendSyncMessage(sm, nil)
return err
}
func handleSyncRead(readMessages []*signalservice.SyncMessage_Read) error {
if client.SyncReadHandler != nil {
for _, s := range readMessages {
client.SyncReadHandler(s.GetSenderE164(), s.GetTimestamp())
}
}
return nil
}
// Encodes a 32bit base 128 variable-length integer and returns the bytes
func varint32(value int) []byte {
buf := make([]byte, binary.MaxVarintLen32)
n := binary.PutUvarint(buf, uint64(value))
return buf[:n]
}