~samwhited/xmpp

ref: ae670e1d6448899cc7cd1d2279d256222d0f42ff xmpp/iq.go -rw-r--r-- 1.6 KiB
ae670e1dSam Whited Add a TODO for later 5 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Copyright 2016 Sam Whited.
// Use of this source code is governed by the BSD 2-clause license that can be
// found in the LICENSE file.

package xmpp

import (
	"encoding/xml"
	"errors"
	"strings"

	"mellium.im/xmpp/jid"
)

// IQ ("Information Query") is used as a general request response mechanism.
// IQ's are one-to-one, provide get and set semantics, and always require a
// response in the form of a result or an error.
type IQ struct {
	XMLName xml.Name `xml:"iq"`
	ID      string   `xml:"id,attr"`
	Inner   string   `xml:",innerxml"`
	To      *jid.JID `xml:"to,attr"`
	From    *jid.JID `xml:"from,attr"`
	Lang    string   `xml:"http://www.w3.org/XML/1998/namespace lang,attr,omitempty"`
	Type    iqType   `xml:"type,attr"`
}

type iqType int

const (
	// GetIQ is used to query another entity for information.
	GetIQ iqType = iota

	// SetIQ is used to provide data to another entity, set new values, and
	// replace existing values.
	SetIQ

	// ResultIQ is sent in response to a successful get or set IQ.
	ResultIQ

	// ErrorIQ is sent to report that an error occured during the delivery or
	// processing of a get or set IQ.
	ErrorIQ
)

func (t iqType) MarshalXMLAttr(name xml.Name) (xml.Attr, error) {
	s := t.String()
	return xml.Attr{Name: name, Value: strings.ToLower(s[:len(s)-2])}, nil
}

func (t *iqType) UnmarshalXMLAttr(attr xml.Attr) error {
	switch attr.Value {
	case "get":
		*t = GetIQ
	case "set":
		*t = SetIQ
	case "result":
		*t = ResultIQ
	case "error":
		*t = ErrorIQ
	default:
		// TODO: This should be a stanza error with the bad-request condition.
		return errors.New("bad-request")
	}
	return nil
}