structs.go
author Chris Jones <chris@cjones.org>
Sat, 24 Dec 2011 11:18:52 -0700
changeset 4 a8fbec71a194
parent 3 6121aa2f21b1
child 5 faef59c8db05
permissions -rw-r--r--
Added an interactive test and made Client implement io.Closer.
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"
0
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    16
	"xml"
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    17
)
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
const (
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    20
	// 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
    21
	Version = "1.0"
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    22
	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
    23
	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
    24
	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
    25
)
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
// 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
    28
// 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
    29
// sometimes optional.
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    30
type JID struct {
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    31
	Node *string
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    32
	Domain string
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    33
	Resource *string
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    34
}
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    35
var _ fmt.Stringer = &JID{}
3
6121aa2f21b1 Made JID implement flag.Value.
Chris Jones <chris@cjones.org>
parents: 2
diff changeset
    36
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
    37
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    38
// 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
    39
type Stream struct {
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    40
	to string `xml:"attr"`
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    41
	from string `xml:"attr"`
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    42
	id string `xml:"attr"`
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    43
	lang string `xml:"attr"`
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    44
	version string `xml:"attr"`
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    45
}
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    46
var _ xml.Marshaler = &Stream{}
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    47
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    48
type StreamError struct {
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    49
	cond definedCondition
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
    50
	text *errText
0
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    51
}
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    52
var _ xml.Marshaler = &StreamError{}
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    53
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    54
type definedCondition struct {
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    55
	// 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
    56
	XMLName xml.Name
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    57
}
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    58
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    59
type errText struct {
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    60
	Lang string
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    61
	text string `xml:"chardata"`
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
var _ xml.Marshaler = &errText{}
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    64
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    65
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
    66
	result := jid.Domain
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    67
	if jid.Node != nil {
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    68
		result = *jid.Node + "@" + result
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    69
	}
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    70
	if jid.Resource != nil {
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    71
		result = result + "/" + *jid.Resource
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    72
	}
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    73
	return result
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    74
}
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    75
3
6121aa2f21b1 Made JID implement flag.Value.
Chris Jones <chris@cjones.org>
parents: 2
diff changeset
    76
func (jid *JID) Set(val string) bool {
6121aa2f21b1 Made JID implement flag.Value.
Chris Jones <chris@cjones.org>
parents: 2
diff changeset
    77
	r := regexp.MustCompile("^(([^@/]+)@)?([^@/]+)(/([^@/]+))?$")
6121aa2f21b1 Made JID implement flag.Value.
Chris Jones <chris@cjones.org>
parents: 2
diff changeset
    78
	parts := r.FindStringSubmatch(val)
6121aa2f21b1 Made JID implement flag.Value.
Chris Jones <chris@cjones.org>
parents: 2
diff changeset
    79
	if parts == nil {
6121aa2f21b1 Made JID implement flag.Value.
Chris Jones <chris@cjones.org>
parents: 2
diff changeset
    80
		return false
6121aa2f21b1 Made JID implement flag.Value.
Chris Jones <chris@cjones.org>
parents: 2
diff changeset
    81
	}
6121aa2f21b1 Made JID implement flag.Value.
Chris Jones <chris@cjones.org>
parents: 2
diff changeset
    82
	if parts[2] == "" {
6121aa2f21b1 Made JID implement flag.Value.
Chris Jones <chris@cjones.org>
parents: 2
diff changeset
    83
		jid.Node = nil
6121aa2f21b1 Made JID implement flag.Value.
Chris Jones <chris@cjones.org>
parents: 2
diff changeset
    84
	} else {
6121aa2f21b1 Made JID implement flag.Value.
Chris Jones <chris@cjones.org>
parents: 2
diff changeset
    85
		jid.Node = &parts[2]
6121aa2f21b1 Made JID implement flag.Value.
Chris Jones <chris@cjones.org>
parents: 2
diff changeset
    86
	}
6121aa2f21b1 Made JID implement flag.Value.
Chris Jones <chris@cjones.org>
parents: 2
diff changeset
    87
	jid.Domain = parts[3]
6121aa2f21b1 Made JID implement flag.Value.
Chris Jones <chris@cjones.org>
parents: 2
diff changeset
    88
	if parts[5] == "" {
6121aa2f21b1 Made JID implement flag.Value.
Chris Jones <chris@cjones.org>
parents: 2
diff changeset
    89
		jid.Resource = nil
6121aa2f21b1 Made JID implement flag.Value.
Chris Jones <chris@cjones.org>
parents: 2
diff changeset
    90
	} else {
6121aa2f21b1 Made JID implement flag.Value.
Chris Jones <chris@cjones.org>
parents: 2
diff changeset
    91
		jid.Resource = &parts[5]
6121aa2f21b1 Made JID implement flag.Value.
Chris Jones <chris@cjones.org>
parents: 2
diff changeset
    92
	}
6121aa2f21b1 Made JID implement flag.Value.
Chris Jones <chris@cjones.org>
parents: 2
diff changeset
    93
	return true
6121aa2f21b1 Made JID implement flag.Value.
Chris Jones <chris@cjones.org>
parents: 2
diff changeset
    94
}
6121aa2f21b1 Made JID implement flag.Value.
Chris Jones <chris@cjones.org>
parents: 2
diff changeset
    95
0
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    96
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
    97
	buf := bytes.NewBuffer(nil)
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    98
	buf.WriteString("<stream:stream")
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    99
	writeField(buf, "to", s.to)
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   100
	writeField(buf, "from", s.from)
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   101
	writeField(buf, "id", s.id)
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   102
	writeField(buf, "xml:lang", s.lang)
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   103
	writeField(buf, "version", s.version)
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   104
	buf.WriteString(">")
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   105
	// 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
   106
	return buf.Bytes(), nil
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   107
}
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   108
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   109
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
   110
	buf := bytes.NewBuffer(nil)
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   111
	buf.WriteString("<stream:error>")
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   112
	xml.Marshal(buf, s.cond)
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
   113
	if s.text != nil {
4dabfef08c8c Forgot to add the new xmpp.go from my last commit. Also added some
Chris Jones <chris@cjones.org>
parents: 1
diff changeset
   114
		xml.Marshal(buf, s.text)
4dabfef08c8c Forgot to add the new xmpp.go from my last commit. Also added some
Chris Jones <chris@cjones.org>
parents: 1
diff changeset
   115
	}
0
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   116
	buf.WriteString("</stream:error>")
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   117
	return buf.Bytes(), nil
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   118
}
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   119
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   120
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
   121
	buf := bytes.NewBuffer(nil)
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   122
	buf.WriteString("<text")
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   123
	writeField(buf, "xmlns", nsStreams)
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   124
	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
   125
	buf.WriteString(">")
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   126
	xml.Escape(buf, []byte(e.text))
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   127
	buf.WriteString("</text>")
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   128
	return buf.Bytes(), nil
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   129
}
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   130
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   131
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
   132
	if value != "" {
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   133
		io.WriteString(w, " ")
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   134
		io.WriteString(w, field)
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   135
		io.WriteString(w, `="`)
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   136
		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
   137
		io.WriteString(w, `"`)
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   138
	}
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   139
}