xmpp.go
author Chris Jones <christian.jones@sri.com>
Thu, 19 Jan 2012 22:57:36 -0700
changeset 84 25c4296a3524
parent 81 a74e7fc4ecee
child 93 fbd51fa6b7ea
permissions -rw-r--r--
Updates to BUG comments.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
4dabfef08c8c Forgot to add the new xmpp.go from my last commit. Also added some
Chris Jones <chris@cjones.org>
parents:
diff changeset
     1
// Copyright 2011 The Go Authors.  All rights reserved.
4dabfef08c8c Forgot to add the new xmpp.go from my last commit. Also added some
Chris Jones <chris@cjones.org>
parents:
diff changeset
     2
// Use of this source code is governed by a BSD-style
4dabfef08c8c Forgot to add the new xmpp.go from my last commit. Also added some
Chris Jones <chris@cjones.org>
parents:
diff changeset
     3
// license that can be found in the LICENSE file.
4dabfef08c8c Forgot to add the new xmpp.go from my last commit. Also added some
Chris Jones <chris@cjones.org>
parents:
diff changeset
     4
4dabfef08c8c Forgot to add the new xmpp.go from my last commit. Also added some
Chris Jones <chris@cjones.org>
parents:
diff changeset
     5
// This package implements a simple XMPP client according to RFCs 3920
4dabfef08c8c Forgot to add the new xmpp.go from my last commit. Also added some
Chris Jones <chris@cjones.org>
parents:
diff changeset
     6
// and 3921, plus the various XEPs at http://xmpp.org/protocols/.
4dabfef08c8c Forgot to add the new xmpp.go from my last commit. Also added some
Chris Jones <chris@cjones.org>
parents:
diff changeset
     7
package xmpp
4dabfef08c8c Forgot to add the new xmpp.go from my last commit. Also added some
Chris Jones <chris@cjones.org>
parents:
diff changeset
     8
4dabfef08c8c Forgot to add the new xmpp.go from my last commit. Also added some
Chris Jones <chris@cjones.org>
parents:
diff changeset
     9
import (
6
8e425e340ca1 Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents: 5
diff changeset
    10
	"bytes"
78
a5848c75d270 Allow the app to specify a tls.Config to use for purposes of negotiating the TLS layer.
Chris Jones <christian.jones@sri.com>
parents: 76
diff changeset
    11
	"crypto/tls"
74
e619e18dcec3 Ran gofix from weekly-2012-01-15.
Chris Jones <christian.jones@sri.com>
parents: 72
diff changeset
    12
	"encoding/xml"
e619e18dcec3 Ran gofix from weekly-2012-01-15.
Chris Jones <christian.jones@sri.com>
parents: 72
diff changeset
    13
	"errors"
2
4dabfef08c8c Forgot to add the new xmpp.go from my last commit. Also added some
Chris Jones <chris@cjones.org>
parents:
diff changeset
    14
	"fmt"
4
a8fbec71a194 Added an interactive test and made Client implement io.Closer.
Chris Jones <chris@cjones.org>
parents: 2
diff changeset
    15
	"io"
76
caa722ab8a0f Fixed logging to use log rather than syslog.
Chris Jones <christian.jones@sri.com>
parents: 74
diff changeset
    16
	"log"
74
e619e18dcec3 Ran gofix from weekly-2012-01-15.
Chris Jones <christian.jones@sri.com>
parents: 72
diff changeset
    17
	"log/syslog"
2
4dabfef08c8c Forgot to add the new xmpp.go from my last commit. Also added some
Chris Jones <chris@cjones.org>
parents:
diff changeset
    18
	"net"
10
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents: 9
diff changeset
    19
	"sync"
2
4dabfef08c8c Forgot to add the new xmpp.go from my last commit. Also added some
Chris Jones <chris@cjones.org>
parents:
diff changeset
    20
)
4dabfef08c8c Forgot to add the new xmpp.go from my last commit. Also added some
Chris Jones <chris@cjones.org>
parents:
diff changeset
    21
4dabfef08c8c Forgot to add the new xmpp.go from my last commit. Also added some
Chris Jones <chris@cjones.org>
parents:
diff changeset
    22
const (
10
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents: 9
diff changeset
    23
	// Version of RFC 3920 that we implement.
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents: 9
diff changeset
    24
	Version = "1.0"
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents: 9
diff changeset
    25
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents: 9
diff changeset
    26
	// Various XML namespaces.
34
7b1f924c75e2 Made the namespace constants public.
Chris Jones <chris@cjones.org>
parents: 33
diff changeset
    27
	NsStreams = "urn:ietf:params:xml:ns:xmpp-streams"
72
Chris Jones <christian.jones@sri.com>
parents: 67
diff changeset
    28
	NsStream  = "http://etherx.jabber.org/streams"
Chris Jones <christian.jones@sri.com>
parents: 67
diff changeset
    29
	NsTLS     = "urn:ietf:params:xml:ns:xmpp-tls"
Chris Jones <christian.jones@sri.com>
parents: 67
diff changeset
    30
	NsSASL    = "urn:ietf:params:xml:ns:xmpp-sasl"
Chris Jones <christian.jones@sri.com>
parents: 67
diff changeset
    31
	NsBind    = "urn:ietf:params:xml:ns:xmpp-bind"
34
7b1f924c75e2 Made the namespace constants public.
Chris Jones <chris@cjones.org>
parents: 33
diff changeset
    32
	NsSession = "urn:ietf:params:xml:ns:xmpp-session"
72
Chris Jones <christian.jones@sri.com>
parents: 67
diff changeset
    33
	NsRoster  = "jabber:iq:roster"
10
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents: 9
diff changeset
    34
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents: 9
diff changeset
    35
	// DNS SRV names
2
4dabfef08c8c Forgot to add the new xmpp.go from my last commit. Also added some
Chris Jones <chris@cjones.org>
parents:
diff changeset
    36
	serverSrv = "xmpp-server"
4dabfef08c8c Forgot to add the new xmpp.go from my last commit. Also added some
Chris Jones <chris@cjones.org>
parents:
diff changeset
    37
	clientSrv = "xmpp-client"
62
6e2eea62ccca Added global variables for logging.
Chris Jones <chris@cjones.org>
parents: 61
diff changeset
    38
)
10
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents: 9
diff changeset
    39
62
6e2eea62ccca Added global variables for logging.
Chris Jones <chris@cjones.org>
parents: 61
diff changeset
    40
