stream.go
author Chris Jones <christian.jones@sri.com>
Sun, 16 Dec 2012 15:24:55 -0700
changeset 104 99e03b33b20d
parent 102 872e936f9f3f
child 105 aa895dfae3f6
permissions -rw-r--r--
Fixed reverse logic error on loggers.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
     1
// Copyright 2011 The Go Authors.  All rights reserved.
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
     2
// Use of this source code is governed by a BSD-style
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
     3
// license that can be found in the LICENSE file.
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
     4
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
     5
// This file contains the three layers of processing for the
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
     6
// communication with the server: transport (where TLS happens), XML
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
     7
// (where strings are converted to go structures), and Stream (where
17
d269d9c0fc8e Code review.
Chris Jones <chris@cjones.org>
parents: 16
diff changeset
     8
// we respond to XMPP events on behalf of the library client), or send
d269d9c0fc8e Code review.
Chris Jones <chris@cjones.org>
parents: 16
diff changeset
     9
// those events to the client.
10
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    10
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    11
package xmpp
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    12
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    13
import (
11
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
    14
	"crypto/md5"
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
    15
	"crypto/rand"
10
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    16
	"crypto/tls"
11
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
    17
	"encoding/base64"
98
c9cc4eda6dce Updated for Go 1.0 + upcoming XML fixes.
Chris Jones <chris@cjones.org>
parents: 72
diff changeset
    18
	"encoding/xml"
11
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
    19
	"fmt"
10
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    20
	"io"
98
c9cc4eda6dce Updated for Go 1.0 + upcoming XML fixes.
Chris Jones <chris@cjones.org>
parents: 72
diff changeset
    21
	"math/big"
10
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    22
	"net"
11
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
    23
	"regexp"
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
    24
	"strings"
10
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    25
	"time"
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    26
)
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    27
17
d269d9c0fc8e Code review.
Chris Jones <chris@cjones.org>
parents: 16
diff changeset
    28
// Callback to handle a stanza with a particular id.
13
c9527bbe99a6 Added a callback handler which will handle a stanza with a particular id.
Chris Jones <chris@cjones.org>
parents: 12
diff changeset
    29
type stanzaHandler struct {
c9527bbe99a6 Added a callback handler which will handle a stanza with a particular id.
Chris Jones <chris@cjones.org>
parents: 12
diff changeset
    30
	id string
17
d269d9c0fc8e Code review.
Chris Jones <chris@cjones.org>
parents: 16
diff changeset
    31
	// Return true means pass this to the application
13
c9527bbe99a6 Added a callback handler which will handle a stanza with a particular id.
Chris Jones <chris@cjones.org>
parents: 12
diff changeset
    32
	f func(Stanza) bool
c9527bbe99a6 Added a callback handler which will handle a stanza with a particular id.
Chris Jones <chris@cjones.org>
parents: 12
diff changeset
    33
}
c9527bbe99a6 Added a callback handler which will handle a stanza with a particular id.
Chris Jones <chris@cjones.org>
parents: 12
diff changeset
    34
20
e119444a1119 Replaced TODO comments with Go-style BUG(me) comments.
Chris Jones <chris@cjones.org>
parents: 19
diff changeset
    35
// BUG(cjyar) Review all these *Client receiver methods. They should
17
d269d9c0fc8e Code review.
Chris Jones <chris@cjones.org>
parents: 16
diff changeset
    36
// probably either all be receivers, or none.
d269d9c0fc8e Code review.
Chris Jones <chris@cjones.org>
parents: 16
diff changeset
    37
64
ac0639692317 Properly close all the channels and writers if Client.Out is close.
Chris Jones <chris@cjones.org>
parents: 63
diff changeset
    38
func (cl *Client) readTransport(w io.WriteCloser) {
ac0639692317 Properly close all the channels and writers if Client.Out is close.
Chris Jones <chris@cjones.org>
parents: 63
diff changeset
    39
	defer w.Close()
10
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    40
	p := make([]byte, 1024)
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    41
	for {
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    42
		if cl.socket == nil {
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    43
			cl.waitForSocket()
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    44
		}
98
c9cc4eda6dce Updated for Go 1.0 + upcoming XML fixes.
Chris Jones <chris@cjones.org>
parents: 72
diff changeset
    45
		cl.socket.SetReadDeadline(time.Now().Add(time.Second))
10
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    46
		nr, err := cl.socket.Read(p)
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    47
		if nr == 0 {
72
Chris Jones <christian.jones@sri.com>
parents: 66
diff changeset
    48
			if errno, ok := err.(*net.OpError); ok {
10
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    49
				if errno.Timeout() {
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    50
					continue
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    51
				}
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    52
			}
102
872e936f9f3f Reworked the logging again, and also added namespaces to a couple of fields on Features.
Chris Jones <christian.jones@sri.com>
parents: 100
diff changeset
    53
			Warn.Logf("read: %s", err)
10
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    54
			break
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    55
		}
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    56
		nw, err := w.Write(p[:nr])
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    57
		if nw < nr {
102
872e936f9f3f Reworked the logging again, and also added namespaces to a couple of fields on Features.
Chris Jones <christian.jones@sri.com>
parents: 100
diff changeset
    58
			Warn.Logf("read: %s", err)
10
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    59
			break
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    60
		}
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    61
	}
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    62
}
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    63
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    64
func (cl *Client) writeTransport(r io.Reader) {
64
ac0639692317 Properly close all the channels and writers if Client.Out is close.
Chris Jones <chris@cjones.org>
parents: 63
diff changeset
    65
	defer cl.socket.Close()
10
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    66
	p := make([]byte, 1024)
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    67
	for {
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    68
		nr, err := r.Read(p)
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    69
		if nr == 0 {
102
872e936f9f3f Reworked the logging again, and also added namespaces to a couple of fields on Features.
Chris Jones <christian.jones@sri.com>
parents: 100
diff changeset
    70
			Warn.Logf("write: %s", err)
10
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    71
			break
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    72
		}
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    73
		nw, err := cl.socket.Write(p[:nr])
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    74
		if nw < nr {
102
872e936f9f3f Reworked the logging again, and also added namespaces to a couple of fields on Features.
Chris Jones <christian.jones@sri.com>
parents: 100
diff changeset
    75
			Warn.Logf("write: %s", err)
10
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    76
			break
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    77
		}
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    78
	}
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    79
}
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    80
36
9fe022261dcc Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents: 35
diff changeset
    81
