--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() { func (c *Client) disconnect() {
c.connected = false c.connected = false
c.connection.Close()
} }
func (c *Client) sendGlobalMessage(motd string) { func (c *Client) sendGlobalMessage(motd string) {

136
server.go
View File

@ -7,6 +7,7 @@ import (
"libnmdc" "libnmdc"
"net" "net"
"strings" "strings"
"time"
) )
type Server struct { type Server struct {
@ -16,18 +17,21 @@ type Server struct {
client *Client client *Client
channel Channel // Single blessed channel channel Channel // Single blessed channel
upstreamLauncher libnmdc.HubConnectionOptions upstreamLauncher libnmdc.HubConnectionOptions
upstream libnmdc.HubConnection upstream *libnmdc.HubConnection
motd string motd string
} }
func NewServer(name string, upstream libnmdc.HubAddress) *Server { func NewServer(name string, upstream libnmdc.HubAddress) *Server {
self := libnmdc.NewUserInfo("")
self.ClientTag = APP_DESCRIPTION
return &Server{eventChan: make(chan Event), return &Server{eventChan: make(chan Event),
name: name, name: name,
client: nil, client: nil,
motd: "Connected to " + name, motd: "Connected to " + name,
upstreamLauncher: libnmdc.HubConnectionOptions{ upstreamLauncher: libnmdc.HubConnectionOptions{
Address: upstream, Address: upstream,
Self: *libnmdc.NewUserInfo(""), Self: *self,
}, },
channel: Channel{ channel: Channel{
clientMap: make(map[string]*Client), clientMap: make(map[string]*Client),
@ -48,59 +52,37 @@ func (s *Server) RunClient(conn net.Conn) {
s.client.sendGlobalMessage(s.motd) s.client.sendGlobalMessage(s.motd)
// Can't connect to the upstream server yet, until we've recieved a nick. // 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 { for {
buf := make([]byte, CLIENT_READ_BUFFSIZE) buf := make([]byte, CLIENT_READ_BUFFSIZE)
// s.client.connection.SetReadDeadline(time.Now().Add(time.Second * CLIENT_READ_TIMEOUT_SEC)) s.client.connection.SetReadDeadline(time.Now().Add(time.Second * CLIENT_READ_TIMEOUT_SEC))
ln, err := s.client.connection.Read(buf)
select { if err != nil {
case <-closeChan: if err == io.EOF {
return s.client.disconnect()
return // FIXME cleanup
case ln, err := s.client.connection.Read(buf):
if err != nil {
if err == io.EOF {
s.client.disconnect()
return // FIXME cleanup
}
continue
} }
continue
}
rawLines := buf[:ln] rawLines := buf[:ln]
rawLines = bytes.Replace(rawLines, []byte("\r\n"), []byte("\n"), -1) rawLines = bytes.Replace(rawLines, []byte("\r\n"), []byte("\n"), -1)
rawLines = bytes.Replace(rawLines, []byte("\r"), []byte("\n"), -1) rawLines = bytes.Replace(rawLines, []byte("\r"), []byte("\n"), -1)
lines := bytes.Split(rawLines, []byte("\n")) lines := bytes.Split(rawLines, []byte("\n"))
for _, line := range lines { for _, line := range lines {
if len(line) > 0 { if len(line) > 0 {
// Client sent a command // Client sent a command
fields := strings.Fields(string(line)) fields := strings.Fields(string(line))
if len(fields) < 1 { if len(fields) < 1 {
return return
}
if strings.HasPrefix(fields[0], ":") {
fields = fields[1:]
}
s.handleCommand(strings.ToUpper(fields[0]), fields[1:])
} }
if strings.HasPrefix(fields[0], ":") {
fields = fields[1:]
}
s.handleCommand(strings.ToUpper(fields[0]), fields[1:])
} }
} }
} }
@ -108,14 +90,15 @@ func (s *Server) ProtocolReadLoop_IRC(closeChan chan struct{}) {
} }
func (s *Server) ProtocolReadLoop_NMDC(closeChan chan struct{}) { func (s *Server) ProtocolReadLoop_NMDC(closeChan chan struct{}) {
defer func() {
closeChan <- struct{}{} // Initiate connection
}() s.upstream = s.upstreamLauncher.Connect()
// Read loop // Read loop
for { for {
select { select {
case <-closeChan: case <-closeChan:
// Need some way of deliberately shutting down a libnmdc connection...
return return
case hubEvent := <-s.upstream.OnEvent: case hubEvent := <-s.upstream.OnEvent:
@ -186,22 +169,30 @@ func (s *Server) handleCommand(command string, args []string) {
} }
case "USER": 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 { if s.client.registered == true {
s.client.reply(rplKill, "You're already registered.", "") s.client.reply(rplKill, "You're already registered.", "")
s.client.disconnect() s.client.disconnect()
return
} }
if s.client.nick == "" { if s.client.nick == "" {
s.client.reply(rplKill, "Your nickname is already being used", "") s.client.reply(rplKill, "Your nickname is already being used", "")
s.client.disconnect() s.client.disconnect()
return
} else {
s.client.reply(rplWelcome)
s.client.registered = true
// Spawn upstream connection
} }
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": case "JOIN":
if s.client.registered == false { if s.client.registered == false {
s.client.reply(errNotReg) s.client.reply(errNotReg)
@ -247,22 +238,12 @@ func (s *Server) handleCommand(command string, args []string) {
// IRC is case-insensitive case-preserving. We can respect that for the // IRC is case-insensitive case-preserving. We can respect that for the
// channel name, but not really for user nicks // 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) 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 { } else if _, clientExists := s.upstream.Users[args[0]]; clientExists {
s.upstream.SayPrivate(args[0], message)
s.upstream.SayPrivate(recipient, message)
// client2.reply(rplMsg, s.client.nick, client2.nick, message)
} else { } else {
s.client.reply(errNoSuchNick, args[0]) s.client.reply(errNoSuchNick, args[0])
@ -325,17 +306,8 @@ func (s *Server) handleCommand(command string, args []string) {
return return
} }
//username := args[0] // Can't use this command.
//password := args[1] s.client.reply(errPassword)
if false { // op the user
s.client.operator = true
s.client.reply(rplOper)
return
} else {
s.client.reply(errPassword)
}
case "KILL": case "KILL":
if s.client.registered == false { if s.client.registered == false {
@ -372,7 +344,7 @@ func (s *Server) handleCommand(command string, args []string) {
} }
if len(args) == 1 { if len(args) == 1 {
//No more args, they just want the mode // No more args, they just want the mode
s.client.reply(rplChannelModeIs, args[0], BLESSED_CHANNEL_MODE, "") s.client.reply(rplChannelModeIs, args[0], BLESSED_CHANNEL_MODE, "")
} else { } else {
// Setting modes is disallowed // Setting modes is disallowed