structs.go
author Chris Jones <chris@cjones.org>
Mon, 26 Dec 2011 11:48:35 -0700
changeset 8 30a7752cf8f7
parent 7 4f0f66f9a441
child 10 f38b0ee7b1c1
permissions -rw-r--r--
Added the ability to parse <stream:features>.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
     1
// Copyright 2011 The Go Authors.  All rights reserved.
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
     2
// Use of this source code is governed by a BSD-style
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
     3
// license that can be found in the LICENSE file.
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
     4
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
     5
package xmpp
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
     6
1
a01e06faf0db Added code to look up SRV records and open a TCP connection.
Chris Jones <chris@cjones.org>
parents: 0
diff changeset
     7
// This file contains data structures.
a01e06faf0db Added code to look up SRV records and open a TCP connection.
Chris Jones <chris@cjones.org>
parents: 0
diff changeset
     8
0
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
     9
import (
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    10
	"bytes"
3
6121aa2f21b1 Made JID implement flag.Value.
Chris Jones <chris@cjones.org>
parents: 2
diff changeset
    11
	"flag"
0
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    12
	"fmt"
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    13
	"io"
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    14
	"os"
3
6121aa2f21b1 Made JID implement flag.Value.
Chris Jones <chris@cjones.org>
parents: 2
diff changeset
    15
	"regexp"
5
faef59c8db05 Added a goroutine to read data from the remote and parse it into
Chris Jones <chris@cjones.org>
parents: 3
diff changeset
    16
	"strings"
0
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    17
	"xml"
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    18
)
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    19
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    20
const (
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    21
	// Version of RFC 3920 that we implement.
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    22
	Version = "1.0"
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    23
	nsStreams = "urn:ietf:params:xml:ns:xmpp-streams"
1
a01e06faf0db Added code to look up SRV records and open a TCP connection.
Chris Jones <chris@cjones.org>
parents: 0
diff changeset
    24
	nsStream = "http://etherx.jabber.org/streams"
a01e06faf0db Added code to look up SRV records and open a TCP connection.
Chris Jones <chris@cjones.org>
parents: 0
diff changeset
    25
	nsTLS = "urn:ietf:params:xml:ns:xmpp-tls"
0
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    26
)
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    27
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    28
// JID represents an entity that can communicate with other
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    29
// entities. It looks like node@domain/resource. Node and resource are
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    30
// sometimes optional.
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    31
type JID struct {
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    32
	Node *string
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    33
	Domain string
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    34
	Resource *string
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    35
}
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    36
var _ fmt.Stringer = &JID{}
3
6121aa2f21b1 Made JID implement flag.Value.
Chris Jones <chris@cjones.org>
parents: 2
diff changeset
    37
var _ flag.Value = &JID{}
0
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    38
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    39
// XMPP's <stream:stream> XML element
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    40
type Stream struct {
6
8e425e340ca1 Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents: 5
diff changeset
    41
	To string `xml:"attr"`
8e425e340ca1 Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents: 5
diff changeset
    42
	From string `xml:"attr"`
8e425e340ca1 Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents: 5
diff changeset
    43
	Id string `xml:"attr"`
8e425e340ca1 Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents: 5
diff changeset
    44
	Lang string `xml:"attr"`
8e425e340ca1 Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents: 5
diff changeset
    45
	Version string `xml:"attr"`
0
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    46
}
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    47
var _ xml.Marshaler = &Stream{}
6
8e425e340ca1 Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents: 5
diff changeset
    48
var _ fmt.Stringer = &Stream{}
0
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    49
6
8e425e340ca1 Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents: 5
diff changeset
    50
// <stream:error>
0
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    51
type StreamError struct {
6
8e425e340ca1 Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents: 5
diff changeset
    52
	Any definedCondition
8e425e340ca1 Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents: 5
diff changeset
    53
	Text *errText
0
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    54
}
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    55
var _ xml.Marshaler = &StreamError{}
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    56
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    57
type definedCondition struct {
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    58
	// Must always be in namespace nsStreams
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    59
	XMLName xml.Name
7
4f0f66f9a441 Support contents for defined error conditions (such as <see-other-host/>).
Chris Jones <christian.jones@sri.com>
parents: 6
diff changeset
    60
	Chardata string `xml:"chardata"`
0
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    61
}
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    62
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    63
type errText struct {
6
8e425e340ca1 Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents: 5
diff changeset
    64
	Lang string `xml:"attr"`
8e425e340ca1 Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents: 5
diff changeset
    65
	Text string `xml:"chardata"`
0
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    66
}
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    67
var _ xml.Marshaler = &errText{}
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    68
8
30a7752cf8f7 Added the ability to parse <stream:features>.
Chris Jones <chris@cjones.org>
parents: 7
diff changeset
    69
