soju/xirc/whox.go

180 lines
3.8 KiB
Go
Raw Normal View History

2022-05-29 09:33:29 -07:00
package xirc
import (
2022-11-14 03:06:58 -08:00
"gopkg.in/irc.v4"
Add WHO cache This adds a new field to upstreams, members, which is a casemapped map of upstream users known to the soju. The upstream users known to soju are: self, any monitored user, and any user with whom we share a channel. The information stored for each upstream user corresponds to the info that can be returned by a WHO/WHOX command. We build the upstream user information both incrementally, capturing information contained in JOIN and AWAY messages; and with the bulk user information contained in WHO replies we receive. This lets us build a user cache that can then be used to return synthetic WHO responses to later WHO requests by downstreams. This is useful because some networks (eg Libera) heavily throttle WHO commands, and without this cache, any downstream connecting would send 1 WHO command per channel, so possibly more than a dozen WHO commands, which soju then forwarded to the upstream as WHO commands. With this cache most WHO commands can be cached and avoid sending WHO commands to the upstream. In order to cache the "flags" field, we synthetize the field from user info we get from incremental messages: away status (H/G) and bot status (B). This could result in incorrect values for proprietary user fields. Support for the server-operator status (*) is also not supported. Of note is that it is difficult to obtain a user "connected server" field incrementally, so clients that want to maximize their WHO cache hit ratio can use WHOX to only request fields they need, and in particular not include the server field flag. Co-authored-by: delthas <delthas@dille.cc>
2022-12-01 06:47:58 -08:00
"fmt"
"strings"
2022-05-29 09:33:29 -07:00
)
// whoxFields is the list of all WHOX field letters, by order of appearance in
// RPL_WHOSPCRPL messages.
var whoxFields = []byte("tcuihsnfdlaor")
type WHOXInfo struct {
Token string
Channel string
2022-05-29 09:33:29 -07:00
Username string
Hostname string
Server string
Nickname string
Flags string
Account string
Realname string
}
Add WHO cache This adds a new field to upstreams, members, which is a casemapped map of upstream users known to the soju. The upstream users known to soju are: self, any monitored user, and any user with whom we share a channel. The information stored for each upstream user corresponds to the info that can be returned by a WHO/WHOX command. We build the upstream user information both incrementally, capturing information contained in JOIN and AWAY messages; and with the bulk user information contained in WHO replies we receive. This lets us build a user cache that can then be used to return synthetic WHO responses to later WHO requests by downstreams. This is useful because some networks (eg Libera) heavily throttle WHO commands, and without this cache, any downstream connecting would send 1 WHO command per channel, so possibly more than a dozen WHO commands, which soju then forwarded to the upstream as WHO commands. With this cache most WHO commands can be cached and avoid sending WHO commands to the upstream. In order to cache the "flags" field, we synthetize the field from user info we get from incremental messages: away status (H/G) and bot status (B). This could result in incorrect values for proprietary user fields. Support for the server-operator status (*) is also not supported. Of note is that it is difficult to obtain a user "connected server" field incrementally, so clients that want to maximize their WHO cache hit ratio can use WHOX to only request fields they need, and in particular not include the server field flag. Co-authored-by: delthas <delthas@dille.cc>
2022-12-01 06:47:58 -08:00
func (info *WHOXInfo) get(k byte) string {
switch k {
2022-05-29 09:33:29 -07:00
case 't':
return info.Token
case 'c':
channel := info.Channel
if channel == "" {
channel = "*"
}
return channel
2022-05-29 09:33:29 -07:00
case 'u':
return info.Username
case 'i':
return "255.255.255.255"
case 'h':
hostname := info.Hostname
if strings.HasPrefix(info.Hostname, ":") {
// The hostname cannot start with a colon as this would get parsed
// as a trailing parameter. IPv6 addresses such as "::1" are
// prefixed with a zero to ensure this.
hostname = "0" + hostname
}
return hostname
2022-05-29 09:33:29 -07:00
case 's':
return info.Server
case 'n':
return info.Nickname
case 'f':
return info.Flags
case 'd':
return "0"
case 'l': // idle time
return "0"
case 'a':
account := "0" // WHOX uses "0" to mean "no account"
if info.Account != "" && info.Account != "*" {
account = info.Account
}
return account
case 'o':
return "0"
case 'r':
return info.Realname
}
return ""
}
Add WHO cache This adds a new field to upstreams, members, which is a casemapped map of upstream users known to the soju. The upstream users known to soju are: self, any monitored user, and any user with whom we share a channel. The information stored for each upstream user corresponds to the info that can be returned by a WHO/WHOX command. We build the upstream user information both incrementally, capturing information contained in JOIN and AWAY messages; and with the bulk user information contained in WHO replies we receive. This lets us build a user cache that can then be used to return synthetic WHO responses to later WHO requests by downstreams. This is useful because some networks (eg Libera) heavily throttle WHO commands, and without this cache, any downstream connecting would send 1 WHO command per channel, so possibly more than a dozen WHO commands, which soju then forwarded to the upstream as WHO commands. With this cache most WHO commands can be cached and avoid sending WHO commands to the upstream. In order to cache the "flags" field, we synthetize the field from user info we get from incremental messages: away status (H/G) and bot status (B). This could result in incorrect values for proprietary user fields. Support for the server-operator status (*) is also not supported. Of note is that it is difficult to obtain a user "connected server" field incrementally, so clients that want to maximize their WHO cache hit ratio can use WHOX to only request fields they need, and in particular not include the server field flag. Co-authored-by: delthas <delthas@dille.cc>
2022-12-01 06:47:58 -08:00
func (info *WHOXInfo) set(k byte, v string) {
switch k {
case 't':
info.Token = v
case 'c':
info.Channel = v
Add WHO cache This adds a new field to upstreams, members, which is a casemapped map of upstream users known to the soju. The upstream users known to soju are: self, any monitored user, and any user with whom we share a channel. The information stored for each upstream user corresponds to the info that can be returned by a WHO/WHOX command. We build the upstream user information both incrementally, capturing information contained in JOIN and AWAY messages; and with the bulk user information contained in WHO replies we receive. This lets us build a user cache that can then be used to return synthetic WHO responses to later WHO requests by downstreams. This is useful because some networks (eg Libera) heavily throttle WHO commands, and without this cache, any downstream connecting would send 1 WHO command per channel, so possibly more than a dozen WHO commands, which soju then forwarded to the upstream as WHO commands. With this cache most WHO commands can be cached and avoid sending WHO commands to the upstream. In order to cache the "flags" field, we synthetize the field from user info we get from incremental messages: away status (H/G) and bot status (B). This could result in incorrect values for proprietary user fields. Support for the server-operator status (*) is also not supported. Of note is that it is difficult to obtain a user "connected server" field incrementally, so clients that want to maximize their WHO cache hit ratio can use WHOX to only request fields they need, and in particular not include the server field flag. Co-authored-by: delthas <delthas@dille.cc>
2022-12-01 06:47:58 -08:00
case 'u':
info.Username = v
case 'h':
info.Hostname = v
case 's':
info.Server = v
case 'n':
info.Nickname = v
case 'f':
info.Flags = v
case 'a':
info.Account = v
case 'r':
info.Realname = v
}
}
func GenerateWHOXReply(prefix *irc.Prefix, fields string, info *WHOXInfo) *irc.Message {
2022-05-29 09:33:29 -07:00
if fields == "" {
hostname := info.Hostname
if strings.HasPrefix(info.Hostname, ":") {
// The hostname cannot start with a colon as this would get parsed
// as a trailing parameter. IPv6 addresses such as "::1" are
// prefixed with a zero to ensure this.
hostname = "0" + hostname
}
channel := info.Channel
if channel == "" {
channel = "*"
}
2022-05-29 09:33:29 -07:00
return &irc.Message{
Prefix: prefix,
Command: irc.RPL_WHOREPLY,
Params: []string{"*", channel, info.Username, hostname, info.Server, info.Nickname, info.Flags, "0 " + info.Realname},
2022-05-29 09:33:29 -07:00
}
}
fieldSet := make(map[byte]bool)
for i := 0; i < len(fields); i++ {
fieldSet[fields[i]] = true
}
var values []string
for _, field := range whoxFields {
if !fieldSet[field] {
continue
}
values = append(values, info.get(field))
}
return &irc.Message{
Prefix: prefix,
Command: RPL_WHOSPCRPL,
Params: append([]string{"*"}, values...),
2022-05-29 09:33:29 -07:00
}
}
Add WHO cache This adds a new field to upstreams, members, which is a casemapped map of upstream users known to the soju. The upstream users known to soju are: self, any monitored user, and any user with whom we share a channel. The information stored for each upstream user corresponds to the info that can be returned by a WHO/WHOX command. We build the upstream user information both incrementally, capturing information contained in JOIN and AWAY messages; and with the bulk user information contained in WHO replies we receive. This lets us build a user cache that can then be used to return synthetic WHO responses to later WHO requests by downstreams. This is useful because some networks (eg Libera) heavily throttle WHO commands, and without this cache, any downstream connecting would send 1 WHO command per channel, so possibly more than a dozen WHO commands, which soju then forwarded to the upstream as WHO commands. With this cache most WHO commands can be cached and avoid sending WHO commands to the upstream. In order to cache the "flags" field, we synthetize the field from user info we get from incremental messages: away status (H/G) and bot status (B). This could result in incorrect values for proprietary user fields. Support for the server-operator status (*) is also not supported. Of note is that it is difficult to obtain a user "connected server" field incrementally, so clients that want to maximize their WHO cache hit ratio can use WHOX to only request fields they need, and in particular not include the server field flag. Co-authored-by: delthas <delthas@dille.cc>
2022-12-01 06:47:58 -08:00
func ParseWHOXOptions(options string) (fields, whoxToken string) {
optionsParts := strings.SplitN(options, "%", 2)
// TODO: add support for WHOX flags in optionsParts[0]
if len(optionsParts) == 2 {
optionsParts := strings.SplitN(optionsParts[1], ",", 2)
fields = strings.ToLower(optionsParts[0])
if len(optionsParts) == 2 && strings.Contains(fields, "t") {
whoxToken = optionsParts[1]
}
}
return fields, whoxToken
}
func ParseWHOXReply(msg *irc.Message, fields string) (*WHOXInfo, error) {
if msg.Command != RPL_WHOSPCRPL {
return nil, fmt.Errorf("invalid WHOX reply %q", msg.Command)
} else if len(msg.Params) == 0 {
return nil, fmt.Errorf("invalid RPL_WHOSPCRPL: no params")
}
fieldSet := make(map[byte]bool)
for i := 0; i < len(fields); i++ {
fieldSet[fields[i]] = true
}
var info WHOXInfo
values := msg.Params[1:]
for _, field := range whoxFields {
if !fieldSet[field] {
continue
}
if len(values) == 0 {
return nil, fmt.Errorf("invalid RPL_WHOSPCRPL: missing value for field %q", string(field))
}
info.set(field, values[0])
values = values[1:]
}
return &info, nil
}