forked from ortuman/jackal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathelement.go
339 lines (296 loc) · 7.66 KB
/
element.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/*
* Copyright (c) 2018 Miguel Ángel Ortuño.
* See the LICENSE file for more information.
*/
package xmpp
import (
"bytes"
"encoding/gob"
"fmt"
"io"
"github.com/ortuman/jackal/xmpp/jid"
)
const (
// MessageName represents "message" stanza name
MessageName = "message"
// PresenceName represents "presence" stanza name
PresenceName = "presence"
// IQName represents "iq" stanza name
IQName = "iq"
)
// Element represents a generic and mutable XML node element.
type Element struct {
name string
text string
attrs attributeSet
elements elementSet
}
// NewElementName creates a mutable XML XElement instance with a given name.
func NewElementName(name string) *Element {
return &Element{name: name}
}
// NewElementNamespace creates a mutable XML XElement instance with a given name and namespace.
func NewElementNamespace(name, namespace string) *Element {
return &Element{
name: name,
attrs: attributeSet([]Attribute{{"xmlns", namespace}}),
}
}
// NewElementFromElement creates a mutable XML XElement by copying an element.
func NewElementFromElement(elem XElement) *Element {
e := &Element{}
e.copyFrom(elem)
return e
}
// NewErrorStanzaFromStanza returns a copy of an element of stanza error class.
func NewErrorStanzaFromStanza(stanza Stanza, stanzaErr *StanzaError, errorElements []XElement) Stanza {
e := &stanzaElement{}
e.copyFrom(stanza)
e.SetType(ErrorType)
e.SetFromJID(stanza.ToJID())
e.SetToJID(stanza.FromJID())
errEl := stanzaErr.Element()
errEl.AppendElements(errorElements)
e.AppendElement(errEl)
return e
}
// NewElementFromBytes creates and returns a new Element from its byte representation.
func NewElementFromBytes(buf *bytes.Buffer) (*Element, error) {
e := &Element{}
if err := e.FromBytes(buf); err != nil {
return nil, err
}
return e, nil
}
// Name returns XML node name.
func (e *Element) Name() string {
return e.name
}
// Attributes returns XML node attribute value.
func (e *Element) Attributes() AttributeSet {
return e.attrs
}
// Elements returns all instance's child elements.
func (e *Element) Elements() ElementSet {
return e.elements
}
// Text returns XML node text value.
// Returns an empty string if not set.
func (e *Element) Text() string {
return e.text
}
// Namespace returns 'xmlns' node attribute.
func (e *Element) Namespace() string {
return e.attrs.Get("xmlns")
}
// ID returns 'id' node attribute.
func (e *Element) ID() string {
return e.attrs.Get("id")
}
// Language returns 'xml:lang' node attribute.
func (e *Element) Language() string {
return e.attrs.Get("xml:lang")
}
// Version returns 'version' node attribute.
func (e *Element) Version() string {
return e.attrs.Get("version")
}
// From returns 'from' node attribute.
func (e *Element) From() string {
return e.attrs.Get("from")
}
// To returns 'to' node attribute.
func (e *Element) To() string {
return e.attrs.Get("to")
}
// Type returns 'type' node attribute.
func (e *Element) Type() string {
return e.attrs.Get("type")
}
// IsStanza returns true if element is an XMPP stanza.
func (e *Element) IsStanza() bool {
switch e.Name() {
case "iq", "presence", "message":
return true
}
return false
}
// IsError returns true if element has a 'type' attribute of value 'error'.
func (e *Element) IsError() bool {
return e.Type() == ErrorType
}
// Error returns element error sub element.
func (e *Element) Error() XElement {
return e.elements.Child("error")
}
// String returns a string representation of the element.
func (e *Element) String() string {
buf := bufPool.Get()
defer bufPool.Put(buf)
_ = e.ToXML(buf, true)
return buf.String()
}
// ToXML serializes element to a raw XML representation.
// includeClosing determines if closing tag should be attached.
func (e *Element) ToXML(w io.Writer, includeClosing bool) error {
if _, err := io.WriteString(w, "<"); err != nil {
return err
}
if _, err := io.WriteString(w, e.name); err != nil {
return err
}
// serialize attributes
for _, attr := range e.attrs {
if len(attr.Value) == 0 {
continue
}
if _, err := io.WriteString(w, ` `); err != nil {
return err
}
if _, err := io.WriteString(w, attr.Label); err != nil {
return err
}
if _, err := io.WriteString(w, `="`); err != nil {
return err
}
if _, err := io.WriteString(w, attr.Value); err != nil {
return err
}
if _, err := io.WriteString(w, `"`); err != nil {
return err
}
}
// serialize elements
if e.elements.Count() > 0 || len(e.text) > 0 {
if _, err := io.WriteString(w, ">"); err != nil {
return err
}
if len(e.text) > 0 {
if err := escapeText(w, []byte(e.text), false); err != nil {
return err
}
}
for _, elem := range e.elements {
if err := elem.ToXML(w, true); err != nil {
return err
}
}
if includeClosing {
if _, err := io.WriteString(w, "</"); err != nil {
return err
}
if _, err := io.WriteString(w, e.name); err != nil {
return err
}
if _, err := io.WriteString(w, ">"); err != nil {
return err
}
}
} else {
if includeClosing {
if _, err := io.WriteString(w, "/>"); err != nil {
return err
}
} else {
if _, err := io.WriteString(w, ">"); err != nil {
return err
}
}
}
return nil
}
// FromBytes deserializes an element node from it's gob binary representation.
func (e *Element) FromBytes(buf *bytes.Buffer) error {
dec := gob.NewDecoder(buf)
if err := dec.Decode(&e.name); err != nil {
return err
}
if err := dec.Decode(&e.text); err != nil {
return err
}
if err := e.attrs.FromBytes(buf); err != nil {
return err
}
return e.elements.FromBytes(buf)
}
// ToBytes serializes an element node to it's gob binary representation.
func (e *Element) ToBytes(buf *bytes.Buffer) error {
enc := gob.NewEncoder(buf)
if err := enc.Encode(&e.name); err != nil {
return err
}
if err := enc.Encode(&e.text); err != nil {
return err
}
if err := e.attrs.ToBytes(buf); err != nil {
return err
}
return e.elements.ToBytes(buf)
}
func (e *Element) copyFrom(el XElement) {
e.name = el.Name()
e.text = el.Text()
e.attrs.copyFrom(el.Attributes().(attributeSet))
e.elements.copyFrom(el.Elements().(elementSet))
}
type stanzaElement struct {
Element
fromJID *jid.JID
toJID *jid.JID
}
// NewStanzaFromElement returns a new stanza instance derived from an XMPP element.
func NewStanzaFromElement(elem XElement) (Stanza, error) {
fromJID, err := jid.NewWithString(elem.From(), false)
if err != nil {
return nil, err
}
toJID, err := jid.NewWithString(elem.To(), false)
if err != nil {
return nil, err
}
switch elem.Name() {
case IQName:
return NewIQFromElement(elem, fromJID, toJID)
case PresenceName:
return NewPresenceFromElement(elem, fromJID, toJID)
case MessageName:
return NewMessageFromElement(elem, fromJID, toJID)
}
return nil, fmt.Errorf("unrecognized stanza name: %s", elem.Name())
}
// ToJID returns iq 'from' JID value.
func (s *stanzaElement) ToJID() *jid.JID {
return s.toJID
}
// SetToJID sets the IQ 'to' JID value.
func (s *stanzaElement) SetToJID(j *jid.JID) {
s.toJID = j
s.SetTo(j.String())
}
// FromJID returns presence 'from' JID value.
func (s *stanzaElement) FromJID() *jid.JID {
return s.fromJID
}
// SetFromJID sets the IQ 'from' JID value.
func (s *stanzaElement) SetFromJID(j *jid.JID) {
s.fromJID = j
s.SetFrom(j.String())
}
// FromBytes deserializes a stanza element from it's gob binary representation.
func (s *stanzaElement) FromBytes(buf *bytes.Buffer) error {
if err := s.Element.FromBytes(buf); err != nil {
return err
}
// set from and to JIDs
fromJID, err := jid.NewWithString(s.From(), false)
if err != nil {
return err
}
toJID, err := jid.NewWithString(s.To(), false)
if err != nil {
return err
}
s.SetFromJID(fromJID)
s.SetToJID(toJID)
return nil
}