Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

list.go 3.1KB

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