diff --git a/server.go b/server.go index 27b3d06..e73dfb3 100644 --- a/server.go +++ b/server.go @@ -11,19 +11,16 @@ import ( ) type Server struct { - //eventChan chan Event - running bool - name string + name string + motd string clientConn net.Conn clientNick string clientRegistered bool - clientOperator bool upstreamLauncher libnmdc.HubConnectionOptions upstreamCloser chan struct{} upstream *libnmdc.HubConnection - motd string } func NewServer(name string, upstream libnmdc.HubAddress, conn net.Conn) *Server { @@ -55,11 +52,18 @@ func (s *Server) RunWorker() { ln, err := s.clientConn.Read(buf) if err != nil { if err == io.EOF { - s.DisconnectClient() + s.DisconnectClient() // if not already done + + // Cleanup upstream if s.clientConn != nil && s.clientRegistered { s.upstreamCloser <- struct{}{} } - return // FIXME cleanup + + // Clean up ourselves + s.clientRegistered = false + + // Abandon thread + return } continue } @@ -146,8 +150,7 @@ func (s *Server) handleCommand(command string, args []string) { s.reply(rplVersion, VERSION) case "PASS": - // RFC2812 registration - // Stash the password for later + // RFC2812 registration. Stash the password for later if len(args) < 1 { s.reply(errMoreArgs) return @@ -188,16 +191,25 @@ func (s *Server) handleCommand(command string, args []string) { s.clientRegistered = true // Spawn upstream connection - go s.upstreamWorker() // FIXME need shutdown synchronisation + go s.upstreamWorker() // Tell the user that they themselves joined the chat channel s.reply(rplJoin, s.clientNick, BLESSED_CHANNEL) + default: + s.handleRegisteredCommand(command, args) + } +} + +func (s *Server) handleRegisteredCommand(command string, args []string) { + + if s.clientRegistered == false { + s.reply(errNotReg) + return + } + + switch command { case "JOIN": - if s.clientRegistered == false { - s.reply(errNotReg) - return - } if len(args) < 1 { s.reply(errMoreArgs) @@ -215,19 +227,11 @@ func (s *Server) handleCommand(command string, args []string) { } case "PART": - if s.clientRegistered == false { - s.reply(errNotReg) - return - } // you can check out any time you like, but you can never leave // we'll need to transmit s.reply(rplPart, s.clientNick, channel.name, reason) messages on behalf of other nmdc users, though case "PRIVMSG": - if s.clientRegistered == false { - s.reply(errNotReg) - return - } if len(args) < 2 { s.reply(errMoreArgs) @@ -251,18 +255,10 @@ func (s *Server) handleCommand(command string, args []string) { } case "QUIT": - if s.clientRegistered == false { - s.reply(errNotReg) - return - } s.DisconnectClient() case "TOPIC": - if s.clientRegistered == false { - s.reply(errNotReg) - return - } if len(args) < 1 { s.reply(errMoreArgs) @@ -286,20 +282,12 @@ func (s *Server) handleCommand(command string, args []string) { return case "LIST": - if s.clientRegistered == false { - s.reply(errNotReg) - return - } listItem := fmt.Sprintf("%s %d :%s", BLESSED_CHANNEL, len(s.upstream.Users), s.upstream.HubName) s.reply(rplList, listItem) s.reply(rplListEnd) case "OPER": - if s.clientRegistered == false { - s.reply(errNotReg) - return - } if len(args) < 2 { s.reply(errMoreArgs) @@ -310,10 +298,6 @@ func (s *Server) handleCommand(command string, args []string) { s.reply(errPassword) case "KILL": - if s.clientRegistered == false { - s.reply(errNotReg) - return - } s.reply(errNoPriv) return @@ -328,10 +312,6 @@ func (s *Server) handleCommand(command string, args []string) { return case "MODE": - if s.clientRegistered == false { - s.reply(errNotReg) - return - } if len(args) < 1 { s.reply(errMoreArgs) @@ -361,6 +341,8 @@ func (s *Server) handleCommand(command string, args []string) { func (s *Server) DisconnectClient() { s.clientConn.Close() s.clientConn = nil + + // Readloop will stop, which kills the upstream connection too } func (s *Server) sendClientGlobalMessage(motd string) {