|
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 package main |
|
6 |
|
7 import ( |
|
8 xmpp ".." |
|
9 "crypto/tls" |
|
10 "encoding/xml" |
|
11 "flag" |
|
12 "fmt" |
|
13 "log" |
|
14 "os" |
|
15 "strings" |
|
16 ) |
|
17 |
|
18 type StdLogger struct { |
|
19 } |
|
20 |
|
21 func (s *StdLogger) Log(v ...interface{}) { |
|
22 log.Println(v...) |
|
23 } |
|
24 |
|
25 func (s *StdLogger) Logf(fmt string, v ...interface{}) { |
|
26 log.Printf(fmt, v...) |
|
27 } |
|
28 |
|
29 func init() { |
|
30 logger := &StdLogger{} |
|
31 // xmpp.Debug = logger |
|
32 xmpp.Info = logger |
|
33 xmpp.Warn = logger |
|
34 |
|
35 xmpp.TlsConfig = tls.Config{InsecureSkipVerify: true} |
|
36 } |
|
37 |
|
38 // Demonstrate the API, and allow the user to interact with an XMPP |
|
39 // server via the terminal. |
|
40 func main() { |
|
41 var jid xmpp.JID |
|
42 flag.Var(&jid, "jid", "JID to log in as") |
|
43 var pw *string = flag.String("pw", "", "password") |
|
44 flag.Parse() |
|
45 if jid.Domain == "" || *pw == "" { |
|
46 flag.Usage() |
|
47 os.Exit(2) |
|
48 } |
|
49 |
|
50 c, err := xmpp.NewClient(&jid, *pw, nil) |
|
51 if err != nil { |
|
52 log.Fatalf("NewClient(%v): %v", jid, err) |
|
53 } |
|
54 defer close(c.Out) |
|
55 |
|
56 err = c.StartSession(&xmpp.Presence{}) |
|
57 if err != nil { |
|
58 log.Fatalf("StartSession: %v", err) |
|
59 } |
|
60 c.Roster.Update() |
|
61 roster := c.Roster.Get() |
|
62 fmt.Printf("%d roster entries:\n", len(roster)) |
|
63 for i, entry := range roster { |
|
64 fmt.Printf("%d: %v\n", i, entry) |
|
65 } |
|
66 |
|
67 go func(ch <-chan xmpp.Stanza) { |
|
68 for obj := range ch { |
|
69 fmt.Printf("s: %v\n", obj) |
|
70 } |
|
71 fmt.Println("done reading") |
|
72 }(c.In) |
|
73 |
|
74 p := make([]byte, 1024) |
|
75 for { |
|
76 nr, _ := os.Stdin.Read(p) |
|
77 if nr == 0 { |
|
78 break |
|
79 } |
|
80 s := string(p) |
|
81 dec := xml.NewDecoder(strings.NewReader(s)) |
|
82 t, err := dec.Token() |
|
83 if err != nil { |
|
84 fmt.Printf("token: %s\n", err) |
|
85 break |
|
86 } |
|
87 var se *xml.StartElement |
|
88 var ok bool |
|
89 if se, ok = t.(*xml.StartElement); !ok { |
|
90 fmt.Println("Couldn't find start element") |
|
91 break |
|
92 } |
|
93 var stan xmpp.Stanza |
|
94 switch se.Name.Local { |
|
95 case "iq": |
|
96 stan = &xmpp.Iq{} |
|
97 case "message": |
|
98 stan = &xmpp.Message{} |
|
99 case "presence": |
|
100 stan = &xmpp.Presence{} |
|
101 default: |
|
102 fmt.Println("Can't parse non-stanza.") |
|
103 continue |
|
104 } |
|
105 err = dec.Decode(stan) |
|
106 if err == nil { |
|
107 c.Out <- stan |
|
108 } else { |
|
109 fmt.Printf("Parse error: %v\n", err) |
|
110 break |
|
111 } |
|
112 } |
|
113 fmt.Println("done sending") |
|
114 } |