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

View File

@ -121,12 +121,18 @@ func (sv *Server) dispatch() {
sv.packetsTransferred++
case cl := <-sv.AddClient:
nick := cl.Name()
if _, exists := sv.clients[nick]; exists {
cl.Register(false)
xlog.Info("Client registration failed: '%s'", nick)
} else {
sv.clients[nick] = cl
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:
nick := cl.Name()
cl.Destroy()