6358ebe50a
--HG-- branch : nmdc-ircfrontend
464 lines
11 KiB
Go
464 lines
11 KiB
Go
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"fmt"
|
|
"io"
|
|
"libnmdc"
|
|
"net"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
type Server struct {
|
|
//eventChan chan Event
|
|
running bool
|
|
name string
|
|
|
|
clientConn net.Conn
|
|
clientNick string
|
|
clientRegistered bool
|
|
clientOperator bool
|
|
|
|
upstreamLauncher libnmdc.HubConnectionOptions
|
|
upstreamCloser chan struct{}
|
|
upstream *libnmdc.HubConnection
|
|
motd string
|
|
}
|
|
|
|
func NewServer(name string, upstream libnmdc.HubAddress, conn net.Conn) *Server {
|
|
self := libnmdc.NewUserInfo("")
|
|
self.ClientTag = APP_DESCRIPTION
|
|
|
|
return &Server{
|
|
name: name,
|
|
clientConn: conn,
|
|
motd: "Connected to " + name,
|
|
upstreamLauncher: libnmdc.HubConnectionOptions{
|
|
Address: upstream,
|
|
Self: *self,
|
|
},
|
|
upstreamCloser: make(chan struct{}, 1),
|
|
}
|
|
}
|
|
|
|
func (s *Server) RunWorker() {
|
|
|
|
// Send the connection handshake
|
|
s.sendClientGlobalMessage(s.motd)
|
|
|
|
// Can't connect to the upstream server yet, until we've recieved a nick.
|
|
|
|
for {
|
|
buf := make([]byte, CLIENT_READ_BUFFSIZE)
|
|
s.clientConn.SetReadDeadline(time.Now().Add(time.Second * CLIENT_READ_TIMEOUT_SEC))
|
|
ln, err := s.clientConn.Read(buf)
|
|
if err != nil {
|
|
if err == io.EOF {
|
|
s.DisconnectClient()
|
|
if s.clientConn != nil && s.clientRegistered {
|
|
s.upstreamCloser <- struct{}{}
|
|
}
|
|
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 {
|
|
|
|
// 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:])
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
func (s *Server) upstreamWorker() {
|
|
|
|
// Initiate connection
|
|
s.upstream = s.upstreamLauncher.Connect()
|
|
|
|
// Read loop
|
|
for {
|
|
select {
|
|
case <-s.upstreamCloser:
|
|
// Abandon the upstream connection
|
|
s.upstream.Disconnect()
|
|
return
|
|
|
|
case hubEvent := <-s.upstream.OnEvent:
|
|
switch hubEvent.EventType {
|
|
case libnmdc.EVENT_USER_JOINED:
|
|
s.reply(rplJoin, hubEvent.Nick, BLESSED_CHANNEL)
|
|
|
|
case libnmdc.EVENT_USER_PART:
|
|
s.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.sendClientGlobalMessage("Upstream: " + hubEvent.StateChange.Format())
|
|
|
|
case libnmdc.EVENT_HUBNAME_CHANGED:
|
|
s.reply(rplTopic, BLESSED_CHANNEL, hubEvent.Nick)
|
|
// c.reply(rplNoTopic, BLESSED_CHANNEL)
|
|
|
|
case libnmdc.EVENT_PRIVATE:
|
|
s.reply(rplMsg, s.clientNick, hubEvent.Nick, hubEvent.Message)
|
|
|
|
case libnmdc.EVENT_PUBLIC:
|
|
s.reply(rplMsg, s.clientNick, BLESSED_CHANNEL, hubEvent.Message)
|
|
|
|
case libnmdc.EVENT_SYSTEM_MESSAGE_FROM_CONN, libnmdc.EVENT_SYSTEM_MESSAGE_FROM_HUB:
|
|
s.sendClientGlobalMessage(hubEvent.Message)
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func (s *Server) handleCommand(command string, args []string) {
|
|
|
|
switch command {
|
|
case "PING":
|
|
s.reply(rplPong)
|
|
|
|
case "INFO":
|
|
s.reply(rplInfo, APP_DESCRIPTION)
|
|
|
|
case "VERSION":
|
|
s.reply(rplVersion, VERSION)
|
|
|
|
case "PASS":
|
|
// RFC2812 registration
|
|
// Stash the password for later
|
|
if len(args) < 1 {
|
|
s.reply(errMoreArgs)
|
|
return
|
|
}
|
|
s.upstreamLauncher.NickPassword = args[0]
|
|
|
|
case "NICK":
|
|
if len(args) < 1 {
|
|
s.reply(errMoreArgs)
|
|
return
|
|
}
|
|
|
|
if s.upstreamLauncher.Self.Nick == "" {
|
|
// allow set, as part of the login phase
|
|
s.upstreamLauncher.Self.Nick = args[0]
|
|
} else {
|
|
s.reply(rplKill, "Can't change nicks on this server", "")
|
|
s.DisconnectClient()
|
|
}
|
|
|
|
case "USER":
|
|
// This command sets altname, realname, ... none of which we use
|
|
// It's the final step in a PASS/NICK/USER login handshake.
|
|
|
|
if s.clientRegistered == true {
|
|
s.reply(rplKill, "You're already registered.", "")
|
|
s.DisconnectClient()
|
|
return
|
|
}
|
|
|
|
if s.clientNick == "" {
|
|
s.reply(rplKill, "Your nickname is already being used", "")
|
|
s.DisconnectClient()
|
|
return
|
|
}
|
|
|
|
s.reply(rplWelcome)
|
|
s.clientRegistered = true
|
|
|
|
// Spawn upstream connection
|
|
go s.upstreamWorker() // FIXME need shutdown synchronisation
|
|
|
|
// Tell the user that they themselves joined the chat channel
|
|
s.reply(rplJoin, s.clientNick, BLESSED_CHANNEL)
|
|
|
|
case "JOIN":
|
|
if s.clientRegistered == false {
|
|
s.reply(errNotReg)
|
|
return
|
|
}
|
|
|
|
if len(args) < 1 {
|
|
s.reply(errMoreArgs)
|
|
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:
|
|
s.reply(rplKill, "There is only '"+BLESSED_CHANNEL+"'.", "")
|
|
s.DisconnectClient()
|
|
}
|
|
|
|
case "PART":
|
|
if s.clientRegistered == false {
|
|
s.reply(errNotReg)
|
|
return
|
|
}
|
|
|
|
// you can check out any time you like, but you can never leave
|
|
// we'll need to transmit s.reply(rplPart, s.clientNick, channel.name, reason) messages on behalf of other nmdc users, though
|
|
|
|
case "PRIVMSG":
|
|
if s.clientRegistered == false {
|
|
s.reply(errNotReg)
|
|
return
|
|
}
|
|
|
|
if len(args) < 2 {
|
|
s.reply(errMoreArgs)
|
|
return
|
|
}
|
|
|
|
message := strings.Join(args[1:], " ")
|
|
|
|
// IRC is case-insensitive case-preserving. We can respect that for the
|
|
// channel name, but not really for user nicks
|
|
|
|
if strings.ToLower(args[0]) == BLESSED_CHANNEL {
|
|
s.upstream.SayPublic(message)
|
|
|
|
} else if _, clientExists := s.upstream.Users[args[0]]; clientExists {
|
|
s.upstream.SayPrivate(args[0], message)
|
|
|
|
} else {
|
|
s.reply(errNoSuchNick, args[0])
|
|
|
|
}
|
|
|
|
case "QUIT":
|
|
if s.clientRegistered == false {
|
|
s.reply(errNotReg)
|
|
return
|
|
}
|
|
|
|
s.DisconnectClient()
|
|
|
|
case "TOPIC":
|
|
if s.clientRegistered == false {
|
|
s.reply(errNotReg)
|
|
return
|
|
}
|
|
|
|
if len(args) < 1 {
|
|
s.reply(errMoreArgs)
|
|
return
|
|
}
|
|
|
|
exists := strings.ToLower(args[0]) == BLESSED_CHANNEL
|
|
if exists == false {
|
|
s.reply(errNoSuchNick, args[0])
|
|
return
|
|
}
|
|
|
|
// Valid topic get
|
|
if len(args) == 1 {
|
|
s.reply(rplTopic, BLESSED_CHANNEL, s.upstream.HubName)
|
|
return
|
|
}
|
|
|
|
// Disallow topic set
|
|
s.reply(errNoPriv)
|
|
return
|
|
|
|
case "LIST":
|
|
if s.clientRegistered == false {
|
|
s.reply(errNotReg)
|
|
return
|
|
}
|
|
|
|
listItem := fmt.Sprintf("%s %d :%s", BLESSED_CHANNEL, len(s.upstream.Users), s.upstream.HubName)
|
|
s.reply(rplList, listItem)
|
|
s.reply(rplListEnd)
|
|
|
|
case "OPER":
|
|
if s.clientRegistered == false {
|
|
s.reply(errNotReg)
|
|
return
|
|
}
|
|
|
|
if len(args) < 2 {
|
|
s.reply(errMoreArgs)
|
|
return
|
|
}
|
|
|
|
// Can't use this command.
|
|
s.reply(errPassword)
|
|
|
|
case "KILL":
|
|
if s.clientRegistered == false {
|
|
s.reply(errNotReg)
|
|
return
|
|
}
|
|
|
|
s.reply(errNoPriv)
|
|
return
|
|
|
|
case "KICK":
|
|
if s.clientRegistered == false {
|
|
s.reply(errNotReg)
|
|
return
|
|
}
|
|
|
|
s.reply(errNoPriv)
|
|
return
|
|
|
|
case "MODE":
|
|
if s.clientRegistered == false {
|
|
s.reply(errNotReg)
|
|
return
|
|
}
|
|
|
|
if len(args) < 1 {
|
|
s.reply(errMoreArgs)
|
|
return
|
|
}
|
|
|
|
if strings.ToLower(args[0]) != BLESSED_CHANNEL {
|
|
s.reply(errNoSuchNick, args[0])
|
|
return
|
|
}
|
|
|
|
if len(args) == 1 {
|
|
// No more args, they just want the mode
|
|
s.reply(rplChannelModeIs, args[0], BLESSED_CHANNEL_MODE, "")
|
|
} else {
|
|
// Setting modes is disallowed
|
|
s.reply(errNoPriv)
|
|
}
|
|
|
|
return
|
|
|
|
default:
|
|
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
|
|
}
|
|
}
|