Add downstream NAMES support

NAMES reply for channels currently joined will be returned from cache;
requests for channels not joined will be forwarded from upstream.
This commit is contained in:
delthas 2020-03-21 01:24:29 +01:00 committed by Simon Ser
parent 98a95e9955
commit 9ff8429a53
No known key found for this signature in database
GPG Key ID: 0FDE7BE0E88F5E48
3 changed files with 69 additions and 7 deletions

View File

@ -26,8 +26,14 @@ func forwardChannel(dc *downstreamConn, ch *upstreamChannel) {
} }
// TODO: rpl_topicwhotime // TODO: rpl_topicwhotime
sendNames(dc, ch)
}
func sendNames(dc *downstreamConn, ch *upstreamChannel) {
// TODO: send multiple members in each message // TODO: send multiple members in each message
downstreamName := dc.marshalChannel(ch.conn, ch.Name)
for nick, membership := range ch.Members { for nick, membership := range ch.Members {
s := membership.String() + dc.marshalNick(ch.conn, nick) s := membership.String() + dc.marshalNick(ch.conn, nick)

View File

@ -913,13 +913,41 @@ func (dc *downstreamConn) handleMessageRegistered(msg *irc.Message) error {
Params: params, Params: params,
}) })
} }
case "NAMES":
if len(msg.Params) == 0 {
dc.SendMessage(&irc.Message{
Prefix: dc.srv.prefix(),
Command: irc.RPL_ENDOFNAMES,
Params: []string{dc.nick, "*", "End of /NAMES list"},
})
return nil
}
channels := strings.Split(msg.Params[0], ",")
for _, channel := range channels {
uc, upstreamChannel, err := dc.unmarshalEntity(channel)
if err != nil {
return err
}
ch, ok := uc.channels[upstreamChannel]
if ok {
sendNames(dc, ch)
} else {
// NAMES on a channel we have not joined, ask upstream
uc.SendMessage(&irc.Message{
Command: "NAMES",
Params: []string{upstreamChannel},
})
}
}
case "WHO": case "WHO":
if len(msg.Params) == 0 { if len(msg.Params) == 0 {
// TODO: support WHO without parameters // TODO: support WHO without parameters
dc.SendMessage(&irc.Message{ dc.SendMessage(&irc.Message{
Prefix: dc.srv.prefix(), Prefix: dc.srv.prefix(),
Command: irc.RPL_ENDOFWHO, Command: irc.RPL_ENDOFWHO,
Params: []string{dc.nick, "*", "End of /WHO list."}, Params: []string{dc.nick, "*", "End of /WHO list"},
}) })
return nil return nil
} }

View File

@ -651,9 +651,26 @@ func (uc *upstreamConn) handleMessage(msg *irc.Message) error {
if err := parseMessageParams(msg, nil, &statusStr, &name, &members); err != nil { if err := parseMessageParams(msg, nil, &statusStr, &name, &members); err != nil {
return err return err
} }
ch, err := uc.getChannel(name)
if err != nil { ch, ok := uc.channels[name]
return err if !ok {
// NAMES on a channel we have not joined, forward to downstream
uc.forEachDownstream(func(dc *downstreamConn) {
channel := dc.marshalChannel(uc, name)
members := strings.Split(members, " ")
for i, member := range members {
membership, nick := uc.parseMembershipPrefix(member)
members[i] = membership.String() + dc.marshalNick(uc, nick)
}
memberStr := strings.Join(members, " ")
dc.SendMessage(&irc.Message{
Prefix: dc.srv.prefix(),
Command: irc.RPL_NAMREPLY,
Params: []string{dc.nick, statusStr, channel, memberStr},
})
})
return nil
} }
status, err := parseChannelStatus(statusStr) status, err := parseChannelStatus(statusStr)
@ -671,9 +688,20 @@ func (uc *upstreamConn) handleMessage(msg *irc.Message) error {
if err := parseMessageParams(msg, nil, &name); err != nil { if err := parseMessageParams(msg, nil, &name); err != nil {
return err return err
} }
ch, err := uc.getChannel(name)
if err != nil { ch, ok := uc.channels[name]
return err if !ok {
// NAMES on a channel we have not joined, forward to downstream
uc.forEachDownstream(func(dc *downstreamConn) {
channel := dc.marshalChannel(uc, name)
dc.SendMessage(&irc.Message{
Prefix: dc.srv.prefix(),
Command: irc.RPL_ENDOFNAMES,
Params: []string{dc.nick, channel, "End of /NAMES list"},
})
})
return nil
} }
if ch.complete { if ch.complete {