3
0
mirror of https://github.com/ergochat/ergo.git synced 2024-11-11 06:29:29 +01:00
ergo/irc/strings.go
Shivaram Lingamneni 40e63dbbe8 fix #306
Fix spurious bidi rule violations in casefolding channel names
by stripping the # before starting the casefolding.
2018-12-05 22:39:01 -05:00

99 lines
2.2 KiB
Go

// Copyright (c) 2012-2014 Jeremy Latt
// Copyright (c) 2014-2015 Edmund Huber
// Copyright (c) 2016-2017 Daniel Oaks <daniel@danieloaks.net>
// released under the MIT license
package irc
import (
"strings"
"golang.org/x/text/secure/precis"
)
const (
casemappingName = "rfc8265"
)
// Casefold returns a casefolded string, without doing any name or channel character checks.
func Casefold(str string) (string, error) {
var err error
oldStr := str
// follow the stabilizing rules laid out here:
// https://tools.ietf.org/html/draft-ietf-precis-7564bis-10.html#section-7
for i := 0; i < 4; i++ {
str, err = precis.UsernameCaseMapped.CompareKey(str)
if err != nil {
return "", err
}
if oldStr == str {
break
}
oldStr = str
}
if oldStr != str {
return "", errCouldNotStabilize
}
return str, nil
}
// CasefoldChannel returns a casefolded version of a channel name.
func CasefoldChannel(name string) (string, error) {
if len(name) == 0 {
return "", errStringIsEmpty
}
// don't casefold the preceding #'s
var start int
for start = 0; start < len(name) && name[start] == '#'; start += 1 {
}
if start == 0 {
// no preceding #'s
return "", errInvalidCharacter
}
lowered, err := Casefold(name[start:])
if err != nil {
return "", err
}
// space can't be used
// , is used as a separator
// * is used in mask matching
// ? is used in mask matching
if strings.ContainsAny(lowered, " ,*?") {
return "", errInvalidCharacter
}
return name[:start] + lowered, err
}
// CasefoldName returns a casefolded version of a nick/user name.
func CasefoldName(name string) (string, error) {
lowered, err := Casefold(name)
if err != nil {
return "", err
} else if len(lowered) == 0 {
return "", errStringIsEmpty
}
// space can't be used
// , is used as a separator
// * is used in mask matching
// ? is used in mask matching
// . denotes a server name
// ! separates nickname from username
// @ separates username from hostname
// : means trailing
// # is a channel prefix
// ~&@%+ are channel membership prefixes
// - I feel like disallowing
if strings.ContainsAny(lowered, " ,*?.!@:") || strings.ContainsAny(string(lowered[0]), "#~&@%+-") {
return "", errInvalidCharacter
}
return lowered, err
}