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

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