~samwhited/xmpp

ref: 00fe2d17ffb3376ac68ef60e66db4fc290b7fc0d xmpp/stream.go -rw-r--r-- 1.8 KiB
00fe2d17Sam Whited Add missing import 6 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
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
package xmpp

import (
	"encoding/xml"
	"errors"

	"bitbucket.org/mellium/xmpp/jid"
)

type Stream struct {
	to, from *jid.EnforcedJID
	version  string
	xmlns    string
	lang     string
	id       string
}

// StreamFromStartElement constructs a new Stream from the given
// xml.StartElement (which must be of the form <stream:stream>).
func StreamFromStartElement(start xml.StartElement) (*Stream, error) {

	if start.Name.Local != "stream" || start.Name.Space != "stream" {
		return nil, errors.New("Start element must be stream:stream")
	}

	stream := &Stream{}
	for _, attr := range start.Attr {
		switch attr.Name.Local {
		case "from":
			j, err := jid.EnforcedFromString(attr.Value)
			if err != nil {
				return nil, err
			}
			stream.from = j
		case "to":
			j, err := jid.EnforcedFromString(attr.Value)
			if err != nil {
				return nil, err
			}
			stream.to = j
		case "xmlns":
			stream.xmlns = attr.Value
		case "lang":
			if attr.Name.Space == "xml" {
				stream.lang = attr.Value
			}
		case "id":
			stream.id = attr.Value
		}
	}

	return stream, nil
}

// StartElement creates an XML start element from the given stream which is
// suitable for starting an XMPP stream.
func (s *Stream) StartElement() xml.StartElement {
	return xml.StartElement{
		Name: xml.Name{"stream", "stream"},
		Attr: []xml.Attr{
			xml.Attr{
				xml.Name{"", "to"},
				s.to.String(),
			},
			xml.Attr{
				xml.Name{"", "from"},
				s.from.String(),
			},
			xml.Attr{
				xml.Name{"", "version"},
				s.version,
			},
			xml.Attr{
				xml.Name{"xml", "lang"},
				s.lang,
			},
			xml.Attr{
				xml.Name{"", "id"},
				s.id,
			},
			xml.Attr{
				xml.Name{"", "xmlns"},
				s.xmlns,
			},
		},
	}
}

func (s *Stream) Handle(encoder *xml.Encoder, decoder *xml.Decoder) error {
	return errors.New("Test me")
}