type Features struct {
30a7752cf8f7 Added the ability to parse <stream:features>.
Chris Jones <chris@cjones.org>
parents: 7
diff changeset
    70
	Starttls starttls
30a7752cf8f7 Added the ability to parse <stream:features>.
Chris Jones <chris@cjones.org>
parents: 7
diff changeset
    71
	Mechanisms mechs
30a7752cf8f7 Added the ability to parse <stream:features>.
Chris Jones <chris@cjones.org>
parents: 7
diff changeset
    72
}
30a7752cf8f7 Added the ability to parse <stream:features>.
Chris Jones <chris@cjones.org>
parents: 7
diff changeset
    73
30a7752cf8f7 Added the ability to parse <stream:features>.
Chris Jones <chris@cjones.org>
parents: 7
diff changeset
    74
type starttls struct {
30a7752cf8f7 Added the ability to parse <stream:features>.
Chris Jones <chris@cjones.org>
parents: 7
diff changeset
    75
	required *string
30a7752cf8f7 Added the ability to parse <stream:features>.
Chris Jones <chris@cjones.org>
parents: 7
diff changeset
    76
}
30a7752cf8f7 Added the ability to parse <stream:features>.
Chris Jones <chris@cjones.org>
parents: 7
diff changeset
    77
30a7752cf8f7 Added the ability to parse <stream:features>.
Chris Jones <chris@cjones.org>
parents: 7
diff changeset
    78
type mechs struct {
30a7752cf8f7 Added the ability to parse <stream:features>.
Chris Jones <chris@cjones.org>
parents: 7
diff changeset
    79
	Mechanism []string
30a7752cf8f7 Added the ability to parse <stream:features>.
Chris Jones <chris@cjones.org>
parents: 7
diff changeset
    80
}
30a7752cf8f7 Added the ability to parse <stream:features>.
Chris Jones <chris@cjones.org>
parents: 7
diff changeset
    81
5
faef59c8db05 Added a goroutine to read data from the remote and parse it into
Chris Jones <chris@cjones.org>
parents: 3
diff changeset
    82
type Unrecognized struct {
faef59c8db05 Added a goroutine to read data from the remote and parse it into
Chris Jones <chris@cjones.org>
parents: 3
diff changeset
    83
	XMLName xml.Name
faef59c8db05 Added a goroutine to read data from the remote and parse it into
Chris Jones <chris@cjones.org>
parents: 3
diff changeset
    84
}
8
30a7752cf8f7 Added the ability to parse <stream:features>.
Chris Jones <chris@cjones.org>
parents: 7
diff changeset
    85
var _ fmt.Stringer = &Unrecognized{}
5
faef59c8db05 Added a goroutine to read data from the remote and parse it into
Chris Jones <chris@cjones.org>
parents: 3
diff changeset
    86
0
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    87
func (jid *JID) String() string {
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    88
	result := jid.Domain
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    89
	if jid.Node != nil {
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    90
		result = *jid.Node + "@" + result
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    91
	}
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    92
	if jid.Resource != nil {
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    93
		result = result + "/" + *jid.Resource
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    94
	}
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    95
	return result
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    96
}
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    97
3
6121aa2f21b1 Made JID implement flag.Value.
Chris Jones <chris@cjones.org>
parents: 2
diff changeset
    98
