structs.go
author Chris Jones <christian.jones@sri.com>
Sun, 25 Dec 2011 18:46:13 -0700
changeset 6 8e425e340ca1
parent 5 faef59c8db05
child 7 4f0f66f9a441
permissions -rw-r--r--
Implemented writing to the remote. Now we have bidirectional communication.
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
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    60
}
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
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
    63
	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
    64
	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
    65
}
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    66
var _ xml.Marshaler = &errText{}
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    67
5
faef59c8db05 Added a goroutine to read data from the remote and parse it into
Chris Jones <chris@cjones.org>
parents: 3
diff changeset
    68
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
    69
	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
    70
}
faef59c8db05 Added a goroutine to read data from the remote and parse it into
Chris Jones <chris@cjones.org>
parents: 3
diff changeset
    71
0
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    72
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
    73
	result := jid.Domain
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    74
	if jid.Node != nil {
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    75
		result = *jid.Node + "@" + result
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    76
	}
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    77
	if jid.Resource != nil {
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    78
		result = result + "/" + *jid.Resource
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    79
	}
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    80
	return result
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    81
}
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    82
3
6121aa2f21b1 Made JID implement flag.Value.
Chris Jones <chris@cjones.org>
parents: 2
diff changeset
    83
func (jid *JID) Set(val string) bool {
6121aa2f21b1 Made JID implement flag.Value.
Chris Jones <chris@cjones.org>
parents: 2
diff changeset
    84
	r := regexp.MustCompile("^(([^@/]+)@)?([^@/]+)(/([^@/]+))?$")
6121aa2f21b1 Made JID implement flag.Value.
Chris Jones <chris@cjones.org>
parents: 2
diff changeset
    85
	parts := r.FindStringSubmatch(val)
6121aa2f21b1 Made JID implement flag.Value.
Chris Jones <chris@cjones.org>
parents: 2
diff changeset
    86
	if parts == nil {
6121aa2f21b1 Made JID implement flag.Value.
Chris Jones <chris@cjones.org>
parents: 2
diff changeset
    87
		return false
6121aa2f21b1 Made JID implement flag.Value.
Chris Jones <chris@cjones.org>
parents: 2
diff changeset
    88
	}
6121aa2f21b1 Made JID implement flag.Value.
Chris Jones <chris@cjones.org>
parents: 2
diff changeset
    89
	if parts[2] == "" {
6121aa2f21b1 Made JID implement flag.Value.
Chris Jones <chris@cjones.org>
parents: 2
diff changeset
    90
		jid.Node = nil
6121aa2f21b1 Made JID implement flag.Value.
Chris Jones <chris@cjones.org>
parents: 2
diff changeset
    91
	} else {
6121aa2f21b1 Made JID implement flag.Value.
Chris Jones <chris@cjones.org>
parents: 2
diff changeset
    92
		jid.Node = &parts[2]
6121aa2f21b1 Made JID implement flag.Value.
Chris Jones <chris@cjones.org>
parents: 2
diff changeset
    93
	}
6121aa2f21b1 Made JID implement flag.Value.
Chris Jones <chris@cjones.org>
parents: 2
diff changeset
    94
	jid.Domain = parts[3]
6121aa2f21b1 Made JID implement flag.Value.
Chris Jones <chris@cjones.org>
parents: 2
diff changeset
    95
	if parts[5] == "" {
6121aa2f21b1 Made JID implement flag.Value.
Chris Jones <chris@cjones.org>
parents: 2
diff changeset
    96
		jid.Resource = nil
6121aa2f21b1 Made JID implement flag.Value.
Chris Jones <chris@cjones.org>
parents: 2
diff changeset
    97
	} else {
6121aa2f21b1 Made JID implement flag.Value.
Chris Jones <chris@cjones.org>
parents: 2
diff changeset
    98
		jid.Resource = &parts[5]
6121aa2f21b1 Made JID implement flag.Value.
Chris Jones <chris@cjones.org>
parents: 2
diff changeset
    99
	}
6121aa2f21b1 Made JID implement flag.Value.
Chris Jones <chris@cjones.org>
parents: 2
diff changeset
   100
	return true
6121aa2f21b1 Made JID implement flag.Value.
Chris Jones <chris@cjones.org>
parents: 2
diff changeset
   101
}
6121aa2f21b1 Made JID implement flag.Value.
Chris Jones <chris@cjones.org>
parents: 2
diff changeset
   102
0
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   103
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
   104
	buf := bytes.NewBuffer(nil)
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   105
	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
   106
	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
   107
	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
   108
	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
   109
	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
   110
	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
   111
	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
   112
	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
   113
	buf.WriteString(">")
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   114
	// 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
   115
	return buf.Bytes(), nil
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   116
}
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   117
6
8e425e340ca1 Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents: 5
diff changeset
   118
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
   119
	result, _ := s.MarshalXML()
8e425e340ca1 Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents: 5
diff changeset
   120
	return string(result)
8e425e340ca1 Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents: 5
diff changeset
   121
}
8e425e340ca1 Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents: 5
diff changeset
   122
5
faef59c8db05 Added a goroutine to read data from the remote and parse it into
Chris Jones <chris@cjones.org>
parents: 3
diff changeset
   123
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
   124
	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
   125
	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
   126
		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
   127
		case "to":
6
8e425e340ca1 Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents: 5
diff changeset
   128
			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
   129
		case "from":
6
8e425e340ca1 Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents: 5
diff changeset
   130
			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
   131
		case "id":
6
8e425e340ca1 Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents: 5
diff changeset
   132
			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
   133
		case "lang":
6
8e425e340ca1 Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents: 5
diff changeset
   134
			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
   135
		case "version":
6
8e425e340ca1 Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents: 5
diff changeset
   136
			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
   137
		}
faef59c8db05 Added a goroutine to read data from the remote and parse it into
Chris Jones <chris@cjones.org>
parents: 3
diff changeset
   138
	}
faef59c8db05 Added a goroutine to read data from the remote and parse it into
Chris Jones <chris@cjones.org>
parents: 3
diff changeset
   139
	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
   140
}
faef59c8db05 Added a goroutine to read data from the remote and parse it into
Chris Jones <chris@cjones.org>
parents: 3
diff changeset
   141
0
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   142
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
   143
	buf := bytes.NewBuffer(nil)
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   144
	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
   145
	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
   146
	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
   147
		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
   148
	}
0
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   149
	buf.WriteString("</stream:error>")
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   150
	return buf.Bytes(), nil
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   151
}
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   152
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   153
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
   154
	buf := bytes.NewBuffer(nil)
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   155
	buf.WriteString("<text")
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   156
	writeField(buf, "xmlns", nsStreams)
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   157
	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
   158
	buf.WriteString(">")
6
8e425e340ca1 Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents: 5
diff changeset
   159
	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
   160
	buf.WriteString("</text>")
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   161
	return buf.Bytes(), nil
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   162
}
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   163
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   164
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
   165
	if value != "" {
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   166
		io.WriteString(w, " ")
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   167
		io.WriteString(w, field)
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   168
		io.WriteString(w, `="`)
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   169
		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
   170
		io.WriteString(w, `"`)
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   171
	}
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   172
}