2024-04-26 08:48:48 +02:00
|
|
|
/*
|
|
|
|
Copyright © 2024 Raul <raul@bulgariu.xyz>
|
|
|
|
*/
|
|
|
|
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"fmt"
|
2024-05-13 12:02:32 +02:00
|
|
|
"io"
|
2024-04-26 08:48:48 +02:00
|
|
|
"log"
|
|
|
|
"net"
|
|
|
|
"os"
|
2024-05-10 09:50:26 +02:00
|
|
|
"runtime"
|
2024-04-26 08:48:48 +02:00
|
|
|
"strings"
|
|
|
|
"time"
|
2024-05-10 09:50:26 +02:00
|
|
|
|
|
|
|
"github.com/jroimartin/gocui"
|
|
|
|
"github.com/nsf/termbox-go"
|
2024-04-26 08:48:48 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type Message struct {
|
|
|
|
Contents string
|
|
|
|
//Date time.Time
|
|
|
|
Server net.Conn
|
|
|
|
}
|
|
|
|
|
2024-05-06 13:45:06 +02:00
|
|
|
type ProfileData struct {
|
|
|
|
Username string
|
|
|
|
}
|
|
|
|
|
|
|
|
var Profile ProfileData
|
|
|
|
|
2024-04-26 08:48:48 +02:00
|
|
|
func (m Message) toSend() {
|
|
|
|
m.Server.Write([]byte(m.Contents))
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
serverPort string = "1302"
|
|
|
|
serverIP string
|
|
|
|
data Message
|
|
|
|
)
|
|
|
|
|
|
|
|
func Client() {
|
|
|
|
conn, err := net.Dial("tcp", serverIP+":"+serverPort)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Error occurred trying to connect to server: %v\n", err)
|
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
data.Server = conn
|
|
|
|
|
|
|
|
nameRequest, err := receiveMessage(conn)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Error occurred reading from server while requesting name: %v\n", err)
|
|
|
|
}
|
|
|
|
fmt.Print(nameRequest)
|
2024-05-13 12:02:32 +02:00
|
|
|
test := make([]byte, 2048)
|
|
|
|
copy(test, "Password: ")
|
|
|
|
|
|
|
|
if nameRequest == string(test) {
|
|
|
|
pass, err := scanLine()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
conn.Write([]byte(pass))
|
|
|
|
|
|
|
|
nameRequest, err := receiveMessage(conn)
|
|
|
|
if err != nil {
|
|
|
|
if err != io.EOF {
|
|
|
|
log.Fatalf("Error occurred reading from server while requesting name: %v\n", err)
|
|
|
|
}
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
fmt.Print(nameRequest)
|
|
|
|
sendName(conn)
|
|
|
|
} else {
|
|
|
|
sendName(conn)
|
|
|
|
}
|
2024-04-26 08:48:48 +02:00
|
|
|
GUI()
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////
|
|
|
|
// REFACTORING BELOW //
|
|
|
|
////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
func listenMessages(g *gocui.Gui) {
|
|
|
|
time.Sleep(time.Millisecond * 250)
|
|
|
|
chatbox, err := g.View("chatbox")
|
|
|
|
if err != nil {
|
|
|
|
log.Panicln(err)
|
|
|
|
}
|
|
|
|
for {
|
|
|
|
messageFromServer, err := receiveMessage(data.Server)
|
|
|
|
if err != nil {
|
|
|
|
// Avoid triggering an error if client quits the client
|
|
|
|
if err == gocui.ErrQuit {
|
|
|
|
g.Close()
|
|
|
|
} else {
|
|
|
|
g.Close()
|
|
|
|
log.Fatalf("Error occurred reading from server: %v\n", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
formattedMessage := strings.TrimRight(messageFromServer, "\n")
|
|
|
|
fmt.Fprintln(chatbox, formattedMessage)
|
|
|
|
termbox.Interrupt()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func receiveMessage(conn net.Conn) (s string, err error) {
|
|
|
|
serverMessage := make([]byte, 2048)
|
|
|
|
if _, err := conn.Read(serverMessage); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
finalMessage := string(serverMessage)
|
|
|
|
return finalMessage, nil
|
|
|
|
}
|
|
|
|
|
2024-05-10 09:50:26 +02:00
|
|
|
func scanLine() (line string, err error) {
|
|
|
|
switch runtime.GOOS {
|
|
|
|
case "linux":
|
|
|
|
message, err := bufio.NewReader(os.Stdin).ReadString('\n')
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
line = message
|
|
|
|
case "windows":
|
|
|
|
message, err := bufio.NewReader(os.Stdin).ReadString('\r')
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
line = message
|
|
|
|
}
|
|
|
|
return line, nil
|
|
|
|
}
|
2024-04-26 08:48:48 +02:00
|
|
|
|
2024-05-10 09:50:26 +02:00
|
|
|
func sendName(conn net.Conn) {
|
|
|
|
message, err := scanLine()
|
2024-04-26 08:48:48 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Error occurred sending message to server: %v\n", err)
|
|
|
|
}
|
2024-05-06 13:45:06 +02:00
|
|
|
Profile.Username = strings.TrimRight(message, "\n")
|
|
|
|
|
2024-04-26 08:48:48 +02:00
|
|
|
if _, err := conn.Write([]byte(message)); err != nil {
|
|
|
|
log.Fatalf("Error occurred writing to server: %v\n", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func GUI() {
|
|
|
|
g, err := gocui.NewGui(gocui.OutputNormal)
|
|
|
|
if err != nil {
|
|
|
|
log.Panicln(err)
|
|
|
|
}
|
|
|
|
defer g.Close()
|
|
|
|
g.SetManagerFunc(layout)
|
|
|
|
g.Mouse = true
|
|
|
|
g.Cursor = true
|
|
|
|
initKeybindings(g)
|
|
|
|
|
|
|
|
go listenMessages(g)
|
|
|
|
|
|
|
|
if err := g.MainLoop(); err != nil && err != gocui.ErrQuit {
|
|
|
|
log.Panicln(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func quit(*gocui.Gui, *gocui.View) error {
|
|
|
|
return gocui.ErrQuit
|
|
|
|
}
|
|
|
|
|
|
|
|
func sendToServer(g *gocui.Gui, v *gocui.View) error {
|
|
|
|
textarea, err := g.View("textarea")
|
|
|
|
if err != nil {
|
|
|
|
log.Panicln(err)
|
|
|
|
}
|
|
|
|
message := textarea.Buffer()
|
|
|
|
msg := string(message)
|
|
|
|
if msg == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if msg == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
data.Contents = msg
|
|
|
|
data.toSend()
|
|
|
|
|
|
|
|
textarea.Clear()
|
|
|
|
textarea.SetCursor(0, 0)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-05-06 13:31:44 +02:00
|
|
|
func scrollView(v *gocui.View, dy int) error {
|
|
|
|
if v != nil {
|
|
|
|
v.Autoscroll = false
|
|
|
|
ox, oy := v.Origin()
|
|
|
|
if err := v.SetOrigin(ox, oy+dy); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-05-06 14:09:47 +02:00
|
|
|
func autoscroll(g *gocui.Gui, v *gocui.View) error {
|
|
|
|
chatbox, err := g.View("chatbox")
|
|
|
|
if err != nil {
|
|
|
|
log.Panicf("Error happened setting autoscroll: %v\n", err)
|
|
|
|
}
|
|
|
|
chatbox.Autoscroll = true
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-04-26 08:48:48 +02:00
|
|
|
func initKeybindings(g *gocui.Gui) error {
|
|
|
|
if err := g.SetKeybinding("", gocui.KeyCtrlC, gocui.ModNone, quit); err != nil {
|
|
|
|
log.Panicln(err)
|
|
|
|
}
|
|
|
|
|
2024-05-06 13:34:29 +02:00
|
|
|
// if err := g.SetKeybinding("button", gocui.MouseLeft, gocui.ModNone, sendToServer); err != nil {
|
|
|
|
// log.Panicln(err)
|
|
|
|
// }
|
2024-04-26 08:48:48 +02:00
|
|
|
|
2024-05-06 14:09:47 +02:00
|
|
|
if err := g.SetKeybinding("textarea", gocui.KeyCtrlA, gocui.ModNone, autoscroll); err != nil {
|
|
|
|
log.Panicln(err)
|
|
|
|
}
|
|
|
|
|
2024-04-26 08:48:48 +02:00
|
|
|
if err := g.SetKeybinding("textarea", gocui.KeyEnter, gocui.ModNone, sendToServer); err != nil {
|
|
|
|
log.Panicln(err)
|
|
|
|
}
|
2024-05-06 13:31:44 +02:00
|
|
|
|
|
|
|
if err := g.SetKeybinding("chatbox", gocui.MouseWheelUp, gocui.ModNone,
|
|
|
|
func(g *gocui.Gui, v *gocui.View) error {
|
|
|
|
scrollView(v, -1)
|
|
|
|
return nil
|
|
|
|
}); err != nil {
|
|
|
|
log.Panicln(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := g.SetKeybinding("chatbox", gocui.MouseWheelDown, gocui.ModNone,
|
|
|
|
func(g *gocui.Gui, v *gocui.View) error {
|
|
|
|
scrollView(v, 1)
|
|
|
|
return nil
|
|
|
|
}); err != nil {
|
|
|
|
log.Panicln(err)
|
|
|
|
}
|
|
|
|
|
2024-04-26 08:48:48 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func layout(g *gocui.Gui) error {
|
|
|
|
maxX, maxY := g.Size()
|
|
|
|
|
2024-05-07 09:49:34 +02:00
|
|
|
if chatbox, err := g.SetView("chatbox", 2, 1, maxX-2, maxY-5); err != nil {
|
2024-04-26 08:48:48 +02:00
|
|
|
if err != gocui.ErrUnknownView {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
chatbox.Autoscroll = true
|
2024-05-06 13:45:06 +02:00
|
|
|
chatbox.Title = "Chat Box (Find source at https://git.bulgariu.xyz/raul/mini-chat!)"
|
2024-04-26 08:48:48 +02:00
|
|
|
}
|
|
|
|
|
2024-05-06 13:34:29 +02:00
|
|
|
// if button, err := g.SetView("button", maxX/2+32, maxY-4, maxX-28, maxY-2); err != nil {
|
|
|
|
// if err != gocui.ErrUnknownView {
|
|
|
|
// return err
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// button.Wrap = true
|
|
|
|
// button.Frame = true
|
|
|
|
// fmt.Fprintln(button, "Send it")
|
|
|
|
// }
|
2024-04-26 08:48:48 +02:00
|
|
|
|
2024-05-07 09:49:34 +02:00
|
|
|
if textarea, err := g.SetView("textarea", 2, maxY-4, maxX-2, maxY-2); err != nil {
|
2024-04-26 08:48:48 +02:00
|
|
|
if err != gocui.ErrUnknownView {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if _, err := g.SetCurrentView("textarea"); err != nil {
|
|
|
|
log.Panicln(err)
|
|
|
|
}
|
2024-05-06 14:09:47 +02:00
|
|
|
textarea.Title = "(" + Profile.Username + ") " + "Send message" + " (Ctrl+A: Autoscroll)"
|
2024-04-26 08:48:48 +02:00
|
|
|
textarea.Wrap = true
|
|
|
|
textarea.Editable = true
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|