func readXml(r io.Reader, ch chan<- interface{},
72
Chris Jones <christian.jones@sri.com>
parents: 66
diff changeset
    82
extStanza map[string]func(*xml.Name) interface{}) {
104
99e03b33b20d Fixed reverse logic error on loggers.
Chris Jones <christian.jones@sri.com>
parents: 102
diff changeset
    83
	if _, ok := Debug.(*noLog) ; !ok {
10
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    84
		pr, pw := io.Pipe()
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    85
		go tee(r, pw, "S: ")
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    86
		r = pr
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    87
	}
64
ac0639692317 Properly close all the channels and writers if Client.Out is close.
Chris Jones <chris@cjones.org>
parents: 63
diff changeset
    88
	defer close(ch)
10
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    89
98
c9cc4eda6dce Updated for Go 1.0 + upcoming XML fixes.
Chris Jones <chris@cjones.org>
parents: 72
diff changeset
    90
	p := xml.NewDecoder(r)
c9cc4eda6dce Updated for Go 1.0 + upcoming XML fixes.
Chris Jones <chris@cjones.org>
parents: 72
diff changeset
    91
	p.Context.Map[""] = NsClient
c9cc4eda6dce Updated for Go 1.0 + upcoming XML fixes.
Chris Jones <chris@cjones.org>
parents: 72
diff changeset
    92
	p.Context.Map["stream"] = NsStream
63
c7f2edd25f4a Intermediate commit. Fixing how we close our channels and sockets and shut down our goroutines.
Chris Jones <chris@cjones.org>
parents: 62
diff changeset
    93
Loop:
10
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    94
	for {
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    95
		// Sniff the next token on the stream.
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    96
		t, err := p.Token()
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    97
		if t == nil {
98
c9cc4eda6dce Updated for Go 1.0 + upcoming XML fixes.
Chris Jones <chris@cjones.org>
parents: 72
diff changeset
    98
			if err != io.EOF {
102
872e936f9f3f Reworked the logging again, and also added namespaces to a couple of fields on Features.
Chris Jones <christian.jones@sri.com>
parents: 100
diff changeset
    99
				Warn.Logf("read: %s", err)
10
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   100
			}
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   101
			break
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   102
		}
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   103
		var se xml.StartElement
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   104
		var ok bool
72
Chris Jones <christian.jones@sri.com>
parents: 66
diff changeset
   105
		if se, ok = t.(xml.StartElement); !ok {
10
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   106
			continue
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   107
		}
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   108
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   109
		// Allocate the appropriate structure for this token.
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   110
		var obj interface{}
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   111
		switch se.Name.Space + " " + se.Name.Local {
34
7b1f924c75e2 Made the namespace constants public.
Chris Jones <chris@cjones.org>
parents: 33
diff changeset
   112
		case NsStream + " stream":
10
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   113
			st, err := parseStream(se)
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   114
			if err != nil {
102
872e936f9f3f Reworked the logging again, and also added namespaces to a couple of fields on Features.
Chris Jones <christian.jones@sri.com>
parents: 100
diff changeset
   115
				Warn.Logf("unmarshal stream: %s", err)
63
c7f2edd25f4a Intermediate commit. Fixing how we close our channels and sockets and shut down our goroutines.
Chris Jones <chris@cjones.org>
parents: 62
diff changeset
   116
				break Loop
10
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   117
			}
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   118
			ch <- st
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   119
			continue
34
7b1f924c75e2 Made the namespace constants public.
Chris Jones <chris@cjones.org>
parents: 33
diff changeset
   120
		case "stream error", NsStream + " error":
31
1dc47df5c99f Made streamError non-public, and made a first attempt at a stream
Chris Jones <chris@cjones.org>
parents: 30
diff changeset
   121
			obj = &streamError{}
34
7b1f924c75e2 Made the namespace constants public.
Chris Jones <chris@cjones.org>
parents: 33
diff changeset
   122
		case NsStream + " features":
10
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   123
			obj = &Features{}
34
7b1f924c75e2 Made the namespace constants public.
Chris Jones <chris@cjones.org>
parents: 33
diff changeset
   124
		case NsTLS + " proceed", NsTLS + " failure":
10
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   125
			obj = &starttls{}
34
7b1f924c75e2 Made the namespace constants public.
Chris Jones <chris@cjones.org>
parents: 33
diff changeset
   126
		case NsSASL + " challenge", NsSASL + " failure",
7b1f924c75e2 Made the namespace constants public.
Chris Jones <chris@cjones.org>
parents: 33
diff changeset
   127
			NsSASL + " success":
11
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   128
			obj = &auth{}
98
c9cc4eda6dce Updated for Go 1.0 + upcoming XML fixes.
Chris Jones <chris@cjones.org>
parents: 72
diff changeset
   129
		case NsClient + " iq":
12
122ab6208c3c Added resource binding and structures for <iq>, <message>, and <presence>.
Chris Jones <chris@cjones.org>
parents: 11
diff changeset
   130
			obj = &Iq{}
98
c9cc4eda6dce Updated for Go 1.0 + upcoming XML fixes.
Chris Jones <chris@cjones.org>
parents: 72
diff changeset
   131
		case NsClient + " message":
16
b839e37b3f29 Parse <presence> and <message> stanzas.
Chris Jones <chris@cjones.org>
parents: 15
diff changeset
   132
			obj = &Message{}
98
c9cc4eda6dce Updated for Go 1.0 + upcoming XML fixes.
Chris Jones <chris@cjones.org>
parents: 72
diff changeset
   133
		case NsClient + " presence":
16
b839e37b3f29 Parse <presence> and <message> stanzas.
Chris Jones <chris@cjones.org>
parents: 15
diff changeset
   134
			obj = &Presence{}
10
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   135
		default:
21
8f6ae5cfc9b9 Renamed Unrecognized to Generic.
Chris Jones <chris@cjones.org>
parents: 20
diff changeset
   136
			obj = &Generic{}
102
872e936f9f3f Reworked the logging again, and also added namespaces to a couple of fields on Features.
Chris Jones <christian.jones@sri.com>
parents: 100
diff changeset
   137
			Info.Logf("Ignoring unrecognized: %s %s", se.Name.Space,
100
24231ff0016c Reworked logging.
Chris Jones <christian.jones@sri.com>
parents: 98
diff changeset
   138
				se.Name.Local)
10
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   139
		}
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   140
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   141
		// Read the complete XML stanza.
98
c9cc4eda6dce Updated for Go 1.0 + upcoming XML fixes.
Chris Jones <chris@cjones.org>
parents: 72
diff changeset
   142
		err = p.DecodeElement(obj, &se)
10
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   143
		if err != nil {
102
872e936f9f3f Reworked the logging again, and also added namespaces to a couple of fields on Features.
Chris Jones <christian.jones@sri.com>
parents: 100
diff changeset
   144
			Warn.Logf("unmarshal: %s", err)
63
c7f2edd25f4a Intermediate commit. Fixing how we close our channels and sockets and shut down our goroutines.
Chris Jones <chris@cjones.org>
parents: 62
diff changeset
   145
			break Loop
10
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   146
		}
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   147
61
16513974d273 Stanzas can now contain multiple nested (extended) elements.
Chris Jones <chris@cjones.org>
parents: 59
diff changeset
   148
		// If it's a Stanza, we try to unmarshal its innerxml
16513974d273 Stanzas can now contain multiple nested (extended) elements.
Chris Jones <chris@cjones.org>
parents: 59
diff changeset
   149
		// into objects of the appropriate respective
16513974d273 Stanzas can now contain multiple nested (extended) elements.
Chris Jones <chris@cjones.org>
parents: 59
diff changeset
   150
		// types. This is specified by our extensions.
72
Chris Jones <christian.jones@sri.com>
parents: 66
diff changeset
   151
		if st, ok := obj.(Stanza); ok {
61
16513974d273 Stanzas can now contain multiple nested (extended) elements.
Chris Jones <chris@cjones.org>
parents: 59
diff changeset
   152
			err = parseExtended(st, extStanza)
16513974d273 Stanzas can now contain multiple nested (extended) elements.
Chris Jones <chris@cjones.org>
parents: 59
diff changeset
   153
			if err != nil {
102
872e936f9f3f Reworked the logging again, and also added namespaces to a couple of fields on Features.
Chris Jones <christian.jones@sri.com>
parents: 100
diff changeset
   154
				Warn.Logf("ext unmarshal: %s", err)
63
c7f2edd25f4a Intermediate commit. Fixing how we close our channels and sockets and shut down our goroutines.
Chris Jones <chris@cjones.org>
parents: 62
diff changeset
   155
				break Loop
36
9fe022261dcc Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents: 35
diff changeset
   156
			}
9fe022261dcc Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents: 35
diff changeset
   157
		}
17
d269d9c0fc8e Code review.
Chris Jones <chris@cjones.org>
parents: 16
diff changeset
   158
10
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   159
		// Put it on the channel.
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   160
		ch <- obj
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   161
	}
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   162
}
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   163
98
c9cc4eda6dce Updated for Go 1.0 + upcoming XML fixes.
Chris Jones <chris@cjones.org>
parents: 72
diff changeset
   164
