2024-04-16 09:02:23 +01:00
package client
2024-04-17 15:44:38 +01:00
import (
"bufio"
"fmt"
"os"
2024-04-20 17:16:52 +01:00
"strings"
2024-04-17 15:44:38 +01:00
)
2024-04-20 17:16:52 +01:00
func readStdin ( message string ) string {
fmt . Println ( message )
2024-04-17 15:44:38 +01:00
scanner := bufio . NewScanner ( os . Stdin )
scanner . Scan ( )
2024-04-17 19:54:14 +01:00
// FIX: make sure this doesnt die
return scanner . Text ( )
}
func AskUserPassword ( ) string {
2024-04-18 17:15:47 +01:00
fmt . Println ( "Enter key store password" )
2024-04-17 19:54:14 +01:00
scanner := bufio . NewScanner ( os . Stdin )
scanner . Scan ( )
// FIX: make sure this doesnt die
2024-04-17 15:44:38 +01:00
return scanner . Text ( )
}
2024-04-18 13:06:16 +01:00
func commandError ( ) {
2024-04-19 23:59:26 +01:00
fmt . Println ( "MSG SERVICE: command error!" )
showHelp ( )
2024-04-18 13:06:16 +01:00
}
2024-04-17 15:44:38 +01:00
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." )
}
2024-04-19 23:59:26 +01:00
2024-04-20 17:16:52 +01:00
func showMessagesInfo ( page int , numPages int , messages [ ] ClientMessageInfo ) int {
if messages == nil {
fmt . Println ( "No unread messages in the queue" )
return 0
}
2024-04-19 23:59:26 +01:00
for _ , message := range messages {
fmt . Printf ( "%v:%v:%v:%v\n" , message . Num , message . FromUID , message . Timestamp , message . Subject )
}
2024-04-20 17:16:52 +01:00
fmt . Printf ( "Page %v/%v\n" , page , numPages )
return messagesInfoPageNavigation ( page , numPages )
2024-04-19 23:59:26 +01:00
}
2024-04-20 17:16:52 +01:00
func messagesInfoPageNavigation ( page int , numPages int ) int {
var action string
switch page {
case 1 :
if page == numPages {
action = readStdin ( "Actions: quit" )
} 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
}
2024-04-19 23:59:26 +01:00
func showMessage ( message ClientMessage ) {
2024-04-20 17:16:52 +01:00
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 )
2024-04-19 23:59:26 +01:00
}
2024-04-20 17:16:52 +01:00