Changeset 778 in code


Ignore:
Timestamp:
Feb 9, 2022, 2:16:54 PM (3 years ago)
Author:
contact
Message:

Refactor generateWHOXReply

Isolate the field letter -> value logic into a separate function.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/irc.go

    r761 r778  
    729729}
    730730
     731// whoxFields is the list of all WHOX field letters, by order of appearance in
     732// RPL_WHOSPCRPL messages.
     733var whoxFields = []byte("tcuihsnfdlaor")
     734
    731735type whoxInfo struct {
    732736        Token    string
     
    740744}
    741745
     746func (info *whoxInfo) get(field byte) string {
     747        switch field {
     748        case 't':
     749                return info.Token
     750        case 'c':
     751                return "*"
     752        case 'u':
     753                return info.Username
     754        case 'i':
     755                return "255.255.255.255"
     756        case 'h':
     757                return info.Hostname
     758        case 's':
     759                return info.Server
     760        case 'n':
     761                return info.Nickname
     762        case 'f':
     763                return info.Flags
     764        case 'd':
     765                return "0"
     766        case 'l': // idle time
     767                return "0"
     768        case 'a':
     769                account := "0" // WHOX uses "0" to mean "no account"
     770                if info.Account != "" && info.Account != "*" {
     771                        account = info.Account
     772                }
     773                return account
     774        case 'o':
     775                return "0"
     776        case 'r':
     777                return info.Realname
     778        }
     779        return ""
     780}
     781
    742782func generateWHOXReply(prefix *irc.Prefix, nick, fields string, info *whoxInfo) *irc.Message {
    743783        if fields == "" {
     
    754794        }
    755795
    756         var params []string
    757         if fieldSet['t'] {
    758                 params = append(params, info.Token)
    759         }
    760         if fieldSet['c'] {
    761                 params = append(params, "*")
    762         }
    763         if fieldSet['u'] {
    764                 params = append(params, info.Username)
    765         }
    766         if fieldSet['i'] {
    767                 params = append(params, "255.255.255.255")
    768         }
    769         if fieldSet['h'] {
    770                 params = append(params, info.Hostname)
    771         }
    772         if fieldSet['s'] {
    773                 params = append(params, info.Server)
    774         }
    775         if fieldSet['n'] {
    776                 params = append(params, info.Nickname)
    777         }
    778         if fieldSet['f'] {
    779                 params = append(params, info.Flags)
    780         }
    781         if fieldSet['d'] {
    782                 params = append(params, "0")
    783         }
    784         if fieldSet['l'] { // idle time
    785                 params = append(params, "0")
    786         }
    787         if fieldSet['a'] {
    788                 account := "0" // WHOX uses "0" to mean "no account"
    789                 if info.Account != "" && info.Account != "*" {
    790                         account = info.Account
    791                 }
    792                 params = append(params, account)
    793         }
    794         if fieldSet['o'] {
    795                 params = append(params, "0")
    796         }
    797         if fieldSet['r'] {
    798                 params = append(params, info.Realname)
     796        var values []string
     797        for _, field := range whoxFields {
     798                if !fieldSet[field] {
     799                        continue
     800                }
     801                values = append(values, info.get(field))
    799802        }
    800803
     
    802805                Prefix:  prefix,
    803806                Command: rpl_whospcrpl,
    804                 Params:  append([]string{nick}, params...),
     807                Params:  append([]string{nick}, values...),
    805808        }
    806809}
Note: See TracChangeset for help on using the changeset viewer.