2017-09-29 09:33:29 +02:00
|
|
|
// Copyright (c) 2017 Daniel Oaks <daniel@danieloaks.net>
|
|
|
|
// released under the MIT license
|
|
|
|
|
2017-09-29 09:25:58 +02:00
|
|
|
package caps
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sort"
|
|
|
|
"strings"
|
2018-06-26 00:08:15 +02:00
|
|
|
|
|
|
|
"github.com/oragono/oragono/irc/utils"
|
2017-09-29 09:25:58 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// Set holds a set of enabled capabilities.
|
2018-06-26 00:08:15 +02:00
|
|
|
type Set [bitsetLen]uint64
|
2017-09-29 09:25:58 +02:00
|
|
|
|
|
|
|
// NewSet returns a new Set, with the given capabilities enabled.
|
|
|
|
func NewSet(capabs ...Capability) *Set {
|
2018-06-26 00:08:15 +02:00
|
|
|
var newSet Set
|
2017-09-29 09:25:58 +02:00
|
|
|
newSet.Enable(capabs...)
|
|
|
|
return &newSet
|
|
|
|
}
|
|
|
|
|
|
|
|
// Enable enables the given capabilities.
|
|
|
|
func (s *Set) Enable(capabs ...Capability) {
|
2018-06-26 00:08:15 +02:00
|
|
|
asSlice := s[:]
|
2017-09-29 09:25:58 +02:00
|
|
|
for _, capab := range capabs {
|
2018-06-26 00:08:15 +02:00
|
|
|
utils.BitsetSet(asSlice, uint(capab), true)
|
2017-09-29 09:25:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Disable disables the given capabilities.
|
|
|
|
func (s *Set) Disable(capabs ...Capability) {
|
2018-06-26 00:08:15 +02:00
|
|
|
asSlice := s[:]
|
2017-09-29 09:25:58 +02:00
|
|
|
for _, capab := range capabs {
|
2018-06-26 00:08:15 +02:00
|
|
|
utils.BitsetSet(asSlice, uint(capab), false)
|
2017-09-29 09:25:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add adds the given capabilities to this set.
|
|
|
|
// this is just a wrapper to allow more clear use.
|
|
|
|
func (s *Set) Add(capabs ...Capability) {
|
|
|
|
s.Enable(capabs...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove removes the given capabilities from this set.
|
|
|
|
// this is just a wrapper to allow more clear use.
|
|
|
|
func (s *Set) Remove(capabs ...Capability) {
|
|
|
|
s.Disable(capabs...)
|
|
|
|
}
|
|
|
|
|
2018-06-26 00:08:15 +02:00
|
|
|
// Has returns true if this set has the given capability.
|
|
|
|
func (s *Set) Has(capab Capability) bool {
|
|
|
|
return utils.BitsetGet(s[:], uint(capab))
|
2017-09-29 09:25:58 +02:00
|
|
|
}
|
|
|
|
|
2018-06-26 00:08:15 +02:00
|
|
|
// Union adds all the capabilities of another set to this set.
|
|
|
|
func (s *Set) Union(other *Set) {
|
|
|
|
utils.BitsetUnion(s[:], other[:])
|
2017-09-29 09:25:58 +02:00
|
|
|
}
|
|
|
|
|
2019-02-03 02:00:23 +01:00
|
|
|
// Subtract removes all the capabilities of another set from this set.
|
|
|
|
func (s *Set) Subtract(other *Set) {
|
|
|
|
utils.BitsetSubtract(s[:], other[:])
|
|
|
|
}
|
|
|
|
|
2018-06-26 00:08:15 +02:00
|
|
|
// Empty returns whether the set is empty.
|
|
|
|
func (s *Set) Empty() bool {
|
|
|
|
return utils.BitsetEmpty(s[:])
|
2017-09-29 09:25:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// String returns all of our enabled capabilities as a string.
|
|
|
|
func (s *Set) String(version Version, values *Values) string {
|
|
|
|
var strs sort.StringSlice
|
|
|
|
|
2018-06-26 00:08:15 +02:00
|
|
|
var capab Capability
|
|
|
|
asSlice := s[:]
|
|
|
|
for capab = 0; capab < numCapabs; capab++ {
|
|
|
|
// skip any capabilities that are not enabled
|
|
|
|
if !utils.BitsetGet(asSlice, uint(capab)) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
capString := capab.Name()
|
2017-09-29 09:25:58 +02:00
|
|
|
if version == Cap302 {
|
2018-06-26 00:08:15 +02:00
|
|
|
val, exists := values.Get(capab)
|
2017-09-29 09:25:58 +02:00
|
|
|
if exists {
|
|
|
|
capString += "=" + val
|
|
|
|
}
|
|
|
|
}
|
|
|
|
strs = append(strs, capString)
|
|
|
|
}
|
|
|
|
|
|
|
|
// sort the cap string before we send it out
|
|
|
|
sort.Sort(strs)
|
|
|
|
|
|
|
|
return strings.Join(strs, " ")
|
|
|
|
}
|