nmdc-ircfrontend/server.go

464 lines
11 KiB
Go
Raw Normal View History

2013-08-24 06:48:28 +00:00
package main
import (
"bytes"
2013-08-24 06:48:28 +00:00
"fmt"
"io"
2016-05-03 06:25:27 +00:00
"libnmdc"
2013-08-24 06:48:28 +00:00
"net"
"strings"
2016-05-03 07:57:02 +00:00
"time"
2013-08-24 06:48:28 +00:00
)
2016-05-03 06:25:27 +00:00
type Server struct {
2016-05-05 07:11:40 +00:00
//eventChan chan Event
running bool
name string
clientConn net.Conn
clientNick string
clientRegistered bool
clientOperator bool
2016-05-03 07:25:37 +00:00
upstreamLauncher libnmdc.HubConnectionOptions
upstreamCloser chan struct{}
2016-05-03 07:57:02 +00:00
upstream *libnmdc.HubConnection
2016-05-03 07:25:37 +00:00
motd string
2016-05-03 06:25:27 +00:00
}
2013-08-24 06:48:28 +00:00
2016-05-05 07:11:40 +00:00
func NewServer(name string, upstream libnmdc.HubAddress, conn net.Conn) *Server {
2016-05-03 07:57:02 +00:00
self := libnmdc.NewUserInfo("")
self.ClientTag = APP_DESCRIPTION
2016-05-05 07:11:40 +00:00
return &Server{
name: name,
clientConn: conn,
motd: "Connected to " + name,
2016-05-03 07:25:37 +00:00
upstreamLauncher: libnmdc.HubConnectionOptions{
Address: upstream,
2016-05-03 07:57:02 +00:00
Self: *self,
2016-05-03 07:25:37 +00:00
},
upstreamCloser: make(chan struct{}, 1),
}
2013-08-24 06:48:28 +00:00
}
2016-05-05 07:11:40 +00:00
func (s *Server) RunWorker() {
2013-08-24 06:48:28 +00:00
// Send the connection handshake
2016-05-05 07:11:40 +00:00
s.sendClientGlobalMessage(s.motd)
2016-05-03 07:25:37 +00:00
// Can't connect to the upstream server yet, until we've recieved a nick.
for {
buf := make([]byte, CLIENT_READ_BUFFSIZE)
2016-05-05 07:11:40 +00:00
s.clientConn.SetReadDeadline(time.Now().Add(time.Second * CLIENT_READ_TIMEOUT_SEC))
ln, err := s.clientConn.Read(buf)
2016-05-03 07:57:02 +00:00
if err != nil {
if err == io.EOF {
2016-05-05 07:11:40 +00:00
s.DisconnectClient()
if s.clientConn != nil && s.clientRegistered {
s.upstreamCloser <- struct{}{}
}
2016-05-03 07:57:02 +00:00
return // FIXME cleanup
}
2016-05-03 07:57:02 +00:00
continue
}
2016-05-03 07:57:02 +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
}
2016-05-03 07:25:37 +00:00
2016-05-03 07:57:02 +00:00
if strings.HasPrefix(fields[0], ":") {
fields = fields[1:]
2016-05-03 07:25:37 +00:00
}
2016-05-03 07:57:02 +00:00
s.handleCommand(strings.ToUpper(fields[0]), fields[1:])
}
}
}
}
2016-05-05 07:11:40 +00:00
func (s *Server) upstreamWorker() {
2016-05-03 07:57:02 +00:00
// Initiate connection
s.upstream = s.upstreamLauncher.Connect()
2013-08-24 06:48:28 +00:00
2016-05-03 07:25:37 +00:00
// Read loop
for {
select {
2016-05-05 07:11:40 +00:00
case <-s.upstreamCloser:
// Abandon the upstream connection
s.upstream.Disconnect()
2016-05-03 07:25:37 +00:00
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:
2016-05-05 07:11:40 +00:00
s.reply(rplJoin, hubEvent.Nick, BLESSED_CHANNEL)
2016-05-03 07:25:37 +00:00
case libnmdc.EVENT_USER_PART:
2016-05-05 07:11:40 +00:00
s.reply(rplPart, hubEvent.Nick, BLESSED_CHANNEL, "Disconnected")
2016-05-03 07:25:37 +00:00
case libnmdc.EVENT_USER_UPDATED_INFO:
// description change - no relevance for IRC users
case libnmdc.EVENT_CONNECTION_STATE_CHANGED:
2016-05-05 07:11:40 +00:00
s.sendClientGlobalMessage("Upstream: " + hubEvent.StateChange.Format())
2016-05-03 07:25:37 +00:00
case libnmdc.EVENT_HUBNAME_CHANGED:
2016-05-05 07:11:40 +00:00
s.reply(rplTopic, BLESSED_CHANNEL, hubEvent.Nick)
2016-05-03 07:25:37 +00:00
// c.reply(rplNoTopic, BLESSED_CHANNEL)
2016-05-03 07:25:37 +00:00
case libnmdc.EVENT_PRIVATE:
2016-05-05 07:11:40 +00:00
s.reply(rplMsg, s.clientNick, hubEvent.Nick, hubEvent.Message)
2016-05-03 07:25:37 +00:00
case libnmdc.EVENT_PUBLIC:
2016-05-05 07:11:40 +00:00
s.reply(rplMsg, s.clientNick, BLESSED_CHANNEL, hubEvent.Message)
2016-05-03 07:25:37 +00:00
case libnmdc.EVENT_SYSTEM_MESSAGE_FROM_CONN, libnmdc.EVENT_SYSTEM_MESSAGE_FROM_HUB:
2016-05-05 07:11:40 +00:00
s.sendClientGlobalMessage(hubEvent.Message)
2016-05-03 07:25:37 +00:00
}
}
}
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-05 07:11:40 +00:00
s.reply(rplPong)
2013-09-08 15:24:17 +00:00
case "INFO":
2016-05-05 07:11:40 +00:00
s.reply(rplInfo, APP_DESCRIPTION)
2013-09-08 15:24:17 +00:00
case "VERSION":
2016-05-05 07:11:40 +00:00
s.reply(rplVersion, VERSION)
2016-05-03 07:25:37 +00:00
case "PASS":
// RFC2812 registration
// Stash the password for later
if len(args) < 1 {
2016-05-05 07:11:40 +00:00
s.reply(errMoreArgs)
2016-05-03 07:25:37 +00:00
return
}
s.upstreamLauncher.NickPassword = args[0]
2013-09-08 15:24:17 +00:00
case "NICK":
2016-05-03 07:25:37 +00:00
if len(args) < 1 {
2016-05-05 07:11:40 +00:00
s.reply(errMoreArgs)
2016-05-03 07:25:37 +00:00
return
}
if s.upstreamLauncher.Self.Nick == "" {
// allow set, as part of the login phase
s.upstreamLauncher.Self.Nick = args[0]
} else {
2016-05-05 07:11:40 +00:00
s.reply(rplKill, "Can't change nicks on this server", "")
s.DisconnectClient()
2016-05-03 07:25:37 +00:00
}
2013-08-24 06:48:28 +00:00
2013-09-08 15:24:17 +00:00
case "USER":
2016-05-03 07:57:02 +00:00
// This command sets altname, realname, ... none of which we use
// It's the final step in a PASS/NICK/USER login handshake.
2016-05-05 07:11:40 +00:00
if s.clientRegistered == true {
s.reply(rplKill, "You're already registered.", "")
s.DisconnectClient()
2016-05-03 07:57:02 +00:00
return
2016-05-03 06:25:27 +00:00
}
2016-05-05 07:11:40 +00:00
if s.clientNick == "" {
s.reply(rplKill, "Your nickname is already being used", "")
s.DisconnectClient()
2016-05-03 07:57:02 +00:00
return
}
2016-05-03 06:25:27 +00:00
2016-05-05 07:11:40 +00:00
s.reply(rplWelcome)
s.clientRegistered = true
2016-05-03 06:25:27 +00:00
2016-05-03 07:57:02 +00:00
// Spawn upstream connection
2016-05-05 07:11:40 +00:00
go s.upstreamWorker() // FIXME need shutdown synchronisation
2016-05-03 07:57:02 +00:00
// Tell the user that they themselves joined the chat channel
2016-05-05 07:11:40 +00:00
s.reply(rplJoin, s.clientNick, BLESSED_CHANNEL)
2013-08-24 06:48:28 +00:00
2013-09-08 15:24:17 +00:00
case "JOIN":
2016-05-05 07:11:40 +00:00
if s.clientRegistered == false {
s.reply(errNotReg)
2013-08-24 06:48:28 +00:00
return
}
if len(args) < 1 {
2016-05-05 07:11:40 +00:00
s.reply(errMoreArgs)
2013-08-24 06:48:28 +00:00
return
}
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-05 07:11:40 +00:00
s.reply(rplKill, "There is only '"+BLESSED_CHANNEL+"'.", "")
s.DisconnectClient()
2013-08-24 06:48:28 +00:00
}
2013-09-08 15:24:17 +00:00
case "PART":
2016-05-05 07:11:40 +00:00
if s.clientRegistered == false {
s.reply(errNotReg)
2013-08-24 06:48:28 +00:00
return
}
// you can check out any time you like, but you can never leave
2016-05-05 07:11:40 +00:00
// we'll need to transmit s.reply(rplPart, s.clientNick, 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-05 07:11:40 +00:00
if s.clientRegistered == false {
s.reply(errNotReg)
2013-08-24 06:48:28 +00:00
return
}
if len(args) < 2 {
2016-05-05 07:11:40 +00:00
s.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
2016-05-03 07:57:02 +00:00
if strings.ToLower(args[0]) == BLESSED_CHANNEL {
2016-05-03 07:25:37 +00:00
s.upstream.SayPublic(message)
2016-05-03 07:57:02 +00:00
} else if _, clientExists := s.upstream.Users[args[0]]; clientExists {
s.upstream.SayPrivate(args[0], message)
2013-08-24 06:48:28 +00:00
} else {
2016-05-05 07:11:40 +00:00
s.reply(errNoSuchNick, args[0])
2013-08-24 06:48:28 +00:00
}
2013-09-08 15:24:17 +00:00
case "QUIT":
2016-05-05 07:11:40 +00:00
if s.clientRegistered == false {
s.reply(errNotReg)
2013-08-24 06:48:28 +00:00
return
}
2016-05-05 07:11:40 +00:00
s.DisconnectClient()
2013-08-24 06:48:28 +00:00
2013-09-08 15:24:17 +00:00
case "TOPIC":
2016-05-05 07:11:40 +00:00
if s.clientRegistered == false {
s.reply(errNotReg)
2013-08-24 06:48:28 +00:00
return
}
if len(args) < 1 {
2016-05-05 07:11:40 +00:00
s.reply(errMoreArgs)
2013-08-24 06:48:28 +00:00
return
}
exists := strings.ToLower(args[0]) == BLESSED_CHANNEL
2013-08-24 06:48:28 +00:00
if exists == false {
2016-05-05 07:11:40 +00:00
s.reply(errNoSuchNick, args[0])
2013-08-24 06:48:28 +00:00
return
}
// Valid topic get
2013-08-24 06:48:28 +00:00
if len(args) == 1 {
2016-05-05 07:11:40 +00:00
s.reply(rplTopic, BLESSED_CHANNEL, s.upstream.HubName)
2013-08-24 06:48:28 +00:00
return
}
// Disallow topic set
2016-05-05 07:11:40 +00:00
s.reply(errNoPriv)
return
2013-08-24 06:48:28 +00:00
2013-09-08 15:24:17 +00:00
case "LIST":
2016-05-05 07:11:40 +00:00
if s.clientRegistered == false {
s.reply(errNotReg)
2013-08-24 06:48:28 +00:00
return
}
2016-05-05 07:11:40 +00:00
listItem := fmt.Sprintf("%s %d :%s", BLESSED_CHANNEL, len(s.upstream.Users), s.upstream.HubName)
s.reply(rplList, listItem)
s.reply(rplListEnd)
2013-08-24 06:48:28 +00:00
2013-09-08 15:24:17 +00:00
case "OPER":
2016-05-05 07:11:40 +00:00
if s.clientRegistered == false {
s.reply(errNotReg)
2013-08-24 06:48:28 +00:00
return
}
if len(args) < 2 {
2016-05-05 07:11:40 +00:00
s.reply(errMoreArgs)
2013-08-24 06:48:28 +00:00
return
}
2016-05-03 07:57:02 +00:00
// Can't use this command.
2016-05-05 07:11:40 +00:00
s.reply(errPassword)
2013-08-24 06:48:28 +00:00
2013-09-08 15:24:17 +00:00
case "KILL":
2016-05-05 07:11:40 +00:00
if s.clientRegistered == false {
s.reply(errNotReg)
2013-08-24 06:48:28 +00:00
return
}
2016-05-05 07:11:40 +00:00
s.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-05 07:11:40 +00:00
if s.clientRegistered == false {
s.reply(errNotReg)
2013-08-29 20:10:28 +00:00
return
}
2016-05-05 07:11:40 +00:00
s.reply(errNoPriv)
return
2013-08-29 20:10:28 +00:00
2013-09-08 15:24:17 +00:00
case "MODE":
2016-05-05 07:11:40 +00:00
if s.clientRegistered == false {
s.reply(errNotReg)
return
}
if len(args) < 1 {
2016-05-05 07:11:40 +00:00
s.reply(errMoreArgs)
return
}
if strings.ToLower(args[0]) != BLESSED_CHANNEL {
2016-05-05 07:11:40 +00:00
s.reply(errNoSuchNick, args[0])
return
}
if len(args) == 1 {
2016-05-03 07:57:02 +00:00
// No more args, they just want the mode
2016-05-05 07:11:40 +00:00
s.reply(rplChannelModeIs, args[0], BLESSED_CHANNEL_MODE, "")
} else {
// Setting modes is disallowed
2016-05-05 07:11:40 +00:00
s.reply(errNoPriv)
}
return
2013-08-24 06:48:28 +00:00
default:
2016-05-05 07:11:40 +00:00
s.reply(errUnknownCommand, command)
}
}
func (s *Server) DisconnectClient() {
s.clientConn.Close()
s.clientConn = nil
}
func (s *Server) sendClientGlobalMessage(motd string) {
s.reply(rplMOTDStart)
for len(motd) > 80 {
s.reply(rplMOTD, motd[:80])
motd = motd[80:]
}
if len(motd) > 0 {
s.reply(rplMOTD, motd)
}
s.reply(rplEndOfMOTD)
}
// Send a reply to a user with the code specified
func (s *Server) reply(code replyCode, args ...string) {
switch code {
case rplWelcome:
s.writeClient(fmt.Sprintf(":%s 001 %s :Welcome to %s", s.name, s.clientNick, s.name))
case rplJoin:
s.writeClient(fmt.Sprintf(":%s JOIN %s", args[0], args[1]))
case rplPart:
s.writeClient(fmt.Sprintf(":%s PART %s %s", args[0], args[1], args[2]))
case rplTopic:
s.writeClient(fmt.Sprintf(":%s 332 %s %s :%s", s.name, s.clientNick, args[0], args[1]))
case rplNoTopic:
s.writeClient(fmt.Sprintf(":%s 331 %s %s :No topic is set", s.name, s.clientNick, args[0]))
case rplNames:
s.writeClient(fmt.Sprintf(":%s 353 %s = %s :%s", s.name, s.clientNick, args[0], args[1]))
case rplEndOfNames:
s.writeClient(fmt.Sprintf(":%s 366 %s %s :End of NAMES list", s.name, s.clientNick, args[0]))
case rplNickChange:
s.writeClient(fmt.Sprintf(":%s NICK %s", args[0], args[1]))
case rplKill:
s.writeClient(fmt.Sprintf(":%s KILL %s A %s", args[0], s.clientNick, args[1]))
case rplMsg:
for _, itm := range strings.Split(args[2], "\n") {
s.writeClient(fmt.Sprintf(":%s PRIVMSG %s %s", args[0], args[1], itm))
}
case rplList:
s.writeClient(fmt.Sprintf(":%s 322 %s %s", s.name, s.clientNick, args[0]))
case rplListEnd:
s.writeClient(fmt.Sprintf(":%s 323 %s", s.name, s.clientNick))
case rplOper:
s.writeClient(fmt.Sprintf(":%s 381 %s :You are now an operator", s.name, s.clientNick))
case rplChannelModeIs:
s.writeClient(fmt.Sprintf(":%s 324 %s %s %s %s", s.name, s.clientNick, args[0], args[1], args[2]))
case rplKick:
s.writeClient(fmt.Sprintf(":%s KICK %s %s %s", args[0], args[1], args[2], args[3]))
case rplInfo:
s.writeClient(fmt.Sprintf(":%s 371 %s :%s", s.name, s.clientNick, args[0]))
case rplVersion:
s.writeClient(fmt.Sprintf(":%s 351 %s %s", s.name, s.clientNick, args[0]))
case rplMOTDStart:
s.writeClient(fmt.Sprintf(":%s 375 %s :- Message of the day - ", s.name, s.clientNick))
case rplMOTD:
s.writeClient(fmt.Sprintf(":%s 372 %s :- %s", s.name, s.clientNick, args[0]))
case rplEndOfMOTD:
s.writeClient(fmt.Sprintf(":%s 376 %s :End of MOTD Command", s.name, s.clientNick))
case rplPong:
s.writeClient(fmt.Sprintf(":%s PONG %s %s", s.name, s.clientNick, s.name))
case errMoreArgs:
s.writeClient(fmt.Sprintf(":%s 461 %s :Not enough params", s.name, s.clientNick))
case errNoNick:
s.writeClient(fmt.Sprintf(":%s 431 %s :No nickname given", s.name, s.clientNick))
case errInvalidNick:
s.writeClient(fmt.Sprintf(":%s 432 %s %s :Erronenous nickname", s.name, s.clientNick, args[0]))
case errNickInUse:
s.writeClient(fmt.Sprintf(":%s 433 %s %s :Nick already in use", s.name, s.clientNick, args[0]))
case errAlreadyReg:
s.writeClient(fmt.Sprintf(":%s 462 :You need a valid nick first", s.name))
case errNoSuchNick:
s.writeClient(fmt.Sprintf(":%s 401 %s %s :No such nick/channel", s.name, s.clientNick, args[0]))
case errUnknownCommand:
s.writeClient(fmt.Sprintf(":%s 421 %s %s :Unknown command", s.name, s.clientNick, args[0]))
case errNotReg:
s.writeClient(fmt.Sprintf(":%s 451 :You have not registered", s.name))
case errPassword:
s.writeClient(fmt.Sprintf(":%s 464 %s :Error, password incorrect", s.name, s.clientNick))
case errNoPriv:
s.writeClient(fmt.Sprintf(":%s 481 %s :Permission denied", s.name, s.clientNick))
case errCannotSend:
s.writeClient(fmt.Sprintf(":%s 404 %s %s :Cannot send to channel", s.name, s.clientNick, args[0]))
}
}
func (s *Server) writeClient(output string) {
if s.clientConn == nil {
return
}
s.clientConn.SetWriteDeadline(time.Now().Add(time.Second * 30))
if _, err := fmt.Fprintf(s.clientConn, "%s\r\n", output); err != nil {
s.DisconnectClient()
return
2013-08-24 06:48:28 +00:00
}
}