2023-01-07 00:39:05 +01:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"forge.redroom.link/yves/meowlib"
|
|
|
|
"github.com/google/uuid"
|
|
|
|
_ "github.com/mattn/go-sqlite3"
|
2023-01-08 22:57:17 +01:00
|
|
|
"google.golang.org/protobuf/proto"
|
2023-01-07 00:39:05 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
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()
|
2023-01-08 22:57:17 +01:00
|
|
|
// Detach Files
|
2023-01-07 00:39:05 +01:00
|
|
|
if len(usermessage.Files) > 0 {
|
|
|
|
for _, f := range usermessage.Files {
|
|
|
|
hiddenFilename := uuid.NewString()
|
2023-01-08 22:57:17 +01:00
|
|
|
// Cypher file
|
2023-01-07 00:39:05 +01:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
2023-01-08 22:57:17 +01:00
|
|
|
// 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
|
2023-01-07 00:39:05 +01:00
|
|
|
insertMessageSQL := `INSERT INTO message(m) VALUES (?)`
|
|
|
|
statement, err := db.Prepare(insertMessageSQL) // Prepare statement.
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-01-08 22:57:17 +01:00
|
|
|
_, err = statement.Exec(encData)
|
2023-01-07 00:39:05 +01:00
|
|
|
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
|
|
|
|
}
|