Echo downstream PRIVMSGs to other downstream connections

This commit is contained in:
Simon Ser 2020-03-16 14:28:45 +01:00
parent d5db7c988f
commit 75e698f671
No known key found for this signature in database
GPG Key ID: 0FDE7BE0E88F5E48

View File

@ -6,6 +6,7 @@ import (
"io" "io"
"net" "net"
"strings" "strings"
"sync"
"time" "time"
"golang.org/x/crypto/bcrypt" "golang.org/x/crypto/bcrypt"
@ -69,6 +70,9 @@ type downstreamConn struct {
realname string realname string
password string // empty after authentication password string // empty after authentication
network *network // can be nil network *network // can be nil
lock sync.Mutex
ourMessages map[*irc.Message]struct{}
} }
func newDownstreamConn(srv *Server, netConn net.Conn) *downstreamConn { func newDownstreamConn(srv *Server, netConn net.Conn) *downstreamConn {
@ -80,6 +84,7 @@ func newDownstreamConn(srv *Server, netConn net.Conn) *downstreamConn {
outgoing: make(chan *irc.Message, 64), outgoing: make(chan *irc.Message, 64),
ringMessages: make(chan ringMessage), ringMessages: make(chan ringMessage),
closed: make(chan struct{}), closed: make(chan struct{}),
ourMessages: make(map[*irc.Message]struct{}),
} }
go func() { go func() {
@ -229,6 +234,17 @@ func (dc *downstreamConn) writeMessages() error {
if msg == nil { if msg == nil {
break break
} }
dc.lock.Lock()
_, ours := dc.ourMessages[msg]
delete(dc.ourMessages, msg)
dc.lock.Unlock()
if ours {
// The message comes from our connection, don't echo it
// back
continue
}
msg = msg.Copy() msg = msg.Copy()
switch msg.Command { switch msg.Command {
case "PRIVMSG": case "PRIVMSG":
@ -638,6 +654,12 @@ func (dc *downstreamConn) handleMessageRegistered(msg *irc.Message) error {
Command: "PRIVMSG", Command: "PRIVMSG",
Params: []string{upstreamName, text}, Params: []string{upstreamName, text},
}) })
dc.lock.Lock()
dc.ourMessages[msg] = struct{}{}
dc.lock.Unlock()
uc.ring.Produce(msg)
} }
default: default:
dc.logger.Printf("unhandled message: %v", msg) dc.logger.Printf("unhandled message: %v", msg)