~samwhited/xmpp

xmpp/stream/stream.go -rw-r--r-- 1.2 KiB
60a076f3Sam Whited .builds: disable testing against gotip 4 days 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
// Copyright 2020 The Mellium Contributors.
// Use of this source code is governed by the BSD 2-clause
// license that can be found in the LICENSE file.

package stream

import (
	"encoding/xml"

	"mellium.im/xmpp/jid"
)

// Info contains metadata extracted from a stream start token.
type Info struct {
	Name    xml.Name
	XMLNS   string
	To      jid.JID
	From    jid.JID
	ID      string
	Version Version
	Lang    string
}

// FromStartElement sets the data in Info from the provided StartElement.
func (i *Info) FromStartElement(s xml.StartElement) error {
	i.Name = s.Name
	for _, attr := range s.Attr {
		switch attr.Name {
		case xml.Name{Space: "", Local: "xmlns"}:
			i.XMLNS = attr.Value
		case xml.Name{Space: "", Local: "to"}:
			if err := (&i.To).UnmarshalXMLAttr(attr); err != nil {
				return ImproperAddressing
			}
		case xml.Name{Space: "", Local: "from"}:
			if err := (&i.From).UnmarshalXMLAttr(attr); err != nil {
				return ImproperAddressing
			}
		case xml.Name{Space: "", Local: "id"}:
			i.ID = attr.Value
		case xml.Name{Space: "", Local: "version"}:
			err := (&i.Version).UnmarshalXMLAttr(attr)
			if err != nil {
				return BadFormat
			}
		case xml.Name{Space: "xml", Local: "lang"}:
			i.Lang = attr.Value
		}
	}
	return nil
}