var (
6e2eea62ccca Added global variables for logging.
Chris Jones <chris@cjones.org>
parents: 61
diff changeset
    41
	// If non-nil when NewClient() is called, log messages will be
6e2eea62ccca Added global variables for logging.
Chris Jones <chris@cjones.org>
parents: 61
diff changeset
    42
	// sent to this writer.
76
caa722ab8a0f Fixed logging to use log rather than syslog.
Chris Jones <christian.jones@sri.com>
parents: 74
diff changeset
    43
	Log *log.Logger
62
6e2eea62ccca Added global variables for logging.
Chris Jones <chris@cjones.org>
parents: 61
diff changeset
    44
	// Threshold for which messages are logged.
6e2eea62ccca Added global variables for logging.
Chris Jones <chris@cjones.org>
parents: 61
diff changeset
    45
	Loglevel syslog.Priority = syslog.LOG_NOTICE
78
a5848c75d270 Allow the app to specify a tls.Config to use for purposes of negotiating the TLS layer.
Chris Jones <christian.jones@sri.com>
parents: 76
diff changeset
    46
	// Use this Config to negotiate TLS connections.
a5848c75d270 Allow the app to specify a tls.Config to use for purposes of negotiating the TLS layer.
Chris Jones <christian.jones@sri.com>
parents: 76
diff changeset
    47
	TLSConfig *tls.Config = nil
2
4dabfef08c8c Forgot to add the new xmpp.go from my last commit. Also added some
Chris Jones <chris@cjones.org>
parents:
diff changeset
    48
)
4dabfef08c8c Forgot to add the new xmpp.go from my last commit. Also added some
Chris Jones <chris@cjones.org>
parents:
diff changeset
    49
57
e6cb3f049137 Revamped how the roster works. We're now using a channel to transmit snapshots
Chris Jones <chris@cjones.org>
parents: 51
diff changeset
    50
// This channel may be used as a convenient way to generate a unique
e6cb3f049137 Revamped how the roster works. We're now using a channel to transmit snapshots
Chris Jones <chris@cjones.org>
parents: 51
diff changeset
    51
// id for an iq, message, or presence stanza.
e6cb3f049137 Revamped how the roster works. We're now using a channel to transmit snapshots
Chris Jones <chris@cjones.org>
parents: 51
diff changeset
    52
var Id <-chan string
e6cb3f049137 Revamped how the roster works. We're now using a channel to transmit snapshots
Chris Jones <chris@cjones.org>
parents: 51
diff changeset
    53
e6cb3f049137 Revamped how the roster works. We're now using a channel to transmit snapshots
Chris Jones <chris@cjones.org>
parents: 51
diff changeset
    54
func init() {
e6cb3f049137 Revamped how the roster works. We're now using a channel to transmit snapshots
Chris Jones <chris@cjones.org>
parents: 51
diff changeset
    55
	// Start the unique id generator.
e6cb3f049137 Revamped how the roster works. We're now using a channel to transmit snapshots
Chris Jones <chris@cjones.org>
parents: 51
diff changeset
    56
	idCh := make(chan string)
e6cb3f049137 Revamped how the roster works. We're now using a channel to transmit snapshots
Chris Jones <chris@cjones.org>
parents: 51
diff changeset
    57
	Id = idCh
e6cb3f049137 Revamped how the roster works. We're now using a channel to transmit snapshots
Chris Jones <chris@cjones.org>
parents: 51
diff changeset
    58
	go func(ch chan<- string) {
e6cb3f049137 Revamped how the roster works. We're now using a channel to transmit snapshots
Chris Jones <chris@cjones.org>
parents: 51
diff changeset
    59
		id := int64(1)
e6cb3f049137 Revamped how the roster works. We're now using a channel to transmit snapshots
Chris Jones <chris@cjones.org>
parents: 51
diff changeset
    60
		for {
e6cb3f049137 Revamped how the roster works. We're now using a channel to transmit snapshots
Chris Jones <chris@cjones.org>
parents: 51
diff changeset
    61
			str := fmt.Sprintf("id_%d", id)
e6cb3f049137 Revamped how the roster works. We're now using a channel to transmit snapshots
Chris Jones <chris@cjones.org>
parents: 51
diff changeset
    62
			ch <- str
e6cb3f049137 Revamped how the roster works. We're now using a channel to transmit snapshots
Chris Jones <chris@cjones.org>
parents: 51
diff changeset
    63
			id++
e6cb3f049137 Revamped how the roster works. We're now using a channel to transmit snapshots
Chris Jones <chris@cjones.org>
parents: 51
diff changeset
    64
		}
e6cb3f049137 Revamped how the roster works. We're now using a channel to transmit snapshots
Chris Jones <chris@cjones.org>
parents: 51
diff changeset
    65
	}(idCh)
e6cb3f049137 Revamped how the roster works. We're now using a channel to transmit snapshots
Chris Jones <chris@cjones.org>
parents: 51
diff changeset
    66
}
e6cb3f049137 Revamped how the roster works. We're now using a channel to transmit snapshots
Chris Jones <chris@cjones.org>
parents: 51
diff changeset
    67
60
6d4f43f7dc19 Made a generic extension interface.
Chris Jones <chris@cjones.org>
parents: 59
diff changeset
    68
// Extensions can add stanza filters and/or new XML element types.
6d4f43f7dc19 Made a generic extension interface.
Chris Jones <chris@cjones.org>
parents: 59
diff changeset
    69
type Extension struct {
72
Chris Jones <christian.jones@sri.com>
parents: 67
diff changeset
    70
	StanzaHandlers map[string]func(*xml.Name) interface{}
Chris Jones <christian.jones@sri.com>
parents: 67
diff changeset
    71
	Start          func(*Client)
60
6d4f43f7dc19 Made a generic extension interface.
Chris Jones <chris@cjones.org>
parents: 59
diff changeset
    72
}
6d4f43f7dc19 Made a generic extension interface.
Chris Jones <chris@cjones.org>
parents: 59
diff changeset
    73
2
4dabfef08c8c Forgot to add the new xmpp.go from my last commit. Also added some
Chris Jones <chris@cjones.org>
parents:
diff changeset
    74
// The client in a client-server XMPP connection.
4dabfef08c8c Forgot to add the new xmpp.go from my last commit. Also added some
Chris Jones <chris@cjones.org>
parents:
diff changeset
    75
