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

View File

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