2021-09-06 11:07:57 -07:00
|
|
|
package proxy
|
|
|
|
|
2021-09-12 04:38:40 -07:00
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"sync"
|
|
|
|
)
|
2021-09-06 11:07:57 -07:00
|
|
|
|
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
|
|
|
}
|
2021-09-06 11:07:57 -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 {
|
2021-09-06 11:07:57 -07:00
|
|
|
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]
|
2021-09-06 11:07:57 -07:00
|
|
|
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 {
|
2021-09-06 11:07:57 -07:00
|
|
|
initChatCmds()
|
|
|
|
|
2021-09-09 07:23:42 -07:00
|
|
|
if ChatCmdExists(cmd.Name) {
|
2021-09-06 11:07:57 -07:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
chatCmdsMu.Lock()
|
|
|
|
defer chatCmdsMu.Unlock()
|
|
|
|
|
2021-09-09 07:23:42 -07:00
|
|
|
chatCmds[cmd.Name] = cmd
|
2021-09-06 11:07:57 -07:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func initChatCmds() {
|
|
|
|
chatCmdsOnce.Do(func() {
|
|
|
|
chatCmdsMu.Lock()
|
|
|
|
defer chatCmdsMu.Unlock()
|
|
|
|
|
|
|
|
chatCmds = make(map[string]ChatCmd)
|
|
|
|
})
|
|
|
|
}
|