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 1.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. // Copyright (c) 2016- Daniel Oaks <daniel@danieloaks.net>
  2. // released under the MIT license
  3. package irc
  4. import (
  5. "fmt"
  6. "strings"
  7. )
  8. // ISupportList holds a list of ISUPPORT tokens
  9. type ISupportList struct {
  10. Tokens map[string]*string
  11. CachedReply []string
  12. }
  13. // NewISupportList returns a new ISupportList
  14. func NewISupportList() *ISupportList {
  15. var il ISupportList
  16. il.Tokens = make(map[string]*string)
  17. il.CachedReply = make([]string, 0)
  18. return &il
  19. }
  20. // Add adds an RPL_ISUPPORT token to our internal list
  21. func (il *ISupportList) Add(name string, value string) {
  22. il.Tokens[name] = &value
  23. }
  24. // AddNoValue adds an RPL_ISUPPORT token that does not have a value
  25. func (il *ISupportList) AddNoValue(name string) {
  26. il.Tokens[name] = nil
  27. }
  28. // RegenerateCachedReply regenerates the cached RPL_ISUPPORT reply
  29. func (il *ISupportList) RegenerateCachedReply() {
  30. il.CachedReply = make([]string, 0)
  31. maxlen := 400 // Max length of a single ISUPPORT token line
  32. var length int // Length of the current cache
  33. var cache []string // Token list cache
  34. for name, value := range il.Tokens {
  35. var token string
  36. if value == nil {
  37. token = name
  38. } else {
  39. token = fmt.Sprintf("%s=%s", name, *value)
  40. }
  41. if len(token)+length <= maxlen {
  42. // account for the space separating tokens
  43. if len(cache) > 0 {
  44. length++
  45. }
  46. cache = append(cache, token)
  47. length += len(token)
  48. }
  49. if len(cache) == 13 || len(token)+length >= maxlen {
  50. il.CachedReply = append(il.CachedReply, strings.Join(cache, " "))
  51. cache = make([]string, 0)
  52. length = 0
  53. }
  54. }
  55. if len(cache) > 0 {
  56. il.CachedReply = append(il.CachedReply, strings.Join(cache, " "))
  57. }
  58. }