type Client struct {
57
e6cb3f049137 Revamped how the roster works. We're now using a channel to transmit snapshots
Chris Jones <chris@cjones.org>
parents: 51
diff changeset
    76
	// This client's unique ID. It's unique within the context of
e6cb3f049137 Revamped how the roster works. We're now using a channel to transmit snapshots
Chris Jones <chris@cjones.org>
parents: 51
diff changeset
    77
	// this process, so if multiple Client objects exist, each
e6cb3f049137 Revamped how the roster works. We're now using a channel to transmit snapshots
Chris Jones <chris@cjones.org>
parents: 51
diff changeset
    78
	// will be distinguishable by its Uid.
e6cb3f049137 Revamped how the roster works. We're now using a channel to transmit snapshots
Chris Jones <chris@cjones.org>
parents: 51
diff changeset
    79
	Uid string
29
a456133ed0ac Don't accept data on Client.Out until resource binding is
Chris Jones <chris@cjones.org>
parents: 28
diff changeset
    80
	// This client's JID. This will be updated asynchronously by
a456133ed0ac Don't accept data on Client.Out until resource binding is
Chris Jones <chris@cjones.org>
parents: 28
diff changeset
    81
	// the time StartSession() returns.
72
Chris Jones <christian.jones@sri.com>
parents: 67
diff changeset
    82
	Jid          JID
Chris Jones <christian.jones@sri.com>
parents: 67
diff changeset
    83
	password     string
Chris Jones <christian.jones@sri.com>
parents: 67
diff changeset
    84
	socket       net.Conn
Chris Jones <christian.jones@sri.com>
parents: 67
diff changeset
    85
	socketSync   sync.WaitGroup
11
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
    86
	saslExpected string
72
Chris Jones <christian.jones@sri.com>
parents: 67
diff changeset
    87
	authDone     bool
Chris Jones <christian.jones@sri.com>
parents: 67
diff changeset
    88
	handlers     chan *stanzaHandler
29
a456133ed0ac Don't accept data on Client.Out until resource binding is
Chris Jones <chris@cjones.org>
parents: 28
diff changeset
    89
	inputControl chan int
17
d269d9c0fc8e Code review.
Chris Jones <chris@cjones.org>
parents: 15
diff changeset
    90
	// Incoming XMPP stanzas from the server will be published on
d269d9c0fc8e Code review.
Chris Jones <chris@cjones.org>
parents: 15
diff changeset
    91
	// this channel. Information which is only used by this
d269d9c0fc8e Code review.
Chris Jones <chris@cjones.org>
parents: 15
diff changeset
    92
	// library to set up the XMPP stream will not appear here.
23
b5de44679389 Made the input and output channels of type Stanza rather than
Chris Jones <chris@cjones.org>
parents: 22
diff changeset
    93
	In <-chan Stanza
17
d269d9c0fc8e Code review.
Chris Jones <chris@cjones.org>
parents: 15
diff changeset
    94
	// Outgoing XMPP stanzas to the server should be sent to this
d269d9c0fc8e Code review.
Chris Jones <chris@cjones.org>
parents: 15
diff changeset
    95
	// channel.
72
Chris Jones <christian.jones@sri.com>
parents: 67
diff changeset
    96
	Out    chan<- Stanza
10
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents: 9
diff changeset
    97
	xmlOut chan<- interface{}
32
4e68d8f89dc3 Make the server's advertised features available to the app.
Chris Jones <chris@cjones.org>
parents: 30
diff changeset
    98
	// Features advertised by the remote. This will be updated
4e68d8f89dc3 Make the server's advertised features available to the app.
Chris Jones <chris@cjones.org>
parents: 30
diff changeset
    99
	// asynchronously as new features are received throughout the
4e68d8f89dc3 Make the server's advertised features available to the app.
Chris Jones <chris@cjones.org>
parents: 30
diff changeset
   100
	// connection process. It should not be updated once
4e68d8f89dc3 Make the server's advertised features available to the app.
Chris Jones <chris@cjones.org>
parents: 30
diff changeset
   101
	// StartSession() returns.
72
Chris Jones <christian.jones@sri.com>
parents: 67
diff changeset
   102
	Features  *Features
45
abf958bcc201 Added a stack of filters which can intercept data before it gets to
Chris Jones <chris@cjones.org>
parents: 44
diff changeset
   103
	filterOut chan<- <-chan Stanza
72
Chris Jones <christian.jones@sri.com>
parents: 67
diff changeset
   104
	filterIn  <-chan <-chan Stanza
2
4dabfef08c8c Forgot to add the new xmpp.go from my last commit. Also added some
Chris Jones <chris@cjones.org>
parents:
diff changeset
   105
}
4dabfef08c8c Forgot to add the new xmpp.go from my last commit. Also added some
Chris Jones <chris@cjones.org>
parents:
diff changeset
   106
4dabfef08c8c Forgot to add the new xmpp.go from my last commit. Also added some
Chris Jones <chris@cjones.org>
parents:
diff changeset
   107
// Connect to the appropriate server and authenticate as the given JID
17
d269d9c0fc8e Code review.
Chris Jones <chris@cjones.org>
parents: 15
diff changeset
   108
// with the given password. This function will return as soon as a TCP
d269d9c0fc8e Code review.
Chris Jones <chris@cjones.org>
parents: 15
diff changeset
   109
// connection has been established, but before XMPP stream negotiation
27
13bcc96a5a6c Clarified a comment.
Chris Jones <chris@cjones.org>
parents: 26
diff changeset
   110
// has completed. The negotiation will occur asynchronously, and any
13bcc96a5a6c Clarified a comment.
Chris Jones <chris@cjones.org>
parents: 26
diff changeset
   111
// send operation to Client.Out will block until negotiation (resource
13bcc96a5a6c Clarified a comment.
Chris Jones <chris@cjones.org>
parents: 26
diff changeset
   112
// binding) is complete.
60
6d4f43f7dc19 Made a generic extension interface.
Chris Jones <chris@cjones.org>
parents: 59
diff changeset
   113
