mt-multiserver-proxy/chat.go

110 lines
2.3 KiB
Go
Raw Normal View History

2021-09-09 11:03:15 -07:00
package proxy
import (
"fmt"
2021-09-12 04:38:40 -07:00
"io"
2021-09-09 11:03:15 -07:00
"strings"
"time"
"github.com/anon55555/mt"
)
// ChatCmdTimeout is the time needed until a user is warned
// about a chat command that's taking long to execute.
var ChatCmdTimeout = 10 * time.Second
2022-05-14 09:55:56 -07:00
// DoChatMsg handles a chat message string
// as if it was sent by the ClientConn.
2022-05-14 07:24:00 -07:00
func (cc *ClientConn) DoChatMsg(msg string) {
2022-05-11 11:49:36 -07:00
cmd := &mt.ToSrvChatMsg{Msg: msg}
2022-05-10 10:01:29 -07:00
2022-05-14 09:55:56 -07:00
result, isCmd := onChatMsg(cc, cmd)
if result != "" {
cc.SendChatMsg(result)
2022-05-14 07:24:00 -07:00
}
2022-05-14 09:55:56 -07:00
if !isCmd {
2022-05-11 11:49:36 -07:00
cc.server().SendCmd(cmd)
2022-05-10 10:01:29 -07:00
}
}
2021-09-10 03:47:19 -07:00
// SendChatMsg sends a chat message to the ClientConn.
2021-09-09 11:40:36 -07:00
func (cc *ClientConn) SendChatMsg(msg ...string) {
2021-09-09 11:03:15 -07:00
cc.SendCmd(&mt.ToCltChatMsg{
Type: mt.SysMsg,
2021-09-09 11:40:36 -07:00
Text: strings.Join(msg, " "),
2021-09-09 11:03:15 -07:00
Timestamp: time.Now().Unix(),
})
}
2021-09-10 03:47:19 -07:00
// Colorize returns the minetest-colorized version of the input.
2021-09-10 02:25:42 -07:00
func Colorize(text, color string) string {
2022-04-21 04:21:15 -07:00
return string([]rune{0x1b}) + "(c@" + color + ")" + text + string([]rune{0x1b}) + "(c@#FFF)"
2021-09-10 02:25:42 -07:00
}
2021-09-09 11:03:15 -07:00
func onChatMsg(cc *ClientConn, cmd *mt.ToSrvChatMsg) (string, bool) {
initChatCmds()
if strings.HasPrefix(cmd.Msg, Conf().CmdPrefix) {
substrs := strings.Split(cmd.Msg, " ")
cmdName := strings.Replace(substrs[0], Conf().CmdPrefix, "", 1)
var args []string
if len(substrs) > 1 {
args = substrs[1:]
}
v := make([]interface{}, 2+len(args))
v[0] = "command"
v[1] = cmdName
for i, arg := range args {
v[i+2] = arg
}
2021-09-13 03:14:11 -07:00
cc.Log("->", v...)
2021-09-09 11:03:15 -07:00
if !ChatCmdExists(cmdName) {
2021-09-13 03:14:11 -07:00
cc.Log("<-", "unknown command", cmdName)
2021-09-09 11:03:15 -07:00
return "Command not found.", true
}
chatCmdsMu.RLock()
defer chatCmdsMu.RUnlock()
cmd := chatCmds[cmdName]
if !cc.HasPerms(cmd.Perm) {
2021-09-13 03:14:11 -07:00
cc.Log("<-", "deny command", cmdName)
2021-09-09 11:03:15 -07:00
return fmt.Sprintf("Missing permission %s.", cmd.Perm), true
}
2021-09-12 04:38:40 -07:00
return cmd.Handler(cc, nil, args...), true
2021-09-09 11:03:15 -07:00
}
return "", false
}
2021-09-12 04:38:40 -07:00
func onTelnetMsg(tlog func(dir string, v ...interface{}), w io.Writer, msg string) string {
initChatCmds()
substrs := strings.Split(msg, " ")
cmdName := substrs[0]
var args []string
if len(substrs) > 1 {
args = substrs[1:]
}
if !ChatCmdExists(cmdName) {
2021-09-13 03:14:11 -07:00
tlog("<-", "unknown command", cmdName)
2021-09-12 04:38:40 -07:00
return "Command not found.\n"
}
chatCmdsMu.RLock()
defer chatCmdsMu.RUnlock()
cmd := chatCmds[cmdName]
return cmd.Handler(nil, w, args...) + "\n"
}