/
stanza.go
198 lines (174 loc) · 5.63 KB
/
stanza.go
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package xmpp
import (
"bytes"
"encoding/xml"
"fmt"
)
const (
IQTypeGet = "get"
IQTypeSet = "set"
IQTypeResult = "result"
IQTypeError = "error"
MessageTypeNormal = "normal"
MessageTypeChat = "chat"
MessageTypeError = "error"
)
// XMPP <iq/> stanza.
type IQ struct {
XMLName xml.Name `xml:"iq"`
ID string `xml:"id,attr"`
Type string `xml:"type,attr"`
To string `xml:"to,attr,omitempty"`
From string `xml:"from,attr,omitempty"`
Payload string `xml:",innerxml"`
Error *Error `xml:"error"`
}
// Encode the value to an XML string and set as the payload. See xml.Marshal
// for how the value is encoded.
func (iq *IQ) PayloadEncode(v interface{}) error {
bytes, err := xml.Marshal(v)
if err != nil {
return err
}
iq.Payload = string(bytes)
return nil
}
// Decode the payload (an XML string) into the given value. See xml.Unmarshal
// for how the value is decoded.
func (iq *IQ) PayloadDecode(v interface{}) error {
return xml.Unmarshal([]byte(iq.Payload), v)
}
// Return the name of the payload element.
func (iq *IQ) PayloadName() (name xml.Name) {
dec := xml.NewDecoder(bytes.NewBufferString(iq.Payload))
tok, err := dec.Token()
if err != nil {
return
}
start, ok := tok.(xml.StartElement)
if !ok {
return
}
return start.Name
}
// Create a response IQ. The ID is kept, To and From are reversed, Type is set
// to the given value.
func (iq *IQ) Response(iqType string) *IQ {
return &IQ{ID: iq.ID, Type: iqType, From: iq.To, To: iq.From}
}
// XMPP <message/> stanza.
type Message struct {
XMLName xml.Name `xml:"message"`
ID string `xml:"id,attr,omitempty"`
Type string `xml:"type,attr,omitempty"`
To string `xml:"to,attr,omitempty"`
From string `xml:"from,attr,omitempty"`
Subject string `xml:"subject,omitempty"`
Body []MessageBody `xml:"body,omitempty"`
Thread string `xml:"thread,omitempty"`
Error *Error `xml:"error"`
Lang string `xml:"xml:lang,attr,omitempty"`
Confirm *Confirm `xml:"confirm"` // XEP-0070
Active *Active `xml:"active"` // XEP-0085
Composing *Composing `xml:"composing"` // XEP-0085
Paused *Paused `xml:"paused"` // XEP-0085
Inactive *Inactive `xml:"inactive"` // XEP-0085
Gone *Gone `xml:"gone"` // XEP-0085
}
type MessageBody struct {
Lang string `xml:"xml:lang,attr,omitempty"`
Value string `xml:",chardata"`
}
// XMPP <presence/> stanza.
type Presence struct {
XMLName xml.Name `xml:"presence"`
ID string `xml:"id,attr,omitempty"`
Type string `xml:"type,attr,omitempty"`
To string `xml:"to,attr,omitempty"`
From string `xml:"from,attr,omitempty"`
Show string `xml:"show"` // away, chat, dnd, xa
Status string `xml:"status"` // sb []clientText
Photo string `xml:"photo,omitempty"` // Avatar
Nick string `xml:"nick,omitempty"` // Nickname
}
// XMPP <error/>. May occur as a top-level stanza or embedded in another
// stanza, e.g. an <iq type="error"/>.
type Error struct {
XMLName xml.Name `xml:"error"`
Code string `xml:"code,attr,omitempty"`
Type string `xml:"type,attr"`
Payload string `xml:",innerxml"`
}
func (e Error) Error() string {
text := e.Text()
if text == "" {
return fmt.Sprintf("[%s] %s", e.Type, e.Condition().Local)
}
return fmt.Sprintf("[%s] %s, %s", e.Type, e.Condition().Local, text)
}
type errorText struct {
XMLName xml.Name
Text string `xml:",chardata"`
}
// Create a new Error instance using the args as the payload.
func NewError(errorType string, condition ErrorCondition, text string) *Error {
// Build payload.
buf := new(bytes.Buffer)
writeXMLStartElement(buf, &xml.StartElement{
Name: xml.Name{"", condition.Local},
Attr: []xml.Attr{
{xml.Name{"", "xmlns"}, condition.Space},
},
})
writeXMLEndElement(buf, &xml.EndElement{Name: xml.Name{"", condition.Local}})
enc := xml.NewEncoder(buf)
if text != "" {
enc.Encode(errorText{xml.Name{condition.Space, "text"}, text})
}
return &Error{Type: errorType, Payload: string(buf.Bytes())}
}
func NewErrorWithCode(code, errorType string, condition ErrorCondition, text string) *Error {
err := NewError(errorType, condition, text)
err.Code = code
return err
}
// Return the error text from the payload, or "" if not present.
func (e Error) Text() string {
dec := xml.NewDecoder(bytes.NewBufferString(e.Payload))
next := startElementIter(dec)
for start := next(); start != nil; {
if start.Name.Local == "text" {
text := errorText{}
dec.DecodeElement(&text, start)
return text.Text
}
dec.Skip()
start = next()
}
return ""
}
// Return the error condition from the payload.
func (e Error) Condition() ErrorCondition {
dec := xml.NewDecoder(bytes.NewBufferString(e.Payload))
next := startElementIter(dec)
for start := next(); start != nil; {
if start.Name.Local != "text" && (start.Name.Space == nsErrorStanzas || start.Name.Space == nsErrorStreams) {
return ErrorCondition(start.Name)
}
dec.Skip()
start = next()
}
return ErrorCondition{}
}
// Error condition.
type ErrorCondition xml.Name
// Stanza errors.
var (
ErrorFeatureNotImplemented = ErrorCondition{nsErrorStanzas, "feature-not-implemented"}
ErrorRemoteServerNotFound = ErrorCondition{nsErrorStanzas, "remote-server-not-found"}
ErrorServiceUnavailable = ErrorCondition{nsErrorStanzas, "service-unavailable"}
ErrorNotAuthorized = ErrorCondition{nsErrorStanzas, "not-authorized"}
ErrorConflict = ErrorCondition{nsErrorStanzas, "conflict"}
ErrorNotAcceptable = ErrorCondition{nsErrorStanzas, "not-acceptable"}
ErrorForbidden = ErrorCondition{nsErrorStanzas, "forbidden"}
)