func NewClient(jid *JID, password string, exts []Extension) (*Client,
74
e619e18dcec3 Ran gofix from weekly-2012-01-15.
Chris Jones <christian.jones@sri.com>
parents: 72
diff changeset
   114
	error) {
60
6d4f43f7dc19 Made a generic extension interface.
Chris Jones <chris@cjones.org>
parents: 59
diff changeset
   115
	// Include the mandatory extensions.
6d4f43f7dc19 Made a generic extension interface.
Chris Jones <chris@cjones.org>
parents: 59
diff changeset
   116
	exts = append(exts, rosterExt)
6d4f43f7dc19 Made a generic extension interface.
Chris Jones <chris@cjones.org>
parents: 59
diff changeset
   117
	exts = append(exts, bindExt)
6d4f43f7dc19 Made a generic extension interface.
Chris Jones <chris@cjones.org>
parents: 59
diff changeset
   118
2
4dabfef08c8c Forgot to add the new xmpp.go from my last commit. Also added some
Chris Jones <chris@cjones.org>
parents:
diff changeset
   119
	// Resolve the domain in the JID.
4dabfef08c8c Forgot to add the new xmpp.go from my last commit. Also added some
Chris Jones <chris@cjones.org>
parents:
diff changeset
   120
	_, srvs, err := net.LookupSRV(clientSrv, "tcp", jid.Domain)
4dabfef08c8c Forgot to add the new xmpp.go from my last commit. Also added some
Chris Jones <chris@cjones.org>
parents:
diff changeset
   121
	if err != nil {
74
e619e18dcec3 Ran gofix from weekly-2012-01-15.
Chris Jones <christian.jones@sri.com>
parents: 72
diff changeset
   122
		return nil, errors.New("LookupSrv " + jid.Domain +
e619e18dcec3 Ran gofix from weekly-2012-01-15.
Chris Jones <christian.jones@sri.com>
parents: 72
diff changeset
   123
			": " + err.Error())
2
4dabfef08c8c Forgot to add the new xmpp.go from my last commit. Also added some
Chris Jones <chris@cjones.org>
parents:
diff changeset
   124
	}
4dabfef08c8c Forgot to add the new xmpp.go from my last commit. Also added some
Chris Jones <chris@cjones.org>
parents:
diff changeset
   125
9
4fe926b03827 Reorganize so we have a layered approach to IO with the server.
Chris Jones <chris@cjones.org>
parents: 8
diff changeset
   126
	var tcp *net.TCPConn
2
4dabfef08c8c Forgot to add the new xmpp.go from my last commit. Also added some
Chris Jones <chris@cjones.org>
parents:
diff changeset
   127
	for _, srv := range srvs {
4dabfef08c8c Forgot to add the new xmpp.go from my last commit. Also added some
Chris Jones <chris@cjones.org>
parents:
diff changeset
   128
		addrStr := fmt.Sprintf("%s:%d", srv.Target, srv.Port)
4dabfef08c8c Forgot to add the new xmpp.go from my last commit. Also added some
Chris Jones <chris@cjones.org>
parents:
diff changeset
   129
		addr, err := net.ResolveTCPAddr("tcp", addrStr)
4dabfef08c8c Forgot to add the new xmpp.go from my last commit. Also added some
Chris Jones <chris@cjones.org>
parents:
diff changeset
   130
		if err != nil {
74
e619e18dcec3 Ran gofix from weekly-2012-01-15.
Chris Jones <christian.jones@sri.com>
parents: 72
diff changeset
   131
			err = errors.New(fmt.Sprintf("ResolveTCPAddr(%s): %s",
e619e18dcec3 Ran gofix from weekly-2012-01-15.
Chris Jones <christian.jones@sri.com>
parents: 72
diff changeset
   132
				addrStr, err.Error()))
2
4dabfef08c8c Forgot to add the new xmpp.go from my last commit. Also added some
Chris Jones <chris@cjones.org>
parents:
diff changeset
   133
			continue
4dabfef08c8c Forgot to add the new xmpp.go from my last commit. Also added some
Chris Jones <chris@cjones.org>
parents:
diff changeset
   134
		}
9
4fe926b03827 Reorganize so we have a layered approach to IO with the server.
Chris Jones <chris@cjones.org>
parents: 8
diff changeset
   135
		tcp, err = net.DialTCP("tcp", nil, addr)
2
4dabfef08c8c Forgot to add the new xmpp.go from my last commit. Also added some
Chris Jones <chris@cjones.org>
parents:
diff changeset
   136
		if err != nil {
74
e619e18dcec3 Ran gofix from weekly-2012-01-15.
Chris Jones <christian.jones@sri.com>
parents: 72
diff changeset
   137
			err = errors.New(fmt.Sprintf("DialTCP(%s): %s",
e619e18dcec3 Ran gofix from weekly-2012-01-15.
Chris Jones <christian.jones@sri.com>
parents: 72
diff changeset
   138
				addr, err.Error()))
2
4dabfef08c8c Forgot to add the new xmpp.go from my last commit. Also added some
Chris Jones <chris@cjones.org>
parents:
diff changeset
   139
			continue
4dabfef08c8c Forgot to add the new xmpp.go from my last commit. Also added some
Chris Jones <chris@cjones.org>
parents:
diff changeset
   140
		}
4dabfef08c8c Forgot to add the new xmpp.go from my last commit. Also added some
Chris Jones <chris@cjones.org>
parents:
diff changeset
   141
	}
9
4fe926b03827 Reorganize so we have a layered approach to IO with the server.
Chris Jones <chris@cjones.org>
parents: 8
diff changeset
   142
	if tcp == nil {
2
4dabfef08c8c Forgot to add the new xmpp.go from my last commit. Also added some
Chris Jones <chris@cjones.org>
parents:
diff changeset
   143
		return nil, err
4dabfef08c8c Forgot to add the new xmpp.go from my last commit. Also added some
Chris Jones <chris@cjones.org>
parents:
diff changeset
   144
	}
4dabfef08c8c Forgot to add the new xmpp.go from my last commit. Also added some
Chris Jones <chris@cjones.org>
parents:
diff changeset
   145
10
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents: 9
diff changeset
   146
	cl := new(Client)
72
Chris Jones <christian.jones@sri.com>
parents: 67
diff changeset
   147
	cl.Uid = <-Id
11
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   148
	cl.password = password
10
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents: 9
diff changeset
   149
	cl.Jid = *jid
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents: 9
diff changeset
   150
	cl.socket = tcp
33
571713f49494 Added roster retrieval to StartSession().
Chris Jones <chris@cjones.org>
parents: 32
diff changeset
   151
	cl.handlers = make(chan *stanzaHandler, 100)
29
a456133ed0ac Don't accept data on Client.Out until resource binding is
Chris Jones <chris@cjones.org>
parents: 28
diff changeset
   152
	cl.inputControl = make(chan int)
30
a77fc342e013 Replaced Client.NextId() with a channel named Id.
Chris Jones <chris@cjones.org>
parents: 29
diff changeset
   153
72
Chris Jones <christian.jones@sri.com>
parents: 67
diff changeset
   154
	extStanza := make(map[string]func(*xml.Name) interface{})
Chris Jones <christian.jones@sri.com>
parents: 67
diff changeset
   155
	for _, ext := range exts {
Chris Jones <christian.jones@sri.com>
parents: 67
diff changeset
   156
		for k, v := range ext.StanzaHandlers {
60
6d4f43f7dc19 Made a generic extension interface.
Chris Jones <chris@cjones.org>
parents: 59
diff changeset
   157
			extStanza[k] = v
6d4f43f7dc19 Made a generic extension interface.
Chris Jones <chris@cjones.org>
parents: 59
diff changeset
   158
		}
36
9fe022261dcc Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents: 34
diff changeset
   159
	}
9fe022261dcc Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents: 34
diff changeset
   160
9
4fe926b03827 Reorganize so we have a layered approach to IO with the server.
Chris Jones <chris@cjones.org>
parents: 8
diff changeset
   161
	// Start the transport handler, initially unencrypted.
10
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents: 9
diff changeset
   162
	tlsr, tlsw := cl.startTransport()
9
4fe926b03827 Reorganize so we have a layered approach to IO with the server.
Chris Jones <chris@cjones.org>
parents: 8
diff changeset
   163
4fe926b03827 Reorganize so we have a layered approach to IO with the server.
Chris Jones <chris@cjones.org>
parents: 8
diff changeset
   164
	// Start the reader and writers that convert to and from XML.
36
9fe022261dcc Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents: 34
diff changeset
   165
	xmlIn := startXmlReader(tlsr, extStanza)
10
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents: 9
diff changeset
   166
	cl.xmlOut = startXmlWriter(tlsw)
9
4fe926b03827 Reorganize so we have a layered approach to IO with the server.
Chris Jones <chris@cjones.org>
parents: 8
diff changeset
   167
4fe926b03827 Reorganize so we have a layered approach to IO with the server.
Chris Jones <chris@cjones.org>
parents: 8
diff changeset
   168
	// Start the XMPP stream handler which filters stream-level
4fe926b03827 Reorganize so we have a layered approach to IO with the server.
Chris Jones <chris@cjones.org>
parents: 8
diff changeset
   169
	// events and responds to them.
45
abf958bcc201 Added a stack of filters which can intercept data before it gets to
Chris Jones <chris@cjones.org>
parents: 44
diff changeset
   170
	stIn := cl.startStreamReader(xmlIn, cl.xmlOut)
29
a456133ed0ac Don't accept data on Client.Out until resource binding is
Chris Jones <chris@cjones.org>
parents: 28
diff changeset
   171
	clOut := cl.startStreamWriter(cl.xmlOut)
60
6d4f43f7dc19 Made a generic extension interface.
Chris Jones <chris@cjones.org>
parents: 59
diff changeset
   172
	cl.Out = clOut
5
faef59c8db05 Added a goroutine to read data from the remote and parse it into
Chris Jones <chris@cjones.org>
parents: 4
diff changeset
   173
45
abf958bcc201 Added a stack of filters which can intercept data before it gets to
Chris Jones <chris@cjones.org>
parents: 44
diff changeset
   174
	// Start the manager for the filters that can modify what the
abf958bcc201 Added a stack of filters which can intercept data before it gets to
Chris Jones <chris@cjones.org>
parents: 44
diff changeset
   175
	// app sees.
abf958bcc201 Added a stack of filters which can intercept data before it gets to
Chris Jones <chris@cjones.org>
parents: 44
diff changeset
   176
	clIn := cl.startFilter(stIn)
60
6d4f43f7dc19 Made a generic extension interface.
Chris Jones <chris@cjones.org>
parents: 59
diff changeset
   177
	cl.In = clIn
6d4f43f7dc19 Made a generic extension interface.
Chris Jones <chris@cjones.org>
parents: 59
diff changeset
   178
6d4f43f7dc19 Made a generic extension interface.
Chris Jones <chris@cjones.org>
parents: 59
diff changeset
   179
	// Add filters for our extensions.
72
Chris Jones <christian.jones@sri.com>
parents: 67
diff changeset
   180
	for _, ext := range exts {
60
6d4f43f7dc19 Made a generic extension interface.
Chris Jones <chris@cjones.org>
parents: 59
diff changeset
   181
		ext.Start(cl)
6d4f43f7dc19 Made a generic extension interface.
Chris Jones <chris@cjones.org>
parents: 59
diff changeset
   182
	}
45
abf958bcc201 Added a stack of filters which can intercept data before it gets to
Chris Jones <chris@cjones.org>
parents: 44
diff changeset
   183
8
30a7752cf8f7 Added the ability to parse <stream:features>.
Chris Jones <chris@cjones.org>
parents: 6
diff changeset
   184
	// Initial handshake.
81
a74e7fc4ecee Consolidate how we generate the "open stream" XML.
Chris Jones <christian.jones@sri.com>
parents: 78
diff changeset
   185
	cl.xmlOut <- openStream(jid)
9
4fe926b03827 Reorganize so we have a layered approach to IO with the server.
Chris Jones <chris@cjones.org>
parents: 8
diff changeset
   186
6
8e425e340ca1 Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents: 5
diff changeset
   187
	return cl, nil
2
4dabfef08c8c Forgot to add the new xmpp.go from my last commit. Also added some
Chris Jones <chris@cjones.org>
parents:
diff changeset
   188
}
4
a8fbec71a194 Added an interactive test and made Client implement io.Closer.
Chris Jones <chris@cjones.org>
parents: 2
diff changeset
   189
