2016-06-15 13:50:56 +02:00
|
|
|
// Copyright (c) 2012-2014 Jeremy Latt
|
|
|
|
// Copyright (c) 2014-2015 Edmund Huber
|
2017-03-27 14:15:02 +02:00
|
|
|
// Copyright (c) 2016-2017 Daniel Oaks <daniel@danieloaks.net>
|
2016-06-15 13:50:56 +02:00
|
|
|
// released under the MIT license
|
|
|
|
|
2014-03-09 21:45:36 +01:00
|
|
|
package irc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
2016-10-26 16:44:36 +02:00
|
|
|
|
|
|
|
"golang.org/x/text/secure/precis"
|
2014-03-09 21:45:36 +01:00
|
|
|
)
|
|
|
|
|
2017-01-13 17:32:15 +01:00
|
|
|
const (
|
2017-12-26 03:30:04 +01:00
|
|
|
casemappingName = "rfc8265"
|
2017-01-13 17:32:15 +01:00
|
|
|
)
|
|
|
|
|
2016-10-11 15:51:46 +02:00
|
|
|
// Casefold returns a casefolded string, without doing any name or channel character checks.
|
|
|
|
func Casefold(str string) (string, error) {
|
2017-08-17 10:23:24 +02:00
|
|
|
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
|
2014-03-09 21:45:36 +01:00
|
|
|
}
|
|
|
|
|
2016-10-11 15:51:46 +02:00
|
|
|
// CasefoldChannel returns a casefolded version of a channel name.
|
|
|
|
func CasefoldChannel(name string) (string, error) {
|
2017-01-13 17:32:15 +01:00
|
|
|
lowered, err := Casefold(name)
|
2014-03-09 21:45:36 +01:00
|
|
|
|
2016-10-11 15:51:46 +02:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
2017-01-22 03:44:05 +01:00
|
|
|
} else if len(lowered) == 0 {
|
2018-02-03 13:03:36 +01:00
|
|
|
return "", errStringIsEmpty
|
2016-04-21 02:48:15 +02:00
|
|
|
}
|
2014-03-09 21:45:36 +01:00
|
|
|
|
2016-10-11 15:51:46 +02:00
|
|
|
if lowered[0] != '#' {
|
|
|
|
return "", errInvalidCharacter
|
|
|
|
}
|
2014-03-09 21:45:36 +01:00
|
|
|
|
2016-10-11 15:51:46 +02:00
|
|
|
// space can't be used
|
|
|
|
// , is used as a separator
|
|
|
|
// * is used in mask matching
|
|
|
|
// ? is used in mask matching
|
2017-07-26 08:02:35 +02:00
|
|
|
if strings.ContainsAny(lowered, " ,*?") {
|
2016-10-11 15:51:46 +02:00
|
|
|
return "", errInvalidCharacter
|
|
|
|
}
|
2014-03-09 21:45:36 +01:00
|
|
|
|
2016-10-11 15:51:46 +02:00
|
|
|
return lowered, err
|
2014-03-09 21:45:36 +01:00
|
|
|
}
|
|
|
|
|
2016-10-11 15:51:46 +02:00
|
|
|
// CasefoldName returns a casefolded version of a nick/user name.
|
|
|
|
func CasefoldName(name string) (string, error) {
|
2017-01-13 17:32:15 +01:00
|
|
|
lowered, err := Casefold(name)
|
2014-03-09 21:45:36 +01:00
|
|
|
|
2016-10-11 15:51:46 +02:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
2017-01-22 03:44:05 +01:00
|
|
|
} else if len(lowered) == 0 {
|
2018-02-03 13:03:36 +01:00
|
|
|
return "", errStringIsEmpty
|
2016-10-11 15:51:46 +02:00
|
|
|
}
|
2014-03-09 21:45:36 +01:00
|
|
|
|
2016-10-11 15:51:46 +02:00
|
|
|
// 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
|
2017-07-26 08:02:35 +02:00
|
|
|
if strings.ContainsAny(lowered, " ,*?.!@:") || strings.ContainsAny(string(lowered[0]), "#~&@%+-") {
|
2016-10-11 15:51:46 +02:00
|
|
|
return "", errInvalidCharacter
|
|
|
|
}
|
2014-03-09 21:45:36 +01:00
|
|
|
|
2016-10-11 15:51:46 +02:00
|
|
|
return lowered, err
|
2014-03-09 21:45:36 +01:00
|
|
|
}
|