Compare commits

...
15 Commits
Author SHA1 Message Date
Illusion d8804add60 fix: .Local() message time 2025-09-02 06:53:29 +02:00
Illusion 096743adca fix: also append the day's date if we append a message with a different date than the last channel date 2025-08-29 15:59:46 +02:00
Illusion f88c2af404 WIP: refactor 2025-08-18 21:10:04 +02:00
Illusion 573a0da672 feat: handle KICK command/reply 2025-08-15 22:21:34 +02:00
Illusion 026021c118 fix: only join the initial channel after registration is complete
During registration, only a subset of commands are allowed.
Servers MUST send a RPL_WELCOME (001) after registration is complete so
we join the channel in response to that numeric reply.

https://modern.ircdocs.horse/#connection-registration
2025-08-15 22:18:55 +02:00
Illusion 1d10198d37 feat: handle ERR_NOSUCHNICK 2025-08-15 22:17:13 +02:00
Illusion eab5f8747a feat: support and handle changing nicknames 2025-08-15 21:35:27 +02:00
Illusion ff0e201e02 feat: support sending and receiving direct messages from other users 2025-08-15 21:34:04 +02:00
Illusion 0e213c691a feat: implement some miscellaneous numeric replies and errors 2025-08-15 21:21:21 +02:00
Illusion d5a99ada51 fix: better formatting for ERR_NEEDMOREPARAMS; display better information for LOCALUSERS and GLOBALUSERS 2025-08-15 21:18:40 +02:00
Illusion 41645fb679 feat: implement ISUPPORT and add support for a few easy features
fix: CAP ACK commands with no acknowledged capabilities causing an OOB.

