2021-10-20 03:13:57 +03:00
|
|
|
// gorc project
|
2021-11-14 06:31:51 +02:00
|
|
|
// Copyright (C) 2021 IllusionMan1212
|
2021-10-20 03:13:57 +03:00
|
|
|
//
|
|
|
|
|
// This program is free software; you can redistribute it and/or modify
|
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
|
// the Free Software Foundation; either version 3 of the License, or
|
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
//
|
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
|
//
|
|
|
|
|
// You should have received a copy of the GNU General Public License along
|
|
|
|
|
// with this program; if not, see https://www.gnu.org/licenses.
|
|
|
|
|
|
2021-11-13 06:38:12 +02:00
|
|
|
package client
|
2021-10-20 03:13:57 +03:00
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"crypto/tls"
|
|
|
|
|
"fmt"
|
|
|
|
|
"log"
|
2021-12-09 03:48:42 +02:00
|
|
|
"net"
|
2021-11-14 06:19:46 +02:00
|
|
|
"strings"
|
2021-10-20 03:13:57 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type Client struct {
|
2021-12-09 03:48:42 +02:00
|
|
|
// tls connection
|
|
|
|
|
TlsConn *tls.Conn
|
|
|
|
|
|
2021-10-20 03:13:57 +03:00
|
|
|
// tcp connection
|
2021-12-09 03:48:42 +02:00
|
|
|
TcpConn *net.TCPConn
|
2021-10-20 03:13:57 +03:00
|
|
|
}
|
|
|
|
|
|
2021-11-14 06:19:46 +02:00
|
|
|
const CRLF = "\r\n"
|
|
|
|
|
|
|
|
|
|
func NewClient(host string, port string, tlsEnabled bool) *Client {
|
2021-10-20 03:13:57 +03:00
|
|
|
// TODO: distinguish between secure and insecure connections
|
|
|
|
|
addr := fmt.Sprintf("%s:%s", host, port)
|
2021-11-14 06:19:46 +02:00
|
|
|
|
2021-12-09 03:48:42 +02:00
|
|
|
if tlsEnabled {
|
|
|
|
|
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)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return &Client{
|
|
|
|
|
TlsConn: conn,
|
|
|
|
|
TcpConn: nil,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
addrTCP, err := net.ResolveTCPAddr("tcp", addr)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
conn, err := net.DialTCP("tcp", nil, addrTCP)
|
2021-10-20 03:13:57 +03:00
|
|
|
if err != nil {
|
|
|
|
|
log.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return &Client{
|
2021-12-09 03:48:42 +02:00
|
|
|
TlsConn: nil,
|
|
|
|
|
TcpConn: conn,
|
2021-11-14 06:19:46 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c Client) Register(nick string, password string, channel string) {
|
2021-11-29 23:07:21 +02:00
|
|
|
c.SendCommand("CAP", "LS")
|
2021-11-14 06:19:46 +02:00
|
|
|
if password != "" {
|
2021-11-29 23:07:21 +02:00
|
|
|
c.SendCommand("PASS", password)
|
2021-11-14 06:19:46 +02:00
|
|
|
}
|
|
|
|
|
// TODO: check if nickname has spaces and remove them
|
2021-11-29 23:07:21 +02:00
|
|
|
c.SendCommand("NICK", nick)
|
|
|
|
|
c.SendCommand("USER", nick, "0", "*", nick)
|
2021-11-14 06:19:46 +02:00
|
|
|
// TODO: CAP REQ :whatever capability the client recognizes and supports
|
2021-12-09 05:14:20 +02:00
|
|
|
c.SendCommand("CAP", "REQ", ":message-tags")
|
2021-11-29 23:07:21 +02:00
|
|
|
c.SendCommand("CAP", "END")
|
|
|
|
|
c.SendCommand("JOIN", channel)
|
2021-10-20 03:13:57 +03:00
|
|
|
}
|
|
|
|
|
|
2021-11-29 23:07:21 +02:00
|
|
|
func (c Client) SendCommand(cmd string, params ...string) {
|
2021-12-09 03:48:42 +02:00
|
|
|
if c.TlsConn == nil && c.TcpConn == nil {
|
|
|
|
|
// TODO: properly handle the error instead of Fatal-ing
|
|
|
|
|
log.Fatal("Attempted to write data to nil connection")
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-14 06:19:46 +02:00
|
|
|
paramsString := ""
|
|
|
|
|
|
2022-01-03 23:59:52 +02:00
|
|
|
// if we have more than 1 param
|
2021-11-14 06:19:46 +02:00
|
|
|
if len(params) > 1 {
|
2022-01-03 23:59:52 +02:00
|
|
|
lastParam := params[len(params)-1]
|
|
|
|
|
// if the last param is a trailing param
|
|
|
|
|
// we prepend a colon to it
|
|
|
|
|
if strings.Contains(lastParam, " ") {
|
|
|
|
|
lastParam = " :" + lastParam
|
|
|
|
|
} else {
|
|
|
|
|
lastParam = " " + lastParam
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
paramsString = " " + strings.Join(params[:len(params)-1], " ")
|
|
|
|
|
paramsString += lastParam
|
|
|
|
|
|
|
|
|
|
// if we have exactly 1 param
|
|
|
|
|
} else if len(params) == 1 {
|
|
|
|
|
// if this 1 param contains spaces, we prepend a colon
|
|
|
|
|
if strings.Contains(params[0], " ") {
|
|
|
|
|
paramsString = " :" + params[0]
|
|
|
|
|
} else {
|
|
|
|
|
paramsString = " " + params[0]
|
|
|
|
|
}
|
2021-11-14 06:19:46 +02:00
|
|
|
}
|
|
|
|
|
|
2021-12-09 03:48:42 +02:00
|
|
|
if c.TlsConn != nil {
|
|
|
|
|
c.TlsConn.Write([]byte(cmd + paramsString + CRLF))
|
|
|
|
|
} else if c.TcpConn != nil {
|
|
|
|
|
c.TcpConn.Write([]byte(cmd + paramsString + CRLF))
|
|
|
|
|
} else {
|
|
|
|
|
// TODO: properly handle the error instead of Fatal-ing
|
|
|
|
|
log.Fatal("No valid connections to write to")
|
|
|
|
|
}
|
2021-11-14 06:19:46 +02:00
|
|
|
}
|