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.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. )
  12. // List holds a list of ISUPPORT tokens
  13. type List struct {
  14. Tokens map[string]*string
  15. CachedReply [][]string
  16. }
  17. // NewList returns a new List
  18. func NewList() *List {
  19. var il List
  20. il.Tokens = make(map[string]*string)
  21. il.CachedReply = make([][]string, 0)
  22. return &il
  23. }
  24. // Add adds an RPL_ISUPPORT token to our internal list
  25. func (il *List) Add(name string, value string) {
  26. il.Tokens[name] = &value
  27. }
  28. // AddNoValue adds an RPL_ISUPPORT token that does not have a value
  29. func (il *List) AddNoValue(name string) {
  30. il.Tokens[name] = nil
  31. }
  32. // getTokenString gets the appropriate string for a token+value.
  33. func getTokenString(name string, value *string) string {
  34. if value == nil {
  35. return name
  36. }
  37. return fmt.Sprintf("%s=%s", name, *value)
  38. }
  39. // GetDifference returns the difference between two token lists.
  40. func (il *List) GetDifference(newil *List) [][]string {
  41. var outTokens sort.StringSlice
  42. // append removed tokens
  43. for name := range il.Tokens {
  44. _, exists := newil.Tokens[name]
  45. if exists {
  46. continue
  47. }
  48. token := fmt.Sprintf("-%s", name)
  49. outTokens = append(outTokens, token)
  50. }
  51. // append added tokens
  52. for name, value := range newil.Tokens {
  53. newval, exists := il.Tokens[name]
  54. if exists && ((value == nil && newval == nil) || (value != nil && newval != nil && *value == *newval)) {
  55. continue
  56. }
  57. token := getTokenString(name, value)
  58. outTokens = append(outTokens, token)
  59. }
  60. sort.Sort(outTokens)
  61. // create output list
  62. replies := make([][]string, 0)
  63. var length int // Length of the current cache
  64. var cache []string // Token list cache
  65. for _, token := range outTokens {
  66. if len(token)+length <= maxLastArgLength {
  67. // account for the space separating tokens
  68. if len(cache) > 0 {
  69. length++
  70. }
  71. cache = append(cache, token)
  72. length += len(token)
  73. }
  74. if len(cache) == 13 || len(token)+length >= maxLastArgLength {
  75. replies = append(replies, cache)
  76. cache = make([]string, 0)
  77. length = 0
  78. }
  79. }
  80. if len(cache) > 0 {
  81. replies = append(replies, cache)
  82. }
  83. return replies
  84. }
  85. // RegenerateCachedReply regenerates the cached RPL_ISUPPORT reply
  86. func (il *List) RegenerateCachedReply() (err error) {
  87. il.CachedReply = make([][]string, 0)
  88. var length int // Length of the current cache
  89. var cache []string // Token list cache
  90. // make sure we get a sorted list of tokens, needed for tests and looks nice
  91. var tokens sort.StringSlice
  92. for name := range il.Tokens {
  93. tokens = append(tokens, name)
  94. }
  95. sort.Sort(tokens)
  96. for _, name := range tokens {
  97. token := getTokenString(name, il.Tokens[name])
  98. if token[0] == ':' || strings.Contains(token, " ") {
  99. err = fmt.Errorf("bad isupport token (cannot contain spaces or start with :): %s", token)
  100. continue
  101. }
  102. if len(token)+length <= maxLastArgLength {
  103. // account for the space separating tokens
  104. if len(cache) > 0 {
  105. length++
  106. }
  107. cache = append(cache, token)
  108. length += len(token)
  109. }
  110. if len(cache) == 13 || len(token)+length >= maxLastArgLength {
  111. il.CachedReply = append(il.CachedReply, cache)
  112. cache = make([]string, 0)
  113. length = 0
  114. }
  115. }
  116. if len(cache) > 0 {
  117. il.CachedReply = append(il.CachedReply, cache)
  118. }
  119. return
  120. }