Add history parameter to restore old chats

This commit is contained in:
raul 2024-05-07 10:48:16 +02:00
parent 9e1affdc6c
commit 158bf9373f
2 changed files with 48 additions and 8 deletions

View File

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

View File

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