upstream: add forwardMsgByID

This commit is contained in:
Simon Ser 2023-04-03 22:32:49 +02:00
parent be6cc27981
commit b0f2f9c36b

View File

@ -421,6 +421,12 @@ func (uc *upstreamConn) isOurNick(nick string) bool {
return uc.network.equalCasemap(uc.nick, nick) return uc.network.equalCasemap(uc.nick, nick)
} }
func (uc *upstreamConn) forwardMsgByID(id uint64, msg *irc.Message) {
uc.forEachDownstreamByID(id, func(dc *downstreamConn) {
dc.SendMessage(msg)
})
}
func (uc *upstreamConn) abortPendingCommands() { func (uc *upstreamConn) abortPendingCommands() {
for _, l := range uc.pendingCmds { for _, l := range uc.pendingCmds {
for _, pendingCmd := range l { for _, pendingCmd := range l {
@ -1005,13 +1011,7 @@ func (uc *upstreamConn) handleMessage(ctx context.Context, msg *irc.Message) err
return nil return nil
} }
uc.forEachDownstreamByID(downstreamID, func(dc *downstreamConn) { uc.forwardMsgByID(downstreamID, msg)
dc.SendMessage(&irc.Message{
Prefix: uc.srv.prefix(),
Command: msg.Command,
Params: msg.Params,
})
})
case "BATCH": case "BATCH":
var tag string var tag string
if err := parseMessageParams(msg, &tag); err != nil { if err := parseMessageParams(msg, &tag); err != nil {
@ -1471,9 +1471,7 @@ func (uc *upstreamConn) handleMessage(ctx context.Context, msg *irc.Message) err
ch := uc.channels.Get(name) ch := uc.channels.Get(name)
if ch == nil { if ch == nil {
// NAMES on a channel we have not joined, forward to downstream // NAMES on a channel we have not joined, forward to downstream
uc.forEachDownstreamByID(downstreamID, func(dc *downstreamConn) { uc.forwardMsgByID(downstreamID, msg)
dc.SendMessage(msg)
})
return nil return nil
} }
@ -1496,9 +1494,7 @@ func (uc *upstreamConn) handleMessage(ctx context.Context, msg *irc.Message) err
ch := uc.channels.Get(name) ch := uc.channels.Get(name)
if ch == nil { if ch == nil {
// NAMES on a channel we have not joined, forward to downstream // NAMES on a channel we have not joined, forward to downstream
uc.forEachDownstreamByID(downstreamID, func(dc *downstreamConn) { uc.forwardMsgByID(downstreamID, msg)
dc.SendMessage(msg)
})
return nil return nil
} }
@ -1628,9 +1624,7 @@ func (uc *upstreamConn) handleMessage(ctx context.Context, msg *irc.Message) err
return err return err
} }
uc.forEachDownstreamByID(downstreamID, func(dc *downstreamConn) { uc.forwardMsgByID(downstreamID, msg)
dc.SendMessage(msg)
})
case irc.RPL_MONONLINE, irc.RPL_MONOFFLINE: case irc.RPL_MONONLINE, irc.RPL_MONOFFLINE:
var targetsStr string var targetsStr string
if err := parseMessageParams(msg, nil, &targetsStr); err != nil { if err := parseMessageParams(msg, nil, &targetsStr); err != nil {
@ -1695,9 +1689,7 @@ func (uc *upstreamConn) handleMessage(ctx context.Context, msg *irc.Message) err
} }
}) })
case irc.RPL_AWAY: case irc.RPL_AWAY:
uc.forEachDownstreamByID(downstreamID, func(dc *downstreamConn) { uc.forwardMsgByID(downstreamID, msg)
dc.SendMessage(msg)
})
case "AWAY": case "AWAY":
// Update user flags, if we already have the flags cached // Update user flags, if we already have the flags cached
uu := uc.users.Get(msg.Prefix.Name) uu := uc.users.Get(msg.Prefix.Name)
@ -1728,9 +1720,7 @@ func (uc *upstreamConn) handleMessage(ctx context.Context, msg *irc.Message) err
dc.SendMessage(msg) dc.SendMessage(msg)
}) })
case irc.RPL_BANLIST, irc.RPL_INVITELIST, irc.RPL_EXCEPTLIST, irc.RPL_ENDOFBANLIST, irc.RPL_ENDOFINVITELIST, irc.RPL_ENDOFEXCEPTLIST: case irc.RPL_BANLIST, irc.RPL_INVITELIST, irc.RPL_EXCEPTLIST, irc.RPL_ENDOFBANLIST, irc.RPL_ENDOFINVITELIST, irc.RPL_ENDOFEXCEPTLIST:
uc.forEachDownstreamByID(downstreamID, func(dc *downstreamConn) { uc.forwardMsgByID(downstreamID, msg)
dc.SendMessage(msg)
})
case irc.ERR_NOSUCHNICK: case irc.ERR_NOSUCHNICK:
var nick, reason string var nick, reason string
if err := parseMessageParams(msg, nil, &nick, &reason); err != nil { if err := parseMessageParams(msg, nil, &nick, &reason); err != nil {
@ -1760,13 +1750,7 @@ func (uc *upstreamConn) handleMessage(ctx context.Context, msg *irc.Message) err
uc.saslStarted = false uc.saslStarted = false
} }
uc.forEachDownstreamByID(downstreamID, func(dc *downstreamConn) { uc.forwardMsgByID(downstreamID, msg)
dc.SendMessage(&irc.Message{
Prefix: uc.srv.prefix(),
Command: msg.Command,
Params: []string{dc.nick, command, reason},
})
})
case "FAIL": case "FAIL":
var command, code string var command, code string
if err := parseMessageParams(msg, &command, &code); err != nil { if err := parseMessageParams(msg, &command, &code); err != nil {
@ -1781,9 +1765,7 @@ func (uc *upstreamConn) handleMessage(ctx context.Context, msg *irc.Message) err
downstreamID = dc.id downstreamID = dc.id
} }
uc.forEachDownstreamByID(downstreamID, func(dc *downstreamConn) { uc.forwardMsgByID(downstreamID, msg)
dc.SendMessage(msg)
})
case "ACK": case "ACK":
// Ignore // Ignore
case irc.RPL_NOWAWAY, irc.RPL_UNAWAY: case irc.RPL_NOWAWAY, irc.RPL_UNAWAY:
@ -1803,13 +1785,7 @@ func (uc *upstreamConn) handleMessage(ctx context.Context, msg *irc.Message) err
return nil return nil
} }
uc.forEachDownstreamByID(downstreamID, func(dc *downstreamConn) { uc.forwardMsgByID(downstreamID, msg)
dc.SendMessage(&irc.Message{
Prefix: uc.srv.prefix(),
Command: msg.Command,
Params: msg.Params,
})
})
case irc.RPL_LISTSTART: case irc.RPL_LISTSTART:
// Ignore // Ignore
case "ERROR": case "ERROR":
@ -1852,10 +1828,7 @@ func (uc *upstreamConn) handleMessage(ctx context.Context, msg *irc.Message) err
fallthrough fallthrough
default: default:
uc.logger.Printf("unhandled message: %v", msg) uc.logger.Printf("unhandled message: %v", msg)
uc.forwardMsgByID(downstreamID, msg)
uc.forEachDownstreamByID(downstreamID, func(dc *downstreamConn) {
dc.SendMessage(msg)
})
} }
return nil return nil
} }