mirror of
https://github.com/IllusionMan1212/gorc.git
synced 2026-10-24 02:12:33 +00:00
restructure app state, basic server connection using inputted user info and basic mainscreen
This commit is contained in:
+57
-21
@@ -20,57 +20,93 @@ import (
|
||||
"crypto/tls"
|
||||
"fmt"
|
||||
"log"
|
||||
"strings"
|
||||
|
||||
"github.com/illusionman1212/gorc/parser"
|
||||
)
|
||||
|
||||
type Client struct {
|
||||
// tcp connection
|
||||
conn *tls.Conn
|
||||
Conn *tls.Conn
|
||||
|
||||
// channel to receive data that comes through from the connection
|
||||
receive chan string
|
||||
Receive chan string
|
||||
}
|
||||
|
||||
func NewClient(host string, port string) *Client {
|
||||
// TODO: allow for insecure connections thru a checkbox (needed for irc servers that maybe don't have encryption on)
|
||||
const CRLF = "\r\n"
|
||||
|
||||
func NewClient(host string, port string, tlsEnabled bool) *Client {
|
||||
// TODO: distinguish between secure and insecure connections
|
||||
cfg := &tls.Config{ServerName: host}
|
||||
addr := fmt.Sprintf("%s:%s", host, port)
|
||||
|
||||
cfg := &tls.Config{ServerName: host}
|
||||
conn, err := tls.Dial("tcp", addr, cfg)
|
||||
if err != nil {
|
||||
// TODO: properly handle the error instead of Fatal-ing (failed to initiate a connection to server)
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
// TODO: read nickname and channel from user input (stdin ?? or TUI)
|
||||
conn.Write([]byte("CAP LS\r\n"))
|
||||
// conn.Write([]byte("PASS password"))
|
||||
conn.Write([]byte("NICK illusion434\r\n"))
|
||||
conn.Write([]byte("USER illusion434 0 * :illusion434\r\n"))
|
||||
// TODO: CAP REQ :whatever capability the client recognizes and supports
|
||||
conn.Write([]byte("CAP END\r\n"))
|
||||
conn.Write([]byte("JOIN ##programming\r\n"))
|
||||
|
||||
return &Client{
|
||||
conn: conn,
|
||||
receive: make(chan string),
|
||||
Conn: conn,
|
||||
Receive: make(chan string),
|
||||
}
|
||||
}
|
||||
|
||||
func (c Client) handleCommand(message IRCMessage) {
|
||||
func (c Client) Register(nick string, password string, channel string) {
|
||||
if c.Conn == nil {
|
||||
// TODO: properly handle the error instead of Fatal-ing
|
||||
log.Fatal("Attempted to write data to nil connection")
|
||||
}
|
||||
|
||||
c.sendCommand("CAP", "LS")
|
||||
if password != "" {
|
||||
c.sendCommand("PASS", password)
|
||||
}
|
||||
// TODO: check if nickname has spaces and remove them
|
||||
c.sendCommand("NICK", nick)
|
||||
c.sendCommand("USER", nick, "0", "*", nick)
|
||||
// TODO: CAP REQ :whatever capability the client recognizes and supports
|
||||
c.sendCommand("CAP", "END")
|
||||
c.sendCommand("JOIN", channel)
|
||||
}
|
||||
|
||||
func (c Client) handleCommand(message parser.IRCMessage) {
|
||||
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.conn.Write([]byte("PONG\r\n"))
|
||||
c.sendCommand("PONG")
|
||||
break
|
||||
}
|
||||
// TODO: handle all other commands
|
||||
}
|
||||
|
||||
func (c Client) sendCommand(cmd string, params ...string) {
|
||||
paramsString := ""
|
||||
if len(params) > 0 {
|
||||
paramsString = " " + strings.Join(params, " ")
|
||||
}
|
||||
|
||||
// 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 {
|
||||
i := strings.LastIndex(paramsString, " ")
|
||||
paramsString = paramsString[:i] + strings.Replace(paramsString[i:], " ", " :", 1)
|
||||
}
|
||||
|
||||
c.Conn.Write([]byte(cmd + paramsString + CRLF))
|
||||
}
|
||||
|
||||
func (c Client) Run() {
|
||||
for {
|
||||
select {
|
||||
case line := <-c.receive:
|
||||
messages := parseIRCMessage(line)
|
||||
c.handleCommand(messages)
|
||||
case line := <-c.Receive:
|
||||
message := parser.ParseIRCMessage(line)
|
||||
c.handleCommand(message)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user