func parseExtended(st Stanza, extStanza map[string]func(*xml.Name) interface{}) error {
38
2839fece923e Extended stanzas work now.
Chris Jones <chris@cjones.org>
parents: 37
diff changeset
   165
	// Now parse the stanza's innerxml to find the string that we
2839fece923e Extended stanzas work now.
Chris Jones <chris@cjones.org>
parents: 37
diff changeset
   166
	// can unmarshal this nested element from.
2839fece923e Extended stanzas work now.
Chris Jones <chris@cjones.org>
parents: 37
diff changeset
   167
	reader := strings.NewReader(st.innerxml())
98
c9cc4eda6dce Updated for Go 1.0 + upcoming XML fixes.
Chris Jones <chris@cjones.org>
parents: 72
diff changeset
   168
	p := xml.NewDecoder(reader)
38
2839fece923e Extended stanzas work now.
Chris Jones <chris@cjones.org>
parents: 37
diff changeset
   169
	for {
2839fece923e Extended stanzas work now.
Chris Jones <chris@cjones.org>
parents: 37
diff changeset
   170
		t, err := p.Token()
98
c9cc4eda6dce Updated for Go 1.0 + upcoming XML fixes.
Chris Jones <chris@cjones.org>
parents: 72
diff changeset
   171
		if err == io.EOF {
61
16513974d273 Stanzas can now contain multiple nested (extended) elements.
Chris Jones <chris@cjones.org>
parents: 59
diff changeset
   172
			break
16513974d273 Stanzas can now contain multiple nested (extended) elements.
Chris Jones <chris@cjones.org>
parents: 59
diff changeset
   173
		}
38
2839fece923e Extended stanzas work now.
Chris Jones <chris@cjones.org>
parents: 37
diff changeset
   174
		if err != nil {
2839fece923e Extended stanzas work now.
Chris Jones <chris@cjones.org>
parents: 37
diff changeset
   175
			return err
2839fece923e Extended stanzas work now.
Chris Jones <chris@cjones.org>
parents: 37
diff changeset
   176
		}
72
Chris Jones <christian.jones@sri.com>
parents: 66
diff changeset
   177
		if se, ok := t.(xml.StartElement); ok {
Chris Jones <christian.jones@sri.com>
parents: 66
diff changeset
   178
			if con, ok := extStanza[se.Name.Space]; ok {
61
16513974d273 Stanzas can now contain multiple nested (extended) elements.
Chris Jones <chris@cjones.org>
parents: 59
diff changeset
   179
				// Call the indicated constructor.
16513974d273 Stanzas can now contain multiple nested (extended) elements.
Chris Jones <chris@cjones.org>
parents: 59
diff changeset
   180
				nested := con(&se.Name)
16513974d273 Stanzas can now contain multiple nested (extended) elements.
Chris Jones <chris@cjones.org>
parents: 59
diff changeset
   181
16513974d273 Stanzas can now contain multiple nested (extended) elements.
Chris Jones <chris@cjones.org>
parents: 59
diff changeset
   182
				// Unmarshal the nested element and
16513974d273 Stanzas can now contain multiple nested (extended) elements.
Chris Jones <chris@cjones.org>
parents: 59
diff changeset
   183
				// stuff it back into the stanza.
98
c9cc4eda6dce Updated for Go 1.0 + upcoming XML fixes.
Chris Jones <chris@cjones.org>
parents: 72
diff changeset
   184
				err := p.DecodeElement(nested, &se)
61
16513974d273 Stanzas can now contain multiple nested (extended) elements.
Chris Jones <chris@cjones.org>
parents: 59
diff changeset
   185
				if err != nil {
16513974d273 Stanzas can now contain multiple nested (extended) elements.
Chris Jones <chris@cjones.org>
parents: 59
diff changeset
   186
					return err
16513974d273 Stanzas can now contain multiple nested (extended) elements.
Chris Jones <chris@cjones.org>
parents: 59
diff changeset
   187
				}
16513974d273 Stanzas can now contain multiple nested (extended) elements.
Chris Jones <chris@cjones.org>
parents: 59
diff changeset
   188
				st.addNested(nested)
38
2839fece923e Extended stanzas work now.
Chris Jones <chris@cjones.org>
parents: 37
diff changeset
   189
			}
2839fece923e Extended stanzas work now.
Chris Jones <chris@cjones.org>
parents: 37
diff changeset
   190
		}
2839fece923e Extended stanzas work now.
Chris Jones <chris@cjones.org>
parents: 37
diff changeset
   191
	}
2839fece923e Extended stanzas work now.
Chris Jones <chris@cjones.org>
parents: 37
diff changeset
   192
2839fece923e Extended stanzas work now.
Chris Jones <chris@cjones.org>
parents: 37
diff changeset
   193
	return nil
2839fece923e Extended stanzas work now.
Chris Jones <chris@cjones.org>
parents: 37
diff changeset
   194
}
2839fece923e Extended stanzas work now.
Chris Jones <chris@cjones.org>
parents: 37
diff changeset
   195
10
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   196
func writeXml(w io.Writer, ch <-chan interface{}) {
104
99e03b33b20d Fixed reverse logic error on loggers.
Chris Jones <christian.jones@sri.com>
parents: 102
diff changeset
   197
	if _, ok := Debug.(*noLog) ; !ok {
10
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   198
		pr, pw := io.Pipe()
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   199
		go tee(pr, w, "C: ")
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   200
		w = pw
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   201
	}
64
ac0639692317 Properly close all the channels and writers if Client.Out is close.
Chris Jones <chris@cjones.org>
parents: 63
diff changeset
   202
	defer func(w io.Writer) {
72
Chris Jones <christian.jones@sri.com>
parents: 66
diff changeset
   203
		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
   204
			c.Close()
ac0639692317 Properly close all the channels and writers if Client.Out is close.
Chris Jones <chris@cjones.org>
parents: 63
diff changeset
   205
		}
ac0639692317 Properly close all the channels and writers if Client.Out is close.
Chris Jones <chris@cjones.org>
parents: 63
diff changeset
   206
	}(w)
10
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   207
98
c9cc4eda6dce Updated for Go 1.0 + upcoming XML fixes.
Chris Jones <chris@cjones.org>
parents: 72
diff changeset
   208
	enc := xml.NewEncoder(w)
c9cc4eda6dce Updated for Go 1.0 + upcoming XML fixes.
Chris Jones <chris@cjones.org>
parents: 72
diff changeset
   209
	enc.Context.Map[NsClient] = ""
c9cc4eda6dce Updated for Go 1.0 + upcoming XML fixes.
Chris Jones <chris@cjones.org>
parents: 72
diff changeset
   210
	enc.Context.Map[NsStream] = "stream"
c9cc4eda6dce Updated for Go 1.0 + upcoming XML fixes.
Chris Jones <chris@cjones.org>
parents: 72
diff changeset
   211
10
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   212
	for obj := range ch {
98
c9cc4eda6dce Updated for Go 1.0 + upcoming XML fixes.
Chris Jones <chris@cjones.org>
parents: 72
diff changeset
   213
		if st, ok := obj.(*stream); ok {
c9cc4eda6dce Updated for Go 1.0 + upcoming XML fixes.
Chris Jones <chris@cjones.org>
parents: 72
diff changeset
   214
			_, err := w.Write([]byte(st.String()))
c9cc4eda6dce Updated for Go 1.0 + upcoming XML fixes.
Chris Jones <chris@cjones.org>
parents: 72
diff changeset
   215
			if err != nil {
102
872e936f9f3f Reworked the logging again, and also added namespaces to a couple of fields on Features.
Chris Jones <christian.jones@sri.com>
parents: 100
diff changeset
   216
				Warn.Logf("write: %s", err)
62
6e2eea62ccca Added global variables for logging.
Chris Jones <chris@cjones.org>
parents: 61
diff changeset
   217
			}
98
c9cc4eda6dce Updated for Go 1.0 + upcoming XML fixes.
Chris Jones <chris@cjones.org>
parents: 72
diff changeset
   218
		} else {
c9cc4eda6dce Updated for Go 1.0 + upcoming XML fixes.
Chris Jones <chris@cjones.org>
parents: 72
diff changeset
   219
			err := enc.Encode(obj)
c9cc4eda6dce Updated for Go 1.0 + upcoming XML fixes.
Chris Jones <chris@cjones.org>
parents: 72
diff changeset
   220
			if err != nil {
102
872e936f9f3f Reworked the logging again, and also added namespaces to a couple of fields on Features.
Chris Jones <christian.jones@sri.com>
parents: 100
diff changeset
   221
				Warn.Logf("marshal: %s", err)
98
c9cc4eda6dce Updated for Go 1.0 + upcoming XML fixes.
Chris Jones <chris@cjones.org>
parents: 72
diff changeset
   222
				break
c9cc4eda6dce Updated for Go 1.0 + upcoming XML fixes.
Chris Jones <chris@cjones.org>
parents: 72
diff changeset
   223
			}
10
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   224
		}
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   225
	}
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   226
}
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   227
23
b5de44679389 Made the input and output channels of type Stanza rather than
Chris Jones <chris@cjones.org>
parents: 22
diff changeset
   228
