3
0
mirror of https://github.com/ergochat/ergo.git synced 2024-11-25 13:29:27 +01:00

Merge pull request #1193 from slingamn/whox.2

refactor WhoFields to use value receivers
This commit is contained in:
Shivaram Lingamneni 2020-07-12 13:41:49 -07:00 committed by GitHub
commit ad32a01c12
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 47 additions and 15 deletions

View File

@ -56,3 +56,33 @@ func TestUserMasks(t *testing.T) {
t.Error("failure to match") t.Error("failure to match")
} }
} }
func TestWhoFields(t *testing.T) {
var w whoxFields
if w.Has('a') {
t.Error("zero value of whoxFields must be empty")
}
w = w.Add('a')
if !w.Has('a') {
t.Error("failed to set and get")
}
if w.Has('A') {
t.Error("false positive")
}
if w.Has('o') {
t.Error("false positive")
}
w = w.Add('🐬')
if w.Has('🐬') {
t.Error("should not be able to set invalid who field")
}
w = w.Add('o')
if !w.Has('o') {
t.Error("failed to set and get")
}
w = w.Add('z')
if !w.Has('z') {
t.Error("failed to set and get")
}
}

View File

@ -2826,30 +2826,32 @@ func webircHandler(server *Server, client *Client, msg ircmsg.IrcMessage, rb *Re
return true return true
} }
const WhoFieldMinimum = int('a') // lowest rune value type whoxFields uint32 // bitset to hold the WHOX field values, 'a' through 'z'
const WhoFieldMaximum = int('z')
type WhoFields [WhoFieldMaximum - WhoFieldMinimum + 1]bool func (fields whoxFields) Add(field rune) (result whoxFields) {
index := int(field) - int('a')
if 0 <= index && index < 26 {
return fields | (1 << index)
} else {
return fields
}
}
func (fields *WhoFields) Set(field rune) bool { func (fields whoxFields) Has(field rune) bool {
index := int(field) index := int(field) - int('a')
if WhoFieldMinimum <= index && index <= WhoFieldMaximum { if 0 <= index && index < 26 {
fields[int(field)-WhoFieldMinimum] = true return (fields & (1 << index)) != 0
return true
} else { } else {
return false return false
} }
} }
func (fields *WhoFields) Has(field rune) bool {
return fields[int(field)-WhoFieldMinimum]
}
// rplWhoReply returns the WHO(X) reply between one user and another channel/user. // rplWhoReply returns the WHO(X) reply between one user and another channel/user.
// who format: // who format:
// <channel> <user> <host> <server> <nick> <H|G>[*][~|&|@|%|+][B] :<hopcount> <real name> // <channel> <user> <host> <server> <nick> <H|G>[*][~|&|@|%|+][B] :<hopcount> <real name>
// whox format: // whox format:
// <type> <channel> <user> <ip> <host> <server> <nick> <H|G>[*][~|&|@|%|+][B] <hops> <idle> <account> <rank> :<real name> // <type> <channel> <user> <ip> <host> <server> <nick> <H|G>[*][~|&|@|%|+][B] <hops> <idle> <account> <rank> :<real name>
func (client *Client) rplWhoReply(channel *Channel, target *Client, rb *ResponseBuffer, isWhox bool, fields WhoFields, whoType string) { func (client *Client) rplWhoReply(channel *Channel, target *Client, rb *ResponseBuffer, isWhox bool, fields whoxFields, whoType string) {
params := []string{client.Nick()} params := []string{client.Nick()}
details := target.Details() details := target.Details()
@ -2897,7 +2899,7 @@ func (client *Client) rplWhoReply(channel *Channel, target *Client, rb *Response
} }
if channel != nil { if channel != nil {
flags.WriteString(channel.ClientPrefixes(target, false)) flags.WriteString(channel.ClientPrefixes(target, rb.session.capabilities.Has(caps.MultiPrefix)))
} }
if target.HasMode(modes.Bot) { if target.HasMode(modes.Bot) {
@ -2972,9 +2974,9 @@ func whoHandler(server *Server, client *Client, msg ircmsg.IrcMessage, rb *Respo
sFields = strings.ToLower(sFields[:typeIndex]) sFields = strings.ToLower(sFields[:typeIndex])
} }
} }
var fields WhoFields var fields whoxFields
for _, field := range sFields { for _, field := range sFields {
fields.Set(field) fields = fields.Add(field)
} }
//TODO(dan): is this used and would I put this param in the Modern doc? //TODO(dan): is this used and would I put this param in the Modern doc?