From 059a799d160103d47b57b4ab7b495132629053b9 Mon Sep 17 00:00:00 2001 From: Simon Ser Date: Fri, 7 Feb 2020 11:46:44 +0100 Subject: [PATCH] Add user.forEachUpstream --- downstream.go | 14 +++++--------- server.go | 11 +++++++++++ 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/downstream.go b/downstream.go index e0d4f40..228502e 100644 --- a/downstream.go +++ b/downstream.go @@ -4,6 +4,7 @@ import ( "fmt" "io" "net" + "strings" "gopkg.in/irc.v3" ) @@ -167,8 +168,9 @@ func (c *downstreamConn) handleMessageUnregistered(msg *irc.Message) error { } func (c *downstreamConn) register() error { - u := c.srv.getUser(c.username) + u := c.srv.getUser(strings.TrimPrefix(c.username, "~")) if u == nil { + c.logger.Printf("failed authentication: unknown username %q", c.username) c.messages <- &irc.Message{ Prefix: c.srv.prefix(), Command: irc.ERR_PASSWDMISMATCH, @@ -206,20 +208,14 @@ func (c *downstreamConn) register() error { Params: []string{c.nick, "No MOTD"}, } - u.lock.Lock() - for _, uc := range u.upstreamConns { + u.forEachUpstream(func(uc *upstreamConn) { // TODO: fix races accessing upstream connection data - if !uc.registered { - continue - } for _, ch := range uc.channels { if ch.complete { forwardChannel(c, ch) } } - } - u.lock.Unlock() - + }) return nil } diff --git a/server.go b/server.go index 3076eb8..ec30a38 100644 --- a/server.go +++ b/server.go @@ -39,6 +39,17 @@ type user struct { 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 { Addr string Nick string