mirror of
https://github.com/IllusionMan1212/gorc.git
synced 2026-10-24 02:12:33 +00:00
feat: BIG changes
* use commands constants * use a separate goroutine to read from connection * refactor connection reading and ui updating * add sidepanel for users in channel * other cleanups
This commit is contained in:
+53
-16
@@ -24,12 +24,35 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Client struct {
|
||||
// tls connection
|
||||
TlsConn *tls.Conn
|
||||
type User struct {
|
||||
// Nickname of user
|
||||
Nick string
|
||||
}
|
||||
|
||||
type Channel struct {
|
||||
// Channel topic
|
||||
Topic string
|
||||
|
||||
// Channel messages history
|
||||
History string
|
||||
|
||||
// Users in this channel
|
||||
Users []User
|
||||
}
|
||||
|
||||
type Client struct {
|
||||
// tcp connection
|
||||
TcpConn *net.TCPConn
|
||||
TcpConn net.Conn
|
||||
|
||||
// Channel to receive quit msg
|
||||
Quit chan struct{}
|
||||
|
||||
Host string
|
||||
Port string
|
||||
Nickname string
|
||||
MOTD string // TODO:
|
||||
Channels map[string]Channel // TODO:
|
||||
ActiveChannel string
|
||||
}
|
||||
|
||||
const CRLF = "\r\n"
|
||||
@@ -46,8 +69,12 @@ func NewClient(host string, port string, tlsEnabled bool) Client {
|
||||
}
|
||||
|
||||
return Client{
|
||||
TlsConn: conn,
|
||||
TcpConn: nil,
|
||||
TcpConn: conn,
|
||||
Host: host,
|
||||
Port: port,
|
||||
Quit: make(chan struct{}),
|
||||
ActiveChannel: host,
|
||||
Channels: make(map[string]Channel),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -62,27 +89,39 @@ func NewClient(host string, port string, tlsEnabled bool) Client {
|
||||
}
|
||||
|
||||
return Client{
|
||||
TlsConn: nil,
|
||||
TcpConn: conn,
|
||||
TcpConn: conn,
|
||||
Host: host,
|
||||
Port: port,
|
||||
Quit: make(chan struct{}),
|
||||
ActiveChannel: host,
|
||||
Channels: make(map[string]Channel),
|
||||
}
|
||||
}
|
||||
|
||||
func (c Client) Register(nick string, password string, channel string) {
|
||||
func (c *Client) Register(nick string, password string, channel string) {
|
||||
c.SendCommand("CAP", "LS")
|
||||
if password != "" {
|
||||
c.SendCommand("PASS", password)
|
||||
}
|
||||
// TODO: check if nickname has spaces and remove them
|
||||
c.SendCommand("NICK", nick)
|
||||
c.Nickname = nick
|
||||
c.SendCommand("USER", nick, "0", "*", nick)
|
||||
// TODO: CAP REQ :whatever capability the client recognizes and supports
|
||||
c.SendCommand("CAP", "REQ", ":message-tags")
|
||||
c.SendCommand("CAP", "END")
|
||||
c.SendCommand("JOIN", channel)
|
||||
// joining a channel when registering is optional
|
||||
if channel != "" {
|
||||
c.SendCommand("JOIN", channel)
|
||||
c.ActiveChannel = channel
|
||||
c.Channels[c.ActiveChannel] = Channel{}
|
||||
} else {
|
||||
c.Channels[c.ActiveChannel] = Channel{}
|
||||
}
|
||||
}
|
||||
|
||||
func (c Client) SendCommand(cmd string, params ...string) {
|
||||
if c.TlsConn == nil && c.TcpConn == nil {
|
||||
if c.TcpConn == nil {
|
||||
// TODO: properly handle the error instead of Fatal-ing
|
||||
log.Fatal("Attempted to write data to nil connection")
|
||||
}
|
||||
@@ -103,8 +142,8 @@ func (c Client) SendCommand(cmd string, params ...string) {
|
||||
paramsString = " " + strings.Join(params[:len(params)-1], " ")
|
||||
paramsString += lastParam
|
||||
|
||||
// if we have exactly 1 param
|
||||
} else if len(params) == 1 {
|
||||
// if we have exactly 1 param and it's not empty
|
||||
} else if len(params) == 1 && params[0] != "" {
|
||||
// if this 1 param contains spaces, we prepend a colon
|
||||
if strings.Contains(params[0], " ") {
|
||||
paramsString = " :" + params[0]
|
||||
@@ -113,9 +152,7 @@ func (c Client) SendCommand(cmd string, params ...string) {
|
||||
}
|
||||
}
|
||||
|
||||
if c.TlsConn != nil {
|
||||
c.TlsConn.Write([]byte(cmd + paramsString + CRLF))
|
||||
} else if c.TcpConn != nil {
|
||||
if c.TcpConn != nil {
|
||||
c.TcpConn.Write([]byte(cmd + paramsString + CRLF))
|
||||
} else {
|
||||
// TODO: properly handle the error instead of Fatal-ing
|
||||
|
||||
Reference in New Issue
Block a user