[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"
|
"PD1/internal/utils/networking"
|
||||||
"crypto/x509"
|
"crypto/x509"
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"log"
|
||||||
"sort"
|
"sort"
|
||||||
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Run() {
|
func Run() {
|
||||||
|
@ -76,40 +77,33 @@ func Run() {
|
||||||
|
|
||||||
showMessagesInfo(clientMessages)
|
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":
|
case "getmsg":
|
||||||
if flag.NArg() < 2 {
|
if flag.NArg() < 2 {
|
||||||
panic("Insufficient arguments for 'getmsg' command. Usage: getmsg <NUM>")
|
panic("Insufficient arguments for 'getmsg' command. Usage: getmsg <NUM>")
|
||||||
}
|
}
|
||||||
//num := flag.Arg(1)
|
numString := flag.Arg(1)
|
||||||
cl := networking.NewClient[protocol.Packet](&clientKeyStore)
|
cl := networking.NewClient[protocol.Packet](&clientKeyStore)
|
||||||
defer cl.Connection.Conn.Close()
|
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":
|
case "help":
|
||||||
showHelp()
|
showHelp()
|
||||||
|
|
||||||
|
@ -163,7 +157,6 @@ func getManyMessagesInfo(cl networking.Client[protocol.Packet]) ([]protocol.Serv
|
||||||
//Get senders' certificates
|
//Get senders' certificates
|
||||||
for senderUID := range senderSet {
|
for senderUID := range senderSet {
|
||||||
senderCert := getUserCert(cl, senderUID)
|
senderCert := getUserCert(cl, senderUID)
|
||||||
fmt.Println("Got a User cert")
|
|
||||||
certificatesMap[senderUID] = senderCert
|
certificatesMap[senderUID] = senderCert
|
||||||
}
|
}
|
||||||
return serverMessageInfoPackets, certificatesMap
|
return serverMessageInfoPackets, certificatesMap
|
||||||
|
|
|
@ -63,7 +63,6 @@ func (ds DataStore) GetMessage(toUID string, position int) protocol.Packet {
|
||||||
SELECT fromUID, toUID, subject, body, timestamp
|
SELECT fromUID, toUID, subject, body, timestamp
|
||||||
FROM messages
|
FROM messages
|
||||||
WHERE toUID = ?
|
WHERE toUID = ?
|
||||||
AND status = 0
|
|
||||||
ORDER BY timestamp
|
ORDER BY timestamp
|
||||||
LIMIT 1 OFFSET ?
|
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) {
|
func (ds DataStore) MarkMessageInQueueAsRead(toUID string, position int) {
|
||||||
query := `
|
query := `
|
||||||
UPDATE messages
|
UPDATE messages
|
||||||
SET status = 1
|
SET status = 1
|
||||||
WHERE toUID = ? AND status = 0
|
WHERE (fromUID,toUID,timestamp) = (
|
||||||
ORDER BY timestamp
|
SELECT fromUID,toUID,timestamp
|
||||||
LIMIT 1 OFFSET ?
|
FROM messages
|
||||||
|
WHERE toUID = ?
|
||||||
|
ORDER BY timestamp
|
||||||
|
LIMIT 1 OFFSET ?
|
||||||
|
)
|
||||||
`
|
`
|
||||||
|
|
||||||
// Execute the SQL statement
|
// Execute the SQL statement
|
||||||
|
|
|
@ -48,6 +48,7 @@ F:
|
||||||
if active := connection.Send(message); !active {
|
if active := connection.Send(message); !active {
|
||||||
break F
|
break F
|
||||||
}
|
}
|
||||||
|
dataStore.MarkMessageInQueueAsRead(UID, reqMsg.Num)
|
||||||
case protocol.SubmitMsgPkt:
|
case protocol.SubmitMsgPkt:
|
||||||
submitMsg := protocol.UnmarshalSubmitMessagePacket(pac.Body)
|
submitMsg := protocol.UnmarshalSubmitMessagePacket(pac.Body)
|
||||||
if submitMsg.ToUID != UID && dataStore.userExists(submitMsg.ToUID) {
|
if submitMsg.ToUID != UID && dataStore.userExists(submitMsg.ToUID) {
|
||||||
|
|
Binary file not shown.
Loading…
Reference in a new issue