Also log unsupported and unknown capabilities and features.
2025-08-15 21:10:52 +02:00
Illusion 4be731c813 irc: NOT IMPL -> UNIMPL; red color for error messages; deduplicate some code 2025-08-15 20:18:24 +02:00
Illusion 71471a0f69 sidepanel: sort user nicks alphabetically and ascendingly 2025-08-15 19:53:09 +02:00
Illusion 7e2f446671 refactor/feat: move IRCMessage to irc package; handle NOTICE commands 2025-08-13 13:05:15 +02:00
Illusion 6c2e54275c feat: implement TOPIC-related commands and replies 2025-08-13 11:31:16 +02:00
9 changed files with 1101 additions and 387 deletions
+8 -6
View File
@@ -17,7 +17,6 @@
package cmds package cmds
import ( import (
"fmt"
"time" "time"
tea "github.com/charmbracelet/bubbletea" tea "github.com/charmbracelet/bubbletea"
@@ -42,17 +41,14 @@ func Quit(client *irc.Client) tea.Cmd {
type SendPrivMsgMsg struct { type SendPrivMsgMsg struct {
Msg string Msg string
Timestamp string Datetime time.Time
} }
func SendPrivMsg(msg string) tea.Cmd { func SendPrivMsg(msg string) tea.Cmd {
return func() tea.Msg { return func() tea.Msg {
now := time.Now()
timestamp := fmt.Sprintf("[%02d:%02d]", now.Hour(), now.Minute())
return SendPrivMsgMsg{ return SendPrivMsgMsg{
Msg: msg, Msg: msg,
Timestamp: timestamp, Datetime: time.Now(),
} }
} }
} }
@@ -69,6 +65,12 @@ func SwitchChannels() tea.Msg {
return SwitchChannelsMsg{} return SwitchChannelsMsg{}
} }
type UpdateNicksMsg struct{}
func UpdateNicks() tea.Msg {
return UpdateNicksMsg{}
}
type UpdateTabBarMsg struct{} type UpdateTabBarMsg struct{}
func UpdateTabBar() tea.Msg { func UpdateTabBar() tea.Msg {
+98 -39
View File
@@ -30,11 +30,27 @@ import (
"github.com/illusionman1212/gorc/ui" "github.com/illusionman1212/gorc/ui"
) )
type Message struct {
DateTime time.Time
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
type User struct { type User struct {
// User prefix in channel // User prefix in channel
Prefix string Prefix string
} }
type Node[T any] struct {
Next *Node[T]
Prev *Node[T]
Value T
}
type Channel struct { type Channel struct {
// Channel name // Channel name
Name string Name string
@@ -45,6 +61,9 @@ type Channel struct {
// Channel messages history // Channel messages history
History string History string
// Date of the last message we received in this channel
LastUpdatedDate time.Time
// Users in this channel // Users in this channel
// The map key is the user's nickname // The map key is the user's nickname
// and the user struct holds data about that user // and the user struct holds data about that user
@@ -65,54 +84,75 @@ type Client struct {
// Nickname currently in use by the user // Nickname currently in use by the user
Nickname string Nickname string
// Joined channels // The root of the doubly linked list of channel
Channels []Channel RootChannel *Node[Channel]
// Active channel name // Active channel
ActiveChannel string ActiveChannel *Node[Channel]
// Active channel index // The channel to join immediately after registration completes.
ActiveChannelIndex int InitialChannel string
// Reference to the bubbletea program // Reference to the bubbletea program
Tea *tea.Program Tea *tea.Program
// Index of the first visible tab in the tab bar // Index of the first visible tab in the tab bar
FirstTabIndexInTabBar int // FirstTabIndexInTabBar int
// Index of the last visible tab in the tab bar // Index of the last visible tab in the tab bar
LastTabIndexInTabBar int // LastTabIndexInTabBar int
// The acknowledged capabilities // The acknowledged capabilities
EnabledCapabilities Capabilities EnabledCapabilities Capabilities
// The features currently enabled for this client
EnabledFeatures Features
} }
type Capabilities map[string]string type Capabilities map[string]string
type Features map[string]string
type MsgFmtOpts struct { type MsgFmtOpts struct {
WithTimestamp bool WithTimestamp bool
// TODO: make these a bitset? they should compliment eachother instead of conflicting or rendering twice.
AsServerMsg bool AsServerMsg bool
AsErrorMsg bool
NotImpl bool NotImpl bool
AsDate bool
} }
var notImpl = lipgloss.NewStyle().Foreground(ui.ErrorColor).Render("[NOT IMPL]") var unimpl = lipgloss.NewStyle().Foreground(ui.ErrorColor).Render("[UNIMPL]")
var serverMsgStyle = lipgloss.NewStyle().Foreground(ui.ServerMsgColor) var serverMsgStyle = lipgloss.NewStyle().Foreground(ui.ServerMsgColor)
var errorMsgStyle = lipgloss.NewStyle().Foreground(ui.ErrorColor)
var timestampStyle = serverMsgStyle var timestampStyle = serverMsgStyle
var dateStyle = lipgloss.NewStyle().Foreground(ui.DateColor) var dateStyle = lipgloss.NewStyle().Foreground(ui.DateColor)
const CRLF = "\r\n" const CRLF = "\r\n"
func (c *Channel) AppendMsg(timestamp string, fullMsg string, opts MsgFmtOpts) { 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.DateTime = t.Local()
} else {
m.DateTime = time.Now()
}
}
func (c *Channel) AppendMsg(datetime time.Time, fullMsg string, opts MsgFmtOpts) {
prefixes := "" prefixes := ""
style := ui.DefaultStyle style := ui.DefaultStyle
if opts.WithTimestamp { if opts.WithTimestamp {
prefixes += timestampStyle.Render(timestamp) + " " str := fmt.Sprintf("[%02d:%02d]", datetime.Hour(), datetime.Minute())
prefixes += timestampStyle.Render(str) + " "
} }
if opts.NotImpl { if opts.NotImpl {
prefixes += notImpl + " " prefixes += unimpl + " "
} }
if opts.AsServerMsg { if opts.AsServerMsg {
@@ -120,8 +160,15 @@ func (c *Channel) AppendMsg(timestamp string, fullMsg string, opts MsgFmtOpts) {
style = serverMsgStyle style = serverMsgStyle
} }
if opts.AsDate { if opts.AsErrorMsg {
style = dateStyle prefixes += errorMsgStyle.Render("==") + " "
style = errorMsgStyle
}
if datetime.Year() > c.LastUpdatedDate.Year() || datetime.YearDay() > c.LastUpdatedDate.YearDay() {
c.LastUpdatedDate = datetime
dateMsg := fmt.Sprintf("————— %s %d —————", c.LastUpdatedDate.Month().String(), c.LastUpdatedDate.Day())
c.History += dateStyle.Render(dateMsg) + CRLF
} }
c.History += prefixes + style.Render(fullMsg) + CRLF c.History += prefixes + style.Render(fullMsg) + CRLF
@@ -130,6 +177,11 @@ func (c *Channel) AppendMsg(timestamp string, fullMsg string, opts MsgFmtOpts) {
func (c *Client) Initialize(host string, port string, tlsEnabled bool) { func (c *Client) Initialize(host string, port string, tlsEnabled bool) {
addr := fmt.Sprintf("%s:%s", host, port) addr := fmt.Sprintf("%s:%s", host, port)
c.Host = host
c.Port = port
c.EnabledCapabilities = make(Capabilities, 0)
c.EnabledFeatures = make(Features, 0)
if tlsEnabled { if tlsEnabled {
cfg := &tls.Config{ServerName: host} cfg := &tls.Config{ServerName: host}
conn, err := tls.Dial("tcp", addr, cfg) conn, err := tls.Dial("tcp", addr, cfg)
@@ -139,11 +191,6 @@ func (c *Client) Initialize(host string, port string, tlsEnabled bool) {
} }
c.TCPConn = conn c.TCPConn = conn
c.Host = host
c.Port = port
c.ActiveChannel = host
c.Channels = make([]Channel, 0)
c.EnabledCapabilities = make(Capabilities, 0)
return return
} }
@@ -158,18 +205,19 @@ func (c *Client) Initialize(host string, port string, tlsEnabled bool) {
} }
c.TCPConn = conn c.TCPConn = conn
c.Host = host
c.Port = port
c.ActiveChannel = host
c.Channels = make([]Channel, 0)
c.EnabledCapabilities = make(Capabilities, 0)
} }
func (c *Client) Register(nick string, password string, channel string) { func (c *Client) Register(nick string, password string, channel string) {
c.Channels = append(c.Channels, Channel{ hostChannel := Channel{
Name: c.Host, Name: c.Host,
Users: make(map[string]User), Users: make(map[string]User),
}) }
root := &Node[Channel]{Value: hostChannel}
root.Next = root
root.Prev = root
c.ActiveChannel = root
c.RootChannel = root
c.SendCommand(commands.CAP, "LS", "302") c.SendCommand(commands.CAP, "LS", "302")
if password != "" { if password != "" {
@@ -179,25 +227,36 @@ func (c *Client) Register(nick string, password string, channel string) {
// set user-wanted nickname // set user-wanted nickname
c.Nickname = nick c.Nickname = nick
c.SendCommand(commands.USER, nick, "0", "*", nick) c.SendCommand(commands.USER, nick, "0", "*", nick)
// joining a channel when registering is optional
if channel != "" {
c.SendCommand(commands.JOIN, channel)
}
} }
func (c *Client) SetDay() { func (c *Client) AppendChannel(channel Channel) *Node[Channel] {
msgOpts := MsgFmtOpts{ first := c.RootChannel
AsDate: true, last := c.RootChannel.Prev
newNode := &Node[Channel]{
Value: channel,
Next: first,
Prev: last,
} }
now := time.Now() last.Next = newNode
msg := fmt.Sprintf("————— %s %d —————", now.Month().String(), now.Day()) first.Prev = newNode
c.Channels[0].AppendMsg("", msg, msgOpts)
// TODO: append new day to each channel whenever the day changes return newNode
} }
func (c Client) SendCommand(cmd string, params ...string) { func (c *Client) RemoveChannel(channel *Node[Channel]) {
prev := channel.Prev
next := channel.Next
prev.Next = next
next.Prev = prev
channel.Prev = nil
channel.Next = nil
}
func (c *Client) SendCommand(cmd string, params ...string) {
if c.TCPConn == nil { if c.TCPConn == nil {
// TODO: properly handle the error instead of Fatal-ing // TODO: properly handle the error instead of Fatal-ing
log.Fatal("Attempted to write data to nil connection") log.Fatal("Attempted to write data to nil connection")
+72 -69
View File
@@ -58,7 +58,7 @@ const (
NOTICE = "NOTICE" // Send notices between users and to channels. NOTICE = "NOTICE" // Send notices between users and to channels.
// Operator Messages // Operator Messages
KILL = "KILL" // CLose connection between a client and the server. IRC Operators only (do we need this ??) KILL = "KILL" // Close connection between a client and the server. IRC Operators only (do we need this ??)
// Optional Messages // Optional Messages
AWAY = "AWAY" // Indicate that the client (user) is away/afk/etc.. AWAY = "AWAY" // Indicate that the client (user) is away/afk/etc..
@@ -167,12 +167,12 @@ const (
RPL_GLOBALUSERS = "266" // aircd, Hybird, Bahamut - Implemented RPL_GLOBALUSERS = "266" // aircd, Hybird, Bahamut - Implemented
RPL_WHOISCERTFP = "276" // oftc-hybrid - Not Implemented - Has Conflicts (TODO:) RPL_WHOISCERTFP = "276" // oftc-hybrid - Not Implemented - Has Conflicts (TODO:)
RPL_NONE = "300" // RFC1459 - Not Implemented (TODO:) RPL_NONE = "300" // RFC1459 - Not Implemented (TODO:)
RPL_AWAY = "301" // RFC1459 - Not Implemented (TODO:) RPL_AWAY = "301" // RFC1459 - Implemented
RPL_USERHOST = "302" // RFC1459 - Not Implemented (TODO:) RPL_USERHOST = "302" // RFC1459 - Not Implemented (TODO:)
RPL_ISON = "303" // RFC1459 - Not Implemented (TODO:) RPL_ISON = "303" // RFC1459 - Not Implemented (TODO:)
RPL_TEXT = "304" // irc2 - Not Implemented (TODO:) RPL_TEXT = "304" // irc2 - Not Implemented (TODO:)
RPL_UNAWAY = "305" // RFC1459 - Not Implemented (TODO:) RPL_UNAWAY = "305" // RFC1459 - Implemented
RPL_NOWAWAY = "306" // RFC1459 - Not Implemented (TODO:) RPL_NOWAWAY = "306" // RFC1459 - Implemented
RPL_WHOISUSER = "311" // RFC1459 - Implemented RPL_WHOISUSER = "311" // RFC1459 - Implemented
RPL_WHOISSERVER = "312" // RFC1459 - Implemented RPL_WHOISSERVER = "312" // RFC1459 - Implemented
RPL_WHOISOPERATOR = "313" // RFC1459 - Not Implemented (TODO:) RPL_WHOISOPERATOR = "313" // RFC1459 - Not Implemented (TODO:)
@@ -187,8 +187,8 @@ const (
RPL_CHANNELMODEIS = "324" // RFC1459 - Not Implemented (TODO:) RPL_CHANNELMODEIS = "324" // RFC1459 - Not Implemented (TODO:)
RPL_UNIQOPIS = "325" // RFC2812 - Not Implemented - Has Conflicts (TODO:) RPL_UNIQOPIS = "325" // RFC2812 - Not Implemented - Has Conflicts (TODO:)
RPL_CREATIONTIME = "329" // Bahamut,InspIRCd - Not Implemented (TODO:) RPL_CREATIONTIME = "329" // Bahamut,InspIRCd - Not Implemented (TODO:)
RPL_NOTOPIC = "331" // RFC1459 - Not Implemented (TODO:) RPL_NOTOPIC = "331" // RFC1459 - Implemented
RPL_TOPIC = "332" // RFC1459 - Not Implemented (TODO:) RPL_TOPIC = "332" // RFC1459 - Implemented
RPL_TOPICWHOTIME = "333" // ircu,InspIRCd - Not Implemented (TODO:) RPL_TOPICWHOTIME = "333" // ircu,InspIRCd - Not Implemented (TODO:)
RPL_INVITING = "341" // RFC1459 - Not Implemented (TODO:) RPL_INVITING = "341" // RFC1459 - Not Implemented (TODO:)
RPL_SUMMONING = "342" // RFC1459 - Not Implemented - Deprecated (TODO:) RPL_SUMMONING = "342" // RFC1459 - Not Implemented - Deprecated (TODO:)
@@ -197,7 +197,7 @@ const (
RPL_EXCEPTLIST = "348" // RFC2812 - Not Implemented (TODO:) RPL_EXCEPTLIST = "348" // RFC2812 - Not Implemented (TODO:)
RPL_ENDOFEXCEPTLIST = "349" // RFC2812 - Not Implemented (TODO:) RPL_ENDOFEXCEPTLIST = "349" // RFC2812 - Not Implemented (TODO:)
RPL_WHOISGATEWAY = "350" // InspIRCd - Not Implemented (TODO:) RPL_WHOISGATEWAY = "350" // InspIRCd - Not Implemented (TODO:)
RPL_VERSION = "351" // RFC1459 - Not Implemented (TODO:) RPL_VERSION = "351" // RFC1459 - Implemented
RPL_WHOREPLY = "352" // RFC1459 - Not Implemented (TODO:) RPL_WHOREPLY = "352" // RFC1459 - Not Implemented (TODO:)
RPL_NAMREPLY = "353" // RFC1459 - Implemented RPL_NAMREPLY = "353" // RFC1459 - Implemented
RPL_KILLDONE = "361" // RFC1459 - Not Implemented - Deprecated (TODO:) RPL_KILLDONE = "361" // RFC1459 - Not Implemented - Deprecated (TODO:)
@@ -209,10 +209,10 @@ const (
RPL_BANLIST = "367" // RFC1459 - Not Implemented (TODO:) RPL_BANLIST = "367" // RFC1459 - Not Implemented (TODO:)
RPL_ENDOFBANLIST = "368" // RFC1459 - Not Implemented (TODO:) RPL_ENDOFBANLIST = "368" // RFC1459 - Not Implemented (TODO:)
RPL_ENDOFWHOWAS = "369" // RFC1459 - Not Implemented (TODO:) RPL_ENDOFWHOWAS = "369" // RFC1459 - Not Implemented (TODO:)
RPL_INFO = "371" // RFC1459 - Not Implemented (TODO:) RPL_INFO = "371" // RFC1459 - Implemented
RPL_MOTD = "372" // RFC1459 - Implemented RPL_MOTD = "372" // RFC1459 - Implemented
RPL_INFOSTART = "373" // RFC1459 - Not Implemented - Deprecated (TODO:) RPL_INFOSTART = "373" // RFC1459 - Not Implemented - Deprecated (TODO:)
RPL_ENDOFINFO = "374" // RFC1459 - Not Implemented (TODO:) RPL_ENDOFINFO = "374" // RFC1459 - Implemented
RPL_MOTDSTART = "375" // RFC1459 - Implemented RPL_MOTDSTART = "375" // RFC1459 - Implemented
RPL_ENDOFMOTD = "376" // RFC1459 - Not Implemented (TODO:) RPL_ENDOFMOTD = "376" // RFC1459 - Not Implemented (TODO:)
RPL_WHOISHOST = "378" // ??????? - Implemented - Has Conflicts RPL_WHOISHOST = "378" // ??????? - Implemented - Has Conflicts
@@ -228,11 +228,11 @@ const (
RPL_NOUSERS = "395" // RFC1459 - Not Implemented (TODO:) RPL_NOUSERS = "395" // RFC1459 - Not Implemented (TODO:)
ERR_UNKNOWNERROR = "400" // ??????? - Not Implemented (TODO:) ERR_UNKNOWNERROR = "400" // ??????? - Not Implemented (TODO:)
ERR_NOSUCHNICK = "401" // RFC1459 - Not Implemented (TODO:) ERR_NOSUCHNICK = "401" // RFC1459 - Implemented
ERR_NOSUCHSERVER = "402" // RFC1459 - Implemented ERR_NOSUCHSERVER = "402" // RFC1459 - Implemented
ERR_NOSUCHCHANNEL = "403" // RFC1459 - Not Implemented (TODO:) ERR_NOSUCHCHANNEL = "403" // RFC1459 - Implemented
ERR_CANNOTSENDTOCHAN = "404" // RFC1459 - Not Implemented (TODO:) ERR_CANNOTSENDTOCHAN = "404" // RFC1459 - Implemented
ERR_TOOMANYCHANNELS = "405" // RFC1459 - Not Implemented (TODO:) ERR_TOOMANYCHANNELS = "405" // RFC1459 - Implemented
ERR_WASNOSUCHNICK = "406" // RFC1459 - Not Implemented (TODO:) ERR_WASNOSUCHNICK = "406" // RFC1459 - Not Implemented (TODO:)
ERR_NOSUCHSERVICE = "408" // RFC2812 - Not Implemented - Has Conflicts (TODO:) ERR_NOSUCHSERVICE = "408" // RFC2812 - Not Implemented - Has Conflicts (TODO:)
ERR_NOORIGIN = "409" // RFC1459 - Not Implemented (TODO:) ERR_NOORIGIN = "409" // RFC1459 - Not Implemented (TODO:)
@@ -242,7 +242,7 @@ const (
ERR_WILDTOPLEVEL = "414" // RFC1459 - Not Implemented (TODO:) ERR_WILDTOPLEVEL = "414" // RFC1459 - Not Implemented (TODO:)
ERR_BADMASK = "415" // RFC2812 - Not Implemented (TODO:) ERR_BADMASK = "415" // RFC2812 - Not Implemented (TODO:)
ERR_UNKNOWNCOMMAND = "421" // RFC1459 - Implemented ERR_UNKNOWNCOMMAND = "421" // RFC1459 - Implemented
ERR_NOMOTD = "422" // RFC1459 - Not Implemented (TODO:) ERR_NOMOTD = "422" // RFC1459 - Implemented
ERR_NOADMININFO = "423" // RFC1459 - Not Implemented (TODO:) ERR_NOADMININFO = "423" // RFC1459 - Not Implemented (TODO:)
ERR_FILEERROR = "424" // RFC1459 - Not Implemented (TODO:) ERR_FILEERROR = "424" // RFC1459 - Not Implemented (TODO:)
ERR_NONICKNAMEGIVEN = "431" // RFC1459 - Implemented ERR_NONICKNAMEGIVEN = "431" // RFC1459 - Implemented
@@ -269,10 +269,10 @@ const (
ERR_INVITEONLYCHAN = "473" // RFC1459 - Not Implemented (TODO:) ERR_INVITEONLYCHAN = "473" // RFC1459 - Not Implemented (TODO:)
ERR_BANNEDFROMCHAN = "474" // RFC1459 - Not Implemented (TODO:) ERR_BANNEDFROMCHAN = "474" // RFC1459 - Not Implemented (TODO:)
ERR_BADCHANNELKEY = "475" // RFC1459 - Not Implemented (TODO:) ERR_BADCHANNELKEY = "475" // RFC1459 - Not Implemented (TODO:)
ERR_BADCHANMASK = "476" // RFC2812 - Not Implemented (TODO:) ERR_BADCHANMASK = "476" // RFC2812 - Implemented
ERR_BANLISTFULL = "478" // RFC2812 - Not Implemented (TODO:) ERR_BANLISTFULL = "478" // RFC2812 - Not Implemented (TODO:)
ERR_NOPRIVILEGES = "481" // RFC1459 - Not Implemented (TODO:) ERR_NOPRIVILEGES = "481" // RFC1459 - Not Implemented (TODO:)
ERR_CHANOPRIVSNEEDED = "482" // RFC1459 - Not Implemented (TODO:) ERR_CHANOPRIVSNEEDED = "482" // RFC1459 - Implemented
ERR_CANTKILLSERVER = "483" // RFC1459 - Not Implemented (TODO:) ERR_CANTKILLSERVER = "483" // RFC1459 - Not Implemented (TODO:)
ERR_RESTRICTED = "484" // RFC2812 - Not Implemented - Has Conflicts (TODO:) ERR_RESTRICTED = "484" // RFC2812 - Not Implemented - Has Conflicts (TODO:)
ERR_UNIQOPRIVSNEEDED = "485" // RFC2812 - Not Implemented (TODO:) ERR_UNIQOPRIVSNEEDED = "485" // RFC2812 - Not Implemented (TODO:)
@@ -305,64 +305,67 @@ const (
/* --- RPL_ISUPPORT Parameters --- */ /* --- RPL_ISUPPORT Parameters --- */
var Features = map[string]bool{ var Features = map[string]bool{
"ACCEPT": true, "ACCEPT": false,
// The AWAYLEN parameter indicates the maximum length for the <reason> of an AWAY command.
// If an AWAY <reason> has more characters than this parameter, it may be silently truncated by the server before being passed on to other clients.
// Clients MAY receive an AWAY <reason> that has more characters than this parameter
"AWAYLEN": true, "AWAYLEN": true,
"BOT": true, "BOT": false,
"CALLERID": true, "CALLERID": false,
"CASEMAPPING": true, "CASEMAPPING": false,
"CHANLIMIT": true, "CHANLIMIT": false,
"CHANMODES": true, "CHANMODES": false,
"CHANNELLEN": true, "CHANNELLEN": false,
"CHANTYPES": true, // Channel Types. Default is #. Available are #& "CHANTYPES": false, // Channel Types. Default is #. Available are #&
"CHARSET": true, // Deprecated but might still be used "CHARSET": false, // Deprecated but might still be used
"CLIENTVER": true, // Deprecated but might still be used "CLIENTVER": false, // Deprecated but might still be used
"CNOTICE": true, "CNOTICE": false,
"CPRIVMSG": true, "CPRIVMSG": false,
"DEAF": true, "DEAF": false,
"ELIST": true, "ELIST": false,
"ESILENCE": true, "ESILENCE": false,
"ETRACE": true, "ETRACE": false,
"EXCEPTS": true, "EXCEPTS": false,
"EXTBAN": true, "EXTBAN": false,
"FNC": true, // Deprecated but might still be used "FNC": false, // Deprecated but might still be used
"INVEX": true, "INVEX": false,
"KEYLEN": true, "KEYLEN": false,
"KICKLEN": true, "KICKLEN": true,
"KNOCK": true, "KNOCK": false,
"LINELEN": true, // Proposed "LINELEN": false, // Proposed
"MAP": true, // Deprecated but might still be used "MAP": false, // Deprecated but might still be used
"MAXBANS": true, // Deprecated but might still be used "MAXBANS": false, // Deprecated but might still be used
"MAXCHANNELS": true, // Deprecated but might still be used "MAXCHANNELS": false, // Deprecated but might still be used
"MAXLIST": true, "MAXLIST": false,
"MAXNICKLEN": true, "MAXNICKLEN": false,
"MAXPARA": true, // Deprecated but might still be used "MAXPARA": false, // Deprecated but might still be used
"MAXTARGETS": true, "MAXTARGETS": false,
"METADATA": true, "METADATA": false,
"MODES": true, "MODES": false,
"MONITOR": true, "MONITOR": false,
"NAMESX": true, // Deprecated but might still be used "NAMESX": false, // Deprecated but might still be used
"NETWORK": true, "NETWORK": true,
"NICKLEN": true, "NICKLEN": true,
"OVERRIDE": true, "OVERRIDE": false,
"PREFIX": true, "PREFIX": false,
"SAFELIST": true, "SAFELIST": false,
"SECURELIST": true, "SECURELIST": false,
"SILENCE": true, "SILENCE": false,
"SSL": true, // Deprecated but might still be used "SSL": false, // Deprecated but might still be used
"STARTTLS": true, // Deprecated but might still be used "STARTTLS": false, // Deprecated but might still be used
"STATUSMSG": true, "STATUSMSG": false,
"STD": true, // Deprecated but might still be used "STD": false, // Deprecated but might still be used
"TARGMAX": true, "TARGMAX": false,
"TOPICLEN": true, "TOPICLEN": true,
"UHNAMES": true, // Deprecated but might still be used "UHNAMES": false, // Deprecated but might still be used
"USERIP": true, "USERIP": false,
"USERLEN": true, // Proposed "USERLEN": false, // Proposed
"VBANLIST": true, // Deprecared but might still be used "VBANLIST": false, // Deprecared but might still be used
"VLIST": true, "VLIST": false,
"WALLCHOPS": true, // Deprecated but might still be used "WALLCHOPS": false, // Deprecated but might still be used
"WALLVOICES": true, // Deprecated but might still be used "WALLVOICES": false, // Deprecated but might still be used
"WATCH": true, "WATCH": false,
"WHOX": true, "WHOX": false,
} }
var Capabilities = map[string]bool{ var Capabilities = map[string]bool{
+651 -121
View File
File diff suppressed because it is too large Load Diff
+66 -5
View File
@@ -17,7 +17,9 @@
package handler package handler
import ( import (
"fmt"
"strings" "strings"
"time"
tea "github.com/charmbracelet/bubbletea" tea "github.com/charmbracelet/bubbletea"
"github.com/illusionman1212/gorc/cmds" "github.com/illusionman1212/gorc/cmds"
@@ -25,18 +27,75 @@ import (
"github.com/illusionman1212/gorc/irc/commands" "github.com/illusionman1212/gorc/irc/commands"
) )
func handleSlashPrivMsg(params []string, client *irc.Client) tea.Cmd {
var batchedCmds []tea.Cmd
if len(params) < 2 {
client.SendCommand(commands.PRIVMSG, params...)
return nil
}
target := strings.ToLower(params[0])
msgOpts := irc.MsgFmtOpts{
WithTimestamp: true,
}
now := time.Now()
msg := fmt.Sprintf("%s: %s", client.Nickname, strings.Join(params[1:], " "))
c := client.RootChannel
for {
if c.Value.Name == target {
client.ActiveChannel = c
c.Value.AppendMsg(now, msg, msgOpts)
client.SendCommand(commands.PRIVMSG, params...)
return cmds.SwitchChannels
}
c = c.Next
if c == client.RootChannel {
break
}
}
// If we're messaging a user and their "channel" wasn't found in the previous loop, then create it and append it
if target[0] != '#' && target[0] != '&' {
newChannel := irc.Channel{
Name: target,
Users: map[string]irc.User{target: {}},
}
newChannel.AppendMsg(now, msg, msgOpts)
client.ActiveChannel = client.AppendChannel(newChannel)
batchedCmds = append(batchedCmds, cmds.UpdateTabBar)
}
client.SendCommand(commands.PRIVMSG, params...)
batchedCmds = append(batchedCmds, cmds.SwitchChannels)
return tea.Batch(batchedCmds...)
}
func handleSlashJoin(params []string, client *irc.Client) tea.Cmd { func handleSlashJoin(params []string, client *irc.Client) tea.Cmd {
channel := "" channel := ""
if len(params) > 0 { if len(params) > 0 {
channel = params[0] channel = strings.ToLower(params[0])
} }
for i, c := range client.Channels { c := client.RootChannel
if c.Name == channel { for {
client.ActiveChannelIndex = i if c.Value.Name == channel {
client.ActiveChannel = channel client.ActiveChannel = c
return cmds.SwitchChannels return cmds.SwitchChannels
} }
c = c.Next
if c == client.RootChannel {
break
}
} }
client.SendCommand(commands.JOIN, params...) client.SendCommand(commands.JOIN, params...)
@@ -52,6 +111,8 @@ func HandleSlashCommand(msg string, client *irc.Client) tea.Cmd {
} }
switch command { switch command {
case commands.PRIVMSG:
return handleSlashPrivMsg(params, client)
case commands.JOIN: case commands.JOIN:
return handleSlashJoin(params, client) return handleSlashJoin(params, client)
case commands.QUIT: case commands.QUIT:
+8 -33
View File
@@ -17,37 +17,12 @@
package parser package parser
import ( import (
"fmt"
"log"
"regexp" "regexp"
"strings" "strings"
"time"
"github.com/illusionman1212/gorc/irc"
) )
type IRCMessage struct {
Timestamp string
Tags Tags // 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 Tags map[string]string
func (m *IRCMessage) 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())
}
}
func escapeCharacters(char rune) string { func escapeCharacters(char rune) string {
switch char { switch char {
case 's': case 's':
@@ -75,8 +50,8 @@ func escapeCharacters(char rune) string {
} }
} }
func parseTags(rawTags string) Tags { func parseTags(rawTags string) irc.MessageTags {
tags := make(Tags, 0) tags := make(irc.MessageTags, 0)
rawTagsSlice := strings.Split(rawTags, ";") rawTagsSlice := strings.Split(rawTags, ";")
for _, tag := range rawTagsSlice { for _, tag := range rawTagsSlice {
@@ -113,8 +88,8 @@ func parseTags(rawTags string) Tags {
return tags return tags
} }
func ParseIRCMessage(line string) (IRCMessage, bool) { func ParseIRCMessage(line string) (irc.Message, bool) {
ircMessage := IRCMessage{} ircMessage := irc.Message{}
multipleSpacesRegex := regexp.MustCompile(`[^\S\t]+`) multipleSpacesRegex := regexp.MustCompile(`[^\S\t]+`)
trailingParamRegex := regexp.MustCompile(`[^\S\t]+:`) trailingParamRegex := regexp.MustCompile(`[^\S\t]+:`)
whitespaceRegex := regexp.MustCompile(`^[^\S\t]+$`) whitespaceRegex := regexp.MustCompile(`^[^\S\t]+$`)
@@ -122,7 +97,7 @@ func ParseIRCMessage(line string) (IRCMessage, bool) {
if len(line) <= 0 { if len(line) <= 0 {
// TODO: do something here ??? // TODO: do something here ???
// log.Println("empty message") // log.Println("empty message")
return IRCMessage{}, false return irc.Message{}, false
} }
if line[0] == '@' { if line[0] == '@' {
@@ -169,7 +144,7 @@ func ParseIRCMessage(line string) (IRCMessage, bool) {
ircMessage.Parameters = finalParams ircMessage.Parameters = finalParams
} }
ircMessage.setTimestamp() ircMessage.SetTimestamp()
return ircMessage, true return ircMessage, true
} }
+12 -1
View File
@@ -42,6 +42,8 @@ type UI struct {
type State struct { type State struct {
UI UI UI UI
TerminalWidth int
TerminalHeight int
Client *irc.Client Client *irc.Client
} }
@@ -78,8 +80,15 @@ func (s State) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
Width(msg.Width). Width(msg.Width).
Height(msg.Height) Height(msg.Height)
s.TerminalWidth = msg.Width
s.TerminalHeight = msg.Height
switch s.UI.CurrentScreen {
case Login:
s.UI.Login.SetSize(msg.Width, msg.Height) s.UI.Login.SetSize(msg.Width, msg.Height)
case MainScreen:
s.UI.MainScreen.SetSize(msg.Width, msg.Height) s.UI.MainScreen.SetSize(msg.Width, msg.Height)
}
return s, nil return s, nil
case cmds.ConnectMsg: case cmds.ConnectMsg:
@@ -91,10 +100,12 @@ func (s State) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
tlsEnabled := s.UI.Login.TLS tlsEnabled := s.UI.Login.TLS
s.UI.CurrentScreen = MainScreen s.UI.CurrentScreen = MainScreen
s.Client.InitialChannel = channel
s.Client.Initialize(host, port, tlsEnabled) s.Client.Initialize(host, port, tlsEnabled)
s.Client.Register(nickname, password, channel) s.Client.Register(nickname, password, channel)
s.Client.SetDay()
s.UI.MainScreen.SetSize(s.TerminalWidth, s.TerminalHeight)
go handler.ReadLoop(s.Client) go handler.ReadLoop(s.Client)
+118 -72
View File
@@ -18,6 +18,7 @@ package mainscreen
import ( import (
"math" "math"
"strconv"
"strings" "strings"
"github.com/charmbracelet/bubbles/textinput" "github.com/charmbracelet/bubbles/textinput"
@@ -49,7 +50,7 @@ type State struct {
Client *irc.Client Client *irc.Client
Viewport *viewport.Model Viewport *viewport.Model
FocusIndex Window FocusIndex Window
TabRenderingDirection TabDirection // TabRenderingDirection TabDirection
InputBox InputState InputBox InputState
SidePanel *SidePanelState SidePanel *SidePanelState
@@ -65,7 +66,7 @@ func NewMainScreen(client *irc.Client) State {
FocusIndex: InputBox, FocusIndex: InputBox,
InputBox: NewInputBox(), InputBox: NewInputBox(),
SidePanel: NewSidePanel(client), SidePanel: NewSidePanel(client),
TabRenderingDirection: Right, // TabRenderingDirection: Right,
} }
} }
@@ -76,7 +77,7 @@ func (s State) Update(msg tea.Msg) (State, tea.Cmd) {
switch msg := msg.(type) { switch msg := msg.(type) {
case cmds.ReceivedIRCMsgMsg: case cmds.ReceivedIRCMsgMsg:
wasAtBottom := s.Viewport.AtBottom() wasAtBottom := s.Viewport.AtBottom()
s.Viewport.SetContent(s.Client.Channels[s.Client.ActiveChannelIndex].History) s.Viewport.SetContent(s.Client.ActiveChannel.Value.History)
if wasAtBottom { if wasAtBottom {
s.Viewport.GotoBottom() s.Viewport.GotoBottom()
@@ -87,30 +88,31 @@ func (s State) Update(msg tea.Msg) (State, tea.Cmd) {
cmd = handler.HandleSlashCommand(msg.Msg, s.Client) cmd = handler.HandleSlashCommand(msg.Msg, s.Client)
return s, cmd return s, cmd
} else { } else {
if s.Client.ActiveChannel != s.Client.Host { if s.Client.ActiveChannel.Value.Name != s.Client.Host {
fullMsg := s.Client.Nickname + ": " + msg.Msg fullMsg := s.Client.Nickname + ": " + msg.Msg
msgOpts := irc.MsgFmtOpts{ msgOpts := irc.MsgFmtOpts{
WithTimestamp: true, WithTimestamp: true,
} }
s.Client.Channels[s.Client.ActiveChannelIndex].AppendMsg(msg.Timestamp, fullMsg, msgOpts) s.Client.ActiveChannel.Value.AppendMsg(msg.Datetime, fullMsg, msgOpts)
// TODO: make sure to only append the message to the history if server sends back no errors // TODO: make sure to only append the message to the history if server sends back no errors
s.Client.SendCommand(commands.PRIVMSG, s.Client.ActiveChannel, msg.Msg) s.Client.SendCommand(commands.PRIVMSG, s.Client.ActiveChannel.Value.Name, msg.Msg)
s.Viewport.SetContent(s.Client.Channels[s.Client.ActiveChannelIndex].History) s.Viewport.SetContent(s.Client.ActiveChannel.Value.History)
s.Viewport.GotoBottom() s.Viewport.GotoBottom()
} }
} }
return s, nil return s, nil
case cmds.SwitchChannelsMsg: case cmds.SwitchChannelsMsg:
s.Viewport.SetContent(s.Client.Channels[s.Client.ActiveChannelIndex].History) s.Viewport.SetContent(s.Client.ActiveChannel.Value.History)
// log.Println(s)
s.Viewport.GotoBottom() s.Viewport.GotoBottom()
*s.SidePanel, cmd = s.SidePanel.Update(msg) *s.SidePanel, cmd = s.SidePanel.Update(msg)
return s, cmd return s, cmd
case cmds.UpdateTabBarMsg: case cmds.UpdateTabBarMsg:
s.Client.LastTabIndexInTabBar = len(s.Client.Channels) - 1 // s.Client.LastTabIndexInTabBar = len(s.Client.Channels) - 1
s.TabRenderingDirection = Left // s.TabRenderingDirection = Left
return s, nil return s, nil
case tea.KeyMsg: case tea.KeyMsg:
key := msg.String() key := msg.String()
@@ -151,31 +153,33 @@ func (s State) Update(msg tea.Msg) (State, tea.Cmd) {
} }
// we need this so that the viewport doesnt scroll to the bottom // we need this so that the viewport doesnt scroll to the bottom
if len(s.Client.Channels) == 1 { if s.Client.RootChannel.Next == s.Client.RootChannel {
return s, nil return s, nil
} }
if key == "right" { if key == "right" {
s.Client.ActiveChannelIndex++ // s.Client.ActiveChannelIndex++
s.Client.ActiveChannel = s.Client.ActiveChannel.Next
} else { } else {
s.Client.ActiveChannelIndex-- s.Client.ActiveChannel = s.Client.ActiveChannel.Prev
// s.Client.ActiveChannelIndex--
} }
if s.Client.ActiveChannelIndex >= len(s.Client.Channels) { // if s.Client.ActiveChannelIndex >= len(s.Client.Channels) {
s.Client.ActiveChannelIndex = 0 // s.Client.ActiveChannelIndex = 0
} else if s.Client.ActiveChannelIndex < 0 { // } else if s.Client.ActiveChannelIndex < 0 {
s.Client.ActiveChannelIndex = len(s.Client.Channels) - 1 // s.Client.ActiveChannelIndex = len(s.Client.Channels) - 1
} // }
if s.Client.ActiveChannelIndex > s.Client.LastTabIndexInTabBar { // if s.Client.ActiveChannelIndex > s.Client.LastTabIndexInTabBar {
s.TabRenderingDirection = Left // s.TabRenderingDirection = Left
s.Client.LastTabIndexInTabBar = s.Client.ActiveChannelIndex // s.Client.LastTabIndexInTabBar = s.Client.ActiveChannelIndex
} else if s.Client.ActiveChannelIndex < s.Client.FirstTabIndexInTabBar { // } else if s.Client.ActiveChannelIndex < s.Client.FirstTabIndexInTabBar {
s.Client.FirstTabIndexInTabBar = s.Client.ActiveChannelIndex // s.Client.FirstTabIndexInTabBar = s.Client.ActiveChannelIndex
s.TabRenderingDirection = Right // s.TabRenderingDirection = Right
} // }
s.Client.ActiveChannel = s.Client.Channels[s.Client.ActiveChannelIndex].Name // s.Client.ActiveChannel = s.Client.Channels[s.Client.ActiveChannelIndex].Name
return s, cmds.SwitchChannels return s, cmds.SwitchChannels
case "g": case "g":
if s.FocusIndex == Viewport { if s.FocusIndex == Viewport {
@@ -194,6 +198,36 @@ func (s State) Update(msg tea.Msg) (State, tea.Cmd) {
cmdsToProcess = append(cmdsToProcess, cmd) cmdsToProcess = append(cmdsToProcess, cmd)
case InputBox: case InputBox:
s.InputBox, cmd = s.InputBox.Update(msg) s.InputBox, cmd = s.InputBox.Update(msg)
width := 0
command := strings.Split(s.InputBox.Input.Value(), " ")[0]
if len(command) > 0 && command[0] == '/' {
switch strings.ToUpper(command)[1:] {
case commands.AWAY:
w, err := strconv.ParseInt(s.Client.EnabledFeatures["AWAYLEN"], 10, 32)
if err == nil {
width = int(w)
}
case commands.TOPIC:
w, err := strconv.ParseInt(s.Client.EnabledFeatures["TOPICLEN"], 10, 32)
if err == nil {
width = int(w)
}
case commands.NICK:
w, err := strconv.ParseInt(s.Client.EnabledFeatures["NICKLEN"], 10, 32)
if err == nil {
width = int(w)
}
case commands.KICK:
w, err := strconv.ParseInt(s.Client.EnabledFeatures["KICKLEN"], 10, 32)
if err == nil {
width = int(w)
}
}
}
s.InputBox.Input.CharLimit = width
cmdsToProcess = append(cmdsToProcess, cmd) cmdsToProcess = append(cmdsToProcess, cmd)
} }
@@ -241,10 +275,8 @@ func (s *State) SetSize(width, height int) {
s.Viewport.Style = s.Viewport.Style.Height(newHeight) s.Viewport.Style = s.Viewport.Style.Height(newHeight)
// we need this to render an empty viewport // we need this to render an empty viewport
history := "" // history := ""
if len(s.Client.Channels) != 0 { history := s.Client.ActiveChannel.Value.History
history = s.Client.Channels[s.Client.ActiveChannelIndex].History
}
// we need to re-set the content because words wrap differently on different sizes // we need to re-set the content because words wrap differently on different sizes
s.Viewport.SetContent(history) s.Viewport.SetContent(history)
@@ -255,50 +287,64 @@ func (s State) buildTabBar(rightArrow string, leftArrow string) string {
var renderedTabs []string var renderedTabs []string
tabs := "" tabs := ""
switch s.TabRenderingDirection { current := s.Client.RootChannel
case Left: for {
for i := s.Client.LastTabIndexInTabBar; i >= 0; i-- { if current == s.Client.ActiveChannel {
if s.Client.Channels[i].Name == s.Client.ActiveChannel { renderedTabs = append(renderedTabs, activeTab.Render(current.Value.Name))
renderedTabs = append([]string{activeTab.Render(s.Client.Channels[i].Name)}, renderedTabs...)
} else { } else {
renderedTabs = append([]string{tab.Render(s.Client.Channels[i].Name)}, renderedTabs...) renderedTabs = append(renderedTabs, tab.Render(current.Value.Name))
} }
tabs = lipgloss.JoinHorizontal( current = current.Next
lipgloss.Top, if current == s.Client.RootChannel {
renderedTabs...,
)
if lipgloss.Width(tabs) > lipgloss.Width(s.Viewport.View())-lipgloss.Width(leftArrow)-lipgloss.Width(rightArrow) {
// set the first tab to be displayed to the index of the previous tab in the loop
s.Client.FirstTabIndexInTabBar = i + 1
// dont render the newly added tab
renderedTabs = renderedTabs[1:]
break break
} }
} }
case Right:
for i := s.Client.FirstTabIndexInTabBar; i < len(s.Client.Channels); i++ {
if s.Client.Channels[i].Name == s.Client.ActiveChannel {
renderedTabs = append(renderedTabs, activeTab.Render(s.Client.Channels[i].Name))
} else {
renderedTabs = append(renderedTabs, tab.Render(s.Client.Channels[i].Name))
}
tabs = lipgloss.JoinHorizontal( // switch s.TabRenderingDirection {
lipgloss.Top, // case Left:
renderedTabs..., // for i := s.Client.LastTabIndexInTabBar; i >= 0; i-- {
) // if s.Client.Channels[i].Name == s.Client.ActiveChannel.Name {
// renderedTabs = append([]string{activeTab.Render(s.Client.Channels[i].Name)}, renderedTabs...)
// } else {
// renderedTabs = append([]string{tab.Render(s.Client.Channels[i].Name)}, renderedTabs...)
// }
if lipgloss.Width(tabs) > lipgloss.Width(s.Viewport.View())-lipgloss.Width(leftArrow)-lipgloss.Width(rightArrow) { // tabs = lipgloss.JoinHorizontal(
// set the last tab to be displayed to the index of the previous tab in the loop // lipgloss.Top,
s.Client.LastTabIndexInTabBar = i - 1 // renderedTabs...,
// dont render the newly added tab // )
renderedTabs = renderedTabs[:len(renderedTabs)-1]
break // if lipgloss.Width(tabs) > lipgloss.Width(s.Viewport.View())-lipgloss.Width(leftArrow)-lipgloss.Width(rightArrow) {
} // // set the first tab to be displayed to the index of the previous tab in the loop
} // s.Client.FirstTabIndexInTabBar = i + 1
} // // dont render the newly added tab
// renderedTabs = renderedTabs[1:]
// break
// }
// }
// case Right:
// for i := s.Client.FirstTabIndexInTabBar; i < len(s.Client.Channels); i++ {
// if s.Client.Channels[i].Name == s.Client.ActiveChannel.Name {
// renderedTabs = append(renderedTabs, activeTab.Render(s.Client.Channels[i].Name))
// } else {
// renderedTabs = append(renderedTabs, tab.Render(s.Client.Channels[i].Name))
// }
// tabs = lipgloss.JoinHorizontal(
// lipgloss.Top,
// renderedTabs...,
// )
// if lipgloss.Width(tabs) > lipgloss.Width(s.Viewport.View())-lipgloss.Width(leftArrow)-lipgloss.Width(rightArrow) {
// // set the last tab to be displayed to the index of the previous tab in the loop
// s.Client.LastTabIndexInTabBar = i - 1
// // dont render the newly added tab
// renderedTabs = renderedTabs[:len(renderedTabs)-1]
// break
// }
// }
// }
tabs = lipgloss.JoinHorizontal( tabs = lipgloss.JoinHorizontal(
lipgloss.Top, lipgloss.Top,
@@ -315,13 +361,13 @@ func (s State) View() string {
tabs := s.buildTabBar(leftArrow, rightArrow) tabs := s.buildTabBar(leftArrow, rightArrow)
tabBar := strings.Builder{} tabBar := strings.Builder{}
if s.Client.FirstTabIndexInTabBar != 0 { // if s.Client.FirstTabIndexInTabBar != 0 {
leftArrow = leftArrowLit.Render("❰") // leftArrow = leftArrowLit.Render("❰")
} // }
if s.Client.LastTabIndexInTabBar != len(s.Client.Channels)-1 { // if s.Client.LastTabIndexInTabBar != len(s.Client.Channels)-1 {
rightArrow = rightArrowLit.Render("❱") // rightArrow = rightArrowLit.Render("❱")
} // }
tabBarLine := tabLine.Render( tabBarLine := tabLine.Render(
strings.Repeat( strings.Repeat(
+35 -8
View File
@@ -19,7 +19,10 @@ package mainscreen
import ( import (
"fmt" "fmt"
"math" "math"
"sort"
"strings" "strings"
"unicode"
"unicode/utf8"
"github.com/charmbracelet/bubbles/viewport" "github.com/charmbracelet/bubbles/viewport"
tea "github.com/charmbracelet/bubbletea" tea "github.com/charmbracelet/bubbletea"
@@ -36,25 +39,49 @@ type SidePanelState struct {
func (s *SidePanelState) getHeader() string { func (s *SidePanelState) getHeader() string {
usersCount := 0 usersCount := 0
if len(s.Client.Channels) != 0 { usersCount = len(s.Client.ActiveChannel.Value.Users)
usersCount = len(s.Client.Channels[s.Client.ActiveChannelIndex].Users)
}
separator := strings.Repeat("—", s.Viewport.Width-s.Viewport.Style.GetHorizontalFrameSize()) + "\n" separator := strings.Repeat("—", s.Viewport.Width-s.Viewport.Style.GetHorizontalFrameSize()) + "\n"
header := fmt.Sprintf("Users in this channel (%d)\n", usersCount) + separator header := fmt.Sprintf("%d Users\n", usersCount) + separator
return header return header
} }
// lessCaseInsensitive compares s, t without allocating
func lessCaseInsensitive(s, t string) bool {
for {
if len(t) == 0 {
return false
}
if len(s) == 0 {
return true
}
c, sizec := utf8.DecodeRuneInString(s)
d, sized := utf8.DecodeRuneInString(t)
lowerc := unicode.ToLower(c)
lowerd := unicode.ToLower(d)
if lowerc < lowerd {
return true
}
if lowerc > lowerd {
return false
}
s = s[sizec:]
t = t[sized:]
}
}
func (s *SidePanelState) getLatestNicks() []string { func (s *SidePanelState) getLatestNicks() []string {
nicks := make([]string, 0) nicks := make([]string, 0)
if len(s.Client.Channels) != 0 { for nick, user := range s.Client.ActiveChannel.Value.Users {
for nick, user := range s.Client.Channels[s.Client.ActiveChannelIndex].Users {
_nick := user.Prefix + nick _nick := user.Prefix + nick
nicks = append(nicks, _nick) nicks = append(nicks, _nick)
} }
}
sort.Slice(nicks, func(i, j int) bool { return lessCaseInsensitive(nicks[i], nicks[j]) })
return nicks return nicks
} }
@@ -73,7 +100,7 @@ func (s SidePanelState) Update(msg tea.Msg) (SidePanelState, tea.Cmd) {
var cmdsToProcess []tea.Cmd var cmdsToProcess []tea.Cmd
switch msg := msg.(type) { switch msg := msg.(type) {
case cmds.SwitchChannelsMsg: case cmds.SwitchChannelsMsg, cmds.UpdateNicksMsg:
s.UpdateNicks() s.UpdateNicks()
return s, nil return s, nil
case tea.KeyMsg: case tea.KeyMsg: