remove all multiuser support, all goroutines and all channels

--HG--
branch : nmdc-ircfrontend
This commit is contained in:
. 2016-05-03 18:47:24 +12:00
parent be5715a562
commit 46114809bd
4 changed files with 99 additions and 174 deletions

154
client.go
View File

@ -1,21 +1,15 @@
package main package main
import ( import (
"bytes"
"fmt" "fmt"
"io"
"net" "net"
"strings" "strings"
"time" "time"
) )
type Client struct { type Client struct {
server *Server
connection net.Conn connection net.Conn
signalChan chan signalCode
outputChan chan string
nick string nick string
key string
registered bool registered bool
connected bool connected bool
operator bool operator bool
@ -30,7 +24,7 @@ func (c *Client) joinChannel(channelName string) {
} }
channel := c.server.channel channel := c.server.channel
if _, inChannel := channel.clientMap[c.key]; inChannel { if _, inChannel := channel.clientMap[c.nick]; inChannel {
//Client is already in the channel, do nothing //Client is already in the channel, do nothing
return return
} }
@ -53,7 +47,7 @@ func (c *Client) joinChannel(channelName string) {
for _, client := range channel.clientMap { for _, client := range channel.clientMap {
prefix := "" prefix := ""
if mode, exists := channel.modeMap[client.key]; exists { if mode, exists := channel.modeMap[client.nick]; exists {
prefix = mode.Prefix() prefix = mode.Prefix()
} }
@ -74,7 +68,6 @@ func (c *Client) joinChannel(channelName string) {
func (c *Client) disconnect() { func (c *Client) disconnect() {
c.connected = false c.connected = false
c.signalChan <- signalStop
} }
//Send a reply to a user with the code specified //Send a reply to a user with the code specified
@ -85,153 +78,76 @@ func (c *Client) reply(code replyCode, args ...string) {
switch code { switch code {
case rplWelcome: case rplWelcome:
c.outputChan <- fmt.Sprintf(":%s 001 %s :Welcome to %s", c.server.name, c.nick, c.server.name) c.write(fmt.Sprintf(":%s 001 %s :Welcome to %s", c.server.name, c.nick, c.server.name))
case rplJoin: case rplJoin:
c.outputChan <- fmt.Sprintf(":%s JOIN %s", args[0], args[1]) c.write(fmt.Sprintf(":%s JOIN %s", args[0], args[1]))
case rplPart: case rplPart:
c.outputChan <- fmt.Sprintf(":%s PART %s %s", args[0], args[1], args[2]) c.write(fmt.Sprintf(":%s PART %s %s", args[0], args[1], args[2]))
case rplTopic: case rplTopic:
c.outputChan <- fmt.Sprintf(":%s 332 %s %s :%s", c.server.name, c.nick, args[0], args[1]) c.write(fmt.Sprintf(":%s 332 %s %s :%s", c.server.name, c.nick, args[0], args[1]))
case rplNoTopic: case rplNoTopic:
c.outputChan <- fmt.Sprintf(":%s 331 %s %s :No topic is set", c.server.name, c.nick, args[0]) c.write(fmt.Sprintf(":%s 331 %s %s :No topic is set", c.server.name, c.nick, args[0]))
case rplNames: case rplNames:
c.outputChan <- fmt.Sprintf(":%s 353 %s = %s :%s", c.server.name, c.nick, args[0], args[1]) c.write(fmt.Sprintf(":%s 353 %s = %s :%s", c.server.name, c.nick, args[0], args[1]))
case rplEndOfNames: case rplEndOfNames:
c.outputChan <- fmt.Sprintf(":%s 366 %s %s :End of NAMES list", c.server.name, c.nick, args[0]) c.write(fmt.Sprintf(":%s 366 %s %s :End of NAMES list", c.server.name, c.nick, args[0]))
case rplNickChange: case rplNickChange:
c.outputChan <- fmt.Sprintf(":%s NICK %s", args[0], args[1]) c.write(fmt.Sprintf(":%s NICK %s", args[0], args[1]))
case rplKill: case rplKill:
c.outputChan <- fmt.Sprintf(":%s KILL %s A %s", args[0], c.nick, args[1]) c.write(fmt.Sprintf(":%s KILL %s A %s", args[0], c.nick, args[1]))
case rplMsg: case rplMsg:
c.outputChan <- fmt.Sprintf(":%s PRIVMSG %s %s", args[0], args[1], args[2]) c.write(fmt.Sprintf(":%s PRIVMSG %s %s", args[0], args[1], args[2]))
case rplList: case rplList:
c.outputChan <- fmt.Sprintf(":%s 322 %s %s", c.server.name, c.nick, args[0]) c.write(fmt.Sprintf(":%s 322 %s %s", c.server.name, c.nick, args[0]))
case rplListEnd: case rplListEnd:
c.outputChan <- fmt.Sprintf(":%s 323 %s", c.server.name, c.nick) c.write(fmt.Sprintf(":%s 323 %s", c.server.name, c.nick))
case rplOper: case rplOper:
c.outputChan <- fmt.Sprintf(":%s 381 %s :You are now an operator", c.server.name, c.nick) c.write(fmt.Sprintf(":%s 381 %s :You are now an operator", c.server.name, c.nick))
case rplChannelModeIs: case rplChannelModeIs:
c.outputChan <- fmt.Sprintf(":%s 324 %s %s %s %s", c.server.name, c.nick, args[0], args[1], args[2]) c.write(fmt.Sprintf(":%s 324 %s %s %s %s", c.server.name, c.nick, args[0], args[1], args[2]))
case rplKick: case rplKick:
c.outputChan <- fmt.Sprintf(":%s KICK %s %s %s", args[0], args[1], args[2], args[3]) c.write(fmt.Sprintf(":%s KICK %s %s %s", args[0], args[1], args[2], args[3]))
case rplInfo: case rplInfo:
c.outputChan <- fmt.Sprintf(":%s 371 %s :%s", c.server.name, c.nick, args[0]) c.write(fmt.Sprintf(":%s 371 %s :%s", c.server.name, c.nick, args[0]))
case rplVersion: case rplVersion:
c.outputChan <- fmt.Sprintf(":%s 351 %s %s", c.server.name, c.nick, args[0]) c.write(fmt.Sprintf(":%s 351 %s %s", c.server.name, c.nick, args[0]))
case rplMOTDStart: case rplMOTDStart:
c.outputChan <- fmt.Sprintf(":%s 375 %s :- Message of the day - ", c.server.name, c.nick) c.write(fmt.Sprintf(":%s 375 %s :- Message of the day - ", c.server.name, c.nick))
case rplMOTD: case rplMOTD:
c.outputChan <- fmt.Sprintf(":%s 372 %s :- %s", c.server.name, c.nick, args[0]) c.write(fmt.Sprintf(":%s 372 %s :- %s", c.server.name, c.nick, args[0]))
case rplEndOfMOTD: case rplEndOfMOTD:
c.outputChan <- fmt.Sprintf(":%s 376 %s :End of MOTD Command", c.server.name, c.nick) c.write(fmt.Sprintf(":%s 376 %s :End of MOTD Command", c.server.name, c.nick))
case rplPong: case rplPong:
c.outputChan <- fmt.Sprintf(":%s PONG %s %s", c.server.name, c.nick, c.server.name) c.write(fmt.Sprintf(":%s PONG %s %s", c.server.name, c.nick, c.server.name))
case errMoreArgs: case errMoreArgs:
c.outputChan <- fmt.Sprintf(":%s 461 %s :Not enough params", c.server.name, c.nick) c.write(fmt.Sprintf(":%s 461 %s :Not enough params", c.server.name, c.nick))
case errNoNick: case errNoNick:
c.outputChan <- fmt.Sprintf(":%s 431 %s :No nickname given", c.server.name, c.nick) c.write(fmt.Sprintf(":%s 431 %s :No nickname given", c.server.name, c.nick))
case errInvalidNick: case errInvalidNick:
c.outputChan <- fmt.Sprintf(":%s 432 %s %s :Erronenous nickname", c.server.name, c.nick, args[0]) c.write(fmt.Sprintf(":%s 432 %s %s :Erronenous nickname", c.server.name, c.nick, args[0]))
case errNickInUse: case errNickInUse:
c.outputChan <- fmt.Sprintf(":%s 433 %s %s :Nick already in use", c.server.name, c.nick, args[0]) c.write(fmt.Sprintf(":%s 433 %s %s :Nick already in use", c.server.name, c.nick, args[0]))
case errAlreadyReg: case errAlreadyReg:
c.outputChan <- fmt.Sprintf(":%s 462 :You need a valid nick first", c.server.name) c.write(fmt.Sprintf(":%s 462 :You need a valid nick first", c.server.name))
case errNoSuchNick: case errNoSuchNick:
c.outputChan <- fmt.Sprintf(":%s 401 %s %s :No such nick/channel", c.server.name, c.nick, args[0]) c.write(fmt.Sprintf(":%s 401 %s %s :No such nick/channel", c.server.name, c.nick, args[0]))
case errUnknownCommand: case errUnknownCommand:
c.outputChan <- fmt.Sprintf(":%s 421 %s %s :Unknown command", c.server.name, c.nick, args[0]) c.write(fmt.Sprintf(":%s 421 %s %s :Unknown command", c.server.name, c.nick, args[0]))
case errNotReg: case errNotReg:
c.outputChan <- fmt.Sprintf(":%s 451 :You have not registered", c.server.name) c.write(fmt.Sprintf(":%s 451 :You have not registered", c.server.name))
case errPassword: case errPassword:
c.outputChan <- fmt.Sprintf(":%s 464 %s :Error, password incorrect", c.server.name, c.nick) c.write(fmt.Sprintf(":%s 464 %s :Error, password incorrect", c.server.name, c.nick))
case errNoPriv: case errNoPriv:
c.outputChan <- fmt.Sprintf(":%s 481 %s :Permission denied", c.server.name, c.nick) c.write(fmt.Sprintf(":%s 481 %s :Permission denied", c.server.name, c.nick))
case errCannotSend: case errCannotSend:
c.outputChan <- fmt.Sprintf(":%s 404 %s %s :Cannot send to channel", c.server.name, c.nick, args[0]) c.write(fmt.Sprintf(":%s 404 %s %s :Cannot send to channel", c.server.name, c.nick, args[0]))
} }
} }
func (c *Client) clientThread() { func (c *Client) write(output string) {
readSignalChan := make(chan signalCode, 3)
writeSignalChan := make(chan signalCode, 3)
writeChan := make(chan string, 100)
c.server.eventChan <- Event{client: c, event: connected}
go c.readThread(readSignalChan)
go c.writeThread(writeSignalChan, writeChan)
defer func() {
// Implicit part from all channels
// FIXME also drop the upstream connection
c.connection.Close()
}()
for {
select {
case signal := <-c.signalChan:
if signal == signalStop {
readSignalChan <- signalStop
writeSignalChan <- signalStop
return
}
case line := <-c.outputChan:
select {
case writeChan <- line:
continue
default:
c.disconnect()
}
}
}
}
func (c *Client) readThread(signalChan chan signalCode) {
for {
select {
case signal := <-signalChan:
if signal == signalStop {
return
}
default:
c.connection.SetReadDeadline(time.Now().Add(time.Second * 3))
buf := make([]byte, 512)
ln, err := c.connection.Read(buf)
if err != nil {
if err == io.EOF {
c.disconnect()
return
}
continue
}
rawLines := buf[:ln]
rawLines = bytes.Replace(rawLines, []byte("\r\n"), []byte("\n"), -1)
rawLines = bytes.Replace(rawLines, []byte("\r"), []byte("\n"), -1)
lines := bytes.Split(rawLines, []byte("\n"))
for _, line := range lines {
if len(line) > 0 {
c.server.eventChan <- Event{client: c, event: command, input: string(line)}
}
}
}
}
}
func (c *Client) writeThread(signalChan chan signalCode, outputChan chan string) {
for {
select {
case signal := <-signalChan:
if signal == signalStop {
return
}
case output := <-outputChan:
c.connection.SetWriteDeadline(time.Now().Add(time.Second * 30)) c.connection.SetWriteDeadline(time.Now().Add(time.Second * 30))
if _, err := fmt.Fprintf(c.connection, "%s\r\n", output); err != nil { if _, err := fmt.Fprintf(c.connection, "%s\r\n", output); err != nil {
c.disconnect() c.disconnect()
return return
} }
} }
}
}

View File

@ -16,10 +16,6 @@ var (
func main() { func main() {
flag.Parse() flag.Parse()
if len(*serverName) == 0 {
log.Println("Please specify the -servername parameter.")
return
}
listener, err := net.Listen("tcp", *ircAddress) listener, err := net.Listen("tcp", *ircAddress)
if err != nil { if err != nil {
@ -41,7 +37,6 @@ func main() {
// from the irc client // from the irc client
server := NewServer(*serverName, libnmdc.HubAddress(*dcAddress)) server := NewServer(*serverName, libnmdc.HubAddress(*dcAddress))
go server.Run() go server.RunClient(conn)
server.HandleConnection(conn)
} }
} }

View File

@ -1,10 +1,13 @@
package main package main
import ( import (
"bytes"
"fmt" "fmt"
"io"
"libnmdc" "libnmdc"
"net" "net"
"strings" "strings"
"time"
) )
type Server struct { type Server struct {
@ -31,45 +34,55 @@ func NewServer(name string, upstream libnmdc.HubAddress) *Server {
} }
} }
func (s *Server) Run() { func (s *Server) RunClient(conn net.Conn) {
for event := range s.eventChan {
s.handleEvent(event)
}
}
func (s *Server) HandleConnection(conn net.Conn) { s.client = &Client{
client := &Client{server: s,
connection: conn, connection: conn,
outputChan: make(chan string),
signalChan: make(chan signalCode, 3),
connected: true, connected: true,
} }
go client.clientThread() // Send the connection handshake
} s.client.reply(rplMOTDStart)
func (s *Server) handleEvent(e Event) {
switch e.event {
case connected:
//Client connected
e.client.reply(rplMOTDStart)
motd := s.motd motd := s.motd
for len(motd) > 80 { for len(motd) > 80 {
e.client.reply(rplMOTD, motd[:80]) s.client.reply(rplMOTD, motd[:80])
motd = motd[80:] motd = motd[80:]
} }
if len(motd) > 0 { if len(motd) > 0 {
e.client.reply(rplMOTD, motd) s.client.reply(rplMOTD, motd)
} }
e.client.reply(rplEndOfMOTD) s.client.reply(rplEndOfMOTD)
case disconnected: // Read loop
//Client disconnected for {
s.client.connection.SetReadDeadline(time.Now().Add(time.Second * CLIENT_READ_TIMEOUT_SEC))
buf := make([]byte, CLIENT_READ_BUFFSIZE)
ln, err := s.client.connection.Read(buf)
if err != nil {
if err == io.EOF {
s.client.disconnect()
return // FIXME cleanup
}
continue
}
rawLines := buf[:ln]
rawLines = bytes.Replace(rawLines, []byte("\r\n"), []byte("\n"), -1)
rawLines = bytes.Replace(rawLines, []byte("\r"), []byte("\n"), -1)
lines := bytes.Split(rawLines, []byte("\n"))
for _, line := range lines {
if len(line) > 0 {
s.handleCommandEvent(string(line))
}
}
}
}
func (s *Server) handleCommandEvent(input string) {
case command:
//Client send a command //Client send a command
fields := strings.Fields(e.input) fields := strings.Fields(input)
if len(fields) < 1 { if len(fields) < 1 {
return return
} }
@ -80,8 +93,8 @@ func (s *Server) handleEvent(e Event) {
command := strings.ToUpper(fields[0]) command := strings.ToUpper(fields[0])
args := fields[1:] args := fields[1:]
s.handleCommand(e.client, command, args) s.handleCommand(s.client, command, args)
}
} }
func (s *Server) handleCommand(client *Client, command string, args []string) { func (s *Server) handleCommand(client *Client, command string, args []string) {

View File

@ -11,6 +11,8 @@ const (
BLESSED_CHANNEL_MODE = "n" // means that you have to be in the channel to chat, but that's it BLESSED_CHANNEL_MODE = "n" // means that you have to be in the channel to chat, but that's it
NICKS_PER_PROTOMSG = 128 //The capacity sets the max number of nicks to send per message NICKS_PER_PROTOMSG = 128 //The capacity sets the max number of nicks to send per message
CLIENT_READ_BUFFSIZE = 512
CLIENT_READ_TIMEOUT_SEC = 3
) )
var ( var (
@ -21,8 +23,7 @@ var (
type eventType int type eventType int
const ( const (
connected eventType = iota disconnected eventType = iota
disconnected
command command
) )