[PD2] Server done?
Co-authored-by: tsousa111 <tiagao2001@hotmail.com>
This commit is contained in:
parent
08a73a4f76
commit
49a29e43a7
66 changed files with 2777 additions and 5 deletions
92
Projs/PD2/internal/client/interface.go
Normal file
92
Projs/PD2/internal/client/interface.go
Normal file
|
@ -0,0 +1,92 @@
|
|||
package client
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func readStdin(message string) string {
|
||||
fmt.Println(message)
|
||||
scanner := bufio.NewScanner(os.Stdin)
|
||||
scanner.Scan()
|
||||
return scanner.Text()
|
||||
}
|
||||
|
||||
func printError(err string) {
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
}
|
||||
|
||||
func showHelp() {
|
||||
fmt.Println("Comandos da aplicação cliente:")
|
||||
fmt.Println("-user <FNAME>: Especifica o ficheiro com dados do utilizador. Por omissão, será assumido que esse ficheiro é userdata.p12.")
|
||||
fmt.Println("send <UID> <SUBJECT>: Envia uma mensagem com assunto <SUBJECT> destinada ao utilizador com identificador <UID>. O conteúdo da mensagem será lido do stdin, e o tamanho deve ser limitado a 1000 bytes.")
|
||||
fmt.Println("askqueue: Solicita ao servidor que lhe envie a lista de mensagens não lidas da queue do utilizador.")
|
||||
fmt.Println("getmsg <NUM>: Solicita ao servidor o envio da mensagem da sua queue com número <NUM>.")
|
||||
fmt.Println("help: Imprime instruções de uso do programa.")
|
||||
}
|
||||
|
||||
func showMessagesInfo(page int, numPages int, messages []ClientMessageInfo) int {
|
||||
if messages == nil {
|
||||
fmt.Println("No unread messages in the queue")
|
||||
return 0
|
||||
}
|
||||
for _, message := range messages {
|
||||
if message.decryptError != nil {
|
||||
fmt.Printf("ERROR: %v:%v:%v:", message.Num, message.FromUID, message.Timestamp)
|
||||
fmt.Println(message.decryptError)
|
||||
} else {
|
||||
fmt.Printf("%v:%v:%v:%v\n", message.Num, message.FromUID, message.Timestamp, message.Subject)
|
||||
}
|
||||
}
|
||||
fmt.Printf("Page %v/%v\n", page, numPages)
|
||||
return messagesInfoPageNavigation(page, numPages)
|
||||
}
|
||||
|
||||
func messagesInfoPageNavigation(page int, numPages int) int {
|
||||
var action string
|
||||
|
||||
switch page {
|
||||
case 1:
|
||||
if page == numPages {
|
||||
return 0
|
||||
} else {
|
||||
action = readStdin("Actions: quit/next")
|
||||
}
|
||||
case numPages:
|
||||
action = readStdin("Actions: prev/quit")
|
||||
default:
|
||||
action = readStdin("prev/quit/next")
|
||||
}
|
||||
|
||||
switch strings.ToLower(action) {
|
||||
case "prev":
|
||||
if page == 1 {
|
||||
fmt.Println("Unavailable action: Already in first page")
|
||||
messagesInfoPageNavigation(page, numPages)
|
||||
} else {
|
||||
return -1
|
||||
}
|
||||
case "quit":
|
||||
return 0
|
||||
case "next":
|
||||
if page == numPages {
|
||||
fmt.Println("Unavailable action: Already in last page")
|
||||
messagesInfoPageNavigation(page, numPages)
|
||||
} else {
|
||||
return 1
|
||||
}
|
||||
default:
|
||||
fmt.Println("Unknown action")
|
||||
messagesInfoPageNavigation(page, numPages)
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func showMessage(message ClientMessage) {
|
||||
fmt.Printf("From: %s\n", message.FromUID)
|
||||
fmt.Printf("To: %s\n", message.ToUID)
|
||||
fmt.Printf("Subject: %s\n", message.Subject)
|
||||
fmt.Printf("Body: %s\n", message.Body)
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue