refactor: handle slash commands in a separate function

* quit gorc when "/quit" is sent
* move tea commands into their own package
* other small fixes
This commit is contained in:
2022-01-24 04:23:02 +02:00
parent e34cc39e63
commit 913e76f9db
11 changed files with 161 additions and 133 deletions
+6 -6
View File
@@ -23,10 +23,10 @@ import (
"log"
"strings"
"github.com/illusionman1212/gorc/cmds"
"github.com/illusionman1212/gorc/irc"
"github.com/illusionman1212/gorc/irc/commands"
"github.com/illusionman1212/gorc/irc/parser"
"github.com/illusionman1212/gorc/ui/mainscreen"
)
func ReadLoop(client *irc.Client) {
@@ -83,7 +83,7 @@ func handleJoin(msg parser.IRCMessage, client *irc.Client) {
}
if channel == client.ActiveChannel {
client.Tea.Send(mainscreen.SwitchChannels())
client.Tea.Send(cmds.SwitchChannels())
}
}
@@ -101,7 +101,7 @@ func handleQuit(msg parser.IRCMessage, client *irc.Client) {
delete(client.Channels[i].Users, nick)
}
client.Tea.Send(mainscreen.SwitchChannels())
client.Tea.Send(cmds.SwitchChannels())
}
func handlePart(msg parser.IRCMessage, client *irc.Client) {
@@ -121,7 +121,7 @@ func handlePart(msg parser.IRCMessage, client *irc.Client) {
}
}
client.Tea.Send(mainscreen.SwitchChannels())
client.Tea.Send(cmds.SwitchChannels())
}
func handleWELCOME(msg parser.IRCMessage, client *irc.Client) {
@@ -216,7 +216,7 @@ func handleNAMREPLY(msg parser.IRCMessage, client *irc.Client) {
}
if channel == client.ActiveChannel {
client.Tea.Send(mainscreen.SwitchChannels())
client.Tea.Send(cmds.SwitchChannels())
}
}
@@ -293,5 +293,5 @@ func HandleCommand(msg parser.IRCMessage, client *irc.Client) {
// send a receivedIRCmsg tea message so the ui can update
// we also use this tea message to scroll the viewport down
client.Tea.Send(mainscreen.ReceivedIRCMsg())
client.Tea.Send(cmds.ReceivedIRCMsg())
}
+78
View File
@@ -0,0 +1,78 @@
// gorc project
// Copyright (C) 2021 IllusionMan1212
//
// 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.
package handler
import (
"strings"
tea "github.com/charmbracelet/bubbletea"
"github.com/illusionman1212/gorc/cmds"
"github.com/illusionman1212/gorc/irc"
"github.com/illusionman1212/gorc/irc/commands"
)
func handleSlashJoin(params []string, client *irc.Client) tea.Cmd {
// TODO: only add channel to client slice when
// we get a JOIN command from the server with our nickname
// TODO: handle comma separated channels
var cmdsToProcess []tea.Cmd
channel := ""
if len(params) > 0 {
channel = params[0]
}
client.ActiveChannel = channel
for i, c := range client.Channels {
if c.Name == channel {
client.ActiveChannelIndex = i
break
} else if i == len(client.Channels)-1 {
client.Channels = append(client.Channels, irc.Channel{
Name: channel,
Users: make(map[string]irc.User),
})
client.ActiveChannelIndex = len(client.Channels) - 1
cmdsToProcess = append(cmdsToProcess, cmds.UpdateTabBar)
}
}
client.SendCommand(commands.JOIN, params...)
cmdsToProcess = append(cmdsToProcess, cmds.SwitchChannels)
return tea.Batch(cmdsToProcess...)
}
func HandleSlashCommand(msg string, client *irc.Client) tea.Cmd {
substrs := strings.Fields(msg[1:])
command := strings.ToUpper(substrs[0])
var params []string
if len(substrs) > 1 {
params = substrs[1:]
}
switch command {
case commands.JOIN:
return handleSlashJoin(params, client)
case commands.QUIT:
client.SendCommand(command, params...)
return cmds.Quit(client)
default:
client.SendCommand(command, params...)
return nil
}
}