-
Notifications
You must be signed in to change notification settings - Fork 26
/
groups.go
341 lines (298 loc) · 7.18 KB
/
groups.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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
// Copyright (c) 2014 Canonical Ltd.
// Licensed under the GPLv3, see the COPYING file for details.
package textsecure
import (
"encoding/hex"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"strings"
"github.com/janimo/textsecure/protobuf"
"gopkg.in/yaml.v2"
)
// Group holds group metadata.
type Group struct {
ID []byte
Hexid string
Flags uint32
Name string
Members []string
Avatar io.Reader `yaml:"-"`
}
var (
groupDir string
groups = map[string]*Group{}
)
// idToHex returns the hex representation of the group id byte-slice
// to be used as both keys in the map and for naming the files.
func idToHex(id []byte) string {
return hex.EncodeToString(id)
}
// idToPath returns the path of the file for storing a group's state
func idToPath(hexid string) string {
return filepath.Join(groupDir, hexid)
}
// FIXME: for now using unencrypted YAML files for group state,
// should be definitely encrypted and maybe another format.
// saveGroup stores a group's state in a file.
func saveGroup(hexid string) error {
b, err := yaml.Marshal(groups[hexid])
if err != nil {
return err
}
return ioutil.WriteFile(idToPath(hexid), b, 0600)
}
// loadGroup loads a group's state from a file.
func loadGroup(path string) error {
_, hexid := filepath.Split(path)
b, err := ioutil.ReadFile(path)
if err != nil {
return err
}
group := &Group{}
err = yaml.Unmarshal(b, group)
if err != nil {
return err
}
groups[hexid] = group
return nil
}
// setupGroups reads all groups' state from storage.
func setupGroups() error {
groupDir = filepath.Join(config.StorageDir, "groups")
if err := os.MkdirAll(groupDir, 0700); err != nil {
return err
}
filepath.Walk(groupDir, func(path string, fi os.FileInfo, err error) error {
if !fi.IsDir() {
if !strings.Contains(path, "avatar") {
loadGroup(path)
}
}
return nil
})
return nil
}
// removeMember removes a given number from a list.
func removeMember(tel string, members []string) []string {
for i, m := range members {
if m == tel {
members = append(members[:i], members[i+1:]...)
break
}
}
return members
}
// updateGroup updates a group's state based on an incoming message.
func updateGroup(gr *textsecure.GroupContext) error {
hexid := idToHex(gr.GetId())
var r io.Reader
if av := gr.GetAvatar(); av != nil {
att, err := handleSingleAttachment(av)
if err != nil {
return err
}
r = att.R
}
groups[hexid] = &Group{
ID: gr.GetId(),
Hexid: hexid,
Name: gr.GetName(),
Members: gr.GetMembers(),
Avatar: r,
}
return saveGroup(hexid)
}
// UnknownGroupIDError is returned when an unknown group id is encountered
type UnknownGroupIDError struct {
id string
}
func (err UnknownGroupIDError) Error() string {
return fmt.Sprintf("unknown group ID %s", err.id)
}
// quitGroup removes a quitting member from the local group state.
func quitGroup(src string, hexid string) error {
gr, ok := groups[hexid]
if !ok {
return UnknownGroupIDError{hexid}
}
gr.Members = removeMember(src, gr.Members)
return saveGroup(hexid)
}
// GroupUpdateFlag signals that this message updates the group membership or name.
var GroupUpdateFlag uint32 = 1
// GroupLeavelag signals that this message is a group leave message
var GroupLeaveFlag uint32 = 2
// handleGroups is the main entry point for handling the group metadata on messages.
func handleGroups(src string, dm *textsecure.DataMessage) (*Group, error) {
gr := dm.GetGroup()
if gr == nil {
return nil, nil
}
hexid := idToHex(gr.GetId())
switch gr.GetType() {
case textsecure.GroupContext_UPDATE:
if err := updateGroup(gr); err != nil {
return nil, err
}
groups[hexid].Flags = GroupUpdateFlag
case textsecure.GroupContext_DELIVER:
if _, ok := groups[hexid]; !ok {
return nil, UnknownGroupIDError{hexid}
}
groups[hexid].Flags = 0
case textsecure.GroupContext_QUIT:
if err := quitGroup(src, hexid); err != nil {
return nil, err
}
groups[hexid].Flags = GroupLeaveFlag
}
return groups[hexid], nil
}
type groupMessage struct {
id []byte
name string
members []string
typ textsecure.GroupContext_Type
}
func sendGroupHelper(hexid string, msg string, a *att) (uint64, error) {
var ts uint64
var err error
g, ok := groups[hexid]
if !ok {
return 0, UnknownGroupIDError{hexid}
}
for _, m := range g.Members {
if m != config.Tel {
omsg := &outgoingMessage{
tel: m,
msg: msg,
attachment: a,
group: &groupMessage{
id: g.ID,
typ: textsecure.GroupContext_DELIVER,
},
}
ts, err = sendMessage(omsg)
if err != nil {
return 0, err
}
}
}
return ts, nil
}
// SendGroupMessage sends a text message to a given group.
func SendGroupMessage(hexid string, msg string) (uint64, error) {
return sendGroupHelper(hexid, msg, nil)
}
// SendGroupAttachment sends an attachment to a given group.
func SendGroupAttachment(hexid string, msg string, r io.Reader) (uint64, error) {
ct, r := MIMETypeFromReader(r)
a, err := uploadAttachment(r, ct)
if err != nil {
return 0, err
}
return sendGroupHelper(hexid, msg, a)
}
func newGroupID() []byte {
id := make([]byte, 16)
randBytes(id)
return id
}
func newGroup(name string, members []string) (*Group, error) {
id := newGroupID()
hexid := idToHex(id)
groups[hexid] = &Group{
ID: id,
Hexid: hexid,
Name: name,
Members: append(members, config.Tel),
}
err := saveGroup(hexid)
if err != nil {
return nil, err
}
return groups[hexid], nil
}
func changeGroup(hexid, name string, members []string) (*Group, error) {
g, ok := groups[hexid]
if !ok {
return nil, UnknownGroupIDError{hexid}
}
g.Name = name
g.Members = append(members, config.Tel)
saveGroup(hexid)
return g, nil
}
func sendUpdate(g *Group) error {
for _, m := range g.Members {
if m != config.Tel {
omsg := &outgoingMessage{
tel: m,
group: &groupMessage{
id: g.ID,
name: g.Name,
members: g.Members,
typ: textsecure.GroupContext_UPDATE,
},
}
_, err := sendMessage(omsg)
if err != nil {
return err
}
}
}
return nil
}
// NewGroup creates a group and notifies its members.
// Our phone number is automatically added to members.
func NewGroup(name string, members []string) (*Group, error) {
g, err := newGroup(name, members)
if err != nil {
return nil, err
}
return g, sendUpdate(g)
}
// UpdateGroup updates the group name and/or membership.
// Our phone number is automatically added to members.
func UpdateGroup(hexid, name string, members []string) (*Group, error) {
g, err := changeGroup(hexid, name, members)
if err != nil {
return nil, err
}
return g, sendUpdate(g)
}
func removeGroup(id []byte) error {
hexid := idToHex(id)
err := os.Remove(idToPath(hexid))
if err != nil {
return err
}
return nil
}
// LeaveGroup sends a group quit message to the other members of the given group.
func LeaveGroup(hexid string) error {
g, ok := groups[hexid]
if !ok {
return UnknownGroupIDError{hexid}
}
for _, m := range g.Members {
if m != config.Tel {
omsg := &outgoingMessage{
tel: m,
group: &groupMessage{
id: g.ID,
typ: textsecure.GroupContext_QUIT,
},
}
_, err := sendMessage(omsg)
if err != nil {
return err
}
}
}
removeGroup(g.ID)
return nil
}