Add upstreamConn.SendMessage

Allows us to change upstreamConn implementation details without updating
the whole codebase.
This commit is contained in:
Simon Ser 2020-02-17 16:17:31 +01:00
parent 70d811f5a8
commit 3c3f218b2b
No known key found for this signature in database
GPG Key ID: 0FDE7BE0E88F5E48
2 changed files with 18 additions and 14 deletions

View File

@ -326,7 +326,7 @@ func (dc *downstreamConn) handleMessageRegistered(msg *irc.Message) error {
}} }}
case "NICK": case "NICK":
dc.user.forEachUpstream(func(uc *upstreamConn) { dc.user.forEachUpstream(func(uc *upstreamConn) {
uc.messages <- msg uc.SendMessage(msg)
}) })
case "JOIN": case "JOIN":
var name string var name string
@ -354,7 +354,7 @@ func (dc *downstreamConn) handleMessageRegistered(msg *irc.Message) error {
return err return err
} }
ch.conn.messages <- msg ch.conn.SendMessage(msg)
// TODO: remove channel from upstream config // TODO: remove channel from upstream config
case "MODE": case "MODE":
var name string var name string
@ -374,7 +374,7 @@ func (dc *downstreamConn) handleMessageRegistered(msg *irc.Message) error {
} }
if modeStr != "" { if modeStr != "" {
ch.conn.messages <- msg ch.conn.SendMessage(msg)
} else { } else {
dc.SendMessage(&irc.Message{ dc.SendMessage(&irc.Message{
Prefix: dc.srv.prefix(), Prefix: dc.srv.prefix(),
@ -392,7 +392,7 @@ func (dc *downstreamConn) handleMessageRegistered(msg *irc.Message) error {
if modeStr != "" { if modeStr != "" {
dc.user.forEachUpstream(func(uc *upstreamConn) { dc.user.forEachUpstream(func(uc *upstreamConn) {
uc.messages <- msg uc.SendMessage(msg)
}) })
} else { } else {
dc.SendMessage(&irc.Message{ dc.SendMessage(&irc.Message{
@ -414,11 +414,11 @@ func (dc *downstreamConn) handleMessageRegistered(msg *irc.Message) error {
return err return err
} }
ch.conn.messages <- &irc.Message{ ch.conn.SendMessage(&irc.Message{
Prefix: msg.Prefix, Prefix: msg.Prefix,
Command: "PRIVMSG", Command: "PRIVMSG",
Params: []string{name, text}, Params: []string{name, text},
} })
} }
default: default:
dc.logger.Printf("unhandled message: %v", msg) dc.logger.Printf("unhandled message: %v", msg)

View File

@ -107,10 +107,10 @@ func (uc *upstreamConn) handleMessage(msg *irc.Message) error {
switch msg.Command { switch msg.Command {
case "PING": case "PING":
// TODO: handle params // TODO: handle params
uc.messages <- &irc.Message{ uc.SendMessage(&irc.Message{
Command: "PONG", Command: "PONG",
Params: []string{uc.srv.Hostname}, Params: []string{uc.srv.Hostname},
} })
return nil return nil
case "MODE": case "MODE":
var name, modeStr string var name, modeStr string
@ -143,10 +143,10 @@ func (uc *upstreamConn) handleMessage(msg *irc.Message) error {
uc.logger.Printf("connection registered") uc.logger.Printf("connection registered")
for _, ch := range uc.upstream.Channels { for _, ch := range uc.upstream.Channels {
uc.messages <- &irc.Message{ uc.SendMessage(&irc.Message{
Command: "JOIN", Command: "JOIN",
Params: []string{ch}, Params: []string{ch},
} })
} }
case irc.RPL_MYINFO: case irc.RPL_MYINFO:
if err := parseMessageParams(msg, nil, &uc.serverName, nil, &uc.availableUserModes, &uc.availableChannelModes); err != nil { if err := parseMessageParams(msg, nil, &uc.serverName, nil, &uc.availableUserModes, &uc.availableChannelModes); err != nil {
@ -325,14 +325,14 @@ func (uc *upstreamConn) handleMessage(msg *irc.Message) error {
func (uc *upstreamConn) register() { func (uc *upstreamConn) register() {
uc.nick = uc.upstream.Nick uc.nick = uc.upstream.Nick
uc.messages <- &irc.Message{ uc.SendMessage(&irc.Message{
Command: "NICK", Command: "NICK",
Params: []string{uc.upstream.Nick}, Params: []string{uc.upstream.Nick},
} })
uc.messages <- &irc.Message{ uc.SendMessage(&irc.Message{
Command: "USER", Command: "USER",
Params: []string{uc.upstream.Username, "0", "*", uc.upstream.Realname}, Params: []string{uc.upstream.Username, "0", "*", uc.upstream.Realname},
} })
} }
func (uc *upstreamConn) readMessages() error { func (uc *upstreamConn) readMessages() error {
@ -351,3 +351,7 @@ func (uc *upstreamConn) readMessages() error {
return nil return nil
} }
func (uc *upstreamConn) SendMessage(msg *irc.Message) {
uc.messages <- msg
}