64
ac0639692317 Properly close all the channels and writers if Client.Out is close.
Chris Jones <chris@cjones.org>
parents: 63
diff changeset
   190
func (cl *Client) startTransport() (io.Reader, io.WriteCloser) {
9
4fe926b03827 Reorganize so we have a layered approach to IO with the server.
Chris Jones <chris@cjones.org>
parents: 8
diff changeset
   191
	inr, inw := io.Pipe()
4fe926b03827 Reorganize so we have a layered approach to IO with the server.
Chris Jones <chris@cjones.org>
parents: 8
diff changeset
   192
	outr, outw := io.Pipe()
10
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents: 9
diff changeset
   193
	go cl.readTransport(inw)
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents: 9
diff changeset
   194
	go cl.writeTransport(outr)
9
4fe926b03827 Reorganize so we have a layered approach to IO with the server.
Chris Jones <chris@cjones.org>
parents: 8
diff changeset
   195
	return inr, outw
4
a8fbec71a194 Added an interactive test and made Client implement io.Closer.
Chris Jones <chris@cjones.org>
parents: 2
diff changeset
   196
}
5
faef59c8db05 Added a goroutine to read data from the remote and parse it into
Chris Jones <chris@cjones.org>
parents: 4
diff changeset
   197
36
9fe022261dcc Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents: 34
diff changeset
   198
