2024-04-16 09:02:23 +01:00
|
|
|
package server
|
2024-04-18 01:18:51 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"PD1/internal/protocol"
|
2024-04-19 23:59:26 +01:00
|
|
|
"crypto/x509"
|
2024-04-18 01:18:51 +01:00
|
|
|
"database/sql"
|
2024-04-19 23:59:26 +01:00
|
|
|
"fmt"
|
2024-04-18 01:18:51 +01:00
|
|
|
"log"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
_ "github.com/mattn/go-sqlite3"
|
|
|
|
)
|
|
|
|
|
|
|
|
type DataStore struct {
|
|
|
|
db *sql.DB
|
|
|
|
}
|
|
|
|
|
|
|
|
func OpenDB() DataStore {
|
|
|
|
db, err := sql.Open("sqlite3", "server.db")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalln("Error opening db file")
|
|
|
|
}
|
2024-04-19 23:59:26 +01:00
|
|
|
ds := DataStore{db: db}
|
|
|
|
ds.CreateTables()
|
|
|
|
return ds
|
2024-04-18 01:18:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ds DataStore) CreateTables() error {
|
|
|
|
// Create users table
|
|
|
|
_, err := ds.db.Exec(`CREATE TABLE IF NOT EXISTS users (
|
|
|
|
UID TEXT PRIMARY KEY,
|
|
|
|
userCert BLOB
|
|
|
|
)`)
|
|
|
|
if err != nil {
|
2024-04-19 23:59:26 +01:00
|
|
|
fmt.Println("Error creating users table", err)
|
2024-04-18 01:18:51 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create messages table
|
|
|
|
_, err = ds.db.Exec(`CREATE TABLE IF NOT EXISTS messages (
|
|
|
|
fromUID TEXT,
|
|
|
|
toUID TEXT,
|
|
|
|
timestamp TIMESTAMP,
|
2024-04-20 17:16:52 +01:00
|
|
|
queue_position INT DEFAULT 0,
|
2024-04-19 23:59:26 +01:00
|
|
|
subject BLOB,
|
|
|
|
body BLOB,
|
|
|
|
status INT CHECK (status IN (0,1)),
|
2024-04-18 01:18:51 +01:00
|
|
|
PRIMARY KEY (toUID, fromUID, timestamp),
|
|
|
|
FOREIGN KEY(fromUID) REFERENCES users(UID),
|
|
|
|
FOREIGN KEY(toUID) REFERENCES users(UID)
|
|
|
|
)`)
|
|
|
|
if err != nil {
|
2024-04-19 23:59:26 +01:00
|
|
|
fmt.Println("Error creating messages table", err)
|
2024-04-18 01:18:51 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-04-20 17:16:52 +01:00
|
|
|
// Define a trigger to automatically assign numbers for each message of each user starting from 1
|
|
|
|
_, err = ds.db.Exec(`
|
|
|
|
CREATE TRIGGER IF NOT EXISTS assign_queue_position
|
|
|
|
AFTER INSERT ON messages
|
|
|
|
FOR EACH ROW
|
|
|
|
BEGIN
|
|
|
|
UPDATE messages
|
|
|
|
SET queue_position = (
|
|
|
|
SELECT COUNT(*)
|
|
|
|
FROM messages
|
|
|
|
WHERE toUID = NEW.toUID
|
|
|
|
)
|
|
|
|
WHERE toUID = NEW.toUID AND rowid = NEW.rowid;
|
|
|
|
END;
|
|
|
|
`)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Error creating trigger", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-04-18 01:18:51 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-04-19 23:59:26 +01:00
|
|
|
func (ds DataStore) GetMessage(toUID string, position int) protocol.Packet {
|
2024-04-18 01:18:51 +01:00
|
|
|
|
2024-04-20 17:16:52 +01:00
|
|
|
var serverMessage protocol.AnswerGetMsg
|
2024-04-18 01:18:51 +01:00
|
|
|
query := `
|
2024-04-19 23:59:26 +01:00
|
|
|
SELECT fromUID, toUID, subject, body, timestamp
|
2024-04-18 01:18:51 +01:00
|
|
|
FROM messages
|
2024-04-20 17:16:52 +01:00
|
|
|
WHERE toUID = ? AND queue_position = ?
|
2024-04-18 01:18:51 +01:00
|
|
|
`
|
|
|
|
// Execute the query
|
|
|
|
row := ds.db.QueryRow(query, toUID, position)
|
2024-04-19 23:59:26 +01:00
|
|
|
err := row.Scan(&serverMessage.FromUID, &serverMessage.ToUID, &serverMessage.Subject, &serverMessage.Body, &serverMessage.Timestamp)
|
2024-04-23 11:12:18 +01:00
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
log.Printf("No message with NUM %v for UID %v\n", position, toUID)
|
|
|
|
errorMessage := fmt.Sprintf("No message with NUM %v", position)
|
|
|
|
return protocol.NewReportErrorPacket(errorMessage)
|
2024-04-18 01:18:51 +01:00
|
|
|
}
|
2024-04-19 23:59:26 +01:00
|
|
|
|
2024-04-20 17:16:52 +01:00
|
|
|
return protocol.NewAnswerGetMsgPacket(serverMessage.FromUID, serverMessage.ToUID, serverMessage.Subject, serverMessage.Body, serverMessage.Timestamp, true)
|
2024-04-18 01:18:51 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2024-04-19 23:59:26 +01:00
|
|
|
func (ds DataStore) MarkMessageInQueueAsRead(toUID string, position int) {
|
2024-04-18 01:18:51 +01:00
|
|
|
query := `
|
|
|
|
UPDATE messages
|
2024-04-20 00:55:16 +01:00
|
|
|
SET status = 1
|
|
|
|
WHERE (fromUID,toUID,timestamp) = (
|
|
|
|
SELECT fromUID,toUID,timestamp
|
|
|
|
FROM messages
|
2024-04-20 17:16:52 +01:00
|
|
|
WHERE toUID = ? AND queue_position = ?
|
2024-04-20 00:55:16 +01:00
|
|
|
)
|
2024-04-18 01:18:51 +01:00
|
|
|
`
|
|
|
|
|
|
|
|
// Execute the SQL statement
|
|
|
|
_, err := ds.db.Exec(query, toUID, position)
|
|
|
|
if err != nil {
|
2024-04-19 23:59:26 +01:00
|
|
|
log.Printf("Error marking the message in position %v from UID %v as read: %v", position, toUID, err)
|
2024-04-18 01:18:51 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-20 17:16:52 +01:00
|
|
|
func (ds DataStore) GetUnreadMsgsInfo(toUID string, page int, pageSize int) protocol.Packet {
|
|
|
|
|
|
|
|
// Retrieve the total count of unread messages
|
|
|
|
var totalCount int
|
|
|
|
err := ds.db.QueryRow("SELECT COUNT(*) FROM messages WHERE toUID = ? AND status = 0", toUID).Scan(&totalCount)
|
2024-04-23 11:12:18 +01:00
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
log.Printf("No unread messages for UID %v: %v", toUID, err)
|
|
|
|
return protocol.NewAnswerGetUnreadMsgsInfoPacket(0, 0, []protocol.MsgInfo{})
|
2024-04-20 17:16:52 +01:00
|
|
|
}
|
2024-04-18 01:18:51 +01:00
|
|
|
|
|
|
|
// Query to retrieve all messages from the user's queue
|
|
|
|
query := `
|
2024-04-19 23:59:26 +01:00
|
|
|
SELECT
|
|
|
|
fromUID,
|
|
|
|
toUID,
|
|
|
|
timestamp,
|
|
|
|
queue_position,
|
|
|
|
subject,
|
|
|
|
status
|
2024-04-20 17:16:52 +01:00
|
|
|
FROM messages
|
2024-04-19 23:59:26 +01:00
|
|
|
WHERE
|
2024-04-20 17:16:52 +01:00
|
|
|
toUID = ? AND status = 0
|
2024-04-19 23:59:26 +01:00
|
|
|
ORDER BY
|
2024-04-20 17:16:52 +01:00
|
|
|
queue_position DESC
|
|
|
|
LIMIT ? OFFSET ?;
|
2024-04-18 01:18:51 +01:00
|
|
|
`
|
|
|
|
|
|
|
|
// Execute the query
|
2024-04-20 17:16:52 +01:00
|
|
|
rows, err := ds.db.Query(query, toUID, pageSize, (page-1)*pageSize)
|
2024-04-18 01:18:51 +01:00
|
|
|
if err != nil {
|
2024-04-23 11:12:18 +01:00
|
|
|
log.Printf("Error getting unread messages for UID %v: %v", toUID, err)
|
2024-04-18 01:18:51 +01:00
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
2024-04-20 17:16:52 +01:00
|
|
|
messageInfoPackets := []protocol.MsgInfo{}
|
|
|
|
for rows.Next() {
|
2024-04-18 01:18:51 +01:00
|
|
|
var fromUID string
|
2024-04-19 23:59:26 +01:00
|
|
|
var subject []byte
|
2024-04-18 01:18:51 +01:00
|
|
|
var timestamp time.Time
|
2024-04-19 23:59:26 +01:00
|
|
|
var queuePosition, status int
|
|
|
|
if err := rows.Scan(&fromUID, &toUID, ×tamp, &queuePosition, &subject, &status); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2024-04-20 17:16:52 +01:00
|
|
|
answerGetUnreadMsgsInfo := protocol.NewMsgInfo(queuePosition, fromUID, subject, timestamp)
|
|
|
|
messageInfoPackets = append(messageInfoPackets, answerGetUnreadMsgsInfo)
|
2024-04-18 01:18:51 +01:00
|
|
|
}
|
|
|
|
if err := rows.Err(); err != nil {
|
2024-04-19 23:59:26 +01:00
|
|
|
log.Printf("Error when getting messages for UID %v: %v", toUID, err)
|
2024-04-23 11:12:18 +01:00
|
|
|
return protocol.NewReportErrorPacket(err.Error())
|
2024-04-18 01:18:51 +01:00
|
|
|
}
|
|
|
|
|
2024-04-20 17:16:52 +01:00
|
|
|
numberOfPages := (totalCount + pageSize - 1) / pageSize
|
|
|
|
currentPage := min(numberOfPages, page)
|
|
|
|
return protocol.NewAnswerGetUnreadMsgsInfoPacket(currentPage, numberOfPages, messageInfoPackets)
|
2024-04-18 01:18:51 +01:00
|
|
|
}
|
|
|
|
|
2024-04-23 11:12:18 +01:00
|
|
|
func (ds DataStore) AddMessageToQueue(fromUID string, message protocol.SendMsg) protocol.Packet {
|
2024-04-18 01:18:51 +01:00
|
|
|
query := `
|
2024-04-19 23:59:26 +01:00
|
|
|
INSERT INTO messages (fromUID, toUID, subject, body, timestamp, status)
|
|
|
|
VALUES (?, ?, ?, ?, ?, 0)
|
2024-04-18 01:18:51 +01:00
|
|
|
`
|
|
|
|
|
|
|
|
// Execute the SQL statement
|
|
|
|
currentTime := time.Now()
|
2024-04-19 23:59:26 +01:00
|
|
|
_, err := ds.db.Exec(query, fromUID, message.ToUID, message.Subject, message.Body, currentTime)
|
2024-04-18 01:18:51 +01:00
|
|
|
if err != nil {
|
2024-04-19 23:59:26 +01:00
|
|
|
log.Printf("Error adding message to UID %v: %v", fromUID, err)
|
2024-04-23 11:12:18 +01:00
|
|
|
return protocol.NewReportErrorPacket(err.Error())
|
2024-04-18 01:18:51 +01:00
|
|
|
}
|
2024-04-23 11:12:18 +01:00
|
|
|
return protocol.NewAnswerSendMsgPacket()
|
2024-04-18 01:18:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ds DataStore) GetUserCertificate(uid string) protocol.Packet {
|
|
|
|
query := `
|
|
|
|
SELECT userCert
|
|
|
|
FROM users
|
|
|
|
WHERE UID = ?
|
|
|
|
`
|
|
|
|
|
|
|
|
// Execute the SQL query
|
2024-04-19 23:59:26 +01:00
|
|
|
var userCertBytes []byte
|
|
|
|
err := ds.db.QueryRow(query, uid).Scan(&userCertBytes)
|
|
|
|
if err == sql.ErrNoRows {
|
2024-04-23 11:12:18 +01:00
|
|
|
errorMessage := fmt.Sprintf("No certificate for UID %v found in the database", uid)
|
|
|
|
log.Println(errorMessage)
|
|
|
|
return protocol.NewReportErrorPacket(errorMessage)
|
2024-04-18 01:18:51 +01:00
|
|
|
}
|
2024-04-20 17:16:52 +01:00
|
|
|
return protocol.NewAnswerGetUserCertPacket(uid, userCertBytes)
|
2024-04-18 01:18:51 +01:00
|
|
|
}
|
2024-04-19 11:55:16 +01:00
|
|
|
|
2024-04-19 23:59:26 +01:00
|
|
|
func (ds DataStore) userExists(uid string) bool {
|
|
|
|
// Prepare the SQL statement for checking if a user exists
|
|
|
|
query := `
|
2024-04-19 11:55:16 +01:00
|
|
|
SELECT COUNT(*)
|
|
|
|
FROM users
|
|
|
|
WHERE UID = ?
|
|
|
|
`
|
|
|
|
|
2024-04-19 23:59:26 +01:00
|
|
|
var count int
|
|
|
|
// Execute the SQL query
|
|
|
|
err := ds.db.QueryRow(query, uid).Scan(&count)
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
log.Printf("User with UID %v does not exist", uid)
|
|
|
|
return false
|
|
|
|
} else {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ds DataStore) storeUserCertIfNotExists(uid string, cert x509.Certificate) {
|
|
|
|
// Check if the user already exists
|
|
|
|
if ds.userExists(uid) {
|
|
|
|
return
|
|
|
|
}
|
2024-04-19 11:55:16 +01:00
|
|
|
|
2024-04-19 23:59:26 +01:00
|
|
|
// Insert the user certificate
|
|
|
|
insertQuery := `
|
|
|
|
INSERT INTO users (UID, userCert)
|
|
|
|
VALUES (?, ?)
|
|
|
|
`
|
|
|
|
_, err := ds.db.Exec(insertQuery, uid, cert.Raw)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Error storing user certificate for UID %s: %v\n", uid, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
log.Printf("User certificate for UID %s stored successfully.\n", uid)
|
2024-04-19 11:55:16 +01:00
|
|
|
}
|