2020-02-06 15:18:19 +00:00
|
|
|
package jounce
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net"
|
2020-02-07 10:46:44 +00:00
|
|
|
"strings"
|
2020-02-06 15:18:19 +00:00
|
|
|
|
|
|
|
"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
|
2020-02-07 10:36:42 +00:00
|
|
|
user *user
|
2020-02-06 15:18:19 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
2020-02-07 11:42:24 +00:00
|
|
|
if err := conn.net.Close(); err != nil {
|
|
|
|
conn.logger.Printf("failed to close connection: %v", err)
|
|
|
|
} else {
|
|
|
|
conn.logger.Printf("connection closed")
|
|
|
|
}
|
2020-02-06 20:52:04 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
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")
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-07 11:42:24 +00:00
|
|
|
return nil
|
2020-02-06 20:11:35 +00:00
|
|
|
}
|
|
|
|
|
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-07 10:56:36 +00:00
|
|
|
|
|
|
|
if u := c.user; u != nil {
|
|
|
|
u.lock.Lock()
|
|
|
|
for i := range u.downstreamConns {
|
|
|
|
if u.downstreamConns[i] == c {
|
|
|
|
u.downstreamConns = append(u.downstreamConns[:i], u.downstreamConns[i+1:]...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
u.lock.Unlock()
|
2020-02-06 15:18:19 +00:00
|
|
|
}
|
2020-02-07 10:56:36 +00:00
|
|
|
|
2020-02-06 20:52:04 +00:00
|
|
|
close(c.messages)
|
2020-02-06 15:18:19 +00:00
|
|
|
c.closed = true
|
2020-02-07 10:56:36 +00:00
|
|
|
|
2020-02-07 11:42:24 +00:00
|
|
|
return nil
|
2020-02-06 15:18:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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":
|
2020-02-07 11:36:02 +00:00
|
|
|
if err := parseMessageParams(msg, &c.nick); err != nil {
|
|
|
|
return err
|
2020-02-06 15:18:19 +00:00
|
|
|
}
|
|
|
|
case "USER":
|
2020-02-07 11:36:02 +00:00
|
|
|
var username string
|
|
|
|
if err := parseMessageParams(msg, &username, nil, nil, &c.realname); err != nil {
|
|
|
|
return err
|
2020-02-06 15:18:19 +00:00
|
|
|
}
|
2020-02-07 11:36:02 +00:00
|
|
|
c.username = "~" + username
|
2020-02-06 15:18:19 +00:00
|
|
|
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 {
|
2020-02-07 10:46:44 +00:00
|
|
|
u := c.srv.getUser(strings.TrimPrefix(c.username, "~"))
|
2020-02-07 10:39:56 +00:00
|
|
|
if u == nil {
|
2020-02-07 10:46:44 +00:00
|
|
|
c.logger.Printf("failed authentication: unknown username %q", c.username)
|
2020-02-07 10:36:42 +00:00
|
|
|
c.messages <- &irc.Message{
|
|
|
|
Prefix: c.srv.prefix(),
|
|
|
|
Command: irc.ERR_PASSWDMISMATCH,
|
|
|
|
Params: []string{"*", "Invalid username or password"},
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-02-06 15:18:19 +00:00
|
|
|
c.registered = true
|
2020-02-07 10:36:42 +00:00
|
|
|
c.user = u
|
2020-02-06 15:18:19 +00:00
|
|
|
|
2020-02-07 10:56:36 +00:00
|
|
|
u.lock.Lock()
|
|
|
|
u.downstreamConns = append(u.downstreamConns, c)
|
|
|
|
u.lock.Unlock()
|
|
|
|
|
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
|
|
|
}
|
|
|
|
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
|
|
|
}
|
|
|
|
c.messages <- &irc.Message{
|
|
|
|
Prefix: c.srv.prefix(),
|
2020-02-06 15:18:19 +00:00
|
|
|
Command: irc.RPL_CREATED,
|
2020-02-07 12:23:09 +00:00
|
|
|
Params: []string{c.nick, "Who cares when the server was created?"},
|
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
|
|
|
}
|
|
|
|
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-07 10:46:44 +00:00
|
|
|
u.forEachUpstream(func(uc *upstreamConn) {
|
2020-02-06 21:29:24 +00:00
|
|
|
// TODO: fix races accessing upstream connection data
|
|
|
|
for _, ch := range uc.channels {
|
|
|
|
if ch.complete {
|
|
|
|
forwardChannel(c, ch)
|
|
|
|
}
|
|
|
|
}
|
2020-02-07 10:46:44 +00:00
|
|
|
})
|
2020-02-06 15:18:19 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *downstreamConn) handleMessageRegistered(msg *irc.Message) error {
|
|
|
|
switch msg.Command {
|
2020-02-07 11:19:42 +00:00
|
|
|
case "USER":
|
2020-02-06 15:18:19 +00:00
|
|
|
return ircError{&irc.Message{
|
|
|
|
Command: irc.ERR_ALREADYREGISTERED,
|
2020-02-07 11:19:42 +00:00
|
|
|
Params: []string{c.nick, "You may not reregister"},
|
2020-02-06 15:18:19 +00:00
|
|
|
}}
|
2020-02-07 11:19:42 +00:00
|
|
|
case "NICK":
|
|
|
|
c.user.forEachUpstream(func(uc *upstreamConn) {
|
|
|
|
uc.messages <- msg
|
|
|
|
})
|
2020-02-07 12:31:09 +00:00
|
|
|
case "JOIN":
|
|
|
|
var name string
|
|
|
|
if err := parseMessageParams(msg, &name); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if ch, _ := c.user.getChannel(name); ch != nil {
|
|
|
|
break // already joined
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: extract network name from channel name
|
|
|
|
return ircError{&irc.Message{
|
|
|
|
Command: irc.ERR_NOSUCHCHANNEL,
|
|
|
|
Params: []string{name, "Channel name ambiguous"},
|
|
|
|
}}
|
2020-02-07 12:08:27 +00:00
|
|
|
case "MODE":
|
|
|
|
var name string
|
|
|
|
if err := parseMessageParams(msg, &name); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var modeStr string
|
|
|
|
if len(msg.Params) > 1 {
|
|
|
|
modeStr = msg.Params[1]
|
|
|
|
}
|
|
|
|
|
|
|
|
if msg.Prefix.Name != name {
|
|
|
|
ch, err := c.user.getChannel(name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if modeStr != "" {
|
|
|
|
ch.conn.messages <- msg
|
|
|
|
} else {
|
|
|
|
c.messages <- &irc.Message{
|
|
|
|
Prefix: c.srv.prefix(),
|
|
|
|
Command: irc.RPL_CHANNELMODEIS,
|
|
|
|
Params: []string{ch.Name, string(ch.modes)},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if name != c.nick {
|
|
|
|
return ircError{&irc.Message{
|
|
|
|
Command: irc.ERR_USERSDONTMATCH,
|
|
|
|
Params: []string{c.nick, "Cannot change mode for other users"},
|
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
|
|
|
if modeStr != "" {
|
|
|
|
c.user.forEachUpstream(func(uc *upstreamConn) {
|
|
|
|
uc.messages <- msg
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
c.messages <- &irc.Message{
|
|
|
|
Prefix: c.srv.prefix(),
|
|
|
|
Command: irc.RPL_UMODEIS,
|
|
|
|
Params: []string{""}, // TODO
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-02-06 15:18:19 +00:00
|
|
|
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)
|
|
|
|
}
|
2020-02-07 11:19:42 +00:00
|
|
|
return nil
|
2020-02-06 15:18:19 +00:00
|
|
|
}
|