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:
parent
86f9221ddc
commit
0cf731aeab
20
bridge.go
20
bridge.go
@ -9,24 +9,24 @@ func forwardChannel(dc *downstreamConn, ch *upstreamChannel) {
|
|||||||
panic("Tried to forward a partial channel")
|
panic("Tried to forward a partial channel")
|
||||||
}
|
}
|
||||||
|
|
||||||
dc.messages <- &irc.Message{
|
dc.SendMessage(&irc.Message{
|
||||||
Prefix: dc.prefix(),
|
Prefix: dc.prefix(),
|
||||||
Command: "JOIN",
|
Command: "JOIN",
|
||||||
Params: []string{ch.Name},
|
Params: []string{ch.Name},
|
||||||
}
|
})
|
||||||
|
|
||||||
if ch.Topic != "" {
|
if ch.Topic != "" {
|
||||||
dc.messages <- &irc.Message{
|
dc.SendMessage(&irc.Message{
|
||||||
Prefix: dc.srv.prefix(),
|
Prefix: dc.srv.prefix(),
|
||||||
Command: irc.RPL_TOPIC,
|
Command: irc.RPL_TOPIC,
|
||||||
Params: []string{dc.nick, ch.Name, ch.Topic},
|
Params: []string{dc.nick, ch.Name, ch.Topic},
|
||||||
}
|
})
|
||||||
} else {
|
} else {
|
||||||
dc.messages <- &irc.Message{
|
dc.SendMessage(&irc.Message{
|
||||||
Prefix: dc.srv.prefix(),
|
Prefix: dc.srv.prefix(),
|
||||||
Command: irc.RPL_NOTOPIC,
|
Command: irc.RPL_NOTOPIC,
|
||||||
Params: []string{dc.nick, ch.Name, "No topic is set"},
|
Params: []string{dc.nick, ch.Name, "No topic is set"},
|
||||||
}
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: rpl_topicwhotime
|
// TODO: rpl_topicwhotime
|
||||||
@ -38,16 +38,16 @@ func forwardChannel(dc *downstreamConn, ch *upstreamChannel) {
|
|||||||
s = string(membership) + nick
|
s = string(membership) + nick
|
||||||
}
|
}
|
||||||
|
|
||||||
dc.messages <- &irc.Message{
|
dc.SendMessage(&irc.Message{
|
||||||
Prefix: dc.srv.prefix(),
|
Prefix: dc.srv.prefix(),
|
||||||
Command: irc.RPL_NAMREPLY,
|
Command: irc.RPL_NAMREPLY,
|
||||||
Params: []string{dc.nick, string(ch.Status), ch.Name, s},
|
Params: []string{dc.nick, string(ch.Status), ch.Name, s},
|
||||||
}
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
dc.messages <- &irc.Message{
|
dc.SendMessage(&irc.Message{
|
||||||
Prefix: dc.srv.prefix(),
|
Prefix: dc.srv.prefix(),
|
||||||
Command: irc.RPL_ENDOFNAMES,
|
Command: irc.RPL_ENDOFNAMES,
|
||||||
Params: []string{dc.nick, ch.Name, "End of /NAMES list"},
|
Params: []string{dc.nick, ch.Name, "End of /NAMES list"},
|
||||||
}
|
})
|
||||||
}
|
}
|
||||||
|
@ -102,7 +102,7 @@ func (c *downstreamConn) readMessages() error {
|
|||||||
err = c.handleMessage(msg)
|
err = c.handleMessage(msg)
|
||||||
if ircErr, ok := err.(ircError); ok {
|
if ircErr, ok := err.(ircError); ok {
|
||||||
ircErr.Message.Prefix = c.srv.prefix()
|
ircErr.Message.Prefix = c.srv.prefix()
|
||||||
c.messages <- ircErr.Message
|
c.SendMessage(ircErr.Message)
|
||||||
} else if err != nil {
|
} else if err != nil {
|
||||||
return fmt.Errorf("failed to handle IRC command %q: %v", msg.Command, err)
|
return fmt.Errorf("failed to handle IRC command %q: %v", msg.Command, err)
|
||||||
}
|
}
|
||||||
@ -142,17 +142,21 @@ func (c *downstreamConn) Close() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *downstreamConn) SendMessage(msg *irc.Message) {
|
||||||
|
c.messages <- msg
|
||||||
|
}
|
||||||
|
|
||||||
func (c *downstreamConn) handleMessage(msg *irc.Message) error {
|
func (c *downstreamConn) handleMessage(msg *irc.Message) error {
|
||||||
switch msg.Command {
|
switch msg.Command {
|
||||||
case "QUIT":
|
case "QUIT":
|
||||||
return c.Close()
|
return c.Close()
|
||||||
case "PING":
|
case "PING":
|
||||||
// TODO: handle params
|
// TODO: handle params
|
||||||
c.messages <- &irc.Message{
|
c.SendMessage(&irc.Message{
|
||||||
Prefix: c.srv.prefix(),
|
Prefix: c.srv.prefix(),
|
||||||
Command: "PONG",
|
Command: "PONG",
|
||||||
Params: []string{c.srv.Hostname},
|
Params: []string{c.srv.Hostname},
|
||||||
}
|
})
|
||||||
return nil
|
return nil
|
||||||
default:
|
default:
|
||||||
if c.registered {
|
if c.registered {
|
||||||
@ -189,11 +193,11 @@ func (c *downstreamConn) register() error {
|
|||||||
u := c.srv.getUser(strings.TrimPrefix(c.username, "~"))
|
u := c.srv.getUser(strings.TrimPrefix(c.username, "~"))
|
||||||
if u == nil {
|
if u == nil {
|
||||||
c.logger.Printf("failed authentication: unknown username %q", c.username)
|
c.logger.Printf("failed authentication: unknown username %q", c.username)
|
||||||
c.messages <- &irc.Message{
|
c.SendMessage(&irc.Message{
|
||||||
Prefix: c.srv.prefix(),
|
Prefix: c.srv.prefix(),
|
||||||
Command: irc.ERR_PASSWDMISMATCH,
|
Command: irc.ERR_PASSWDMISMATCH,
|
||||||
Params: []string{"*", "Invalid username or password"},
|
Params: []string{"*", "Invalid username or password"},
|
||||||
}
|
})
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -204,31 +208,31 @@ func (c *downstreamConn) register() error {
|
|||||||
u.downstreamConns = append(u.downstreamConns, c)
|
u.downstreamConns = append(u.downstreamConns, c)
|
||||||
u.lock.Unlock()
|
u.lock.Unlock()
|
||||||
|
|
||||||
c.messages <- &irc.Message{
|
c.SendMessage(&irc.Message{
|
||||||
Prefix: c.srv.prefix(),
|
Prefix: c.srv.prefix(),
|
||||||
Command: irc.RPL_WELCOME,
|
Command: irc.RPL_WELCOME,
|
||||||
Params: []string{c.nick, "Welcome to jounce, " + c.nick},
|
Params: []string{c.nick, "Welcome to jounce, " + c.nick},
|
||||||
}
|
})
|
||||||
c.messages <- &irc.Message{
|
c.SendMessage(&irc.Message{
|
||||||
Prefix: c.srv.prefix(),
|
Prefix: c.srv.prefix(),
|
||||||
Command: irc.RPL_YOURHOST,
|
Command: irc.RPL_YOURHOST,
|
||||||
Params: []string{c.nick, "Your host is " + c.srv.Hostname},
|
Params: []string{c.nick, "Your host is " + c.srv.Hostname},
|
||||||
}
|
})
|
||||||
c.messages <- &irc.Message{
|
c.SendMessage(&irc.Message{
|
||||||
Prefix: c.srv.prefix(),
|
Prefix: c.srv.prefix(),
|
||||||
Command: irc.RPL_CREATED,
|
Command: irc.RPL_CREATED,
|
||||||
Params: []string{c.nick, "Who cares when the server was created?"},
|
Params: []string{c.nick, "Who cares when the server was created?"},
|
||||||
}
|
})
|
||||||
c.messages <- &irc.Message{
|
c.SendMessage(&irc.Message{
|
||||||
Prefix: c.srv.prefix(),
|
Prefix: c.srv.prefix(),
|
||||||
Command: irc.RPL_MYINFO,
|
Command: irc.RPL_MYINFO,
|
||||||
Params: []string{c.nick, c.srv.Hostname, "jounce", "aiwroO", "OovaimnqpsrtklbeI"},
|
Params: []string{c.nick, c.srv.Hostname, "jounce", "aiwroO", "OovaimnqpsrtklbeI"},
|
||||||
}
|
})
|
||||||
c.messages <- &irc.Message{
|
c.SendMessage(&irc.Message{
|
||||||
Prefix: c.srv.prefix(),
|
Prefix: c.srv.prefix(),
|
||||||
Command: irc.ERR_NOMOTD,
|
Command: irc.ERR_NOMOTD,
|
||||||
Params: []string{c.nick, "No MOTD"},
|
Params: []string{c.nick, "No MOTD"},
|
||||||
}
|
})
|
||||||
|
|
||||||
u.forEachUpstream(func(uc *upstreamConn) {
|
u.forEachUpstream(func(uc *upstreamConn) {
|
||||||
// TODO: fix races accessing upstream connection data
|
// TODO: fix races accessing upstream connection data
|
||||||
@ -246,7 +250,7 @@ func (c *downstreamConn) register() error {
|
|||||||
if msg == nil {
|
if msg == nil {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
c.messages <- msg
|
c.SendMessage(msg)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -312,11 +316,11 @@ func (c *downstreamConn) handleMessageRegistered(msg *irc.Message) error {
|
|||||||
if modeStr != "" {
|
if modeStr != "" {
|
||||||
ch.conn.messages <- msg
|
ch.conn.messages <- msg
|
||||||
} else {
|
} else {
|
||||||
c.messages <- &irc.Message{
|
c.SendMessage(&irc.Message{
|
||||||
Prefix: c.srv.prefix(),
|
Prefix: c.srv.prefix(),
|
||||||
Command: irc.RPL_CHANNELMODEIS,
|
Command: irc.RPL_CHANNELMODEIS,
|
||||||
Params: []string{ch.Name, string(ch.modes)},
|
Params: []string{ch.Name, string(ch.modes)},
|
||||||
}
|
})
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if name != c.nick {
|
if name != c.nick {
|
||||||
@ -331,11 +335,11 @@ func (c *downstreamConn) handleMessageRegistered(msg *irc.Message) error {
|
|||||||
uc.messages <- msg
|
uc.messages <- msg
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
c.messages <- &irc.Message{
|
c.SendMessage(&irc.Message{
|
||||||
Prefix: c.srv.prefix(),
|
Prefix: c.srv.prefix(),
|
||||||
Command: irc.RPL_UMODEIS,
|
Command: irc.RPL_UMODEIS,
|
||||||
Params: []string{""}, // TODO
|
Params: []string{""}, // TODO
|
||||||
}
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
|
10
upstream.go
10
upstream.go
@ -132,7 +132,7 @@ func (c *upstreamConn) handleMessage(msg *irc.Message) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
c.user.forEachDownstream(func(dc *downstreamConn) {
|
c.user.forEachDownstream(func(dc *downstreamConn) {
|
||||||
dc.messages <- msg
|
dc.SendMessage(msg)
|
||||||
})
|
})
|
||||||
case "NOTICE":
|
case "NOTICE":
|
||||||
c.logger.Print(msg)
|
c.logger.Print(msg)
|
||||||
@ -172,7 +172,7 @@ func (c *upstreamConn) handleMessage(msg *irc.Message) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
c.user.forEachDownstream(func(dc *downstreamConn) {
|
c.user.forEachDownstream(func(dc *downstreamConn) {
|
||||||
dc.messages <- msg
|
dc.SendMessage(msg)
|
||||||
})
|
})
|
||||||
case "JOIN":
|
case "JOIN":
|
||||||
var channels string
|
var channels string
|
||||||
@ -198,7 +198,7 @@ func (c *upstreamConn) handleMessage(msg *irc.Message) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
c.user.forEachDownstream(func(dc *downstreamConn) {
|
c.user.forEachDownstream(func(dc *downstreamConn) {
|
||||||
dc.messages <- msg
|
dc.SendMessage(msg)
|
||||||
})
|
})
|
||||||
case "PART":
|
case "PART":
|
||||||
var channels string
|
var channels string
|
||||||
@ -220,7 +220,7 @@ func (c *upstreamConn) handleMessage(msg *irc.Message) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
c.user.forEachDownstream(func(dc *downstreamConn) {
|
c.user.forEachDownstream(func(dc *downstreamConn) {
|
||||||
dc.messages <- msg
|
dc.SendMessage(msg)
|
||||||
})
|
})
|
||||||
case irc.RPL_TOPIC, irc.RPL_NOTOPIC:
|
case irc.RPL_TOPIC, irc.RPL_NOTOPIC:
|
||||||
var name, topic string
|
var name, topic string
|
||||||
@ -306,7 +306,7 @@ func (c *upstreamConn) handleMessage(msg *irc.Message) error {
|
|||||||
case "PRIVMSG":
|
case "PRIVMSG":
|
||||||
c.ring.Produce(msg)
|
c.ring.Produce(msg)
|
||||||
c.user.forEachDownstream(func(dc *downstreamConn) {
|
c.user.forEachDownstream(func(dc *downstreamConn) {
|
||||||
dc.messages <- msg
|
dc.SendMessage(msg)
|
||||||
})
|
})
|
||||||
case irc.RPL_YOURHOST, irc.RPL_CREATED:
|
case irc.RPL_YOURHOST, irc.RPL_CREATED:
|
||||||
// Ignore
|
// Ignore
|
||||||
|
Loading…
Reference in New Issue
Block a user