forked from cxindex/xmpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xmpp.go
834 lines (716 loc) · 22.7 KB
/
xmpp.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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
// Package xmpp implements the XMPP IM protocol, as specified in RFC 6120 and 6121.
// forked from github.com/agl/xmpp
package xmpp
import (
"bytes"
"crypto/tls"
"crypto/x509"
"encoding/base64"
"encoding/xml"
"errors"
"fmt"
"io"
"net"
"strconv"
"strings"
"sync"
)
const (
nsStream = "http://etherx.jabber.org/streams"
nsTLS = "urn:ietf:params:xml:ns:xmpp-tls"
nsSASL = "urn:ietf:params:xml:ns:xmpp-sasl"
nsBind = "urn:ietf:params:xml:ns:xmpp-bind"
nsSession = "urn:ietf:params:xml:ns:xmpp-session"
nsClient = "jabber:client"
)
// RemoveResourceFromJid returns the user@domain portion of a JID.
func RemoveResourceFromJid(jid string) string {
slash := strings.Index(jid, "/")
if slash != -1 {
return jid[:slash]
}
return jid
}
// Conn represents a connection to an XMPP server.
type Conn struct {
net.Conn
out io.Writer
rawOut io.Writer // doesn't log. Used for <auth>
in *xml.Decoder
jid string
nextCookie Cookie
lock sync.Mutex
inflights map[Cookie]chan<- Stanza
}
// Stanza represents a message from the XMPP server.
type Stanza struct {
Name xml.Name
Value interface{}
}
// Cookie is used to give a unique identifier to each request.
type Cookie uint64
func (c *Conn) getCookie() Cookie {
ret := c.nextCookie
c.nextCookie++
return ret
}
// Next reads stanzas from the server. If the stanza is a reply, it dispatches
// it to the correct channel and reads the next message. Otherwise it returns
// the stanza for processing.
func (c *Conn) Next() (stanza Stanza, err error) {
for {
if stanza.Name, stanza.Value, err = next(c.in); err != nil {
return
}
if iq, ok := stanza.Value.(*ClientIQ); ok && iq.Type == "error" {
var cookieValue uint64
if cookieValue, err = strconv.ParseUint(iq.Id, 10, 64); err != nil {
err = errors.New("xmpp: failed to parse id from iq: " + err.Error())
return
}
cookie := Cookie(cookieValue)
c.lock.Lock()
ch, ok := c.inflights[cookie]
if ok {
delete(c.inflights, cookie)
}
c.lock.Unlock()
if ok {
ch <- stanza
}
continue
}
return
}
panic("unreachable")
}
// Cancel cancels and outstanding request. The request's channel is closed.
func (c *Conn) Cancel(cookie Cookie) bool {
c.lock.Lock()
defer c.lock.Unlock()
ch, ok := c.inflights[cookie]
if !ok {
return false
}
delete(c.inflights, cookie)
close(ch)
return true
}
// RequestRoster requests the user's roster from the server. It returns a
// channel on which the reply can be read when received and a Cookie that can
// be used to cancel the request.
func (c *Conn) RequestRoster() (<-chan Stanza, Cookie, error) {
cookie := c.getCookie()
if _, err := fmt.Fprintf(c.out, "<iq type='get' id='%x'><query xmlns='jabber:iq:roster'/></iq>", cookie); err != nil {
return nil, 0, err
}
c.lock.Lock()
defer c.lock.Unlock()
ch := make(chan Stanza, 1)
c.inflights[cookie] = ch
return ch, cookie, nil
}
// ParseRoster extracts roster information from the given Stanza.
func ParseRoster(reply Stanza) ([]RosterEntry, error) {
iq, ok := reply.Value.(*ClientIQ)
if !ok {
return nil, errors.New("xmpp: roster request resulted in tag of type " + reply.Name.Local)
}
var roster Roster
if err := xml.NewDecoder(bytes.NewBuffer(iq.Query)).Decode(&roster); err != nil {
return nil, err
}
return roster.Item, nil
}
// SendIQ sends an info/query message to the given user. It returns a channel
// on which the reply can be read when received and a Cookie that can be used
// to cancel the request.
func (c *Conn) SendIQ(to, typ string, value interface{}) (reply chan Stanza, cookie Cookie, err error) {
c.lock.Lock()
defer c.lock.Unlock()
cookie = c.getCookie()
reply = make(chan Stanza, 1)
toAttr := ""
if len(to) > 0 {
toAttr = "to='" + xmlEscape(to) + "'"
}
if _, err = fmt.Fprintf(c.out, "<iq %s from='%s' type='%s' id='%d'>", toAttr, xmlEscape(c.jid), xmlEscape(typ), cookie); err != nil {
return
}
if _, ok := value.(EmptyReply); !ok {
if _, ok := value.(string); !ok {
if err = xml.NewEncoder(c.out).Encode(value); err != nil {
return
}
} else {
if _, err = fmt.Fprintf(c.out, value.(string)); err != nil {
return
}
}
}
if _, err = fmt.Fprintf(c.out, "</iq>"); err != nil {
return
}
c.inflights[cookie] = reply
return
}
// SendIQReply sends a reply to an IQ query.
func (c *Conn) SendIQReply(to, typ, id string, value interface{}) error {
if _, err := fmt.Fprintf(c.out, "<iq to='%s' from='%s' type='%s' id='%s'>", xmlEscape(to), xmlEscape(c.jid), xmlEscape(typ), xmlEscape(id)); err != nil {
return err
}
if _, ok := value.(EmptyReply); !ok {
if err := xml.NewEncoder(c.out).Encode(value); err != nil {
return err
}
}
_, err := fmt.Fprintf(c.out, "</iq>")
return err
}
// Send sends an IM message to the given user.
func (c *Conn) Send(to, typ, msg string) error {
_, err := fmt.Fprintf(c.out, "<message to='%s' from='%s' type='%s'><body>%s</body></message>", xmlEscape(to), xmlEscape(c.jid), xmlEscape(typ), xmlEscape(msg))
return err
}
func (c *Conn) SetSubject(to, subject string) error {
id := strconv.FormatUint(uint64(c.getCookie()), 10)
_, err := fmt.Fprintf(c.out, "<message from='%s' id='%s' to='%s' type='groupchat'><subject>%s</subject></message>", xmlEscape(c.jid), xmlEscape(id), xmlEscape(to), xmlEscape(subject))
return err
}
//possible roles: none (kick), participant (voice), visitor (no-voice)
func (c *Conn) ModUse(to, nick, role, reason string) error {
_, _, err := c.SendIQ(to, "set", ClientQuery{
Attr: "http://jabber.org/protocol/muc#admin",
Query: []byte(fmt.Sprintf("<item nick='%s' role='%s'><reason>%s</reason></item>", xmlEscape(nick), xmlEscape(role), xmlEscape(reason))),
})
return err
}
//possible affiliations: owner, admin, member, outcast, none
func (c *Conn) AdminUse(to, jid, affiliation, reason string) error {
_, _, err := c.SendIQ(to, "set", ClientQuery{
Attr: "http://jabber.org/protocol/muc#admin",
Query: []byte(fmt.Sprintf("<item affiliation='%s' jid='%s'><reason>%s</reason></item>", xmlEscape(affiliation), xmlEscape(jid), xmlEscape(reason))),
})
return err
}
// SendPresence sends a presence stanza. If id is empty, a unique id is
// generated.
func (c *Conn) SendPresence(to, typ string) error {
id := strconv.FormatUint(uint64(c.getCookie()), 10)
var err error
if len(typ) == 0 { //room
_, err = fmt.Fprintf(c.out, "<presence from='%s' id='%s' to='%s'/>", xmlEscape(c.jid), xmlEscape(id), xmlEscape(to))
} else {
_, err = fmt.Fprintf(c.out, "<presence from='%s' id='%s' to='%s' type='%s'/>", xmlEscape(c.jid), xmlEscape(id), xmlEscape(to), xmlEscape(typ))
}
return err
}
func (c *Conn) SignalPresence(state, status string, priority int) error {
_, err := fmt.Fprintf(c.out, "<presence><show>%s</show><status>%s</status><priority>%v</priority></presence>", xmlEscape(state), xmlEscape(status), priority)
return err
}
// rfc3920 section 5.2
func (c *Conn) getFeatures(domain string) (features streamFeatures, err error) {
if _, err = fmt.Fprintf(c.out, "<?xml version='1.0'?><stream:stream to='%s' xmlns='%s' xmlns:stream='%s' version='1.0'>\n", xmlEscape(domain), nsClient, nsStream); err != nil {
return
}
se, err := nextStart(c.in)
if err != nil {
return
}
if se.Name.Space != nsStream || se.Name.Local != "stream" {
err = errors.New("xmpp: expected <stream> but got <" + se.Name.Local + "> in " + se.Name.Space)
return
}
// Now we're in the stream and can use Unmarshal.
// Next message should be <features> to tell us authentication options.
// See section 4.6 in RFC 3920.
if err = c.in.DecodeElement(&features, nil); err != nil {
err = errors.New("unmarshal <features>: " + err.Error())
return
}
return
}
func (c *Conn) authenticate(features streamFeatures, user, password string) (err error) {
havePlain := false
for _, m := range features.Mechanisms.Mechanism {
if m == "PLAIN" {
havePlain = true
break
}
}
if !havePlain {
return errors.New("xmpp: PLAIN authentication is not an option")
}
// Plain authentication: send base64-encoded \x00 user \x00 password.
raw := "\x00" + user + "\x00" + password
enc := make([]byte, base64.StdEncoding.EncodedLen(len(raw)))
base64.StdEncoding.Encode(enc, []byte(raw))
fmt.Fprintf(c.rawOut, "<auth xmlns='%s' mechanism='PLAIN'>%s</auth>\n", nsSASL, enc)
// Next message should be either success or failure.
name, val, err := next(c.in)
switch v := val.(type) {
case *saslSuccess:
case *saslFailure:
// v.Any is type of sub-element in failure,
// which gives a description of what failed.
return errors.New("xmpp: authentication failure: " + v.Any.Local)
default:
return errors.New("expected <success> or <failure>, got <" + name.Local + "> in " + name.Space)
}
return nil
}
func certName(cert *x509.Certificate) string {
name := cert.Subject
ret := ""
for _, org := range name.Organization {
ret += "O=" + org + "/"
}
for _, ou := range name.OrganizationalUnit {
ret += "OU=" + ou + "/"
}
if len(name.CommonName) > 0 {
ret += "CN=" + name.CommonName + "/"
}
return ret
}
// Resolve performs a DNS SRV lookup for the XMPP server that serves the given
// domain.
func Resolve(domain string) (host string, port uint16, err error) {
_, addrs, err := net.LookupSRV("xmpp-client", "tcp", domain)
if err != nil {
return "", 0, err
}
if len(addrs) == 0 {
return "", 0, errors.New("xmpp: no SRV records found for " + domain)
}
return addrs[0].Target, addrs[0].Port, nil
}
// Config contains options for an XMPP connection.
type Config struct {
// Conn is the connection to the server, if non-nill.
Conn net.Conn
// InLog is an optional Writer which receives the raw contents of the
// XML from the server.
InLog io.Writer
// OutLog is an option Writer which receives the raw XML sent to the
// server.
OutLog io.Writer
// Log is an optional Writer which receives human readable log messages
// during the connection.
Log io.Writer
// Create, if true, causes a new account to be created on the server.
Create bool
// TrustedAddress, if true, means that the address passed to Dial is
// trusted and that certificates for that name should be accepted.
TrustedAddress bool
// Skip TLS encryption
SkipTLS bool
}
// Dial creates a new connection to an XMPP server and authenticates as the
// given user.
func Dial(address, user, domain, password, resource string, config *Config) (c *Conn, err error) {
c = new(Conn)
c.inflights = make(map[Cookie]chan<- Stanza)
var log io.Writer
if config != nil && config.Log != nil {
log = config.Log
}
var conn net.Conn
if config != nil && config.Conn != nil {
conn = config.Conn
} else {
if log != nil {
io.WriteString(log, "Making TCP connection to "+address+"\n")
}
if conn, err = net.Dial("tcp", address); err != nil {
return nil, err
}
}
c.Conn = conn
c.in, c.out = makeInOut(conn, config)
features, err := c.getFeatures(domain)
if err != nil {
return nil, err
}
if features.StartTLS.XMLName.Local == "" {
return nil, errors.New("xmpp: server doesn't support TLS")
}
fmt.Fprintf(c.out, "<starttls xmlns='%s'/>", nsTLS)
proceed, err := nextStart(c.in)
if err != nil {
return nil, err
}
if proceed.Name.Space != nsTLS || proceed.Name.Local != "proceed" {
return nil, errors.New("xmpp: expected <proceed> after <starttls> but got <" + proceed.Name.Local + "> in " + proceed.Name.Space)
}
if log != nil {
io.WriteString(log, "Starting TLS handshake\n")
}
var tlsConn *tls.Conn
if config != nil && config.SkipTLS {
tlsConn = tls.Client(conn, &tls.Config{InsecureSkipVerify: true})
} else {
tlsConn = tls.Client(conn, &tls.Config{ServerName: domain})
}
if err := tlsConn.Handshake(); err != nil {
return nil, err
}
tlsState := tlsConn.ConnectionState()
if len(tlsState.VerifiedChains) == 0 && !config.SkipTLS {
return nil, errors.New("xmpp: failed to verify TLS certificate")
}
if log != nil && !config.SkipTLS {
for i, cert := range tlsState.VerifiedChains[0] {
fmt.Fprintf(log, " certificate %d: %s\n", i, certName(cert))
}
}
if err := tlsConn.VerifyHostname(domain); err != nil {
if config.TrustedAddress {
if log != nil {
fmt.Fprintf(log, "Certificate fails to verify against domain in username: %s\n", err)
}
host, _, err := net.SplitHostPort(address)
if err != nil {
return nil, errors.New("xmpp: failed to split address when checking whether TLS certificate is valid: " + err.Error())
}
if err = tlsConn.VerifyHostname(host); err != nil {
return nil, errors.New("xmpp: failed to match TLS certificate to address after failing to match to username: " + err.Error())
}
if log != nil {
fmt.Fprintf(log, "Certificate matches against trusted server hostname: %s\n", host)
}
} else {
return nil, errors.New("xmpp: failed to match TLS certificate to name: " + err.Error())
}
}
c.in, c.out = makeInOut(tlsConn, config)
c.rawOut = tlsConn
if features, err = c.getFeatures(domain); err != nil {
return nil, err
}
if config != nil && config.Create {
if log != nil {
io.WriteString(log, "Attempting to create account\n")
}
fmt.Fprintf(c.rawOut, "<iq type='set' id='create_1'><query xmlns='jabber:iq:register'><username>%s</username><password>%s</password></query></iq>", user, password)
var iq ClientIQ
if err = c.in.DecodeElement(&iq, nil); err != nil {
return nil, errors.New("unmarshal <iq>: " + err.Error())
}
if iq.Type == "error" {
return nil, errors.New("xmpp: account creation failed")
}
}
if log != nil {
io.WriteString(log, "Authenticating as "+user+"\n")
}
if err := c.authenticate(features, user, password); err != nil {
return nil, err
}
if log != nil {
io.WriteString(log, "Authentication successful\n")
}
if features, err = c.getFeatures(domain); err != nil {
return nil, err
}
// Send IQ message asking to bind to the local user name.
fmt.Fprintf(c.out, "<iq type='set' id='bind_2'><bind xmlns='%s'><resource>%s</resource></bind></iq>", nsBind, resource)
var iq ClientIQ
if err = c.in.DecodeElement(&iq, nil); err != nil {
return nil, errors.New("unmarshal <iq>: " + err.Error())
}
if &iq.Bind == nil {
return nil, errors.New("<iq> result missing <bind>")
}
c.jid = iq.Bind.Jid // our local id
if features.Session != nil {
// The server needs a session to be established. See RFC 3921,
// section 3.
fmt.Fprintf(c.out, "<iq to='%s' type='set' id='sess_1'><session xmlns='%s'/></iq>", domain, nsSession)
if err = c.in.DecodeElement(&iq, nil); err != nil {
return nil, errors.New("xmpp: unmarshal <iq>: " + err.Error())
}
if iq.Type != "result" {
return nil, errors.New("xmpp: session establishment failed")
}
}
return c, nil
}
func makeInOut(conn io.ReadWriter, config *Config) (in *xml.Decoder, out io.Writer) {
if config != nil && config.InLog != nil {
in = xml.NewDecoder(io.TeeReader(conn, config.InLog))
} else {
in = xml.NewDecoder(conn)
}
if config != nil && config.OutLog != nil {
out = io.MultiWriter(conn, config.OutLog)
} else {
out = conn
}
return
}
var xmlSpecial = map[byte]string{
'<': "<",
'>': ">",
'"': """,
'\'': "'",
'&': "&",
}
func xmlEscape(s string) string {
var b bytes.Buffer
for i := 0; i < len(s); i++ {
c := s[i]
if s, ok := xmlSpecial[c]; ok {
b.WriteString(s)
} else {
b.WriteByte(c)
}
}
return b.String()
}
// Scan XML token stream to find next StartElement.
func nextStart(p *xml.Decoder) (elem xml.StartElement, err error) {
for {
var t xml.Token
t, err = p.Token()
if err != nil {
return
}
switch t := t.(type) {
case xml.StartElement:
elem = t
return
}
}
panic("unreachable")
}
// RFC 3920 C.1 Streams name space
type streamFeatures struct {
XMLName xml.Name `xml:"http://etherx.jabber.org/streams features"`
StartTLS tlsStartTLS
Mechanisms saslMechanisms
Bind bindBind
// This is a hack for now to get around the fact that the new encoding/xml
// doesn't unmarshal to XMLName elements.
Session *string `xml:"session"`
}
type streamError struct {
XMLName xml.Name `xml:"http://etherx.jabber.org/streams error"`
Any xml.Name `xml:",any"`
Text string `xml:"text"`
}
// RFC 3920 C.3 TLS name space
type tlsStartTLS struct {
XMLName xml.Name `xml:"urn:ietf:params:xml:ns:xmpp-tls starttls"`
Required xml.Name `xml:"required"`
}
type tlsProceed struct {
XMLName xml.Name `xml:"urn:ietf:params:xml:ns:xmpp-tls proceed"`
}
type tlsFailure struct {
XMLName xml.Name `xml:"urn:ietf:params:xml:ns:xmpp-tls failure"`
}
// RFC 3920 C.4 SASL name space
type saslMechanisms struct {
XMLName xml.Name `xml:"urn:ietf:params:xml:ns:xmpp-sasl mechanisms"`
Mechanism []string `xml:"mechanism"`
}
type saslAuth struct {
XMLName xml.Name `xml:"urn:ietf:params:xml:ns:xmpp-sasl auth"`
Mechanism string `xml:"mechanism,attr"`
}
type saslChallenge string
type saslResponse string
type saslAbort struct {
XMLName xml.Name `xml:"urn:ietf:params:xml:ns:xmpp-sasl abort"`
}
type saslSuccess struct {
XMLName xml.Name `xml:"urn:ietf:params:xml:ns:xmpp-sasl success"`
}
type saslFailure struct {
XMLName xml.Name `xml:"urn:ietf:params:xml:ns:xmpp-sasl failure"`
Any xml.Name `xml:",any"`
}
// RFC 3920 C.5 Resource binding name space
type bindBind struct {
XMLName xml.Name `xml:"urn:ietf:params:xml:ns:xmpp-bind bind"`
Resource string `xml:"resource"`
Jid string `xml:"jid"`
}
// RFC 3921 B.1 jabber:client
type ClientMessage struct {
XMLName xml.Name `xml:"jabber:client message"`
From string `xml:"from,attr"`
Id string `xml:"id,attr"`
To string `xml:"to,attr"`
Type string `xml:"type,attr"` // chat, error, groupchat, headline, or normal
Subject string `xml:"subject"`
Body string `xml:"body"`
Thread string `xml:"thread"`
Delay Delayed `xml:"delay"`
}
type Delayed struct {
Xmlns string `xml:"xmlns,attr"`
From string `xml:"from,attr"`
Stamp string `xml:"stamp,attr"`
}
type ClientText struct {
Lang string `xml:"lang,attr"`
Body string `xml:",chardata"`
}
type ClientPresence struct {
XMLName xml.Name `xml:"jabber:client presence"`
From string `xml:"from,attr"`
Id string `xml:"id,attr"`
To string `xml:"to,attr"`
Type string `xml:"type,attr"` // error, probe, subscribe, subscribed, unavailable, unsubscribe, unsubscribed
Lang string `xml:"lang,attr"`
Show string `xml:"show"` // away, chat, dnd, xa
Status string `xml:"status"` // sb []clientText
Priority string `xml:"priority"`
Error *ClientError `xml:"error"`
Item Item `xml:"x>item"`
}
type ClientIQ struct { // info/query
XMLName xml.Name `xml:"jabber:client iq"`
From string `xml:"from,attr"`
Id string `xml:"id,attr"`
To string `xml:"to,attr"`
Type string `xml:"type,attr"` // error, get, result, set
Query []byte `xml:",innerxml"`
Error ClientError `xml:"error"`
Bind bindBind `xml:"bind"`
}
type ClientQuery struct { //just clear query to send
XMLName xml.Name `xml:"query"`
Attr string `xml:"xmlns,attr"`
Query []byte `xml:",innerxml"`
}
type Item struct {
Nick string `xml:"nick,attr,omitempty"`
Affiliation string `xml:"affiliation,attr,omitempty"`
Role string `xml:"role,attr,omitempty"`
}
type ClientError struct {
XMLName xml.Name `xml:"jabber:client error"`
Code string `xml:"code,attr"`
Type string `xml:"type,attr"`
Any xml.Name `xml:",any"`
Text string `xml:"text"`
}
type Roster struct {
XMLName xml.Name `xml:"jabber:iq:roster query"`
Item []RosterEntry `xml:"item"`
}
type RosterEntry struct {
Jid string `xml:"jid,attr"`
Subscription string `xml:"subscription,attr"`
Name string `xml:"name,attr"`
Group []string `xml:"group"`
}
// 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 nsStream + " features":
nv = &streamFeatures{}
case nsStream + " error":
nv = &streamError{}
case nsTLS + " starttls":
nv = &tlsStartTLS{}
case nsTLS + " proceed":
nv = &tlsProceed{}
case nsTLS + " failure":
nv = &tlsFailure{}
case nsSASL + " mechanisms":
nv = &saslMechanisms{}
case nsSASL + " challenge":
nv = ""
case nsSASL + " response":
nv = ""
case nsSASL + " abort":
nv = &saslAbort{}
case nsSASL + " success":
nv = &saslSuccess{}
case nsSASL + " failure":
nv = &saslFailure{}
case nsBind + " bind":
nv = &bindBind{}
case nsClient + " message":
nv = &ClientMessage{}
case nsClient + " presence":
nv = &ClientPresence{}
case nsClient + " iq":
nv = &ClientIQ{}
case nsClient + " 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
}
type DiscoveryReply struct {
XMLName xml.Name `xml:"http://jabber.org/protocol/disco#info query"`
Identities []DiscoveryIdentity
Features []DiscoveryFeature
}
type DiscoveryIdentity struct {
XMLName xml.Name `xml:"http://jabber.org/protocol/disco#info identity"`
Category string `xml:"category,attr"`
Type string `xml:"type,attr"`
Name string `xml:"name,attr"`
}
type DiscoveryFeature struct {
XMLName xml.Name `xml:"http://jabber.org/protocol/disco#info feature"`
Var string `xml:"var,attr"`
}
type VersionQuery struct {
XMLName xml.Name `xml:"jabber:iq:version query"`
}
type VersionReply struct {
XMLName xml.Name `xml:"jabber:iq:version query"`
Name string `xml:"name"`
Version string `xml:"version"`
OS string `xml:"os"`
}
// ErrorReply reflects an XMPP error stanza. See
// http://xmpp.org/rfcs/rfc6120.html#stanzas-error-syntax
type ErrorReply struct {
XMLName xml.Name `xml:"error"`
Type string `xml:"type,attr"`
Error interface{} `xml:"error"`
}
// ErrorBadRequest reflects a bad-request stanza. See
// http://xmpp.org/rfcs/rfc6120.html#stanzas-error-conditions-bad-request
type ErrorBadRequest struct {
XMLName xml.Name `xml:"urn:ietf:params:xml:ns:xmpp-stanzas bad-request"`
}
// RosterRequest is used to request that the server update the user's roster.
// See RFC 6121, section 2.3.
type RosterRequest struct {
XMLName xml.Name `xml:"jabber:iq:roster query"`
Item RosterRequestItem `xml:"item"`
}
type RosterRequestItem struct {
Jid string `xml:"jid,attr"`
Subscription string `xml:"subscription,attr"`
Name string `xml:"name,attr"`
Group []string `xml:"group"`
}
// An EmptyReply results in in no XML.
type EmptyReply struct {
}