ssh-chat/chat/command.go

205 lines
4.3 KiB
Go
Raw Normal View History

package chat
import (
"errors"
2014-12-26 23:34:13 +01:00
"fmt"
"sort"
"strings"
2014-12-26 23:34:13 +01:00
"sync"
)
2014-12-27 02:59:29 +01:00
// The error returned when an invalid command is issued.
var ErrInvalidCommand = errors.New("invalid command")
2014-12-27 02:59:29 +01:00
// The error returned when a command is given without an owner.
var ErrNoOwner = errors.New("command without owner")
2014-12-27 02:59:29 +01:00
// The error returned when a command is performed without the necessary number
// of arguments.
2014-12-26 23:34:13 +01:00
var ErrMissingArg = errors.New("missing argument")
2015-01-02 00:37:28 +01:00
// The error returned when a command is added without a prefix.
var ErrMissingPrefix = errors.New("command missing prefix")
// Command is a definition of a handler for a command.
type Command struct {
Prefix string
PrefixHelp string
Help string
Handler func(*Channel, CommandMsg) error
}
2014-12-27 02:59:29 +01:00
// Commands is a registry of available commands.
2014-12-26 23:34:13 +01:00
type Commands struct {
2015-01-02 00:37:28 +01:00
commands map[string]Command
2014-12-26 23:34:13 +01:00
sync.RWMutex
}
2014-12-27 02:59:29 +01:00
// NewCommands returns a new Commands registry.
2014-12-26 23:34:13 +01:00
func NewCommands() *Commands {
return &Commands{
2015-01-02 00:37:28 +01:00
commands: map[string]Command{},
2014-12-26 23:34:13 +01:00
}
}
2014-12-27 02:59:29 +01:00
// Add will register a command. If help string is empty, it will be hidden from
// Help().
2015-01-02 00:37:28 +01:00
func (c *Commands) Add(cmd Command) error {
2014-12-26 23:34:13 +01:00
c.Lock()
defer c.Unlock()
2015-01-02 00:37:28 +01:00
if cmd.Prefix == "" {
return ErrMissingPrefix
2014-12-26 23:34:13 +01:00
}
2015-01-02 00:37:28 +01:00
c.commands[cmd.Prefix] = cmd
return nil
2014-12-26 23:34:13 +01:00
}
2014-12-26 23:34:13 +01:00
// Alias will add another command for the same handler, won't get added to help.
2015-01-02 00:37:28 +01:00
func (c *Commands) Alias(command string, alias string) error {
2014-12-26 23:34:13 +01:00
c.Lock()
defer c.Unlock()
2015-01-02 00:37:28 +01:00
cmd, ok := c.commands[command]
2014-12-26 23:34:13 +01:00
if !ok {
return ErrInvalidCommand
}
2015-01-02 00:37:28 +01:00
c.commands[alias] = cmd
2014-12-26 23:34:13 +01:00
return nil
}
2014-12-27 02:59:29 +01:00
// Run executes a command message.
2015-01-02 00:37:28 +01:00
func (c *Commands) Run(channel *Channel, msg CommandMsg) error {
2014-12-27 02:59:29 +01:00
if msg.From == nil {
return ErrNoOwner
}
2014-12-26 23:34:13 +01:00
c.RLock()
defer c.RUnlock()
2015-01-02 00:37:28 +01:00
cmd, ok := c.commands[msg.Command()]
if !ok {
return ErrInvalidCommand
}
2015-01-02 00:37:28 +01:00
return cmd.Handler(channel, msg)
}
2014-12-26 23:34:13 +01:00
// Help will return collated help text as one string.
2015-01-02 00:37:28 +01:00
func (c *Commands) Help() string {
2014-12-26 23:34:13 +01:00
c.RLock()
defer c.RUnlock()
2014-12-27 01:05:04 +01:00
r := []string{}
2015-01-02 00:37:28 +01:00
for _, cmd := range c.commands {
r = append(r, fmt.Sprintf("%s %s - %s", cmd.Prefix, cmd.PrefixHelp, cmd.Help))
2014-12-26 23:34:13 +01:00
}
sort.Strings(r)
return strings.Join(r, Newline)
}
var defaultCmdHandlers *Commands
func init() {
2014-12-26 23:34:13 +01:00
c := NewCommands()
2015-01-02 00:37:28 +01:00
c.Add(Command{
Prefix: "/help",
Handler: func(channel *Channel, msg CommandMsg) error {
channel.Send(NewSystemMsg("Available commands:"+Newline+c.Help(), msg.From()))
return nil
},
2014-12-27 01:05:04 +01:00
})
2015-01-02 00:37:28 +01:00
c.Add(Command{
Prefix: "/me",
Handler: func(channel *Channel, msg CommandMsg) error {
me := strings.TrimLeft(msg.body, "/me")
if me == "" {
me = " is at a loss for words."
} else {
me = me[1:]
}
2015-01-02 00:37:28 +01:00
channel.Send(NewEmoteMsg(me, msg.From()))
return nil
},
})
2015-01-02 00:37:28 +01:00
c.Add(Command{
Prefix: "/exit",
Help: "Exit the chat.",
Handler: func(channel *Channel, msg CommandMsg) error {
msg.From().Close()
return nil
},
2014-12-26 23:34:13 +01:00
})
c.Alias("/exit", "/quit")
2015-01-02 00:37:28 +01:00
c.Add(Command{
Prefix: "/nick",
PrefixHelp: "NAME",
Help: "Rename yourself.",
Handler: func(channel *Channel, msg CommandMsg) error {
args := msg.Args()
if len(args) != 1 {
return ErrMissingArg
}
u := msg.From()
oldName := u.Name()
u.SetName(args[0])
2014-12-27 01:05:04 +01:00
2015-01-02 00:37:28 +01:00
body := fmt.Sprintf("%s is now known as %s.", oldName, u.Name())
channel.Send(NewAnnounceMsg(body))
return nil
},
2014-12-26 23:34:13 +01:00
})
2015-01-02 00:37:28 +01:00
c.Add(Command{
Prefix: "/names",
Help: "List users who are connected.",
Handler: func(channel *Channel, msg CommandMsg) error {
// TODO: colorize
names := channel.NamesPrefix("")
body := fmt.Sprintf("%d connected: %s", len(names), strings.Join(names, ", "))
channel.Send(NewSystemMsg(body, msg.From()))
return nil
2015-01-02 00:37:28 +01:00
},
})
c.Alias("/names", "/list")
2015-01-02 00:37:28 +01:00
c.Add(Command{
Prefix: "/theme",
PrefixHelp: "[mono|colors]",
Help: "Set your color theme.",
Handler: func(channel *Channel, msg CommandMsg) error {
user := msg.From()
args := msg.Args()
if len(args) == 0 {
theme := "plain"
if user.Config.Theme != nil {
theme = user.Config.Theme.Id()
}
body := fmt.Sprintf("Current theme: %s", theme)
channel.Send(NewSystemMsg(body, user))
return nil
}
2015-01-02 00:37:28 +01:00
id := args[0]
for _, t := range Themes {
if t.Id() == id {
user.Config.Theme = &t
body := fmt.Sprintf("Set theme: %s", id)
channel.Send(NewSystemMsg(body, user))
return nil
}
}
return errors.New("theme not found")
},
})
defaultCmdHandlers = c
}