remove extra nick var

--HG--
branch : nmdc-ircfrontend
This commit is contained in:
. 2016-05-05 19:45:59 +12:00
parent 26ede341ef
commit 8c5b148bc1
2 changed files with 51 additions and 51 deletions

View File

@ -50,10 +50,7 @@ func main() {
continue continue
} }
// Spin up an IRC server for the new user. // Spin up a worker for the new user.
// The upstream connection doesn't get launched until we hear a nick
// from the irc client
server := NewServer(*serverName, libnmdc.HubAddress(*dcAddress), conn) server := NewServer(*serverName, libnmdc.HubAddress(*dcAddress), conn)
go server.RunWorker() go server.RunWorker()
} }

View File

@ -33,7 +33,6 @@ type Server struct {
motd string motd string
clientConn net.Conn clientConn net.Conn
clientNick string
clientRegistered bool clientRegistered bool
upstreamLauncher libnmdc.HubConnectionOptions upstreamLauncher libnmdc.HubConnectionOptions
@ -59,29 +58,20 @@ func NewServer(name string, upstream libnmdc.HubAddress, conn net.Conn) *Server
func (s *Server) RunWorker() { func (s *Server) RunWorker() {
// Send the connection handshake // Send the connection handshake.
// Can't connect to the upstream server yet, until we've recieved a nick.
s.sendClientGlobalMessage(s.motd) s.sendClientGlobalMessage(s.motd)
// Can't connect to the upstream server yet, until we've recieved a nick.
for { for {
if s.clientConn == nil {
break // abandon thread
}
buf := make([]byte, CLIENT_READ_BUFFSIZE) buf := make([]byte, CLIENT_READ_BUFFSIZE)
s.clientConn.SetReadDeadline(time.Now().Add(time.Second * CLIENT_READ_TIMEOUT_SEC))
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() // if not already done break // abandon thread
// Cleanup upstream
if s.clientConn != nil && s.clientRegistered {
s.upstreamCloser <- struct{}{}
}
// Clean up ourselves
s.clientRegistered = false
// Abandon thread
return
} }
continue continue
} }
@ -108,6 +98,17 @@ func (s *Server) RunWorker() {
} }
} }
// Cleanup
s.DisconnectClient() // if not already done
// Cleanup upstream
if s.clientConn != nil && s.clientRegistered {
s.upstreamCloser <- struct{}{}
}
// Clean up ourselves
s.clientRegistered = false
} }
func (s *Server) upstreamWorker() { func (s *Server) upstreamWorker() {
@ -138,10 +139,10 @@ func (s *Server) upstreamWorker() {
s.sendChannelTopic(hubEvent.Nick) s.sendChannelTopic(hubEvent.Nick)
case libnmdc.EVENT_PRIVATE: case libnmdc.EVENT_PRIVATE:
s.reply(rplMsg, s.clientNick, hubEvent.Nick, hubEvent.Message) s.reply(rplMsg, s.upstreamLauncher.Self.Nick, hubEvent.Nick, hubEvent.Message)
case libnmdc.EVENT_PUBLIC: case libnmdc.EVENT_PUBLIC:
s.reply(rplMsg, s.clientNick, BLESSED_CHANNEL, hubEvent.Message) s.reply(rplMsg, s.upstreamLauncher.Self.Nick, BLESSED_CHANNEL, hubEvent.Message)
case libnmdc.EVENT_SYSTEM_MESSAGE_FROM_CONN, libnmdc.EVENT_SYSTEM_MESSAGE_FROM_HUB: case libnmdc.EVENT_SYSTEM_MESSAGE_FROM_CONN, libnmdc.EVENT_SYSTEM_MESSAGE_FROM_HUB:
s.sendClientGlobalMessage(hubEvent.Message) s.sendClientGlobalMessage(hubEvent.Message)
@ -195,7 +196,7 @@ func (s *Server) handleCommand(command string, args []string) {
return return
} }
if s.clientNick == "" { if s.upstreamLauncher.Self.Nick == "" {
s.reply(rplKill, "Your nickname is already being used", "") s.reply(rplKill, "Your nickname is already being used", "")
s.DisconnectClient() s.DisconnectClient()
return return
@ -209,7 +210,7 @@ func (s *Server) handleCommand(command string, args []string) {
go s.upstreamWorker() 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.upstreamLauncher.Self.Nick, BLESSED_CHANNEL)
default: default:
s.handleRegisteredCommand(command, args) s.handleRegisteredCommand(command, args)
@ -339,7 +340,9 @@ func (s *Server) handleRegisteredCommand(command string, args []string) {
} }
func (s *Server) DisconnectClient() { func (s *Server) DisconnectClient() {
if s.clientConn != nil {
s.clientConn.Close() s.clientConn.Close()
}
s.clientConn = nil s.clientConn = nil
// Readloop will stop, which kills the upstream connection too // Readloop will stop, which kills the upstream connection too
@ -372,71 +375,71 @@ func (s *Server) reply(code replyCode, args ...string) {
switch code { switch code {
case rplWelcome: case rplWelcome:
s.writeClient(fmt.Sprintf(":%s 001 %s :Welcome to %s", s.name, s.clientNick, s.name)) s.writeClient(fmt.Sprintf(":%s 001 %s :Welcome to %s", s.name, s.upstreamLauncher.Self.Nick, s.name))
case rplJoin: case rplJoin:
s.writeClient(fmt.Sprintf(":%s JOIN %s", args[0], args[1])) s.writeClient(fmt.Sprintf(":%s JOIN %s", args[0], args[1]))
case rplPart: case rplPart:
s.writeClient(fmt.Sprintf(":%s PART %s %s", args[0], args[1], args[2])) s.writeClient(fmt.Sprintf(":%s PART %s %s", args[0], args[1], args[2]))
case rplTopic: case rplTopic:
s.writeClient(fmt.Sprintf(":%s 332 %s %s :%s", s.name, s.clientNick, args[0], args[1])) s.writeClient(fmt.Sprintf(":%s 332 %s %s :%s", s.name, s.upstreamLauncher.Self.Nick, args[0], args[1]))
case rplNoTopic: case rplNoTopic:
s.writeClient(fmt.Sprintf(":%s 331 %s %s :No topic is set", s.name, s.clientNick, args[0])) s.writeClient(fmt.Sprintf(":%s 331 %s %s :No topic is set", s.name, s.upstreamLauncher.Self.Nick, args[0]))
case rplNames: case rplNames:
s.writeClient(fmt.Sprintf(":%s 353 %s = %s :%s", s.name, s.clientNick, args[0], args[1])) s.writeClient(fmt.Sprintf(":%s 353 %s = %s :%s", s.name, s.upstreamLauncher.Self.Nick, args[0], args[1]))
case rplEndOfNames: case rplEndOfNames:
s.writeClient(fmt.Sprintf(":%s 366 %s %s :End of NAMES list", s.name, s.clientNick, args[0])) s.writeClient(fmt.Sprintf(":%s 366 %s %s :End of NAMES list", s.name, s.upstreamLauncher.Self.Nick, args[0]))
case rplNickChange: case rplNickChange:
s.writeClient(fmt.Sprintf(":%s NICK %s", args[0], args[1])) s.writeClient(fmt.Sprintf(":%s NICK %s", args[0], args[1]))
case rplKill: case rplKill:
s.writeClient(fmt.Sprintf(":%s KILL %s A %s", args[0], s.clientNick, args[1])) s.writeClient(fmt.Sprintf(":%s KILL %s A %s", args[0], s.upstreamLauncher.Self.Nick, args[1]))
case rplMsg: case rplMsg:
for _, itm := range strings.Split(args[2], "\n") { for _, itm := range strings.Split(args[2], "\n") {
s.writeClient(fmt.Sprintf(":%s PRIVMSG %s %s", args[0], args[1], itm)) s.writeClient(fmt.Sprintf(":%s PRIVMSG %s %s", args[0], args[1], itm))
} }
case rplList: case rplList:
s.writeClient(fmt.Sprintf(":%s 322 %s %s", s.name, s.clientNick, args[0])) s.writeClient(fmt.Sprintf(":%s 322 %s %s", s.name, s.upstreamLauncher.Self.Nick, args[0]))
case rplListEnd: case rplListEnd:
s.writeClient(fmt.Sprintf(":%s 323 %s", s.name, s.clientNick)) s.writeClient(fmt.Sprintf(":%s 323 %s", s.name, s.upstreamLauncher.Self.Nick))
case rplOper: case rplOper:
s.writeClient(fmt.Sprintf(":%s 381 %s :You are now an operator", s.name, s.clientNick)) s.writeClient(fmt.Sprintf(":%s 381 %s :You are now an operator", s.name, s.upstreamLauncher.Self.Nick))
case rplChannelModeIs: case rplChannelModeIs:
s.writeClient(fmt.Sprintf(":%s 324 %s %s %s %s", s.name, s.clientNick, args[0], args[1], args[2])) s.writeClient(fmt.Sprintf(":%s 324 %s %s %s %s", s.name, s.upstreamLauncher.Self.Nick, args[0], args[1], args[2]))
case rplKick: case rplKick:
s.writeClient(fmt.Sprintf(":%s KICK %s %s %s", args[0], args[1], args[2], args[3])) s.writeClient(fmt.Sprintf(":%s KICK %s %s %s", args[0], args[1], args[2], args[3]))
case rplInfo: case rplInfo:
s.writeClient(fmt.Sprintf(":%s 371 %s :%s", s.name, s.clientNick, args[0])) s.writeClient(fmt.Sprintf(":%s 371 %s :%s", s.name, s.upstreamLauncher.Self.Nick, args[0]))
case rplVersion: case rplVersion:
s.writeClient(fmt.Sprintf(":%s 351 %s %s", s.name, s.clientNick, args[0])) s.writeClient(fmt.Sprintf(":%s 351 %s %s", s.name, s.upstreamLauncher.Self.Nick, args[0]))
case rplMOTDStart: case rplMOTDStart:
s.writeClient(fmt.Sprintf(":%s 375 %s :- Message of the day - ", s.name, s.clientNick)) s.writeClient(fmt.Sprintf(":%s 375 %s :- Message of the day - ", s.name, s.upstreamLauncher.Self.Nick))
case rplMOTD: case rplMOTD:
s.writeClient(fmt.Sprintf(":%s 372 %s :- %s", s.name, s.clientNick, args[0])) s.writeClient(fmt.Sprintf(":%s 372 %s :- %s", s.name, s.upstreamLauncher.Self.Nick, args[0]))
case rplEndOfMOTD: case rplEndOfMOTD:
s.writeClient(fmt.Sprintf(":%s 376 %s :End of MOTD Command", s.name, s.clientNick)) s.writeClient(fmt.Sprintf(":%s 376 %s :End of MOTD Command", s.name, s.upstreamLauncher.Self.Nick))
case rplPong: case rplPong:
s.writeClient(fmt.Sprintf(":%s PONG %s %s", s.name, s.clientNick, s.name)) s.writeClient(fmt.Sprintf(":%s PONG %s %s", s.name, s.upstreamLauncher.Self.Nick, s.name))
case errMoreArgs: case errMoreArgs:
s.writeClient(fmt.Sprintf(":%s 461 %s :Not enough params", s.name, s.clientNick)) s.writeClient(fmt.Sprintf(":%s 461 %s :Not enough params", s.name, s.upstreamLauncher.Self.Nick))
case errNoNick: case errNoNick:
s.writeClient(fmt.Sprintf(":%s 431 %s :No nickname given", s.name, s.clientNick)) s.writeClient(fmt.Sprintf(":%s 431 %s :No nickname given", s.name, s.upstreamLauncher.Self.Nick))
case errInvalidNick: case errInvalidNick:
s.writeClient(fmt.Sprintf(":%s 432 %s %s :Erronenous nickname", s.name, s.clientNick, args[0])) s.writeClient(fmt.Sprintf(":%s 432 %s %s :Erronenous nickname", s.name, s.upstreamLauncher.Self.Nick, args[0]))
case errNickInUse: case errNickInUse:
s.writeClient(fmt.Sprintf(":%s 433 %s %s :Nick already in use", s.name, s.clientNick, args[0])) s.writeClient(fmt.Sprintf(":%s 433 %s %s :Nick already in use", s.name, s.upstreamLauncher.Self.Nick, args[0]))
case errAlreadyReg: case errAlreadyReg:
s.writeClient(fmt.Sprintf(":%s 462 :You need a valid nick first", s.name)) s.writeClient(fmt.Sprintf(":%s 462 :You need a valid nick first", s.name))
case errNoSuchNick: case errNoSuchNick:
s.writeClient(fmt.Sprintf(":%s 401 %s %s :No such nick/channel", s.name, s.clientNick, args[0])) s.writeClient(fmt.Sprintf(":%s 401 %s %s :No such nick/channel", s.name, s.upstreamLauncher.Self.Nick, args[0]))
case errUnknownCommand: case errUnknownCommand:
s.writeClient(fmt.Sprintf(":%s 421 %s %s :Unknown command", s.name, s.clientNick, args[0])) s.writeClient(fmt.Sprintf(":%s 421 %s %s :Unknown command", s.name, s.upstreamLauncher.Self.Nick, args[0]))
case errNotReg: case errNotReg:
s.writeClient(fmt.Sprintf(":%s 451 :You have not registered", s.name)) s.writeClient(fmt.Sprintf(":%s 451 :You have not registered", s.name))
case errPassword: case errPassword:
s.writeClient(fmt.Sprintf(":%s 464 %s :Error, password incorrect", s.name, s.clientNick)) s.writeClient(fmt.Sprintf(":%s 464 %s :Error, password incorrect", s.name, s.upstreamLauncher.Self.Nick))
case errNoPriv: case errNoPriv:
s.writeClient(fmt.Sprintf(":%s 481 %s :Permission denied", s.name, s.clientNick)) s.writeClient(fmt.Sprintf(":%s 481 %s :Permission denied", s.name, s.upstreamLauncher.Self.Nick))
case errCannotSend: case errCannotSend:
s.writeClient(fmt.Sprintf(":%s 404 %s %s :Cannot send to channel", s.name, s.clientNick, args[0])) s.writeClient(fmt.Sprintf(":%s 404 %s %s :Cannot send to channel", s.name, s.upstreamLauncher.Self.Nick, args[0]))
} }
} }