Support multiple command elements

This commit is contained in:
Bohdan Horbeshko 2024-05-12 09:13:13 -04:00 committed by Mickaël Rémond
parent 9af32ad7e0
commit 655f875918
2 changed files with 19 additions and 12 deletions

View File

@ -23,7 +23,7 @@ const (
type Command struct { type Command struct {
XMLName xml.Name `xml:"http://jabber.org/protocol/commands command"` XMLName xml.Name `xml:"http://jabber.org/protocol/commands command"`
CommandElement CommandElement CommandElements []CommandElement
BadAction *struct{} `xml:"bad-action,omitempty"` BadAction *struct{} `xml:"bad-action,omitempty"`
BadLocale *struct{} `xml:"bad-locale,omitempty"` BadLocale *struct{} `xml:"bad-locale,omitempty"`
@ -124,19 +124,19 @@ func (c *Command) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
case "actions": case "actions":
a := Actions{} a := Actions{}
err = d.DecodeElement(&a, &tt) err = d.DecodeElement(&a, &tt)
c.CommandElement = &a c.CommandElements = append(c.CommandElements, &a)
case "note": case "note":
nt := Note{} nt := Note{}
err = d.DecodeElement(&nt, &tt) err = d.DecodeElement(&nt, &tt)
c.CommandElement = &nt c.CommandElements = append(c.CommandElements, &nt)
case "x": case "x":
f := Form{} f := Form{}
err = d.DecodeElement(&f, &tt) err = d.DecodeElement(&f, &tt)
c.CommandElement = &f c.CommandElements = append(c.CommandElements, &f)
default: default:
n := Node{} n := Node{}
err = d.DecodeElement(&n, &tt) err = d.DecodeElement(&n, &tt)
c.CommandElement = &n c.CommandElements = append(c.CommandElements, &n)
if err != nil { if err != nil {
return err return err
} }

View File

@ -240,7 +240,7 @@ func NewApprovePendingSubRequest(serviceId, sessionId, nodeId string) (*IQ, erro
Node: "http://jabber.org/protocol/pubsub#get-pending", Node: "http://jabber.org/protocol/pubsub#get-pending",
Action: CommandActionExecute, Action: CommandActionExecute,
SessionId: sessionId, SessionId: sessionId,
CommandElement: &n, CommandElements: []CommandElement{&n},
} }
return iq, nil return iq, nil
} }
@ -353,11 +353,18 @@ func (iq *IQ) GetFormFields() (map[string]*Field, error) {
case *Command: case *Command:
fieldMap := make(map[string]*Field) fieldMap := make(map[string]*Field)
co, ok := payload.CommandElement.(*Form) var form *Form
if !ok { for _, ce := range payload.CommandElements {
fo, ok := ce.(*Form)
if ok {
form = fo
break
}
}
if form == nil {
return nil, errors.New("this IQ does not contain a command payload with a form") return nil, errors.New("this IQ does not contain a command payload with a form")
} }
for _, elt := range co.Fields { for _, elt := range form.Fields {
fieldMap[elt.Var] = elt fieldMap[elt.Var] = elt
} }
return fieldMap, nil return fieldMap, nil