func (jid *JID) Set(val string) bool {
6121aa2f21b1 Made JID implement flag.Value.
Chris Jones <chris@cjones.org>
parents: 2
diff changeset
    99
	r := regexp.MustCompile("^(([^@/]+)@)?([^@/]+)(/([^@/]+))?$")
6121aa2f21b1 Made JID implement flag.Value.
Chris Jones <chris@cjones.org>
parents: 2
diff changeset
   100
	parts := r.FindStringSubmatch(val)
6121aa2f21b1 Made JID implement flag.Value.
Chris Jones <chris@cjones.org>
parents: 2
diff changeset
   101
	if parts == nil {
6121aa2f21b1 Made JID implement flag.Value.
Chris Jones <chris@cjones.org>
parents: 2
diff changeset
   102
		return false
6121aa2f21b1 Made JID implement flag.Value.
Chris Jones <chris@cjones.org>
parents: 2
diff changeset
   103
	}
6121aa2f21b1 Made JID implement flag.Value.
Chris Jones <chris@cjones.org>
parents: 2
diff changeset
   104
	if parts[2] == "" {
6121aa2f21b1 Made JID implement flag.Value.
Chris Jones <chris@cjones.org>
parents: 2
diff changeset
   105
		jid.Node = nil
6121aa2f21b1 Made JID implement flag.Value.
Chris Jones <chris@cjones.org>
parents: 2
diff changeset
   106
	} else {
6121aa2f21b1 Made JID implement flag.Value.
Chris Jones <chris@cjones.org>
parents: 2
diff changeset
   107
		jid.Node = &parts[2]
6121aa2f21b1 Made JID implement flag.Value.
Chris Jones <chris@cjones.org>
parents: 2
diff changeset
   108
	}
6121aa2f21b1 Made JID implement flag.Value.
Chris Jones <chris@cjones.org>
parents: 2
diff changeset
   109
	jid.Domain = parts[3]
6121aa2f21b1 Made JID implement flag.Value.
Chris Jones <chris@cjones.org>
parents: 2
diff changeset
   110
	if parts[5] == "" {
6121aa2f21b1 Made JID implement flag.Value.
Chris Jones <chris@cjones.org>
parents: 2
diff changeset
   111
		jid.Resource = nil
6121aa2f21b1 Made JID implement flag.Value.
Chris Jones <chris@cjones.org>
parents: 2
diff changeset
   112
	} else {
6121aa2f21b1 Made JID implement flag.Value.
Chris Jones <chris@cjones.org>
parents: 2
diff changeset
   113
		jid.Resource = &parts[5]
6121aa2f21b1 Made JID implement flag.Value.
Chris Jones <chris@cjones.org>
parents: 2
diff changeset
   114
	}
6121aa2f21b1 Made JID implement flag.Value.
Chris Jones <chris@cjones.org>
parents: 2
diff changeset
   115
	return true
6121aa2f21b1 Made JID implement flag.Value.
Chris Jones <chris@cjones.org>
parents: 2
diff changeset
   116
}
6121aa2f21b1 Made JID implement flag.Value.
Chris Jones <chris@cjones.org>
parents: 2
diff changeset
   117
0
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   118
func (s *Stream) MarshalXML() ([]byte, os.Error) {
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   119
	buf := bytes.NewBuffer(nil)
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   120
	buf.WriteString("<stream:stream")
6
8e425e340ca1 Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents: 5
diff changeset
   121
	writeField(buf, "xmlns", "jabber:client")
8e425e340ca1 Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents: 5
diff changeset
   122
	writeField(buf, "xmlns:stream", nsStream)
8e425e340ca1 Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents: 5
diff changeset
   123
	writeField(buf, "to", s.To)
8e425e340ca1 Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents: 5
diff changeset
   124
	writeField(buf, "from", s.From)
8e425e340ca1 Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents: 5
diff changeset
   125
	writeField(buf, "id", s.Id)
8e425e340ca1 Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents: 5
diff changeset
   126
	writeField(buf, "xml:lang", s.Lang)
8e425e340ca1 Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents: 5
diff changeset
   127
	writeField(buf, "version", s.Version)
0
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   128
	buf.WriteString(">")
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   129
	// We never write </stream:stream>
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   130
	return buf.Bytes(), nil
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   131
}
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   132
6
8e425e340ca1 Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents: 5
diff changeset
   133
func (s *Stream) String() string {
8e425e340ca1 Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents: 5
diff changeset
   134
	result, _ := s.MarshalXML()
8e425e340ca1 Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents: 5
diff changeset
   135
	return string(result)
8e425e340ca1 Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents: 5
diff changeset
   136
}
8e425e340ca1 Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents: 5
diff changeset
   137
5
faef59c8db05 Added a goroutine to read data from the remote and parse it into
Chris Jones <chris@cjones.org>
parents: 3
diff changeset
   138
