author | Chris Jones <chris@cjones.org> |
Sun, 01 Jan 2012 19:32:51 -0700 | |
changeset 46 | 4a4530b8f622 |
parent 42 | f6bb47ca12f2 |
child 47 | 22e575eff35a |
permissions | -rw-r--r-- |
36
9fe022261dcc
Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
1 |
// Copyright 2011 The Go Authors. All rights reserved. |
9fe022261dcc
Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
2 |
// Use of this source code is governed by a BSD-style |
9fe022261dcc
Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
3 |
// license that can be found in the LICENSE file. |
9fe022261dcc
Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
4 |
|
9fe022261dcc
Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
5 |
package xmpp |
9fe022261dcc
Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
6 |
|
9fe022261dcc
Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
7 |
import ( |
9fe022261dcc
Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
8 |
"fmt" |
9fe022261dcc
Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
9 |
"os" |
9fe022261dcc
Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
10 |
"xml" |
9fe022261dcc
Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
11 |
) |
9fe022261dcc
Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
12 |
|
9fe022261dcc
Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
13 |
// This file contains support for roster management, RFC 3921, Section 7. |
9fe022261dcc
Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
14 |
|
9fe022261dcc
Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
15 |
// Roster query/result |
9fe022261dcc
Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
16 |
type RosterQuery struct { |
39
4a06f7ccfa84
Use name tags for roster data structures.
Chris Jones <chris@cjones.org>
parents:
38
diff
changeset
|
17 |
XMLName xml.Name `xml:"jabber:iq:roster query"` |
36
9fe022261dcc
Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
18 |
Item []RosterItem |
9fe022261dcc
Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
19 |
} |
9fe022261dcc
Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
20 |
|
9fe022261dcc
Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
21 |
// See RFC 3921, Section 7.1. |
9fe022261dcc
Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
22 |
type RosterItem struct { |
39
4a06f7ccfa84
Use name tags for roster data structures.
Chris Jones <chris@cjones.org>
parents:
38
diff
changeset
|
23 |
XMLName xml.Name `xml:"item"` |
36
9fe022261dcc
Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
24 |
Jid string `xml:"attr"` |
9fe022261dcc
Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
25 |
Subscription string `xml:"attr"` |
9fe022261dcc
Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
26 |
Name string `xml:"attr"` |
9fe022261dcc
Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
27 |
Group []string |
9fe022261dcc
Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
28 |
} |
9fe022261dcc
Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
29 |
|
9fe022261dcc
Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
30 |
// Implicitly becomes part of NewClient's extStanza arg. |
38 | 31 |
func newRosterQuery(name *xml.Name) interface{} { |
32 |
return &RosterQuery{} |
|
36
9fe022261dcc
Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
33 |
} |
9fe022261dcc
Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
34 |
|
9fe022261dcc
Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
35 |
// Synchronously fetch this entity's roster from the server and cache |
9fe022261dcc
Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
36 |
// that information. |
9fe022261dcc
Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
37 |
func (cl *Client) fetchRoster() os.Error { |
38 | 38 |
iq := &Iq{From: cl.Jid.String(), Id: <- cl.Id, Type: "get", |
39
4a06f7ccfa84
Use name tags for roster data structures.
Chris Jones <chris@cjones.org>
parents:
38
diff
changeset
|
39 |
Nested: RosterQuery{}} |
36
9fe022261dcc
Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
40 |
ch := make(chan os.Error) |
9fe022261dcc
Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
41 |
f := func(st Stanza) bool { |
9fe022261dcc
Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
42 |
if iq.Type == "error" { |
9fe022261dcc
Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
43 |
ch <- iq.Error |
9fe022261dcc
Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
44 |
return false |
9fe022261dcc
Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
45 |
} |
42
f6bb47ca12f2
Renamed the somewhat obscure XTo(), etc. to GetTo(), etc.
Chris Jones <chris@cjones.org>
parents:
39
diff
changeset
|
46 |
rq, ok := st.GetNested().(*RosterQuery) |
38 | 47 |
if !ok { |
48 |
ch <- os.NewError(fmt.Sprintf( |
|
49 |
"Roster query result not query: %v", st)) |
|
50 |
return false |
|
51 |
} |
|
52 |
cl.roster = make(map[string] *RosterItem, len(rq.Item)) |
|
53 |
for _, item := range(rq.Item) { |
|
36
9fe022261dcc
Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
54 |
cl.roster[item.Jid] = &item |
9fe022261dcc
Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
55 |
} |
9fe022261dcc
Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
56 |
ch <- nil |
9fe022261dcc
Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
57 |
return false |
9fe022261dcc
Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
58 |
} |
9fe022261dcc
Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
59 |
cl.HandleStanza(iq.Id, f) |
9fe022261dcc
Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
60 |
cl.Out <- iq |
9fe022261dcc
Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
61 |
// Wait for f to complete. |
9fe022261dcc
Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
62 |
return <- ch |
9fe022261dcc
Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
63 |
} |
9fe022261dcc
Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
64 |
|
9fe022261dcc
Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
65 |
// Returns the current roster of other entities which this one has a |
9fe022261dcc
Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
66 |
// relationship with. Changes to the roster will be signaled by an |
9fe022261dcc
Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
67 |
// appropriate Iq appearing on Client.In. See RFC 3921, Section 7.4. |
9fe022261dcc
Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
68 |
func (cl *Client) Roster() map[string] *RosterItem { |
9fe022261dcc
Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
69 |
r := make(map[string] *RosterItem) |
9fe022261dcc
Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
70 |
for key, val := range(cl.roster) { |
9fe022261dcc
Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
71 |
r[key] = val |
9fe022261dcc
Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
72 |
} |
9fe022261dcc
Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
73 |
return r |
9fe022261dcc
Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
74 |
} |
46 | 75 |
|
76 |
// The roster filter updates the Client's representation of the |
|
77 |
// roster, but it lets the relevant stanzas through. |
|
78 |
func (cl *Client) startRosterFilter() { |
|
79 |
out := make(chan Stanza) |
|
80 |
in := cl.AddFilter(out) |
|
81 |
go func(inSave <-chan Stanza, outSave chan<- Stanza) { |
|
82 |
defer close(out) |
|
83 |
in := inSave |
|
84 |
var out chan<- Stanza |
|
85 |
var st Stanza |
|
86 |
var ok bool |
|
87 |
for { |
|
88 |
select { |
|
89 |
case st, ok = <- in: |
|
90 |
if !ok { |
|
91 |
break |
|
92 |
} |
|
93 |
cl.maybeUpdateRoster(st) |
|
94 |
in = nil |
|
95 |
out = outSave |
|
96 |
case out <- st: |
|
97 |
out = nil |
|
98 |
in = inSave |
|
99 |
} |
|
100 |
} |
|
101 |
}(in, out) |
|
102 |
} |
|
103 |
||
104 |
func (cl *Client) maybeUpdateRoster(st Stanza) { |
|
105 |
rq, ok := st.GetNested().(*RosterQuery) |
|
106 |
if st.GetName() == "iq" && st.GetType() == "set" && ok { |
|
107 |
for _, item := range(rq.Item) { |
|
108 |
cl.roster[item.Jid] = &item |
|
109 |
} |
|
110 |
} |
|
111 |
} |