diff options
author | Himbeer <himbeer@disroot.org> | 2024-12-07 14:15:18 +0100 |
---|---|---|
committer | Himbeer <himbeer@disroot.org> | 2024-12-07 14:15:18 +0100 |
commit | 56cc91a565dabe8eeedf75c7b381eaeb902e7061 (patch) | |
tree | fc4dae3b8b899e36863480f58362c585093280a3 | |
parent | 2fe0cd17e14473f0d7ffd5a1f5f100bd2419b018 (diff) |
Allow plugins to intercept client-to-server chat messages
-rw-r--r-- | chat.go | 10 | ||||
-rw-r--r-- | plugin_chat.go | 21 | ||||
-rw-r--r-- | process.go | 6 |
3 files changed, 33 insertions, 4 deletions
@@ -81,5 +81,13 @@ func onChatMsg(cc *ClientConn, cmd *mt.ToSrvChatMsg) (string, bool) { return cmd.Handler(cc, args...), true } - return "", false + onChatMsgMu.RLock() + defer onChatMsgMu.RUnlock() + + msg := cmd.Msg + for _, handler := range onChatMsgs { + msg = handler(cc, msg) + } + + return msg, msg == "" } diff --git a/plugin_chat.go b/plugin_chat.go new file mode 100644 index 0000000..a21526f --- /dev/null +++ b/plugin_chat.go @@ -0,0 +1,21 @@ +package proxy + +import "sync" + +var ( + onChatMsgs []func(*ClientConn, string) string + onChatMsgMu sync.RWMutex +) + +// RegisterOnChatMsg registers a handler that is called +// when a client sends a chat message that is not a proxy command. +// The returned string overrides the original message. +// Later handlers will receive the modified message. +// Handlers are called in registration order. +// If the final message is empty, it is not forwarded to the upstream server. +func RegisterOnChatMsg(handler func(*ClientConn, string) string) { + onChatMsgMu.Lock() + defer onChatMsgMu.Unlock() + + onChatMsgs = append(onChatMsgs, handler) +} @@ -462,6 +462,7 @@ func (cc *ClientConn) process(pkt mt.Pkt) { go func(done chan<- struct{}) { result, isCmd := onChatMsg(cc, cmd) if !isCmd { + cmd.Msg = result forward(pkt) } else if result != "" { cc.SendChatMsg(result) @@ -474,9 +475,8 @@ func (cc *ClientConn) process(pkt mt.Pkt) { select { case <-done: case <-time.After(ChatCmdTimeout): - cmdName := strings.Split(cmd.Msg, " ")[0] - cc.Log("<-", "timeout command", cmd.Msg) - cc.SendChatMsg("Command", cmdName, "is taking suspiciously long to execute.") + cc.Log("<-", "timeout chat msg", cmd.Msg) + cc.SendChatMsg("Proxy chat message processing timeout.") } }(done) |