Quality of Life update #4

Merged
raul merged 10 commits from testing into main 2024-05-08 08:04:46 +02:00
2 changed files with 48 additions and 8 deletions
Showing only changes of commit 158bf9373f - Show all commits

View File

@ -37,6 +37,7 @@ func init() {
// and all subcommands, e.g.:
// serverCmd.PersistentFlags().String("foo", "", "A help for foo")
serverCmd.PersistentFlags().String("port", "1302", "port to use for listening")
serverCmd.PersistentFlags().String("history", "", "File to store and recover chat history from")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
@ -51,5 +52,13 @@ func setServerParameters(cmd *cobra.Command) error {
if parameterPort != "" {
listenPort = parameterPort
}
parameterHistory, err := cmd.Flags().GetString("history")
if err != nil {
return err
}
if parameterHistory != "" {
logLocation = parameterHistory
isLogging = true
}
return nil
}

View File

@ -9,17 +9,17 @@ import (
"fmt"
"log"
"net"
"os"
"strings"
)
var (
listenPort string = "1302"
isLogging bool = false
logLocation string
listenerList []chan string
)
type Creator interface {
CreateUser()
}
type User struct {
Username string
IP string
@ -49,8 +49,6 @@ func Server() {
}
}
var listenerList []chan string
func getUsername(conn net.Conn) (s string, err error) {
conn.Write([]byte("What's your name?\nChoice: "))
name, err := bufio.NewReader(conn).ReadString('\n')
@ -78,6 +76,22 @@ func removeFromList(chatChan chan string) {
}
}
func populateChat(conn net.Conn) {
if isLogging == false {
return
}
file, err := os.Open(logLocation)
if err != nil {
log.Printf("Error opening file for populating: %v\n", err)
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
conn.Write([]byte(fmt.Sprintln(scanner.Text())))
}
}
func handleConn(conn net.Conn, chatChan chan string) {
defer conn.Close()
@ -94,11 +108,13 @@ func handleConn(conn net.Conn, chatChan chan string) {
}
userIP := getIP(conn)
//////////////////////////////////
populateChat(conn)
newUserTemplate := new(User)
newUser := newUserTemplate.CreateUser(userName, userIP)
joinMessage := fmt.Sprintf("%v has joined the chat!", newUser.Username)
fmt.Println(joinMessage)
addToLog(fmt.Sprintln(joinMessage))
//conn.Write([]byte(joinMessage))
sendMessage(joinMessage)
@ -108,6 +124,7 @@ func handleConn(conn net.Conn, chatChan chan string) {
if err != nil {
quitMessage := fmt.Sprintf("%v has disconnected!", newUser.Username)
fmt.Println(quitMessage)
addToLog(fmt.Sprintln(quitMessage))
sendMessage(quitMessage)
//removeFromList(chatChan)
// if _, err := conn.Write([]byte(quitMessage)); err != nil {
@ -116,7 +133,9 @@ func handleConn(conn net.Conn, chatChan chan string) {
return
}
finalMessage := fmt.Sprintf("[%v] %v: %v", newUser.IP, newUser.Username, strings.TrimRight(message, "\n"))
fmt.Printf("%v\n", finalMessage)
fm := fmt.Sprintf("%v\n", finalMessage)
fmt.Print(fm)
addToLog(fm)
sendMessage(finalMessage)
//chatChan <- finalMessage
@ -148,3 +167,15 @@ func getIP(conn net.Conn) (IP string) {
}
return IP
}
func addToLog(s string) {
if isLogging == false {
return
}
file, err := os.OpenFile(logLocation, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0640)
if err != nil {
log.Printf("Error occurred: %v\n", err)
}
defer file.Close()
file.WriteString(s)
}