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

simplify username logic

This commit is contained in:
Shivaram Lingamneni 2019-02-05 02:40:49 -05:00
parent ea07d99074
commit bf1d758de9
2 changed files with 19 additions and 23 deletions

View File

@ -99,7 +99,6 @@ 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
} }
@ -176,15 +175,9 @@ func NewClient(server *Server, conn net.Conn, isTLS bool) {
client.Notice(client.t("*** Looking up your username")) client.Notice(client.t("*** Looking up your username"))
resp, err := ident.Query(clientHost, serverPort, clientPort, IdentTimeoutSeconds) resp, err := ident.Query(clientHost, serverPort, clientPort, IdentTimeoutSeconds)
if err == nil { if err == nil {
ident := resp.Identifier err := client.SetNames(resp.Identifier, "", true)
if config.Limits.IdentLen < len(ident) { if err == nil {
ident = ident[:config.Limits.IdentLen]
}
if isIdent(ident) {
identLower := strings.ToLower(ident) // idents can only be ASCII chars only
client.Notice(client.t("*** Found your username")) 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 // 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"))
@ -627,20 +620,28 @@ func (client *Client) HasUsername() bool {
} }
// SetNames sets the client's ident and realname. // SetNames sets the client's ident and realname.
func (client *Client) SetNames(username, realname string) error { func (client *Client) SetNames(username, realname string, fromIdent bool) error {
// do this before casefolding to ensure these are actually ascii limit := client.server.Config().Limits.IdentLen
if !fromIdent {
limit -= 1 // leave room for the prepended ~
}
if len(username) > limit {
username = username[:limit]
}
if !isIdent(username) { if !isIdent(username) {
return errInvalidUsername return errInvalidUsername
} }
usernameCasefolded := strings.ToLower(username) // only ascii is supported in idents anyway if !fromIdent {
username = "~" + username
}
client.stateMutex.Lock() client.stateMutex.Lock()
defer client.stateMutex.Unlock() defer client.stateMutex.Unlock()
if client.username == "" { if client.username == "" {
client.username = "~" + username client.username = username
client.usernameCasefolded = "~" + usernameCasefolded
} }
if client.realname == "" { 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.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. // AllNickmasks returns all the possible nickmasks for the client.
func (client *Client) AllNickmasks() (masks []string) { func (client *Client) AllNickmasks() (masks []string) {
client.stateMutex.RLock() client.stateMutex.RLock()
nick := client.nickCasefolded nick := client.nickCasefolded
username := client.usernameCasefolded username := client.username
rawHostname := client.rawHostname rawHostname := client.rawHostname
vhost := client.getVHostNoMutex() vhost := client.getVHostNoMutex()
client.stateMutex.RUnlock() client.stateMutex.RUnlock()
username = strings.ToLower(client.username)
if len(vhost) > 0 { if len(vhost) > 0 {
cfvhost, err := Casefold(vhost) cfvhost, err := Casefold(vhost)

View File

@ -2193,13 +2193,7 @@ func userHandler(server *Server, client *Client, msg ircmsg.IrcMessage, rb *Resp
return false return false
} }
ident := msg.Params[0] err := client.SetNames(msg.Params[0], msg.Params[3], false)
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])
if err == errInvalidUsername { if err == errInvalidUsername {
rb.Add(nil, server.name, ERR_INVALIDUSERNAME, client.t("Malformed username")) rb.Add(nil, server.name, ERR_INVALIDUSERNAME, client.t("Malformed username"))
} }