[PD1] Logic all done?

This commit is contained in:
Afonso Franco 2024-04-20 00:55:16 +01:00
parent cfe9f3c7b3
commit 4c141bbc6e
Signed by: afonso
SSH key fingerprint: SHA256:aiLbdlPwXKJS5wMnghdtod0SPy8imZjlVvCyUX9DJNk
4 changed files with 31 additions and 34 deletions

View file

@ -6,8 +6,9 @@ import (
"PD1/internal/utils/networking"
"crypto/x509"
"flag"
"fmt"
"log"
"sort"
"strconv"
)
func Run() {
@ -76,40 +77,33 @@ func Run() {
showMessagesInfo(clientMessages)
//case "getall":
// cl := networking.NewClient[protocol.Packet](&clientKeyStore)
// defer cl.Connection.Conn.Close()
// requestAllMsgPacket := protocol.NewRequestAllMsgPacket()
// if !cl.Connection.Send(requestAllMsgPacket) {
// return
// }
// serverMessagePackets,certificates := getManyMessages(cl)
// var clientMessages []ClientMessage
// for _, message := range serverMessagePackets {
// senderCert, ok := certificates[message.FromUID]
// if ok {
// decryptedContentBytes := clientKeyStore.DecryptMessageContent(senderCert, message.Content)
// content := UnmarshalContent(decryptedContentBytes)
// clientMessage := newClientMessage(message.FromUID, message.ToUID, content, message.Timestamp)
// clientMessages = append(clientMessages, clientMessage)
// }
// }
// //Sort the messages
// sort.Slice(clientMessages, func(i, j int) bool {
// return clientMessages[i].Timestamp.After(clientMessages[j].Timestamp)
// })
// showMessages(clientMessages)
case "getmsg":
if flag.NArg() < 2 {
panic("Insufficient arguments for 'getmsg' command. Usage: getmsg <NUM>")
}
//num := flag.Arg(1)
numString := flag.Arg(1)
cl := networking.NewClient[protocol.Packet](&clientKeyStore)
defer cl.Connection.Conn.Close()
num,err :=strconv.Atoi(numString)
if err!=nil{
log.Panicln("NUM argument provided is not a number")
}
packet := protocol.NewRequestMsgPacket(num)
cl.Connection.Send(packet)
receivedMsgPacket,active := cl.Connection.Receive()
if !active{
return
}
serverMessagePacket := protocol.UnmarshalServerMessagePacket(receivedMsgPacket.Body)
senderCert := getUserCert(cl, serverMessagePacket.FromUID)
decryptedSubjectBytes := clientKeyStore.DecryptMessageContent(senderCert, serverMessagePacket.Subject)
decryptedBodyBytes := clientKeyStore.DecryptMessageContent(senderCert, serverMessagePacket.Body)
subject := Unmarshal(decryptedSubjectBytes)
body := Unmarshal(decryptedBodyBytes)
message := newClientMessage(serverMessagePacket.FromUID, serverMessagePacket.ToUID, subject, body, serverMessagePacket.Timestamp)
showMessage(message)
case "help":
showHelp()
@ -163,7 +157,6 @@ func getManyMessagesInfo(cl networking.Client[protocol.Packet]) ([]protocol.Serv
//Get senders' certificates
for senderUID := range senderSet {
senderCert := getUserCert(cl, senderUID)
fmt.Println("Got a User cert")
certificatesMap[senderUID] = senderCert
}
return serverMessageInfoPackets, certificatesMap

View file

@ -63,7 +63,6 @@ func (ds DataStore) GetMessage(toUID string, position int) protocol.Packet {
SELECT fromUID, toUID, subject, body, timestamp
FROM messages
WHERE toUID = ?
AND status = 0
ORDER BY timestamp
LIMIT 1 OFFSET ?
`
@ -81,10 +80,14 @@ func (ds DataStore) GetMessage(toUID string, position int) protocol.Packet {
func (ds DataStore) MarkMessageInQueueAsRead(toUID string, position int) {
query := `
UPDATE messages
SET status = 1
WHERE toUID = ? AND status = 0
ORDER BY timestamp
LIMIT 1 OFFSET ?
SET status = 1
WHERE (fromUID,toUID,timestamp) = (
SELECT fromUID,toUID,timestamp
FROM messages
WHERE toUID = ?
ORDER BY timestamp
LIMIT 1 OFFSET ?
)
`
// Execute the SQL statement

View file

@ -48,6 +48,7 @@ F:
if active := connection.Send(message); !active {
break F
}
dataStore.MarkMessageInQueueAsRead(UID, reqMsg.Num)
case protocol.SubmitMsgPkt:
submitMsg := protocol.UnmarshalSubmitMessagePacket(pac.Body)
if submitMsg.ToUID != UID && dataStore.userExists(submitMsg.ToUID) {

Binary file not shown.