Extracted each command to a separate method

This commit is contained in:
Peter Hellberg 2014-12-16 01:36:40 +01:00
parent 21abb26040
commit 84ff8ea4e7

187
client.go
View File

@ -210,36 +210,116 @@ func (c *Client) handleShell(channel ssh.Channel) {
isCmd := strings.HasPrefix(parts[0], "/")
if isCmd {
// TODO: Factor this out.
switch parts[0] {
case "/test-colors": // Shh, this command is a secret!
c.Write(ColorString("32", "Lorem ipsum dolor sit amet,"))
c.Write("consectetur " + ColorString("31;1", "adipiscing") + " elit.")
c.testColorsCommand()
case "/exit":
channel.Close()
case "/help":
c.helpCommand()
case "/about":
c.aboutCommand()
case "/uptime":
c.uptimeCommand()
case "/beep":
c.beepCommand()
case "/me":
c.meCommand(line)
case "/slap":
c.slapCommand(parts)
case "/nick":
c.nickCommand(parts)
case "/whois":
c.whoisCommand(parts)
case "/names", "/list":
c.listCommand()
case "/ban":
c.banCommand(parts)
case "/op":
c.opCommand(parts)
case "/kick":
c.kickCommand(parts)
case "/silence":
c.silenceCommand(parts)
case "/shutdown":
c.shutdownCommand(line)
case "/msg":
// Make sure we have a recipient and a message
if len(parts) < 2 {
c.SysMsg("Missing $NAME from: /msg $NAME $MESSAGE")
break
} else if len(parts) < 3 {
c.SysMsg("Missing $MESSAGE from: /msg $NAME $MESSAGE")
break
}
c.msgCommand(parts)
case "/motd":
c.motdCommand(parts)
case "/theme":
c.themeCommand(parts)
case "/whitelist":
c.whitelistCommand(parts)
default:
c.SysMsg("Invalid command: %s", line)
}
continue
}
msg := fmt.Sprintf("%s: %s", c.ColoredName(), line)
// Rate limit
if time.Now().Sub(c.lastTX) < RequiredWait {
c.SysMsg("Rate limiting in effect.")
continue
}
if c.IsSilenced() || len(msg) > 1000 || len(line) < 1 {
c.SysMsg("Message rejected.")
continue
}
c.Server.Broadcast(msg, c)
c.lastTX = time.Now()
}
}
func (c *Client) testColorsCommand() {
c.Write(ColorString("32", "Lorem ipsum dolor sit amet,"))
c.Write("consectetur " + ColorString("31;1", "adipiscing") + " elit.")
}
func (c *Client) helpCommand() {
c.WriteLines(strings.Split(HelpText, "\n"))
if c.Server.IsOp(c) {
c.WriteLines(strings.Split(OpHelpText, "\n"))
}
case "/about":
}
func (c *Client) aboutCommand() {
c.WriteLines(strings.Split(AboutText, "\n"))
case "/uptime":
}
func (c *Client) uptimeCommand() {
c.Write(c.Server.Uptime())
case "/beep":
}
func (c *Client) beepCommand() {
c.beepMe = !c.beepMe
if c.beepMe {
c.SysMsg("I'll beep you good.")
} else {
c.SysMsg("No more beeps. :(")
}
case "/me":
}
func (c *Client) meCommand(line string) {
me := strings.TrimLeft(line, "/me")
if me == "" {
me = " is at a loss for words."
}
c.Emote(me)
case "/slap":
}
func (c *Client) slapCommand(parts []string) {
slappee := "themself"
if len(parts) > 1 {
slappee = parts[1]
@ -248,13 +328,17 @@ func (c *Client) handleShell(channel ssh.Channel) {
}
}
c.Emote(fmt.Sprintf(" slaps %s around a bit with a large trout.", slappee))
case "/nick":
}
func (c *Client) nickCommand(parts []string) {
if len(parts) == 2 {
c.Server.Rename(c, parts[1])
} else {
c.SysMsg("Missing $NAME from: /nick $NAME")
}
case "/whois":
}
func (c *Client) whoisCommand(parts []string) {
if len(parts) == 2 {
client := c.Server.Who(parts[1])
if client != nil {
@ -269,17 +353,24 @@ func (c *Client) handleShell(channel ssh.Channel) {
} else {
c.SysMsg("Missing $NAME from: /whois $NAME")
}
case "/names", "/list":
}
func (c *Client) listCommand() {
names := ""
nameList := c.Server.List(nil)
for _, name := range nameList {
names += c.Server.Who(name).ColoredName() + systemMessageFormat + ", "
}
if len(names) > 2 {
names = names[:len(names)-2]
}
c.SysMsg("%d connected: %s", len(nameList), names)
case "/ban":
}
func (c *Client) banCommand(parts []string) {
if !c.Server.IsOp(c) {
c.SysMsg("You're not an admin.")
} else if len(parts) != 2 {
@ -296,7 +387,9 @@ func (c *Client) handleShell(channel ssh.Channel) {
c.Server.Broadcast(fmt.Sprintf("* %s was banned by %s", parts[1], c.ColoredName()), nil)
}
}
case "/op":
}
func (c *Client) opCommand(parts []string) {
if !c.Server.IsOp(c) {
c.SysMsg("You're not an admin.")
} else if len(parts) != 2 {
@ -311,7 +404,9 @@ func (c *Client) handleShell(channel ssh.Channel) {
c.Server.Op(fingerprint)
}
}
case "/kick":
}
func (c *Client) kickCommand(parts []string) {
if !c.Server.IsOp(c) {
c.SysMsg("You're not an admin.")
} else if len(parts) != 2 {
@ -320,13 +415,16 @@ func (c *Client) handleShell(channel ssh.Channel) {
client := c.Server.Who(parts[1])
if client == nil {
c.SysMsg("No such name: %s", parts[1])
} else {
return
}
client.SysMsg("Kicked by %s.", c.ColoredName())
client.Conn.Close()
c.Server.Broadcast(fmt.Sprintf("* %s was kicked by %s", parts[1], c.ColoredName()), nil)
}
}
case "/silence":
}
func (c *Client) silenceCommand(parts []string) {
if !c.Server.IsOp(c) {
c.SysMsg("You're not an admin.")
} else if len(parts) < 2 {
@ -347,17 +445,21 @@ func (c *Client) handleShell(channel ssh.Channel) {
client.SysMsg("Silenced for %s by %s.", duration, c.ColoredName())
}
}
case "/shutdown":
}
func (c *Client) shutdownCommand(line string) {
if !c.Server.IsOp(c) {
c.SysMsg("You're not an admin.")
} else {
var split = strings.SplitN(line, " ", 2)
var msg string
if len(split) > 1 {
msg = split[1]
} else {
msg = ""
}
// Shutdown after 5 seconds
go func() {
c.Server.Broadcast(ColorString("31", msg), nil)
@ -365,35 +467,35 @@ func (c *Client) handleShell(channel ssh.Channel) {
c.Server.Stop()
}()
}
case "/msg": /* Send a PM */
/* Make sure we have a recipient and a message */
if len(parts) < 2 {
c.SysMsg("Missing $NAME from: /msg $NAME $MESSAGE")
break
} else if len(parts) < 3 {
c.SysMsg("Missing $MESSAGE from: /msg $NAME $MESSAGE")
break
}
/* Ask the server to send the message */
}
func (c *Client) msgCommand(parts []string) {
// Ask the server to send the message
if err := c.Server.Privmsg(parts[1], parts[2], c); nil != err {
c.SysMsg("Unable to send message to %v: %v", parts[1], err)
}
case "/motd": /* print motd */
}
func (c *Client) motdCommand(parts []string) {
if !c.Server.IsOp(c) {
c.Server.MotdUnicast(c)
} else if len(parts) < 2 {
c.Server.MotdUnicast(c)
} else {
var newmotd string
if len(parts) == 2 {
newmotd = parts[1]
} else {
newmotd = parts[1] + " " + parts[2]
}
c.Server.SetMotd(newmotd)
c.Server.MotdBroadcast(c)
}
case "/theme":
}
func (c *Client) themeCommand(parts []string) {
if len(parts) < 2 {
c.SysMsg("Missing $THEME from: /theme $THEME")
c.SysMsg("Choose either color or mono")
@ -404,11 +506,13 @@ func (c *Client) handleShell(channel ssh.Channel) {
} else if parts[1] == "color" {
c.colorMe = true
}
// Rename to reset prompt
c.Rename(c.Name)
}
}
case "/whitelist": /* whitelist a fingerprint */
func (c *Client) whitelistCommand(parts []string) {
if !c.Server.IsOp(c) {
c.SysMsg("You're not an admin.")
} else if len(parts) != 2 {
@ -418,27 +522,6 @@ func (c *Client) handleShell(channel ssh.Channel) {
c.Server.Whitelist(fingerprint)
c.SysMsg("Added %s to the whitelist", fingerprint)
}
default:
c.SysMsg("Invalid command: %s", line)
}
continue
}
msg := fmt.Sprintf("%s: %s", c.ColoredName(), line)
/* Rate limit */
if time.Now().Sub(c.lastTX) < RequiredWait {
c.SysMsg("Rate limiting in effect.")
continue
}
if c.IsSilenced() || len(msg) > 1000 || len(line) < 1 {
c.SysMsg("Message rejected.")
continue
}
c.Server.Broadcast(msg, c)
c.lastTX = time.Now()
}
}
func (c *Client) handleChannels(channels <-chan ssh.NewChannel) {