diff --git a/server.go b/server.go index 9af5fc1..1c23a23 100644 --- a/server.go +++ b/server.go @@ -171,47 +171,9 @@ func (sv *Server) dispatcher() (err error) { sv.recvMsg(msg) sv.packetsTransferred++ case cl := <-sv.addq: - clid := strings.ToLower(cl.Name()) - if _, exists := sv.clients[clid]; exists { - cl.Receive(irc.M(sv.host, ERR_NICKNAMEINUSE, cl.Name(), "Nickname is already in use")) - go func() { - time.Sleep(5 * time.Second) - cl.Register(false) - }() - xlog.Info("Client registration failed: '%s' (client exists)", clid) - continue - } - if !sv.authCallback(cl.Name(), cl.Password()) { - cl.Receive(irc.M(sv.host, ERR_PASSWDMISMATCH, "", "Password incorrect")) - go func() { - time.Sleep(5 * time.Second) - cl.Register(false) - }() - xlog.Info("Client registration failed: '%s' (wrong password)", clid) - continue - } - sv.clients[clid] = cl - sv.clients[clid] = cl - sv.sendLogon(cl.Name()) - sv.connectionsCurrent = float64(len(sv.clients)) - cl.Register(true) - xlog.Info("Client registered: '%s'", clid) - xlog.Info("Server has %d client(s)", len(sv.clients)) - xlog.Debug("Goroutines running: %d", runtime.NumGoroutine()) + sv.addClient(cl) case cl := <-sv.delq: - clid := strings.ToLower(cl.Name()) - cl.Destroy() - for chname, ch := range sv.chUsers { - if _, exists := ch[clid]; exists { - delete(ch, clid) - sv.sendMsg(irc.M(cl.Name(), "PART", chname, "quit")) - } - } - delete(sv.clients, clid) - sv.connectionsCurrent = float64(len(sv.clients)) - xlog.Info("Client deleted: '%s'", clid) - xlog.Info("Server has %d client(s)", len(sv.clients)) - xlog.Debug("Goroutines running: %d", runtime.NumGoroutine()) + sv.delClient(cl) default: time.Sleep(100 * time.Microsecond) } @@ -228,6 +190,52 @@ func (sv *Server) loadConfig() { sv.config = cfg } +func (sv *Server) addClient(cl Client) { + clid := strings.ToLower(cl.Name()) + if _, exists := sv.clients[clid]; exists { + cl.Receive(irc.M(sv.host, ERR_NICKNAMEINUSE, cl.Name(), "Nickname is already in use")) + go func() { + time.Sleep(5 * time.Second) + cl.Register(false) + }() + xlog.Info("Client registration failed: '%s' (client exists)", clid) + return + } + if !sv.authCallback(cl.Name(), cl.Password()) { + cl.Receive(irc.M(sv.host, ERR_PASSWDMISMATCH, "", "Password incorrect")) + go func() { + time.Sleep(5 * time.Second) + cl.Register(false) + }() + xlog.Info("Client registration failed: '%s' (wrong password)", clid) + return + } + sv.clients[clid] = cl + sv.clients[clid] = cl + sv.sendLogon(cl.Name()) + sv.connectionsCurrent = float64(len(sv.clients)) + cl.Register(true) + xlog.Info("Client registered: '%s'", clid) + xlog.Info("Server has %d client(s)", len(sv.clients)) + xlog.Debug("Goroutines running: %d", runtime.NumGoroutine()) +} + +func (sv *Server) delClient(cl Client) { + clid := strings.ToLower(cl.Name()) + cl.Destroy() + for chname, ch := range sv.chUsers { + if _, exists := ch[clid]; exists { + delete(ch, clid) + sv.sendMsg(irc.M(cl.Name(), "PART", chname, "quit")) + } + } + delete(sv.clients, clid) + sv.connectionsCurrent = float64(len(sv.clients)) + xlog.Info("Client deleted: '%s'", clid) + xlog.Info("Server has %d client(s)", len(sv.clients)) + xlog.Debug("Goroutines running: %d", runtime.NumGoroutine()) +} + func (sv *Server) recvMsg(msg *irc.Message) { cmd := msg.Cmd hook, exists := svCommandHooks[cmd]