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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. // Copyright (c) 2016 Daniel Oaks <daniel@danieloaks.net>
  2. // released under the MIT license
  3. package isupport
  4. import "fmt"
  5. import "sort"
  6. const (
  7. maxLastArgLength = 400
  8. supportedString = "are supported by this server"
  9. )
  10. // List holds a list of ISUPPORT tokens
  11. type List struct {
  12. Tokens map[string]*string
  13. CachedReply [][]string
  14. }
  15. // NewList returns a new List
  16. func NewList() *List {
  17. var il List
  18. il.Tokens = make(map[string]*string)
  19. il.CachedReply = make([][]string, 0)
  20. return &il
  21. }
  22. // Add adds an RPL_ISUPPORT token to our internal list
  23. func (il *List) Add(name string, value string) {
  24. il.Tokens[name] = &value
  25. }
  26. // AddNoValue adds an RPL_ISUPPORT token that does not have a value
  27. func (il *List) AddNoValue(name string) {
  28. il.Tokens[name] = nil
  29. }
  30. // getTokenString gets the appropriate string for a token+value.
  31. func getTokenString(name string, value *string) string {
  32. if value == nil {
  33. return name
  34. }
  35. return fmt.Sprintf("%s=%s", name, *value)
  36. }
  37. // GetDifference returns the difference between two token lists.
  38. func (il *List) GetDifference(newil *List) [][]string {
  39. var outTokens sort.StringSlice
  40. // append removed tokens
  41. for name := range il.Tokens {
  42. _, exists := newil.Tokens[name]
  43. if exists {
  44. continue
  45. }
  46. token := fmt.Sprintf("-%s", name)
  47. outTokens = append(outTokens, token)
  48. }
  49. // append added tokens
  50. for name, value := range newil.Tokens {
  51. newval, exists := il.Tokens[name]
  52. if exists && ((value == nil && newval == nil) || (value != nil && newval != nil && *value == *newval)) {
  53. continue
  54. }
  55. token := getTokenString(name, value)
  56. outTokens = append(outTokens, token)
  57. }
  58. sort.Sort(outTokens)
  59. // create output list
  60. replies := make([][]string, 0)
  61. var length int // Length of the current cache
  62. var cache []string // Token list cache
  63. for _, token := range outTokens {
  64. if len(token)+length <= maxLastArgLength {
  65. // account for the space separating tokens
  66. if len(cache) > 0 {
  67. length++
  68. }
  69. cache = append(cache, token)
  70. length += len(token)
  71. }
  72. if len(cache) == 13 || len(token)+length >= maxLastArgLength {
  73. cache = append(cache, supportedString)
  74. replies = append(replies, cache)
  75. cache = make([]string, 0)
  76. length = 0
  77. }
  78. }
  79. if len(cache) > 0 {
  80. cache = append(cache, supportedString)
  81. replies = append(replies, cache)
  82. }
  83. return replies
  84. }
  85. // RegenerateCachedReply regenerates the cached RPL_ISUPPORT reply
  86. func (il *List) RegenerateCachedReply() {
  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 len(token)+length <= maxLastArgLength {
  99. // account for the space separating tokens
  100. if len(cache) > 0 {
  101. length++
  102. }
  103. cache = append(cache, token)
  104. length += len(token)
  105. }
  106. if len(cache) == 13 || len(token)+length >= maxLastArgLength {
  107. cache = append(cache, supportedString)
  108. il.CachedReply = append(il.CachedReply, cache)
  109. cache = make([]string, 0)
  110. length = 0
  111. }
  112. }
  113. if len(cache) > 0 {
  114. cache = append(cache, supportedString)
  115. il.CachedReply = append(il.CachedReply, cache)
  116. }
  117. }