func (cl *Client) readStream(srvIn <-chan interface{}, cliOut chan<- Stanza) {
63
c7f2edd25f4a Intermediate commit. Fixing how we close our channels and sockets and shut down our goroutines.
Chris Jones <chris@cjones.org>
parents: 62
diff changeset
   229
	defer close(cliOut)
10
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   230
72
Chris Jones <christian.jones@sri.com>
parents: 66
diff changeset
   231
	handlers := make(map[string]func(Stanza) bool)
63
c7f2edd25f4a Intermediate commit. Fixing how we close our channels and sockets and shut down our goroutines.
Chris Jones <chris@cjones.org>
parents: 62
diff changeset
   232
Loop:
13
c9527bbe99a6 Added a callback handler which will handle a stanza with a particular id.
Chris Jones <chris@cjones.org>
parents: 12
diff changeset
   233
	for {
c9527bbe99a6 Added a callback handler which will handle a stanza with a particular id.
Chris Jones <chris@cjones.org>
parents: 12
diff changeset
   234
		select {
72
Chris Jones <christian.jones@sri.com>
parents: 66
diff changeset
   235
		case h := <-cl.handlers:
13
c9527bbe99a6 Added a callback handler which will handle a stanza with a particular id.
Chris Jones <chris@cjones.org>
parents: 12
diff changeset
   236
			handlers[h.id] = h.f
72
Chris Jones <christian.jones@sri.com>
parents: 66
diff changeset
   237
		case x, ok := <-srvIn:
63
c7f2edd25f4a Intermediate commit. Fixing how we close our channels and sockets and shut down our goroutines.
Chris Jones <chris@cjones.org>
parents: 62
diff changeset
   238
			if !ok {
c7f2edd25f4a Intermediate commit. Fixing how we close our channels and sockets and shut down our goroutines.
Chris Jones <chris@cjones.org>
parents: 62
diff changeset
   239
				break Loop
26
4d0a369079ce Removed the TextOut channel.
Chris Jones <chris@cjones.org>
parents: 25
diff changeset
   240
			}
13
c9527bbe99a6 Added a callback handler which will handle a stanza with a particular id.
Chris Jones <chris@cjones.org>
parents: 12
diff changeset
   241
			send := false
c9527bbe99a6 Added a callback handler which will handle a stanza with a particular id.
Chris Jones <chris@cjones.org>
parents: 12
diff changeset
   242
			switch obj := x.(type) {
22
d6b7b4cbf50d Made the stream type non-public.
Chris Jones <chris@cjones.org>
parents: 21
diff changeset
   243
			case *stream:
13
c9527bbe99a6 Added a callback handler which will handle a stanza with a particular id.
Chris Jones <chris@cjones.org>
parents: 12
diff changeset
   244
				handleStream(obj)
31
1dc47df5c99f Made streamError non-public, and made a first attempt at a stream
Chris Jones <chris@cjones.org>
parents: 30
diff changeset
   245
			case *streamError:
1dc47df5c99f Made streamError non-public, and made a first attempt at a stream
Chris Jones <chris@cjones.org>
parents: 30
diff changeset
   246
				cl.handleStreamError(obj)
13
c9527bbe99a6 Added a callback handler which will handle a stanza with a particular id.
Chris Jones <chris@cjones.org>
parents: 12
diff changeset
   247
			case *Features:
c9527bbe99a6 Added a callback handler which will handle a stanza with a particular id.
Chris Jones <chris@cjones.org>
parents: 12
diff changeset
   248
				cl.handleFeatures(obj)
c9527bbe99a6 Added a callback handler which will handle a stanza with a particular id.
Chris Jones <chris@cjones.org>
parents: 12
diff changeset
   249
			case *starttls:
c9527bbe99a6 Added a callback handler which will handle a stanza with a particular id.
Chris Jones <chris@cjones.org>
parents: 12
diff changeset
   250
				cl.handleTls(obj)
c9527bbe99a6 Added a callback handler which will handle a stanza with a particular id.
Chris Jones <chris@cjones.org>
parents: 12
diff changeset
   251
			case *auth:
c9527bbe99a6 Added a callback handler which will handle a stanza with a particular id.
Chris Jones <chris@cjones.org>
parents: 12
diff changeset
   252
				cl.handleSasl(obj)
c9527bbe99a6 Added a callback handler which will handle a stanza with a particular id.
Chris Jones <chris@cjones.org>
parents: 12
diff changeset
   253
			default:
c9527bbe99a6 Added a callback handler which will handle a stanza with a particular id.
Chris Jones <chris@cjones.org>
parents: 12
diff changeset
   254
				send = true
c9527bbe99a6 Added a callback handler which will handle a stanza with a particular id.
Chris Jones <chris@cjones.org>
parents: 12
diff changeset
   255
			}
29
a456133ed0ac Don't accept data on Client.Out until resource binding is
Chris Jones <chris@cjones.org>
parents: 26
diff changeset
   256
			if !send {
a456133ed0ac Don't accept data on Client.Out until resource binding is
Chris Jones <chris@cjones.org>
parents: 26
diff changeset
   257
				continue
a456133ed0ac Don't accept data on Client.Out until resource binding is
Chris Jones <chris@cjones.org>
parents: 26
diff changeset
   258
			}
23
b5de44679389 Made the input and output channels of type Stanza rather than
Chris Jones <chris@cjones.org>
parents: 22
diff changeset
   259
			st, ok := x.(Stanza)
b5de44679389 Made the input and output channels of type Stanza rather than
Chris Jones <chris@cjones.org>
parents: 22
diff changeset
   260
			if !ok {
102
872e936f9f3f Reworked the logging again, and also added namespaces to a couple of fields on Features.
Chris Jones <christian.jones@sri.com>
parents: 100
diff changeset
   261
				Warn.Logf("Unhandled non-stanza: %v", x)
23
b5de44679389 Made the input and output channels of type Stanza rather than
Chris Jones <chris@cjones.org>
parents: 22
diff changeset
   262
				continue
b5de44679389 Made the input and output channels of type Stanza rather than
Chris Jones <chris@cjones.org>
parents: 22
diff changeset
   263
			}
42
f6bb47ca12f2 Renamed the somewhat obscure XTo(), etc. to GetTo(), etc.
Chris Jones <chris@cjones.org>
parents: 41
diff changeset
   264
			if handlers[st.GetId()] != nil {
f6bb47ca12f2 Renamed the somewhat obscure XTo(), etc. to GetTo(), etc.
Chris Jones <chris@cjones.org>
parents: 41
diff changeset
   265
				f := handlers[st.GetId()]
98
c9cc4eda6dce Updated for Go 1.0 + upcoming XML fixes.
Chris Jones <chris@cjones.org>
parents: 72
diff changeset
   266
				delete(handlers, st.GetId())
13
c9527bbe99a6 Added a callback handler which will handle a stanza with a particular id.
Chris Jones <chris@cjones.org>
parents: 12
diff changeset
   267
				send = f(st)
c9527bbe99a6 Added a callback handler which will handle a stanza with a particular id.
Chris Jones <chris@cjones.org>
parents: 12
diff changeset
   268
			}
c9527bbe99a6 Added a callback handler which will handle a stanza with a particular id.
Chris Jones <chris@cjones.org>
parents: 12
diff changeset
   269
			if send {
23
b5de44679389 Made the input and output channels of type Stanza rather than
Chris Jones <chris@cjones.org>
parents: 22
diff changeset
   270
				cliOut <- st
13
c9527bbe99a6 Added a callback handler which will handle a stanza with a particular id.
Chris Jones <chris@cjones.org>
parents: 12
diff changeset
   271
			}
10
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   272
		}
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   273
	}
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   274
}
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   275
29
a456133ed0ac Don't accept data on Client.Out until resource binding is
Chris Jones <chris@cjones.org>
parents: 26
diff changeset
   276
// This loop is paused until resource binding is complete. Otherwise
a456133ed0ac Don't accept data on Client.Out until resource binding is
Chris Jones <chris@cjones.org>
parents: 26
diff changeset
   277
// the app might inject something inappropriate into our negotiations
a456133ed0ac Don't accept data on Client.Out until resource binding is
Chris Jones <chris@cjones.org>
parents: 26
diff changeset
   278
// with the server. The control channel controls this loop's
a456133ed0ac Don't accept data on Client.Out until resource binding is
Chris Jones <chris@cjones.org>
parents: 26
diff changeset
   279
// activity.
a456133ed0ac Don't accept data on Client.Out until resource binding is
Chris Jones <chris@cjones.org>
parents: 26
diff changeset
   280
