soju/upstream.go

352 lines
7.8 KiB
Go
Raw Normal View History

package jounce
import (
"crypto/tls"
"fmt"
"io"
"net"
2020-02-06 18:22:04 +00:00
"strconv"
"strings"
2020-02-06 18:22:04 +00:00
"time"
"gopkg.in/irc.v3"
)
2020-02-06 18:22:04 +00:00
type upstreamChannel struct {
Name string
2020-02-07 12:08:27 +00:00
conn *upstreamConn
2020-02-06 18:22:04 +00:00
Topic string
TopicWho string
TopicTime time.Time
Status channelStatus
2020-02-07 10:01:53 +00:00
modes modeSet
2020-02-06 18:22:04 +00:00
Members map[string]membership
complete bool
2020-02-06 18:22:04 +00:00
}
type upstreamConn struct {
2020-02-06 18:22:04 +00:00
upstream *Upstream
2020-02-06 19:25:37 +00:00
logger Logger
2020-02-06 18:22:04 +00:00
net net.Conn
irc *irc.Conn
srv *Server
2020-02-07 10:36:42 +00:00
user *user
messages chan<- *irc.Message
2020-02-06 16:04:49 +00:00
serverName string
availableUserModes string
availableChannelModes string
channelModesWithParam string
2020-02-06 18:22:04 +00:00
registered bool
2020-02-07 11:19:42 +00:00
nick string
closed bool
2020-02-06 18:22:04 +00:00
modes modeSet
channels map[string]*upstreamChannel
}
2020-02-07 10:36:42 +00:00
func connectToUpstream(u *user, upstream *Upstream) (*upstreamConn, error) {
logger := &prefixLogger{u.srv.Logger, fmt.Sprintf("upstream %q: ", upstream.Addr)}
logger.Printf("connecting to server")
netConn, err := tls.Dial("tcp", upstream.Addr, nil)
if err != nil {
return nil, fmt.Errorf("failed to dial %q: %v", upstream.Addr, err)
}
msgs := make(chan *irc.Message, 64)
conn := &upstreamConn{
upstream: upstream,
logger: logger,
net: netConn,
irc: irc.NewConn(netConn),
2020-02-07 10:36:42 +00:00
srv: u.srv,
user: u,
messages: msgs,
channels: make(map[string]*upstreamChannel),
}
go func() {
for msg := range msgs {
if err := conn.irc.WriteMessage(msg); err != nil {
conn.logger.Printf("failed to write message: %v", err)
}
}
if err := conn.net.Close(); err != nil {
conn.logger.Printf("failed to close connection: %v", err)
} else {
conn.logger.Printf("connection closed")
}
}()
return conn, nil
}
func (c *upstreamConn) Close() error {
if c.closed {
return fmt.Errorf("upstream connection already closed")
}
close(c.messages)
c.closed = true
return nil
}
2020-02-06 18:22:04 +00:00
func (c *upstreamConn) getChannel(name string) (*upstreamChannel, error) {
ch, ok := c.channels[name]
if !ok {
return nil, fmt.Errorf("unknown channel %q", name)
}
return ch, nil
}
func (c *upstreamConn) handleMessage(msg *irc.Message) error {
switch msg.Command {
case "PING":
// TODO: handle params
c.messages <- &irc.Message{
Command: "PONG",
Params: []string{c.srv.Hostname},
}
return nil
case "MODE":
2020-02-07 11:36:02 +00:00
var name, modeStr string
if err := parseMessageParams(msg, &name, &modeStr); err != nil {
return err
}
2020-02-07 10:01:53 +00:00
if name == msg.Prefix.Name { // user mode change
2020-02-07 11:19:42 +00:00
if name != c.nick {
2020-02-07 10:01:53 +00:00
return fmt.Errorf("received MODE message for unknow nick %q", name)
}
return c.modes.Apply(modeStr)
} else { // channel mode change
ch, err := c.getChannel(name)
if err != nil {
return err
}
if err := ch.modes.Apply(modeStr); err != nil {
return err
}
}
2020-02-07 12:08:27 +00:00
c.user.forEachDownstream(func(dc *downstreamConn) {
dc.messages <- msg
})
2020-02-06 17:10:13 +00:00
case "NOTICE":
2020-02-06 19:25:37 +00:00
c.logger.Print(msg)
2020-02-06 15:39:09 +00:00
case irc.RPL_WELCOME:
c.registered = true
2020-02-06 19:25:37 +00:00
c.logger.Printf("connection registered")
2020-02-06 18:22:04 +00:00
for _, ch := range c.upstream.Channels {
c.messages <- &irc.Message{
2020-02-06 18:22:04 +00:00
Command: "JOIN",
Params: []string{ch},
}
}
2020-02-06 16:04:49 +00:00
case irc.RPL_MYINFO:
2020-02-07 11:36:02 +00:00
if err := parseMessageParams(msg, nil, &c.serverName, nil, &c.availableUserModes, &c.availableChannelModes); err != nil {
return err
2020-02-06 16:04:49 +00:00
}
if len(msg.Params) > 5 {
c.channelModesWithParam = msg.Params[5]
}
2020-02-07 11:19:42 +00:00
case "NICK":
2020-02-07 11:36:02 +00:00
var newNick string
if err := parseMessageParams(msg, &newNick); err != nil {
return err
2020-02-07 11:19:42 +00:00
}
if msg.Prefix.Name == c.nick {
c.logger.Printf("changed nick from %q to %q", c.nick, newNick)
c.nick = newNick
}
for _, ch := range c.channels {
if membership, ok := ch.Members[msg.Prefix.Name]; ok {
delete(ch.Members, msg.Prefix.Name)
ch.Members[newNick] = membership
}
}
c.user.forEachDownstream(func(dc *downstreamConn) {
dc.messages <- msg
})
2020-02-06 18:22:04 +00:00
case "JOIN":
2020-02-07 11:36:02 +00:00
var channels string
if err := parseMessageParams(msg, &channels); err != nil {
return err
2020-02-06 18:22:04 +00:00
}
2020-02-07 11:36:02 +00:00
for _, ch := range strings.Split(channels, ",") {
2020-02-07 11:19:42 +00:00
if msg.Prefix.Name == c.nick {
c.logger.Printf("joined channel %q", ch)
c.channels[ch] = &upstreamChannel{
Name: ch,
2020-02-07 12:08:27 +00:00
conn: c,
Members: make(map[string]membership),
}
} else {
ch, err := c.getChannel(ch)
if err != nil {
return err
}
ch.Members[msg.Prefix.Name] = 0
2020-02-06 18:22:04 +00:00
}
}
2020-02-07 10:56:36 +00:00
c.user.forEachDownstream(func(dc *downstreamConn) {
dc.messages <- msg
2020-02-07 10:56:36 +00:00
})
case "PART":
2020-02-07 11:36:02 +00:00
var channels string
if err := parseMessageParams(msg, &channels); err != nil {
return err
}
2020-02-07 11:36:02 +00:00
for _, ch := range strings.Split(channels, ",") {
2020-02-07 11:19:42 +00:00
if msg.Prefix.Name == c.nick {
c.logger.Printf("parted channel %q", ch)
delete(c.channels, ch)
} else {
ch, err := c.getChannel(ch)
if err != nil {
return err
}
delete(ch.Members, msg.Prefix.Name)
}
}
2020-02-07 10:56:36 +00:00
c.user.forEachDownstream(func(dc *downstreamConn) {
dc.messages <- msg
2020-02-07 10:56:36 +00:00
})
2020-02-06 18:22:04 +00:00
case irc.RPL_TOPIC, irc.RPL_NOTOPIC:
2020-02-07 11:36:02 +00:00
var name, topic string
if err := parseMessageParams(msg, nil, &name, &topic); err != nil {
return err
2020-02-06 18:22:04 +00:00
}
2020-02-07 11:36:02 +00:00
ch, err := c.getChannel(name)
2020-02-06 18:22:04 +00:00
if err != nil {
return err
}
if msg.Command == irc.RPL_TOPIC {
2020-02-07 11:36:02 +00:00
ch.Topic = topic
2020-02-06 18:22:04 +00:00
} else {
ch.Topic = ""
}
case "TOPIC":
2020-02-07 11:36:02 +00:00
var name string
if err := parseMessageParams(msg, nil, &name); err != nil {
return err
2020-02-06 18:22:04 +00:00
}
2020-02-07 11:36:02 +00:00
ch, err := c.getChannel(name)
2020-02-06 18:22:04 +00:00
if err != nil {
return err
}
if len(msg.Params) > 1 {
ch.Topic = msg.Params[1]
} else {
ch.Topic = ""
}
case rpl_topicwhotime:
2020-02-07 11:36:02 +00:00
var name, who, timeStr string
if err := parseMessageParams(msg, nil, &name, &who, &timeStr); err != nil {
return err
2020-02-06 18:22:04 +00:00
}
2020-02-07 11:36:02 +00:00
ch, err := c.getChannel(name)
2020-02-06 18:22:04 +00:00
if err != nil {
return err
}
2020-02-07 11:36:02 +00:00
ch.TopicWho = who
sec, err := strconv.ParseInt(timeStr, 10, 64)
2020-02-06 18:22:04 +00:00
if err != nil {
return fmt.Errorf("failed to parse topic time: %v", err)
}
ch.TopicTime = time.Unix(sec, 0)
case irc.RPL_NAMREPLY:
2020-02-07 11:36:02 +00:00
var name, statusStr, members string
if err := parseMessageParams(msg, nil, &statusStr, &name, &members); err != nil {
return err
2020-02-06 18:22:04 +00:00
}
2020-02-07 11:36:02 +00:00
ch, err := c.getChannel(name)
2020-02-06 18:22:04 +00:00
if err != nil {
return err
}
2020-02-07 11:36:02 +00:00
status, err := parseChannelStatus(statusStr)
2020-02-06 18:22:04 +00:00
if err != nil {
return err
}
ch.Status = status
2020-02-07 11:36:02 +00:00
for _, s := range strings.Split(members, " ") {
2020-02-06 18:22:04 +00:00
membership, nick := parseMembershipPrefix(s)
ch.Members[nick] = membership
}
case irc.RPL_ENDOFNAMES:
2020-02-07 11:36:02 +00:00
var name string
if err := parseMessageParams(msg, nil, &name); err != nil {
return err
}
2020-02-07 11:36:02 +00:00
ch, err := c.getChannel(name)
if err != nil {
return err
}
if ch.complete {
return fmt.Errorf("received unexpected RPL_ENDOFNAMES")
}
ch.complete = true
2020-02-06 21:19:31 +00:00
2020-02-07 10:56:36 +00:00
c.user.forEachDownstream(func(dc *downstreamConn) {
2020-02-06 21:19:31 +00:00
forwardChannel(dc, ch)
2020-02-07 10:56:36 +00:00
})
2020-02-07 10:07:01 +00:00
case "PRIVMSG":
2020-02-07 10:56:36 +00:00
c.user.forEachDownstream(func(dc *downstreamConn) {
2020-02-07 10:07:01 +00:00
dc.messages <- msg
2020-02-07 10:56:36 +00:00
})
2020-02-06 16:04:49 +00:00
case irc.RPL_YOURHOST, irc.RPL_CREATED:
2020-02-06 15:39:09 +00:00
// Ignore
case irc.RPL_LUSERCLIENT, irc.RPL_LUSEROP, irc.RPL_LUSERUNKNOWN, irc.RPL_LUSERCHANNELS, irc.RPL_LUSERME:
// Ignore
case irc.RPL_MOTDSTART, irc.RPL_MOTD, irc.RPL_ENDOFMOTD:
// Ignore
case rpl_localusers, rpl_globalusers:
// Ignore
case irc.RPL_STATSVLINE, irc.RPL_STATSPING, irc.RPL_STATSBLINE, irc.RPL_STATSDLINE:
// Ignore
default:
2020-02-06 19:25:37 +00:00
c.logger.Printf("unhandled upstream message: %v", msg)
}
2020-02-06 15:39:09 +00:00
return nil
}
2020-02-07 11:37:44 +00:00
func (c *upstreamConn) register() {
2020-02-07 11:19:42 +00:00
c.nick = c.upstream.Nick
c.messages <- &irc.Message{
Command: "NICK",
Params: []string{c.upstream.Nick},
}
c.messages <- &irc.Message{
Command: "USER",
Params: []string{c.upstream.Username, "0", "*", c.upstream.Realname},
}
2020-02-07 11:37:44 +00:00
}
func (c *upstreamConn) readMessages() error {
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)
}
if err := c.handleMessage(msg); err != nil {
2020-02-06 19:25:37 +00:00
c.logger.Printf("failed to handle message %q: %v", msg, err)
}
}
return nil
}