CSI-ES-2324/Projs/PD1/internal/client/client.go

68 lines
1.6 KiB
Go
Raw Normal View History

2024-04-16 12:23:00 +01:00
package client
import (
"PD1/internal/protocol"
2024-04-18 13:06:16 +01:00
"PD1/internal/utils/cryptoUtils"
"PD1/internal/utils/networking"
"flag"
"fmt"
)
2024-04-16 12:23:00 +01:00
func Run() {
var userFile string
flag.StringVar(&userFile, "user", "userdata.p12", "Specify user data file")
flag.Parse()
if flag.NArg() == 0 {
panic("No command provided. Use 'help' for instructions.")
}
command := flag.Arg(0)
switch command {
case "send":
if flag.NArg() < 3 {
panic("Insufficient arguments for 'send' command. Usage: send <UID> <SUBJECT>")
}
uid := flag.Arg(1)
subject := flag.Arg(2)
messageContent := readMessageContent()
2024-04-18 13:06:16 +01:00
clientCert := cryptoUtils.LoadKeyStore("userdata.p12")
cl := networking.NewClient[protocol.Packet](clientCert)
defer cl.Connection.Conn.Close()
2024-04-18 13:06:16 +01:00
certRequestPacket := protocol.NewRequestUserCertPacket(uid)
cl.Connection.Send(certRequestPacket)
certPacket := cl.Connection.Receive()
2024-04-18 13:06:16 +01:00
// TODO: Encrypt message
submitMessage(cl,uid,cipherContent)
case "askqueue":
cl := networking.NewClient[protocol.Packet]()
defer cl.Connection.Conn.Close()
case "getmsg":
if flag.NArg() < 2 {
panic("Insufficient arguments for 'getmsg' command. Usage: getmsg <NUM>")
}
num := flag.Arg(1)
cl := networking.NewClient[protocol.Packet]()
defer cl.Connection.Conn.Close()
case "help":
showHelp()
default:
2024-04-18 13:06:16 +01:00
commandError()
}
}
func submitMessage(cl networking.Client[protocol.Packet],uid string, content []byte) {
pack := protocol.NewSubmitMessage(uid,content)
cl.Connection.Send(pack)
2024-04-16 12:23:00 +01:00
}