From 5bb37a53feb7430e2e3966c195455924a3059125 Mon Sep 17 00:00:00 2001 From: raul Date: Tue, 14 May 2024 15:30:16 +0200 Subject: [PATCH] Add date formatting to messages I also went ahead and removed adding the user's IP to each one of its messages to retain the user's privacy and security. --- cmd/serverFunc.go | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/cmd/serverFunc.go b/cmd/serverFunc.go index 44857a8..74b679e 100644 --- a/cmd/serverFunc.go +++ b/cmd/serverFunc.go @@ -14,6 +14,7 @@ import ( "os" "os/exec" "strings" + "time" ) var ( @@ -69,6 +70,12 @@ func startSecureServer() (net.Listener, error) { return ln, err } +func getTime() string { + t := time.Now() + currentTime := fmt.Sprintf("%d-%02d-%02d | %02d:%02d:%02d", t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second()) + return currentTime +} + func Server() { var ln net.Listener var err error @@ -188,8 +195,8 @@ func handleConn(conn net.Conn, chatChan chan string) { newUserTemplate := new(User) newUser := newUserTemplate.CreateUser(userName, userIP) - joinMessage := fmt.Sprintf("%v has joined the chat!", newUser.Username) - fmt.Println(joinMessage) + joinMessage := fmt.Sprintf("[%v] %v has joined the chat!", getTime(), newUser.Username) + fmt.Println(joinMessage + " [" + newUser.IP + "]") addToLog(fmt.Sprintln(joinMessage)) //conn.Write([]byte(joinMessage)) sendMessage(joinMessage) @@ -198,8 +205,8 @@ func handleConn(conn net.Conn, chatChan chan string) { for { message, err := getUserInput(conn) if err != nil { - quitMessage := fmt.Sprintf("%v has disconnected!", newUser.Username) - fmt.Println(quitMessage) + quitMessage := fmt.Sprintf("[%v] %v has disconnected!", getTime(), newUser.Username) + fmt.Println(quitMessage + " [" + newUser.IP + "]") addToLog(fmt.Sprintln(quitMessage)) sendMessage(quitMessage) //removeFromList(chatChan) @@ -208,7 +215,7 @@ func handleConn(conn net.Conn, chatChan chan string) { // } return } - finalMessage := fmt.Sprintf("[%v] %v: %v", newUser.IP, newUser.Username, strings.TrimRight(message, "\n")) + finalMessage := fmt.Sprintf("[%v] %v: %v", getTime(), newUser.Username, strings.TrimRight(message, "\n")) fm := fmt.Sprintf("%v\n", finalMessage) fmt.Print(fm) addToLog(fm)