xirc: move over WHOX helpers

This commit is contained in:
Simon Ser 2022-05-29 18:33:29 +02:00
parent 9b37bd2eaa
commit f3b06f4236
3 changed files with 89 additions and 84 deletions

View File

@ -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,

80
irc.go
View File

@ -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

85
xirc/whox.go Normal file
View File

@ -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...),
}
}