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.9KB

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