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ů.

isupport.go 1.5KB

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