author | Chris Jones <christian.jones@sri.com> |
Sat, 31 Aug 2013 23:06:55 +0100 | |
changeset 119 | 712aa5780660 |
parent 116 | 5c6d6d51d3ba |
child 124 | 34e917ca6a11 |
permissions | -rw-r--r-- |
4
a8fbec71a194
Added an interactive test and made Client implement io.Closer.
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
1 |
// Copyright 2011 The Go Authors. All rights reserved. |
a8fbec71a194
Added an interactive test and made Client implement io.Closer.
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
2 |
// Use of this source code is governed by a BSD-style |
a8fbec71a194
Added an interactive test and made Client implement io.Closer.
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
3 |
// license that can be found in the LICENSE file. |
a8fbec71a194
Added an interactive test and made Client implement io.Closer.
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
4 |
|
a8fbec71a194
Added an interactive test and made Client implement io.Closer.
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
5 |
package main |
a8fbec71a194
Added an interactive test and made Client implement io.Closer.
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
6 |
|
a8fbec71a194
Added an interactive test and made Client implement io.Closer.
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
7 |
import ( |
100 | 8 |
xmpp ".." |
105
aa895dfae3f6
Allow the user to override the TLS config. Also fixed up some log statements.
Chris Jones <christian.jones@sri.com>
parents:
103
diff
changeset
|
9 |
"crypto/tls" |
110
7696e6a01709
Instead of making Stanza an interface that Iq, Message, and Presence implement, change it to an embedded struct.
Chris Jones <chris@cjones.org>
parents:
109
diff
changeset
|
10 |
"encoding/xml" |
4
a8fbec71a194
Added an interactive test and made Client implement io.Closer.
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
11 |
"flag" |
6
8e425e340ca1
Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents:
4
diff
changeset
|
12 |
"fmt" |
4
a8fbec71a194
Added an interactive test and made Client implement io.Closer.
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
13 |
"log" |
a8fbec71a194
Added an interactive test and made Client implement io.Closer.
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
14 |
"os" |
110
7696e6a01709
Instead of making Stanza an interface that Iq, Message, and Presence implement, change it to an embedded struct.
Chris Jones <chris@cjones.org>
parents:
109
diff
changeset
|
15 |
"strings" |
63
c7f2edd25f4a
Intermediate commit. Fixing how we close our channels and sockets and shut down our goroutines.
Chris Jones <chris@cjones.org>
parents:
57
diff
changeset
|
16 |
) |
4
a8fbec71a194
Added an interactive test and made Client implement io.Closer.
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
17 |
|
103
f27f78706623
Updated the example for the new log setup.
Chris Jones <christian.jones@sri.com>
parents:
101
diff
changeset
|
18 |
type StdLogger struct { |
f27f78706623
Updated the example for the new log setup.
Chris Jones <christian.jones@sri.com>
parents:
101
diff
changeset
|
19 |
} |
f27f78706623
Updated the example for the new log setup.
Chris Jones <christian.jones@sri.com>
parents:
101
diff
changeset
|
20 |
|
f27f78706623
Updated the example for the new log setup.
Chris Jones <christian.jones@sri.com>
parents:
101
diff
changeset
|
21 |
func (s *StdLogger) Log(v ...interface{}) { |
110
7696e6a01709
Instead of making Stanza an interface that Iq, Message, and Presence implement, change it to an embedded struct.
Chris Jones <chris@cjones.org>
parents:
109
diff
changeset
|
22 |
log.Println(v...) |
103
f27f78706623
Updated the example for the new log setup.
Chris Jones <christian.jones@sri.com>
parents:
101
diff
changeset
|
23 |
} |
f27f78706623
Updated the example for the new log setup.
Chris Jones <christian.jones@sri.com>
parents:
101
diff
changeset
|
24 |
|
f27f78706623
Updated the example for the new log setup.
Chris Jones <christian.jones@sri.com>
parents:
101
diff
changeset
|
25 |
func (s *StdLogger) Logf(fmt string, v ...interface{}) { |
110
7696e6a01709
Instead of making Stanza an interface that Iq, Message, and Presence implement, change it to an embedded struct.
Chris Jones <chris@cjones.org>
parents:
109
diff
changeset
|
26 |
log.Printf(fmt, v...) |
103
f27f78706623
Updated the example for the new log setup.
Chris Jones <christian.jones@sri.com>
parents:
101
diff
changeset
|
27 |
} |
f27f78706623
Updated the example for the new log setup.
Chris Jones <christian.jones@sri.com>
parents:
101
diff
changeset
|
28 |
|
101
5d721a565503
Use the new logging setup from the example program.
Chris Jones <christian.jones@sri.com>
parents:
100
diff
changeset
|
29 |
func init() { |
103
f27f78706623
Updated the example for the new log setup.
Chris Jones <christian.jones@sri.com>
parents:
101
diff
changeset
|
30 |
logger := &StdLogger{} |
109 | 31 |
// xmpp.Debug = logger |
103
f27f78706623
Updated the example for the new log setup.
Chris Jones <christian.jones@sri.com>
parents:
101
diff
changeset
|
32 |
xmpp.Info = logger |
f27f78706623
Updated the example for the new log setup.
Chris Jones <christian.jones@sri.com>
parents:
101
diff
changeset
|
33 |
xmpp.Warn = logger |
105
aa895dfae3f6
Allow the user to override the TLS config. Also fixed up some log statements.
Chris Jones <christian.jones@sri.com>
parents:
103
diff
changeset
|
34 |
|
aa895dfae3f6
Allow the user to override the TLS config. Also fixed up some log statements.
Chris Jones <christian.jones@sri.com>
parents:
103
diff
changeset
|
35 |
xmpp.TlsConfig = tls.Config{InsecureSkipVerify: true} |
101
5d721a565503
Use the new logging setup from the example program.
Chris Jones <christian.jones@sri.com>
parents:
100
diff
changeset
|
36 |
} |
5d721a565503
Use the new logging setup from the example program.
Chris Jones <christian.jones@sri.com>
parents:
100
diff
changeset
|
37 |
|
4
a8fbec71a194
Added an interactive test and made Client implement io.Closer.
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
38 |
// Demonstrate the API, and allow the user to interact with an XMPP |
a8fbec71a194
Added an interactive test and made Client implement io.Closer.
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
39 |
// server via the terminal. |
a8fbec71a194
Added an interactive test and made Client implement io.Closer.
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
40 |
func main() { |
a8fbec71a194
Added an interactive test and made Client implement io.Closer.
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
41 |
var jid xmpp.JID |
a8fbec71a194
Added an interactive test and made Client implement io.Closer.
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
42 |
flag.Var(&jid, "jid", "JID to log in as") |
a8fbec71a194
Added an interactive test and made Client implement io.Closer.
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
43 |
var pw *string = flag.String("pw", "", "password") |
a8fbec71a194
Added an interactive test and made Client implement io.Closer.
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
44 |
flag.Parse() |
a8fbec71a194
Added an interactive test and made Client implement io.Closer.
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
45 |
if jid.Domain == "" || *pw == "" { |
a8fbec71a194
Added an interactive test and made Client implement io.Closer.
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
46 |
flag.Usage() |
a8fbec71a194
Added an interactive test and made Client implement io.Closer.
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
47 |
os.Exit(2) |
a8fbec71a194
Added an interactive test and made Client implement io.Closer.
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
48 |
} |
a8fbec71a194
Added an interactive test and made Client implement io.Closer.
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
49 |
|
36
9fe022261dcc
Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
33
diff
changeset
|
50 |
c, err := xmpp.NewClient(&jid, *pw, nil) |
4
a8fbec71a194
Added an interactive test and made Client implement io.Closer.
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
51 |
if err != nil { |
a8fbec71a194
Added an interactive test and made Client implement io.Closer.
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
52 |
log.Fatalf("NewClient(%v): %v", jid, err) |
a8fbec71a194
Added an interactive test and made Client implement io.Closer.
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
53 |
} |
63
c7f2edd25f4a
Intermediate commit. Fixing how we close our channels and sockets and shut down our goroutines.
Chris Jones <chris@cjones.org>
parents:
57
diff
changeset
|
54 |
defer close(c.Out) |
6
8e425e340ca1
Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents:
4
diff
changeset
|
55 |
|
33
571713f49494
Added roster retrieval to StartSession().
Chris Jones <chris@cjones.org>
parents:
29
diff
changeset
|
56 |
err = c.StartSession(true, &xmpp.Presence{}) |
29
a456133ed0ac
Don't accept data on Client.Out until resource binding is
Chris Jones <chris@cjones.org>
parents:
26
diff
changeset
|
57 |
if err != nil { |
a456133ed0ac
Don't accept data on Client.Out until resource binding is
Chris Jones <chris@cjones.org>
parents:
26
diff
changeset
|
58 |
log.Fatalf("StartSession: %v", err) |
a456133ed0ac
Don't accept data on Client.Out until resource binding is
Chris Jones <chris@cjones.org>
parents:
26
diff
changeset
|
59 |
} |
57
e6cb3f049137
Revamped how the roster works. We're now using a channel to transmit snapshots
Chris Jones <chris@cjones.org>
parents:
36
diff
changeset
|
60 |
roster := xmpp.Roster(c) |
33
571713f49494
Added roster retrieval to StartSession().
Chris Jones <chris@cjones.org>
parents:
29
diff
changeset
|
61 |
fmt.Printf("%d roster entries:\n", len(roster)) |
116 | 62 |
for i, entry := range roster { |
57
e6cb3f049137
Revamped how the roster works. We're now using a channel to transmit snapshots
Chris Jones <chris@cjones.org>
parents:
36
diff
changeset
|
63 |
fmt.Printf("%d: %v\n", i, entry) |
33
571713f49494
Added roster retrieval to StartSession().
Chris Jones <chris@cjones.org>
parents:
29
diff
changeset
|
64 |
} |
29
a456133ed0ac
Don't accept data on Client.Out until resource binding is
Chris Jones <chris@cjones.org>
parents:
26
diff
changeset
|
65 |
|
113
bee6cc131798
Step 3 of converting to interface Stanza and embedded struct Header.
Chris Jones <chris@cjones.org>
parents:
110
diff
changeset
|
66 |
go func(ch <-chan xmpp.Stanza) { |
6
8e425e340ca1
Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents:
4
diff
changeset
|
67 |
for obj := range ch { |
8e425e340ca1
Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents:
4
diff
changeset
|
68 |
fmt.Printf("s: %v\n", obj) |
8e425e340ca1
Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents:
4
diff
changeset
|
69 |
} |
8e425e340ca1
Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents:
4
diff
changeset
|
70 |
fmt.Println("done reading") |
8e425e340ca1
Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents:
4
diff
changeset
|
71 |
}(c.In) |
8e425e340ca1
Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents:
4
diff
changeset
|
72 |
|
68
d693ecc11f29
Restore this example program to its normal operation.
Chris Jones <chris@cjones.org>
parents:
63
diff
changeset
|
73 |
p := make([]byte, 1024) |
d693ecc11f29
Restore this example program to its normal operation.
Chris Jones <chris@cjones.org>
parents:
63
diff
changeset
|
74 |
for { |
d693ecc11f29
Restore this example program to its normal operation.
Chris Jones <chris@cjones.org>
parents:
63
diff
changeset
|
75 |
nr, _ := os.Stdin.Read(p) |
d693ecc11f29
Restore this example program to its normal operation.
Chris Jones <chris@cjones.org>
parents:
63
diff
changeset
|
76 |
if nr == 0 { |
d693ecc11f29
Restore this example program to its normal operation.
Chris Jones <chris@cjones.org>
parents:
63
diff
changeset
|
77 |
break |
d693ecc11f29
Restore this example program to its normal operation.
Chris Jones <chris@cjones.org>
parents:
63
diff
changeset
|
78 |
} |
d693ecc11f29
Restore this example program to its normal operation.
Chris Jones <chris@cjones.org>
parents:
63
diff
changeset
|
79 |
s := string(p) |
110
7696e6a01709
Instead of making Stanza an interface that Iq, Message, and Presence implement, change it to an embedded struct.
Chris Jones <chris@cjones.org>
parents:
109
diff
changeset
|
80 |
dec := xml.NewDecoder(strings.NewReader(s)) |
7696e6a01709
Instead of making Stanza an interface that Iq, Message, and Presence implement, change it to an embedded struct.
Chris Jones <chris@cjones.org>
parents:
109
diff
changeset
|
81 |
t, err := dec.Token() |
7696e6a01709
Instead of making Stanza an interface that Iq, Message, and Presence implement, change it to an embedded struct.
Chris Jones <chris@cjones.org>
parents:
109
diff
changeset
|
82 |
if err != nil { |
7696e6a01709
Instead of making Stanza an interface that Iq, Message, and Presence implement, change it to an embedded struct.
Chris Jones <chris@cjones.org>
parents:
109
diff
changeset
|
83 |
fmt.Printf("token: %s\n", err) |
7696e6a01709
Instead of making Stanza an interface that Iq, Message, and Presence implement, change it to an embedded struct.
Chris Jones <chris@cjones.org>
parents:
109
diff
changeset
|
84 |
break |
7696e6a01709
Instead of making Stanza an interface that Iq, Message, and Presence implement, change it to an embedded struct.
Chris Jones <chris@cjones.org>
parents:
109
diff
changeset
|
85 |
} |
7696e6a01709
Instead of making Stanza an interface that Iq, Message, and Presence implement, change it to an embedded struct.
Chris Jones <chris@cjones.org>
parents:
109
diff
changeset
|
86 |
var se *xml.StartElement |
7696e6a01709
Instead of making Stanza an interface that Iq, Message, and Presence implement, change it to an embedded struct.
Chris Jones <chris@cjones.org>
parents:
109
diff
changeset
|
87 |
var ok bool |
116 | 88 |
if se, ok = t.(*xml.StartElement); !ok { |
110
7696e6a01709
Instead of making Stanza an interface that Iq, Message, and Presence implement, change it to an embedded struct.
Chris Jones <chris@cjones.org>
parents:
109
diff
changeset
|
89 |
fmt.Println("Couldn't find start element") |
7696e6a01709
Instead of making Stanza an interface that Iq, Message, and Presence implement, change it to an embedded struct.
Chris Jones <chris@cjones.org>
parents:
109
diff
changeset
|
90 |
break |
7696e6a01709
Instead of making Stanza an interface that Iq, Message, and Presence implement, change it to an embedded struct.
Chris Jones <chris@cjones.org>
parents:
109
diff
changeset
|
91 |
} |
113
bee6cc131798
Step 3 of converting to interface Stanza and embedded struct Header.
Chris Jones <chris@cjones.org>
parents:
110
diff
changeset
|
92 |
var stan xmpp.Stanza |
110
7696e6a01709
Instead of making Stanza an interface that Iq, Message, and Presence implement, change it to an embedded struct.
Chris Jones <chris@cjones.org>
parents:
109
diff
changeset
|
93 |
switch se.Name.Local { |
7696e6a01709
Instead of making Stanza an interface that Iq, Message, and Presence implement, change it to an embedded struct.
Chris Jones <chris@cjones.org>
parents:
109
diff
changeset
|
94 |
case "iq": |
7696e6a01709
Instead of making Stanza an interface that Iq, Message, and Presence implement, change it to an embedded struct.
Chris Jones <chris@cjones.org>
parents:
109
diff
changeset
|
95 |
stan = &xmpp.Iq{} |
7696e6a01709
Instead of making Stanza an interface that Iq, Message, and Presence implement, change it to an embedded struct.
Chris Jones <chris@cjones.org>
parents:
109
diff
changeset
|
96 |
case "message": |
7696e6a01709
Instead of making Stanza an interface that Iq, Message, and Presence implement, change it to an embedded struct.
Chris Jones <chris@cjones.org>
parents:
109
diff
changeset
|
97 |
stan = &xmpp.Message{} |
7696e6a01709
Instead of making Stanza an interface that Iq, Message, and Presence implement, change it to an embedded struct.
Chris Jones <chris@cjones.org>
parents:
109
diff
changeset
|
98 |
case "presence": |
7696e6a01709
Instead of making Stanza an interface that Iq, Message, and Presence implement, change it to an embedded struct.
Chris Jones <chris@cjones.org>
parents:
109
diff
changeset
|
99 |
stan = &xmpp.Presence{} |
7696e6a01709
Instead of making Stanza an interface that Iq, Message, and Presence implement, change it to an embedded struct.
Chris Jones <chris@cjones.org>
parents:
109
diff
changeset
|
100 |
default: |
7696e6a01709
Instead of making Stanza an interface that Iq, Message, and Presence implement, change it to an embedded struct.
Chris Jones <chris@cjones.org>
parents:
109
diff
changeset
|
101 |
fmt.Println("Can't parse non-stanza.") |
7696e6a01709
Instead of making Stanza an interface that Iq, Message, and Presence implement, change it to an embedded struct.
Chris Jones <chris@cjones.org>
parents:
109
diff
changeset
|
102 |
continue |
7696e6a01709
Instead of making Stanza an interface that Iq, Message, and Presence implement, change it to an embedded struct.
Chris Jones <chris@cjones.org>
parents:
109
diff
changeset
|
103 |
} |
7696e6a01709
Instead of making Stanza an interface that Iq, Message, and Presence implement, change it to an embedded struct.
Chris Jones <chris@cjones.org>
parents:
109
diff
changeset
|
104 |
err = dec.Decode(stan) |
68
d693ecc11f29
Restore this example program to its normal operation.
Chris Jones <chris@cjones.org>
parents:
63
diff
changeset
|
105 |
if err == nil { |
d693ecc11f29
Restore this example program to its normal operation.
Chris Jones <chris@cjones.org>
parents:
63
diff
changeset
|
106 |
c.Out <- stan |
d693ecc11f29
Restore this example program to its normal operation.
Chris Jones <chris@cjones.org>
parents:
63
diff
changeset
|
107 |
} else { |
d693ecc11f29
Restore this example program to its normal operation.
Chris Jones <chris@cjones.org>
parents:
63
diff
changeset
|
108 |
fmt.Printf("Parse error: %v\n", err) |
d693ecc11f29
Restore this example program to its normal operation.
Chris Jones <chris@cjones.org>
parents:
63
diff
changeset
|
109 |
break |
d693ecc11f29
Restore this example program to its normal operation.
Chris Jones <chris@cjones.org>
parents:
63
diff
changeset
|
110 |
} |
d693ecc11f29
Restore this example program to its normal operation.
Chris Jones <chris@cjones.org>
parents:
63
diff
changeset
|
111 |
} |
d693ecc11f29
Restore this example program to its normal operation.
Chris Jones <chris@cjones.org>
parents:
63
diff
changeset
|
112 |
fmt.Println("done sending") |
4
a8fbec71a194
Added an interactive test and made Client implement io.Closer.
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
113 |
} |