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.

set.go 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // Copyright (c) 2017 Daniel Oaks <daniel@danieloaks.net>
  2. // released under the MIT license
  3. package caps
  4. import (
  5. "sort"
  6. "strings"
  7. "github.com/oragono/oragono/irc/utils"
  8. )
  9. // Set holds a set of enabled capabilities.
  10. type Set [bitsetLen]uint64
  11. // NewSet returns a new Set, with the given capabilities enabled.
  12. func NewSet(capabs ...Capability) *Set {
  13. var newSet Set
  14. newSet.Enable(capabs...)
  15. return &newSet
  16. }
  17. // NewCompleteSet returns a new Set, with all defined capabilities enabled.
  18. func NewCompleteSet() *Set {
  19. var newSet Set
  20. asSlice := newSet[:]
  21. for i := 0; i < numCapabs; i += 1 {
  22. utils.BitsetSet(asSlice, uint(i), true)
  23. }
  24. return &newSet
  25. }
  26. // Enable enables the given capabilities.
  27. func (s *Set) Enable(capabs ...Capability) {
  28. asSlice := s[:]
  29. for _, capab := range capabs {
  30. utils.BitsetSet(asSlice, uint(capab), true)
  31. }
  32. }
  33. // Disable disables the given capabilities.
  34. func (s *Set) Disable(capabs ...Capability) {
  35. asSlice := s[:]
  36. for _, capab := range capabs {
  37. utils.BitsetSet(asSlice, uint(capab), false)
  38. }
  39. }
  40. // Add adds the given capabilities to this set.
  41. // this is just a wrapper to allow more clear use.
  42. func (s *Set) Add(capabs ...Capability) {
  43. s.Enable(capabs...)
  44. }
  45. // Remove removes the given capabilities from this set.
  46. // this is just a wrapper to allow more clear use.
  47. func (s *Set) Remove(capabs ...Capability) {
  48. s.Disable(capabs...)
  49. }
  50. // Has returns true if this set has the given capability.
  51. func (s *Set) Has(capab Capability) bool {
  52. return utils.BitsetGet(s[:], uint(capab))
  53. }
  54. // HasAll returns true if the set has all the given capabilities.
  55. func (s *Set) HasAll(capabs ...Capability) bool {
  56. for _, capab := range capabs {
  57. if !s.Has(capab) {
  58. return false
  59. }
  60. }
  61. return true
  62. }
  63. // Union adds all the capabilities of another set to this set.
  64. func (s *Set) Union(other *Set) {
  65. utils.BitsetUnion(s[:], other[:])
  66. }
  67. // Subtract removes all the capabilities of another set from this set.
  68. func (s *Set) Subtract(other *Set) {
  69. utils.BitsetSubtract(s[:], other[:])
  70. }
  71. // Empty returns whether the set is empty.
  72. func (s *Set) Empty() bool {
  73. return utils.BitsetEmpty(s[:])
  74. }
  75. // String returns all of our enabled capabilities as a string.
  76. func (s *Set) String(version Version, values *Values) string {
  77. var strs sort.StringSlice
  78. var capab Capability
  79. asSlice := s[:]
  80. for capab = 0; capab < numCapabs; capab++ {
  81. // skip any capabilities that are not enabled
  82. if !utils.BitsetGet(asSlice, uint(capab)) {
  83. continue
  84. }
  85. capString := capab.Name()
  86. if version == Cap302 {
  87. val, exists := values.Get(capab)
  88. if exists {
  89. capString += "=" + val
  90. }
  91. }
  92. strs = append(strs, capString)
  93. }
  94. // sort the cap string before we send it out
  95. sort.Sort(strs)
  96. return strings.Join(strs, " ")
  97. }
  98. // returns whether we should send `znc.in/self-message`-style echo messages
  99. // to sessions other than that which originated the message
  100. func (capabs *Set) SelfMessagesEnabled() bool {
  101. return capabs.Has(EchoMessage) || capabs.Has(ZNCSelfMessage)
  102. }