aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHimbeer <himbeer@disroot.org>2024-12-07 14:15:18 +0100
committerHimbeer <himbeer@disroot.org>2024-12-07 14:15:18 +0100
commit56cc91a565dabe8eeedf75c7b381eaeb902e7061 (patch)
treefc4dae3b8b899e36863480f58362c585093280a3
parent2fe0cd17e14473f0d7ffd5a1f5f100bd2419b018 (diff)
Allow plugins to intercept client-to-server chat messages
-rw-r--r--chat.go10
-rw-r--r--plugin_chat.go21
-rw-r--r--process.go6
3 files changed, 33 insertions, 4 deletions
diff --git a/chat.go b/chat.go
index 2a8cda4..0578cb2 100644
--- a/chat.go
+++ b/chat.go
@@ -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)
+}
diff --git a/process.go b/process.go
index b7623b5..88c999f 100644
--- a/process.go
+++ b/process.go
@@ -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)