2021-10-20 03:13:57 +03:00
|
|
|
// gorc project
|
2022-01-24 04:24:58 +02:00
|
|
|
// Copyright (C) 2022 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.
|
|
|
|
|
|
2022-01-24 02:26:24 +02:00
|
|
|
package irc
|
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"
|
2022-02-19 16:24:39 +02:00
|
|
|
"time"
|
2022-01-15 21:56:06 +02:00
|
|
|
|
|
|
|
|
tea "github.com/charmbracelet/bubbletea"
|
2022-02-19 16:24:39 +02:00
|
|
|
"github.com/charmbracelet/lipgloss"
|
|
|
|
|
"github.com/illusionman1212/gorc/irc/commands"
|
|
|
|
|
"github.com/illusionman1212/gorc/ui"
|
2021-10-20 03:13:57 +03:00
|
|
|
)
|
|
|
|
|
|
2025-08-13 13:05:15 +02:00
|
|
|
type Message struct {
|
|
|
|
|
Timestamp string
|
|
|
|
|
Tags MessageTags // starts with @ | Optional
|
|
|
|
|
Source string // starts with : | Optional
|
|
|
|
|
Command string // can either be a string or a numeric value | Required
|
|
|
|
|
Parameters []string // Optional (Dependant on command)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type MessageTags map[string]string
|
|
|
|
|
|
2022-01-09 04:30:22 +02:00
|
|
|
type User struct {
|
2022-01-15 21:56:06 +02:00
|
|
|
// User prefix in channel
|
|
|
|
|
Prefix string
|
2022-01-09 04:30:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type Channel struct {
|
2022-01-20 05:08:39 +02:00
|
|
|
// Channel name
|
|
|
|
|
Name string
|
|
|
|
|
|
2022-01-09 04:30:22 +02:00
|
|
|
// Channel topic
|
|
|
|
|
Topic string
|
|
|
|
|
|
|
|
|
|
// Channel messages history
|
|
|
|
|
History string
|
|
|
|
|
|
|
|
|
|
// Users in this channel
|
2022-01-15 21:56:06 +02:00
|
|
|
// The map key is the user's nickname
|
|
|
|
|
// and the user struct holds data about that user
|
|
|
|
|
// such as, prefixes in this channel and etc...
|
|
|
|
|
Users map[string]User
|
2022-01-09 04:30:22 +02:00
|
|
|
}
|
2021-12-09 03:48:42 +02:00
|
|
|
|
2022-01-09 04:30:22 +02:00
|
|
|
type Client struct {
|
2021-10-20 03:13:57 +03:00
|
|
|
// tcp connection
|
2025-08-11 22:51:50 +02:00
|
|
|
TCPConn net.Conn
|
2022-01-09 04:30:22 +02:00
|
|
|
|
2022-01-20 05:08:39 +02:00
|
|
|
// Host that client is connected to
|
|
|
|
|
Host string
|
|
|
|
|
|
|
|
|
|
// Port that client is connected to
|
|
|
|
|
Port string
|
|
|
|
|
|
|
|
|
|
// Nickname currently in use by the user
|
|
|
|
|
Nickname string
|
|
|
|
|
|
|
|
|
|
// Joined channels
|
|
|
|
|
Channels []Channel
|
|
|
|
|
|
|
|
|
|
// Active channel name
|
2022-01-09 04:30:22 +02:00
|
|
|
ActiveChannel string
|
2022-01-20 05:08:39 +02:00
|
|
|
|
|
|
|
|
// Active channel index
|
2022-01-21 03:23:02 +02:00
|
|
|
ActiveChannelIndex int
|
2022-01-20 05:08:39 +02:00
|
|
|
|
2025-08-15 22:18:55 +02:00
|
|
|
// The channel to join immediately after registration completes.
|
|
|
|
|
InitialChannel string
|
|
|
|
|
|
2022-01-20 05:08:39 +02:00
|
|
|
// Reference to the bubbletea program
|
|
|
|
|
Tea *tea.Program
|
2022-01-24 04:23:02 +02:00
|
|
|
|
|
|
|
|
// Index of the first visible tab in the tab bar
|
|
|
|
|
FirstTabIndexInTabBar int
|
|
|
|
|
|
|
|
|
|
// Index of the last visible tab in the tab bar
|
|
|
|
|
LastTabIndexInTabBar int
|
2023-02-22 08:52:26 +02:00
|
|
|
|
|
|
|
|
// The acknowledged capabilities
|
|
|
|
|
EnabledCapabilities Capabilities
|
2025-08-15 21:10:52 +02:00
|
|
|
|
|
|
|
|
// The features currently enabled for this client
|
|
|
|
|
EnabledFeatures Features
|
2021-10-20 03:13:57 +03:00
|
|
|
}
|
|
|
|
|
|
2023-02-22 08:52:26 +02:00
|
|
|
type Capabilities map[string]string
|
2025-08-15 21:10:52 +02:00
|
|
|
type Features map[string]string
|
2023-02-22 08:52:26 +02:00
|
|
|
|
2022-02-19 16:24:39 +02:00
|
|
|
type MsgFmtOpts struct {
|
|
|
|
|
WithTimestamp bool
|
2025-08-15 20:18:24 +02:00
|
|
|
// TODO: make these a bitset? they should compliment eachother instead of conflicting or rendering twice.
|
|
|
|
|
AsServerMsg bool
|
|
|
|
|
AsErrorMsg bool
|
|
|
|
|
|
|
|
|
|
NotImpl bool
|
|
|
|
|
AsDate bool
|
2022-02-19 16:24:39 +02:00
|
|
|
}
|
|
|
|
|
|
2025-08-15 20:18:24 +02:00
|
|
|
var unimpl = lipgloss.NewStyle().Foreground(ui.ErrorColor).Render("[UNIMPL]")
|
2022-02-19 16:24:39 +02:00
|
|
|
var serverMsgStyle = lipgloss.NewStyle().Foreground(ui.ServerMsgColor)
|
2025-08-15 20:18:24 +02:00
|
|
|
var errorMsgStyle = lipgloss.NewStyle().Foreground(ui.ErrorColor)
|
2025-08-11 22:51:50 +02:00
|
|
|
var timestampStyle = serverMsgStyle
|
2022-02-19 16:24:39 +02:00
|
|
|
var dateStyle = lipgloss.NewStyle().Foreground(ui.DateColor)
|
|
|
|
|
|
2021-11-14 06:19:46 +02:00
|
|
|
const CRLF = "\r\n"
|
|
|
|
|
|
2025-08-13 13:05:15 +02:00
|
|
|
func (m *Message) SetTimestamp() {
|
|
|
|
|
if serverTime, ok := m.Tags["time"]; ok {
|
|
|
|
|
t, err := time.Parse("2006-01-02T15:04:05.000Z", serverTime)
|
|
|
|
|
if err != nil {
|
|
|
|
|
// TODO: properly handle error
|
|
|
|
|
log.Fatalln(err)
|
|
|
|
|
}
|
|
|
|
|
m.Timestamp = fmt.Sprintf("[%02d:%02d]", t.Local().Hour(), t.Local().Minute())
|
|
|
|
|
} else {
|
|
|
|
|
now := time.Now()
|
|
|
|
|
m.Timestamp = fmt.Sprintf("[%02d:%02d]", now.Hour(), now.Minute())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-19 16:24:39 +02:00
|
|
|
func (c *Channel) AppendMsg(timestamp string, fullMsg string, opts MsgFmtOpts) {
|
|
|
|
|
prefixes := ""
|
|
|
|
|
style := ui.DefaultStyle
|
|
|
|
|
|
|
|
|
|
if opts.WithTimestamp {
|
|
|
|
|
prefixes += timestampStyle.Render(timestamp) + " "
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if opts.NotImpl {
|
2025-08-15 20:18:24 +02:00
|
|
|
prefixes += unimpl + " "
|
2022-02-19 16:24:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if opts.AsServerMsg {
|
|
|
|
|
prefixes += serverMsgStyle.Render("==") + " "
|
|
|
|
|
style = serverMsgStyle
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-15 20:18:24 +02:00
|
|
|
if opts.AsErrorMsg {
|
|
|
|
|
prefixes += errorMsgStyle.Render("==") + " "
|
|
|
|
|
style = errorMsgStyle
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-19 16:24:39 +02:00
|
|
|
if opts.AsDate {
|
|
|
|
|
style = dateStyle
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
c.History += prefixes + style.Render(fullMsg) + CRLF
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-11 22:51:50 +02:00
|
|
|
func (c *Client) Initialize(host string, port string, tlsEnabled bool) {
|
2021-10-20 03:13:57 +03:00
|
|
|
addr := fmt.Sprintf("%s:%s", host, port)
|
2021-11-14 06:19:46 +02:00
|
|
|
|
2025-08-15 20:18:24 +02:00
|
|
|
c.Host = host
|
|
|
|
|
c.Port = port
|
|
|
|
|
c.ActiveChannel = host
|
|
|
|
|
c.Channels = make([]Channel, 0)
|
|
|
|
|
c.EnabledCapabilities = make(Capabilities, 0)
|
|
|
|
|
c.EnabledFeatures = make(Features, 0)
|
|
|
|
|
|
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)
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-11 22:51:50 +02:00
|
|
|
c.TCPConn = conn
|
2022-01-15 21:56:06 +02:00
|
|
|
return
|
2021-12-09 03:48:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-11 22:51:50 +02:00
|
|
|
c.TCPConn = conn
|
2021-11-14 06:19:46 +02:00
|
|
|
}
|
|
|
|
|
|
2022-01-09 04:30:22 +02:00
|
|
|
func (c *Client) Register(nick string, password string, channel string) {
|
2022-01-20 05:08:39 +02:00
|
|
|
c.Channels = append(c.Channels, Channel{
|
|
|
|
|
Name: c.Host,
|
2022-01-15 22:36:34 +02:00
|
|
|
Users: make(map[string]User),
|
2022-01-20 05:08:39 +02:00
|
|
|
})
|
2022-01-15 22:36:34 +02:00
|
|
|
|
2023-02-22 08:52:26 +02:00
|
|
|
c.SendCommand(commands.CAP, "LS", "302")
|
2021-11-14 06:19:46 +02:00
|
|
|
if password != "" {
|
2022-02-19 16:24:39 +02:00
|
|
|
c.SendCommand(commands.PASS, password)
|
2021-11-14 06:19:46 +02:00
|
|
|
}
|
2022-02-19 16:24:39 +02:00
|
|
|
c.SendCommand(commands.NICK, nick)
|
|
|
|
|
// set user-wanted nickname
|
2022-01-09 04:30:22 +02:00
|
|
|
c.Nickname = nick
|
2022-02-19 16:24:39 +02:00
|
|
|
c.SendCommand(commands.USER, nick, "0", "*", nick)
|
2021-10-20 03:13:57 +03:00
|
|
|
}
|
|
|
|
|
|
2022-02-19 16:24:39 +02:00
|
|
|
func (c *Client) SetDay() {
|
|
|
|
|
msgOpts := MsgFmtOpts{
|
|
|
|
|
AsDate: true,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
now := time.Now()
|
|
|
|
|
msg := fmt.Sprintf("————— %s %d —————", now.Month().String(), now.Day())
|
|
|
|
|
c.Channels[0].AppendMsg("", msg, msgOpts)
|
|
|
|
|
|
|
|
|
|
// TODO: append new day to each channel whenever the day changes
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-29 23:07:21 +02:00
|
|
|
func (c Client) SendCommand(cmd string, params ...string) {
|
2025-08-11 22:51:50 +02:00
|
|
|
if c.TCPConn == nil {
|
2021-12-09 03:48:42 +02:00
|
|
|
// 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
|
|
|
|
|
|
2022-01-09 04:30:22 +02:00
|
|
|
// if we have exactly 1 param and it's not empty
|
|
|
|
|
} else if len(params) == 1 && params[0] != "" {
|
2022-01-03 23:59:52 +02:00
|
|
|
// 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
|
|
|
}
|
|
|
|
|
|
2025-08-11 22:51:50 +02:00
|
|
|
c.TCPConn.Write([]byte(cmd + paramsString + CRLF))
|
2021-11-14 06:19:46 +02:00
|
|
|
}
|