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:47:24 +00:00
|
|
|
"time"
|
2013-08-24 06:48:28 +00:00
|
|
|
)
|
|
|
|
|
2016-05-03 06:25:27 +00:00
|
|
|
type Server struct {
|
|
|
|
eventChan chan Event
|
|
|
|
running bool
|
|
|
|
name string
|
|
|
|
client *Client
|
|
|
|
channel Channel // Single blessed channel
|
|
|
|
upstreamAddr libnmdc.HubAddress
|
|
|
|
upstream libnmdc.HubConnection
|
|
|
|
motd string
|
|
|
|
}
|
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 06:25:27 +00:00
|
|
|
name: name,
|
|
|
|
client: nil,
|
|
|
|
motd: "Connected to " + name,
|
|
|
|
upstreamAddr: upstream,
|
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,
|
|
|
|
}
|
2013-08-24 06:48:28 +00:00
|
|
|
|
2016-05-03 06:47:24 +00:00
|
|
|
// Send the connection handshake
|
|
|
|
s.client.reply(rplMOTDStart)
|
|
|
|
motd := s.motd
|
|
|
|
for len(motd) > 80 {
|
|
|
|
s.client.reply(rplMOTD, motd[:80])
|
|
|
|
motd = motd[80:]
|
|
|
|
}
|
|
|
|
if len(motd) > 0 {
|
|
|
|
s.client.reply(rplMOTD, motd)
|
|
|
|
}
|
|
|
|
s.client.reply(rplEndOfMOTD)
|
2013-08-24 06:48:28 +00:00
|
|
|
|
2016-05-03 06:47:24 +00:00
|
|
|
// Read loop
|
|
|
|
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
|
2016-03-13 06:51:10 +00:00
|
|
|
}
|
2016-05-03 06:47:24 +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 {
|
|
|
|
s.handleCommandEvent(string(line))
|
|
|
|
}
|
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 06:47:24 +00:00
|
|
|
func (s *Server) handleCommandEvent(input string) {
|
2013-08-24 06:48:28 +00:00
|
|
|
|
2016-05-03 06:47:24 +00:00
|
|
|
//Client send a command
|
|
|
|
fields := strings.Fields(input)
|
|
|
|
if len(fields) < 1 {
|
|
|
|
return
|
|
|
|
}
|
2013-09-08 15:24:17 +00:00
|
|
|
|
2016-05-03 06:47:24 +00:00
|
|
|
if strings.HasPrefix(fields[0], ":") {
|
|
|
|
fields = fields[1:]
|
2013-08-24 06:48:28 +00:00
|
|
|
}
|
2016-05-03 06:47:24 +00:00
|
|
|
command := strings.ToUpper(fields[0])
|
|
|
|
args := fields[1:]
|
|
|
|
|
|
|
|
s.handleCommand(s.client, command, args)
|
|
|
|
|
2013-09-08 15:24:17 +00:00
|
|
|
}
|
2013-08-24 06:48:28 +00:00
|
|
|
|
2013-09-08 15:24:17 +00:00
|
|
|
func (s *Server) handleCommand(client *Client, 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":
|
|
|
|
client.reply(rplPong)
|
2016-05-02 07:00:24 +00:00
|
|
|
|
2013-09-08 15:24:17 +00:00
|
|
|
case "INFO":
|
2016-05-02 06:45:13 +00:00
|
|
|
client.reply(rplInfo, APP_DESCRIPTION)
|
2016-05-02 07:00:24 +00:00
|
|
|
|
2013-09-08 15:24:17 +00:00
|
|
|
case "VERSION":
|
|
|
|
client.reply(rplVersion, VERSION)
|
2016-05-02 07:00:24 +00:00
|
|
|
|
2013-09-08 15:24:17 +00:00
|
|
|
case "NICK":
|
2016-05-02 06:54:52 +00:00
|
|
|
client.reply(rplKill, "Can't change nicks on this server", "")
|
|
|
|
client.disconnect()
|
2013-08-24 06:48:28 +00:00
|
|
|
|
2013-09-08 15:24:17 +00:00
|
|
|
case "USER":
|
2016-05-03 06:25:27 +00:00
|
|
|
if client.registered == true {
|
|
|
|
client.reply(rplKill, "You're already registered.", "")
|
|
|
|
client.disconnect()
|
|
|
|
}
|
|
|
|
|
2013-09-08 15:24:17 +00:00
|
|
|
if client.nick == "" {
|
2016-02-28 01:54:33 +00:00
|
|
|
client.reply(rplKill, "Your nickname is already being used", "")
|
2013-09-08 15:24:17 +00:00
|
|
|
client.disconnect()
|
2016-05-03 06:25:27 +00:00
|
|
|
|
2013-08-24 06:48:28 +00:00
|
|
|
} else {
|
2013-09-08 15:24:17 +00:00
|
|
|
client.reply(rplWelcome)
|
|
|
|
client.registered = true
|
2016-05-03 06:25:27 +00:00
|
|
|
|
|
|
|
// Spawn
|
2013-08-24 06:48:28 +00:00
|
|
|
}
|
|
|
|
|
2013-09-08 15:24:17 +00:00
|
|
|
case "JOIN":
|
|
|
|
if client.registered == false {
|
|
|
|
client.reply(errNotReg)
|
2013-08-24 06:48:28 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(args) < 1 {
|
2013-09-08 15:24:17 +00:00
|
|
|
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:
|
|
|
|
client.reply(rplKill, "There is only '"+BLESSED_CHANNEL+"'.", "")
|
|
|
|
client.disconnect()
|
2013-08-24 06:48:28 +00:00
|
|
|
}
|
|
|
|
|
2013-09-08 15:24:17 +00:00
|
|
|
case "PART":
|
|
|
|
if client.registered == false {
|
|
|
|
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
|
|
|
|
// we'll need to transmit 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":
|
|
|
|
if client.registered == false {
|
|
|
|
client.reply(errNotReg)
|
2013-08-24 06:48:28 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(args) < 2 {
|
2013-09-08 15:24:17 +00:00
|
|
|
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-02 07:23:15 +00:00
|
|
|
for _, c := range s.channel.clientMap {
|
2013-09-08 15:24:17 +00:00
|
|
|
if c != client {
|
|
|
|
c.reply(rplMsg, client.nick, args[0], message)
|
2013-08-24 06:48:28 +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)
|
|
|
|
// client2.reply(rplMsg, client.nick, client2.nick, message)
|
2016-05-03 06:00:40 +00:00
|
|
|
|
2013-08-24 06:48:28 +00:00
|
|
|
} else {
|
2013-09-08 15:24:17 +00:00
|
|
|
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":
|
|
|
|
if client.registered == false {
|
|
|
|
client.reply(errNotReg)
|
2013-08-24 06:48:28 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-09-08 15:24:17 +00:00
|
|
|
client.disconnect()
|
2013-08-24 06:48:28 +00:00
|
|
|
|
2013-09-08 15:24:17 +00:00
|
|
|
case "TOPIC":
|
|
|
|
if client.registered == false {
|
|
|
|
client.reply(errNotReg)
|
2013-08-24 06:48:28 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(args) < 1 {
|
2013-09-08 15:24:17 +00:00
|
|
|
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 {
|
2013-09-08 15:24:17 +00:00
|
|
|
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 06:25:27 +00:00
|
|
|
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
|
|
|
|
client.reply(errNoPriv)
|
|
|
|
return
|
2013-08-24 06:48:28 +00:00
|
|
|
|
2013-09-08 15:24:17 +00:00
|
|
|
case "LIST":
|
|
|
|
if client.registered == false {
|
|
|
|
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-02 07:13:09 +00:00
|
|
|
client.reply(rplList, listItem)
|
|
|
|
client.reply(rplListEnd)
|
2013-08-24 06:48:28 +00:00
|
|
|
|
2013-09-08 15:24:17 +00:00
|
|
|
case "OPER":
|
|
|
|
if client.registered == false {
|
|
|
|
client.reply(errNotReg)
|
2013-08-24 06:48:28 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(args) < 2 {
|
2013-09-08 15:24:17 +00:00
|
|
|
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
|
|
|
|
client.operator = true
|
|
|
|
client.reply(rplOper)
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
client.reply(errPassword)
|
2013-08-24 06:48:28 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-09-08 15:24:17 +00:00
|
|
|
case "KILL":
|
|
|
|
if client.registered == false {
|
|
|
|
client.reply(errNotReg)
|
2013-08-24 06:48:28 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-05-03 06:25:27 +00:00
|
|
|
client.reply(errNoPriv)
|
|
|
|
return
|
2013-08-31 17:44:25 +00:00
|
|
|
|
2013-09-08 15:24:17 +00:00
|
|
|
case "KICK":
|
|
|
|
if client.registered == false {
|
|
|
|
client.reply(errNotReg)
|
2013-08-29 20:10:28 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-05-02 07:00:24 +00:00
|
|
|
client.reply(errNoPriv)
|
|
|
|
return
|
2013-08-29 20:10:28 +00:00
|
|
|
|
2013-09-08 15:24:17 +00:00
|
|
|
case "MODE":
|
|
|
|
if client.registered == false {
|
|
|
|
client.reply(errNotReg)
|
2013-08-29 16:57:33 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(args) < 1 {
|
2013-09-08 15:24:17 +00:00
|
|
|
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 {
|
2013-09-08 15:24:17 +00:00
|
|
|
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 06:00:40 +00:00
|
|
|
client.reply(rplChannelModeIs, args[0], BLESSED_CHANNEL_MODE, "")
|
|
|
|
} else {
|
|
|
|
// Setting modes is disallowed
|
|
|
|
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:
|
2013-09-08 15:24:17 +00:00
|
|
|
client.reply(errUnknownCommand, command)
|
2013-08-24 06:48:28 +00:00
|
|
|
}
|
|
|
|
}
|