meowlib/client/messagestorage.go

243 lines
6.5 KiB
Go
Raw Normal View History

package client
import (
"database/sql"
"math"
"os"
"path/filepath"
"forge.redroom.link/yves/meowlib"
"github.com/google/uuid"
_ "github.com/mattn/go-sqlite3"
"google.golang.org/protobuf/proto"
)
2024-02-29 21:03:15 +01:00
func StoreMessage(peer *Peer, usermessage *meowlib.UserMessage, filenames []string, password string) error {
var dbid string
// If no db/no ID create DB + Tablz
// TODO : if file size > X new db
if len(peer.DbIds) == 0 {
dbid = uuid.NewString()
peer.DbIds = []string{dbid}
GetConfig().me.Save()
file, err := os.Create(filepath.Join(GetConfig().StoragePath, dbid+GetConfig().DbSuffix))
if err != nil {
return err
}
file.Close()
peer.DbIds = append(peer.DbIds, dbid)
sqliteDatabase, _ := sql.Open("sqlite3", filepath.Join(GetConfig().StoragePath, dbid+GetConfig().DbSuffix)) // Open the created SQLite File
err = createMessageTable(sqliteDatabase)
if err != nil {
return err
}
sqliteDatabase.Close()
} else {
dbid = peer.DbIds[len(peer.DbIds)-1]
}
// Open Db
db, _ := sql.Open("sqlite3", filepath.Join(GetConfig().StoragePath, dbid+GetConfig().DbSuffix)) // Open the created SQLite File
defer db.Close()
// Detach Files
if len(usermessage.Files) > 0 {
for _, f := range usermessage.Files {
hiddenFilename := uuid.NewString()
// 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)
}
}
2024-02-29 21:03:15 +01:00
outbound := true
if usermessage.From == peer.ContactPublicKey {
outbound = false
}
// Convert UserMessage to DbMessage
dbm := UserMessageToDbMessage(outbound, usermessage, filenames)
// Encrypt message
2024-02-29 21:03:15 +01:00
out, err := proto.Marshal(dbm)
if err != nil {
return err
}
encData, err := meowlib.SymEncrypt(password, out)
if err != nil {
return err
}
// Insert message
insertMessageSQL := `INSERT INTO message(m) VALUES (?) RETURNING ID`
statement, err := db.Prepare(insertMessageSQL) // Prepare statement.
if err != nil {
return err
}
_, err = statement.Exec(encData)
if err != nil {
return err
}
return nil
}
// Get new messages from a peer
func GetNewMessages(peer *Peer, lastDbId int, password string) ([]*InternalUserMessage, error) {
var messages []*InternalUserMessage
fileidx := len(peer.DbIds) - 1
// There fileidx should provide the db that we need (unless wantMore overlaps the next DB)
db, _ := sql.Open("sqlite3", filepath.Join(GetConfig().StoragePath, peer.DbIds[fileidx]+GetConfig().DbSuffix)) // Open the created SQLite File
defer db.Close()
// if it's first app query, it won't hold a lastIndex, so let's start from end
if lastDbId == 0 {
lastDbId = math.MaxInt64
}
stm, err := db.Prepare("SELECT id, m FROM message WHERE id > ? ORDER BY id DESC")
if err != nil {
return nil, err
}
defer stm.Close()
rows, err := stm.Query(lastDbId)
if err != nil {
return nil, err
}
defer rows.Close()
for rows.Next() {
var ium *InternalUserMessage
2024-02-29 21:03:15 +01:00
var dbm meowlib.DbMessage
var id int64
var m []byte
err = rows.Scan(&id, &m)
if err != nil {
return nil, err
}
decdata, err := meowlib.SymDecrypt(password, m)
if err != nil {
return nil, err
}
2024-02-29 21:03:15 +01:00
err = proto.Unmarshal(decdata, &dbm)
if err != nil {
return nil, err
}
2024-02-29 21:03:15 +01:00
ium = DbMessageToInternalUserMessage(id, peer.DbIds[fileidx], &dbm)
ium.Dbid = id
ium.Dbfile = peer.DbIds[fileidx]
messages = append(messages, ium)
}
// TODO DB overlap
return messages, nil
}
// Get old messages from a peer
func GetMessagesHistory(peer *Peer, inAppMsgCount int, lastDbId int, wantMore int, password string) ([]InternalUserMessage, error) {
var messages []InternalUserMessage
fileidx := len(peer.DbIds) - 1
// initialize count with last db message count
countStack, err := getMessageCount(peer.DbIds[fileidx])
if err != nil {
return nil, err
}
// while the db message count < what we already have in app, step to next db file
for inAppMsgCount > countStack {
fileidx--
if fileidx < 0 {
return nil, nil
}
newCount, err := getMessageCount(peer.DbIds[fileidx])
if err != nil {
return nil, err
}
countStack += newCount
}
// There fileidx should provide the db that we need (unless wantMore overlaps the next DB)
db, _ := sql.Open("sqlite3", filepath.Join(GetConfig().StoragePath, peer.DbIds[fileidx]+GetConfig().DbSuffix)) // Open the created SQLite File
defer db.Close()
// if it's first app query, it won't hold a lastIndex, so let's start from end
if lastDbId == 0 {
lastDbId = math.MaxInt64
}
stm, err := db.Prepare("SELECT id, m FROM message WHERE id < ? ORDER BY id DESC LIMIT ?")
if err != nil {
return nil, err
}
defer stm.Close()
rows, err := stm.Query(lastDbId, wantMore)
if err != nil {
return nil, err
}
defer rows.Close()
for rows.Next() {
var ium *InternalUserMessage
2024-02-29 21:03:15 +01:00
var dbm meowlib.DbMessage
var id int64
var m []byte
err = rows.Scan(&id, &m)
if err != nil {
return nil, err
}
decdata, err := meowlib.SymDecrypt(password, m)
if err != nil {
return nil, err
}
2024-02-29 21:03:15 +01:00
err = proto.Unmarshal(decdata, &dbm)
if err != nil {
return nil, err
}
2024-02-29 21:03:15 +01:00
ium = DbMessageToInternalUserMessage(id, peer.DbIds[fileidx], &dbm)
ium.Dbid = id
ium.Dbfile = peer.DbIds[fileidx]
messages = append(messages, *ium)
}
// TODO DB overlap
return messages, nil
}
func getMessageCount(dbid string) (int, error) {
db, _ := sql.Open("sqlite3", filepath.Join(GetConfig().StoragePath, dbid+GetConfig().DbSuffix)) // Open the created SQLite File
defer db.Close()
var count int
query := "SELECT COUNT(*) FROM message"
err := db.QueryRow(query).Scan(&count)
if err != nil {
return 0, err
}
return count, nil
}
func createMessageTable(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
}
func createServerTable(db *sql.DB) error {
createServerTableSQL := `CREATE TABLE servers (
"id" integer NOT NULL PRIMARY KEY AUTOINCREMENT,
"country" varchar(2),
"public" bool,
"uptime" int,
"bandwith" float,
"load" float,
"url" varchar(2000)
"name" varchar(255);
"description" varchar(5000)
"publickey" varchar(10000)
)` // SQL Statement for Create Table
statement, err := db.Prepare(createServerTableSQL) // Prepare SQL Statement
if err != nil {
return err
}
statement.Exec() // Execute SQL Statements
return nil
}