func parseStream(se xml.StartElement) (*Stream, os.Error) {
faef59c8db05 Added a goroutine to read data from the remote and parse it into
Chris Jones <chris@cjones.org>
parents: 3
diff changeset
   139
	s := &Stream{}
faef59c8db05 Added a goroutine to read data from the remote and parse it into
Chris Jones <chris@cjones.org>
parents: 3
diff changeset
   140
	for _, attr := range se.Attr {
faef59c8db05 Added a goroutine to read data from the remote and parse it into
Chris Jones <chris@cjones.org>
parents: 3
diff changeset
   141
		switch strings.ToLower(attr.Name.Local) {
faef59c8db05 Added a goroutine to read data from the remote and parse it into
Chris Jones <chris@cjones.org>
parents: 3
diff changeset
   142
		case "to":
6
8e425e340ca1 Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents: 5
diff changeset
   143
			s.To = attr.Value
5
faef59c8db05 Added a goroutine to read data from the remote and parse it into
Chris Jones <chris@cjones.org>
parents: 3
diff changeset
   144
		case "from":
6
8e425e340ca1 Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents: 5
diff changeset
   145
			s.From = attr.Value
5
faef59c8db05 Added a goroutine to read data from the remote and parse it into
Chris Jones <chris@cjones.org>
parents: 3
diff changeset
   146
		case "id":
6
8e425e340ca1 Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents: 5
diff changeset
   147
			s.Id = attr.Value
5
faef59c8db05 Added a goroutine to read data from the remote and parse it into
Chris Jones <chris@cjones.org>
parents: 3
diff changeset
   148
		case "lang":
6
8e425e340ca1 Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents: 5
diff changeset
   149
			s.Lang = attr.Value
5
faef59c8db05 Added a goroutine to read data from the remote and parse it into
Chris Jones <chris@cjones.org>
parents: 3
diff changeset
   150
		case "version":
6
8e425e340ca1 Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents: 5
diff changeset
   151
			s.Version = attr.Value
5
faef59c8db05 Added a goroutine to read data from the remote and parse it into
Chris Jones <chris@cjones.org>
parents: 3
diff changeset
   152
		}
faef59c8db05 Added a goroutine to read data from the remote and parse it into
Chris Jones <chris@cjones.org>
parents: 3
diff changeset
   153
	}
faef59c8db05 Added a goroutine to read data from the remote and parse it into
Chris Jones <chris@cjones.org>
parents: 3
diff changeset
   154
	return s, nil
faef59c8db05 Added a goroutine to read data from the remote and parse it into
Chris Jones <chris@cjones.org>
parents: 3
diff changeset
   155
}
faef59c8db05 Added a goroutine to read data from the remote and parse it into
Chris Jones <chris@cjones.org>
parents: 3
diff changeset
   156
0
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   157
func (s *StreamError) MarshalXML() ([]byte, os.Error) {
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   158
	buf := bytes.NewBuffer(nil)
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   159
	buf.WriteString("<stream:error>")
6
8e425e340ca1 Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents: 5
diff changeset
   160
	xml.Marshal(buf, s.Any)
8e425e340ca1 Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents: 5
diff changeset
   161
	if s.Text != nil {
8e425e340ca1 Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents: 5
diff changeset
   162
		xml.Marshal(buf, s.Text)
2
4dabfef08c8c Forgot to add the new xmpp.go from my last commit. Also added some
Chris Jones <chris@cjones.org>
parents: 1
diff changeset
   163
	}
0
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   164
	buf.WriteString("</stream:error>")
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   165
	return buf.Bytes(), nil
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   166
}
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   167
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   168
func (e *errText) MarshalXML() ([]byte, os.Error) {
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   169
	buf := bytes.NewBuffer(nil)
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   170
	buf.WriteString("<text")
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   171
	writeField(buf, "xmlns", nsStreams)
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   172
	writeField(buf, "xml:lang", e.Lang)
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   173
	buf.WriteString(">")
6
8e425e340ca1 Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents: 5
diff changeset
   174
	xml.Escape(buf, []byte(e.Text))
0
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   175
	buf.WriteString("</text>")
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   176
	return buf.Bytes(), nil
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   177
}
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   178
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   179
func writeField(w io.Writer, field, value string) {
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   180
	if value != "" {
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   181
		io.WriteString(w, " ")
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   182
		io.WriteString(w, field)
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   183
		io.WriteString(w, `="`)
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   184
		xml.Escape(w, []byte(value))
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   185
		io.WriteString(w, `"`)
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   186
	}
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   187
}
8
30a7752cf8f7 Added the ability to parse <stream:features>.
Chris Jones <chris@cjones.org>
parents: 7
diff changeset
   188
30a7752cf8f7 Added the ability to parse <stream:features>.
Chris Jones <chris@cjones.org>
parents: 7
diff changeset
   189
func (u *Unrecognized) String() string {
30a7752cf8f7 Added the ability to parse <stream:features>.
Chris Jones <chris@cjones.org>
parents: 7
diff changeset
   190
	return fmt.Sprintf("unrecognized{%s %s}", u.XMLName.Space,
30a7752cf8f7 Added the ability to parse <stream:features>.
Chris Jones <chris@cjones.org>
parents: 7
diff changeset
   191
		u.XMLName.Local)
30a7752cf8f7 Added the ability to parse <stream:features>.
Chris Jones <chris@cjones.org>
parents: 7
diff changeset
   192
}