func writeStream(srvOut chan<- interface{}, cliIn <-chan Stanza,
72
Chris Jones <christian.jones@sri.com>
parents: 66
diff changeset
   281
control <-chan int) {
63
c7f2edd25f4a Intermediate commit. Fixing how we close our channels and sockets and shut down our goroutines.
Chris Jones <chris@cjones.org>
parents: 62
diff changeset
   282
	defer close(srvOut)
10
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   283
29
a456133ed0ac Don't accept data on Client.Out until resource binding is
Chris Jones <chris@cjones.org>
parents: 26
diff changeset
   284
	var input <-chan Stanza
63
c7f2edd25f4a Intermediate commit. Fixing how we close our channels and sockets and shut down our goroutines.
Chris Jones <chris@cjones.org>
parents: 62
diff changeset
   285
Loop:
29
a456133ed0ac Don't accept data on Client.Out until resource binding is
Chris Jones <chris@cjones.org>
parents: 26
diff changeset
   286
	for {
a456133ed0ac Don't accept data on Client.Out until resource binding is
Chris Jones <chris@cjones.org>
parents: 26
diff changeset
   287
		select {
72
Chris Jones <christian.jones@sri.com>
parents: 66
diff changeset
   288
		case status := <-control:
29
a456133ed0ac Don't accept data on Client.Out until resource binding is
Chris Jones <chris@cjones.org>
parents: 26
diff changeset
   289
			switch status {
a456133ed0ac Don't accept data on Client.Out until resource binding is
Chris Jones <chris@cjones.org>
parents: 26
diff changeset
   290
			case 0:
a456133ed0ac Don't accept data on Client.Out until resource binding is
Chris Jones <chris@cjones.org>
parents: 26
diff changeset
   291
				input = nil
a456133ed0ac Don't accept data on Client.Out until resource binding is
Chris Jones <chris@cjones.org>
parents: 26
diff changeset
   292
			case 1:
a456133ed0ac Don't accept data on Client.Out until resource binding is
Chris Jones <chris@cjones.org>
parents: 26
diff changeset
   293
				input = cliIn
a456133ed0ac Don't accept data on Client.Out until resource binding is
Chris Jones <chris@cjones.org>
parents: 26
diff changeset
   294
			case -1:
63
c7f2edd25f4a Intermediate commit. Fixing how we close our channels and sockets and shut down our goroutines.
Chris Jones <chris@cjones.org>
parents: 62
diff changeset
   295
				break Loop
29
a456133ed0ac Don't accept data on Client.Out until resource binding is
Chris Jones <chris@cjones.org>
parents: 26
diff changeset
   296
			}
72
Chris Jones <christian.jones@sri.com>
parents: 66
diff changeset
   297
		case x, ok := <-input:
63
c7f2edd25f4a Intermediate commit. Fixing how we close our channels and sockets and shut down our goroutines.
Chris Jones <chris@cjones.org>
parents: 62
diff changeset
   298
			if !ok {
c7f2edd25f4a Intermediate commit. Fixing how we close our channels and sockets and shut down our goroutines.
Chris Jones <chris@cjones.org>
parents: 62
diff changeset
   299
				break Loop
c7f2edd25f4a Intermediate commit. Fixing how we close our channels and sockets and shut down our goroutines.
Chris Jones <chris@cjones.org>
parents: 62
diff changeset
   300
			}
51
1af366d10d32 Nil checks and a greatly simplified filter manager which is less buggy.
Chris Jones <chris@cjones.org>
parents: 46
diff changeset
   301
			if x == nil {
102
872e936f9f3f Reworked the logging again, and also added namespaces to a couple of fields on Features.
Chris Jones <christian.jones@sri.com>
parents: 100
diff changeset
   302
				Info.Logf("Refusing to send nil stanza")
51
1af366d10d32 Nil checks and a greatly simplified filter manager which is less buggy.
Chris Jones <chris@cjones.org>
parents: 46
diff changeset
   303
				continue
1af366d10d32 Nil checks and a greatly simplified filter manager which is less buggy.
Chris Jones <chris@cjones.org>
parents: 46
diff changeset
   304
			}
29
a456133ed0ac Don't accept data on Client.Out until resource binding is
Chris Jones <chris@cjones.org>
parents: 26
diff changeset
   305
			srvOut <- x
a456133ed0ac Don't accept data on Client.Out until resource binding is
Chris Jones <chris@cjones.org>
parents: 26
diff changeset
   306
		}
10
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   307
	}
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   308
}
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   309
45
abf958bcc201 Added a stack of filters which can intercept data before it gets to
Chris Jones <chris@cjones.org>
parents: 42
diff changeset
   310
// Stanzas from the remote go up through a stack of filters to the
abf958bcc201 Added a stack of filters which can intercept data before it gets to
Chris Jones <chris@cjones.org>
parents: 42
diff changeset
   311
// app. This function manages the filters.
51
1af366d10d32 Nil checks and a greatly simplified filter manager which is less buggy.
Chris Jones <chris@cjones.org>
parents: 46
diff changeset
   312
func filterTop(filterOut <-chan <-chan Stanza, filterIn chan<- <-chan Stanza,
72
Chris Jones <christian.jones@sri.com>
parents: 66
diff changeset
   313
topFilter <-chan Stanza, app chan<- Stanza) {
51
1af366d10d32 Nil checks and a greatly simplified filter manager which is less buggy.
Chris Jones <chris@cjones.org>
parents: 46
diff changeset
   314
	defer close(app)
63
c7f2edd25f4a Intermediate commit. Fixing how we close our channels and sockets and shut down our goroutines.
Chris Jones <chris@cjones.org>
parents: 62
diff changeset
   315
Loop:
45
abf958bcc201 Added a stack of filters which can intercept data before it gets to
Chris Jones <chris@cjones.org>
parents: 42
diff changeset
   316
	for {
abf958bcc201 Added a stack of filters which can intercept data before it gets to
Chris Jones <chris@cjones.org>
parents: 42
diff changeset
   317
		select {
72
Chris Jones <christian.jones@sri.com>
parents: 66
diff changeset
   318
		case newFilterOut := <-filterOut:
51
1af366d10d32 Nil checks and a greatly simplified filter manager which is less buggy.
Chris Jones <chris@cjones.org>
parents: 46
diff changeset
   319
			if newFilterOut == nil {
102
872e936f9f3f Reworked the logging again, and also added namespaces to a couple of fields on Features.
Chris Jones <christian.jones@sri.com>
parents: 100
diff changeset
   320
				Warn.Logf("Received nil filter")
51
1af366d10d32 Nil checks and a greatly simplified filter manager which is less buggy.
Chris Jones <chris@cjones.org>
parents: 46
diff changeset
   321
				filterIn <- nil
1af366d10d32 Nil checks and a greatly simplified filter manager which is less buggy.
Chris Jones <chris@cjones.org>
parents: 46
diff changeset
   322
				continue
45
abf958bcc201 Added a stack of filters which can intercept data before it gets to
Chris Jones <chris@cjones.org>
parents: 42
diff changeset
   323
			}
51
1af366d10d32 Nil checks and a greatly simplified filter manager which is less buggy.
Chris Jones <chris@cjones.org>
parents: 46
diff changeset
   324
			filterIn <- topFilter
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
   325
			topFilter = newFilterOut
45
abf958bcc201 Added a stack of filters which can intercept data before it gets to
Chris Jones <chris@cjones.org>
parents: 42
diff changeset
   326
51
1af366d10d32 Nil checks and a greatly simplified filter manager which is less buggy.
Chris Jones <chris@cjones.org>
parents: 46
diff changeset
   327
		case data, ok := <-topFilter:
45
abf958bcc201 Added a stack of filters which can intercept data before it gets to
Chris Jones <chris@cjones.org>
parents: 42
diff changeset
   328
			if !ok {
63
c7f2edd25f4a Intermediate commit. Fixing how we close our channels and sockets and shut down our goroutines.
Chris Jones <chris@cjones.org>
parents: 62
diff changeset
   329
				break Loop
45
abf958bcc201 Added a stack of filters which can intercept data before it gets to
Chris Jones <chris@cjones.org>
parents: 42
diff changeset
   330
			}
51
1af366d10d32 Nil checks and a greatly simplified filter manager which is less buggy.
Chris Jones <chris@cjones.org>
parents: 46
diff changeset
   331
			app <- data
1af366d10d32 Nil checks and a greatly simplified filter manager which is less buggy.
Chris Jones <chris@cjones.org>
parents: 46
diff changeset
   332
		}
1af366d10d32 Nil checks and a greatly simplified filter manager which is less buggy.
Chris Jones <chris@cjones.org>
parents: 46
diff changeset
   333
	}
1af366d10d32 Nil checks and a greatly simplified filter manager which is less buggy.
Chris Jones <chris@cjones.org>
parents: 46
diff changeset
   334
}
45
abf958bcc201 Added a stack of filters which can intercept data before it gets to
Chris Jones <chris@cjones.org>
parents: 42
diff changeset
   335
51
1af366d10d32 Nil checks and a greatly simplified filter manager which is less buggy.
Chris Jones <chris@cjones.org>
parents: 46
diff changeset
   336
func filterBottom(from <-chan Stanza, to chan<- Stanza) {
1af366d10d32 Nil checks and a greatly simplified filter manager which is less buggy.
Chris Jones <chris@cjones.org>
parents: 46
diff changeset
   337
	defer close(to)
72
Chris Jones <christian.jones@sri.com>
parents: 66
diff changeset
   338
	for data := range from {
51
1af366d10d32 Nil checks and a greatly simplified filter manager which is less buggy.
Chris Jones <chris@cjones.org>
parents: 46
diff changeset
   339
		to <- data
45
abf958bcc201 Added a stack of filters which can intercept data before it gets to
Chris Jones <chris@cjones.org>
parents: 42
diff changeset
   340
	}
abf958bcc201 Added a stack of filters which can intercept data before it gets to
Chris Jones <chris@cjones.org>
parents: 42
diff changeset
   341
}
abf958bcc201 Added a stack of filters which can intercept data before it gets to
Chris Jones <chris@cjones.org>
parents: 42
diff changeset
   342
22
d6b7b4cbf50d Made the stream type non-public.
Chris Jones <chris@cjones.org>
parents: 21
diff changeset
   343
func handleStream(ss *stream) {
10
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   344
}
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   345
31
1dc47df5c99f Made streamError non-public, and made a first attempt at a stream
Chris Jones <chris@cjones.org>
parents: 30
diff changeset
   346
