Skip to content
This repository has been archived by the owner on Jul 22, 2024. It is now read-only.

Commit

Permalink
Merge pull request #59 from signal-golang/profile_fixes
Browse files Browse the repository at this point in the history
Profile fixes
  • Loading branch information
nanu-c committed Sep 2, 2022
2 parents 1e49055 + 16b8d0f commit a0891d4
Show file tree
Hide file tree
Showing 15 changed files with 105 additions and 59 deletions.
9 changes: 4 additions & 5 deletions attachments.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"strconv"

Expand Down Expand Up @@ -70,7 +69,7 @@ func uploadAttachment(r io.Reader, ct string) (*att, error) {
keys := make([]byte, 64)
randBytes(keys)

b, err := ioutil.ReadAll(r)
b, err := io.ReadAll(r)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -101,7 +100,7 @@ func uploadVoiceNote(r io.Reader, ct string) (*att, error) {
keys := make([]byte, 64)
randBytes(keys)

b, err := ioutil.ReadAll(r)
b, err := io.ReadAll(r)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -141,7 +140,7 @@ func handleSingleAttachment(a *textsecure.AttachmentPointer) (*Attachment, error
}
defer r.Close()

b, err := ioutil.ReadAll(r)
b, err := io.ReadAll(r)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -172,7 +171,7 @@ func handleProfileAvatar(profileAvatar *signalservice.ContactDetails_Avatar, key
}
defer r.Close()

b, err := ioutil.ReadAll(r)
b, err := io.ReadAll(r)
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/textsecure/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ func GroupsHandler(w http.ResponseWriter, r *http.Request) {

filepath.Walk(".storage/groups", func(path string, info os.FileInfo, e error) error {
if info.Mode().IsRegular() {
b, err := ioutil.ReadFile(path)
b, err := os.ReadFile(path)
if err != nil {
return err
}
Expand Down Expand Up @@ -414,7 +414,7 @@ func JSONHandler(w http.ResponseWriter, r *http.Request) {
if messageField == "" {
messageField = "message"
}
body, err := ioutil.ReadAll(r.Body)
body, err := io.ReadAll(r.Body)
if err != nil {
log.Error("Error: ", err.Error())
w.WriteHeader(http.StatusInternalServerError)
Expand Down
3 changes: 2 additions & 1 deletion config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package textsecure

import (
"io/ioutil"
"os"

"github.com/go-yaml/yaml"
"github.com/signal-golang/textsecure/config"
Expand Down Expand Up @@ -36,7 +37,7 @@ func checkUUID(cfg *config.Config) *config.Config {

// ReadConfig reads a YAML config file
func ReadConfig(fileName string) (*config.Config, error) {
b, err := ioutil.ReadFile(fileName)
b, err := os.ReadFile(fileName)
if err != nil {
return nil, err
}
Expand Down
15 changes: 11 additions & 4 deletions contacts/contacts.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
package contacts

import (
"fmt"
"io/ioutil"
"os"

signalservice "github.com/signal-golang/textsecure/protobuf"
log "github.com/sirupsen/logrus"
Expand Down Expand Up @@ -67,7 +67,7 @@ var filePath string
// ReadContacts loads the contacts yaml file and pareses it
func ReadContacts(fileName string) ([]Contact, error) {
log.Debug("[textsecure] read contacts from ", fileName)
b, err := ioutil.ReadFile(fileName)
b, err := os.ReadFile(fileName)
filePath = fileName
contactsYaml := &yamlContacts{}
if err != nil {
Expand Down Expand Up @@ -127,7 +127,7 @@ func updateContact(c *signalservice.ContactDetails) error {
// r = att.R
// buf.ReadFrom(r)
// }
// avatar, _ := ioutil.ReadAll(buf)
// avatar, _ := io.ReadAll(buf)

Contacts[c.GetUuid()] = Contact{
Tel: c.GetNumber(),
Expand Down Expand Up @@ -167,7 +167,14 @@ func UpdateProfileKey(src string, profileKey []byte) error {
Contacts[src] = contact
return WriteContactsToPath()
}
return fmt.Errorf("Contact to update not found %s", src)
// create new contact
Contacts[src] = Contact{
UUID: src,
Name: src,
ProfileKey: profileKey,
}
return WriteContactsToPath()

}

func GetContact(uuid string) Contact {
Expand Down
2 changes: 1 addition & 1 deletion groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func saveGroup(hexid string) error {
// loadGroup loads a group's state from a file.
func loadGroup(path string) error {
_, hexid := filepath.Split(path)
b, err := ioutil.ReadFile(path)
b, err := os.ReadFile(path)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions groupsv2/credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package groupsv2
import (
"encoding/json"
"fmt"
"io/ioutil"
"io"
"time"

signalservice "github.com/signal-golang/textsecure/protobuf"
Expand Down Expand Up @@ -88,7 +88,7 @@ func GetGroupExternalCredential(credential *GroupCredential) (*signalservice.Gro
if resp.IsError() {
return nil, resp
}
data, err := ioutil.ReadAll(resp.Body)
data, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion groupsv2/groupsv2.go
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ func saveGroupV2(hexid string) error {

// loadGroup loads a group's state from a file.
func loadGroupV2(hexid string) (*GroupV2, error) {
b, err := ioutil.ReadFile(idToPath(hexid))
b, err := os.ReadFile(idToPath(hexid))
if err != nil {
return nil, err
}
Expand Down
3 changes: 1 addition & 2 deletions handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package textsecure

import (
"github.com/golang/protobuf/proto"
"github.com/signal-golang/textsecure/config"
"github.com/signal-golang/textsecure/groupsv2"
signalservice "github.com/signal-golang/textsecure/protobuf"
log "github.com/sirupsen/logrus"
Expand All @@ -20,7 +19,7 @@ func handleMessage(srcE164 string, srcUUID string, timestamp uint64, b []byte) e

if dm := content.GetDataMessage(); dm != nil {
return handleDataMessage(srcE164, srcUUID, timestamp, dm)
} else if sm := content.GetSyncMessage(); sm != nil && config.ConfigFile.Tel == srcE164 {
} else if sm := content.GetSyncMessage(); sm != nil {
return handleSyncMessage(srcE164, srcUUID, timestamp, sm)
} else if cm := content.GetCallMessage(); cm != nil {
return handleCallMessage(srcE164, srcUUID, timestamp, cm)
Expand Down
18 changes: 15 additions & 3 deletions profiles/avatars.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,21 @@ func GetRemoteAvatar(avatarURL string) (io.ReadCloser, error) {

return resp.Body, nil
}

func getLocalAvatar(uuid string) (io.ReadCloser, error) {
func GetLocalAvatarPath(uuid string) (string, error) {
log.Debugln("[textsecure] get local avatar for ", uuid)
if uuid == "" {
return "", fmt.Errorf("empty uuid")
}
if avatarsPath == "" {
err := setupAvatarsPath()
if err != nil {
return "", err
}
}
avatarFile := filepath.Join(avatarsPath, uuid)
return avatarFile, nil
}
func GetLocalAvatar(uuid string) (io.ReadCloser, error) {
log.Debugln("[textsecure] get local avatar for ", uuid)
if uuid == "" {
return nil, fmt.Errorf("empty uuid")
Expand Down Expand Up @@ -101,7 +114,6 @@ func setupAvatarsPath() error {
if err := os.MkdirAll(avatarsPath, 0700); err != nil {
return err
}
log.Debugln("[textsecure] setup avatars path ", avatarsPath)
return nil
}

Expand Down
15 changes: 13 additions & 2 deletions profiles/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,10 @@ func GetProfile(UUID string, profileKey []byte) (*Profile, error) {

}
func GetProfileAndCredential(UUID string, profileKey []byte) (*Profile, error) {
if len(profileKey) == 0 {
return nil, errors.New("profileKey is empty")
}

log.Infoln("[textsecure] GetProfileAndCredential for " + UUID)
uuid, err := uuidUtil.FromString(UUID)
if err != nil {
Expand Down Expand Up @@ -229,7 +233,6 @@ func GetProfileAndCredential(UUID string, profileKey []byte) (*Profile, error) {
if err != nil {
return nil, err
}
// fmt.Printf("%s\n", string(bytes))
err = json.Unmarshal(bytes, profile)
if err != nil {
log.Debugln("[textsecure] GetProfileAndCredential json unmarshall", err)
Expand All @@ -241,6 +244,11 @@ func GetProfileAndCredential(UUID string, profileKey []byte) (*Profile, error) {
return nil, err
}
}
log.Debugf("[textsecure] GetProfileAndCredential profile %+v", profile)
if profile.Credential == nil {
log.Debugf("[textsecure] GetProfileAndCredential profile %+v", profile)
return nil, errors.New("profile credential is empty")
}
response := zkgroup.ProfileKeyCredentialResponse(profile.Credential)
if err != nil {
log.Debugln("[textsecure] GetProfileAndCredential", err)
Expand All @@ -261,6 +269,9 @@ func GetProfileAndCredential(UUID string, profileKey []byte) (*Profile, error) {

}
func decryptProfile(profileKey []byte, profile *Profile) error {
if len(profileKey) == 0 {
return fmt.Errorf("[textsecure] decryptProfile: no profile key")
}
log.Println("[textsecure] decryptProfile")
if profile.Name != "" {
name, err := decryptString(profileKey, profile.Name)
Expand Down Expand Up @@ -361,7 +372,7 @@ func GetProfileE164(tel string) (contacts.Contact, error) {

// GetProfileUUID get a profile by a phone number
func GetProfileUUID(uuid string) (*Profile, error) {
log.Debug("[textsecure] GetProfileUUID", uuid)
log.Debugln("[textsecure] GetProfileUUID", uuid)
c := contacts.Contacts[uuid]
profile := &Profile{}
var err error
Expand Down
4 changes: 2 additions & 2 deletions rootCa/rootca.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package rootCa

import (
"crypto/x509"
"io/ioutil"
"os"

"github.com/signal-golang/textsecure/utils"

Expand Down Expand Up @@ -70,7 +70,7 @@ var DirectoryCA *x509.CertPool
func SetupCA(rootca string) {
pem := []byte(rootPEM)
if rootca != "" && utils.Exists(rootca) {
b, err := ioutil.ReadFile(rootca)
b, err := os.ReadFile(rootca)
if err != nil {
log.Error(err)
return
Expand Down
10 changes: 8 additions & 2 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"io"
"mime/quotedprintable"
"regexp"
"strconv"
Expand Down Expand Up @@ -377,6 +377,12 @@ func GetProfileByUUID(uuid string) (*profiles.Profile, error) {
}
return profile, nil
}
func GetAvatar(uuid string) (io.ReadCloser, error) {
return profiles.GetLocalAvatar(uuid)
}
func GetAvatarPath(uuid string) (string, error) {
return profiles.GetLocalAvatarPath(uuid)
}

func GetProfileAndCredential(uuid string, profileKey []byte) (*profiles.Profile, error) {
if uuid == "" || len(profileKey) == 0 {
Expand Down Expand Up @@ -678,7 +684,7 @@ func GetRegisteredContacts() ([]contacts.Contact, error) {
UUID := idToHexUUID(responseData[ind*uuidlength : (ind+1)*uuidlength])
index := findIndexByE147(phone, localContacts)
if strings.Count(localContacts[index].Name, "=") > 2 {
decodedName, err := ioutil.ReadAll(quotedprintable.NewReader(strings.NewReader(localContacts[index].Name)))
decodedName, err := io.ReadAll(quotedprintable.NewReader(strings.NewReader(localContacts[index].Name)))
if err != nil {
log.Debug("[textsecure] GetRegisteredContacts update name from quoted printable error:", err)
} else {
Expand Down
4 changes: 2 additions & 2 deletions store.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func newStore(password, path string) (*store, error) {
return nil, err
}
} else {
salt, err = ioutil.ReadFile(saltFile)
salt, err = os.ReadFile(saltFile)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -161,7 +161,7 @@ func (s *store) decrypt(ciphertext []byte) ([]byte, error) {
}

func (s *store) readFile(path string) ([]byte, error) {
b, err := ioutil.ReadFile(path)
b, err := os.ReadFile(path)
if err != nil {
return nil, err
}
Expand Down
Loading

0 comments on commit a0891d4

Please sign in to comment.