func startXmlReader(r io.Reader,
74
e619e18dcec3 Ran gofix from weekly-2012-01-15.
Chris Jones <christian.jones@sri.com>
parents: 72
diff changeset
   199
	extStanza map[string]func(*xml.Name) interface{}) <-chan interface{} {
9
4fe926b03827 Reorganize so we have a layered approach to IO with the server.
Chris Jones <chris@cjones.org>
parents: 8
diff changeset
   200
	ch := make(chan interface{})
36
9fe022261dcc Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents: 34
diff changeset
   201
	go readXml(r, ch, extStanza)
9
4fe926b03827 Reorganize so we have a layered approach to IO with the server.
Chris Jones <chris@cjones.org>
parents: 8
diff changeset
   202
	return ch
4fe926b03827 Reorganize so we have a layered approach to IO with the server.
Chris Jones <chris@cjones.org>
parents: 8
diff changeset
   203
}
4fe926b03827 Reorganize so we have a layered approach to IO with the server.
Chris Jones <chris@cjones.org>
parents: 8
diff changeset
   204
64
ac0639692317 Properly close all the channels and writers if Client.Out is close.
Chris Jones <chris@cjones.org>
parents: 63
diff changeset
   205
func startXmlWriter(w io.WriteCloser) chan<- interface{} {
9
4fe926b03827 Reorganize so we have a layered approach to IO with the server.
Chris Jones <chris@cjones.org>
parents: 8
diff changeset
   206
	ch := make(chan interface{})
4fe926b03827 Reorganize so we have a layered approach to IO with the server.
Chris Jones <chris@cjones.org>
parents: 8
diff changeset
   207
	go writeXml(w, ch)
4fe926b03827 Reorganize so we have a layered approach to IO with the server.
Chris Jones <chris@cjones.org>
parents: 8
diff changeset
   208
	return ch
6
8e425e340ca1 Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents: 5
diff changeset
   209
}
8e425e340ca1 Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents: 5
diff changeset
   210
23
b5de44679389 Made the input and output channels of type Stanza rather than
Chris Jones <chris@cjones.org>
parents: 22
diff changeset
   211
func (cl *Client) startStreamReader(xmlIn <-chan interface{}, srvOut chan<- interface{}) <-chan Stanza {
b5de44679389 Made the input and output channels of type Stanza rather than
Chris Jones <chris@cjones.org>
parents: 22
diff changeset
   212
	ch := make(chan Stanza)
11
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   213
	go cl.readStream(xmlIn, ch)
9
4fe926b03827 Reorganize so we have a layered approach to IO with the server.
Chris Jones <chris@cjones.org>
parents: 8
diff changeset
   214
	return ch
4fe926b03827 Reorganize so we have a layered approach to IO with the server.
Chris Jones <chris@cjones.org>
parents: 8
diff changeset
   215
}
4fe926b03827 Reorganize so we have a layered approach to IO with the server.
Chris Jones <chris@cjones.org>
parents: 8
diff changeset
   216
29
a456133ed0ac Don't accept data on Client.Out until resource binding is
Chris Jones <chris@cjones.org>
parents: 28
diff changeset
   217
func (cl *Client) startStreamWriter(xmlOut chan<- interface{}) chan<- Stanza {
23
b5de44679389 Made the input and output channels of type Stanza rather than
Chris Jones <chris@cjones.org>
parents: 22
diff changeset
   218
	ch := make(chan Stanza)
29
a456133ed0ac Don't accept data on Client.Out until resource binding is
Chris Jones <chris@cjones.org>
parents: 28
diff changeset
   219
	go writeStream(xmlOut, ch, cl.inputControl)
9
4fe926b03827 Reorganize so we have a layered approach to IO with the server.
Chris Jones <chris@cjones.org>
parents: 8
diff changeset
   220
	return ch
4fe926b03827 Reorganize so we have a layered approach to IO with the server.
Chris Jones <chris@cjones.org>
parents: 8
diff changeset
   221
}
4fe926b03827 Reorganize so we have a layered approach to IO with the server.
Chris Jones <chris@cjones.org>
parents: 8
diff changeset
   222
45
abf958bcc201 Added a stack of filters which can intercept data before it gets to
Chris Jones <chris@cjones.org>
parents: 44
diff changeset
   223
func (cl *Client) startFilter(srvIn <-chan Stanza) <-chan Stanza {
64
ac0639692317 Properly close all the channels and writers if Client.Out is close.
Chris Jones <chris@cjones.org>
parents: 63
diff changeset
   224
	cliIn := make(chan Stanza)
45
abf958bcc201 Added a stack of filters which can intercept data before it gets to
Chris Jones <chris@cjones.org>
parents: 44
diff changeset
   225
	filterOut := make(chan (<-chan Stanza))
abf958bcc201 Added a stack of filters which can intercept data before it gets to
Chris Jones <chris@cjones.org>
parents: 44
diff changeset
   226
	filterIn := make(chan (<-chan Stanza))
51
1af366d10d32 Nil checks and a greatly simplified filter manager which is less buggy.
Chris Jones <chris@cjones.org>
parents: 48
diff changeset
   227
	nullFilter := make(chan Stanza)
1af366d10d32 Nil checks and a greatly simplified filter manager which is less buggy.
Chris Jones <chris@cjones.org>
parents: 48
diff changeset
   228
	go filterBottom(srvIn, nullFilter)
64
ac0639692317 Properly close all the channels and writers if Client.Out is close.
Chris Jones <chris@cjones.org>
parents: 63
diff changeset
   229
	go filterTop(filterOut, filterIn, nullFilter, cliIn)
46
4a4530b8f622 Added roster updating.
Chris Jones <chris@cjones.org>
parents: 45
diff changeset
   230
	cl.filterOut = filterOut
4a4530b8f622 Added roster updating.
Chris Jones <chris@cjones.org>
parents: 45
diff changeset
   231
	cl.filterIn = filterIn
64
ac0639692317 Properly close all the channels and writers if Client.Out is close.
Chris Jones <chris@cjones.org>
parents: 63
diff changeset
   232
	return cliIn
45
abf958bcc201 Added a stack of filters which can intercept data before it gets to
Chris Jones <chris@cjones.org>
parents: 44
diff changeset
   233
}
abf958bcc201 Added a stack of filters which can intercept data before it gets to
Chris Jones <chris@cjones.org>
parents: 44
diff changeset
   234
