author | Chris Jones <christian.jones@sri.com> |
Thu, 05 Jan 2012 14:32:15 -0700 | |
changeset 55 | ce9f9f7108c8 |
parent 54 | b345e0473430 |
child 57 | e6cb3f049137 |
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)) |
|
54
b345e0473430
When ranging over an array of structures, the iterand is reused. So storing a
Chris Jones <christian.jones@sri.com>
parents:
52
diff
changeset
|
53 |
for i, item := range(rq.Item) { |
b345e0473430
When ranging over an array of structures, the iterand is reused. So storing a
Chris Jones <christian.jones@sri.com>
parents:
52
diff
changeset
|
54 |
cl.roster[item.Jid] = &rq.Item[i] |
36
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) |
|
50
08d2b9deb710
Simplified the roster filter.
Chris Jones <chris@cjones.org>
parents:
47
diff
changeset
|
81 |
go func(in <-chan Stanza, out chan<- Stanza) { |
46 | 82 |
defer close(out) |
50
08d2b9deb710
Simplified the roster filter.
Chris Jones <chris@cjones.org>
parents:
47
diff
changeset
|
83 |
for st := range(in) { |
08d2b9deb710
Simplified the roster filter.
Chris Jones <chris@cjones.org>
parents:
47
diff
changeset
|
84 |
cl.maybeUpdateRoster(st) |
08d2b9deb710
Simplified the roster filter.
Chris Jones <chris@cjones.org>
parents:
47
diff
changeset
|
85 |
out <- st |
46 | 86 |
} |
87 |
}(in, out) |
|
88 |
} |
|
89 |
||
55
ce9f9f7108c8
Added another BUG comment.
Chris Jones <christian.jones@sri.com>
parents:
54
diff
changeset
|
90 |
// BUG(cjyar) This isn't getting updates. |
47
22e575eff35a
Implemented roster item delete, and added another BUG comment.
Chris Jones <chris@cjones.org>
parents:
46
diff
changeset
|
91 |
// BUG(cjyar) This isn't actually thread safe, though it's unlikely it |
22e575eff35a
Implemented roster item delete, and added another BUG comment.
Chris Jones <chris@cjones.org>
parents:
46
diff
changeset
|
92 |
// will fail in practice. Either the roster should be protected with a |
22e575eff35a
Implemented roster item delete, and added another BUG comment.
Chris Jones <chris@cjones.org>
parents:
46
diff
changeset
|
93 |
// mutex, or we should make the roster available on a channel instead |
22e575eff35a
Implemented roster item delete, and added another BUG comment.
Chris Jones <chris@cjones.org>
parents:
46
diff
changeset
|
94 |
// of via a method call. |
52 | 95 |
// BUG(cjyar) RFC 3921, Section 7.4 says we need to reply. |
46 | 96 |
func (cl *Client) maybeUpdateRoster(st Stanza) { |
97 |
rq, ok := st.GetNested().(*RosterQuery) |
|
98 |
if st.GetName() == "iq" && st.GetType() == "set" && ok { |
|
54
b345e0473430
When ranging over an array of structures, the iterand is reused. So storing a
Chris Jones <christian.jones@sri.com>
parents:
52
diff
changeset
|
99 |
for i, item := range(rq.Item) { |
47
22e575eff35a
Implemented roster item delete, and added another BUG comment.
Chris Jones <chris@cjones.org>
parents:
46
diff
changeset
|
100 |
if item.Subscription == "remove" { |
22e575eff35a
Implemented roster item delete, and added another BUG comment.
Chris Jones <chris@cjones.org>
parents:
46
diff
changeset
|
101 |
cl.roster[item.Jid] = nil |
22e575eff35a
Implemented roster item delete, and added another BUG comment.
Chris Jones <chris@cjones.org>
parents:
46
diff
changeset
|
102 |
} else { |
54
b345e0473430
When ranging over an array of structures, the iterand is reused. So storing a
Chris Jones <christian.jones@sri.com>
parents:
52
diff
changeset
|
103 |
cl.roster[item.Jid] = &rq.Item[i] |
47
22e575eff35a
Implemented roster item delete, and added another BUG comment.
Chris Jones <chris@cjones.org>
parents:
46
diff
changeset
|
104 |
} |
46 | 105 |
} |
106 |
} |
|
107 |
} |