[PD1] Logic all done?
This commit is contained in:
parent
cfe9f3c7b3
commit
4c141bbc6e
4 changed files with 31 additions and 34 deletions
|
@ -6,8 +6,9 @@ import (
|
|||
"PD1/internal/utils/networking"
|
||||
"crypto/x509"
|
||||
"flag"
|
||||
"fmt"
|
||||
"log"
|
||||
"sort"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func Run() {
|
||||
|
@ -76,39 +77,32 @@ 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
|
||||
|
|
|
@ -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 ?
|
||||
`
|
||||
|
@ -82,9 +81,13 @@ func (ds DataStore) MarkMessageInQueueAsRead(toUID string, position int) {
|
|||
query := `
|
||||
UPDATE messages
|
||||
SET status = 1
|
||||
WHERE toUID = ? AND status = 0
|
||||
WHERE (fromUID,toUID,timestamp) = (
|
||||
SELECT fromUID,toUID,timestamp
|
||||
FROM messages
|
||||
WHERE toUID = ?
|
||||
ORDER BY timestamp
|
||||
LIMIT 1 OFFSET ?
|
||||
)
|
||||
`
|
||||
|
||||
// Execute the SQL statement
|
||||
|
|
|
@ -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.
Loading…
Reference in a new issue