-
Notifications
You must be signed in to change notification settings - Fork 26
/
sync.go
188 lines (153 loc) · 3.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
package textsecure
import (
"bytes"
"encoding/binary"
"fmt"
log "github.com/Sirupsen/logrus"
"github.com/golang/protobuf/proto"
"github.com/janimo/textsecure/protobuf"
)
// handleSyncMessage handles an incoming SyncMessage.
func handleSyncMessage(src string, timestamp uint64, sm *textsecure.SyncMessage) error {
log.Debugf("SyncMessage recieved at %d", timestamp)
if sm.GetSent() != nil {
return handleSyncSent(sm.GetSent(), timestamp)
} else if sm.GetRequest() != nil {
return handleSyncRequest(sm.GetRequest())
} else if sm.GetRead() != nil {
return handleSyncRead(sm.GetRead())
} else {
log.Errorf("SyncMessage contains no known sync types")
}
return nil
}
// handleSyncSent handles sync sent messages
func handleSyncSent(s *textsecure.SyncMessage_Sent, ts uint64) error {
dm := s.GetMessage()
dest := s.GetDestination()
timestamp := s.GetTimestamp()
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
}
msg := &Message{
source: dest,
message: dm.GetBody(),
attachments: atts,
group: gr,
timestamp: timestamp,
flags: flags,
}
if client.SyncSentHandler != nil {
client.SyncSentHandler(msg, ts)
}
return nil
}
// handleSyncRequestMessage
func handleSyncRequest(request *textsecure.SyncMessage_Request) error {
if request.GetType() == textsecure.SyncMessage_Request_CONTACTS {
return sendContactUpdate()
} else if request.GetType() == textsecure.SyncMessage_Request_GROUPS {
return sendGroupUpdate()
}
return nil
}
// sendContactUpdate
func sendContactUpdate() error {
log.Debugf("Sending contact SyncMessage")
lc, err := GetRegisteredContacts()
if err != nil {
return fmt.Errorf("could not get local contacts: %s", err)
}
var buf bytes.Buffer
for _, c := range lc {
cd := &textsecure.ContactDetails{
Number: &c.Tel,
Name: &c.Name,
// TODO: handle avatars
}
b, err := proto.Marshal(cd)
if err != nil {
log.Errorf("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 := &textsecure.SyncMessage{
Contacts: &textsecure.SyncMessage_Contacts{
Blob: &textsecure.AttachmentPointer{
Id: &a.id,
ContentType: &a.ct,
Key: a.keys,
},
},
}
_, err = sendSyncMessage(sm)
return err
}
// sendGroupUpdate
func sendGroupUpdate() error {
log.Debugf("Sending group SyncMessage")
var buf bytes.Buffer
for _, g := range groups {
gd := &textsecure.GroupDetails{
Id: g.ID,
Name: &g.Name,
Members: 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 := &textsecure.SyncMessage{
Groups: &textsecure.SyncMessage_Groups{
Blob: &textsecure.AttachmentPointer{
Id: &a.id,
ContentType: &a.ct,
Key: a.keys,
},
},
}
_, err = sendSyncMessage(sm)
return err
}
func handleSyncRead(readMessages []*textsecure.SyncMessage_Read) error {
if client.SyncReadHandler != nil {
for _, s := range readMessages {
client.SyncReadHandler(s.GetSender(), 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]
}