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

View file

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

View file

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

Binary file not shown.