You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

list.go 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. // Copyright (c) 2016 Daniel Oaks <daniel@danieloaks.net>
  2. // released under the MIT license
  3. package isupport
  4. import (
  5. "fmt"
  6. "sort"
  7. "strings"
  8. )
  9. const (
  10. maxLastArgLength = 400
  11. /* Modern: "As the maximum number of message parameters to any reply is 15,
  12. the maximum number of RPL_ISUPPORT tokens that can be advertised is 13."
  13. <nickname> [up to 13 parameters] <human-readable trailing>
  14. */
  15. maxParameters = 13
  16. )
  17. // List holds a list of ISUPPORT tokens
  18. type List struct {
  19. Tokens map[string]string
  20. CachedReply [][]string
  21. }
  22. // NewList returns a new List
  23. func NewList() *List {
  24. var il List
  25. il.Initialize()
  26. return &il
  27. }
  28. func (il *List) Initialize() {
  29. il.Tokens = make(map[string]string)
  30. il.CachedReply = make([][]string, 0)
  31. }
  32. // Add adds an RPL_ISUPPORT token to our internal list
  33. func (il *List) Add(name string, value string) {
  34. il.Tokens[name] = value
  35. }
  36. // AddNoValue adds an RPL_ISUPPORT token that does not have a value
  37. func (il *List) AddNoValue(name string) {
  38. il.Tokens[name] = ""
  39. }
  40. // getTokenString gets the appropriate string for a token+value.
  41. func getTokenString(name string, value string) string {
  42. if len(value) == 0 {
  43. return name
  44. }
  45. return fmt.Sprintf("%s=%s", name, value)
  46. }
  47. // GetDifference returns the difference between two token lists.
  48. func (il *List) GetDifference(newil *List) [][]string {
  49. var outTokens sort.StringSlice
  50. // append removed tokens
  51. for name := range il.Tokens {
  52. _, exists := newil.Tokens[name]
  53. if exists {
  54. continue
  55. }
  56. token := fmt.Sprintf("-%s", name)
  57. outTokens = append(outTokens, token)
  58. }
  59. // append added tokens
  60. for name, value := range newil.Tokens {
  61. newval, exists := il.Tokens[name]
  62. if exists && value == newval {
  63. continue
  64. }
  65. token := getTokenString(name, value)
  66. outTokens = append(outTokens, token)
  67. }
  68. sort.Sort(outTokens)
  69. // create output list
  70. replies := make([][]string, 0)
  71. var length int // Length of the current cache
  72. var cache []string // Token list cache
  73. for _, token := range outTokens {
  74. if len(token)+length <= maxLastArgLength {
  75. // account for the space separating tokens
  76. if len(cache) > 0 {
  77. length++
  78. }
  79. cache = append(cache, token)
  80. length += len(token)
  81. }
  82. if len(cache) == maxParameters || len(token)+length >= maxLastArgLength {
  83. replies = append(replies, cache)
  84. cache = make([]string, 0)
  85. length = 0
  86. }
  87. }
  88. if len(cache) > 0 {
  89. replies = append(replies, cache)
  90. }
  91. return replies
  92. }
  93. // RegenerateCachedReply regenerates the cached RPL_ISUPPORT reply
  94. func (il *List) RegenerateCachedReply() (err error) {
  95. il.CachedReply = make([][]string, 0)
  96. var length int // Length of the current cache
  97. var cache []string // Token list cache
  98. // make sure we get a sorted list of tokens, needed for tests and looks nice
  99. var tokens sort.StringSlice
  100. for name := range il.Tokens {
  101. tokens = append(tokens, name)
  102. }
  103. sort.Sort(tokens)
  104. for _, name := range tokens {
  105. token := getTokenString(name, il.Tokens[name])
  106. if token[0] == ':' || strings.Contains(token, " ") {
  107. err = fmt.Errorf("bad isupport token (cannot contain spaces or start with :): %s", token)
  108. continue
  109. }
  110. if len(token)+length <= maxLastArgLength {
  111. // account for the space separating tokens
  112. if len(cache) > 0 {
  113. length++
  114. }
  115. cache = append(cache, token)
  116. length += len(token)
  117. }
  118. if len(cache) == maxParameters || len(token)+length >= maxLastArgLength {
  119. il.CachedReply = append(il.CachedReply, cache)
  120. cache = make([]string, 0)
  121. length = 0
  122. }
  123. }
  124. if len(cache) > 0 {
  125. il.CachedReply = append(il.CachedReply, cache)
  126. }
  127. return
  128. }