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.

isupport.go 3.7KB

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