6
8e425e340ca1 Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents: 5
diff changeset
   235
func tee(r io.Reader, w io.Writer, prefix string) {
64
ac0639692317 Properly close all the channels and writers if Client.Out is close.
Chris Jones <chris@cjones.org>
parents: 63
diff changeset
   236
	defer func(w io.Writer) {
72
Chris Jones <christian.jones@sri.com>
parents: 67
diff changeset
   237
		if c, ok := w.(io.Closer); ok {
64
ac0639692317 Properly close all the channels and writers if Client.Out is close.
Chris Jones <chris@cjones.org>
parents: 63
diff changeset
   238
			c.Close()
ac0639692317 Properly close all the channels and writers if Client.Out is close.
Chris Jones <chris@cjones.org>
parents: 63
diff changeset
   239
		}
ac0639692317 Properly close all the channels and writers if Client.Out is close.
Chris Jones <chris@cjones.org>
parents: 63
diff changeset
   240
	}(w)
6
8e425e340ca1 Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents: 5
diff changeset
   241
62
6e2eea62ccca Added global variables for logging.
Chris Jones <chris@cjones.org>
parents: 61
diff changeset
   242
	buf := bytes.NewBuffer([]uint8(prefix))
6
8e425e340ca1 Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents: 5
diff changeset
   243
	for {
8e425e340ca1 Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents: 5
diff changeset
   244
		var c [1]byte
8e425e340ca1 Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents: 5
diff changeset
   245
		n, _ := r.Read(c[:])
8e425e340ca1 Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents: 5
diff changeset
   246
		if n == 0 {
8e425e340ca1 Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents: 5
diff changeset
   247
			break
8e425e340ca1 Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents: 5
diff changeset
   248
		}
9
4fe926b03827 Reorganize so we have a layered approach to IO with the server.
Chris Jones <chris@cjones.org>
parents: 8
diff changeset
   249
		n, _ = w.Write(c[:n])
6
8e425e340ca1 Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents: 5
diff changeset
   250
		if n == 0 {
8e425e340ca1 Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents: 5
diff changeset
   251
			break
8e425e340ca1 Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents: 5
diff changeset
   252
		}
9
4fe926b03827 Reorganize so we have a layered approach to IO with the server.
Chris Jones <chris@cjones.org>
parents: 8
diff changeset
   253
		buf.Write(c[:n])
4fe926b03827 Reorganize so we have a layered approach to IO with the server.
Chris Jones <chris@cjones.org>
parents: 8
diff changeset
   254
		if c[0] == '\n' || c[0] == '>' {
76
caa722ab8a0f Fixed logging to use log rather than syslog.
Chris Jones <christian.jones@sri.com>
parents: 74
diff changeset
   255
			Log.Println(buf.String())
6
8e425e340ca1 Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents: 5
diff changeset
   256
			buf.Reset()
8e425e340ca1 Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents: 5
diff changeset
   257
		}
8e425e340ca1 Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents: 5
diff changeset
   258
	}
8e425e340ca1 Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents: 5
diff changeset
   259
	leftover := buf.String()
8e425e340ca1 Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents: 5
diff changeset
   260
	if leftover != "" {
76
caa722ab8a0f Fixed logging to use log rather than syslog.
Chris Jones <christian.jones@sri.com>
parents: 74
diff changeset
   261
		Log.Println(buf.String())
6
8e425e340ca1 Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents: 5
diff changeset
   262
	}
8e425e340ca1 Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents: 5
diff changeset
   263
}
9
4fe926b03827 Reorganize so we have a layered approach to IO with the server.
Chris Jones <chris@cjones.org>
parents: 8
diff changeset
   264
29
a456133ed0ac Don't accept data on Client.Out until resource binding is
Chris Jones <chris@cjones.org>
parents: 28
diff changeset
   265
// bindDone is called when we've finished resource binding (and all
a456133ed0ac Don't accept data on Client.Out until resource binding is
Chris Jones <chris@cjones.org>
parents: 28
diff changeset
   266
// the negotiations that precede it). Now we can start accepting
a456133ed0ac Don't accept data on Client.Out until resource binding is
Chris Jones <chris@cjones.org>
parents: 28
diff changeset
   267
// traffic from the app.
a456133ed0ac Don't accept data on Client.Out until resource binding is
Chris Jones <chris@cjones.org>
parents: 28
diff changeset
   268
func (cl *Client) bindDone() {
a456133ed0ac Don't accept data on Client.Out until resource binding is
Chris Jones <chris@cjones.org>
parents: 28
diff changeset
   269
	cl.inputControl <- 1
a456133ed0ac Don't accept data on Client.Out until resource binding is
Chris Jones <chris@cjones.org>
parents: 28
diff changeset
   270
}
a456133ed0ac Don't accept data on Client.Out until resource binding is
Chris Jones <chris@cjones.org>
parents: 28
diff changeset
   271
33
571713f49494 Added roster retrieval to StartSession().
Chris Jones <chris@cjones.org>
parents: 32
diff changeset
   272
// Start an XMPP session. A typical XMPP client should call this
571713f49494 Added roster retrieval to StartSession().
Chris Jones <chris@cjones.org>
parents: 32
diff changeset
   273
// immediately after creating the Client in order to start the
571713f49494 Added roster retrieval to StartSession().
Chris Jones <chris@cjones.org>
parents: 32
diff changeset
   274
// session, retrieve the roster, and broadcast an initial
571713f49494 Added roster retrieval to StartSession().
Chris Jones <chris@cjones.org>
parents: 32
diff changeset
   275
// presence. The presence can be as simple as a newly-initialized
571713f49494 Added roster retrieval to StartSession().
Chris Jones <chris@cjones.org>
parents: 32
diff changeset
   276
// Presence struct.  See RFC 3921, Section 3.
74
e619e18dcec3 Ran gofix from weekly-2012-01-15.
Chris Jones <christian.jones@sri.com>
parents: 72
diff changeset
   277
