author | Chris Jones <christian.jones@sri.com> |
Sat, 28 Sep 2013 13:03:51 -0600 | |
changeset 154 | cd9e0ddbd5e9 |
parent 153 | bbd4166df95d |
child 159 | fd4089f717b2 |
permissions | -rw-r--r-- |
125
f464f14e39a7
Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
1 |
package main |
f464f14e39a7
Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
2 |
|
f464f14e39a7
Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
3 |
import ( |
130
da6f37ae3ffe
Pass the TLS config as a parameter to the Client constructor. Updated the example program.
Chris Jones <christian.jones@sri.com>
parents:
125
diff
changeset
|
4 |
"../xmpp" |
125
f464f14e39a7
Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
5 |
"crypto/tls" |
f464f14e39a7
Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
6 |
"encoding/xml" |
f464f14e39a7
Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
7 |
"flag" |
f464f14e39a7
Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
8 |
"fmt" |
f464f14e39a7
Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
9 |
"log" |
f464f14e39a7
Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
10 |
"os" |
f464f14e39a7
Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
11 |
"strings" |
f464f14e39a7
Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
12 |
) |
f464f14e39a7
Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
13 |
|
f464f14e39a7
Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
14 |
type StdLogger struct { |
f464f14e39a7
Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
15 |
} |
f464f14e39a7
Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
16 |
|
f464f14e39a7
Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
17 |
func (s *StdLogger) Log(v ...interface{}) { |
f464f14e39a7
Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
18 |
log.Println(v...) |
f464f14e39a7
Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
19 |
} |
f464f14e39a7
Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
20 |
|
f464f14e39a7
Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
21 |
func (s *StdLogger) Logf(fmt string, v ...interface{}) { |
f464f14e39a7
Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
22 |
log.Printf(fmt, v...) |
f464f14e39a7
Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
23 |
} |
f464f14e39a7
Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
24 |
|
f464f14e39a7
Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
25 |
func init() { |
f464f14e39a7
Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
26 |
logger := &StdLogger{} |
f464f14e39a7
Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
27 |
// xmpp.Debug = logger |
f464f14e39a7
Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
28 |
xmpp.Info = logger |
f464f14e39a7
Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
29 |
xmpp.Warn = logger |
f464f14e39a7
Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
30 |
} |
f464f14e39a7
Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
31 |
|
f464f14e39a7
Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
32 |
// Demonstrate the API, and allow the user to interact with an XMPP |
f464f14e39a7
Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
33 |
// server via the terminal. |
f464f14e39a7
Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
34 |
func main() { |
f464f14e39a7
Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
35 |
var jid xmpp.JID |
f464f14e39a7
Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
36 |
flag.Var(&jid, "jid", "JID to log in as") |
f464f14e39a7
Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
37 |
var pw *string = flag.String("pw", "", "password") |
f464f14e39a7
Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
38 |
flag.Parse() |
f464f14e39a7
Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
39 |
if jid.Domain == "" || *pw == "" { |
f464f14e39a7
Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
40 |
flag.Usage() |
f464f14e39a7
Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
41 |
os.Exit(2) |
f464f14e39a7
Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
42 |
} |
f464f14e39a7
Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
43 |
|
130
da6f37ae3ffe
Pass the TLS config as a parameter to the Client constructor. Updated the example program.
Chris Jones <christian.jones@sri.com>
parents:
125
diff
changeset
|
44 |
tlsConf := tls.Config{InsecureSkipVerify: true} |
153
bbd4166df95d
Simplified the API: There's only one constructor, and it does everything necessary to initiate the stream. StartSession() and Roster.Update() have both been eliminated.
Chris Jones <christian.jones@sri.com>
parents:
142
diff
changeset
|
45 |
c, err := xmpp.NewClient(&jid, *pw, tlsConf, nil, xmpp.Presence{}, nil) |
125
f464f14e39a7
Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
46 |
if err != nil { |
f464f14e39a7
Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
47 |
log.Fatalf("NewClient(%v): %v", jid, err) |
f464f14e39a7
Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
48 |
} |
130
da6f37ae3ffe
Pass the TLS config as a parameter to the Client constructor. Updated the example program.
Chris Jones <christian.jones@sri.com>
parents:
125
diff
changeset
|
49 |
defer close(c.Send) |
125
f464f14e39a7
Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
50 |
|
137
c94a7ce0f4fb
Start reading from the recv channel earlier.
Chris Jones <christian.jones@sri.com>
parents:
130
diff
changeset
|
51 |
go func(ch <-chan xmpp.Stanza) { |
c94a7ce0f4fb
Start reading from the recv channel earlier.
Chris Jones <christian.jones@sri.com>
parents:
130
diff
changeset
|
52 |
for obj := range ch { |
c94a7ce0f4fb
Start reading from the recv channel earlier.
Chris Jones <christian.jones@sri.com>
parents:
130
diff
changeset
|
53 |
fmt.Printf("s: %v\n", obj) |
c94a7ce0f4fb
Start reading from the recv channel earlier.
Chris Jones <christian.jones@sri.com>
parents:
130
diff
changeset
|
54 |
} |
c94a7ce0f4fb
Start reading from the recv channel earlier.
Chris Jones <christian.jones@sri.com>
parents:
130
diff
changeset
|
55 |
fmt.Println("done reading") |
c94a7ce0f4fb
Start reading from the recv channel earlier.
Chris Jones <christian.jones@sri.com>
parents:
130
diff
changeset
|
56 |
}(c.Recv) |
c94a7ce0f4fb
Start reading from the recv channel earlier.
Chris Jones <christian.jones@sri.com>
parents:
130
diff
changeset
|
57 |
|
125
f464f14e39a7
Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
58 |
roster := c.Roster.Get() |
f464f14e39a7
Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
59 |
fmt.Printf("%d roster entries:\n", len(roster)) |
f464f14e39a7
Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
60 |
for i, entry := range roster { |
f464f14e39a7
Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
61 |
fmt.Printf("%d: %v\n", i, entry) |
f464f14e39a7
Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
62 |
} |
f464f14e39a7
Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
63 |
|
f464f14e39a7
Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
64 |
p := make([]byte, 1024) |
f464f14e39a7
Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
65 |
for { |
f464f14e39a7
Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
66 |
nr, _ := os.Stdin.Read(p) |
f464f14e39a7
Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
67 |
if nr == 0 { |
f464f14e39a7
Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
68 |
break |
f464f14e39a7
Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
69 |
} |
f464f14e39a7
Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
70 |
s := string(p) |
f464f14e39a7
Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
71 |
dec := xml.NewDecoder(strings.NewReader(s)) |
f464f14e39a7
Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
72 |
t, err := dec.Token() |
f464f14e39a7
Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
73 |
if err != nil { |
f464f14e39a7
Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
74 |
fmt.Printf("token: %s\n", err) |
f464f14e39a7
Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
75 |
break |
f464f14e39a7
Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
76 |
} |
f464f14e39a7
Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
77 |
var se *xml.StartElement |
f464f14e39a7
Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
78 |
var ok bool |
f464f14e39a7
Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
79 |
if se, ok = t.(*xml.StartElement); !ok { |
f464f14e39a7
Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
80 |
fmt.Println("Couldn't find start element") |
f464f14e39a7
Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
81 |
break |
f464f14e39a7
Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
82 |
} |
f464f14e39a7
Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
83 |
var stan xmpp.Stanza |
f464f14e39a7
Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
84 |
switch se.Name.Local { |
f464f14e39a7
Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
85 |
case "iq": |
f464f14e39a7
Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
86 |
stan = &xmpp.Iq{} |
f464f14e39a7
Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
87 |
case "message": |
f464f14e39a7
Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
88 |
stan = &xmpp.Message{} |
f464f14e39a7
Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
89 |
case "presence": |
f464f14e39a7
Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
90 |
stan = &xmpp.Presence{} |
f464f14e39a7
Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
91 |
default: |
f464f14e39a7
Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
92 |
fmt.Println("Can't parse non-stanza.") |
f464f14e39a7
Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
93 |
continue |
f464f14e39a7
Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
94 |
} |
f464f14e39a7
Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
95 |
err = dec.Decode(stan) |
f464f14e39a7
Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
96 |
if err == nil { |
130
da6f37ae3ffe
Pass the TLS config as a parameter to the Client constructor. Updated the example program.
Chris Jones <christian.jones@sri.com>
parents:
125
diff
changeset
|
97 |
c.Send <- stan |
125
f464f14e39a7
Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
98 |
} else { |
f464f14e39a7
Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
99 |
fmt.Printf("Parse error: %v\n", err) |
f464f14e39a7
Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
100 |
break |
f464f14e39a7
Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
101 |
} |
f464f14e39a7
Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
102 |
} |
f464f14e39a7
Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
103 |
fmt.Println("done sending") |
f464f14e39a7
Renamed examples to example, since there's only one program there.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
104 |
} |