/
contacts.go
164 lines (142 loc) · 3.75 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
163
164
// Copyright (c) 2014 Canonical Ltd.
// Licensed under the GPLv3, see the COPYING file for details.
package contacts
import (
"fmt"
"io/ioutil"
signalservice "github.com/signal-golang/textsecure/protobuf"
log "github.com/sirupsen/logrus"
"gopkg.in/yaml.v2"
)
// Contact contains information about a contact.
type Contact struct {
UUID string
Tel string
ProfileKey []byte
ProfileKeyCredential []byte
IdentityKey []byte
Name string
Username string
Avatar []byte
Color string
Blocked bool
Verified *signalservice.Verified
ExpireTimer uint32
InboxPosition uint32
Archived bool
Certificate []byte
}
func (c *Contact) GetProfileKey() []byte {
return c.ProfileKey
}
type yamlContacts struct {
Contacts []Contact
}
var (
contactsFile string
Contacts = map[string]Contact{}
)
// LoadContacts reads a YAML contacts file
func LoadContacts(contactsYaml *yamlContacts) {
for _, c := range contactsYaml.Contacts {
if c.UUID != "" && c.UUID != "0" && (c.UUID[0] != 0 || c.UUID[len(c.UUID)-1] != 0) {
Contacts[c.UUID] = c
} else {
Contacts[c.Tel] = c
}
}
}
var filePath string
// ReadContacts loads the contacts yaml file and pareses it
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, contacts []Contact) error {
c := &yamlContacts{contacts}
b, err := yaml.Marshal(c)
if err != nil {
return err
}
return ioutil.WriteFile(filename, b, 0600)
}
// WriteContactsToPath saves a list of contacts to a file at the standard location
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
}
func updateContact(c *signalservice.ContactDetails) error {
log.Debugln("[textsecure] updateContact ", c.GetUuid())
// var r io.Reader
// av := c.GetAvatar()
// buf := new(bytes.Buffer)
// if av != nil {
// att, err := handleProfileAvatar(av, c.GetProfileKey())
// if err != nil {
// return err
// }
// r = att.R
// buf.ReadFrom(r)
// }
// avatar, _ := ioutil.ReadAll(buf)
Contacts[c.GetUuid()] = Contact{
Tel: c.GetNumber(),
UUID: c.GetUuid(),
Name: c.GetName(),
// Avatar: avatar,
Avatar: nil,
Color: c.GetColor(),
Verified: c.GetVerified(),
ProfileKey: c.GetProfileKey(),
Blocked: c.GetBlocked(),
ExpireTimer: c.GetExpireTimer(),
InboxPosition: c.GetInboxPosition(),
Archived: c.GetArchived(),
}
// log.Debugln("[textsecure] avatar", c.GetAvatar())
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())
}
return nil, nil
}
func UpdateProfileKey(src string, profileKey []byte) error {
log.Println("[textsecure] update profile key", src)
if contact, ok := Contacts[src]; ok {
contact.ProfileKey = profileKey
Contacts[src] = contact
return WriteContactsToPath()
}
return fmt.Errorf("Contact to update not found")
}
func GetContact(uuid string) Contact {
return Contacts[uuid]
}