func (cl *Client) StartSession(getRoster bool, pr *Presence) error {
72
Chris Jones <christian.jones@sri.com>
parents: 67
diff changeset
   278
	id := <-Id
Chris Jones <christian.jones@sri.com>
parents: 67
diff changeset
   279
	iq := &Iq{To: cl.Jid.Domain, Id: id, Type: "set", Nested: []interface{}{Generic{XMLName: xml.Name{Space: NsSession, Local: "session"}}}}
74
e619e18dcec3 Ran gofix from weekly-2012-01-15.
Chris Jones <christian.jones@sri.com>
parents: 72
diff changeset
   280
	ch := make(chan error)
28
78961db80bae Added Client.StartSession().
Chris Jones <chris@cjones.org>
parents: 27
diff changeset
   281
	f := func(st Stanza) bool {
42
f6bb47ca12f2 Renamed the somewhat obscure XTo(), etc. to GetTo(), etc.
Chris Jones <chris@cjones.org>
parents: 41
diff changeset
   282
		if st.GetType() == "error" {
62
6e2eea62ccca Added global variables for logging.
Chris Jones <chris@cjones.org>
parents: 61
diff changeset
   283
			if Log != nil {
76
caa722ab8a0f Fixed logging to use log rather than syslog.
Chris Jones <christian.jones@sri.com>
parents: 74
diff changeset
   284
				Log.Printf("Can't start session: %v",
caa722ab8a0f Fixed logging to use log rather than syslog.
Chris Jones <christian.jones@sri.com>
parents: 74
diff changeset
   285
					st)
62
6e2eea62ccca Added global variables for logging.
Chris Jones <chris@cjones.org>
parents: 61
diff changeset
   286
			}
42
f6bb47ca12f2 Renamed the somewhat obscure XTo(), etc. to GetTo(), etc.
Chris Jones <chris@cjones.org>
parents: 41
diff changeset
   287
			ch <- st.GetError()
28
78961db80bae Added Client.StartSession().
Chris Jones <chris@cjones.org>
parents: 27
diff changeset
   288
			return false
78961db80bae Added Client.StartSession().
Chris Jones <chris@cjones.org>
parents: 27
diff changeset
   289
		}
78961db80bae Added Client.StartSession().
Chris Jones <chris@cjones.org>
parents: 27
diff changeset
   290
		ch <- nil
78961db80bae Added Client.StartSession().
Chris Jones <chris@cjones.org>
parents: 27
diff changeset
   291
		return false
78961db80bae Added Client.StartSession().
Chris Jones <chris@cjones.org>
parents: 27
diff changeset
   292
	}
78961db80bae Added Client.StartSession().
Chris Jones <chris@cjones.org>
parents: 27
diff changeset
   293
	cl.HandleStanza(id, f)
78961db80bae Added Client.StartSession().
Chris Jones <chris@cjones.org>
parents: 27
diff changeset
   294
	cl.Out <- iq
33
571713f49494 Added roster retrieval to StartSession().
Chris Jones <chris@cjones.org>
parents: 32
diff changeset
   295
28
78961db80bae Added Client.StartSession().
Chris Jones <chris@cjones.org>
parents: 27
diff changeset
   296
	// Now wait until the callback is called.
72
Chris Jones <christian.jones@sri.com>
parents: 67
diff changeset
   297
	if err := <-ch; err != nil {
33
571713f49494 Added roster retrieval to StartSession().
Chris Jones <chris@cjones.org>
parents: 32
diff changeset
   298
		return err
571713f49494 Added roster retrieval to StartSession().
Chris Jones <chris@cjones.org>
parents: 32
diff changeset
   299
	}
571713f49494 Added roster retrieval to StartSession().
Chris Jones <chris@cjones.org>
parents: 32
diff changeset
   300
	if getRoster {
57
e6cb3f049137 Revamped how the roster works. We're now using a channel to transmit snapshots
Chris Jones <chris@cjones.org>
parents: 51
diff changeset
   301
		err := fetchRoster(cl)
33
571713f49494 Added roster retrieval to StartSession().
Chris Jones <chris@cjones.org>
parents: 32
diff changeset
   302
		if err != nil {
571713f49494 Added roster retrieval to StartSession().
Chris Jones <chris@cjones.org>
parents: 32
diff changeset
   303
			return err
571713f49494 Added roster retrieval to StartSession().
Chris Jones <chris@cjones.org>
parents: 32
diff changeset
   304
		}
571713f49494 Added roster retrieval to StartSession().
Chris Jones <chris@cjones.org>
parents: 32
diff changeset
   305
	}
571713f49494 Added roster retrieval to StartSession().
Chris Jones <chris@cjones.org>
parents: 32
diff changeset
   306
	if pr != nil {
571713f49494 Added roster retrieval to StartSession().
Chris Jones <chris@cjones.org>
parents: 32
diff changeset
   307
		cl.Out <- pr
571713f49494 Added roster retrieval to StartSession().
Chris Jones <chris@cjones.org>
parents: 32
diff changeset
   308
	}
571713f49494 Added roster retrieval to StartSession().
Chris Jones <chris@cjones.org>
parents: 32
diff changeset
   309
	return nil
28
78961db80bae Added Client.StartSession().
Chris Jones <chris@cjones.org>
parents: 27
diff changeset
   310
}
45
abf958bcc201 Added a stack of filters which can intercept data before it gets to
Chris Jones <chris@cjones.org>
parents: 44
diff changeset
   311
abf958bcc201 Added a stack of filters which can intercept data before it gets to
Chris Jones <chris@cjones.org>
parents: 44
diff changeset
   312
// AddFilter adds a new filter to the top of the stack through which
abf958bcc201 Added a stack of filters which can intercept data before it gets to
Chris Jones <chris@cjones.org>
parents: 44
diff changeset
   313
// incoming stanzas travel on their way up to the client. The new
abf958bcc201 Added a stack of filters which can intercept data before it gets to
Chris Jones <chris@cjones.org>
parents: 44
diff changeset
   314
// filter's output channel is given to this function, and it returns a
abf958bcc201 Added a stack of filters which can intercept data before it gets to
Chris Jones <chris@cjones.org>
parents: 44
diff changeset
   315
// new input channel which the filter should read from. When its input
abf958bcc201 Added a stack of filters which can intercept data before it gets to
Chris Jones <chris@cjones.org>
parents: 44
diff changeset
   316
// channel closes, the filter should close its output channel.
abf958bcc201 Added a stack of filters which can intercept data before it gets to
Chris Jones <chris@cjones.org>
parents: 44
diff changeset
   317
func (cl *Client) AddFilter(out <-chan Stanza) <-chan Stanza {
abf958bcc201 Added a stack of filters which can intercept data before it gets to
Chris Jones <chris@cjones.org>
parents: 44
diff changeset
   318
	cl.filterOut <- out
72
Chris Jones <christian.jones@sri.com>
parents: 67
diff changeset
   319
	return <-cl.filterIn
45
abf958bcc201 Added a stack of filters which can intercept data before it gets to
Chris Jones <chris@cjones.org>
parents: 44
diff changeset
   320
}