2020-02-06 15:18:19 +00:00
|
|
|
package jounce
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net"
|
|
|
|
|
|
|
|
"gopkg.in/irc.v3"
|
|
|
|
)
|
|
|
|
|
|
|
|
type ircError struct {
|
|
|
|
Message *irc.Message
|
|
|
|
}
|
|
|
|
|
|
|
|
func newUnknownCommandError(cmd string) ircError {
|
|
|
|
return ircError{&irc.Message{
|
|
|
|
Command: irc.ERR_UNKNOWNCOMMAND,
|
|
|
|
Params: []string{
|
|
|
|
"*",
|
|
|
|
cmd,
|
|
|
|
"Unknown command",
|
|
|
|
},
|
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
|
|
|
func newNeedMoreParamsError(cmd string) ircError {
|
|
|
|
return ircError{&irc.Message{
|
|
|
|
Command: irc.ERR_NEEDMOREPARAMS,
|
|
|
|
Params: []string{
|
|
|
|
"*",
|
|
|
|
cmd,
|
|
|
|
"Not enough parameters",
|
|
|
|
},
|
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (err ircError) Error() string {
|
|
|
|
return err.Message.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
type downstreamConn struct {
|
2020-02-06 20:52:04 +00:00
|
|
|
net net.Conn
|
|
|
|
irc *irc.Conn
|
|
|
|
srv *Server
|
|
|
|
logger Logger
|
|
|
|
messages chan<- *irc.Message
|
2020-02-06 20:11:35 +00:00
|
|
|
|
2020-02-06 15:18:19 +00:00
|
|
|
registered bool
|
|
|
|
closed bool
|
|
|
|
nick string
|
|
|
|
username string
|
|
|
|
realname string
|
|
|
|
}
|
|
|
|
|
2020-02-06 20:11:35 +00:00
|
|
|
func newDownstreamConn(srv *Server, netConn net.Conn) *downstreamConn {
|
2020-02-06 20:52:04 +00:00
|
|
|
msgs := make(chan *irc.Message, 64)
|
|
|
|
conn := &downstreamConn{
|
|
|
|
net: netConn,
|
|
|
|
irc: irc.NewConn(netConn),
|
|
|
|
srv: srv,
|
|
|
|
logger: &prefixLogger{srv.Logger, fmt.Sprintf("downstream %q: ", netConn.RemoteAddr())},
|
|
|
|
messages: msgs,
|
2020-02-06 20:11:35 +00:00
|
|
|
}
|
2020-02-06 20:52:04 +00:00
|
|
|
|
|
|
|
go func() {
|
|
|
|
for msg := range msgs {
|
|
|
|
if err := conn.irc.WriteMessage(msg); err != nil {
|
|
|
|
conn.logger.Printf("failed to write message: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return conn
|
2020-02-06 20:11:35 +00:00
|
|
|
}
|
|
|
|
|
2020-02-06 21:19:31 +00:00
|
|
|
func (c *downstreamConn) prefix() *irc.Prefix {
|
|
|
|
return &irc.Prefix{
|
|
|
|
Name: c.nick,
|
|
|
|
User: c.username,
|
|
|
|
// TODO: fill the host?
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-06 20:11:35 +00:00
|
|
|
func (c *downstreamConn) readMessages() error {
|
|
|
|
c.logger.Printf("new connection")
|
|
|
|
defer c.Close()
|
|
|
|
|
|
|
|
for {
|
|
|
|
msg, err := c.irc.ReadMessage()
|
|
|
|
if err == io.EOF {
|
|
|
|
break
|
|
|
|
} else if err != nil {
|
|
|
|
return fmt.Errorf("failed to read IRC command: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = c.handleMessage(msg)
|
|
|
|
if ircErr, ok := err.(ircError); ok {
|
|
|
|
ircErr.Message.Prefix = c.srv.prefix()
|
2020-02-06 20:52:04 +00:00
|
|
|
c.messages <- ircErr.Message
|
2020-02-06 20:11:35 +00:00
|
|
|
} else if err != nil {
|
|
|
|
return fmt.Errorf("failed to handle IRC command %q: %v", msg.Command, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.closed {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.Close()
|
|
|
|
}
|
|
|
|
|
2020-02-06 15:18:19 +00:00
|
|
|
func (c *downstreamConn) Close() error {
|
2020-02-06 20:52:04 +00:00
|
|
|
if c.closed {
|
|
|
|
return fmt.Errorf("downstream connection already closed")
|
|
|
|
}
|
2020-02-06 15:18:19 +00:00
|
|
|
if err := c.net.Close(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-02-06 20:52:04 +00:00
|
|
|
close(c.messages)
|
2020-02-06 15:18:19 +00:00
|
|
|
c.closed = true
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *downstreamConn) handleMessage(msg *irc.Message) error {
|
|
|
|
switch msg.Command {
|
2020-02-06 21:22:14 +00:00
|
|
|
case "QUIT":
|
|
|
|
return c.Close()
|
2020-02-06 15:18:19 +00:00
|
|
|
case "PING":
|
|
|
|
// TODO: handle params
|
2020-02-06 21:35:24 +00:00
|
|
|
c.messages <- &irc.Message{
|
|
|
|
Prefix: c.srv.prefix(),
|
2020-02-06 15:18:19 +00:00
|
|
|
Command: "PONG",
|
|
|
|
Params: []string{c.srv.Hostname},
|
2020-02-06 21:35:24 +00:00
|
|
|
}
|
2020-02-06 20:52:04 +00:00
|
|
|
return nil
|
2020-02-06 15:18:19 +00:00
|
|
|
default:
|
|
|
|
if c.registered {
|
|
|
|
return c.handleMessageRegistered(msg)
|
|
|
|
} else {
|
|
|
|
return c.handleMessageUnregistered(msg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *downstreamConn) handleMessageUnregistered(msg *irc.Message) error {
|
|
|
|
switch msg.Command {
|
|
|
|
case "NICK":
|
|
|
|
if len(msg.Params) != 1 {
|
|
|
|
return newNeedMoreParamsError(msg.Command)
|
|
|
|
}
|
|
|
|
c.nick = msg.Params[0]
|
|
|
|
case "USER":
|
|
|
|
if len(msg.Params) != 4 {
|
|
|
|
return newNeedMoreParamsError(msg.Command)
|
|
|
|
}
|
|
|
|
c.username = "~" + msg.Params[0]
|
|
|
|
c.realname = msg.Params[3]
|
|
|
|
default:
|
2020-02-06 20:11:35 +00:00
|
|
|
c.logger.Printf("unhandled message: %v", msg)
|
2020-02-06 15:18:19 +00:00
|
|
|
return newUnknownCommandError(msg.Command)
|
|
|
|
}
|
|
|
|
if c.username != "" && c.nick != "" {
|
|
|
|
return c.register()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *downstreamConn) register() error {
|
|
|
|
c.registered = true
|
|
|
|
|
2020-02-06 21:35:24 +00:00
|
|
|
c.messages <- &irc.Message{
|
|
|
|
Prefix: c.srv.prefix(),
|
2020-02-06 15:18:19 +00:00
|
|
|
Command: irc.RPL_WELCOME,
|
|
|
|
Params: []string{c.nick, "Welcome to jounce, " + c.nick},
|
2020-02-06 21:35:24 +00:00
|
|
|
}
|
2020-02-06 15:18:19 +00:00
|
|
|
|
2020-02-06 21:35:24 +00:00
|
|
|
c.messages <- &irc.Message{
|
|
|
|
Prefix: c.srv.prefix(),
|
2020-02-06 15:18:19 +00:00
|
|
|
Command: irc.RPL_YOURHOST,
|
|
|
|
Params: []string{c.nick, "Your host is " + c.srv.Hostname},
|
2020-02-06 21:35:24 +00:00
|
|
|
}
|
2020-02-06 15:18:19 +00:00
|
|
|
|
2020-02-06 21:35:24 +00:00
|
|
|
c.messages <- &irc.Message{
|
|
|
|
Prefix: c.srv.prefix(),
|
2020-02-06 15:18:19 +00:00
|
|
|
Command: irc.RPL_CREATED,
|
|
|
|
Params: []string{c.nick, "This server was created <datetime>"}, // TODO
|
2020-02-06 21:35:24 +00:00
|
|
|
}
|
2020-02-06 15:18:19 +00:00
|
|
|
|
2020-02-06 21:35:24 +00:00
|
|
|
c.messages <- &irc.Message{
|
|
|
|
Prefix: c.srv.prefix(),
|
2020-02-06 15:18:19 +00:00
|
|
|
Command: irc.RPL_MYINFO,
|
2020-02-06 21:22:14 +00:00
|
|
|
Params: []string{c.nick, c.srv.Hostname, "jounce", "aiwroO", "OovaimnqpsrtklbeI"},
|
2020-02-06 21:35:24 +00:00
|
|
|
}
|
2020-02-06 15:18:19 +00:00
|
|
|
|
2020-02-06 21:35:24 +00:00
|
|
|
c.messages <- &irc.Message{
|
|
|
|
Prefix: c.srv.prefix(),
|
2020-02-06 15:18:19 +00:00
|
|
|
Command: irc.ERR_NOMOTD,
|
|
|
|
Params: []string{c.nick, "No MOTD"},
|
2020-02-06 21:35:24 +00:00
|
|
|
}
|
2020-02-06 15:18:19 +00:00
|
|
|
|
2020-02-06 21:29:24 +00:00
|
|
|
c.srv.lock.Lock()
|
|
|
|
for _, uc := range c.srv.upstreamConns {
|
|
|
|
// TODO: fix races accessing upstream connection data
|
|
|
|
if !uc.registered {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
for _, ch := range uc.channels {
|
|
|
|
if ch.complete {
|
|
|
|
forwardChannel(c, ch)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
c.srv.lock.Unlock()
|
|
|
|
|
2020-02-06 15:18:19 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *downstreamConn) handleMessageRegistered(msg *irc.Message) error {
|
|
|
|
switch msg.Command {
|
|
|
|
case "NICK", "USER":
|
|
|
|
return ircError{&irc.Message{
|
|
|
|
Command: irc.ERR_ALREADYREGISTERED,
|
|
|
|
Params: []string{
|
|
|
|
c.nick,
|
|
|
|
"You may not reregister",
|
|
|
|
},
|
|
|
|
}}
|
|
|
|
default:
|
2020-02-06 20:11:35 +00:00
|
|
|
c.logger.Printf("unhandled message: %v", msg)
|
2020-02-06 15:18:19 +00:00
|
|
|
return newUnknownCommandError(msg.Command)
|
|
|
|
}
|
|
|
|
}
|