-
Notifications
You must be signed in to change notification settings - Fork 2
/
stanza.go
284 lines (244 loc) · 7.19 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
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
package gxmpp
import (
"fmt"
"encoding/xml"
"bytes"
"errors"
"io"
)
var _ = io.EOF
type streamStart struct {
Name xml.Name `xml:"http://etherx.jabber.org/streams stream"`
From string `xml:"from,attr"` //From
To string `xml:"to,attr"`
Version string `xml:"version,attr"`
Lang string `xml:"xml lang,attr"`
NS string `xml:"xmlns,attr"`
}
type tlsStartTLS struct {
Name xml.Name `xml:urn:ietf:params:xml:ns:xmpp-tls starttls"`
}
type tlsProceed struct {
Name xml.Name `xml:urn:ietf:params:xml:ns:xmpp-tls proceed`
}
type saslAuth struct {
Name xml.Name `xml:"urn:ietf:params:xml:ns:xmpp-sasl auth"`
Mechanism string `xml:"mechanism,attr"`
NS string `xml:"xmlns,attr"`
Body string `xml:",chardata"`
}
type saslResponse struct {
Name xml.Name `xml:"urn:ietf:params:xml:ns:xmpp-sasl response"`
Body string `xml:",chardata"`
}
type saslAbort struct {
Name xml.Name `xml:"urn:ietf:params:xml:ns:xmpp-sasl abort"`
}
const (
xmppStreamEnd = "</stream:stream>"
xmppNsStream = "http://etherx.jabber.org/streams"
xmppNsTLS = "urn:ietf:params:xml:ns:xmpp-tls"
xmppNsSASL = "urn:ietf:params:xml:ns:xmpp-sasl"
xmppNsBind = "urn:ietf:params:xml:ns:xmpp-bind"
xmppNsClient = "jabber:client"
)
/*
usage:
xmppErr(xmppErrBadFormat)
*/
func xmppErr(errName string) string {
return fmt.Sprintf("<stream:error><%s xmlns='urn:ietf:params:xml:ns:xmpp-streams'/>"+
"</stream:error>", errName)
}
const (
//Stream Errors Are Unrecoverable
//rfc6120
//4.9.3.1.
xmppErrBadFormat = "bad-format"
//4.9.3.2.
xmppErrBadNamespacePrefix = "bad-namespace-prefix"
//4.9.3.3.
xmppErrConflict = "conflict"
//4.9.3.4.
xmppErrConnectionTimeout = "connection-timeout"
//4.9.3.5. host-gone
xmppErrHostGone = "host-gone"
//4.9.3.6. host-unknown
xmppErrHostUnknown = "host-unknown"
//4.9.3.7. improper-addressing
xmppErrImproperAddressing = "improper-addressing"
//4.9.3.8. internal-server-error
xmppErrInternalServerError = "internal-server-error"
//4.9.3.9. invalid-from
xmppErrInvalidFrom = "invalid-from"
//4.9.3.10. invalid-namespace
xmppErrInvalidNamespace = "invalid-namespace"
//4.9.3.11. invalid-xml
xmppInvalidXml = "invalid-xml"
//4.9.3.12. not-authorized
xmppNotAuthorized = "not-authorized"
//4.9.3.13. not-well-formed
xmppErrNotWellFormed = "not-well-formed"
//4.9.3.14. policy-violation.
//NOTE: The reason of this error need parameterize. And this is not right yet.
xmppPolicyViolation = "policy-violation"
//4.9.3.15. remote-connection-failed
xmppRemoteConnectionFailed = "remote-connection-failed"
//4.9.3.16. reset
xmppErrReset = "reset"
//4.9.3.17. resource-constraint
xmppErrResourceConstraint = "resource-constraint"
//4.9.3.18. restricted-xml
xmppErrRestrictedXml = "restricted-xml"
//4.9.3.19. see-other-host.
//NOTE:the other host need parameterize. This is not right yet.
xmppErrSeeOtherHost = "see-other-host"
//4.9.3.20. system-shutdown
xmppErrSystemShutDown = "system-shutdown"
//4.9.3.21. undefined-condition
xmppErrUndefinedCondition = "undefined-condition"
//4.9.3.22. unsupported-encoding
xmppErrUnsupportedEncoding = "unsupported-encoding"
//4.9.3.23. unsupported-feature
xmppErrUnsupportedFeature = "unsupported-feature"
//4.9.3.24. unsupported-stanza-type
xmppErrUnsupportedStanzaType = "unsupported-stanza-type"
//4.9.3.25. unsupported-version
xmppErrUnsupportedVersion = "unsupported-version"
//4.9.4. Application-Specific Conditions
//TBD
)
const (
saslErrAborted = "abort"
saslErrAccountDisabled = "account-disabled"
saslErrCredentialsExpired = "credentials-expired"
saslErrEncryptionRequired = "encryption-required"
saslErrIncorrectEncoding = "incorrect-encoding"
saslErrInvalidAuthzid = "invalid-authzid"
saslErrInvalidMechanism = "invalid-mechanism"
saslErrMalformedRequest = "malformed-request"
saslErrMechanismTooWeak = "mechanism-too-weak"
saslErrNotAuthorized = "not-authorized"
saslErrTemporaryAuthFailure = "temporary-auth-failure"
)
func escapeXml(s string) string {
buf := new(bytes.Buffer)
buf.Grow(len(s))
err := xml.EscapeText(buf, []byte(s))
if err != nil {
panic(err)
}
return buf.String()
}
// Scan XML token stream for next element and save into val.
// If val == nil, allocate new element based on proto map.
// Either way, return val.
func next(p *xml.Decoder) (xml.Name, interface{}, error) {
// Read start element to find out what type we want.
se, err := nextStart(p)
if err != nil {
return xml.Name{}, nil, err
}
// Put it in an interface and allocate one.
var nv interface{}
switch se.Name.Space + " " + se.Name.Local {
/*
case xmppNsStream + " features":
nv = &streamFeatures{}
case xmppNsStream + " error":
nv = &streamError{}
*/
case xmppNsTLS + " starttls":
nv = &tlsStartTLS{}
case xmppNsTLS + " proceed":
nv = &tlsProceed{}
/*
case xmppNsTLS + " failure":
nv = &tlsFailure{}
*/
case xmppNsSASL + " auth":
nv = &saslAuth{}
case xmppNsSASL + " response":
nv = &saslResponse{}
case xmppNsSASL + " abort":
nv = &saslAbort{}
/*
case xmppNsSASL + " mechanisms":
nv = &saslMechanisms{}
case xmppNsSASL + " challenge":
nv = ""
case xmppNsSASL + " response":
nv = ""
case xmppNsSASL + " success":
nv = &saslSuccess{}
case xmppNsSASL + " failure":
nv = &saslFailure{}
case xmppNsBind + " bind":
nv = &bindBind{}
case xnmppNsClient + " message":
nv = &clientMessage{}
case xnmppNsClient + " presence":
nv = &clientPresence{}
case xnmppNsClient + " iq":
nv = &clientIQ{}
case xnmppNsClient + " error":
nv = &clientError{}
*/
default:
return xml.Name{}, nil, errors.New("unexpected XMPP message " +
se.Name.Space + " <" + se.Name.Local + "/>")
}
// Unmarshal into that storage.
if err = p.DecodeElement(nv, &se); err != nil {
return xml.Name{}, nil, err
}
return se.Name, nv, err
}
// Scan XML token stream to find next StartElement.
func nextStart(p *xml.Decoder) (xml.StartElement, error) {
for {
t, err := p.Token()
if err != nil/* && err != io.EOF */{
return xml.StartElement{}, err
}
switch t := t.(type) {
case xml.StartElement:
return t, nil
}
}
panic("unreachable")
}
func decodeStreamStart(e *xml.StartElement) (*streamStart, error) {
/*
<stream:stream
from='juliet@im.example.com'
to='im.example.com'
version='1.0'
xml:lang='en'
xmlns='jabber:client'
xmlns:stream='http://etherx.jabber.org/streams'>
{{http://etherx.jabber.org/streams stream} [{
{ xmlns} jabber:client}
{{xmlns stream} http://etherx.jabber.org/streams}
{{ to} lxtap.com}
{{ version} 1.0}
]}
*/
st := new(streamStart)
st.Name.Space = e.Name.Space
st.Name.Local = e.Name.Local
for i := 0; i < len(e.Attr); i ++ {
attr := e.Attr[i] // Attr{Name,Value}
switch attr.Name.Local {
case "from":
st.From = attr.Value
case "to":
st.To = attr.Value
case "version":
st.Version = attr.Value
case "lang":
st.Lang = attr.Value
}
}
return st, nil
}