From 61958593a1a8f6cf8e8e3f2966451b2b8cc4509f Mon Sep 17 00:00:00 2001 From: ycc Date: Sun, 8 Jan 2023 22:57:17 +0100 Subject: [PATCH] identity in config to allow save from everywhere --- client/config.go | 3 ++- client/identity.go | 4 ++-- client/storage.go | 19 ++++++++++++++----- 3 files changed, 18 insertions(+), 8 deletions(-) diff --git a/client/config.go b/client/config.go index 8c81ae6..57cd37f 100644 --- a/client/config.go +++ b/client/config.go @@ -11,6 +11,7 @@ type Config struct { SavePassword bool `json:"save_password,omitempty"` SavedPassword string `json:"saved_password,omitempty"` // Technical + IdentityFile string `json:"identity_file,omitempty"` StoragePath string `json:"storage_path,omitempty"` MaxIdsPerUser int `json:"max_ids_per_user,omitempty"` MsgDbRollingPeriod int `json:"msg_db_rolling_period,omitempty"` @@ -29,10 +30,10 @@ type Config struct { PrivateChatNotifiactions bool `json:"private_chat_notifiactions,omitempty"` GroupChatNotifiactions bool `json:"group_chat_notifiactions,omitempty"` ChannelNotifications bool `json:"channel_notifications,omitempty"` + // Inner memoryPassword string additionalPasswords []string - identityFile string me *Identity } diff --git a/client/identity.go b/client/identity.go index 630455c..6b768d7 100644 --- a/client/identity.go +++ b/client/identity.go @@ -115,7 +115,7 @@ func (id *Identity) FinalizeInvitation(ReceivedContact *meowlib.ContactCard) err func LoadIdentity(filename string, password string) (*Identity, error) { var id Identity GetConfig().memoryPassword = password - GetConfig().identityFile = filename + GetConfig().IdentityFile = filename indata, err := os.ReadFile(filename) if err != nil { return nil, err @@ -135,6 +135,6 @@ func (id *Identity) Save() error { if err != nil { return err } - err = os.WriteFile(GetConfig().identityFile, []byte(armor), 0600) + err = os.WriteFile(GetConfig().IdentityFile, []byte(armor), 0600) return err } diff --git a/client/storage.go b/client/storage.go index 108af21..4b9f60f 100644 --- a/client/storage.go +++ b/client/storage.go @@ -8,6 +8,7 @@ import ( "forge.redroom.link/yves/meowlib" "github.com/google/uuid" _ "github.com/mattn/go-sqlite3" + "google.golang.org/protobuf/proto" ) func StoreMessage(peer *Peer, usermessage *meowlib.UserMessage, password string) error { @@ -34,11 +35,11 @@ func StoreMessage(peer *Peer, usermessage *meowlib.UserMessage, password string) // Open Db db, _ := sql.Open("sqlite3", filepath.Join(GetConfig().StoragePath, dbid)) // Open the created SQLite File defer db.Close() - // TODO Detach Files + // Detach Files if len(usermessage.Files) > 0 { for _, f := range usermessage.Files { hiddenFilename := uuid.NewString() - // TODO cypher file + // Cypher file encData, err := meowlib.SymEncrypt(password, f.Data) if err != nil { return err @@ -48,14 +49,22 @@ func StoreMessage(peer *Peer, usermessage *meowlib.UserMessage, password string) f.Data = []byte(hiddenFilename) } } - // TODO Encrypt message - // Inser message + // Encrypt message + out, err := proto.Marshal(usermessage) + if err != nil { + return err + } + encData, err := meowlib.SymEncrypt(password, out) + if err != nil { + return err + } + // Insert message insertMessageSQL := `INSERT INTO message(m) VALUES (?)` statement, err := db.Prepare(insertMessageSQL) // Prepare statement. if err != nil { return err } - _, err = statement.Exec(usermessage) + _, err = statement.Exec(encData) if err != nil { return err }