upstream: introduce upstreamConn.forwardMessage

This commit is contained in:
Simon Ser 2023-04-06 13:10:18 +02:00
parent 43e6febc15
commit 51768c256a

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) forwardMessage(msg *irc.Message) {
uc.forEachDownstream(func(dc *downstreamConn) {
dc.SendMessage(msg)
})
}
func (uc *upstreamConn) forwardMsgByID(id uint64, msg *irc.Message) { func (uc *upstreamConn) forwardMsgByID(id uint64, msg *irc.Message) {
uc.forEachDownstreamByID(id, func(dc *downstreamConn) { uc.forEachDownstreamByID(id, func(dc *downstreamConn) {
dc.SendMessage(msg) dc.SendMessage(msg)
@ -1082,9 +1088,7 @@ func (uc *upstreamConn) handleMessage(ctx context.Context, msg *irc.Message) err
}) })
if !me { if !me {
uc.forEachDownstream(func(dc *downstreamConn) { uc.forwardMessage(msg)
dc.SendMessage(msg)
})
} else { } else {
uc.forEachDownstream(func(dc *downstreamConn) { uc.forEachDownstream(func(dc *downstreamConn) {
dc.updateNick() dc.updateNick()
@ -1111,9 +1115,7 @@ func (uc *upstreamConn) handleMessage(ctx context.Context, msg *irc.Message) err
dc.updateRealname() dc.updateRealname()
}) })
} else { } else {
uc.forEachDownstream(func(dc *downstreamConn) { uc.forwardMessage(msg)
dc.SendMessage(msg)
})
} }
case "CHGHOST": case "CHGHOST":
var newUsername, newHostname string var newUsername, newHostname string
@ -1136,10 +1138,8 @@ func (uc *upstreamConn) handleMessage(ctx context.Context, msg *irc.Message) err
dc.updateHost() dc.updateHost()
}) })
} else { } else {
uc.forEachDownstream(func(dc *downstreamConn) {
// TODO: add fallback with QUIT/JOIN/MODE messages // TODO: add fallback with QUIT/JOIN/MODE messages
dc.SendMessage(msg) uc.forwardMessage(msg)
})
} }
case "JOIN": case "JOIN":
var channels string var channels string
@ -1274,9 +1274,7 @@ func (uc *upstreamConn) handleMessage(ctx context.Context, msg *irc.Message) err
uc.users.Del(msg.Prefix.Name) uc.users.Del(msg.Prefix.Name)
if msg.Prefix.Name != uc.nick { if msg.Prefix.Name != uc.nick {
uc.forEachDownstream(func(dc *downstreamConn) { uc.forwardMessage(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
@ -1324,13 +1322,7 @@ func (uc *upstreamConn) handleMessage(ctx context.Context, msg *irc.Message) err
return err return err
} }
uc.forEachDownstream(func(dc *downstreamConn) { uc.forwardMessage(msg)
if dc.upstream() == nil {
return
}
dc.SendMessage(msg)
})
} else { // channel mode change } else { // channel mode change
ch, err := uc.getChannel(name) ch, err := uc.getChannel(name)
if err != nil { if err != nil {
@ -1346,9 +1338,7 @@ func (uc *upstreamConn) handleMessage(ctx context.Context, msg *irc.Message) err
c := uc.network.channels.Get(name) c := uc.network.channels.Get(name)
if c == nil || !c.Detached { if c == nil || !c.Detached {
uc.forEachDownstream(func(dc *downstreamConn) { uc.forwardMessage(msg)
dc.SendMessage(msg)
})
} }
} }
case irc.RPL_UMODEIS: case irc.RPL_UMODEIS:
@ -1365,13 +1355,7 @@ func (uc *upstreamConn) handleMessage(ctx context.Context, msg *irc.Message) err
return err return err
} }
uc.forEachDownstream(func(dc *downstreamConn) { uc.forwardMessage(msg)
if dc.upstream() == nil {
return
}
dc.SendMessage(msg)
})
case irc.RPL_CHANNELMODEIS: case irc.RPL_CHANNELMODEIS:
var channel string var channel string
if err := parseMessageParams(msg, nil, &channel); err != nil { if err := parseMessageParams(msg, nil, &channel); err != nil {
@ -1395,9 +1379,7 @@ func (uc *upstreamConn) handleMessage(ctx context.Context, msg *irc.Message) err
c := uc.network.channels.Get(channel) c := uc.network.channels.Get(channel)
if firstMode && (c == nil || !c.Detached) { if firstMode && (c == nil || !c.Detached) {
uc.forEachDownstream(func(dc *downstreamConn) { uc.forwardMessage(msg)
dc.SendMessage(msg)
})
} }
case xirc.RPL_CREATIONTIME: case xirc.RPL_CREATIONTIME:
var channel, creationTime string var channel, creationTime string
@ -1415,9 +1397,7 @@ func (uc *upstreamConn) handleMessage(ctx context.Context, msg *irc.Message) err
c := uc.network.channels.Get(channel) c := uc.network.channels.Get(channel)
if firstCreationTime && (c == nil || !c.Detached) { if firstCreationTime && (c == nil || !c.Detached) {
uc.forEachDownstream(func(dc *downstreamConn) { uc.forwardMessage(msg)
dc.SendMessage(msg)
})
} }
case xirc.RPL_TOPICWHOTIME: case xirc.RPL_TOPICWHOTIME:
var channel, who, timeStr string var channel, who, timeStr string
@ -1440,9 +1420,7 @@ func (uc *upstreamConn) handleMessage(ctx context.Context, msg *irc.Message) err
c := uc.network.channels.Get(channel) c := uc.network.channels.Get(channel)
if firstTopicWhoTime && (c == nil || !c.Detached) { if firstTopicWhoTime && (c == nil || !c.Detached) {
uc.forEachDownstream(func(dc *downstreamConn) { uc.forwardMessage(msg)
dc.SendMessage(msg)
})
} }
case irc.RPL_LIST: case irc.RPL_LIST:
dc, cmd := uc.currentPendingCommand("LIST") dc, cmd := uc.currentPendingCommand("LIST")
@ -1705,9 +1683,7 @@ func (uc *upstreamConn) handleMessage(ctx context.Context, msg *irc.Message) err
}) })
} }
uc.forEachDownstream(func(dc *downstreamConn) { uc.forwardMessage(msg)
dc.SendMessage(msg)
})
case "ACCOUNT": case "ACCOUNT":
var account string var account string
if err := parseMessageParams(msg, &account); err != nil { if err := parseMessageParams(msg, &account); err != nil {
@ -1716,9 +1692,7 @@ func (uc *upstreamConn) handleMessage(ctx context.Context, msg *irc.Message) err
uc.cacheUserInfo(msg.Prefix.Name, &upstreamUser{ uc.cacheUserInfo(msg.Prefix.Name, &upstreamUser{
Account: account, Account: account,
}) })
uc.forEachDownstream(func(dc *downstreamConn) { uc.forwardMessage(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.forwardMsgByID(downstreamID, msg) uc.forwardMsgByID(downstreamID, msg)
case irc.ERR_NOSUCHNICK: case irc.ERR_NOSUCHNICK: