This repository has been archived by the owner on Jul 22, 2024. It is now read-only.
forked from signal-golang/textsecure
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcontacts.go
162 lines (143 loc) · 3.44 KB
/
contacts.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
// Copyright (c) 2014 Canonical Ltd.
// Licensed under the GPLv3, see the COPYING file for details.
package textsecure
import (
"io/ioutil"
"github.com/nanu-c/textsecure/protobuf"
log "github.com/sirupsen/logrus"
"gopkg.in/yaml.v2"
)
// Contact contains information about a contact.
type Contact struct {
Tel string
Uuid string
Name string
Color string
Avatar []byte
Blocked bool
ExpireTimer uint32
}
type yamlContacts struct {
Contacts []Contact
}
var (
contactsFile string
contacts = map[string]Contact{}
)
// ReadContacts reads a YAML contacts file
func loadContacts(contactsYaml *yamlContacts) {
for _, c := range contactsYaml.Contacts {
contacts[c.Tel] = c
}
}
var filePath string
func ReadContacts(fileName string) ([]Contact, error) {
b, err := ioutil.ReadFile(fileName)
filePath = fileName
if err != nil {
return nil, err
}
contactsYaml := &yamlContacts{}
err = yaml.Unmarshal(b, contactsYaml)
if err != nil {
return nil, err
}
loadContacts(contactsYaml)
return contactsYaml.Contacts, nil
}
// WriteContacts saves a list of contacts to a file
func WriteContacts(filename string, contacts2 []Contact) error {
c := &yamlContacts{contacts2}
// func WriteContacts(filename string) error {
b, err := yaml.Marshal(c)
if err != nil {
return err
}
return ioutil.WriteFile(filename, b, 0600)
}
func WriteContactsToPath() error {
c := contactsToYaml()
b, err := yaml.Marshal(c)
if err != nil {
return err
}
return ioutil.WriteFile(filePath, b, 0600)
}
func contactsToYaml() *yamlContacts {
c := &yamlContacts{}
for _, co := range contacts {
c.Contacts = append(c.Contacts, co)
}
return c
}
// type AvatarDetail struct {
// Length
// }
func updateContact(c *signalservice.ContactDetails) error {
log.Debugln("[textsecure] updateContact ", c.GetName())
// var r io.Reader
// av := c.GetAvatar()
// buf := new(bytes.Buffer)
// if av != nil {
// att, err := handleSingleAttachment(av)
// if err != nil {
// return err
// }
// r = att.R
// buf.ReadFrom(r)
// }
contacts[c.GetNumber()] = Contact{
Tel: c.GetNumber(),
Uuid: c.GetUuid(),
Name: c.GetName(),
Color: c.GetColor(),
Avatar: []byte(""),
Blocked: c.GetBlocked(),
ExpireTimer: c.GetExpireTimer(),
}
return WriteContactsToPath()
}
func handleContacts(src string, dm *signalservice.DataMessage) ([]*signalservice.DataMessage_Contact, error) {
cs := dm.GetContact()
if cs == nil {
return nil, nil
}
for _, c := range cs {
log.Debugln("[textsecure] handle Contact", c.GetName())
}
// switch c.GetType() {
// case signalservice.GroupContext_UPDATE:
// if err := updateGroup(gr); err != nil {
// return nil, err
// }
// groups[hexid].Flags = GroupUpdateFlag
// case signalservice.GroupContext_DELIVER:
// if _, ok := groups[hexid]; !ok {
// g, _ := newPartlyGroup(gr.GetId())
// RequestGroupInfo(g)
// setupGroups()
// return nil, UnknownGroupIDError{hexid}
// }
// groups[hexid].Flags = 0
// case signalservice.GroupContext_QUIT:
// if err := quitGroup(src, hexid); err != nil {
// return nil, err
// }
// groups[hexid].Flags = GroupLeaveFlag
// }
return nil, nil
}
func RequestContactInfo() error {
var t signalservice.SyncMessage_Request_Type
t = 1
omsg := &signalservice.SyncMessage{
Request: &signalservice.SyncMessage_Request{
Type: &t,
},
}
_, err := sendSyncMessage(omsg)
if err != nil {
return err
}
return nil
}