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

Merge pull request #346 from slingamn/identfixup.2

simplify username logic
This commit is contained in:
Daniel Oaks 2019-02-05 18:09:06 +10:00 committed by GitHub
commit 35fadd10fa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 23 deletions

View File

@ -99,7 +99,6 @@ type Client struct {
socket *Socket
stateMutex sync.RWMutex // tier 1
username string
usernameCasefolded string
vhost string
history *history.Buffer
}
@ -176,15 +175,9 @@ func NewClient(server *Server, conn net.Conn, isTLS bool) {
client.Notice(client.t("*** Looking up your username"))
resp, err := ident.Query(clientHost, serverPort, clientPort, IdentTimeoutSeconds)
if err == nil {
ident := resp.Identifier
if config.Limits.IdentLen < len(ident) {
ident = ident[:config.Limits.IdentLen]
}
if isIdent(ident) {
identLower := strings.ToLower(ident) // idents can only be ASCII chars only
err := client.SetNames(resp.Identifier, "", true)
if err == nil {
client.Notice(client.t("*** Found your username"))
client.username = ident
client.usernameCasefolded = identLower
// we don't need to updateNickMask here since nickMask is not used for anything yet
} else {
client.Notice(client.t("*** Got a malformed username, ignoring"))
@ -627,20 +620,28 @@ func (client *Client) HasUsername() bool {
}
// SetNames sets the client's ident and realname.
func (client *Client) SetNames(username, realname string) error {
// do this before casefolding to ensure these are actually ascii
func (client *Client) SetNames(username, realname string, fromIdent bool) error {
limit := client.server.Config().Limits.IdentLen
if !fromIdent {
limit -= 1 // leave room for the prepended ~
}
if limit < len(username) {
username = username[:limit]
}
if !isIdent(username) {
return errInvalidUsername
}
usernameCasefolded := strings.ToLower(username) // only ascii is supported in idents anyway
if !fromIdent {
username = "~" + username
}
client.stateMutex.Lock()
defer client.stateMutex.Unlock()
if client.username == "" {
client.username = "~" + username
client.usernameCasefolded = "~" + usernameCasefolded
client.username = username
}
if client.realname == "" {
@ -777,17 +778,18 @@ func (client *Client) updateNickMaskNoMutex() {
}
client.nickMaskString = fmt.Sprintf("%s!%s@%s", client.nick, client.username, client.hostname)
client.nickMaskCasefolded = fmt.Sprintf("%s!%s@%s", client.nickCasefolded, client.usernameCasefolded, cfhostname)
client.nickMaskCasefolded = fmt.Sprintf("%s!%s@%s", client.nickCasefolded, strings.ToLower(client.username), cfhostname)
}
// AllNickmasks returns all the possible nickmasks for the client.
func (client *Client) AllNickmasks() (masks []string) {
client.stateMutex.RLock()
nick := client.nickCasefolded
username := client.usernameCasefolded
username := client.username
rawHostname := client.rawHostname
vhost := client.getVHostNoMutex()
client.stateMutex.RUnlock()
username = strings.ToLower(username)
if len(vhost) > 0 {
cfvhost, err := Casefold(vhost)

View File

@ -2193,13 +2193,7 @@ func userHandler(server *Server, client *Client, msg ircmsg.IrcMessage, rb *Resp
return false
}
ident := msg.Params[0]
identLen := server.Limits().IdentLen
if identLen-1 < len(ident) {
ident = ident[:server.Limits().IdentLen-1] // -1 as SetNames adds the ~ at the start for us
}
err := client.SetNames(ident, msg.Params[3])
err := client.SetNames(msg.Params[0], msg.Params[3], false)
if err == errInvalidUsername {
rb.Add(nil, server.name, ERR_INVALIDUSERNAME, client.t("Malformed username"))
}