Don't write to downstreamConn.messages directly

Use a helper function instead. This will allow us to change
downstreamConn implementation details without having to update the whole
codebase.
This commit is contained in:
Simon Ser 2020-02-17 12:27:48 +01:00
parent 86f9221ddc
commit 0cf731aeab
No known key found for this signature in database
GPG Key ID: 0FDE7BE0E88F5E48
3 changed files with 39 additions and 35 deletions

View File

@ -9,24 +9,24 @@ func forwardChannel(dc *downstreamConn, ch *upstreamChannel) {
panic("Tried to forward a partial channel")
}
dc.messages <- &irc.Message{
dc.SendMessage(&irc.Message{
Prefix: dc.prefix(),
Command: "JOIN",
Params: []string{ch.Name},
}
})
if ch.Topic != "" {
dc.messages <- &irc.Message{
dc.SendMessage(&irc.Message{
Prefix: dc.srv.prefix(),
Command: irc.RPL_TOPIC,
Params: []string{dc.nick, ch.Name, ch.Topic},
}
})
} else {
dc.messages <- &irc.Message{
dc.SendMessage(&irc.Message{
Prefix: dc.srv.prefix(),
Command: irc.RPL_NOTOPIC,
Params: []string{dc.nick, ch.Name, "No topic is set"},
}
})
}
// TODO: rpl_topicwhotime
@ -38,16 +38,16 @@ func forwardChannel(dc *downstreamConn, ch *upstreamChannel) {
s = string(membership) + nick
}
dc.messages <- &irc.Message{
dc.SendMessage(&irc.Message{
Prefix: dc.srv.prefix(),
Command: irc.RPL_NAMREPLY,
Params: []string{dc.nick, string(ch.Status), ch.Name, s},
}
})
}
dc.messages <- &irc.Message{
dc.SendMessage(&irc.Message{
Prefix: dc.srv.prefix(),
Command: irc.RPL_ENDOFNAMES,
Params: []string{dc.nick, ch.Name, "End of /NAMES list"},
}
})
}

View File

@ -102,7 +102,7 @@ func (c *downstreamConn) readMessages() error {
err = c.handleMessage(msg)
if ircErr, ok := err.(ircError); ok {
ircErr.Message.Prefix = c.srv.prefix()
c.messages <- ircErr.Message
c.SendMessage(ircErr.Message)
} else if err != nil {
return fmt.Errorf("failed to handle IRC command %q: %v", msg.Command, err)
}
@ -142,17 +142,21 @@ func (c *downstreamConn) Close() error {
return nil
}
func (c *downstreamConn) SendMessage(msg *irc.Message) {
c.messages <- msg
}
func (c *downstreamConn) handleMessage(msg *irc.Message) error {
switch msg.Command {
case "QUIT":
return c.Close()
case "PING":
// TODO: handle params
c.messages <- &irc.Message{
c.SendMessage(&irc.Message{
Prefix: c.srv.prefix(),
Command: "PONG",
Params: []string{c.srv.Hostname},
}
})
return nil
default:
if c.registered {
@ -189,11 +193,11 @@ func (c *downstreamConn) register() error {
u := c.srv.getUser(strings.TrimPrefix(c.username, "~"))
if u == nil {
c.logger.Printf("failed authentication: unknown username %q", c.username)
c.messages <- &irc.Message{
c.SendMessage(&irc.Message{
Prefix: c.srv.prefix(),
Command: irc.ERR_PASSWDMISMATCH,
Params: []string{"*", "Invalid username or password"},
}
})
return nil
}
@ -204,31 +208,31 @@ func (c *downstreamConn) register() error {
u.downstreamConns = append(u.downstreamConns, c)
u.lock.Unlock()
c.messages <- &irc.Message{
c.SendMessage(&irc.Message{
Prefix: c.srv.prefix(),
Command: irc.RPL_WELCOME,
Params: []string{c.nick, "Welcome to jounce, " + c.nick},
}
c.messages <- &irc.Message{
})
c.SendMessage(&irc.Message{
Prefix: c.srv.prefix(),
Command: irc.RPL_YOURHOST,
Params: []string{c.nick, "Your host is " + c.srv.Hostname},
}
c.messages <- &irc.Message{
})
c.SendMessage(&irc.Message{
Prefix: c.srv.prefix(),
Command: irc.RPL_CREATED,
Params: []string{c.nick, "Who cares when the server was created?"},
}
c.messages <- &irc.Message{
})
c.SendMessage(&irc.Message{
Prefix: c.srv.prefix(),
Command: irc.RPL_MYINFO,
Params: []string{c.nick, c.srv.Hostname, "jounce", "aiwroO", "OovaimnqpsrtklbeI"},
}
c.messages <- &irc.Message{
})
c.SendMessage(&irc.Message{
Prefix: c.srv.prefix(),
Command: irc.ERR_NOMOTD,
Params: []string{c.nick, "No MOTD"},
}
})
u.forEachUpstream(func(uc *upstreamConn) {
// TODO: fix races accessing upstream connection data
@ -246,7 +250,7 @@ func (c *downstreamConn) register() error {
if msg == nil {
break
}
c.messages <- msg
c.SendMessage(msg)
}
})
@ -312,11 +316,11 @@ func (c *downstreamConn) handleMessageRegistered(msg *irc.Message) error {
if modeStr != "" {
ch.conn.messages <- msg
} else {
c.messages <- &irc.Message{
c.SendMessage(&irc.Message{
Prefix: c.srv.prefix(),
Command: irc.RPL_CHANNELMODEIS,
Params: []string{ch.Name, string(ch.modes)},
}
})
}
} else {
if name != c.nick {
@ -331,11 +335,11 @@ func (c *downstreamConn) handleMessageRegistered(msg *irc.Message) error {
uc.messages <- msg
})
} else {
c.messages <- &irc.Message{
c.SendMessage(&irc.Message{
Prefix: c.srv.prefix(),
Command: irc.RPL_UMODEIS,
Params: []string{""}, // TODO
}
})
}
}
default:

View File

@ -132,7 +132,7 @@ func (c *upstreamConn) handleMessage(msg *irc.Message) error {
}
c.user.forEachDownstream(func(dc *downstreamConn) {
dc.messages <- msg
dc.SendMessage(msg)
})
case "NOTICE":
c.logger.Print(msg)
@ -172,7 +172,7 @@ func (c *upstreamConn) handleMessage(msg *irc.Message) error {
}
c.user.forEachDownstream(func(dc *downstreamConn) {
dc.messages <- msg
dc.SendMessage(msg)
})
case "JOIN":
var channels string
@ -198,7 +198,7 @@ func (c *upstreamConn) handleMessage(msg *irc.Message) error {
}
c.user.forEachDownstream(func(dc *downstreamConn) {
dc.messages <- msg
dc.SendMessage(msg)
})
case "PART":
var channels string
@ -220,7 +220,7 @@ func (c *upstreamConn) handleMessage(msg *irc.Message) error {
}
c.user.forEachDownstream(func(dc *downstreamConn) {
dc.messages <- msg
dc.SendMessage(msg)
})
case irc.RPL_TOPIC, irc.RPL_NOTOPIC:
var name, topic string
@ -306,7 +306,7 @@ func (c *upstreamConn) handleMessage(msg *irc.Message) error {
case "PRIVMSG":
c.ring.Produce(msg)
c.user.forEachDownstream(func(dc *downstreamConn) {
dc.messages <- msg
dc.SendMessage(msg)
})
case irc.RPL_YOURHOST, irc.RPL_CREATED:
// Ignore