func (cl *Client) handleStreamError(se *streamError) {
102
872e936f9f3f Reworked the logging again, and also added namespaces to a couple of fields on Features.
Chris Jones <christian.jones@sri.com>
parents: 100
diff changeset
   347
	Info.Logf("Received stream error: %v", se)
63
c7f2edd25f4a Intermediate commit. Fixing how we close our channels and sockets and shut down our goroutines.
Chris Jones <chris@cjones.org>
parents: 62
diff changeset
   348
	close(cl.Out)
31
1dc47df5c99f Made streamError non-public, and made a first attempt at a stream
Chris Jones <chris@cjones.org>
parents: 30
diff changeset
   349
}
1dc47df5c99f Made streamError non-public, and made a first attempt at a stream
Chris Jones <chris@cjones.org>
parents: 30
diff changeset
   350
11
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   351
func (cl *Client) handleFeatures(fe *Features) {
32
4e68d8f89dc3 Make the server's advertised features available to the app.
Chris Jones <chris@cjones.org>
parents: 31
diff changeset
   352
	cl.Features = fe
10
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   353
	if fe.Starttls != nil {
34
7b1f924c75e2 Made the namespace constants public.
Chris Jones <chris@cjones.org>
parents: 33
diff changeset
   354
		start := &starttls{XMLName: xml.Name{Space: NsTLS,
10
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   355
			Local: "starttls"}}
11
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   356
		cl.xmlOut <- start
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   357
		return
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   358
	}
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   359
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   360
	if len(fe.Mechanisms.Mechanism) > 0 {
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   361
		cl.chooseSasl(fe)
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   362
		return
10
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   363
	}
12
122ab6208c3c Added resource binding and structures for <iq>, <message>, and <presence>.
Chris Jones <chris@cjones.org>
parents: 11
diff changeset
   364
122ab6208c3c Added resource binding and structures for <iq>, <message>, and <presence>.
Chris Jones <chris@cjones.org>
parents: 11
diff changeset
   365
	if fe.Bind != nil {
122ab6208c3c Added resource binding and structures for <iq>, <message>, and <presence>.
Chris Jones <chris@cjones.org>
parents: 11
diff changeset
   366
		cl.bind(fe.Bind)
17
d269d9c0fc8e Code review.
Chris Jones <chris@cjones.org>
parents: 16
diff changeset
   367
		return
12
122ab6208c3c Added resource binding and structures for <iq>, <message>, and <presence>.
Chris Jones <chris@cjones.org>
parents: 11
diff changeset
   368
	}
10
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   369
}
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   370
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   371
// readTransport() is running concurrently. We need to stop it,
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   372
// negotiate TLS, then start it again. It calls waitForSocket() in
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   373
// its inner loop; see below.
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   374
func (cl *Client) handleTls(t *starttls) {
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   375
	tcp := cl.socket
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   376
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   377
	// Set the socket to nil, and wait for the reader routine to
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   378
	// signal that it's paused.
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   379
	cl.socket = nil
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   380
	cl.socketSync.Add(1)
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   381
	cl.socketSync.Wait()
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   382
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   383
	// Negotiate TLS with the server.
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   384
	tls := tls.Client(tcp, nil)
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   385
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   386
	// Make the TLS connection available to the reader, and wait
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   387
	// for it to signal that it's working again.
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   388
	cl.socketSync.Add(1)
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   389
	cl.socket = tls
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   390
	cl.socketSync.Wait()
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   391
102
872e936f9f3f Reworked the logging again, and also added namespaces to a couple of fields on Features.
Chris Jones <christian.jones@sri.com>
parents: 100
diff changeset
   392
	Info.Logf("TLS negotiation succeeded.")
32
4e68d8f89dc3 Make the server's advertised features available to the app.
Chris Jones <chris@cjones.org>
parents: 31
diff changeset
   393
	cl.Features = nil
10
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   394
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   395
	// Now re-send the initial handshake message to start the new
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   396
	// session.
22
d6b7b4cbf50d Made the stream type non-public.
Chris Jones <chris@cjones.org>
parents: 21
diff changeset
   397
	hsOut := &stream{To: cl.Jid.Domain, Version: Version}
10
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   398
	cl.xmlOut <- hsOut
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   399
}
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   400
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   401
// Synchronize with handleTls(). Called from readTransport() when
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   402
// cl.socket is nil.
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   403
func (cl *Client) waitForSocket() {
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   404
	// Signal that we've stopped reading from the socket.
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   405
	cl.socketSync.Done()
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   406
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   407
	// Wait until the socket is available again.
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   408
	for cl.socket == nil {
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   409
		time.Sleep(1e8)
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   410
	}
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   411
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   412
	// Signal that we're going back to the read loop.
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   413
	cl.socketSync.Done()
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   414
}
11
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   415
59
be6815a9653a Comment reformat.
Chris Jones <chris@cjones.org>
parents: 57
diff changeset
   416
// BUG(cjyar): Doesn't implement TLS/SASL EXTERNAL.
11
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   417
func (cl *Client) chooseSasl(fe *Features) {
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   418
	var digestMd5 bool
72
Chris Jones <christian.jones@sri.com>
parents: 66
diff changeset
   419
	for _, m := range fe.Mechanisms.Mechanism {
11
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   420
		switch strings.ToLower(m) {
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   421
		case "digest-md5":
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   422
			digestMd5 = true
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   423
		}
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   424
	}
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   425
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   426
	if digestMd5 {
72
Chris Jones <christian.jones@sri.com>
parents: 66
diff changeset
   427
		auth := &auth{XMLName: xml.Name{Space: NsSASL, Local: "auth"}, Mechanism: "DIGEST-MD5"}
11
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   428
		cl.xmlOut <- auth
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   429
	}
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   430
}
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   431
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   432
func (cl *Client) handleSasl(srv *auth) {
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   433
	switch strings.ToLower(srv.XMLName.Local) {
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   434
	case "challenge":
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   435
		b64 := base64.StdEncoding
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   436
		str, err := b64.DecodeString(srv.Chardata)
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   437
		if err != nil {
102
872e936f9f3f Reworked the logging again, and also added namespaces to a couple of fields on Features.
Chris Jones <christian.jones@sri.com>
parents: 100
diff changeset
   438
			Warn.Logf("SASL challenge decode: %s", err)
72
Chris Jones <christian.jones@sri.com>
parents: 66
diff changeset
   439
			return
11
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   440
		}
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   441
		srvMap := parseSasl(string(str))
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   442
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   443
		if cl.saslExpected == "" {
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   444
			cl.saslDigest1(srvMap)
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   445
		} else {
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   446
			cl.saslDigest2(srvMap)
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   447
		}
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   448
	case "failure":
102
872e936f9f3f Reworked the logging again, and also added namespaces to a couple of fields on Features.
Chris Jones <christian.jones@sri.com>
parents: 100
diff changeset
   449
		Info.Logf("SASL authentication failed")
11
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   450
	case "success":
102
872e936f9f3f Reworked the logging again, and also added namespaces to a couple of fields on Features.
Chris Jones <christian.jones@sri.com>
parents: 100
diff changeset
   451
		Info.Logf("Sasl authentication succeeded")
32
4e68d8f89dc3 Make the server's advertised features available to the app.
Chris Jones <chris@cjones.org>
parents: 31
diff changeset
   452
		cl.Features = nil
22
d6b7b4cbf50d Made the stream type non-public.
Chris Jones <chris@cjones.org>
parents: 21
diff changeset
   453
		ss := &stream{To: cl.Jid.Domain, Version: Version}
11
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   454
		cl.xmlOut <- ss
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   455
	}
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   456
}
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   457
72
Chris Jones <christian.jones@sri.com>
parents: 66
diff changeset
   458
