refactor: move some of client network logic into mainscreen

This commit is contained in:
2021-11-29 23:07:21 +02:00
parent 57ad235db7
commit 35ab2cf927
3 changed files with 20 additions and 42 deletions
+10 -39
View File
@@ -21,16 +21,11 @@ import (
"fmt" "fmt"
"log" "log"
"strings" "strings"
"github.com/illusionman1212/gorc/parser"
) )
type Client struct { type Client struct {
// tcp connection // tcp connection
Conn *tls.Conn Conn *tls.Conn
// channel to receive data that comes through from the connection
Receive chan string
} }
const CRLF = "\r\n" const CRLF = "\r\n"
@@ -48,7 +43,6 @@ func NewClient(host string, port string, tlsEnabled bool) *Client {
return &Client{ return &Client{
Conn: conn, Conn: conn,
Receive: make(chan string),
} }
} }
@@ -58,55 +52,32 @@ func (c Client) Register(nick string, password string, channel string) {
log.Fatal("Attempted to write data to nil connection") log.Fatal("Attempted to write data to nil connection")
} }
c.sendCommand("CAP", "LS") c.SendCommand("CAP", "LS")
if password != "" { if password != "" {
c.sendCommand("PASS", password) c.SendCommand("PASS", password)
} }
// TODO: check if nickname has spaces and remove them // TODO: check if nickname has spaces and remove them
c.sendCommand("NICK", nick) c.SendCommand("NICK", nick)
c.sendCommand("USER", nick, "0", "*", nick) c.SendCommand("USER", nick, "0", "*", nick)
// TODO: CAP REQ :whatever capability the client recognizes and supports // TODO: CAP REQ :whatever capability the client recognizes and supports
c.sendCommand("CAP", "END") c.SendCommand("CAP", "END")
c.sendCommand("JOIN", channel) c.SendCommand("JOIN", channel)
} }
func (c Client) handleCommand(message parser.IRCMessage) { func (c Client) SendCommand(cmd string, params ...string) {
fmt.Printf("Tags: %s\n", message.Tags)
fmt.Printf("Source: %s\n", message.Source)
fmt.Printf("Command: %s\n", message.Command)
fmt.Printf("Params: %s\n", message.Parameters)
switch message.Command {
case "PING":
c.sendCommand("PONG")
break
}
// TODO: handle all other commands
}
func (c Client) sendCommand(cmd string, params ...string) {
paramsString := "" paramsString := ""
if len(params) > 0 { if len(params) > 0 {
paramsString = " " + strings.Join(params, " ") paramsString = " " + strings.Join(params, " ")
} }
// if we have more than 1 param then replace the last param's space with a " :" // if we have more than 1 param then replace the last param's space with a " :"
// NOTE: not sure if we should only check if we have more than 1 param
// cuz one param could have spaces and would require a colon
if len(params) > 1 { if len(params) > 1 {
i := strings.LastIndex(paramsString, " ") i := strings.LastIndex(paramsString, " ")
paramsString = paramsString[:i] + strings.Replace(paramsString[i:], " ", " :", 1) paramsString = paramsString[:i] + strings.Replace(paramsString[i:], " ", " :", 1)
// if we have more exactly 1 param and it contains spaces, we prepend colon to the param
} else if len(params) == 1 && strings.Contains(params[0], " ") {
paramsString = ":" + paramsString
} }
c.Conn.Write([]byte(cmd + paramsString + CRLF)) c.Conn.Write([]byte(cmd + paramsString + CRLF))
} }
func (c Client) Run() {
for {
select {
case line := <-c.Receive:
message := parser.ParseIRCMessage(line)
c.handleCommand(message)
}
}
}
-2
View File
@@ -29,8 +29,6 @@ const ( // do we need this as a client ????
COMMA = "," // 0x2C COMMA = "," // 0x2C
) )
const MESSAGE_SEPARATOR = "\r\n" // CRLF (0x0D0A)
/* --- COMMANDS --- */ /* --- COMMANDS --- */
const ( const (
// Connection messages // Connection messages
+9
View File
@@ -22,12 +22,14 @@ import (
"strings" "strings"
tea "github.com/charmbracelet/bubbletea" tea "github.com/charmbracelet/bubbletea"
"github.com/illusionman1212/gorc/client"
"github.com/illusionman1212/gorc/parser" "github.com/illusionman1212/gorc/parser"
) )
type State struct { type State struct {
content string content string
Reader *bufio.Reader Reader *bufio.Reader
Client *client.Client
} }
func (s State) Update(msg tea.Msg) (State, tea.Cmd) { func (s State) Update(msg tea.Msg) (State, tea.Cmd) {
@@ -39,6 +41,13 @@ func (s State) Update(msg tea.Msg) (State, tea.Cmd) {
fullMsg := fmt.Sprintf("%s %s %s", message.Source, message.Command, strings.Join(message.Parameters, " ")) fullMsg := fmt.Sprintf("%s %s %s", message.Source, message.Command, strings.Join(message.Parameters, " "))
s.content += fullMsg s.content += fullMsg
// TODO: handle different commands
switch message.Command {
case "PING":
s.Client.SendCommand("PONG")
break
}
return s, s.readFromServer return s, s.readFromServer
} }