2022-01-18 15:04:20 +00:00
|
|
|
package protocol
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
type CapabilityType int
|
|
|
|
|
|
|
|
const (
|
|
|
|
VersionCapability CapabilityType = iota
|
|
|
|
ReaderCapability
|
|
|
|
IHaveCapability
|
|
|
|
PostCapability
|
|
|
|
NewNewsCapability
|
|
|
|
HdrCapability
|
|
|
|
OverCapability
|
|
|
|
ListCapability
|
|
|
|
ImplementationCapability
|
|
|
|
ModeReaderCapability
|
|
|
|
)
|
|
|
|
|
|
|
|
func (ct CapabilityType) String() string {
|
|
|
|
switch ct {
|
|
|
|
case VersionCapability:
|
|
|
|
return CapabilityNameVersion
|
|
|
|
case ReaderCapability:
|
|
|
|
return CapabilityNameReader
|
|
|
|
case IHaveCapability:
|
|
|
|
return CapabilityNameIHave
|
|
|
|
case PostCapability:
|
|
|
|
return CapabilityNamePost
|
|
|
|
case NewNewsCapability:
|
|
|
|
return CapabilityNameNewNews
|
|
|
|
case HdrCapability:
|
|
|
|
return CapabilityNameHdr
|
|
|
|
case OverCapability:
|
|
|
|
return CapabilityNameOver
|
|
|
|
case ListCapability:
|
|
|
|
return CapabilityNameList
|
|
|
|
case ImplementationCapability:
|
|
|
|
return CapabilityNameImplementation
|
|
|
|
case ModeReaderCapability:
|
|
|
|
return CapabilityNameModeReader
|
|
|
|
default:
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type Capability struct {
|
|
|
|
Type CapabilityType
|
|
|
|
Params string // optional
|
|
|
|
}
|
|
|
|
|
|
|
|
type Capabilities []Capability
|
|
|
|
|
2022-01-18 17:26:37 +00:00
|
|
|
func (cs *Capabilities) Add(c Capability) {
|
|
|
|
for _, v := range *cs {
|
2022-01-18 15:04:20 +00:00
|
|
|
if v.Type == c.Type {
|
|
|
|
return // allowed only unique items
|
|
|
|
}
|
|
|
|
}
|
2022-01-18 17:26:37 +00:00
|
|
|
*cs = append(*cs, c)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cs *Capabilities) Remove(ct CapabilityType) {
|
|
|
|
for i, v := range *cs {
|
|
|
|
if v.Type == ct {
|
|
|
|
*cs = append((*cs)[:i], (*cs)[i+1:]...)
|
|
|
|
}
|
|
|
|
}
|
2022-01-18 15:04:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (cs Capabilities) String() string {
|
|
|
|
sb := strings.Builder{}
|
|
|
|
sb.Write([]byte("101 Capability list:" + CRLF))
|
|
|
|
|
|
|
|
for _, v := range cs {
|
|
|
|
if v.Params != "" {
|
|
|
|
sb.Write([]byte(fmt.Sprintf("%s %s%s", v.Type, v.Params, CRLF)))
|
|
|
|
} else {
|
|
|
|
sb.Write([]byte(fmt.Sprintf("%s%s", v.Type, CRLF)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sb.Write([]byte(MultilineEnding))
|
|
|
|
|
|
|
|
return sb.String()
|
|
|
|
}
|