Refactor generateWHOXReply

Isolate the field letter -> value logic into a separate function.
This commit is contained in:
Simon Ser 2022-02-09 15:16:54 +01:00
parent e72c896bb4
commit 009bc29e09

89
irc.go
View File

@ -728,6 +728,10 @@ func parseChatHistoryBound(param string) time.Time {
}
}
// whoxFields is the list of all WHOX field letters, by order of appearance in
// RPL_WHOSPCRPL messages.
var whoxFields = []byte("tcuihsnfdlaor")
type whoxInfo struct {
Token string
Username string
@ -739,6 +743,42 @@ type whoxInfo struct {
Realname string
}
func (info *whoxInfo) get(field byte) string {
switch field {
case 't':
return info.Token
case 'c':
return "*"
case 'u':
return info.Username
case 'i':
return "255.255.255.255"
case 'h':
return info.Hostname
case 's':
return info.Server
case 'n':
return info.Nickname
case 'f':
return info.Flags
case 'd':
return "0"
case 'l': // idle time
return "0"
case 'a':
account := "0" // WHOX uses "0" to mean "no account"
if info.Account != "" && info.Account != "*" {
account = info.Account
}
return account
case 'o':
return "0"
case 'r':
return info.Realname
}
return ""
}
func generateWHOXReply(prefix *irc.Prefix, nick, fields string, info *whoxInfo) *irc.Message {
if fields == "" {
return &irc.Message{
@ -753,55 +793,18 @@ func generateWHOXReply(prefix *irc.Prefix, nick, fields string, info *whoxInfo)
fieldSet[fields[i]] = true
}
var params []string
if fieldSet['t'] {
params = append(params, info.Token)
var values []string
for _, field := range whoxFields {
if !fieldSet[field] {
continue
}
if fieldSet['c'] {
params = append(params, "*")
}
if fieldSet['u'] {
params = append(params, info.Username)
}
if fieldSet['i'] {
params = append(params, "255.255.255.255")
}
if fieldSet['h'] {
params = append(params, info.Hostname)
}
if fieldSet['s'] {
params = append(params, info.Server)
}
if fieldSet['n'] {
params = append(params, info.Nickname)
}
if fieldSet['f'] {
params = append(params, info.Flags)
}
if fieldSet['d'] {
params = append(params, "0")
}
if fieldSet['l'] { // idle time
params = append(params, "0")
}
if fieldSet['a'] {
account := "0" // WHOX uses "0" to mean "no account"
if info.Account != "" && info.Account != "*" {
account = info.Account
}
params = append(params, account)
}
if fieldSet['o'] {
params = append(params, "0")
}
if fieldSet['r'] {
params = append(params, info.Realname)
values = append(values, info.get(field))
}
return &irc.Message{
Prefix: prefix,
Command: rpl_whospcrpl,
Params: append([]string{nick}, params...),
Params: append([]string{nick}, values...),
}
}