--HG--
branch : nmdc-ircfrontend
This commit is contained in:
. 2016-05-03 19:25:37 +12:00
parent 46114809bd
commit 15b08e0f27
2 changed files with 212 additions and 132 deletions

View File

@ -3,11 +3,11 @@ package main
import ( import (
"fmt" "fmt"
"net" "net"
"strings"
"time" "time"
) )
type Client struct { type Client struct {
server *Server
connection net.Conn connection net.Conn
nick string nick string
registered bool registered bool
@ -15,6 +15,7 @@ type Client struct {
operator bool operator bool
} }
/*
func (c *Client) joinChannel(channelName string) { func (c *Client) joinChannel(channelName string) {
newChannel := false newChannel := false
@ -29,18 +30,6 @@ func (c *Client) joinChannel(channelName string) {
return return
} }
// Send notifications to /other/ clients that /we/ joined this room
for _, client := range channel.clientMap {
client.reply(rplJoin, c.nick, BLESSED_CHANNEL)
}
// Transmit topic
if channel.topic != "" {
c.reply(rplTopic, BLESSED_CHANNEL, channel.topic)
} else {
c.reply(rplNoTopic, BLESSED_CHANNEL)
}
// Transmit the list of joined users to us // Transmit the list of joined users to us
nicks := make([]string, 0, NICKS_PER_PROTOMSG) nicks := make([]string, 0, NICKS_PER_PROTOMSG)
@ -65,11 +54,26 @@ func (c *Client) joinChannel(channelName string) {
c.reply(rplEndOfNames, channelName) c.reply(rplEndOfNames, channelName)
} }
*/
func (c *Client) disconnect() { func (c *Client) disconnect() {
c.connected = false c.connected = false
} }
func (c *Client) sendGlobalMessage(motd string) {
c.reply(rplMOTDStart)
for len(motd) > 80 {
c.reply(rplMOTD, motd[:80])
motd = motd[80:]
}
if len(motd) > 0 {
c.reply(rplMOTD, motd)
}
c.reply(rplEndOfMOTD)
}
//Send a reply to a user with the code specified //Send a reply to a user with the code specified
func (c *Client) reply(code replyCode, args ...string) { func (c *Client) reply(code replyCode, args ...string) {
if c.connected == false { if c.connected == false {
@ -96,6 +100,7 @@ func (c *Client) reply(code replyCode, args ...string) {
case rplKill: case rplKill:
c.write(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:
// FIXME escape newlines in message!!
c.write(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.write(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]))

257
server.go
View File

@ -7,7 +7,6 @@ import (
"libnmdc" "libnmdc"
"net" "net"
"strings" "strings"
"time"
) )
type Server struct { type Server struct {
@ -16,7 +15,7 @@ type Server struct {
name string name string
client *Client client *Client
channel Channel // Single blessed channel channel Channel // Single blessed channel
upstreamAddr libnmdc.HubAddress upstreamLauncher libnmdc.HubConnectionOptions
upstream libnmdc.HubConnection upstream libnmdc.HubConnection
motd string motd string
} }
@ -26,7 +25,10 @@ func NewServer(name string, upstream libnmdc.HubAddress) *Server {
name: name, name: name,
client: nil, client: nil,
motd: "Connected to " + name, motd: "Connected to " + name,
upstreamAddr: upstream, upstreamLauncher: libnmdc.HubConnectionOptions{
Address: upstream,
Self: *libnmdc.NewUserInfo(""),
},
channel: Channel{ channel: Channel{
clientMap: make(map[string]*Client), clientMap: make(map[string]*Client),
modeMap: make(map[string]*ClientMode), modeMap: make(map[string]*ClientMode),
@ -39,25 +41,39 @@ func (s *Server) RunClient(conn net.Conn) {
s.client = &Client{ s.client = &Client{
connection: conn, connection: conn,
connected: true, connected: true,
server: s, // FIXME circular reference!
} }
// Send the connection handshake // Send the connection handshake
s.client.reply(rplMOTDStart) s.client.sendGlobalMessage(s.motd)
motd := s.motd
for len(motd) > 80 { // Can't connect to the upstream server yet, until we've recieved a nick.
s.client.reply(rplMOTD, motd[:80]) // So we can't have a conjoined select statement
motd = motd[80:] // Need a separate goroutine for both IRC and NMDC protocols, and
} // synchronisation to ensure simultaneous cleanup
if len(motd) > 0 {
s.client.reply(rplMOTD, motd) closeChan := make(chan struct{}, 2)
} go s.ProtocolReadLoop_IRC(closeChan)
s.client.reply(rplEndOfMOTD) // FIXME
}
func (s *Server) ProtocolReadLoop_IRC(closeChan chan struct{}) {
defer func() {
closeChan <- struct{}{}
}()
// Read loop // Read loop
for { for {
s.client.connection.SetReadDeadline(time.Now().Add(time.Second * CLIENT_READ_TIMEOUT_SEC))
buf := make([]byte, CLIENT_READ_BUFFSIZE) buf := make([]byte, CLIENT_READ_BUFFSIZE)
ln, err := s.client.connection.Read(buf) // 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):
if err != nil { if err != nil {
if err == io.EOF { if err == io.EOF {
s.client.disconnect() s.client.disconnect()
@ -72,17 +88,9 @@ func (s *Server) RunClient(conn net.Conn) {
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 {
s.handleCommandEvent(string(line))
}
}
}
} // Client sent a command
fields := strings.Fields(string(line))
func (s *Server) handleCommandEvent(input string) {
//Client send a command
fields := strings.Fields(input)
if len(fields) < 1 { if len(fields) < 1 {
return return
} }
@ -90,54 +98,118 @@ func (s *Server) handleCommandEvent(input string) {
if strings.HasPrefix(fields[0], ":") { if strings.HasPrefix(fields[0], ":") {
fields = fields[1:] fields = fields[1:]
} }
command := strings.ToUpper(fields[0])
args := fields[1:]
s.handleCommand(s.client, command, args) s.handleCommand(strings.ToUpper(fields[0]), fields[1:])
}
}
}
}
} }
func (s *Server) handleCommand(client *Client, command string, args []string) { func (s *Server) ProtocolReadLoop_NMDC(closeChan chan struct{}) {
defer func() {
closeChan <- struct{}{}
}()
// Read loop
for {
select {
case <-closeChan:
return
case hubEvent := <-s.upstream.OnEvent:
switch hubEvent.EventType {
case libnmdc.EVENT_USER_JOINED:
s.client.reply(rplJoin, hubEvent.Nick, BLESSED_CHANNEL)
case libnmdc.EVENT_USER_PART:
s.client.reply(rplPart, hubEvent.Nick, BLESSED_CHANNEL, "Disconnected")
case libnmdc.EVENT_USER_UPDATED_INFO:
// description change - no relevance for IRC users
case libnmdc.EVENT_CONNECTION_STATE_CHANGED:
s.client.sendGlobalMessage("Upstream: " + hubEvent.StateChange.Format())
case libnmdc.EVENT_HUBNAME_CHANGED:
s.client.reply(rplTopic, BLESSED_CHANNEL, hubEvent.Nick)
// c.reply(rplNoTopic, BLESSED_CHANNEL)
case libnmdc.EVENT_PRIVATE:
s.client.reply(rplMsg, s.client.nick, hubEvent.Nick, hubEvent.Message)
case libnmdc.EVENT_PUBLIC:
s.client.reply(rplMsg, s.client.nick, BLESSED_CHANNEL, hubEvent.Message)
case libnmdc.EVENT_SYSTEM_MESSAGE_FROM_CONN, libnmdc.EVENT_SYSTEM_MESSAGE_FROM_HUB:
s.client.sendGlobalMessage(hubEvent.Message)
}
}
}
}
func (s *Server) handleCommand(command string, args []string) {
switch command { switch command {
case "PING": case "PING":
client.reply(rplPong) s.client.reply(rplPong)
case "INFO": case "INFO":
client.reply(rplInfo, APP_DESCRIPTION) s.client.reply(rplInfo, APP_DESCRIPTION)
case "VERSION": case "VERSION":
client.reply(rplVersion, VERSION) s.client.reply(rplVersion, VERSION)
case "PASS":
// RFC2812 registration
// Stash the password for later
if len(args) < 1 {
s.client.reply(errMoreArgs)
return
}
s.upstreamLauncher.NickPassword = args[0]
case "NICK": case "NICK":
client.reply(rplKill, "Can't change nicks on this server", "") if len(args) < 1 {
client.disconnect() s.client.reply(errMoreArgs)
return
case "USER":
if client.registered == true {
client.reply(rplKill, "You're already registered.", "")
client.disconnect()
} }
if client.nick == "" { if s.upstreamLauncher.Self.Nick == "" {
client.reply(rplKill, "Your nickname is already being used", "") // allow set, as part of the login phase
client.disconnect() s.upstreamLauncher.Self.Nick = args[0]
} else {
s.client.reply(rplKill, "Can't change nicks on this server", "")
s.client.disconnect()
}
case "USER":
if s.client.registered == true {
s.client.reply(rplKill, "You're already registered.", "")
s.client.disconnect()
}
if s.client.nick == "" {
s.client.reply(rplKill, "Your nickname is already being used", "")
s.client.disconnect()
} else { } else {
client.reply(rplWelcome) s.client.reply(rplWelcome)
client.registered = true s.client.registered = true
// Spawn // Spawn upstream connection
} }
case "JOIN": case "JOIN":
if client.registered == false { if s.client.registered == false {
client.reply(errNotReg) s.client.reply(errNotReg)
return return
} }
if len(args) < 1 { if len(args) < 1 {
client.reply(errMoreArgs) s.client.reply(errMoreArgs)
return return
} }
@ -147,27 +219,27 @@ func (s *Server) handleCommand(client *Client, command string, args []string) {
case "0": case "0":
// Intend to quit all channels, but we don't allow that // Intend to quit all channels, but we don't allow that
default: default:
client.reply(rplKill, "There is only '"+BLESSED_CHANNEL+"'.", "") s.client.reply(rplKill, "There is only '"+BLESSED_CHANNEL+"'.", "")
client.disconnect() s.client.disconnect()
} }
case "PART": case "PART":
if client.registered == false { if s.client.registered == false {
client.reply(errNotReg) s.client.reply(errNotReg)
return return
} }
// you can check out any time you like, but you can never leave // you can check out any time you like, but you can never leave
// we'll need to transmit client.reply(rplPart, c.nick, channel.name, reason) messages on behalf of other nmdc users, though // we'll need to transmit s.client.reply(rplPart, c.nick, channel.name, reason) messages on behalf of other nmdc users, though
case "PRIVMSG": case "PRIVMSG":
if client.registered == false { if s.client.registered == false {
client.reply(errNotReg) s.client.reply(errNotReg)
return return
} }
if len(args) < 2 { if len(args) < 2 {
client.reply(errMoreArgs) s.client.reply(errMoreArgs)
return return
} }
@ -178,75 +250,78 @@ func (s *Server) handleCommand(client *Client, command string, args []string) {
recipient := strings.ToLower(args[0]) recipient := strings.ToLower(args[0])
if recipient == BLESSED_CHANNEL { if recipient == BLESSED_CHANNEL {
s.upstream.SayPublic(message)
/*
for _, c := range s.channel.clientMap { for _, c := range s.channel.clientMap {
if c != client { if c != client {
c.reply(rplMsg, client.nick, args[0], message) c.reply(rplMsg, s.client.nick, args[0], message)
} }
} }
*/
} else if nmdcUser, clientExists := s.upstream.Users[args[0]]; clientExists { } else if nmdcUser, clientExists := s.upstream.Users[args[0]]; clientExists {
s.upstream.SayPrivate(recipient, message) s.upstream.SayPrivate(recipient, message)
// client2.reply(rplMsg, client.nick, client2.nick, message) // client2.reply(rplMsg, s.client.nick, client2.nick, message)
} else { } else {
client.reply(errNoSuchNick, args[0]) s.client.reply(errNoSuchNick, args[0])
} }
case "QUIT": case "QUIT":
if client.registered == false { if s.client.registered == false {
client.reply(errNotReg) s.client.reply(errNotReg)
return return
} }
client.disconnect() s.client.disconnect()
case "TOPIC": case "TOPIC":
if client.registered == false { if s.client.registered == false {
client.reply(errNotReg) s.client.reply(errNotReg)
return return
} }
if len(args) < 1 { if len(args) < 1 {
client.reply(errMoreArgs) s.client.reply(errMoreArgs)
return return
} }
exists := strings.ToLower(args[0]) == BLESSED_CHANNEL exists := strings.ToLower(args[0]) == BLESSED_CHANNEL
if exists == false { if exists == false {
client.reply(errNoSuchNick, args[0]) s.client.reply(errNoSuchNick, args[0])
return return
} }
// Valid topic get // Valid topic get
if len(args) == 1 { if len(args) == 1 {
client.reply(rplTopic, BLESSED_CHANNEL, s.upstream.HubName) s.client.reply(rplTopic, BLESSED_CHANNEL, s.upstream.HubName)
return return
} }
// Disallow topic set // Disallow topic set
client.reply(errNoPriv) s.client.reply(errNoPriv)
return return
case "LIST": case "LIST":
if client.registered == false { if s.client.registered == false {
client.reply(errNotReg) s.client.reply(errNotReg)
return return
} }
listItem := fmt.Sprintf("%s %d :%s", BLESSED_CHANNEL, len(s.channel.clientMap), s.upstream.HubName) listItem := fmt.Sprintf("%s %d :%s", BLESSED_CHANNEL, len(s.channel.clientMap), s.upstream.HubName)
client.reply(rplList, listItem) s.client.reply(rplList, listItem)
client.reply(rplListEnd) s.client.reply(rplListEnd)
case "OPER": case "OPER":
if client.registered == false { if s.client.registered == false {
client.reply(errNotReg) s.client.reply(errNotReg)
return return
} }
if len(args) < 2 { if len(args) < 2 {
client.reply(errMoreArgs) s.client.reply(errMoreArgs)
return return
} }
@ -254,59 +329,59 @@ func (s *Server) handleCommand(client *Client, command string, args []string) {
//password := args[1] //password := args[1]
if false { // op the user if false { // op the user
client.operator = true s.client.operator = true
client.reply(rplOper) s.client.reply(rplOper)
return return
} else { } else {
client.reply(errPassword) s.client.reply(errPassword)
} }
case "KILL": case "KILL":
if client.registered == false { if s.client.registered == false {
client.reply(errNotReg) s.client.reply(errNotReg)
return return
} }
client.reply(errNoPriv) s.client.reply(errNoPriv)
return return
case "KICK": case "KICK":
if client.registered == false { if s.client.registered == false {
client.reply(errNotReg) s.client.reply(errNotReg)
return return
} }
client.reply(errNoPriv) s.client.reply(errNoPriv)
return return
case "MODE": case "MODE":
if client.registered == false { if s.client.registered == false {
client.reply(errNotReg) s.client.reply(errNotReg)
return return
} }
if len(args) < 1 { if len(args) < 1 {
client.reply(errMoreArgs) s.client.reply(errMoreArgs)
return return
} }
if strings.ToLower(args[0]) != BLESSED_CHANNEL { if strings.ToLower(args[0]) != BLESSED_CHANNEL {
client.reply(errNoSuchNick, args[0]) s.client.reply(errNoSuchNick, args[0])
return return
} }
if len(args) == 1 { if len(args) == 1 {
//No more args, they just want the mode //No more args, they just want the mode
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
client.reply(errNoPriv) s.client.reply(errNoPriv)
} }
return return
default: default:
client.reply(errUnknownCommand, command) s.client.reply(errUnknownCommand, command)
} }
} }