--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) {

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,32 +52,11 @@ 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 {
case <-closeChan:
return
case ln, err := s.client.connection.Read(buf):
if err != nil { if err != nil {
if err == io.EOF { if err == io.EOF {
s.client.disconnect() s.client.disconnect()
@ -103,19 +86,19 @@ 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,21 +169,29 @@ 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.reply(rplWelcome)
s.client.registered = true s.client.registered = true
// Spawn upstream connection // 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 {
@ -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,18 +306,9 @@ func (s *Server) handleCommand(command string, args []string) {
return return
} }
//username := args[0] // Can't use this command.
//password := args[1]
if false { // op the user
s.client.operator = true
s.client.reply(rplOper)
return
} else {
s.client.reply(errPassword) s.client.reply(errPassword)
}
case "KILL": case "KILL":
if s.client.registered == false { if s.client.registered == false {
s.client.reply(errNotReg) s.client.reply(errNotReg)
@ -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