2020-01-09 14:33:11 +00:00
|
|
|
package stanza
|
|
|
|
|
|
|
|
import "encoding/xml"
|
|
|
|
|
|
|
|
// Implements the XEP-0050 extension
|
|
|
|
|
|
|
|
const (
|
|
|
|
CommandActionCancel = "cancel"
|
|
|
|
CommandActionComplete = "complete"
|
|
|
|
CommandActionExecute = "execute"
|
|
|
|
CommandActionNext = "next"
|
|
|
|
CommandActionPrevious = "prev"
|
|
|
|
|
|
|
|
CommandStatusCancelled = "canceled"
|
|
|
|
CommandStatusCompleted = "completed"
|
|
|
|
CommandStatusExecuting = "executing"
|
|
|
|
|
|
|
|
CommandNoteTypeErr = "error"
|
|
|
|
CommandNoteTypeInfo = "info"
|
|
|
|
CommandNoteTypeWarn = "warn"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Command struct {
|
|
|
|
XMLName xml.Name `xml:"http://jabber.org/protocol/commands command"`
|
|
|
|
|
2024-05-12 13:13:13 +00:00
|
|
|
CommandElements []CommandElement
|
2020-01-09 14:33:11 +00:00
|
|
|
|
|
|
|
BadAction *struct{} `xml:"bad-action,omitempty"`
|
|
|
|
BadLocale *struct{} `xml:"bad-locale,omitempty"`
|
|
|
|
BadPayload *struct{} `xml:"bad-payload,omitempty"`
|
|
|
|
BadSessionId *struct{} `xml:"bad-sessionid,omitempty"`
|
|
|
|
MalformedAction *struct{} `xml:"malformed-action,omitempty"`
|
|
|
|
SessionExpired *struct{} `xml:"session-expired,omitempty"`
|
|
|
|
|
|
|
|
// Attributes
|
|
|
|
Action string `xml:"action,attr,omitempty"`
|
|
|
|
Node string `xml:"node,attr"`
|
|
|
|
SessionId string `xml:"sessionid,attr,omitempty"`
|
|
|
|
Status string `xml:"status,attr,omitempty"`
|
|
|
|
Lang string `xml:"lang,attr,omitempty"`
|
2020-01-31 10:48:03 +00:00
|
|
|
|
|
|
|
// Result sets
|
|
|
|
ResultSet *ResultSet `xml:"set,omitempty"`
|
2020-01-09 14:33:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Command) Namespace() string {
|
|
|
|
return c.XMLName.Space
|
|
|
|
}
|
|
|
|
|
2020-01-31 10:48:03 +00:00
|
|
|
func (c *Command) GetSet() *ResultSet {
|
|
|
|
return c.ResultSet
|
|
|
|
}
|
|
|
|
|
2020-01-09 14:33:11 +00:00
|
|
|
type CommandElement interface {
|
|
|
|
Ref() string
|
|
|
|
}
|
|
|
|
|
|
|
|
type Actions struct {
|
2024-01-31 01:25:57 +00:00
|
|
|
XMLName xml.Name `xml:"actions"`
|
|
|
|
|
2020-01-09 14:33:11 +00:00
|
|
|
Prev *struct{} `xml:"prev,omitempty"`
|
|
|
|
Next *struct{} `xml:"next,omitempty"`
|
|
|
|
Complete *struct{} `xml:"complete,omitempty"`
|
|
|
|
|
|
|
|
Execute string `xml:"execute,attr,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Actions) Ref() string {
|
|
|
|
return "actions"
|
|
|
|
}
|
|
|
|
|
|
|
|
type Note struct {
|
2024-01-31 01:25:57 +00:00
|
|
|
XMLName xml.Name `xml:"note"`
|
|
|
|
|
2020-01-09 14:33:11 +00:00
|
|
|
Text string `xml:",cdata"`
|
|
|
|
Type string `xml:"type,attr,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *Note) Ref() string {
|
|
|
|
return "note"
|
|
|
|
}
|
2020-01-31 10:48:03 +00:00
|
|
|
func (f *Form) Ref() string { return "form" }
|
2020-01-09 14:33:11 +00:00
|
|
|
|
|
|
|
func (n *Node) Ref() string {
|
|
|
|
return "node"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Command) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
|
|
|
|
c.XMLName = start.Name
|
|
|
|
|
|
|
|
// Extract packet attributes
|
|
|
|
for _, attr := range start.Attr {
|
|
|
|
if attr.Name.Local == "action" {
|
|
|
|
c.Action = attr.Value
|
|
|
|
}
|
|
|
|
if attr.Name.Local == "node" {
|
|
|
|
c.Node = attr.Value
|
|
|
|
}
|
|
|
|
if attr.Name.Local == "sessionid" {
|
|
|
|
c.SessionId = attr.Value
|
|
|
|
}
|
|
|
|
if attr.Name.Local == "status" {
|
|
|
|
c.Status = attr.Value
|
|
|
|
}
|
|
|
|
if attr.Name.Local == "lang" {
|
|
|
|
c.Lang = attr.Value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// decode inner elements
|
|
|
|
for {
|
|
|
|
t, err := d.Token()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
switch tt := t.(type) {
|
|
|
|
|
|
|
|
case xml.StartElement:
|
|
|
|
// Decode sub-elements
|
|
|
|
var err error
|
|
|
|
switch tt.Name.Local {
|
|
|
|
|
2024-01-31 01:25:57 +00:00
|
|
|
case "actions":
|
2020-01-09 14:33:11 +00:00
|
|
|
a := Actions{}
|
2020-01-31 14:17:59 +00:00
|
|
|
err = d.DecodeElement(&a, &tt)
|
2024-05-12 13:13:13 +00:00
|
|
|
c.CommandElements = append(c.CommandElements, &a)
|
2024-01-31 01:25:57 +00:00
|
|
|
case "note":
|
2020-01-09 14:33:11 +00:00
|
|
|
nt := Note{}
|
2020-01-31 14:17:59 +00:00
|
|
|
err = d.DecodeElement(&nt, &tt)
|
2024-05-12 13:13:13 +00:00
|
|
|
c.CommandElements = append(c.CommandElements, &nt)
|
2020-01-31 10:48:03 +00:00
|
|
|
case "x":
|
|
|
|
f := Form{}
|
2020-01-31 14:17:59 +00:00
|
|
|
err = d.DecodeElement(&f, &tt)
|
2024-05-12 13:13:13 +00:00
|
|
|
c.CommandElements = append(c.CommandElements, &f)
|
2020-01-09 14:33:11 +00:00
|
|
|
default:
|
|
|
|
n := Node{}
|
2020-01-31 14:17:59 +00:00
|
|
|
err = d.DecodeElement(&n, &tt)
|
2024-05-12 13:13:13 +00:00
|
|
|
c.CommandElements = append(c.CommandElements, &n)
|
2020-01-09 14:33:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2020-01-31 14:17:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-01-09 14:33:11 +00:00
|
|
|
case xml.EndElement:
|
|
|
|
if tt == start.End() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-01-31 10:48:03 +00:00
|
|
|
|
|
|
|
func init() {
|
|
|
|
TypeRegistry.MapExtension(PKTIQ, xml.Name{Space: "http://jabber.org/protocol/commands", Local: "command"}, Command{})
|
|
|
|
}
|