stream.go
author Chris Jones <chris@cjones.org>
Wed, 28 Dec 2011 11:57:29 -0700
changeset 16 b839e37b3f29
parent 15 aa2cf77f0ed3
child 17 d269d9c0fc8e
permissions -rw-r--r--
Parse <presence> and <message> stanzas.
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
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
     8
// we respond to XMPP events on behalf of the library client).
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
     9
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    10
package xmpp
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    11
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    12
import (
11
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
    13
	"big"
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"
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
    18
	"fmt"
10
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    19
	"io"
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    20
	"log"
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    21
	"net"
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    22
	"os"
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
	"xml"
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    27
)
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    28
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
c9527bbe99a6 Added a callback handler which will handle a stanza with a particular id.
Chris Jones <chris@cjones.org>
parents: 12
diff changeset
    31
	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
    32
}
c9527bbe99a6 Added a callback handler which will handle a stanza with a particular id.
Chris Jones <chris@cjones.org>
parents: 12
diff changeset
    33
10
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    34
func (cl *Client) readTransport(w io.Writer) {
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    35
	defer tryClose(cl.socket, w)
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    36
	cl.socket.SetReadTimeout(1e8)
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    37
	p := make([]byte, 1024)
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    38
	for {
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    39
		if cl.socket == nil {
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    40
			cl.waitForSocket()
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    41
		}
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    42
		nr, err := cl.socket.Read(p)
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    43
		if nr == 0 {
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    44
			if errno, ok := err.(*net.OpError) ; ok {
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    45
				if errno.Timeout() {
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    46
					continue
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    47
				}
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    48
			}
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    49
			log.Printf("read: %s", err.String())
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    50
			break
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
		nw, err := w.Write(p[:nr])
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    53
		if nw < nr {
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    54
			log.Println("read: %s", err.String())
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    55
			break
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    56
		}
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    57
	}
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    58
}
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    59
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    60
func (cl *Client) writeTransport(r io.Reader) {
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    61
	defer tryClose(r, cl.socket)
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    62
	p := make([]byte, 1024)
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    63
	for {
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    64
		nr, err := r.Read(p)
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    65
		if nr == 0 {
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    66
			log.Printf("write: %s", err.String())
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    67
			break
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    68
		}
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    69
		nw, err := cl.socket.Write(p[:nr])
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    70
		if nw < nr {
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    71
			log.Println("write: %s", err.String())
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    72
			break
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    73
		}
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    74
	}
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    75
}
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    76
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    77
func readXml(r io.Reader, ch chan<- interface{}) {
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    78
	if debug {
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    79
		pr, pw := io.Pipe()
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    80
		go tee(r, pw, "S: ")
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    81
		r = pr
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    82
	}
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    83
	defer tryClose(r, ch)
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    84
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    85
	p := xml.NewParser(r)
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    86
	for {
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    87
		// Sniff the next token on the stream.
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    88
		t, err := p.Token()
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    89
		if t == nil {
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    90
			if err != os.EOF {
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    91
				log.Printf("read: %v", err)
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    92
			}
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    93
			break
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    94
		}
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    95
		var se xml.StartElement
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    96
		var ok bool
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    97
		if se, ok = t.(xml.StartElement) ; !ok {
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    98
			continue
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    99
		}
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
		// Allocate the appropriate structure for this token.
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   102
		var obj interface{}
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   103
		switch se.Name.Space + " " + se.Name.Local {
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   104
		case nsStream + " stream":
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   105
			st, err := parseStream(se)
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   106
			if err != nil {
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   107
				log.Printf("unmarshal stream: %v",
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   108
					err)
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   109
				break
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   110
			}
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   111
			ch <- st
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   112
			continue
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   113
		case "stream error", nsStream + " error":
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   114
			obj = &StreamError{}
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   115
		case nsStream + " features":
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   116
			obj = &Features{}
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   117
		case nsTLS + " proceed", nsTLS + " failure":
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   118
			obj = &starttls{}
11
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   119
		case nsSASL + " challenge", nsSASL + " failure",
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   120
			nsSASL + " success":
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   121
			obj = &auth{}
12
122ab6208c3c Added resource binding and structures for <iq>, <message>, and <presence>.
Chris Jones <chris@cjones.org>
parents: 11
diff changeset
   122
		case "jabber:client iq":
122ab6208c3c Added resource binding and structures for <iq>, <message>, and <presence>.
Chris Jones <chris@cjones.org>
parents: 11
diff changeset
   123
			obj = &Iq{}
16
b839e37b3f29 Parse <presence> and <message> stanzas.
Chris Jones <chris@cjones.org>
parents: 15
diff changeset
   124
		case "jabber:client message":
b839e37b3f29 Parse <presence> and <message> stanzas.
Chris Jones <chris@cjones.org>
parents: 15
diff changeset
   125
			obj = &Message{}
b839e37b3f29 Parse <presence> and <message> stanzas.
Chris Jones <chris@cjones.org>
parents: 15
diff changeset
   126
		case "jabber:client presence":
b839e37b3f29 Parse <presence> and <message> stanzas.
Chris Jones <chris@cjones.org>
parents: 15
diff changeset
   127
			obj = &Presence{}
10
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   128
		default:
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   129
			obj = &Unrecognized{}
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   130
			log.Printf("Ignoring unrecognized: %s %s\n",
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   131
				se.Name.Space, se.Name.Local)
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   132
		}
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   133
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   134
		// Read the complete XML stanza.
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   135
		err = p.Unmarshal(obj, &se)
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   136
		if err != nil {
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   137
			log.Printf("unmarshal: %v", err)
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   138
			break
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
		// Put it on the channel.
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   142
		ch <- obj
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   143
	}
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   144
}
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   145
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   146
func writeXml(w io.Writer, ch <-chan interface{}) {
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   147
	if debug {
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   148
		pr, pw := io.Pipe()
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   149
		go tee(pr, w, "C: ")
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   150
		w = pw
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   151
	}
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   152
	defer tryClose(w, ch)
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   153
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   154
	for obj := range ch {
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   155
		err := xml.Marshal(w, obj)
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   156
		if err != nil {
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   157
			log.Printf("write: %v", err)
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   158
			break
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   159
		}
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   160
	}
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
func writeText(w io.Writer, ch <-chan *string) {
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   164
	if debug {
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   165
		pr, pw := io.Pipe()
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   166
		go tee(pr, w, "C: ")
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   167
		w = pw
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   168
	}
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   169
	defer tryClose(w, ch)
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   170
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   171
	for str := range ch {
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   172
		_, err := w.Write([]byte(*str))
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   173
		if err != nil {
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   174
			log.Printf("writeStr: %v", err)
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   175
			break
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   176
		}
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   177
	}
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   178
}
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   179
11
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   180
func (cl *Client) readStream(srvIn <-chan interface{}, cliOut chan<- interface{}) {
10
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   181
	defer tryClose(srvIn, cliOut)
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   182
13
c9527bbe99a6 Added a callback handler which will handle a stanza with a particular id.
Chris Jones <chris@cjones.org>
parents: 12
diff changeset
   183
	handlers := make(map[string] 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
   184
	// TODO This for loop will never terminate, even when the
c9527bbe99a6 Added a callback handler which will handle a stanza with a particular id.
Chris Jones <chris@cjones.org>
parents: 12
diff changeset
   185
	// channels are closed.
c9527bbe99a6 Added a callback handler which will handle a stanza with a particular id.
Chris Jones <chris@cjones.org>
parents: 12
diff changeset
   186
	for {
c9527bbe99a6 Added a callback handler which will handle a stanza with a particular id.
Chris Jones <chris@cjones.org>
parents: 12
diff changeset
   187
		select {
c9527bbe99a6 Added a callback handler which will handle a stanza with a particular id.
Chris Jones <chris@cjones.org>
parents: 12
diff changeset
   188
		case h := <- cl.handlers:
c9527bbe99a6 Added a callback handler which will handle a stanza with a particular id.
Chris Jones <chris@cjones.org>
parents: 12
diff changeset
   189
			handlers[h.id] = h.f
c9527bbe99a6 Added a callback handler which will handle a stanza with a particular id.
Chris Jones <chris@cjones.org>
parents: 12
diff changeset
   190
		case x := <- srvIn:
c9527bbe99a6 Added a callback handler which will handle a stanza with a particular id.
Chris Jones <chris@cjones.org>
parents: 12
diff changeset
   191
			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
   192
			switch obj := x.(type) {
c9527bbe99a6 Added a callback handler which will handle a stanza with a particular id.
Chris Jones <chris@cjones.org>
parents: 12
diff changeset
   193
			case *Stream:
c9527bbe99a6 Added a callback handler which will handle a stanza with a particular id.
Chris Jones <chris@cjones.org>
parents: 12
diff changeset
   194
				handleStream(obj)
c9527bbe99a6 Added a callback handler which will handle a stanza with a particular id.
Chris Jones <chris@cjones.org>
parents: 12
diff changeset
   195
			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
   196
				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
   197
			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
   198
				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
   199
			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
   200
				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
   201
			default:
c9527bbe99a6 Added a callback handler which will handle a stanza with a particular id.
Chris Jones <chris@cjones.org>
parents: 12
diff changeset
   202
				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
   203
			}
c9527bbe99a6 Added a callback handler which will handle a stanza with a particular id.
Chris Jones <chris@cjones.org>
parents: 12
diff changeset
   204
			if st, ok := x.(Stanza) ; ok &&
c9527bbe99a6 Added a callback handler which will handle a stanza with a particular id.
Chris Jones <chris@cjones.org>
parents: 12
diff changeset
   205
				handlers[st.XId()] != nil {
c9527bbe99a6 Added a callback handler which will handle a stanza with a particular id.
Chris Jones <chris@cjones.org>
parents: 12
diff changeset
   206
				f := handlers[st.XId()]
14
fd6781a41e6f Don't forget to remove the stanza handler after it's been used.
Chris Jones <chris@cjones.org>
parents: 13
diff changeset
   207
				handlers[st.XId()] = nil
13
c9527bbe99a6 Added a callback handler which will handle a stanza with a particular id.
Chris Jones <chris@cjones.org>
parents: 12
diff changeset
   208
				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
   209
			}
c9527bbe99a6 Added a callback handler which will handle a stanza with a particular id.
Chris Jones <chris@cjones.org>
parents: 12
diff changeset
   210
			if send {
c9527bbe99a6 Added a callback handler which will handle a stanza with a particular id.
Chris Jones <chris@cjones.org>
parents: 12
diff changeset
   211
				cliOut <- x
c9527bbe99a6 Added a callback handler which will handle a stanza with a particular id.
Chris Jones <chris@cjones.org>
parents: 12
diff changeset
   212
			}
10
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   213
		}
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   214
	}
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   215
}
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   216
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   217
func writeStream(srvOut chan<- interface{}, cliIn <-chan interface{}) {
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   218
	defer tryClose(srvOut, cliIn)
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   219
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   220
	for x := range cliIn {
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   221
		srvOut <- x
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   222
	}
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   223
}
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
func handleStream(ss *Stream) {
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
11
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   228
func (cl *Client) handleFeatures(fe *Features) {
10
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   229
	if fe.Starttls != nil {
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   230
		start := &starttls{XMLName: xml.Name{Space: nsTLS,
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   231
			Local: "starttls"}}
11
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   232
		cl.xmlOut <- start
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   233
		return
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   234
	}
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   235
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   236
	if len(fe.Mechanisms.Mechanism) > 0 {
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   237
		cl.chooseSasl(fe)
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   238
		return
10
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   239
	}
12
122ab6208c3c Added resource binding and structures for <iq>, <message>, and <presence>.
Chris Jones <chris@cjones.org>
parents: 11
diff changeset
   240
122ab6208c3c Added resource binding and structures for <iq>, <message>, and <presence>.
Chris Jones <chris@cjones.org>
parents: 11
diff changeset
   241
	if fe.Bind != nil {
122ab6208c3c Added resource binding and structures for <iq>, <message>, and <presence>.
Chris Jones <chris@cjones.org>
parents: 11
diff changeset
   242
		cl.bind(fe.Bind)
122ab6208c3c Added resource binding and structures for <iq>, <message>, and <presence>.
Chris Jones <chris@cjones.org>
parents: 11
diff changeset
   243
	}
10
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   244
}
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   245
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   246
// readTransport() is running concurrently. We need to stop it,
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   247
// negotiate TLS, then start it again. It calls waitForSocket() in
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   248
// its inner loop; see below.
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   249
func (cl *Client) handleTls(t *starttls) {
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   250
	tcp := cl.socket
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   251
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   252
	// Set the socket to nil, and wait for the reader routine to
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   253
	// signal that it's paused.
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   254
	cl.socket = nil
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   255
	cl.socketSync.Add(1)
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   256
	cl.socketSync.Wait()
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   257
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   258
	// Negotiate TLS with the server.
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   259
	tls := tls.Client(tcp, nil)
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   260
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   261
	// Make the TLS connection available to the reader, and wait
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   262
	// for it to signal that it's working again.
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   263
	cl.socketSync.Add(1)
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   264
	cl.socket = tls
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   265
	cl.socketSync.Wait()
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   266
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   267
	// Reset the read timeout on the (underlying) socket so the
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   268
	// reader doesn't get woken up unnecessarily.
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   269
	tcp.SetReadTimeout(0)
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   270
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   271
	log.Println("TLS negotiation succeeded.")
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
	// Now re-send the initial handshake message to start the new
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   274
	// session.
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   275
	hsOut := &Stream{To: cl.Jid.Domain, Version: Version}
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   276
	cl.xmlOut <- hsOut
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   277
}
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   278
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   279
// Synchronize with handleTls(). Called from readTransport() when
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   280
// cl.socket is nil.
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   281
func (cl *Client) waitForSocket() {
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   282
	// Signal that we've stopped reading from the socket.
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   283
	cl.socketSync.Done()
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   284
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   285
	// Wait until the socket is available again.
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   286
	for cl.socket == nil {
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   287
		time.Sleep(1e8)
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   288
	}
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   289
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   290
	// Signal that we're going back to the read loop.
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   291
	cl.socketSync.Done()
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   292
}
11
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   293
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   294
func (cl *Client) chooseSasl(fe *Features) {
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   295
	var digestMd5 bool
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   296
	for _, m := range(fe.Mechanisms.Mechanism) {
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   297
		switch strings.ToLower(m) {
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   298
		case "digest-md5":
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   299
			digestMd5 = true
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   300
		}
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   301
	}
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   302
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   303
	if digestMd5 {
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   304
		auth := &auth{XMLName: xml.Name{Space: nsSASL, Local:
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   305
				"auth"}, Mechanism: "DIGEST-MD5"}
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   306
		cl.xmlOut <- auth
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   307
	}
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   308
}
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   309
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   310
func (cl *Client) handleSasl(srv *auth) {
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   311
	switch strings.ToLower(srv.XMLName.Local) {
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   312
	case "challenge":
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   313
		b64 := base64.StdEncoding
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   314
		str, err := b64.DecodeString(srv.Chardata)
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   315
		if err != nil {
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   316
			log.Printf("SASL challenge decode: %s",
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   317
				err.String())
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   318
			return;
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   319
		}
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   320
		srvMap := parseSasl(string(str))
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   321
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   322
		if cl.saslExpected == "" {
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   323
			cl.saslDigest1(srvMap)
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   324
		} else {
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   325
			cl.saslDigest2(srvMap)
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   326
		}
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   327
	case "failure":
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   328
		log.Println("SASL authentication failed")
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   329
	case "success":
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   330
		log.Println("SASL authentication succeeded")
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   331
		ss := &Stream{To: cl.Jid.Domain, Version: Version}
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   332
		cl.xmlOut <- ss
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   333
	}
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   334
}
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   335
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   336
func (cl *Client) saslDigest1(srvMap map[string] string) {
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   337
	// Make sure it supports qop=auth
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   338
	var hasAuth bool
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   339
	for _, qop := range(strings.Fields(srvMap["qop"])) {
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   340
		if qop == "auth" {
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   341
			hasAuth = true
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   342
		}
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   343
	}
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   344
	if !hasAuth {
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   345
		log.Println("Server doesn't support SASL auth")
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   346
		return;
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   347
	}
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   348
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   349
	// Pick a realm.
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   350
	var realm string
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   351
	if srvMap["realm"] != "" {
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   352
		realm = strings.Fields(srvMap["realm"])[0]
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   353
	}
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   354
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   355
	passwd := cl.password
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   356
	nonce := srvMap["nonce"]
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   357
	digestUri := "xmpp/" + cl.Jid.Domain
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   358
	nonceCount := int32(1)
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   359
	nonceCountStr := fmt.Sprintf("%08x", nonceCount)
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   360
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   361
	// Begin building the response. Username is
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   362
	// user@domain or just domain.
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   363
	var username string
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   364
	if cl.Jid.Node == nil {
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   365
		username = cl.Jid.Domain
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   366
	} else {
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   367
		username = *cl.Jid.Node
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   368
	}
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   369
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   370
	// Generate our own nonce from random data.
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   371
	randSize := big.NewInt(0)
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   372
	randSize.Lsh(big.NewInt(1), 64)
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   373
	cnonce, err := rand.Int(rand.Reader, randSize)
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   374
	if err != nil {
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   375
		log.Println("SASL rand: %s", err.String())
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   376
		return
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   377
	}
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   378
	cnonceStr := fmt.Sprintf("%016x", cnonce)
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   379
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   380
	/* Now encode the actual password response, as well as the
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   381
	 * expected next challenge from the server. */
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   382
	response := saslDigestResponse(username, realm, passwd, nonce,
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   383
		cnonceStr, "AUTHENTICATE", digestUri, nonceCountStr)
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   384
	next := saslDigestResponse(username, realm, passwd, nonce,
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   385
		cnonceStr, "", digestUri, nonceCountStr)
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   386
	cl.saslExpected = next
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   387
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   388
	// Build the map which will be encoded.
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   389
	clMap := make(map[string]string)
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   390
	clMap["realm"] = `"` + realm + `"`
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   391
	clMap["username"] = `"` + username + `"`
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   392
	clMap["nonce"] = `"` + nonce + `"`
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   393
	clMap["cnonce"] = `"` + cnonceStr + `"`
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   394
	clMap["nc"] =  nonceCountStr
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   395
	clMap["qop"] = "auth"
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   396
	clMap["digest-uri"] = `"` + digestUri + `"`
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   397
	clMap["response"] = response
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   398
	if srvMap["charset"] == "utf-8" {
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   399
		clMap["charset"] = "utf-8"
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   400
	}
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   401
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   402
	// Encode the map and send it.
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   403
	clStr := packSasl(clMap)
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   404
	b64 := base64.StdEncoding
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   405
	clObj := &auth{XMLName: xml.Name{Space: nsSASL, Local:
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   406
			"response"}, Chardata:
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   407
		b64.EncodeToString([]byte(clStr))}
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   408
	cl.xmlOut <- clObj
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   409
}
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   410
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   411
func (cl *Client) saslDigest2(srvMap map[string] string) {
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   412
	if cl.saslExpected == srvMap["rspauth"] {
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   413
		clObj := &auth{XMLName: xml.Name{Space: nsSASL, Local:
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   414
				"response"}}
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   415
		cl.xmlOut <- clObj
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   416
	} else {
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   417
		clObj := &auth{XMLName: xml.Name{Space: nsSASL, Local:
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   418
				"failure"}, Any:
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   419
			&Unrecognized{XMLName: xml.Name{Space: nsSASL,
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   420
				Local: "abort"}}}
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   421
		cl.xmlOut <- clObj
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   422
	}
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
// 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
   426
// key/value map.
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   427
func parseSasl(in string) map[string]string {
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   428
	re := regexp.MustCompile(`([^=]+)="?([^",]+)"?,?`)
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   429
	strs := re.FindAllStringSubmatch(in, -1)
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   430
	m := make(map[string]string)
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   431
	for _, pair := range(strs) {
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   432
		key := strings.ToLower(string(pair[1]))
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   433
		value := string(pair[2])
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   434
		m[key] = value
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   435
	}
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   436
	return m
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   437
}
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   438
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   439
func packSasl(m map[string]string) string {
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   440
	var terms []string
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   441
	for key, value := range(m) {
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   442
		if key == "" || value == "" || value == `""` {
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   443
			continue
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   444
		}
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   445
		terms = append(terms, key + "=" + value)
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   446
	}
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   447
	return strings.Join(terms, ",")
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   448
}
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   449
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   450
func saslDigestResponse(username, realm, passwd, nonce, cnonceStr,
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   451
	authenticate, digestUri, nonceCountStr string) string {
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   452
	h := func(text string) []byte {
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   453
		h := md5.New()
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   454
		h.Write([]byte(text))
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   455
		return h.Sum()
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
	hex := func(bytes []byte) string {
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   458
		return fmt.Sprintf("%x", bytes)
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   459
	}
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   460
	kd := func(secret, data string) []byte {
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   461
		return h(secret + ":" + data)
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   462
	}
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   463
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   464
	a1 := string(h(username + ":" + realm + ":" + passwd)) + ":" +
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   465
		nonce + ":" + cnonceStr
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   466
	a2 := authenticate + ":" + digestUri
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   467
	response := hex(kd(hex(h(a1)), nonce + ":" +
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   468
		nonceCountStr + ":" + cnonceStr + ":auth:" +
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   469
		hex(h(a2))))
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   470
	return response
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   471
}
12
122ab6208c3c Added resource binding and structures for <iq>, <message>, and <presence>.
Chris Jones <chris@cjones.org>
parents: 11
diff changeset
   472
122ab6208c3c Added resource binding and structures for <iq>, <message>, and <presence>.
Chris Jones <chris@cjones.org>
parents: 11
diff changeset
   473
func (cl *Client) bind(bind *Unrecognized) {
122ab6208c3c Added resource binding and structures for <iq>, <message>, and <presence>.
Chris Jones <chris@cjones.org>
parents: 11
diff changeset
   474
	res := cl.Jid.Resource
122ab6208c3c Added resource binding and structures for <iq>, <message>, and <presence>.
Chris Jones <chris@cjones.org>
parents: 11
diff changeset
   475
	msg := &Iq{Type: "set", Id: cl.NextId(), Any:
122ab6208c3c Added resource binding and structures for <iq>, <message>, and <presence>.
Chris Jones <chris@cjones.org>
parents: 11
diff changeset
   476
		&Unrecognized{XMLName: xml.Name{Space: nsBind, Local:
122ab6208c3c Added resource binding and structures for <iq>, <message>, and <presence>.
Chris Jones <chris@cjones.org>
parents: 11
diff changeset
   477
					"bind"}}}
122ab6208c3c Added resource binding and structures for <iq>, <message>, and <presence>.
Chris Jones <chris@cjones.org>
parents: 11
diff changeset
   478
	if res != "" {
122ab6208c3c Added resource binding and structures for <iq>, <message>, and <presence>.
Chris Jones <chris@cjones.org>
parents: 11
diff changeset
   479
		msg.Any.Any = &Unrecognized{XMLName: xml.Name{Local:
122ab6208c3c Added resource binding and structures for <iq>, <message>, and <presence>.
Chris Jones <chris@cjones.org>
parents: 11
diff changeset
   480
				"resource"}, Chardata: res}
122ab6208c3c Added resource binding and structures for <iq>, <message>, and <presence>.
Chris Jones <chris@cjones.org>
parents: 11
diff changeset
   481
	}
15
aa2cf77f0ed3 When the server sends us our newly bound resource, update Client.Jid
Chris Jones <chris@cjones.org>
parents: 14
diff changeset
   482
	f := func(st Stanza) bool {
aa2cf77f0ed3 When the server sends us our newly bound resource, update Client.Jid
Chris Jones <chris@cjones.org>
parents: 14
diff changeset
   483
		if st.XType() == "error" {
aa2cf77f0ed3 When the server sends us our newly bound resource, update Client.Jid
Chris Jones <chris@cjones.org>
parents: 14
diff changeset
   484
			log.Println("Resource binding failed")
aa2cf77f0ed3 When the server sends us our newly bound resource, update Client.Jid
Chris Jones <chris@cjones.org>
parents: 14
diff changeset
   485
			return false
aa2cf77f0ed3 When the server sends us our newly bound resource, update Client.Jid
Chris Jones <chris@cjones.org>
parents: 14
diff changeset
   486
		}
aa2cf77f0ed3 When the server sends us our newly bound resource, update Client.Jid
Chris Jones <chris@cjones.org>
parents: 14
diff changeset
   487
		bind := st.XChild()
aa2cf77f0ed3 When the server sends us our newly bound resource, update Client.Jid
Chris Jones <chris@cjones.org>
parents: 14
diff changeset
   488
		if bind == nil {
aa2cf77f0ed3 When the server sends us our newly bound resource, update Client.Jid
Chris Jones <chris@cjones.org>
parents: 14
diff changeset
   489
			log.Println("nil resource bind")
aa2cf77f0ed3 When the server sends us our newly bound resource, update Client.Jid
Chris Jones <chris@cjones.org>
parents: 14
diff changeset
   490
			return false
aa2cf77f0ed3 When the server sends us our newly bound resource, update Client.Jid
Chris Jones <chris@cjones.org>
parents: 14
diff changeset
   491
		}
aa2cf77f0ed3 When the server sends us our newly bound resource, update Client.Jid
Chris Jones <chris@cjones.org>
parents: 14
diff changeset
   492
		jidEle := bind.Any
aa2cf77f0ed3 When the server sends us our newly bound resource, update Client.Jid
Chris Jones <chris@cjones.org>
parents: 14
diff changeset
   493
		if jidEle == nil {
aa2cf77f0ed3 When the server sends us our newly bound resource, update Client.Jid
Chris Jones <chris@cjones.org>
parents: 14
diff changeset
   494
			log.Println("nil resource")
aa2cf77f0ed3 When the server sends us our newly bound resource, update Client.Jid
Chris Jones <chris@cjones.org>
parents: 14
diff changeset
   495
			return false
aa2cf77f0ed3 When the server sends us our newly bound resource, update Client.Jid
Chris Jones <chris@cjones.org>
parents: 14
diff changeset
   496
		}
aa2cf77f0ed3 When the server sends us our newly bound resource, update Client.Jid
Chris Jones <chris@cjones.org>
parents: 14
diff changeset
   497
		jidStr := jidEle.Chardata
aa2cf77f0ed3 When the server sends us our newly bound resource, update Client.Jid
Chris Jones <chris@cjones.org>
parents: 14
diff changeset
   498
		if jidStr == "" {
aa2cf77f0ed3 When the server sends us our newly bound resource, update Client.Jid
Chris Jones <chris@cjones.org>
parents: 14
diff changeset
   499
			log.Println("empty resource")
aa2cf77f0ed3 When the server sends us our newly bound resource, update Client.Jid
Chris Jones <chris@cjones.org>
parents: 14
diff changeset
   500
			return false
aa2cf77f0ed3 When the server sends us our newly bound resource, update Client.Jid
Chris Jones <chris@cjones.org>
parents: 14
diff changeset
   501
		}
aa2cf77f0ed3 When the server sends us our newly bound resource, update Client.Jid
Chris Jones <chris@cjones.org>
parents: 14
diff changeset
   502
		jid := new(JID)
aa2cf77f0ed3 When the server sends us our newly bound resource, update Client.Jid
Chris Jones <chris@cjones.org>
parents: 14
diff changeset
   503
		if !jid.Set(jidStr) {
aa2cf77f0ed3 When the server sends us our newly bound resource, update Client.Jid
Chris Jones <chris@cjones.org>
parents: 14
diff changeset
   504
			log.Println("Can't parse JID %s", jidStr)
aa2cf77f0ed3 When the server sends us our newly bound resource, update Client.Jid
Chris Jones <chris@cjones.org>
parents: 14
diff changeset
   505
			return false
aa2cf77f0ed3 When the server sends us our newly bound resource, update Client.Jid
Chris Jones <chris@cjones.org>
parents: 14
diff changeset
   506
		}
aa2cf77f0ed3 When the server sends us our newly bound resource, update Client.Jid
Chris Jones <chris@cjones.org>
parents: 14
diff changeset
   507
		cl.Jid = *jid
aa2cf77f0ed3 When the server sends us our newly bound resource, update Client.Jid
Chris Jones <chris@cjones.org>
parents: 14
diff changeset
   508
		log.Printf("Bound resource: %s", cl.Jid.String())
aa2cf77f0ed3 When the server sends us our newly bound resource, update Client.Jid
Chris Jones <chris@cjones.org>
parents: 14
diff changeset
   509
		return true
aa2cf77f0ed3 When the server sends us our newly bound resource, update Client.Jid
Chris Jones <chris@cjones.org>
parents: 14
diff changeset
   510
	}
aa2cf77f0ed3 When the server sends us our newly bound resource, update Client.Jid
Chris Jones <chris@cjones.org>
parents: 14
diff changeset
   511
	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
   512
	cl.xmlOut <- msg
122ab6208c3c Added resource binding and structures for <iq>, <message>, and <presence>.
Chris Jones <chris@cjones.org>
parents: 11
diff changeset
   513
}
13
c9527bbe99a6 Added a callback handler which will handle a stanza with a particular id.
Chris Jones <chris@cjones.org>
parents: 12
diff changeset
   514
c9527bbe99a6 Added a callback handler which will handle a stanza with a particular id.
Chris Jones <chris@cjones.org>
parents: 12
diff changeset
   515
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
   516
	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
   517
	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
   518
}