func (cl *Client) saslDigest1(srvMap map[string]string) {
11
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   459
	// Make sure it supports qop=auth
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   460
	var hasAuth bool
72
Chris Jones <christian.jones@sri.com>
parents: 66
diff changeset
   461
	for _, qop := range strings.Fields(srvMap["qop"]) {
11
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   462
		if qop == "auth" {
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   463
			hasAuth = true
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   464
		}
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   465
	}
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   466
	if !hasAuth {
102
872e936f9f3f Reworked the logging again, and also added namespaces to a couple of fields on Features.
Chris Jones <christian.jones@sri.com>
parents: 100
diff changeset
   467
		Warn.Logf("Server doesn't support SASL auth")
72
Chris Jones <christian.jones@sri.com>
parents: 66
diff changeset
   468
		return
11
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   469
	}
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   470
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   471
	// Pick a realm.
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   472
	var realm string
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   473
	if srvMap["realm"] != "" {
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   474
		realm = strings.Fields(srvMap["realm"])[0]
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   475
	}
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   476
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   477
	passwd := cl.password
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   478
	nonce := srvMap["nonce"]
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   479
	digestUri := "xmpp/" + cl.Jid.Domain
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   480
	nonceCount := int32(1)
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   481
	nonceCountStr := fmt.Sprintf("%08x", nonceCount)
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   482
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   483
	// Begin building the response. Username is
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   484
	// user@domain or just domain.
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   485
	var username string
25
7437d6eed227 Made JID.Node a string rather than *string. This is more appropriate
Chris Jones <chris@cjones.org>
parents: 23
diff changeset
   486
	if cl.Jid.Node == "" {
11
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   487
		username = cl.Jid.Domain
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   488
	} else {
25
7437d6eed227 Made JID.Node a string rather than *string. This is more appropriate
Chris Jones <chris@cjones.org>
parents: 23
diff changeset
   489
		username = cl.Jid.Node
11
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   490
	}
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   491
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   492
	// Generate our own nonce from random data.
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   493
	randSize := big.NewInt(0)
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   494
	randSize.Lsh(big.NewInt(1), 64)
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   495
	cnonce, err := rand.Int(rand.Reader, randSize)
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   496
	if err != nil {
102
872e936f9f3f Reworked the logging again, and also added namespaces to a couple of fields on Features.
Chris Jones <christian.jones@sri.com>
parents: 100
diff changeset
   497
		Warn.Logf("SASL rand: %s", err)
11
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   498
		return
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   499
	}
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   500
	cnonceStr := fmt.Sprintf("%016x", cnonce)
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   501
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   502
	/* Now encode the actual password response, as well as the
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   503
	 * expected next challenge from the server. */
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   504
	response := saslDigestResponse(username, realm, passwd, nonce,
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   505
		cnonceStr, "AUTHENTICATE", digestUri, nonceCountStr)
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   506
	next := saslDigestResponse(username, realm, passwd, nonce,
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   507
		cnonceStr, "", digestUri, nonceCountStr)
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   508
	cl.saslExpected = next
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   509
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   510
	// Build the map which will be encoded.
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   511
	clMap := make(map[string]string)
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   512
	clMap["realm"] = `"` + realm + `"`
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   513
	clMap["username"] = `"` + username + `"`
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   514
	clMap["nonce"] = `"` + nonce + `"`
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   515
	clMap["cnonce"] = `"` + cnonceStr + `"`
72
Chris Jones <christian.jones@sri.com>
parents: 66
diff changeset
   516
	clMap["nc"] = nonceCountStr
11
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   517
	clMap["qop"] = "auth"
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   518
	clMap["digest-uri"] = `"` + digestUri + `"`
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   519
	clMap["response"] = response
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   520
	if srvMap["charset"] == "utf-8" {
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   521
		clMap["charset"] = "utf-8"
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   522
	}
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   523
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   524
	// Encode the map and send it.
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   525
	clStr := packSasl(clMap)
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   526
	b64 := base64.StdEncoding
72
Chris Jones <christian.jones@sri.com>
parents: 66
diff changeset
   527
	clObj := &auth{XMLName: xml.Name{Space: NsSASL, Local: "response"}, Chardata: b64.EncodeToString([]byte(clStr))}
11
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   528
	cl.xmlOut <- clObj
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   529
}
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   530
72
Chris Jones <christian.jones@sri.com>
parents: 66
diff changeset
   531
func (cl *Client) saslDigest2(srvMap map[string]string) {
11
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   532
	if cl.saslExpected == srvMap["rspauth"] {
72
Chris Jones <christian.jones@sri.com>
parents: 66
diff changeset
   533
		clObj := &auth{XMLName: xml.Name{Space: NsSASL, Local: "response"}}
11
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   534
		cl.xmlOut <- clObj
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   535
	} else {
72
Chris Jones <christian.jones@sri.com>
parents: 66
diff changeset
   536
		clObj := &auth{XMLName: xml.Name{Space: NsSASL, Local: "failure"}, Any: &Generic{XMLName: xml.Name{Space: NsSASL,
Chris Jones <christian.jones@sri.com>
parents: 66
diff changeset
   537
			Local: "abort"}}}
11
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   538
		cl.xmlOut <- clObj
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   539
	}
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   540
}
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   541
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   542
// Takes a string like `key1=value1,key2="value2"...` and returns a
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   543
// key/value map.
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   544
func parseSasl(in string) map[string]string {
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   545
	re := regexp.MustCompile(`([^=]+)="?([^",]+)"?,?`)
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   546
	strs := re.FindAllStringSubmatch(in, -1)
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   547
	m := make(map[string]string)
72
Chris Jones <christian.jones@sri.com>
parents: 66
diff changeset
   548
	for _, pair := range strs {
11
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   549
		key := strings.ToLower(string(pair[1]))
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   550
		value := string(pair[2])
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   551
		m[key] = value
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   552
	}
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   553
	return m
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   554
}
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   555
17
d269d9c0fc8e Code review.
Chris Jones <chris@cjones.org>
parents: 16
diff changeset
   556
// Inverse of parseSasl().
11
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   557
func packSasl(m map[string]string) string {
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   558
	var terms []string
72
Chris Jones <christian.jones@sri.com>
parents: 66
diff changeset
   559
	for key, value := range m {
11
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   560
		if key == "" || value == "" || value == `""` {
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   561
			continue
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   562
		}
72
Chris Jones <christian.jones@sri.com>
parents: 66
diff changeset
   563
		terms = append(terms, key+"="+value)
11
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   564
	}
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   565
	return strings.Join(terms, ",")
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   566
}
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   567
17
d269d9c0fc8e Code review.
Chris Jones <chris@cjones.org>
parents: 16
diff changeset
   568
// Computes the response string for digest authentication.
11
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   569
func saslDigestResponse(username, realm, passwd, nonce, cnonceStr,
72
Chris Jones <christian.jones@sri.com>
parents: 66
diff changeset
   570
authenticate, digestUri, nonceCountStr string) string {
11
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   571
	h := func(text string) []byte {
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   572
		h := md5.New()
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   573
		h.Write([]byte(text))
98
c9cc4eda6dce Updated for Go 1.0 + upcoming XML fixes.
Chris Jones <chris@cjones.org>
parents: 72
diff changeset
   574
		return h.Sum(nil)
11
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   575
	}
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   576
	hex := func(bytes []byte) string {
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   577
		return fmt.Sprintf("%x", bytes)
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   578
	}
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   579
	kd := func(secret, data string) []byte {
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   580
		return h(secret + ":" + data)
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   581
	}
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   582
72
Chris Jones <christian.jones@sri.com>
parents: 66
diff changeset
   583
	a1 := string(h(username+":"+realm+":"+passwd)) + ":" +
11
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   584
		nonce + ":" + cnonceStr
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   585
	a2 := authenticate + ":" + digestUri
72
Chris Jones <christian.jones@sri.com>
parents: 66
diff changeset
   586
	response := hex(kd(hex(h(a1)), nonce+":"+
Chris Jones <christian.jones@sri.com>
parents: 66
diff changeset
   587
		nonceCountStr+":"+cnonceStr+":auth:"+
11
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   588
		hex(h(a2))))
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   589
	return response
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   590
}
12
122ab6208c3c Added resource binding and structures for <iq>, <message>, and <presence>.
Chris Jones <chris@cjones.org>
parents: 11
diff changeset
   591
17
d269d9c0fc8e Code review.
Chris Jones <chris@cjones.org>
parents: 16
diff changeset
   592
// Send a request to bind a resource. RFC 3920, section 7.
41
c8c9e6a7e6c9 Made a special-purpose bind structure for resource binding.
Chris Jones <chris@cjones.org>
parents: 40
diff changeset
   593
