From f3b06f4236c115d4e8cc829da5964c1735aed0fa Mon Sep 17 00:00:00 2001 From: Simon Ser Date: Sun, 29 May 2022 18:33:29 +0200 Subject: [PATCH] xirc: move over WHOX helpers --- downstream.go | 8 ++--- irc.go | 80 ------------------------------------------------ xirc/whox.go | 85 +++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 89 insertions(+), 84 deletions(-) create mode 100644 xirc/whox.go diff --git a/downstream.go b/downstream.go index d4f1bc2..a008c0d 100644 --- a/downstream.go +++ b/downstream.go @@ -2266,7 +2266,7 @@ func (dc *downstreamConn) handleMessageRegistered(ctx context.Context, msg *irc. if dc.user.Admin { flags += "*" } - info := whoxInfo{ + info := xirc.WHOXInfo{ Token: whoxToken, Username: dc.user.Username, Hostname: dc.hostname, @@ -2276,7 +2276,7 @@ func (dc *downstreamConn) handleMessageRegistered(ctx context.Context, msg *irc. Account: dc.user.Username, Realname: dc.realname, } - dc.SendMessage(generateWHOXReply(dc.srv.prefix(), dc.nick, fields, &info)) + dc.SendMessage(xirc.GenerateWHOXReply(dc.srv.prefix(), dc.nick, fields, &info)) dc.SendMessage(&irc.Message{ Prefix: dc.srv.prefix(), Command: irc.RPL_ENDOFWHO, @@ -2293,7 +2293,7 @@ func (dc *downstreamConn) handleMessageRegistered(ctx context.Context, msg *irc. flags += *v } } - info := whoxInfo{ + info := xirc.WHOXInfo{ Token: whoxToken, Username: servicePrefix.User, Hostname: servicePrefix.Host, @@ -2303,7 +2303,7 @@ func (dc *downstreamConn) handleMessageRegistered(ctx context.Context, msg *irc. Account: serviceNick, Realname: serviceRealname, } - dc.SendMessage(generateWHOXReply(dc.srv.prefix(), dc.nick, fields, &info)) + dc.SendMessage(xirc.GenerateWHOXReply(dc.srv.prefix(), dc.nick, fields, &info)) dc.SendMessage(&irc.Message{ Prefix: dc.srv.prefix(), Command: irc.RPL_ENDOFWHO, diff --git a/irc.go b/irc.go index 70e89ca..a56fc23 100644 --- a/irc.go +++ b/irc.go @@ -512,86 +512,6 @@ 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 - Hostname string - Server string - Nickname string - Flags string - Account string - 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{ - Prefix: prefix, - Command: irc.RPL_WHOREPLY, - Params: []string{nick, "*", info.Username, info.Hostname, info.Server, info.Nickname, info.Flags, "0 " + info.Realname}, - } - } - - fieldSet := make(map[byte]bool) - for i := 0; i < len(fields); i++ { - fieldSet[fields[i]] = true - } - - var values []string - for _, field := range whoxFields { - if !fieldSet[field] { - continue - } - values = append(values, info.get(field)) - } - - return &irc.Message{ - Prefix: prefix, - Command: xirc.RPL_WHOSPCRPL, - Params: append([]string{nick}, values...), - } -} - func isNumeric(cmd string) bool { if len(cmd) != 3 { return false diff --git a/xirc/whox.go b/xirc/whox.go new file mode 100644 index 0000000..7fb06b4 --- /dev/null +++ b/xirc/whox.go @@ -0,0 +1,85 @@ +package xirc + +import ( + "gopkg.in/irc.v3" +) + +// 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 + Hostname string + Server string + Nickname string + Flags string + Account string + 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{ + Prefix: prefix, + Command: irc.RPL_WHOREPLY, + Params: []string{nick, "*", info.Username, info.Hostname, info.Server, info.Nickname, info.Flags, "0 " + info.Realname}, + } + } + + fieldSet := make(map[byte]bool) + for i := 0; i < len(fields); i++ { + fieldSet[fields[i]] = true + } + + var values []string + for _, field := range whoxFields { + if !fieldSet[field] { + continue + } + values = append(values, info.get(field)) + } + + return &irc.Message{ + Prefix: prefix, + Command: RPL_WHOSPCRPL, + Params: append([]string{nick}, values...), + } +}