author | Chris Jones <christian.jones@sri.com> |
Thu, 19 Jan 2012 22:57:36 -0700 | |
changeset 84 | 25c4296a3524 |
parent 83 | a264cb0129e2 |
child 93 | fbd51fa6b7ea |
permissions | -rw-r--r-- |
10 | 1 |
// Copyright 2011 The Go Authors. All rights reserved. |
2 |
// Use of this source code is governed by a BSD-style |
|
3 |
// license that can be found in the LICENSE file. |
|
4 |
||
5 |
// This file contains the three layers of processing for the |
|
6 |
// communication with the server: transport (where TLS happens), XML |
|
7 |
// (where strings are converted to go structures), and Stream (where |
|
17 | 8 |
// we respond to XMPP events on behalf of the library client), or send |
9 |
// those events to the client. |
|
10 | 10 |
|
11 |
package xmpp |
|
12 |
||
13 |
import ( |
|
82
7ce2432dd66a
Implement (untested) EXTERNAL auth, and fix bugs from my previous commit.
Chris Jones <christian.jones@sri.com>
parents:
81
diff
changeset
|
14 |
"bytes" |
11
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
15 |
"crypto/md5" |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
16 |
"crypto/rand" |
10 | 17 |
"crypto/tls" |
11
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
18 |
"encoding/base64" |
74
e619e18dcec3
Ran gofix from weekly-2012-01-15.
Chris Jones <christian.jones@sri.com>
parents:
72
diff
changeset
|
19 |
"encoding/xml" |
11
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
20 |
"fmt" |
10 | 21 |
"io" |
74
e619e18dcec3
Ran gofix from weekly-2012-01-15.
Chris Jones <christian.jones@sri.com>
parents:
72
diff
changeset
|
22 |
"log/syslog" |
e619e18dcec3
Ran gofix from weekly-2012-01-15.
Chris Jones <christian.jones@sri.com>
parents:
72
diff
changeset
|
23 |
"math/big" |
10 | 24 |
"net" |
11
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
25 |
"regexp" |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
26 |
"strings" |
10 | 27 |
"time" |
28 |
) |
|
29 |
||
17 | 30 |
// 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
|
31 |
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
|
32 |
id string |
17 | 33 |
// 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
|
34 |
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
|
35 |
} |
c9527bbe99a6
Added a callback handler which will handle a stanza with a particular id.
Chris Jones <chris@cjones.org>
parents:
12
diff
changeset
|
36 |
|
81
a74e7fc4ecee
Consolidate how we generate the "open stream" XML.
Chris Jones <christian.jones@sri.com>
parents:
78
diff
changeset
|
37 |
// BUG(cjyar): There's no way to specify xml:lang for the top-level |
a74e7fc4ecee
Consolidate how we generate the "open stream" XML.
Chris Jones <christian.jones@sri.com>
parents:
78
diff
changeset
|
38 |
// <stream:stream> element as the default language for this client. |
a74e7fc4ecee
Consolidate how we generate the "open stream" XML.
Chris Jones <christian.jones@sri.com>
parents:
78
diff
changeset
|
39 |
|
83
a264cb0129e2
Added another SHOULD comment.
Chris Jones <christian.jones@sri.com>
parents:
82
diff
changeset
|
40 |
// BUG(cjyar): We SHOULD send a text declaration before the "open |
a264cb0129e2
Added another SHOULD comment.
Chris Jones <christian.jones@sri.com>
parents:
82
diff
changeset
|
41 |
// stream" element. |
a264cb0129e2
Added another SHOULD comment.
Chris Jones <christian.jones@sri.com>
parents:
82
diff
changeset
|
42 |
|
81
a74e7fc4ecee
Consolidate how we generate the "open stream" XML.
Chris Jones <christian.jones@sri.com>
parents:
78
diff
changeset
|
43 |
// Generate the "open stream" element which tells the remote we want |
a74e7fc4ecee
Consolidate how we generate the "open stream" XML.
Chris Jones <christian.jones@sri.com>
parents:
78
diff
changeset
|
44 |
// to speak XMPP. This is actually done 3 times, since we renegotiate |
a74e7fc4ecee
Consolidate how we generate the "open stream" XML.
Chris Jones <christian.jones@sri.com>
parents:
78
diff
changeset
|
45 |
// our transport layer with TLS and then with SASL. |
a74e7fc4ecee
Consolidate how we generate the "open stream" XML.
Chris Jones <christian.jones@sri.com>
parents:
78
diff
changeset
|
46 |
func openStream(jid *JID) *stream { |
a74e7fc4ecee
Consolidate how we generate the "open stream" XML.
Chris Jones <christian.jones@sri.com>
parents:
78
diff
changeset
|
47 |
return &stream{To: jid.Domain, Version: Version} |
a74e7fc4ecee
Consolidate how we generate the "open stream" XML.
Chris Jones <christian.jones@sri.com>
parents:
78
diff
changeset
|
48 |
} |
a74e7fc4ecee
Consolidate how we generate the "open stream" XML.
Chris Jones <christian.jones@sri.com>
parents:
78
diff
changeset
|
49 |
|
20
e119444a1119
Replaced TODO comments with Go-style BUG(me) comments.
Chris Jones <chris@cjones.org>
parents:
19
diff
changeset
|
50 |
// BUG(cjyar) Review all these *Client receiver methods. They should |
17 | 51 |
// probably either all be receivers, or none. |
52 |
||
64
ac0639692317
Properly close all the channels and writers if Client.Out is close.
Chris Jones <chris@cjones.org>
parents:
63
diff
changeset
|
53 |
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
|
54 |
defer w.Close() |
10 | 55 |
cl.socket.SetReadTimeout(1e8) |
56 |
p := make([]byte, 1024) |
|
57 |
for { |
|
58 |
if cl.socket == nil { |
|
59 |
cl.waitForSocket() |
|
60 |
} |
|
61 |
nr, err := cl.socket.Read(p) |
|
62 |
if nr == 0 { |
|
72 | 63 |
if errno, ok := err.(*net.OpError); ok { |
10 | 64 |
if errno.Timeout() { |
65 |
continue |
|
66 |
} |
|
67 |
} |
|
62
6e2eea62ccca
Added global variables for logging.
Chris Jones <chris@cjones.org>
parents:
61
diff
changeset
|
68 |
if Log != nil { |
76
caa722ab8a0f
Fixed logging to use log rather than syslog.
Chris Jones <christian.jones@sri.com>
parents:
75
diff
changeset
|
69 |
Log.Println("read: " + err.Error()) |
62
6e2eea62ccca
Added global variables for logging.
Chris Jones <chris@cjones.org>
parents:
61
diff
changeset
|
70 |
} |
10 | 71 |
break |
72 |
} |
|
73 |
nw, err := w.Write(p[:nr]) |
|
74 |
if nw < nr { |
|
62
6e2eea62ccca
Added global variables for logging.
Chris Jones <chris@cjones.org>
parents:
61
diff
changeset
|
75 |
if Log != nil { |
76
caa722ab8a0f
Fixed logging to use log rather than syslog.
Chris Jones <christian.jones@sri.com>
parents:
75
diff
changeset
|
76 |
Log.Println("read: " + err.Error()) |
62
6e2eea62ccca
Added global variables for logging.
Chris Jones <chris@cjones.org>
parents:
61
diff
changeset
|
77 |
} |
10 | 78 |
break |
79 |
} |
|
80 |
} |
|
81 |
} |
|
82 |
||
83 |
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
|
84 |
defer cl.socket.Close() |
10 | 85 |
p := make([]byte, 1024) |
86 |
for { |
|
87 |
nr, err := r.Read(p) |
|
88 |
if nr == 0 { |
|
62
6e2eea62ccca
Added global variables for logging.
Chris Jones <chris@cjones.org>
parents:
61
diff
changeset
|
89 |
if Log != nil { |
76
caa722ab8a0f
Fixed logging to use log rather than syslog.
Chris Jones <christian.jones@sri.com>
parents:
75
diff
changeset
|
90 |
Log.Println("write: " + err.Error()) |
62
6e2eea62ccca
Added global variables for logging.
Chris Jones <chris@cjones.org>
parents:
61
diff
changeset
|
91 |
} |
10 | 92 |
break |
93 |
} |
|
94 |
nw, err := cl.socket.Write(p[:nr]) |
|
95 |
if nw < nr { |
|
62
6e2eea62ccca
Added global variables for logging.
Chris Jones <chris@cjones.org>
parents:
61
diff
changeset
|
96 |
if Log != nil { |
76
caa722ab8a0f
Fixed logging to use log rather than syslog.
Chris Jones <christian.jones@sri.com>
parents:
75
diff
changeset
|
97 |
Log.Println("write: " + err.Error()) |
62
6e2eea62ccca
Added global variables for logging.
Chris Jones <chris@cjones.org>
parents:
61
diff
changeset
|
98 |
} |
10 | 99 |
break |
100 |
} |
|
101 |
} |
|
102 |
} |
|
103 |
||
36
9fe022261dcc
Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
35
diff
changeset
|
104 |
func readXml(r io.Reader, ch chan<- interface{}, |
74
e619e18dcec3
Ran gofix from weekly-2012-01-15.
Chris Jones <christian.jones@sri.com>
parents:
72
diff
changeset
|
105 |
extStanza map[string]func(*xml.Name) interface{}) { |
62
6e2eea62ccca
Added global variables for logging.
Chris Jones <chris@cjones.org>
parents:
61
diff
changeset
|
106 |
if Loglevel >= syslog.LOG_DEBUG { |
10 | 107 |
pr, pw := io.Pipe() |
108 |
go tee(r, pw, "S: ") |
|
109 |
r = pr |
|
110 |
} |
|
64
ac0639692317
Properly close all the channels and writers if Client.Out is close.
Chris Jones <chris@cjones.org>
parents:
63
diff
changeset
|
111 |
defer close(ch) |
10 | 112 |
|
113 |
p := xml.NewParser(r) |
|
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
|
114 |
Loop: |
10 | 115 |
for { |
116 |
// Sniff the next token on the stream. |
|
117 |
t, err := p.Token() |
|
118 |
if t == nil { |
|
74
e619e18dcec3
Ran gofix from weekly-2012-01-15.
Chris Jones <christian.jones@sri.com>
parents:
72
diff
changeset
|
119 |
if err != io.EOF { |
62
6e2eea62ccca
Added global variables for logging.
Chris Jones <chris@cjones.org>
parents:
61
diff
changeset
|
120 |
if Log != nil { |
76
caa722ab8a0f
Fixed logging to use log rather than syslog.
Chris Jones <christian.jones@sri.com>
parents:
75
diff
changeset
|
121 |
Log.Println("read: " + err.Error()) |
62
6e2eea62ccca
Added global variables for logging.
Chris Jones <chris@cjones.org>
parents:
61
diff
changeset
|
122 |
} |
10 | 123 |
} |
124 |
break |
|
125 |
} |
|
126 |
var se xml.StartElement |
|
127 |
var ok bool |
|
72 | 128 |
if se, ok = t.(xml.StartElement); !ok { |
10 | 129 |
continue |
130 |
} |
|
131 |
||
132 |
// Allocate the appropriate structure for this token. |
|
133 |
var obj interface{} |
|
134 |
switch se.Name.Space + " " + se.Name.Local { |
|
34
7b1f924c75e2
Made the namespace constants public.
Chris Jones <chris@cjones.org>
parents:
33
diff
changeset
|
135 |
case NsStream + " stream": |
10 | 136 |
st, err := parseStream(se) |
137 |
if err != nil { |
|
62
6e2eea62ccca
Added global variables for logging.
Chris Jones <chris@cjones.org>
parents:
61
diff
changeset
|
138 |
if Log != nil { |
76
caa722ab8a0f
Fixed logging to use log rather than syslog.
Chris Jones <christian.jones@sri.com>
parents:
75
diff
changeset
|
139 |
Log.Println("unmarshal stream: " + |
74
e619e18dcec3
Ran gofix from weekly-2012-01-15.
Chris Jones <christian.jones@sri.com>
parents:
72
diff
changeset
|
140 |
err.Error()) |
62
6e2eea62ccca
Added global variables for logging.
Chris Jones <chris@cjones.org>
parents:
61
diff
changeset
|
141 |
} |
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
|
142 |
break Loop |
10 | 143 |
} |
84
25c4296a3524
Updates to BUG comments.
Chris Jones <christian.jones@sri.com>
parents:
83
diff
changeset
|
144 |
// BUG(cjyar): Does not verify server hostname |
25c4296a3524
Updates to BUG comments.
Chris Jones <christian.jones@sri.com>
parents:
83
diff
changeset
|
145 |
// in DNS as per RFC3920.14.3. |
10 | 146 |
ch <- st |
147 |
continue |
|
34
7b1f924c75e2
Made the namespace constants public.
Chris Jones <chris@cjones.org>
parents:
33
diff
changeset
|
148 |
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
|
149 |
obj = &streamError{} |
34
7b1f924c75e2
Made the namespace constants public.
Chris Jones <chris@cjones.org>
parents:
33
diff
changeset
|
150 |
case NsStream + " features": |
10 | 151 |
obj = &Features{} |
34
7b1f924c75e2
Made the namespace constants public.
Chris Jones <chris@cjones.org>
parents:
33
diff
changeset
|
152 |
case NsTLS + " proceed", NsTLS + " failure": |
10 | 153 |
obj = &starttls{} |
34
7b1f924c75e2
Made the namespace constants public.
Chris Jones <chris@cjones.org>
parents:
33
diff
changeset
|
154 |
case NsSASL + " challenge", NsSASL + " failure", |
7b1f924c75e2
Made the namespace constants public.
Chris Jones <chris@cjones.org>
parents:
33
diff
changeset
|
155 |
NsSASL + " success": |
11
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
156 |
obj = &auth{} |
12
122ab6208c3c
Added resource binding and structures for <iq>, <message>, and <presence>.
Chris Jones <chris@cjones.org>
parents:
11
diff
changeset
|
157 |
case "jabber:client iq": |
122ab6208c3c
Added resource binding and structures for <iq>, <message>, and <presence>.
Chris Jones <chris@cjones.org>
parents:
11
diff
changeset
|
158 |
obj = &Iq{} |
16
b839e37b3f29
Parse <presence> and <message> stanzas.
Chris Jones <chris@cjones.org>
parents:
15
diff
changeset
|
159 |
case "jabber:client message": |
b839e37b3f29
Parse <presence> and <message> stanzas.
Chris Jones <chris@cjones.org>
parents:
15
diff
changeset
|
160 |
obj = &Message{} |
b839e37b3f29
Parse <presence> and <message> stanzas.
Chris Jones <chris@cjones.org>
parents:
15
diff
changeset
|
161 |
case "jabber:client presence": |
b839e37b3f29
Parse <presence> and <message> stanzas.
Chris Jones <chris@cjones.org>
parents:
15
diff
changeset
|
162 |
obj = &Presence{} |
10 | 163 |
default: |
21
8f6ae5cfc9b9
Renamed Unrecognized to Generic.
Chris Jones <chris@cjones.org>
parents:
20
diff
changeset
|
164 |
obj = &Generic{} |
76
caa722ab8a0f
Fixed logging to use log rather than syslog.
Chris Jones <christian.jones@sri.com>
parents:
75
diff
changeset
|
165 |
if Log != nil && Loglevel >= syslog.LOG_NOTICE { |
caa722ab8a0f
Fixed logging to use log rather than syslog.
Chris Jones <christian.jones@sri.com>
parents:
75
diff
changeset
|
166 |
Log.Printf("Ignoring unrecognized: %s %s", |
caa722ab8a0f
Fixed logging to use log rather than syslog.
Chris Jones <christian.jones@sri.com>
parents:
75
diff
changeset
|
167 |
se.Name.Space, se.Name.Local) |
62
6e2eea62ccca
Added global variables for logging.
Chris Jones <chris@cjones.org>
parents:
61
diff
changeset
|
168 |
} |
10 | 169 |
} |
170 |
||
171 |
// Read the complete XML stanza. |
|
172 |
err = p.Unmarshal(obj, &se) |
|
173 |
if err != nil { |
|
62
6e2eea62ccca
Added global variables for logging.
Chris Jones <chris@cjones.org>
parents:
61
diff
changeset
|
174 |
if Log != nil { |
76
caa722ab8a0f
Fixed logging to use log rather than syslog.
Chris Jones <christian.jones@sri.com>
parents:
75
diff
changeset
|
175 |
Log.Println("unmarshal: " + err.Error()) |
62
6e2eea62ccca
Added global variables for logging.
Chris Jones <chris@cjones.org>
parents:
61
diff
changeset
|
176 |
} |
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
|
177 |
break Loop |
10 | 178 |
} |
179 |
||
61
16513974d273
Stanzas can now contain multiple nested (extended) elements.
Chris Jones <chris@cjones.org>
parents:
59
diff
changeset
|
180 |
// 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
|
181 |
// into objects of the appropriate respective |
16513974d273
Stanzas can now contain multiple nested (extended) elements.
Chris Jones <chris@cjones.org>
parents:
59
diff
changeset
|
182 |
// types. This is specified by our extensions. |
72 | 183 |
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
|
184 |
err = parseExtended(st, extStanza) |
16513974d273
Stanzas can now contain multiple nested (extended) elements.
Chris Jones <chris@cjones.org>
parents:
59
diff
changeset
|
185 |
if err != nil { |
62
6e2eea62ccca
Added global variables for logging.
Chris Jones <chris@cjones.org>
parents:
61
diff
changeset
|
186 |
if Log != nil { |
76
caa722ab8a0f
Fixed logging to use log rather than syslog.
Chris Jones <christian.jones@sri.com>
parents:
75
diff
changeset
|
187 |
Log.Println("ext unmarshal: " + |
74
e619e18dcec3
Ran gofix from weekly-2012-01-15.
Chris Jones <christian.jones@sri.com>
parents:
72
diff
changeset
|
188 |
err.Error()) |
62
6e2eea62ccca
Added global variables for logging.
Chris Jones <chris@cjones.org>
parents:
61
diff
changeset
|
189 |
} |
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
|
190 |
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
|
191 |
} |
9fe022261dcc
Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
35
diff
changeset
|
192 |
} |
17 | 193 |
|
10 | 194 |
// Put it on the channel. |
195 |
ch <- obj |
|
196 |
} |
|
197 |
} |
|
198 |
||
74
e619e18dcec3
Ran gofix from weekly-2012-01-15.
Chris Jones <christian.jones@sri.com>
parents:
72
diff
changeset
|
199 |
func parseExtended(st Stanza, extStanza map[string]func(*xml.Name) interface{}) error { |
38 | 200 |
// Now parse the stanza's innerxml to find the string that we |
201 |
// can unmarshal this nested element from. |
|
202 |
reader := strings.NewReader(st.innerxml()) |
|
203 |
p := xml.NewParser(reader) |
|
204 |
for { |
|
205 |
t, err := p.Token() |
|
74
e619e18dcec3
Ran gofix from weekly-2012-01-15.
Chris Jones <christian.jones@sri.com>
parents:
72
diff
changeset
|
206 |
if err == io.EOF { |
61
16513974d273
Stanzas can now contain multiple nested (extended) elements.
Chris Jones <chris@cjones.org>
parents:
59
diff
changeset
|
207 |
break |
16513974d273
Stanzas can now contain multiple nested (extended) elements.
Chris Jones <chris@cjones.org>
parents:
59
diff
changeset
|
208 |
} |
38 | 209 |
if err != nil { |
210 |
return err |
|
211 |
} |
|
72 | 212 |
if se, ok := t.(xml.StartElement); ok { |
213 |
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
|
214 |
// Call the indicated constructor. |
16513974d273
Stanzas can now contain multiple nested (extended) elements.
Chris Jones <chris@cjones.org>
parents:
59
diff
changeset
|
215 |
nested := con(&se.Name) |
16513974d273
Stanzas can now contain multiple nested (extended) elements.
Chris Jones <chris@cjones.org>
parents:
59
diff
changeset
|
216 |
|
16513974d273
Stanzas can now contain multiple nested (extended) elements.
Chris Jones <chris@cjones.org>
parents:
59
diff
changeset
|
217 |
// Unmarshal the nested element and |
16513974d273
Stanzas can now contain multiple nested (extended) elements.
Chris Jones <chris@cjones.org>
parents:
59
diff
changeset
|
218 |
// stuff it back into the stanza. |
16513974d273
Stanzas can now contain multiple nested (extended) elements.
Chris Jones <chris@cjones.org>
parents:
59
diff
changeset
|
219 |
err := p.Unmarshal(nested, &se) |
16513974d273
Stanzas can now contain multiple nested (extended) elements.
Chris Jones <chris@cjones.org>
parents:
59
diff
changeset
|
220 |
if err != nil { |
16513974d273
Stanzas can now contain multiple nested (extended) elements.
Chris Jones <chris@cjones.org>
parents:
59
diff
changeset
|
221 |
return err |
16513974d273
Stanzas can now contain multiple nested (extended) elements.
Chris Jones <chris@cjones.org>
parents:
59
diff
changeset
|
222 |
} |
16513974d273
Stanzas can now contain multiple nested (extended) elements.
Chris Jones <chris@cjones.org>
parents:
59
diff
changeset
|
223 |
st.addNested(nested) |
38 | 224 |
} |
225 |
} |
|
226 |
} |
|
227 |
||
228 |
return nil |
|
229 |
} |
|
230 |
||
10 | 231 |
func writeXml(w io.Writer, ch <-chan interface{}) { |
62
6e2eea62ccca
Added global variables for logging.
Chris Jones <chris@cjones.org>
parents:
61
diff
changeset
|
232 |
if Loglevel >= syslog.LOG_DEBUG { |
10 | 233 |
pr, pw := io.Pipe() |
234 |
go tee(pr, w, "C: ") |
|
235 |
w = pw |
|
236 |
} |
|
64
ac0639692317
Properly close all the channels and writers if Client.Out is close.
Chris Jones <chris@cjones.org>
parents:
63
diff
changeset
|
237 |
defer func(w io.Writer) { |
72 | 238 |
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
|
239 |
c.Close() |
ac0639692317
Properly close all the channels and writers if Client.Out is close.
Chris Jones <chris@cjones.org>
parents:
63
diff
changeset
|
240 |
} |
ac0639692317
Properly close all the channels and writers if Client.Out is close.
Chris Jones <chris@cjones.org>
parents:
63
diff
changeset
|
241 |
}(w) |
10 | 242 |
|
243 |
for obj := range ch { |
|
244 |
err := xml.Marshal(w, obj) |
|
245 |
if err != nil { |
|
62
6e2eea62ccca
Added global variables for logging.
Chris Jones <chris@cjones.org>
parents:
61
diff
changeset
|
246 |
if Log != nil { |
76
caa722ab8a0f
Fixed logging to use log rather than syslog.
Chris Jones <christian.jones@sri.com>
parents:
75
diff
changeset
|
247 |
Log.Println("write: " + err.Error()) |
62
6e2eea62ccca
Added global variables for logging.
Chris Jones <chris@cjones.org>
parents:
61
diff
changeset
|
248 |
} |
10 | 249 |
break |
250 |
} |
|
251 |
} |
|
252 |
} |
|
253 |
||
23
b5de44679389
Made the input and output channels of type Stanza rather than
Chris Jones <chris@cjones.org>
parents:
22
diff
changeset
|
254 |
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
|
255 |
defer close(cliOut) |
10 | 256 |
|
72 | 257 |
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
|
258 |
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
|
259 |
for { |
c9527bbe99a6
Added a callback handler which will handle a stanza with a particular id.
Chris Jones <chris@cjones.org>
parents:
12
diff
changeset
|
260 |
select { |
72 | 261 |
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
|
262 |
handlers[h.id] = h.f |
72 | 263 |
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
|
264 |
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
|
265 |
break Loop |
26 | 266 |
} |
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 := false |
c9527bbe99a6
Added a callback handler which will handle a stanza with a particular id.
Chris Jones <chris@cjones.org>
parents:
12
diff
changeset
|
268 |
switch obj := x.(type) { |
22
d6b7b4cbf50d
Made the stream type non-public.
Chris Jones <chris@cjones.org>
parents:
21
diff
changeset
|
269 |
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
|
270 |
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
|
271 |
case *streamError: |
1dc47df5c99f
Made streamError non-public, and made a first attempt at a stream
Chris Jones <chris@cjones.org>
parents:
30
diff
changeset
|
272 |
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
|
273 |
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
|
274 |
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
|
275 |
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
|
276 |
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
|
277 |
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
|
278 |
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
|
279 |
default: |
c9527bbe99a6
Added a callback handler which will handle a stanza with a particular id.
Chris Jones <chris@cjones.org>
parents:
12
diff
changeset
|
280 |
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
|
281 |
} |
29
a456133ed0ac
Don't accept data on Client.Out until resource binding is
Chris Jones <chris@cjones.org>
parents:
26
diff
changeset
|
282 |
if !send { |
a456133ed0ac
Don't accept data on Client.Out until resource binding is
Chris Jones <chris@cjones.org>
parents:
26
diff
changeset
|
283 |
continue |
a456133ed0ac
Don't accept data on Client.Out until resource binding is
Chris Jones <chris@cjones.org>
parents:
26
diff
changeset
|
284 |
} |
23
b5de44679389
Made the input and output channels of type Stanza rather than
Chris Jones <chris@cjones.org>
parents:
22
diff
changeset
|
285 |
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
|
286 |
if !ok { |
76
caa722ab8a0f
Fixed logging to use log rather than syslog.
Chris Jones <christian.jones@sri.com>
parents:
75
diff
changeset
|
287 |
if Log != nil && Loglevel >= syslog.LOG_WARNING { |
caa722ab8a0f
Fixed logging to use log rather than syslog.
Chris Jones <christian.jones@sri.com>
parents:
75
diff
changeset
|
288 |
Log.Printf( |
caa722ab8a0f
Fixed logging to use log rather than syslog.
Chris Jones <christian.jones@sri.com>
parents:
75
diff
changeset
|
289 |
"Unhandled non-stanza: %v", x) |
62
6e2eea62ccca
Added global variables for logging.
Chris Jones <chris@cjones.org>
parents:
61
diff
changeset
|
290 |
} |
23
b5de44679389
Made the input and output channels of type Stanza rather than
Chris Jones <chris@cjones.org>
parents:
22
diff
changeset
|
291 |
continue |
b5de44679389
Made the input and output channels of type Stanza rather than
Chris Jones <chris@cjones.org>
parents:
22
diff
changeset
|
292 |
} |
42
f6bb47ca12f2
Renamed the somewhat obscure XTo(), etc. to GetTo(), etc.
Chris Jones <chris@cjones.org>
parents:
41
diff
changeset
|
293 |
if handlers[st.GetId()] != nil { |
f6bb47ca12f2
Renamed the somewhat obscure XTo(), etc. to GetTo(), etc.
Chris Jones <chris@cjones.org>
parents:
41
diff
changeset
|
294 |
f := handlers[st.GetId()] |
f6bb47ca12f2
Renamed the somewhat obscure XTo(), etc. to GetTo(), etc.
Chris Jones <chris@cjones.org>
parents:
41
diff
changeset
|
295 |
handlers[st.GetId()] = 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
|
296 |
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
|
297 |
} |
c9527bbe99a6
Added a callback handler which will handle a stanza with a particular id.
Chris Jones <chris@cjones.org>
parents:
12
diff
changeset
|
298 |
if send { |
23
b5de44679389
Made the input and output channels of type Stanza rather than
Chris Jones <chris@cjones.org>
parents:
22
diff
changeset
|
299 |
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
|
300 |
} |
10 | 301 |
} |
302 |
} |
|
303 |
} |
|
304 |
||
29
a456133ed0ac
Don't accept data on Client.Out until resource binding is
Chris Jones <chris@cjones.org>
parents:
26
diff
changeset
|
305 |
// 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
|
306 |
// 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
|
307 |
// 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
|
308 |
// activity. |
a456133ed0ac
Don't accept data on Client.Out until resource binding is
Chris Jones <chris@cjones.org>
parents:
26
diff
changeset
|
309 |
func writeStream(srvOut chan<- interface{}, cliIn <-chan Stanza, |
74
e619e18dcec3
Ran gofix from weekly-2012-01-15.
Chris Jones <christian.jones@sri.com>
parents:
72
diff
changeset
|
310 |
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
|
311 |
defer close(srvOut) |
10 | 312 |
|
29
a456133ed0ac
Don't accept data on Client.Out until resource binding is
Chris Jones <chris@cjones.org>
parents:
26
diff
changeset
|
313 |
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
|
314 |
Loop: |
29
a456133ed0ac
Don't accept data on Client.Out until resource binding is
Chris Jones <chris@cjones.org>
parents:
26
diff
changeset
|
315 |
for { |
a456133ed0ac
Don't accept data on Client.Out until resource binding is
Chris Jones <chris@cjones.org>
parents:
26
diff
changeset
|
316 |
select { |
72 | 317 |
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
|
318 |
switch status { |
a456133ed0ac
Don't accept data on Client.Out until resource binding is
Chris Jones <chris@cjones.org>
parents:
26
diff
changeset
|
319 |
case 0: |
a456133ed0ac
Don't accept data on Client.Out until resource binding is
Chris Jones <chris@cjones.org>
parents:
26
diff
changeset
|
320 |
input = nil |
a456133ed0ac
Don't accept data on Client.Out until resource binding is
Chris Jones <chris@cjones.org>
parents:
26
diff
changeset
|
321 |
case 1: |
a456133ed0ac
Don't accept data on Client.Out until resource binding is
Chris Jones <chris@cjones.org>
parents:
26
diff
changeset
|
322 |
input = cliIn |
a456133ed0ac
Don't accept data on Client.Out until resource binding is
Chris Jones <chris@cjones.org>
parents:
26
diff
changeset
|
323 |
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
|
324 |
break Loop |
29
a456133ed0ac
Don't accept data on Client.Out until resource binding is
Chris Jones <chris@cjones.org>
parents:
26
diff
changeset
|
325 |
} |
72 | 326 |
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
|
327 |
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
|
328 |
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
|
329 |
} |
51
1af366d10d32
Nil checks and a greatly simplified filter manager which is less buggy.
Chris Jones <chris@cjones.org>
parents:
46
diff
changeset
|
330 |
if x == nil { |
76
caa722ab8a0f
Fixed logging to use log rather than syslog.
Chris Jones <christian.jones@sri.com>
parents:
75
diff
changeset
|
331 |
if Log != nil && Loglevel >= syslog.LOG_NOTICE { |
caa722ab8a0f
Fixed logging to use log rather than syslog.
Chris Jones <christian.jones@sri.com>
parents:
75
diff
changeset
|
332 |
Log.Println("Refusing to send" + |
62
6e2eea62ccca
Added global variables for logging.
Chris Jones <chris@cjones.org>
parents:
61
diff
changeset
|
333 |
" nil stanza") |
6e2eea62ccca
Added global variables for logging.
Chris Jones <chris@cjones.org>
parents:
61
diff
changeset
|
334 |
} |
51
1af366d10d32
Nil checks and a greatly simplified filter manager which is less buggy.
Chris Jones <chris@cjones.org>
parents:
46
diff
changeset
|
335 |
continue |
1af366d10d32
Nil checks and a greatly simplified filter manager which is less buggy.
Chris Jones <chris@cjones.org>
parents:
46
diff
changeset
|
336 |
} |
29
a456133ed0ac
Don't accept data on Client.Out until resource binding is
Chris Jones <chris@cjones.org>
parents:
26
diff
changeset
|
337 |
srvOut <- x |
a456133ed0ac
Don't accept data on Client.Out until resource binding is
Chris Jones <chris@cjones.org>
parents:
26
diff
changeset
|
338 |
} |
10 | 339 |
} |
340 |
} |
|
341 |
||
45
abf958bcc201
Added a stack of filters which can intercept data before it gets to
Chris Jones <chris@cjones.org>
parents:
42
diff
changeset
|
342 |
// 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
|
343 |
// 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
|
344 |
func filterTop(filterOut <-chan <-chan Stanza, filterIn chan<- <-chan Stanza, |
74
e619e18dcec3
Ran gofix from weekly-2012-01-15.
Chris Jones <christian.jones@sri.com>
parents:
72
diff
changeset
|
345 |
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
|
346 |
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
|
347 |
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
|
348 |
for { |
abf958bcc201
Added a stack of filters which can intercept data before it gets to
Chris Jones <chris@cjones.org>
parents:
42
diff
changeset
|
349 |
select { |
72 | 350 |
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
|
351 |
if newFilterOut == nil { |
76
caa722ab8a0f
Fixed logging to use log rather than syslog.
Chris Jones <christian.jones@sri.com>
parents:
75
diff
changeset
|
352 |
if Log != nil && Loglevel >= syslog.LOG_WARNING { |
caa722ab8a0f
Fixed logging to use log rather than syslog.
Chris Jones <christian.jones@sri.com>
parents:
75
diff
changeset
|
353 |
Log.Println("Received nil filter") |
62
6e2eea62ccca
Added global variables for logging.
Chris Jones <chris@cjones.org>
parents:
61
diff
changeset
|
354 |
} |
51
1af366d10d32
Nil checks and a greatly simplified filter manager which is less buggy.
Chris Jones <chris@cjones.org>
parents:
46
diff
changeset
|
355 |
filterIn <- nil |
1af366d10d32
Nil checks and a greatly simplified filter manager which is less buggy.
Chris Jones <chris@cjones.org>
parents:
46
diff
changeset
|
356 |
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
|
357 |
} |
51
1af366d10d32
Nil checks and a greatly simplified filter manager which is less buggy.
Chris Jones <chris@cjones.org>
parents:
46
diff
changeset
|
358 |
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
|
359 |
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
|
360 |
|
51
1af366d10d32
Nil checks and a greatly simplified filter manager which is less buggy.
Chris Jones <chris@cjones.org>
parents:
46
diff
changeset
|
361 |
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
|
362 |
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
|
363 |
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
|
364 |
} |
51
1af366d10d32
Nil checks and a greatly simplified filter manager which is less buggy.
Chris Jones <chris@cjones.org>
parents:
46
diff
changeset
|
365 |
app <- data |
1af366d10d32
Nil checks and a greatly simplified filter manager which is less buggy.
Chris Jones <chris@cjones.org>
parents:
46
diff
changeset
|
366 |
} |
1af366d10d32
Nil checks and a greatly simplified filter manager which is less buggy.
Chris Jones <chris@cjones.org>
parents:
46
diff
changeset
|
367 |
} |
1af366d10d32
Nil checks and a greatly simplified filter manager which is less buggy.
Chris Jones <chris@cjones.org>
parents:
46
diff
changeset
|
368 |
} |
45
abf958bcc201
Added a stack of filters which can intercept data before it gets to
Chris Jones <chris@cjones.org>
parents:
42
diff
changeset
|
369 |
|
51
1af366d10d32
Nil checks and a greatly simplified filter manager which is less buggy.
Chris Jones <chris@cjones.org>
parents:
46
diff
changeset
|
370 |
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
|
371 |
defer close(to) |
72 | 372 |
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
|
373 |
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
|
374 |
} |
abf958bcc201
Added a stack of filters which can intercept data before it gets to
Chris Jones <chris@cjones.org>
parents:
42
diff
changeset
|
375 |
} |
abf958bcc201
Added a stack of filters which can intercept data before it gets to
Chris Jones <chris@cjones.org>
parents:
42
diff
changeset
|
376 |
|
22
d6b7b4cbf50d
Made the stream type non-public.
Chris Jones <chris@cjones.org>
parents:
21
diff
changeset
|
377 |
func handleStream(ss *stream) { |
10 | 378 |
} |
379 |
||
31
1dc47df5c99f
Made streamError non-public, and made a first attempt at a stream
Chris Jones <chris@cjones.org>
parents:
30
diff
changeset
|
380 |
func (cl *Client) handleStreamError(se *streamError) { |
76
caa722ab8a0f
Fixed logging to use log rather than syslog.
Chris Jones <christian.jones@sri.com>
parents:
75
diff
changeset
|
381 |
if Log != nil && Loglevel >= syslog.LOG_NOTICE { |
caa722ab8a0f
Fixed logging to use log rather than syslog.
Chris Jones <christian.jones@sri.com>
parents:
75
diff
changeset
|
382 |
Log.Printf("Received stream error: %v", se) |
62
6e2eea62ccca
Added global variables for logging.
Chris Jones <chris@cjones.org>
parents:
61
diff
changeset
|
383 |
} |
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
|
384 |
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
|
385 |
} |
1dc47df5c99f
Made streamError non-public, and made a first attempt at a stream
Chris Jones <chris@cjones.org>
parents:
30
diff
changeset
|
386 |
|
11
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
387 |
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
|
388 |
cl.Features = fe |
10 | 389 |
if fe.Starttls != nil { |
34
7b1f924c75e2
Made the namespace constants public.
Chris Jones <chris@cjones.org>
parents:
33
diff
changeset
|
390 |
start := &starttls{XMLName: xml.Name{Space: NsTLS, |
10 | 391 |
Local: "starttls"}} |
11
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
392 |
cl.xmlOut <- start |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
393 |
return |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
394 |
} |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
395 |
|
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
396 |
if len(fe.Mechanisms.Mechanism) > 0 { |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
397 |
cl.chooseSasl(fe) |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
398 |
return |
10 | 399 |
} |
12
122ab6208c3c
Added resource binding and structures for <iq>, <message>, and <presence>.
Chris Jones <chris@cjones.org>
parents:
11
diff
changeset
|
400 |
|
122ab6208c3c
Added resource binding and structures for <iq>, <message>, and <presence>.
Chris Jones <chris@cjones.org>
parents:
11
diff
changeset
|
401 |
if fe.Bind != nil { |
122ab6208c3c
Added resource binding and structures for <iq>, <message>, and <presence>.
Chris Jones <chris@cjones.org>
parents:
11
diff
changeset
|
402 |
cl.bind(fe.Bind) |
17 | 403 |
return |
12
122ab6208c3c
Added resource binding and structures for <iq>, <message>, and <presence>.
Chris Jones <chris@cjones.org>
parents:
11
diff
changeset
|
404 |
} |
10 | 405 |
} |
406 |
||
82
7ce2432dd66a
Implement (untested) EXTERNAL auth, and fix bugs from my previous commit.
Chris Jones <christian.jones@sri.com>
parents:
81
diff
changeset
|
407 |
// BUG(cjyar): Server certificate is not checked against the provided |
84
25c4296a3524
Updates to BUG comments.
Chris Jones <christian.jones@sri.com>
parents:
83
diff
changeset
|
408 |
// hostname. RFC3920.14.2 |
82
7ce2432dd66a
Implement (untested) EXTERNAL auth, and fix bugs from my previous commit.
Chris Jones <christian.jones@sri.com>
parents:
81
diff
changeset
|
409 |
|
10 | 410 |
// readTransport() is running concurrently. We need to stop it, |
411 |
// negotiate TLS, then start it again. It calls waitForSocket() in |
|
412 |
// its inner loop; see below. |
|
413 |
func (cl *Client) handleTls(t *starttls) { |
|
414 |
tcp := cl.socket |
|
415 |
||
416 |
// Set the socket to nil, and wait for the reader routine to |
|
417 |
// signal that it's paused. |
|
418 |
cl.socket = nil |
|
419 |
cl.socketSync.Add(1) |
|
420 |
cl.socketSync.Wait() |
|
421 |
||
422 |
// Negotiate TLS with the server. |
|
78
a5848c75d270
Allow the app to specify a tls.Config to use for purposes of negotiating the TLS layer.
Chris Jones <christian.jones@sri.com>
parents:
76
diff
changeset
|
423 |
tls := tls.Client(tcp, TLSConfig) |
10 | 424 |
|
425 |
// Make the TLS connection available to the reader, and wait |
|
426 |
// for it to signal that it's working again. |
|
427 |
cl.socketSync.Add(1) |
|
428 |
cl.socket = tls |
|
429 |
cl.socketSync.Wait() |
|
430 |
||
66
4558994ab3b3
Restore this bit of code that got lost in the shuffle.
Chris Jones <chris@cjones.org>
parents:
64
diff
changeset
|
431 |
// Reset the read timeout on the (underlying) socket so the |
4558994ab3b3
Restore this bit of code that got lost in the shuffle.
Chris Jones <chris@cjones.org>
parents:
64
diff
changeset
|
432 |
// reader doesn't get woken up unnecessarily. |
4558994ab3b3
Restore this bit of code that got lost in the shuffle.
Chris Jones <chris@cjones.org>
parents:
64
diff
changeset
|
433 |
tcp.SetReadTimeout(0) |
4558994ab3b3
Restore this bit of code that got lost in the shuffle.
Chris Jones <chris@cjones.org>
parents:
64
diff
changeset
|
434 |
|
76
caa722ab8a0f
Fixed logging to use log rather than syslog.
Chris Jones <christian.jones@sri.com>
parents:
75
diff
changeset
|
435 |
if Log != nil && Loglevel >= syslog.LOG_INFO { |
caa722ab8a0f
Fixed logging to use log rather than syslog.
Chris Jones <christian.jones@sri.com>
parents:
75
diff
changeset
|
436 |
Log.Println("TLS negotiation succeeded.") |
62
6e2eea62ccca
Added global variables for logging.
Chris Jones <chris@cjones.org>
parents:
61
diff
changeset
|
437 |
} |
32
4e68d8f89dc3
Make the server's advertised features available to the app.
Chris Jones <chris@cjones.org>
parents:
31
diff
changeset
|
438 |
cl.Features = nil |
10 | 439 |
|
440 |
// Now re-send the initial handshake message to start the new |
|
441 |
// session. |
|
82
7ce2432dd66a
Implement (untested) EXTERNAL auth, and fix bugs from my previous commit.
Chris Jones <christian.jones@sri.com>
parents:
81
diff
changeset
|
442 |
hsOut := openStream(&cl.Jid) |
10 | 443 |
cl.xmlOut <- hsOut |
444 |
} |
|
445 |
||
446 |
// Synchronize with handleTls(). Called from readTransport() when |
|
447 |
// cl.socket is nil. |
|
448 |
func (cl *Client) waitForSocket() { |
|
449 |
// Signal that we've stopped reading from the socket. |
|
450 |
cl.socketSync.Done() |
|
451 |
||
452 |
// Wait until the socket is available again. |
|
453 |
for cl.socket == nil { |
|
454 |
time.Sleep(1e8) |
|
455 |
} |
|
456 |
||
457 |
// Signal that we're going back to the read loop. |
|
458 |
cl.socketSync.Done() |
|
459 |
} |
|
11
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
460 |
|
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
461 |
func (cl *Client) chooseSasl(fe *Features) { |
82
7ce2432dd66a
Implement (untested) EXTERNAL auth, and fix bugs from my previous commit.
Chris Jones <christian.jones@sri.com>
parents:
81
diff
changeset
|
462 |
var digestMd5, external bool |
72 | 463 |
for _, m := range fe.Mechanisms.Mechanism { |
11
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
464 |
switch strings.ToLower(m) { |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
465 |
case "digest-md5": |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
466 |
digestMd5 = true |
82
7ce2432dd66a
Implement (untested) EXTERNAL auth, and fix bugs from my previous commit.
Chris Jones <christian.jones@sri.com>
parents:
81
diff
changeset
|
467 |
case "external": |
7ce2432dd66a
Implement (untested) EXTERNAL auth, and fix bugs from my previous commit.
Chris Jones <christian.jones@sri.com>
parents:
81
diff
changeset
|
468 |
external = true |
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 |
|
82
7ce2432dd66a
Implement (untested) EXTERNAL auth, and fix bugs from my previous commit.
Chris Jones <christian.jones@sri.com>
parents:
81
diff
changeset
|
472 |
if external { |
7ce2432dd66a
Implement (untested) EXTERNAL auth, and fix bugs from my previous commit.
Chris Jones <christian.jones@sri.com>
parents:
81
diff
changeset
|
473 |
auth := &auth{XMLName: xml.Name{Space: NsSASL, Local: |
7ce2432dd66a
Implement (untested) EXTERNAL auth, and fix bugs from my previous commit.
Chris Jones <christian.jones@sri.com>
parents:
81
diff
changeset
|
474 |
"auth"}, Mechanism: "EXTERNAL"} |
7ce2432dd66a
Implement (untested) EXTERNAL auth, and fix bugs from my previous commit.
Chris Jones <christian.jones@sri.com>
parents:
81
diff
changeset
|
475 |
cl.xmlOut <- auth |
7ce2432dd66a
Implement (untested) EXTERNAL auth, and fix bugs from my previous commit.
Chris Jones <christian.jones@sri.com>
parents:
81
diff
changeset
|
476 |
} else if digestMd5 { |
72 | 477 |
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
|
478 |
cl.xmlOut <- auth |
82
7ce2432dd66a
Implement (untested) EXTERNAL auth, and fix bugs from my previous commit.
Chris Jones <christian.jones@sri.com>
parents:
81
diff
changeset
|
479 |
} else { |
7ce2432dd66a
Implement (untested) EXTERNAL auth, and fix bugs from my previous commit.
Chris Jones <christian.jones@sri.com>
parents:
81
diff
changeset
|
480 |
if Log != nil { |
7ce2432dd66a
Implement (untested) EXTERNAL auth, and fix bugs from my previous commit.
Chris Jones <christian.jones@sri.com>
parents:
81
diff
changeset
|
481 |
buf := bytes.NewBuffer(nil) |
7ce2432dd66a
Implement (untested) EXTERNAL auth, and fix bugs from my previous commit.
Chris Jones <christian.jones@sri.com>
parents:
81
diff
changeset
|
482 |
xml.Marshal(buf, fe) |
7ce2432dd66a
Implement (untested) EXTERNAL auth, and fix bugs from my previous commit.
Chris Jones <christian.jones@sri.com>
parents:
81
diff
changeset
|
483 |
Log.Printf("No supported mechanisms: %s", |
7ce2432dd66a
Implement (untested) EXTERNAL auth, and fix bugs from my previous commit.
Chris Jones <christian.jones@sri.com>
parents:
81
diff
changeset
|
484 |
buf.String()) |
7ce2432dd66a
Implement (untested) EXTERNAL auth, and fix bugs from my previous commit.
Chris Jones <christian.jones@sri.com>
parents:
81
diff
changeset
|
485 |
} |
7ce2432dd66a
Implement (untested) EXTERNAL auth, and fix bugs from my previous commit.
Chris Jones <christian.jones@sri.com>
parents:
81
diff
changeset
|
486 |
abort := Generic{XMLName: xml.Name{Local: "abort", |
7ce2432dd66a
Implement (untested) EXTERNAL auth, and fix bugs from my previous commit.
Chris Jones <christian.jones@sri.com>
parents:
81
diff
changeset
|
487 |
Space: NsSASL}} |
7ce2432dd66a
Implement (untested) EXTERNAL auth, and fix bugs from my previous commit.
Chris Jones <christian.jones@sri.com>
parents:
81
diff
changeset
|
488 |
cl.xmlOut <- abort |
7ce2432dd66a
Implement (untested) EXTERNAL auth, and fix bugs from my previous commit.
Chris Jones <christian.jones@sri.com>
parents:
81
diff
changeset
|
489 |
se := streamError{Any: Generic{XMLName: |
7ce2432dd66a
Implement (untested) EXTERNAL auth, and fix bugs from my previous commit.
Chris Jones <christian.jones@sri.com>
parents:
81
diff
changeset
|
490 |
xml.Name{Local: "undefined-condition", |
7ce2432dd66a
Implement (untested) EXTERNAL auth, and fix bugs from my previous commit.
Chris Jones <christian.jones@sri.com>
parents:
81
diff
changeset
|
491 |
Space: NsStreams}}, Text: |
7ce2432dd66a
Implement (untested) EXTERNAL auth, and fix bugs from my previous commit.
Chris Jones <christian.jones@sri.com>
parents:
81
diff
changeset
|
492 |
&errText{Lang: "en", Text: "No supported mechs"}} |
7ce2432dd66a
Implement (untested) EXTERNAL auth, and fix bugs from my previous commit.
Chris Jones <christian.jones@sri.com>
parents:
81
diff
changeset
|
493 |
cl.xmlOut <- se |
7ce2432dd66a
Implement (untested) EXTERNAL auth, and fix bugs from my previous commit.
Chris Jones <christian.jones@sri.com>
parents:
81
diff
changeset
|
494 |
close(cl.xmlOut) |
11
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
495 |
} |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
496 |
} |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
497 |
|
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
498 |
func (cl *Client) handleSasl(srv *auth) { |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
499 |
switch strings.ToLower(srv.XMLName.Local) { |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
500 |
case "challenge": |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
501 |
b64 := base64.StdEncoding |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
502 |
str, err := b64.DecodeString(srv.Chardata) |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
503 |
if err != nil { |
62
6e2eea62ccca
Added global variables for logging.
Chris Jones <chris@cjones.org>
parents:
61
diff
changeset
|
504 |
if Log != nil { |
76
caa722ab8a0f
Fixed logging to use log rather than syslog.
Chris Jones <christian.jones@sri.com>
parents:
75
diff
changeset
|
505 |
Log.Println("SASL challenge decode: " + |
74
e619e18dcec3
Ran gofix from weekly-2012-01-15.
Chris Jones <christian.jones@sri.com>
parents:
72
diff
changeset
|
506 |
err.Error()) |
62
6e2eea62ccca
Added global variables for logging.
Chris Jones <chris@cjones.org>
parents:
61
diff
changeset
|
507 |
} |
72 | 508 |
return |
11
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 |
srvMap := parseSasl(string(str)) |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
511 |
|
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
512 |
if cl.saslExpected == "" { |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
513 |
cl.saslDigest1(srvMap) |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
514 |
} else { |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
515 |
cl.saslDigest2(srvMap) |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
516 |
} |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
517 |
case "failure": |
76
caa722ab8a0f
Fixed logging to use log rather than syslog.
Chris Jones <christian.jones@sri.com>
parents:
75
diff
changeset
|
518 |
if Log != nil && Loglevel >= syslog.LOG_NOTICE { |
caa722ab8a0f
Fixed logging to use log rather than syslog.
Chris Jones <christian.jones@sri.com>
parents:
75
diff
changeset
|
519 |
Log.Println("SASL authentication failed") |
62
6e2eea62ccca
Added global variables for logging.
Chris Jones <chris@cjones.org>
parents:
61
diff
changeset
|
520 |
} |
11
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
521 |
case "success": |
76
caa722ab8a0f
Fixed logging to use log rather than syslog.
Chris Jones <christian.jones@sri.com>
parents:
75
diff
changeset
|
522 |
if Log != nil && Loglevel >= syslog.LOG_INFO { |
caa722ab8a0f
Fixed logging to use log rather than syslog.
Chris Jones <christian.jones@sri.com>
parents:
75
diff
changeset
|
523 |
Log.Println("Sasl authentication succeeded") |
62
6e2eea62ccca
Added global variables for logging.
Chris Jones <chris@cjones.org>
parents:
61
diff
changeset
|
524 |
} |
32
4e68d8f89dc3
Make the server's advertised features available to the app.
Chris Jones <chris@cjones.org>
parents:
31
diff
changeset
|
525 |
cl.Features = nil |
82
7ce2432dd66a
Implement (untested) EXTERNAL auth, and fix bugs from my previous commit.
Chris Jones <christian.jones@sri.com>
parents:
81
diff
changeset
|
526 |
cl.xmlOut <- openStream(&cl.Jid) |
11
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
527 |
} |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
528 |
} |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
529 |
|
72 | 530 |
func (cl *Client) saslDigest1(srvMap map[string]string) { |
11
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
531 |
// Make sure it supports qop=auth |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
532 |
var hasAuth bool |
72 | 533 |
for _, qop := range strings.Fields(srvMap["qop"]) { |
11
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
534 |
if qop == "auth" { |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
535 |
hasAuth = true |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
536 |
} |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
537 |
} |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
538 |
if !hasAuth { |
62
6e2eea62ccca
Added global variables for logging.
Chris Jones <chris@cjones.org>
parents:
61
diff
changeset
|
539 |
if Log != nil { |
76
caa722ab8a0f
Fixed logging to use log rather than syslog.
Chris Jones <christian.jones@sri.com>
parents:
75
diff
changeset
|
540 |
Log.Println("Server doesn't support SASL auth") |
62
6e2eea62ccca
Added global variables for logging.
Chris Jones <chris@cjones.org>
parents:
61
diff
changeset
|
541 |
} |
72 | 542 |
return |
11
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
543 |
} |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
544 |
|
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
545 |
// Pick a realm. |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
546 |
var realm string |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
547 |
if srvMap["realm"] != "" { |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
548 |
realm = strings.Fields(srvMap["realm"])[0] |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
549 |
} |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
550 |
|
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
551 |
passwd := cl.password |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
552 |
nonce := srvMap["nonce"] |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
553 |
digestUri := "xmpp/" + cl.Jid.Domain |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
554 |
nonceCount := int32(1) |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
555 |
nonceCountStr := fmt.Sprintf("%08x", nonceCount) |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
556 |
|
82
7ce2432dd66a
Implement (untested) EXTERNAL auth, and fix bugs from my previous commit.
Chris Jones <christian.jones@sri.com>
parents:
81
diff
changeset
|
557 |
// Begin building the response. Username is user (with no |
7ce2432dd66a
Implement (untested) EXTERNAL auth, and fix bugs from my previous commit.
Chris Jones <christian.jones@sri.com>
parents:
81
diff
changeset
|
558 |
// @domain) or just domain. |
11
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
559 |
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
|
560 |
if cl.Jid.Node == "" { |
11
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
561 |
username = cl.Jid.Domain |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
562 |
} else { |
25
7437d6eed227
Made JID.Node a string rather than *string. This is more appropriate
Chris Jones <chris@cjones.org>
parents:
23
diff
changeset
|
563 |
username = cl.Jid.Node |
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 |
|
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
566 |
// Generate our own nonce from random data. |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
567 |
randSize := big.NewInt(0) |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
568 |
randSize.Lsh(big.NewInt(1), 64) |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
569 |
cnonce, err := rand.Int(rand.Reader, randSize) |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
570 |
if err != nil { |
62
6e2eea62ccca
Added global variables for logging.
Chris Jones <chris@cjones.org>
parents:
61
diff
changeset
|
571 |
if Log != nil { |
76
caa722ab8a0f
Fixed logging to use log rather than syslog.
Chris Jones <christian.jones@sri.com>
parents:
75
diff
changeset
|
572 |
Log.Println("SASL rand: " + err.Error()) |
62
6e2eea62ccca
Added global variables for logging.
Chris Jones <chris@cjones.org>
parents:
61
diff
changeset
|
573 |
} |
11
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
574 |
return |
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 |
cnonceStr := fmt.Sprintf("%016x", cnonce) |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
577 |
|
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
578 |
/* Now encode the actual password response, as well as the |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
579 |
* expected next challenge from the server. */ |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
580 |
response := saslDigestResponse(username, realm, passwd, nonce, |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
581 |
cnonceStr, "AUTHENTICATE", digestUri, nonceCountStr) |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
582 |
next := saslDigestResponse(username, realm, passwd, nonce, |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
583 |
cnonceStr, "", digestUri, nonceCountStr) |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
584 |
cl.saslExpected = next |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
585 |
|
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
586 |
// Build the map which will be encoded. |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
587 |
clMap := make(map[string]string) |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
588 |
clMap["realm"] = `"` + realm + `"` |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
589 |
clMap["username"] = `"` + username + `"` |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
590 |
clMap["nonce"] = `"` + nonce + `"` |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
591 |
clMap["cnonce"] = `"` + cnonceStr + `"` |
72 | 592 |
clMap["nc"] = nonceCountStr |
11
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
593 |
clMap["qop"] = "auth" |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
594 |
clMap["digest-uri"] = `"` + digestUri + `"` |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
595 |
clMap["response"] = response |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
596 |
if srvMap["charset"] == "utf-8" { |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
597 |
clMap["charset"] = "utf-8" |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
598 |
} |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
599 |
|
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
600 |
// Encode the map and send it. |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
601 |
clStr := packSasl(clMap) |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
602 |
b64 := base64.StdEncoding |
72 | 603 |
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
|
604 |
cl.xmlOut <- clObj |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
605 |
} |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
606 |
|
72 | 607 |
func (cl *Client) saslDigest2(srvMap map[string]string) { |
11
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
608 |
if cl.saslExpected == srvMap["rspauth"] { |
72 | 609 |
clObj := &auth{XMLName: xml.Name{Space: NsSASL, Local: "response"}} |
11
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
610 |
cl.xmlOut <- clObj |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
611 |
} else { |
72 | 612 |
clObj := &auth{XMLName: xml.Name{Space: NsSASL, Local: "failure"}, Any: &Generic{XMLName: xml.Name{Space: NsSASL, |
613 |
Local: "abort"}}} |
|
11
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
614 |
cl.xmlOut <- clObj |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
615 |
} |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
616 |
} |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
617 |
|
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
618 |
// 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
|
619 |
// key/value map. |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
620 |
func parseSasl(in string) map[string]string { |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
621 |
re := regexp.MustCompile(`([^=]+)="?([^",]+)"?,?`) |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
622 |
strs := re.FindAllStringSubmatch(in, -1) |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
623 |
m := make(map[string]string) |
72 | 624 |
for _, pair := range strs { |
11
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
625 |
key := strings.ToLower(string(pair[1])) |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
626 |
value := string(pair[2]) |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
627 |
m[key] = value |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
628 |
} |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
629 |
return m |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
630 |
} |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
631 |
|
17 | 632 |
// Inverse of parseSasl(). |
11
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
633 |
func packSasl(m map[string]string) string { |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
634 |
var terms []string |
72 | 635 |
for key, value := range m { |
11
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
636 |
if key == "" || value == "" || value == `""` { |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
637 |
continue |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
638 |
} |
72 | 639 |
terms = append(terms, key+"="+value) |
11
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
640 |
} |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
641 |
return strings.Join(terms, ",") |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
642 |
} |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
643 |
|
17 | 644 |
// Computes the response string for digest authentication. |
11
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
645 |
func saslDigestResponse(username, realm, passwd, nonce, cnonceStr, |
74
e619e18dcec3
Ran gofix from weekly-2012-01-15.
Chris Jones <christian.jones@sri.com>
parents:
72
diff
changeset
|
646 |
authenticate, digestUri, nonceCountStr string) string { |
11
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
647 |
h := func(text string) []byte { |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
648 |
h := md5.New() |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
649 |
h.Write([]byte(text)) |
74
e619e18dcec3
Ran gofix from weekly-2012-01-15.
Chris Jones <christian.jones@sri.com>
parents:
72
diff
changeset
|
650 |
return h.Sum(nil) |
11
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
651 |
} |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
652 |
hex := func(bytes []byte) string { |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
653 |
return fmt.Sprintf("%x", bytes) |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
654 |
} |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
655 |
kd := func(secret, data string) []byte { |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
656 |
return h(secret + ":" + data) |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
657 |
} |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
658 |
|
72 | 659 |
a1 := string(h(username+":"+realm+":"+passwd)) + ":" + |
11
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
660 |
nonce + ":" + cnonceStr |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
661 |
a2 := authenticate + ":" + digestUri |
72 | 662 |
response := hex(kd(hex(h(a1)), nonce+":"+ |
663 |
nonceCountStr+":"+cnonceStr+":auth:"+ |
|
11
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
664 |
hex(h(a2)))) |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
665 |
return response |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
666 |
} |
12
122ab6208c3c
Added resource binding and structures for <iq>, <message>, and <presence>.
Chris Jones <chris@cjones.org>
parents:
11
diff
changeset
|
667 |
|
17 | 668 |
// 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
|
669 |
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
|
670 |
res := cl.Jid.Resource |
41
c8c9e6a7e6c9
Made a special-purpose bind structure for resource binding.
Chris Jones <chris@cjones.org>
parents:
40
diff
changeset
|
671 |
bindReq := &bindIq{} |
12
122ab6208c3c
Added resource binding and structures for <iq>, <message>, and <presence>.
Chris Jones <chris@cjones.org>
parents:
11
diff
changeset
|
672 |
if res != "" { |
41
c8c9e6a7e6c9
Made a special-purpose bind structure for resource binding.
Chris Jones <chris@cjones.org>
parents:
40
diff
changeset
|
673 |
bindReq.Resource = &res |
12
122ab6208c3c
Added resource binding and structures for <iq>, <message>, and <presence>.
Chris Jones <chris@cjones.org>
parents:
11
diff
changeset
|
674 |
} |
72 | 675 |
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
|
676 |
f := func(st Stanza) bool { |
42
f6bb47ca12f2
Renamed the somewhat obscure XTo(), etc. to GetTo(), etc.
Chris Jones <chris@cjones.org>
parents:
41
diff
changeset
|
677 |
if st.GetType() == "error" { |
62
6e2eea62ccca
Added global variables for logging.
Chris Jones <chris@cjones.org>
parents:
61
diff
changeset
|
678 |
if Log != nil { |
76
caa722ab8a0f
Fixed logging to use log rather than syslog.
Chris Jones <christian.jones@sri.com>
parents:
75
diff
changeset
|
679 |
Log.Println("Resource binding failed") |
62
6e2eea62ccca
Added global variables for logging.
Chris Jones <chris@cjones.org>
parents:
61
diff
changeset
|
680 |
} |
15
aa2cf77f0ed3
When the server sends us our newly bound resource, update Client.Jid
Chris Jones <chris@cjones.org>
parents:
14
diff
changeset
|
681 |
return false |
aa2cf77f0ed3
When the server sends us our newly bound resource, update Client.Jid
Chris Jones <chris@cjones.org>
parents:
14
diff
changeset
|
682 |
} |
61
16513974d273
Stanzas can now contain multiple nested (extended) elements.
Chris Jones <chris@cjones.org>
parents:
59
diff
changeset
|
683 |
var bindRepl *bindIq |
72 | 684 |
for _, ele := range st.GetNested() { |
685 |
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
|
686 |
bindRepl = b |
16513974d273
Stanzas can now contain multiple nested (extended) elements.
Chris Jones <chris@cjones.org>
parents:
59
diff
changeset
|
687 |
break |
16513974d273
Stanzas can now contain multiple nested (extended) elements.
Chris Jones <chris@cjones.org>
parents:
59
diff
changeset
|
688 |
} |
16513974d273
Stanzas can now contain multiple nested (extended) elements.
Chris Jones <chris@cjones.org>
parents:
59
diff
changeset
|
689 |
} |
16513974d273
Stanzas can now contain multiple nested (extended) elements.
Chris Jones <chris@cjones.org>
parents:
59
diff
changeset
|
690 |
if bindRepl == nil { |
62
6e2eea62ccca
Added global variables for logging.
Chris Jones <chris@cjones.org>
parents:
61
diff
changeset
|
691 |
if Log != nil { |
76
caa722ab8a0f
Fixed logging to use log rather than syslog.
Chris Jones <christian.jones@sri.com>
parents:
75
diff
changeset
|
692 |
Log.Printf("Bad bind reply: %v", st) |
62
6e2eea62ccca
Added global variables for logging.
Chris Jones <chris@cjones.org>
parents:
61
diff
changeset
|
693 |
} |
15
aa2cf77f0ed3
When the server sends us our newly bound resource, update Client.Jid
Chris Jones <chris@cjones.org>
parents:
14
diff
changeset
|
694 |
return false |
aa2cf77f0ed3
When the server sends us our newly bound resource, update Client.Jid
Chris Jones <chris@cjones.org>
parents:
14
diff
changeset
|
695 |
} |
41
c8c9e6a7e6c9
Made a special-purpose bind structure for resource binding.
Chris Jones <chris@cjones.org>
parents:
40
diff
changeset
|
696 |
jidStr := bindRepl.Jid |
c8c9e6a7e6c9
Made a special-purpose bind structure for resource binding.
Chris Jones <chris@cjones.org>
parents:
40
diff
changeset
|
697 |
if jidStr == nil || *jidStr == "" { |
62
6e2eea62ccca
Added global variables for logging.
Chris Jones <chris@cjones.org>
parents:
61
diff
changeset
|
698 |
if Log != nil { |
76
caa722ab8a0f
Fixed logging to use log rather than syslog.
Chris Jones <christian.jones@sri.com>
parents:
75
diff
changeset
|
699 |
Log.Println("Can't bind empty resource") |
62
6e2eea62ccca
Added global variables for logging.
Chris Jones <chris@cjones.org>
parents:
61
diff
changeset
|
700 |
} |
15
aa2cf77f0ed3
When the server sends us our newly bound resource, update Client.Jid
Chris Jones <chris@cjones.org>
parents:
14
diff
changeset
|
701 |
return false |
aa2cf77f0ed3
When the server sends us our newly bound resource, update Client.Jid
Chris Jones <chris@cjones.org>
parents:
14
diff
changeset
|
702 |
} |
aa2cf77f0ed3
When the server sends us our newly bound resource, update Client.Jid
Chris Jones <chris@cjones.org>
parents:
14
diff
changeset
|
703 |
jid := new(JID) |
75
03a923eb5c01
Updated for weekly.2012-01-15.
Chris Jones <christian.jones@sri.com>
parents:
74
diff
changeset
|
704 |
if err := jid.Set(*jidStr); err != nil { |
62
6e2eea62ccca
Added global variables for logging.
Chris Jones <chris@cjones.org>
parents:
61
diff
changeset
|
705 |
if Log != nil { |
76
caa722ab8a0f
Fixed logging to use log rather than syslog.
Chris Jones <christian.jones@sri.com>
parents:
75
diff
changeset
|
706 |
Log.Println(err.Error()) |
62
6e2eea62ccca
Added global variables for logging.
Chris Jones <chris@cjones.org>
parents:
61
diff
changeset
|
707 |
} |
15
aa2cf77f0ed3
When the server sends us our newly bound resource, update Client.Jid
Chris Jones <chris@cjones.org>
parents:
14
diff
changeset
|
708 |
return false |
aa2cf77f0ed3
When the server sends us our newly bound resource, update Client.Jid
Chris Jones <chris@cjones.org>
parents:
14
diff
changeset
|
709 |
} |
aa2cf77f0ed3
When the server sends us our newly bound resource, update Client.Jid
Chris Jones <chris@cjones.org>
parents:
14
diff
changeset
|
710 |
cl.Jid = *jid |
76
caa722ab8a0f
Fixed logging to use log rather than syslog.
Chris Jones <christian.jones@sri.com>
parents:
75
diff
changeset
|
711 |
if Log != nil && Loglevel >= syslog.LOG_INFO { |
caa722ab8a0f
Fixed logging to use log rather than syslog.
Chris Jones <christian.jones@sri.com>
parents:
75
diff
changeset
|
712 |
Log.Println("Bound resource: " + cl.Jid.String()) |
62
6e2eea62ccca
Added global variables for logging.
Chris Jones <chris@cjones.org>
parents:
61
diff
changeset
|
713 |
} |
29
a456133ed0ac
Don't accept data on Client.Out until resource binding is
Chris Jones <chris@cjones.org>
parents:
26
diff
changeset
|
714 |
cl.bindDone() |
a456133ed0ac
Don't accept data on Client.Out until resource binding is
Chris Jones <chris@cjones.org>
parents:
26
diff
changeset
|
715 |
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
|
716 |
} |
aa2cf77f0ed3
When the server sends us our newly bound resource, update Client.Jid
Chris Jones <chris@cjones.org>
parents:
14
diff
changeset
|
717 |
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
|
718 |
cl.xmlOut <- msg |
122ab6208c3c
Added resource binding and structures for <iq>, <message>, and <presence>.
Chris Jones <chris@cjones.org>
parents:
11
diff
changeset
|
719 |
} |
13
c9527bbe99a6
Added a callback handler which will handle a stanza with a particular id.
Chris Jones <chris@cjones.org>
parents:
12
diff
changeset
|
720 |
|
17 | 721 |
// Register a callback to handle the next XMPP stanza (iq, message, or |
722 |
// presence) with a given id. The provided function will not be called |
|
723 |
// 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
|
724 |
// 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
|
725 |
// 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
|
726 |
// 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
|
727 |
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
|
728 |
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
|
729 |
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
|
730 |
} |