simplifications

--HG--
branch : nmdc-ircfrontend
This commit is contained in:
. 2016-05-05 19:16:45 +12:00
parent d56203cd39
commit 3bca2640a0

View File

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