Add user.forEachUpstream

This commit is contained in:
Simon Ser 2020-02-07 11:46:44 +01:00
parent 3586ca3d26
commit 059a799d16
No known key found for this signature in database
GPG key ID: 0FDE7BE0E88F5E48
2 changed files with 16 additions and 9 deletions

View file

@ -4,6 +4,7 @@ import (
"fmt" "fmt"
"io" "io"
"net" "net"
"strings"
"gopkg.in/irc.v3" "gopkg.in/irc.v3"
) )
@ -167,8 +168,9 @@ func (c *downstreamConn) handleMessageUnregistered(msg *irc.Message) error {
} }
func (c *downstreamConn) register() error { func (c *downstreamConn) register() error {
u := c.srv.getUser(c.username) u := c.srv.getUser(strings.TrimPrefix(c.username, "~"))
if u == nil { if u == nil {
c.logger.Printf("failed authentication: unknown username %q", c.username)
c.messages <- &irc.Message{ c.messages <- &irc.Message{
Prefix: c.srv.prefix(), Prefix: c.srv.prefix(),
Command: irc.ERR_PASSWDMISMATCH, Command: irc.ERR_PASSWDMISMATCH,
@ -206,20 +208,14 @@ func (c *downstreamConn) register() error {
Params: []string{c.nick, "No MOTD"}, Params: []string{c.nick, "No MOTD"},
} }
u.lock.Lock() u.forEachUpstream(func(uc *upstreamConn) {
for _, uc := range u.upstreamConns {
// TODO: fix races accessing upstream connection data // TODO: fix races accessing upstream connection data
if !uc.registered {
continue
}
for _, ch := range uc.channels { for _, ch := range uc.channels {
if ch.complete { if ch.complete {
forwardChannel(c, ch) forwardChannel(c, ch)
} }
} }
} })
u.lock.Unlock()
return nil return nil
} }

View file

@ -39,6 +39,17 @@ type user struct {
upstreamConns []*upstreamConn upstreamConns []*upstreamConn
} }
func (u *user) forEachUpstream(f func(uc *upstreamConn)) {
u.lock.Lock()
for _, uc := range u.upstreamConns {
if !uc.registered {
continue
}
f(uc)
}
u.lock.Unlock()
}
type Upstream struct { type Upstream struct {
Addr string Addr string
Nick string Nick string