[PD1] Fix adding user cert to db, also finished logs

This commit is contained in:
Afonso Franco 2024-04-28 23:32:19 +01:00
parent f2118fe570
commit 4ea8315aed
Signed by: afonso
SSH key fingerprint: SHA256:aiLbdlPwXKJS5wMnghdtod0SPy8imZjlVvCyUX9DJNk
4 changed files with 24 additions and 16 deletions

View file

@ -8,6 +8,7 @@ import (
"errors"
"flag"
"log"
"os"
"sort"
"strconv"
)
@ -30,8 +31,10 @@ func Run() {
command := flag.Arg(0)
switch command {
case "send":
if flag.NArg() < 3 {
log.Fatalln("Insufficient arguments for 'send' command. Usage: send <UID> <SUBJECT>")
if flag.NArg() != 3 {
printError("MSG SERVICE: command error!")
showHelp()
os.Exit(1)
}
uid := flag.Arg(1)
plainSubject := flag.Arg(2)
@ -42,6 +45,11 @@ func Run() {
}
case "askqueue":
if flag.NArg() > 3 {
printError("MSG SERVICE: command error!")
showHelp()
os.Exit(1)
}
pageInput := flag.Arg(1)
page := 1
if pageInput != "" {
@ -64,7 +72,9 @@ func Run() {
case "getmsg":
if flag.NArg() < 2 {
log.Fatalln("Insufficient arguments for 'getmsg' command. Usage: getmsg <NUM>")
printError("MSG SERVICE: command error!")
showHelp()
os.Exit(1)
}
numString := flag.Arg(1)
num, err := strconv.Atoi(numString)
@ -73,14 +83,15 @@ func Run() {
}
err = getMsgCommand(clientKeyStore, num)
if err != nil {
log.Fatalln(err)
printError(err.Error())
}
case "help":
showHelp()
default:
commandError()
printError("MSG SERVICE: command error!")
showHelp()
}
}

View file

@ -3,7 +3,6 @@ package client
import (
"bufio"
"fmt"
"log"
"os"
"strings"
)
@ -15,9 +14,8 @@ func readStdin(message string) string {
return scanner.Text()
}
func commandError() {
fmt.Println("MSG SERVICE: command error!")
showHelp()
func printError(err string) {
fmt.Fprintln(os.Stderr, err)
}
func showHelp() {
@ -37,7 +35,7 @@ func showMessagesInfo(page int, numPages int, messages []ClientMessageInfo) int
for _, message := range messages {
if message.decryptError != nil {
fmt.Printf("ERROR: %v:%v:%v:", message.Num, message.FromUID, message.Timestamp)
log.Println(message.decryptError)
fmt.Println(message.decryptError)
} else {
fmt.Printf("%v:%v:%v:%v\n", message.Num, message.FromUID, message.Timestamp, message.Subject)
}
@ -52,7 +50,7 @@ func messagesInfoPageNavigation(page int, numPages int) int {
switch page {
case 1:
if page == numPages {
action = readStdin("Actions: quit")
return 0
} else {
action = readStdin("Actions: quit/next")
}

View file

@ -4,7 +4,6 @@ import (
"PD1/internal/protocol"
"crypto/x509"
"database/sql"
"errors"
"fmt"
"log"
"time"
@ -91,7 +90,7 @@ func (ds DataStore) GetMessage(toUID string, position int) protocol.Packet {
err := row.Scan(&serverMessage.FromUID, &serverMessage.ToUID, &serverMessage.Subject, &serverMessage.Body, &serverMessage.Timestamp)
if err == sql.ErrNoRows {
log.Printf("No message with NUM %v for UID %v\n", position, toUID)
errorMessage := fmt.Sprintf("No message with NUM %v", position)
errorMessage := fmt.Sprintf("MSG SERVICE: unknown message!")
return protocol.NewReportErrorPacket(errorMessage)
}
@ -216,8 +215,8 @@ func (ds DataStore) userExists(uid string) bool {
var count int
// Execute the SQL query
err := ds.db.QueryRow(query, uid).Scan(&count)
if err == sql.ErrNoRows {
log.Println("user with UID %v does not exist", uid)
if err != nil || count == 0 {
log.Printf("user with UID %v does not exist\n", uid)
return false
}
return true
@ -236,7 +235,7 @@ func (ds DataStore) storeUserCertIfNotExists(uid string, cert x509.Certificate)
`
_, err := ds.db.Exec(insertQuery, uid, cert.Raw)
if err != nil {
return errors.New(fmt.Sprintf("Error storing user certificate for UID %s: %v\n", uid, err))
return fmt.Errorf("Error storing user certificate for UID %s: %v\n", uid, err)
}
log.Printf("User certificate for UID %s stored successfully.\n", uid)
return nil

Binary file not shown.