--HG--
branch : nmdc-ircfrontend
This commit is contained in:
. 2016-05-03 19:57:02 +12:00
parent 15b08e0f27
commit e97f344f00
2 changed files with 55 additions and 82 deletions

View File

@ -58,6 +58,7 @@ func (c *Client) joinChannel(channelName string) {
func (c *Client) disconnect() {
c.connected = false
c.connection.Close()
}
func (c *Client) sendGlobalMessage(motd string) {

View File

@ -7,6 +7,7 @@ import (
"libnmdc"
"net"
"strings"
"time"
)
type Server struct {
@ -16,18 +17,21 @@ type Server struct {
client *Client
channel Channel // Single blessed channel
upstreamLauncher libnmdc.HubConnectionOptions
upstream libnmdc.HubConnection
upstream *libnmdc.HubConnection
motd string
}
func NewServer(name string, upstream libnmdc.HubAddress) *Server {
self := libnmdc.NewUserInfo("")
self.ClientTag = APP_DESCRIPTION
return &Server{eventChan: make(chan Event),
name: name,
client: nil,
motd: "Connected to " + name,
upstreamLauncher: libnmdc.HubConnectionOptions{
Address: upstream,
Self: *libnmdc.NewUserInfo(""),
Self: *self,
},
channel: Channel{
clientMap: make(map[string]*Client),
@ -48,32 +52,11 @@ func (s *Server) RunClient(conn net.Conn) {
s.client.sendGlobalMessage(s.motd)
// Can't connect to the upstream server yet, until we've recieved a nick.
// So we can't have a conjoined select statement
// Need a separate goroutine for both IRC and NMDC protocols, and
// synchronisation to ensure simultaneous cleanup
closeChan := make(chan struct{}, 2)
go s.ProtocolReadLoop_IRC(closeChan)
// FIXME
}
func (s *Server) ProtocolReadLoop_IRC(closeChan chan struct{}) {
defer func() {
closeChan <- struct{}{}
}()
// Read loop
for {
buf := make([]byte, CLIENT_READ_BUFFSIZE)
// s.client.connection.SetReadDeadline(time.Now().Add(time.Second * CLIENT_READ_TIMEOUT_SEC))
select {
case <-closeChan:
return
case ln, err := s.client.connection.Read(buf):
s.client.connection.SetReadDeadline(time.Now().Add(time.Second * CLIENT_READ_TIMEOUT_SEC))
ln, err := s.client.connection.Read(buf)
if err != nil {
if err == io.EOF {
s.client.disconnect()
@ -103,19 +86,19 @@ func (s *Server) ProtocolReadLoop_IRC(closeChan chan struct{}) {
}
}
}
}
}
func (s *Server) ProtocolReadLoop_NMDC(closeChan chan struct{}) {
defer func() {
closeChan <- struct{}{}
}()
// Initiate connection
s.upstream = s.upstreamLauncher.Connect()
// Read loop
for {
select {
case <-closeChan:
// Need some way of deliberately shutting down a libnmdc connection...
return
case hubEvent := <-s.upstream.OnEvent:
@ -186,21 +169,29 @@ func (s *Server) handleCommand(command string, args []string) {
}
case "USER":
// This command sets altname, realname, ... none of which we use
// It's the final step in a PASS/NICK/USER login handshake.
if s.client.registered == true {
s.client.reply(rplKill, "You're already registered.", "")
s.client.disconnect()
return
}
if s.client.nick == "" {
s.client.reply(rplKill, "Your nickname is already being used", "")
s.client.disconnect()
return
}
} else {
s.client.reply(rplWelcome)
s.client.registered = true
// Spawn upstream connection
}
go s.ProtocolReadLoop_NMDC(nil) // FIXME need shutdown synchronisation
// Tell the user that they themselves joined the chat channel
s.client.reply(rplJoin, s.client.nick, BLESSED_CHANNEL)
case "JOIN":
if s.client.registered == false {
@ -247,22 +238,12 @@ func (s *Server) handleCommand(command string, args []string) {
// IRC is case-insensitive case-preserving. We can respect that for the
// channel name, but not really for user nicks
recipient := strings.ToLower(args[0])
if recipient == BLESSED_CHANNEL {
if strings.ToLower(args[0]) == BLESSED_CHANNEL {
s.upstream.SayPublic(message)
/*
for _, c := range s.channel.clientMap {
if c != client {
c.reply(rplMsg, s.client.nick, args[0], message)
}
}
*/
} else if nmdcUser, clientExists := s.upstream.Users[args[0]]; clientExists {
s.upstream.SayPrivate(recipient, message)
// client2.reply(rplMsg, s.client.nick, client2.nick, message)
} else if _, clientExists := s.upstream.Users[args[0]]; clientExists {
s.upstream.SayPrivate(args[0], message)
} else {
s.client.reply(errNoSuchNick, args[0])
@ -325,18 +306,9 @@ func (s *Server) handleCommand(command string, args []string) {
return
}
//username := args[0]
//password := args[1]
if false { // op the user
s.client.operator = true
s.client.reply(rplOper)
return
} else {
// Can't use this command.
s.client.reply(errPassword)
}
case "KILL":
if s.client.registered == false {
s.client.reply(errNotReg)