3
0
mirror of https://github.com/ergochat/ergo.git synced 2024-11-10 22:19:31 +01:00

Merge pull request #332 from slingamn/rtlnickmasks

fix spurious bidi violations when casefolding the nickmask of an RTL nick
This commit is contained in:
Shivaram Lingamneni 2019-01-29 17:46:32 -05:00 committed by GitHub
commit a11486d699
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -98,6 +98,7 @@ type Client struct {
socket *Socket socket *Socket
stateMutex sync.RWMutex // tier 1 stateMutex sync.RWMutex // tier 1
username string username string
usernameCasefolded string
vhost string vhost string
history *history.Buffer history *history.Buffer
} }
@ -175,10 +176,11 @@ func NewClient(server *Server, conn net.Conn, isTLS bool) {
resp, err := ident.Query(clientHost, serverPort, clientPort, IdentTimeoutSeconds) resp, err := ident.Query(clientHost, serverPort, clientPort, IdentTimeoutSeconds)
if err == nil { if err == nil {
username := resp.Identifier username := resp.Identifier
_, err := CasefoldName(username) // ensure it's a valid username cfusername, err := CasefoldName(username)
if err == nil { if err == nil {
client.Notice(client.t("*** Found your username")) client.Notice(client.t("*** Found your username"))
client.username = username client.username = username
client.usernameCasefolded = cfusername
// we don't need to updateNickMask here since nickMask is not used for anything yet // we don't need to updateNickMask here since nickMask is not used for anything yet
} else { } else {
client.Notice(client.t("*** Got a malformed username, ignoring")) client.Notice(client.t("*** Got a malformed username, ignoring"))
@ -619,7 +621,7 @@ func (client *Client) HasUsername() bool {
} }
func (client *Client) SetNames(username, realname string) error { func (client *Client) SetNames(username, realname string) error {
_, err := CasefoldName(username) usernameCasefolded, err := CasefoldName(username)
if err != nil { if err != nil {
return errInvalidUsername return errInvalidUsername
} }
@ -629,6 +631,7 @@ func (client *Client) SetNames(username, realname string) error {
if client.username == "" { if client.username == "" {
client.username = "~" + username client.username = "~" + username
client.usernameCasefolded = "~" + usernameCasefolded
} }
if client.realname == "" { if client.realname == "" {
@ -759,48 +762,45 @@ func (client *Client) updateNickMaskNoMutex() {
client.hostname = client.rawHostname client.hostname = client.rawHostname
} }
nickMaskString := fmt.Sprintf("%s!%s@%s", client.nick, client.username, client.hostname) cfhostname, err := Casefold(client.hostname)
nickMaskCasefolded, err := Casefold(nickMaskString)
if err != nil { if err != nil {
client.server.logger.Error("internal", "nickmask couldn't be casefolded", nickMaskString, err.Error()) client.server.logger.Error("internal", "hostname couldn't be casefolded", client.hostname, err.Error())
return cfhostname = client.hostname // YOLO
} }
client.nickMaskString = nickMaskString client.nickMaskString = fmt.Sprintf("%s!%s@%s", client.nick, client.username, client.hostname)
client.nickMaskCasefolded = nickMaskCasefolded client.nickMaskCasefolded = fmt.Sprintf("%s!%s@%s", client.nickCasefolded, client.usernameCasefolded, cfhostname)
} }
// AllNickmasks returns all the possible nickmasks for the client. // AllNickmasks returns all the possible nickmasks for the client.
func (client *Client) AllNickmasks() []string { func (client *Client) AllNickmasks() (masks []string) {
var masks []string
var mask string
var err error
client.stateMutex.RLock() client.stateMutex.RLock()
nick := client.nick nick := client.nickCasefolded
username := client.username username := client.usernameCasefolded
rawHostname := client.rawHostname rawHostname := client.rawHostname
vhost := client.getVHostNoMutex() vhost := client.getVHostNoMutex()
client.stateMutex.RUnlock() client.stateMutex.RUnlock()
if len(vhost) > 0 { if len(vhost) > 0 {
mask, err = Casefold(fmt.Sprintf("%s!%s@%s", nick, username, vhost)) cfvhost, err := Casefold(vhost)
if err == nil { if err == nil {
masks = append(masks, mask) masks = append(masks, fmt.Sprintf("%s!%s@%s", nick, username, cfvhost))
} }
} }
mask, err = Casefold(fmt.Sprintf("%s!%s@%s", nick, username, rawHostname)) var rawhostmask string
cfrawhost, err := Casefold(rawHostname)
if err == nil { if err == nil {
masks = append(masks, mask) rawhostmask = fmt.Sprintf("%s!%s@%s", nick, username, cfrawhost)
masks = append(masks, rawhostmask)
} }
mask2, err := Casefold(fmt.Sprintf("%s!%s@%s", nick, username, client.IPString())) ipmask := fmt.Sprintf("%s!%s@%s", nick, username, client.IPString())
if err == nil && mask2 != mask { if ipmask != rawhostmask {
masks = append(masks, mask2) masks = append(masks, ipmask)
} }
return masks return
} }
// LoggedIntoAccount returns true if this client is logged into an account. // LoggedIntoAccount returns true if this client is logged into an account.