mt-multiserver-proxy/plugin_chatcmd.go

67 lines
1.2 KiB
Go
Raw Permalink Normal View History

package proxy
2021-09-12 04:38:40 -07:00
import (
"io"
"sync"
)
2021-09-10 03:47:19 -07:00
// A ChatCmd holds information on how to handle a chat command.
2021-09-09 07:23:42 -07:00
type ChatCmd struct {
2021-09-12 05:46:22 -07:00
Name string
Perm string
Help string
Usage string
TelnetUsage string
Handler func(*ClientConn, io.Writer, ...string) string
2021-09-09 07:23:42 -07:00
}
var chatCmds map[string]ChatCmd
var chatCmdsMu sync.RWMutex
var chatCmdsOnce sync.Once
2021-09-10 03:47:19 -07:00
// ChatCmds returns a map of all ChatCmds indexed by their names.
2021-09-10 01:48:25 -07:00
func ChatCmds() map[string]ChatCmd {
initChatCmds()
chatCmdsMu.RLock()
defer chatCmdsMu.RUnlock()
2021-09-10 01:48:25 -07:00
cmds := make(map[string]ChatCmd)
for name, cmd := range chatCmds {
cmds[name] = cmd
}
return cmds
}
2021-09-10 03:47:19 -07:00
// ChatCmdExists reports if a ChatCmd exists.
2021-09-10 01:48:25 -07:00
func ChatCmdExists(name string) bool {
2021-09-10 03:47:19 -07:00
_, ok := ChatCmds()[name]
return ok
}
2021-09-10 03:47:19 -07:00
// RegisterChatCmd adds a new ChatCmd. It returns true on success
// and false if a command with the same name already exists.
2021-09-09 07:23:42 -07:00
func RegisterChatCmd(cmd ChatCmd) bool {
initChatCmds()
2021-09-09 07:23:42 -07:00
if ChatCmdExists(cmd.Name) {
return false
}
chatCmdsMu.Lock()
defer chatCmdsMu.Unlock()
2021-09-09 07:23:42 -07:00
chatCmds[cmd.Name] = cmd
return true
}
func initChatCmds() {
chatCmdsOnce.Do(func() {
chatCmdsMu.Lock()
defer chatCmdsMu.Unlock()
chatCmds = make(map[string]ChatCmd)
})
}