mirror of
https://github.com/ergochat/ergo.git
synced 2024-11-10 22:19:31 +01:00
send proper replies for cap protocol
This commit is contained in:
parent
36602c9a3c
commit
0874692aa8
@ -235,7 +235,10 @@ func (client *Client) ChangeNickname(nickname string) {
|
||||
}
|
||||
}
|
||||
|
||||
func (client *Client) Reply(reply string) {
|
||||
func (client *Client) Reply(reply string, args ...interface{}) {
|
||||
if len(args) > 0 {
|
||||
reply = fmt.Sprintf(reply, args...)
|
||||
}
|
||||
client.socket.Write(reply)
|
||||
}
|
||||
|
||||
|
@ -709,20 +709,12 @@ func NewOperCommand(args []string) (editableCommand, error) {
|
||||
type CapCommand struct {
|
||||
BaseCommand
|
||||
subCommand CapSubCommand
|
||||
args []string
|
||||
capabilities CapabilitySet
|
||||
}
|
||||
|
||||
func (msg *CapCommand) String() string {
|
||||
return fmt.Sprintf("CAP(subCommand=%s, args=%s)", msg.subCommand, msg.args)
|
||||
}
|
||||
|
||||
func (msg *CapCommand) Capabilities() []Capability {
|
||||
strs := strings.Split(msg.args[0], " ")
|
||||
caps := make([]Capability, len(strs))
|
||||
for index, str := range strs {
|
||||
caps[index] = Capability(str)
|
||||
}
|
||||
return caps
|
||||
return fmt.Sprintf("CAP(subCommand=%s, capabilities=%s)",
|
||||
msg.subCommand, msg.capabilities)
|
||||
}
|
||||
|
||||
func NewCapCommand(args []string) (editableCommand, error) {
|
||||
@ -731,8 +723,15 @@ func NewCapCommand(args []string) (editableCommand, error) {
|
||||
}
|
||||
|
||||
cmd := &CapCommand{
|
||||
subCommand: CapSubCommand(args[0]),
|
||||
args: args[1:],
|
||||
subCommand: CapSubCommand(strings.ToUpper(args[0])),
|
||||
capabilities: make(CapabilitySet),
|
||||
}
|
||||
|
||||
if len(args) > 1 {
|
||||
strs := spacesExpr.Split(args[1], -1)
|
||||
for _, str := range strs {
|
||||
cmd.capabilities[Capability(str)] = true
|
||||
}
|
||||
}
|
||||
return cmd, nil
|
||||
}
|
||||
|
@ -301,28 +301,36 @@ func (msg *CapCommand) HandleRegServer(server *Server) {
|
||||
switch msg.subCommand {
|
||||
case CAP_LS:
|
||||
client.capState = CapNegotiating
|
||||
client.Reply(fmt.Sprintf("CAP LS :%d", MultiPrefix))
|
||||
client.Reply("CAP LS * :%s", SupportedCapabilities)
|
||||
|
||||
case CAP_LIST:
|
||||
client.Reply(fmt.Sprintf("CAP LIST :%s", client.capabilities))
|
||||
client.Reply("CAP LIST * :%s", client.capabilities)
|
||||
|
||||
case CAP_REQ:
|
||||
client.capState = CapNegotiating
|
||||
caps := msg.Capabilities()
|
||||
if (len(caps) != 1) && (caps[0] != MultiPrefix) {
|
||||
client.Reply("CAP NAK :" + msg.args[0])
|
||||
for capability := range msg.capabilities {
|
||||
if !SupportedCapabilities[capability] {
|
||||
client.Reply("CAP NAK * :%s", msg.capabilities)
|
||||
return
|
||||
}
|
||||
for _, capability := range caps {
|
||||
}
|
||||
for capability := range msg.capabilities {
|
||||
client.capabilities[capability] = true
|
||||
}
|
||||
client.Reply("CAP ACK :" + msg.args[0])
|
||||
client.Reply("CAP ACK * :%s", msg.capabilities)
|
||||
|
||||
case CAP_CLEAR:
|
||||
format := strings.TrimRight(
|
||||
strings.Repeat("%s%s ", len(client.capabilities)), " ")
|
||||
args := make([]interface{}, len(client.capabilities))
|
||||
index := 0
|
||||
for capability := range client.capabilities {
|
||||
args[index] = Disable
|
||||
args[index+1] = capability
|
||||
index += 2
|
||||
delete(client.capabilities, capability)
|
||||
}
|
||||
client.Reply("CAP ACK :")
|
||||
client.Reply("CAP ACK * :"+format, args...)
|
||||
|
||||
case CAP_END:
|
||||
client.capState = CapNegotiated
|
||||
|
@ -16,6 +16,10 @@ type Capability string
|
||||
|
||||
type CapModifier rune
|
||||
|
||||
func (mod CapModifier) String() string {
|
||||
return string(mod)
|
||||
}
|
||||
|
||||
type CapState uint
|
||||
|
||||
type CapabilitySet map[Capability]bool
|
||||
@ -25,6 +29,7 @@ func (set CapabilitySet) String() string {
|
||||
index := 0
|
||||
for capability := range set {
|
||||
strs[index] = string(capability)
|
||||
index += 1
|
||||
}
|
||||
return strings.Join(strs, " ")
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user