diff --git a/client/config.go b/client/config.go index 15924aa..8c81ae6 100644 --- a/client/config.go +++ b/client/config.go @@ -30,8 +30,10 @@ type Config struct { GroupChatNotifiactions bool `json:"group_chat_notifiactions,omitempty"` ChannelNotifications bool `json:"channel_notifications,omitempty"` // Inner - memoryPassword string - identityFile string + memoryPassword string + additionalPasswords []string + identityFile string + me *Identity } var instance *Config @@ -75,3 +77,11 @@ func (c *Config) SetMemPass(pass string) { func (c *Config) GetMemPass() string { return c.memoryPassword } + +func (c *Config) SaveIdentity() error { + return c.me.Save() +} + +func (c *Config) Clean() { + c.additionalPasswords = []string{} +} diff --git a/client/identity.go b/client/identity.go index 67de237..630455c 100644 --- a/client/identity.go +++ b/client/identity.go @@ -32,6 +32,7 @@ func CreateIdentity(nickname string) *Identity { var id Identity id.Nickname = nickname id.RootKp = meowlib.NewKeyPair() + GetConfig().me = &id return &id } @@ -124,6 +125,7 @@ func LoadIdentity(filename string, password string) (*Identity, error) { return nil, err } err = json.Unmarshal([]byte(pass), &id) + GetConfig().me = &id return &id, err } diff --git a/client/identity_test.go b/client/identity_test.go index e13fc74..1eb5733 100644 --- a/client/identity_test.go +++ b/client/identity_test.go @@ -24,6 +24,8 @@ func TestLoad(t *testing.T) { if err != nil { id := CreateIdentity("myname") id.Save() + } else { + log.Println(id.Nickname) } id, err = LoadIdentity("test.id", "toto") if err != nil { diff --git a/client/peer.go b/client/peer.go index 81b64c4..87fdeac 100644 --- a/client/peer.go +++ b/client/peer.go @@ -31,7 +31,7 @@ type Peer struct { OnionMode bool `json:"onion_mode,omitempty"` LastMessage time.Time `json:"last_message,omitempty"` DbIds []string `json:"db_ids,omitempty"` - DbPassword string `json:"db_password,omitempty"` + dbPassword string } type PeerList []Peer @@ -193,9 +193,9 @@ func (p *Peer) UnPackUserMessage(protoPackedMessage []byte) (payload []byte, sig return msg.Payload, msg.Signature, nil } -func (p *Peer) GetConversationRequest() meowlib.ToServerMessage_ConversationRequest { +func (p *Peer) GetConversationRequest() *meowlib.ToServerMessage_ConversationRequest { var cr meowlib.ToServerMessage_ConversationRequest - return cr + return &cr } func (p *Peer) ProcessOutboundUserMessage(usermessage *meowlib.UserMessage) (*meowlib.PackedUserMessage, error) { @@ -226,6 +226,14 @@ func (p *Peer) ProcessInboundUserMessage(message []byte, signature []byte) (*meo return msg, nil } +func (p *Peer) SetDbPassword(password string) { + p.dbPassword = password +} + +func (p *Peer) GetDbPassword() string { + return p.dbPassword +} + func (p *Peer) StoreMessage(msg []byte) { } diff --git a/client/storage.go b/client/storage.go new file mode 100644 index 0000000..108af21 --- /dev/null +++ b/client/storage.go @@ -0,0 +1,82 @@ +package client + +import ( + "database/sql" + "os" + "path/filepath" + + "forge.redroom.link/yves/meowlib" + "github.com/google/uuid" + _ "github.com/mattn/go-sqlite3" +) + +func StoreMessage(peer *Peer, usermessage *meowlib.UserMessage, password string) error { + var dbid string + // If no db/no ID create DB + Tablz + if len(peer.DbIds) == 0 { + dbid = uuid.NewString() + file, err := os.Create(filepath.Join(GetConfig().StoragePath, dbid)) + if err != nil { + return err + } + file.Close() + peer.DbIds = append(peer.DbIds, dbid) + sqliteDatabase, _ := sql.Open("sqlite3", filepath.Join(GetConfig().StoragePath, dbid)) // Open the created SQLite File + err = createTable(sqliteDatabase) + if err != nil { + return err + } + sqliteDatabase.Close() + GetConfig().me.Save() + } else { + dbid = peer.DbIds[len(peer.DbIds)-1] + } + // Open Db + db, _ := sql.Open("sqlite3", filepath.Join(GetConfig().StoragePath, dbid)) // Open the created SQLite File + defer db.Close() + // TODO Detach Files + if len(usermessage.Files) > 0 { + for _, f := range usermessage.Files { + hiddenFilename := uuid.NewString() + // TODO cypher file + encData, err := meowlib.SymEncrypt(password, f.Data) + if err != nil { + return err + } + os.WriteFile(hiddenFilename, encData, 0600) + // replace f.Data by uuid filename + f.Data = []byte(hiddenFilename) + } + } + // TODO Encrypt message + // Inser message + insertMessageSQL := `INSERT INTO message(m) VALUES (?)` + statement, err := db.Prepare(insertMessageSQL) // Prepare statement. + if err != nil { + return err + } + _, err = statement.Exec(usermessage) + if err != nil { + return err + } + return nil +} + +// Get last messages from a peer +func GetLastMessages(peer *Peer, lastIdx int, count int, gotUuid string) { + // TODO Try to get count from last DB + // TODO Get missing from previous DB +} + +func createTable(db *sql.DB) error { + createMessageTableSQL := `CREATE TABLE message ( + "id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, + "m" BLOB, + );` // SQL Statement for Create Table + statement, err := db.Prepare(createMessageTableSQL) // Prepare SQL Statement + if err != nil { + return err + } + statement.Exec() // Execute SQL Statements + return nil +} diff --git a/go.mod b/go.mod index 8c21964..5efd044 100644 --- a/go.mod +++ b/go.mod @@ -7,12 +7,12 @@ require ( github.com/go-redis/redis v6.15.9+incompatible github.com/google/uuid v1.3.0 github.com/makiuchi-d/gozxing v0.1.1 + github.com/mattn/go-sqlite3 v1.14.16 github.com/onsi/ginkgo v1.16.5 // indirect github.com/onsi/gomega v1.22.1 // indirect github.com/pkg/errors v0.9.1 github.com/rs/zerolog v1.25.0 github.com/stretchr/testify v1.5.1 - golang.org/x/mobile v0.0.0-20221110043201-43a038452099 // indirect golang.org/x/net v0.3.0 // indirect google.golang.org/protobuf v1.28.1 ) diff --git a/go.sum b/go.sum index 761a359..16a111e 100644 --- a/go.sum +++ b/go.sum @@ -43,6 +43,8 @@ github.com/konsorten/go-windows-terminal-sequences v1.0.1 h1:mweAR1A6xJ3oS2pRaGi github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= github.com/makiuchi-d/gozxing v0.1.1 h1:xxqijhoedi+/lZlhINteGbywIrewVdVv2wl9r5O9S1I= github.com/makiuchi-d/gozxing v0.1.1/go.mod h1:eRIHbOjX7QWxLIDJoQuMLhuXg9LAuw6znsUtRkNw9DU= +github.com/mattn/go-sqlite3 v1.14.16 h1:yOQRA0RpS5PFz/oikGwBEqvAWhWg5ufRz4ETLjwpU1Y= +github.com/mattn/go-sqlite3 v1.14.16/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg= github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A= github.com/nxadm/tail v1.4.8 h1:nPr65rt6Y5JFSKQO7qToXr7pePgD6Gwiw05lkbyAQTE= github.com/nxadm/tail v1.4.8/go.mod h1:+ncqLTQzXmGhMZNUePPaPqPvBxHAIsmXswZKocGu+AU= @@ -94,15 +96,12 @@ golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMx golang.org/x/image v0.0.0-20190802002840-cff245a6509b/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0= golang.org/x/mobile v0.0.0-20190312151609-d3739f865fa6/go.mod h1:z+o9i4GpDbdi3rU15maQ/Ox0txvL9dWGYEHz965HBQE= golang.org/x/mobile v0.0.0-20200801112145-973feb4309de/go.mod h1:skQtrUTUwhdJvXM/2KKJzY8pDgNr9I/FOMqDVRPBUS4= -golang.org/x/mobile v0.0.0-20221110043201-43a038452099 h1:aIu0lKmfdgtn2uTj7JI2oN4TUrQvgB+wzTPO23bCKt8= -golang.org/x/mobile v0.0.0-20221110043201-43a038452099/go.mod h1:aAjjkJNdrh3PMckS4B10TGS2nag27cbKR1y2BpUxsiY= golang.org/x/mod v0.1.0/go.mod h1:0QHyrYULN0/3qlju5TqG8bIK38QM8yzMo5ekMj3DlcY= golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg= golang.org/x/mod v0.1.1-0.20191209134235-331c550502dd/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.6.0-dev.0.20220106191415-9b9b3d81d5e3/go.mod h1:3p9vT2HGsQu2K1YbXdKPJLVgG5VJdoTa1poYQBtP1AY= -golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 h1:6zppjxzCulZykYSLyVDYbneBfbaBIQPYMevg0bEwv2s= golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= @@ -124,7 +123,6 @@ golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJ golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.0.0-20220819030929-7fc1605a5dde/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= @@ -166,7 +164,6 @@ golang.org/x/tools v0.0.0-20200117012304-6edc0a871e69/go.mod h1:TB2adYChydJhpapK golang.org/x/tools v0.0.0-20201224043029-2b0845dc783e/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA= golang.org/x/tools v0.1.5/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk= golang.org/x/tools v0.1.10/go.mod h1:Uh6Zz+xoGYZom868N8YTex3t7RhtHDBrE8Gzo9bV56E= -golang.org/x/tools v0.1.12 h1:VveCTK38A2rkS8ZqFY25HIDFscX5X9OoEhJd3quQmXU= golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=