xmpp.go
author Chris Jones <chris@cjones.org>
Fri, 23 Dec 2011 22:44:51 -0700
changeset 0 6dbd969c8f3a
permissions -rw-r--r--
Some initial data structures from RFC 3920, up through section 4.
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
// This package implements a simple XMPP client according to RFCs 3920
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
     6
// and 3921, plus the various XEPs at http://xmpp.org/protocols/.
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
     7
package xmpp
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
     8
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"
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    11
	"fmt"
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    12
	"io"
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    13
	"os"
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    14
	"xml"
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    15
)
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    16
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    17
const (
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    18
	// 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
    19
	Version = "1.0"
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    20
	nsStreams = "urn:ietf:params:xml:ns:xmpp-streams"
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    21
)
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    22
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    23
// 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
    24
// 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
    25
// sometimes optional.
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    26
type JID struct {
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    27
	Node *string
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    28
	Domain string
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    29
	Resource *string
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    30
}
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    31
var _ fmt.Stringer = &JID{}
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    32
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    33
// 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
    34
type Stream struct {
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    35
	to string `xml:"attr"`
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    36
	from string `xml:"attr"`
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    37
	id string `xml:"attr"`
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    38
	lang string `xml:"attr"`
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    39
	version string `xml:"attr"`
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    40
}
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    41
var _ xml.Marshaler = &Stream{}
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    42
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    43
type StreamError struct {
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    44
	cond definedCondition
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    45
	text errText
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 = &StreamError{}
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    48
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    49
type definedCondition struct {
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    50
	// 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
    51
	XMLName xml.Name
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    52
}
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 errText struct {
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    55
	XMLName xml.Name
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    56
	Lang string
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    57
	text string `xml:"chardata"`
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
var _ xml.Marshaler = &errText{}
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
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
    62
	result := jid.Domain
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    63
	if jid.Node != nil {
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    64
		result = *jid.Node + "@" + result
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
	if jid.Resource != nil {
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    67
		result = result + "/" + *jid.Resource
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    68
	}
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    69
	return result
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    70
}
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    71
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    72
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
    73
	buf := bytes.NewBuffer(nil)
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    74
	buf.WriteString("<stream:stream")
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    75
	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
    76
	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
    77
	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
    78
	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
    79
	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
    80
	buf.WriteString(">")
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    81
	// 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
    82
	return buf.Bytes(), nil
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    83
}
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    84
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    85
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
    86
	buf := bytes.NewBuffer(nil)
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    87
	buf.WriteString("<stream:error>")
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    88
	xml.Marshal(buf, s.cond)
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    89
	xml.Marshal(buf, s.text)
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    90
	buf.WriteString("</stream:error>")
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    91
	return buf.Bytes(), nil
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    92
}
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    93
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    94
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
    95
	buf := bytes.NewBuffer(nil)
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    96
	buf.WriteString("<text")
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    97
	writeField(buf, "xmlns", nsStreams)
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    98
	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
    99
	buf.WriteString(">")
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   100
	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
   101
	buf.WriteString("</text>")
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   102
	return buf.Bytes(), nil
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   103
}
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   104
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   105
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
   106
	if value != "" {
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   107
		io.WriteString(w, " ")
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   108
		io.WriteString(w, field)
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   109
		io.WriteString(w, `="`)
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   110
		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
   111
		io.WriteString(w, `"`)
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   112
	}
6dbd969c8f3a Some initial data structures from RFC 3920, up through section 4.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   113
}