2013-08-24 06:48:28 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2016-05-03 06:47:24 +00:00
|
|
|
"bytes"
|
2013-08-24 06:48:28 +00:00
|
|
|
"fmt"
|
2016-05-03 06:47:24 +00:00
|
|
|
"io"
|
2016-05-03 06:25:27 +00:00
|
|
|
"libnmdc"
|
2013-08-24 06:48:28 +00:00
|
|
|
"net"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2016-05-03 06:25:27 +00:00
|
|
|
type Server struct {
|
2016-05-03 07:25:37 +00:00
|
|
|
eventChan chan Event
|
|
|
|
running bool
|
|
|
|
name string
|
|
|
|
client *Client
|
|
|
|
channel Channel // Single blessed channel
|
|
|
|
upstreamLauncher libnmdc.HubConnectionOptions
|
|
|
|
upstream libnmdc.HubConnection
|
|
|
|
motd string
|
2016-05-03 06:25:27 +00:00
|
|
|
}
|
2013-08-24 06:48:28 +00:00
|
|
|
|
2016-05-03 06:25:27 +00:00
|
|
|
func NewServer(name string, upstream libnmdc.HubAddress) *Server {
|
2013-08-24 06:48:28 +00:00
|
|
|
return &Server{eventChan: make(chan Event),
|
2016-05-03 07:25:37 +00:00
|
|
|
name: name,
|
|
|
|
client: nil,
|
|
|
|
motd: "Connected to " + name,
|
|
|
|
upstreamLauncher: libnmdc.HubConnectionOptions{
|
|
|
|
Address: upstream,
|
|
|
|
Self: *libnmdc.NewUserInfo(""),
|
|
|
|
},
|
2016-05-02 07:23:15 +00:00
|
|
|
channel: Channel{
|
|
|
|
clientMap: make(map[string]*Client),
|
|
|
|
modeMap: make(map[string]*ClientMode),
|
|
|
|
},
|
2016-05-02 06:45:13 +00:00
|
|
|
}
|
2013-08-24 06:48:28 +00:00
|
|
|
}
|
|
|
|
|
2016-05-03 06:47:24 +00:00
|
|
|
func (s *Server) RunClient(conn net.Conn) {
|
2013-08-24 06:48:28 +00:00
|
|
|
|
2016-05-03 06:47:24 +00:00
|
|
|
s.client = &Client{
|
2013-08-24 06:48:28 +00:00
|
|
|
connection: conn,
|
2016-05-02 06:45:31 +00:00
|
|
|
connected: true,
|
2016-05-03 07:25:37 +00:00
|
|
|
server: s, // FIXME circular reference!
|
2016-05-02 06:45:31 +00:00
|
|
|
}
|
2013-08-24 06:48:28 +00:00
|
|
|
|
2016-05-03 06:47:24 +00:00
|
|
|
// Send the connection handshake
|
2016-05-03 07:25:37 +00:00
|
|
|
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{}{}
|
|
|
|
}()
|
2013-08-24 06:48:28 +00:00
|
|
|
|
2016-05-03 06:47:24 +00:00
|
|
|
// Read loop
|
|
|
|
for {
|
|
|
|
buf := make([]byte, CLIENT_READ_BUFFSIZE)
|
2016-05-03 07:25:37 +00:00
|
|
|
// 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 == io.EOF {
|
|
|
|
s.client.disconnect()
|
|
|
|
return // FIXME cleanup
|
|
|
|
}
|
|
|
|
continue
|
2016-05-03 06:47:24 +00:00
|
|
|
}
|
|
|
|
|
2016-05-03 07:25:37 +00:00
|
|
|
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 {
|
|
|
|
|
|
|
|
// Client sent a command
|
|
|
|
fields := strings.Fields(string(line))
|
|
|
|
if len(fields) < 1 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.HasPrefix(fields[0], ":") {
|
|
|
|
fields = fields[1:]
|
|
|
|
}
|
|
|
|
|
|
|
|
s.handleCommand(strings.ToUpper(fields[0]), fields[1:])
|
|
|
|
}
|
2016-05-03 06:47:24 +00:00
|
|
|
}
|
2016-03-13 06:51:10 +00:00
|
|
|
}
|
2016-05-03 06:47:24 +00:00
|
|
|
}
|
2016-05-02 07:23:15 +00:00
|
|
|
|
2016-05-03 06:47:24 +00:00
|
|
|
}
|
2016-05-02 07:23:15 +00:00
|
|
|
|
2016-05-03 07:25:37 +00:00
|
|
|
func (s *Server) ProtocolReadLoop_NMDC(closeChan chan struct{}) {
|
|
|
|
defer func() {
|
|
|
|
closeChan <- struct{}{}
|
|
|
|
}()
|
2013-08-24 06:48:28 +00:00
|
|
|
|
2016-05-03 07:25:37 +00:00
|
|
|
// Read loop
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-closeChan:
|
|
|
|
return
|
2013-09-08 15:24:17 +00:00
|
|
|
|
2016-05-03 07:25:37 +00:00
|
|
|
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)
|
2016-05-03 06:47:24 +00:00
|
|
|
|
2016-05-03 07:25:37 +00:00
|
|
|
case libnmdc.EVENT_PRIVATE:
|
|
|
|
s.client.reply(rplMsg, s.client.nick, hubEvent.Nick, hubEvent.Message)
|
2016-05-03 06:47:24 +00:00
|
|
|
|
2016-05-03 07:25:37 +00:00
|
|
|
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)
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-09-08 15:24:17 +00:00
|
|
|
}
|
2013-08-24 06:48:28 +00:00
|
|
|
|
2016-05-03 07:25:37 +00:00
|
|
|
func (s *Server) handleCommand(command string, args []string) {
|
2013-08-24 06:48:28 +00:00
|
|
|
|
2013-09-08 15:24:17 +00:00
|
|
|
switch command {
|
2013-10-21 12:34:12 +00:00
|
|
|
case "PING":
|
2016-05-03 07:25:37 +00:00
|
|
|
s.client.reply(rplPong)
|
2016-05-02 07:00:24 +00:00
|
|
|
|
2013-09-08 15:24:17 +00:00
|
|
|
case "INFO":
|
2016-05-03 07:25:37 +00:00
|
|
|
s.client.reply(rplInfo, APP_DESCRIPTION)
|
2016-05-02 07:00:24 +00:00
|
|
|
|
2013-09-08 15:24:17 +00:00
|
|
|
case "VERSION":
|
2016-05-03 07:25:37 +00:00
|
|
|
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]
|
2016-05-02 07:00:24 +00:00
|
|
|
|
2013-09-08 15:24:17 +00:00
|
|
|
case "NICK":
|
2016-05-03 07:25:37 +00:00
|
|
|
if len(args) < 1 {
|
|
|
|
s.client.reply(errMoreArgs)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if s.upstreamLauncher.Self.Nick == "" {
|
|
|
|
// allow set, as part of the login phase
|
|
|
|
s.upstreamLauncher.Self.Nick = args[0]
|
|
|
|
} else {
|
|
|
|
s.client.reply(rplKill, "Can't change nicks on this server", "")
|
|
|
|
s.client.disconnect()
|
|
|
|
}
|
2013-08-24 06:48:28 +00:00
|
|
|
|
2013-09-08 15:24:17 +00:00
|
|
|
case "USER":
|
2016-05-03 07:25:37 +00:00
|
|
|
if s.client.registered == true {
|
|
|
|
s.client.reply(rplKill, "You're already registered.", "")
|
|
|
|
s.client.disconnect()
|
2016-05-03 06:25:27 +00:00
|
|
|
}
|
|
|
|
|
2016-05-03 07:25:37 +00:00
|
|
|
if s.client.nick == "" {
|
|
|
|
s.client.reply(rplKill, "Your nickname is already being used", "")
|
|
|
|
s.client.disconnect()
|
2016-05-03 06:25:27 +00:00
|
|
|
|
2013-08-24 06:48:28 +00:00
|
|
|
} else {
|
2016-05-03 07:25:37 +00:00
|
|
|
s.client.reply(rplWelcome)
|
|
|
|
s.client.registered = true
|
2016-05-03 06:25:27 +00:00
|
|
|
|
2016-05-03 07:25:37 +00:00
|
|
|
// Spawn upstream connection
|
2013-08-24 06:48:28 +00:00
|
|
|
}
|
|
|
|
|
2013-09-08 15:24:17 +00:00
|
|
|
case "JOIN":
|
2016-05-03 07:25:37 +00:00
|
|
|
if s.client.registered == false {
|
|
|
|
s.client.reply(errNotReg)
|
2013-08-24 06:48:28 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(args) < 1 {
|
2016-05-03 07:25:37 +00:00
|
|
|
s.client.reply(errMoreArgs)
|
2013-08-24 06:48:28 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-05-02 07:13:09 +00:00
|
|
|
switch args[0] {
|
|
|
|
case BLESSED_CHANNEL:
|
|
|
|
// That's fine, but they're already there
|
|
|
|
case "0":
|
|
|
|
// Intend to quit all channels, but we don't allow that
|
|
|
|
default:
|
2016-05-03 07:25:37 +00:00
|
|
|
s.client.reply(rplKill, "There is only '"+BLESSED_CHANNEL+"'.", "")
|
|
|
|
s.client.disconnect()
|
2013-08-24 06:48:28 +00:00
|
|
|
}
|
|
|
|
|
2013-09-08 15:24:17 +00:00
|
|
|
case "PART":
|
2016-05-03 07:25:37 +00:00
|
|
|
if s.client.registered == false {
|
|
|
|
s.client.reply(errNotReg)
|
2013-08-24 06:48:28 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-05-02 07:07:58 +00:00
|
|
|
// you can check out any time you like, but you can never leave
|
2016-05-03 07:25:37 +00:00
|
|
|
// we'll need to transmit s.client.reply(rplPart, c.nick, channel.name, reason) messages on behalf of other nmdc users, though
|
2013-08-24 06:48:28 +00:00
|
|
|
|
2013-09-08 15:24:17 +00:00
|
|
|
case "PRIVMSG":
|
2016-05-03 07:25:37 +00:00
|
|
|
if s.client.registered == false {
|
|
|
|
s.client.reply(errNotReg)
|
2013-08-24 06:48:28 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(args) < 2 {
|
2016-05-03 07:25:37 +00:00
|
|
|
s.client.reply(errMoreArgs)
|
2013-08-24 06:48:28 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
message := strings.Join(args[1:], " ")
|
|
|
|
|
2016-05-03 06:25:27 +00:00
|
|
|
// 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 {
|
2016-05-03 07:25:37 +00:00
|
|
|
s.upstream.SayPublic(message)
|
|
|
|
/*
|
|
|
|
for _, c := range s.channel.clientMap {
|
|
|
|
if c != client {
|
|
|
|
c.reply(rplMsg, s.client.nick, args[0], message)
|
|
|
|
}
|
2013-08-24 06:48:28 +00:00
|
|
|
}
|
2016-05-03 07:25:37 +00:00
|
|
|
*/
|
2016-05-03 06:00:40 +00:00
|
|
|
|
2016-05-03 06:25:27 +00:00
|
|
|
} else if nmdcUser, clientExists := s.upstream.Users[args[0]]; clientExists {
|
|
|
|
|
|
|
|
s.upstream.SayPrivate(recipient, message)
|
2016-05-03 07:25:37 +00:00
|
|
|
// client2.reply(rplMsg, s.client.nick, client2.nick, message)
|
2016-05-03 06:00:40 +00:00
|
|
|
|
2013-08-24 06:48:28 +00:00
|
|
|
} else {
|
2016-05-03 07:25:37 +00:00
|
|
|
s.client.reply(errNoSuchNick, args[0])
|
2016-05-03 06:00:40 +00:00
|
|
|
|
2013-08-24 06:48:28 +00:00
|
|
|
}
|
|
|
|
|
2013-09-08 15:24:17 +00:00
|
|
|
case "QUIT":
|
2016-05-03 07:25:37 +00:00
|
|
|
if s.client.registered == false {
|
|
|
|
s.client.reply(errNotReg)
|
2013-08-24 06:48:28 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-05-03 07:25:37 +00:00
|
|
|
s.client.disconnect()
|
2013-08-24 06:48:28 +00:00
|
|
|
|
2013-09-08 15:24:17 +00:00
|
|
|
case "TOPIC":
|
2016-05-03 07:25:37 +00:00
|
|
|
if s.client.registered == false {
|
|
|
|
s.client.reply(errNotReg)
|
2013-08-24 06:48:28 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(args) < 1 {
|
2016-05-03 07:25:37 +00:00
|
|
|
s.client.reply(errMoreArgs)
|
2013-08-24 06:48:28 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-05-02 07:23:15 +00:00
|
|
|
exists := strings.ToLower(args[0]) == BLESSED_CHANNEL
|
2013-08-24 06:48:28 +00:00
|
|
|
if exists == false {
|
2016-05-03 07:25:37 +00:00
|
|
|
s.client.reply(errNoSuchNick, args[0])
|
2013-08-24 06:48:28 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-05-02 07:02:41 +00:00
|
|
|
// Valid topic get
|
2013-08-24 06:48:28 +00:00
|
|
|
if len(args) == 1 {
|
2016-05-03 07:25:37 +00:00
|
|
|
s.client.reply(rplTopic, BLESSED_CHANNEL, s.upstream.HubName)
|
2013-08-24 06:48:28 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-05-02 07:02:41 +00:00
|
|
|
// Disallow topic set
|
2016-05-03 07:25:37 +00:00
|
|
|
s.client.reply(errNoPriv)
|
2016-05-02 07:02:41 +00:00
|
|
|
return
|
2013-08-24 06:48:28 +00:00
|
|
|
|
2013-09-08 15:24:17 +00:00
|
|
|
case "LIST":
|
2016-05-03 07:25:37 +00:00
|
|
|
if s.client.registered == false {
|
|
|
|
s.client.reply(errNotReg)
|
2013-08-24 06:48:28 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-05-03 06:25:27 +00:00
|
|
|
listItem := fmt.Sprintf("%s %d :%s", BLESSED_CHANNEL, len(s.channel.clientMap), s.upstream.HubName)
|
2016-05-03 07:25:37 +00:00
|
|
|
s.client.reply(rplList, listItem)
|
|
|
|
s.client.reply(rplListEnd)
|
2013-08-24 06:48:28 +00:00
|
|
|
|
2013-09-08 15:24:17 +00:00
|
|
|
case "OPER":
|
2016-05-03 07:25:37 +00:00
|
|
|
if s.client.registered == false {
|
|
|
|
s.client.reply(errNotReg)
|
2013-08-24 06:48:28 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(args) < 2 {
|
2016-05-03 07:25:37 +00:00
|
|
|
s.client.reply(errMoreArgs)
|
2013-08-24 06:48:28 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-05-02 07:16:25 +00:00
|
|
|
//username := args[0]
|
|
|
|
//password := args[1]
|
|
|
|
|
|
|
|
if false { // op the user
|
2016-05-03 07:25:37 +00:00
|
|
|
s.client.operator = true
|
|
|
|
s.client.reply(rplOper)
|
2016-05-02 07:16:25 +00:00
|
|
|
return
|
|
|
|
} else {
|
2016-05-03 07:25:37 +00:00
|
|
|
s.client.reply(errPassword)
|
2013-08-24 06:48:28 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-09-08 15:24:17 +00:00
|
|
|
case "KILL":
|
2016-05-03 07:25:37 +00:00
|
|
|
if s.client.registered == false {
|
|
|
|
s.client.reply(errNotReg)
|
2013-08-24 06:48:28 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-05-03 07:25:37 +00:00
|
|
|
s.client.reply(errNoPriv)
|
2016-05-03 06:25:27 +00:00
|
|
|
return
|
2013-08-31 17:44:25 +00:00
|
|
|
|
2013-09-08 15:24:17 +00:00
|
|
|
case "KICK":
|
2016-05-03 07:25:37 +00:00
|
|
|
if s.client.registered == false {
|
|
|
|
s.client.reply(errNotReg)
|
2013-08-29 20:10:28 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-05-03 07:25:37 +00:00
|
|
|
s.client.reply(errNoPriv)
|
2016-05-02 07:00:24 +00:00
|
|
|
return
|
2013-08-29 20:10:28 +00:00
|
|
|
|
2013-09-08 15:24:17 +00:00
|
|
|
case "MODE":
|
2016-05-03 07:25:37 +00:00
|
|
|
if s.client.registered == false {
|
|
|
|
s.client.reply(errNotReg)
|
2013-08-29 16:57:33 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(args) < 1 {
|
2016-05-03 07:25:37 +00:00
|
|
|
s.client.reply(errMoreArgs)
|
2013-08-29 16:57:33 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-05-03 06:00:40 +00:00
|
|
|
if strings.ToLower(args[0]) != BLESSED_CHANNEL {
|
2016-05-03 07:25:37 +00:00
|
|
|
s.client.reply(errNoSuchNick, args[0])
|
2013-08-29 16:57:33 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(args) == 1 {
|
|
|
|
//No more args, they just want the mode
|
2016-05-03 07:25:37 +00:00
|
|
|
s.client.reply(rplChannelModeIs, args[0], BLESSED_CHANNEL_MODE, "")
|
2016-05-03 06:00:40 +00:00
|
|
|
} else {
|
|
|
|
// Setting modes is disallowed
|
2016-05-03 07:25:37 +00:00
|
|
|
s.client.reply(errNoPriv)
|
2013-08-29 16:57:33 +00:00
|
|
|
}
|
|
|
|
|
2016-05-02 07:00:24 +00:00
|
|
|
return
|
2013-08-29 16:57:33 +00:00
|
|
|
|
2013-08-24 06:48:28 +00:00
|
|
|
default:
|
2016-05-03 07:25:37 +00:00
|
|
|
s.client.reply(errUnknownCommand, command)
|
2013-08-24 06:48:28 +00:00
|
|
|
}
|
|
|
|
}
|