func (cl *Client) bind(bindAdv *bindIq) {
12
122ab6208c3c Added resource binding and structures for <iq>, <message>, and <presence>.
Chris Jones <chris@cjones.org>
parents: 11
diff changeset
   594
	res := cl.Jid.Resource
41
c8c9e6a7e6c9 Made a special-purpose bind structure for resource binding.
Chris Jones <chris@cjones.org>
parents: 40
diff changeset
   595
	bindReq := &bindIq{}
12
122ab6208c3c Added resource binding and structures for <iq>, <message>, and <presence>.
Chris Jones <chris@cjones.org>
parents: 11
diff changeset
   596
	if res != "" {
41
c8c9e6a7e6c9 Made a special-purpose bind structure for resource binding.
Chris Jones <chris@cjones.org>
parents: 40
diff changeset
   597
		bindReq.Resource = &res
12
122ab6208c3c Added resource binding and structures for <iq>, <message>, and <presence>.
Chris Jones <chris@cjones.org>
parents: 11
diff changeset
   598
	}
72
Chris Jones <christian.jones@sri.com>
parents: 66
diff changeset
   599
	msg := &Iq{Type: "set", Id: <-Id, Nested: []interface{}{bindReq}}
15
aa2cf77f0ed3 When the server sends us our newly bound resource, update Client.Jid
Chris Jones <chris@cjones.org>
parents: 14
diff changeset
   600
	f := func(st Stanza) bool {
98
c9cc4eda6dce Updated for Go 1.0 + upcoming XML fixes.
Chris Jones <chris@cjones.org>
parents: 72
diff changeset
   601
		iq, ok := st.(*Iq)
c9cc4eda6dce Updated for Go 1.0 + upcoming XML fixes.
Chris Jones <chris@cjones.org>
parents: 72
diff changeset
   602
		if !ok {
102
872e936f9f3f Reworked the logging again, and also added namespaces to a couple of fields on Features.
Chris Jones <christian.jones@sri.com>
parents: 100
diff changeset
   603
			Warn.Logf("non-iq response")
98
c9cc4eda6dce Updated for Go 1.0 + upcoming XML fixes.
Chris Jones <chris@cjones.org>
parents: 72
diff changeset
   604
		}
c9cc4eda6dce Updated for Go 1.0 + upcoming XML fixes.
Chris Jones <chris@cjones.org>
parents: 72
diff changeset
   605
		if iq.Type == "error" {
102
872e936f9f3f Reworked the logging again, and also added namespaces to a couple of fields on Features.
Chris Jones <christian.jones@sri.com>
parents: 100
diff changeset
   606
			Warn.Logf("Resource binding failed")
15
aa2cf77f0ed3 When the server sends us our newly bound resource, update Client.Jid
Chris Jones <chris@cjones.org>
parents: 14
diff changeset
   607
			return false
aa2cf77f0ed3 When the server sends us our newly bound resource, update Client.Jid
Chris Jones <chris@cjones.org>
parents: 14
diff changeset
   608
		}
61
16513974d273 Stanzas can now contain multiple nested (extended) elements.
Chris Jones <chris@cjones.org>
parents: 59
diff changeset
   609
		var bindRepl *bindIq
98
c9cc4eda6dce Updated for Go 1.0 + upcoming XML fixes.
Chris Jones <chris@cjones.org>
parents: 72
diff changeset
   610
		for _, ele := range iq.Nested {
72
Chris Jones <christian.jones@sri.com>
parents: 66
diff changeset
   611
			if b, ok := ele.(*bindIq); ok {
61
16513974d273 Stanzas can now contain multiple nested (extended) elements.
Chris Jones <chris@cjones.org>
parents: 59
diff changeset
   612
				bindRepl = b
16513974d273 Stanzas can now contain multiple nested (extended) elements.
Chris Jones <chris@cjones.org>
parents: 59
diff changeset
   613
				break
16513974d273 Stanzas can now contain multiple nested (extended) elements.
Chris Jones <chris@cjones.org>
parents: 59
diff changeset
   614
			}
16513974d273 Stanzas can now contain multiple nested (extended) elements.
Chris Jones <chris@cjones.org>
parents: 59
diff changeset
   615
		}
16513974d273 Stanzas can now contain multiple nested (extended) elements.
Chris Jones <chris@cjones.org>
parents: 59
diff changeset
   616
		if bindRepl == nil {
102
872e936f9f3f Reworked the logging again, and also added namespaces to a couple of fields on Features.
Chris Jones <christian.jones@sri.com>
parents: 100
diff changeset
   617
			Warn.Logf("Bad bind reply: %v", iq)
15
aa2cf77f0ed3 When the server sends us our newly bound resource, update Client.Jid
Chris Jones <chris@cjones.org>
parents: 14
diff changeset
   618
			return false
aa2cf77f0ed3 When the server sends us our newly bound resource, update Client.Jid
Chris Jones <chris@cjones.org>
parents: 14
diff changeset
   619
		}
41
c8c9e6a7e6c9 Made a special-purpose bind structure for resource binding.
Chris Jones <chris@cjones.org>
parents: 40
diff changeset
   620
		jidStr := bindRepl.Jid
c8c9e6a7e6c9 Made a special-purpose bind structure for resource binding.
Chris Jones <chris@cjones.org>
parents: 40
diff changeset
   621
		if jidStr == nil || *jidStr == "" {
102
872e936f9f3f Reworked the logging again, and also added namespaces to a couple of fields on Features.
Chris Jones <christian.jones@sri.com>
parents: 100
diff changeset
   622
			Warn.Logf("Can't bind empty resource")
15
aa2cf77f0ed3 When the server sends us our newly bound resource, update Client.Jid
Chris Jones <chris@cjones.org>
parents: 14
diff changeset
   623
			return false
aa2cf77f0ed3 When the server sends us our newly bound resource, update Client.Jid
Chris Jones <chris@cjones.org>
parents: 14
diff changeset
   624
		}
aa2cf77f0ed3 When the server sends us our newly bound resource, update Client.Jid
Chris Jones <chris@cjones.org>
parents: 14
diff changeset
   625
		jid := new(JID)
98
c9cc4eda6dce Updated for Go 1.0 + upcoming XML fixes.
Chris Jones <chris@cjones.org>
parents: 72
diff changeset
   626
		if err := jid.Set(*jidStr); err != nil {
102
872e936f9f3f Reworked the logging again, and also added namespaces to a couple of fields on Features.
Chris Jones <christian.jones@sri.com>
parents: 100
diff changeset
   627
			Warn.Logf("Can't parse JID %s: %s", *jidStr, err)
15
aa2cf77f0ed3 When the server sends us our newly bound resource, update Client.Jid
Chris Jones <chris@cjones.org>
parents: 14
diff changeset
   628
			return false
aa2cf77f0ed3 When the server sends us our newly bound resource, update Client.Jid
Chris Jones <chris@cjones.org>
parents: 14
diff changeset
   629
		}
aa2cf77f0ed3 When the server sends us our newly bound resource, update Client.Jid
Chris Jones <chris@cjones.org>
parents: 14
diff changeset
   630
		cl.Jid = *jid
102
872e936f9f3f Reworked the logging again, and also added namespaces to a couple of fields on Features.
Chris Jones <christian.jones@sri.com>
parents: 100
diff changeset
   631
		Info.Logf("Bound resource: %s", cl.Jid.String())
29
a456133ed0ac Don't accept data on Client.Out until resource binding is
Chris Jones <chris@cjones.org>
parents: 26
diff changeset
   632
		cl.bindDone()
a456133ed0ac Don't accept data on Client.Out until resource binding is
Chris Jones <chris@cjones.org>
parents: 26
diff changeset
   633
		return false
15
aa2cf77f0ed3 When the server sends us our newly bound resource, update Client.Jid
Chris Jones <chris@cjones.org>
parents: 14
diff changeset
   634
	}
aa2cf77f0ed3 When the server sends us our newly bound resource, update Client.Jid
Chris Jones <chris@cjones.org>
parents: 14
diff changeset
   635
	cl.HandleStanza(msg.Id, f)
12
122ab6208c3c Added resource binding and structures for <iq>, <message>, and <presence>.
Chris Jones <chris@cjones.org>
parents: 11
diff changeset
   636
	cl.xmlOut <- msg
122ab6208c3c Added resource binding and structures for <iq>, <message>, and <presence>.
Chris Jones <chris@cjones.org>
parents: 11
diff changeset
   637
}
13
c9527bbe99a6 Added a callback handler which will handle a stanza with a particular id.
Chris Jones <chris@cjones.org>
parents: 12
diff changeset
   638
17
d269d9c0fc8e Code review.
Chris Jones <chris@cjones.org>
parents: 16
diff changeset
   639
// Register a callback to handle the next XMPP stanza (iq, message, or
d269d9c0fc8e Code review.
Chris Jones <chris@cjones.org>
parents: 16
diff changeset
   640
// presence) with a given id. The provided function will not be called
d269d9c0fc8e Code review.
Chris Jones <chris@cjones.org>
parents: 16
diff changeset
   641
// more than once. If it returns false, the stanza will not be made
33
571713f49494 Added roster retrieval to StartSession().
Chris Jones <chris@cjones.org>
parents: 32
diff changeset
   642
// available on the normal Client.In channel. The stanza handler
571713f49494 Added roster retrieval to StartSession().
Chris Jones <chris@cjones.org>
parents: 32
diff changeset
   643
// must not read from that channel, as deliveries on it cannot proceed
571713f49494 Added roster retrieval to StartSession().
Chris Jones <chris@cjones.org>
parents: 32
diff changeset
   644
// until the handler returns true or false.
13
c9527bbe99a6 Added a callback handler which will handle a stanza with a particular id.
Chris Jones <chris@cjones.org>
parents: 12
diff changeset
   645
func (cl *Client) HandleStanza(id string, f func(Stanza) bool) {
c9527bbe99a6 Added a callback handler which will handle a stanza with a particular id.
Chris Jones <chris@cjones.org>
parents: 12
diff changeset
   646
	h := &stanzaHandler{id: id, f: f}
c9527bbe99a6 Added a callback handler which will handle a stanza with a particular id.
Chris Jones <chris@cjones.org>
parents: 12
diff changeset
   647
	cl.handlers <- h
c9527bbe99a6 Added a callback handler which will handle a stanza with a particular id.
Chris Jones <chris@cjones.org>
parents: 12
diff changeset
   648
}