xirc: Fix sending hostnames starting with ':' in WHO replies

Some IPv6 hostnames can start with a colon (eg '::1'). This breaks
the IRC line format.

To work around this issue, prefix the hostname with a '0'. This
changes the representation of the IP but not its value.

References: https://todo.sr.ht/~taiite/senpai/109
Co-authored-by: Simon Ser <contact@emersion.fr>
This commit is contained in:
delthas 2023-01-21 14:59:26 +01:00 committed by Simon Ser
parent 959baa964f
commit 8b558e39b7

View File

@ -33,7 +33,14 @@ func (info *WHOXInfo) get(k byte) string {
case 'i': case 'i':
return "255.255.255.255" return "255.255.255.255"
case 'h': case 'h':
return info.Hostname 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
case 's': case 's':
return info.Server return info.Server
case 'n': case 'n':
@ -81,10 +88,18 @@ func (info *WHOXInfo) set(k byte, v string) {
func GenerateWHOXReply(prefix *irc.Prefix, nick, fields string, info *WHOXInfo) *irc.Message { func GenerateWHOXReply(prefix *irc.Prefix, nick, fields string, info *WHOXInfo) *irc.Message {
if fields == "" { 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
}
return &irc.Message{ return &irc.Message{
Prefix: prefix, Prefix: prefix,
Command: irc.RPL_WHOREPLY, Command: irc.RPL_WHOREPLY,
Params: []string{nick, "*", info.Username, info.Hostname, info.Server, info.Nickname, info.Flags, "0 " + info.Realname}, Params: []string{nick, "*", info.Username, hostname, info.Server, info.Nickname, info.Flags, "0 " + info.Realname},
} }
} }