Improved client registration handling

This commit is contained in:
Andreas Neue 2016-07-21 10:30:44 +02:00
parent e70874e0d6
commit eb8f0ac3d8
2 changed files with 32 additions and 16 deletions

View File

@ -16,13 +16,15 @@ import (
type Client interface { type Client interface {
Name() string Name() string
Register(bool)
Send(*irc.Message) Send(*irc.Message)
Receive(*irc.Message) Receive(*irc.Message)
//Register() chan bool
Destroy() Destroy()
} }
type RemoteClient struct { type RemoteClient struct {
Registered chan bool
server *Server server *Server
name string name string
@ -34,8 +36,8 @@ type RemoteClient struct {
isAuthed bool isAuthed bool
isClosed bool isClosed bool
receive chan *irc.Message receive chan *irc.Message
register chan bool registered chan bool
conn net.Conn conn net.Conn
writeq chan string writeq chan string
@ -43,6 +45,7 @@ type RemoteClient struct {
func NewRemoteClient(sv *Server, conn net.Conn) *RemoteClient { func NewRemoteClient(sv *Server, conn net.Conn) *RemoteClient {
cl := new(RemoteClient) cl := new(RemoteClient)
cl.server = sv cl.server = sv
cl.name = "" cl.name = ""
@ -50,7 +53,7 @@ func NewRemoteClient(sv *Server, conn net.Conn) *RemoteClient {
cl.modes = "" cl.modes = ""
cl.receive = make(chan *irc.Message) cl.receive = make(chan *irc.Message)
//cl.register = make(chan bool) cl.registered = make(chan bool)
cl.isRegistered = false cl.isRegistered = false
cl.isAuthed = false cl.isAuthed = false
@ -71,6 +74,10 @@ func (cl *RemoteClient) Name() string {
return cl.name return cl.name
} }
func (cl *RemoteClient) Register(success bool) {
cl.registered <- success
}
func (cl *RemoteClient) Send(msg *irc.Message) { func (cl *RemoteClient) Send(msg *irc.Message) {
msg.Pre = cl.name msg.Pre = cl.name
cl.server.Dispatch <- msg cl.server.Dispatch <- msg
@ -116,6 +123,13 @@ func (cl *RemoteClient) dispatch() {
select { select {
case msg := <-cl.receive: case msg := <-cl.receive:
cl.writeMsg(msg) cl.writeMsg(msg)
case success := <-cl.registered:
if !success {
cl.Destroy()
xlog.Debug("User registration failed: '%s'", cl.name)
return
}
xlog.Debug("User '%s' registered", cl.name)
default: default:
continue continue
} }
@ -177,6 +191,7 @@ func (cl *RemoteClient) writeLine(format string, a ...interface{}) {
func (cl *RemoteClient) handleCmd(s string) { func (cl *RemoteClient) handleCmd(s string) {
xlog.Debug("handleCmd: [%s] '%s'", cl.name, s) xlog.Debug("handleCmd: [%s] '%s'", cl.name, s)
msg := irc.Parse(s) msg := irc.Parse(s)
msg.Cmd = strings.ToUpper(msg.Cmd)
if cl.name != "" { if cl.name != "" {
cl.Send(msg) cl.Send(msg)
return return
@ -187,13 +202,8 @@ func (cl *RemoteClient) handleCmd(s string) {
cl.password = msg.Args[0] cl.password = msg.Args[0]
case "NICK": case "NICK":
cl.name = msg.Args[0] cl.name = msg.Args[0]
if _, exists := cl.server.clients[cl.name]; exists {
cl.Destroy()
xlog.Debug("User registration failed: '%s'", msg.Args[0])
return
}
cl.server.AddClient <- cl cl.server.AddClient <- cl
xlog.Debug("User '%s' registered", msg.Args[0])
case "USER": case "USER":
} }
} }

View File

@ -121,12 +121,18 @@ func (sv *Server) dispatch() {
sv.packetsTransferred++ sv.packetsTransferred++
case cl := <-sv.AddClient: case cl := <-sv.AddClient:
nick := cl.Name() nick := cl.Name()
sv.clients[nick] = cl if _, exists := sv.clients[nick]; exists {
sv.sendLogon(cl.Name()) cl.Register(false)
sv.connectionsCurrent = float64(len(sv.clients)) xlog.Info("Client registration failed: '%s'", nick)
xlog.Info("Client registered: '%s'", nick) } else {
xlog.Info("Server has %d client(s)", len(sv.clients)) sv.clients[nick] = cl
xlog.Debug("Goroutines running: %d", runtime.NumGoroutine()) sv.sendLogon(cl.Name())
sv.connectionsCurrent = float64(len(sv.clients))
cl.Register(true)
xlog.Info("Client registered: '%s'", nick)
xlog.Info("Server has %d client(s)", len(sv.clients))
xlog.Debug("Goroutines running: %d", runtime.NumGoroutine())
}
case cl := <-sv.DelClient: case cl := <-sv.DelClient:
nick := cl.Name() nick := cl.Name()
cl.Destroy() cl.Destroy()