author | Chris Jones <chris@cjones.org> |
Thu, 05 Jan 2012 23:19:42 -0700 | |
changeset 58 | c0e8778bdb80 |
parent 57 | e6cb3f049137 |
child 60 | 6d4f43f7dc19 |
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 |
|
57
e6cb3f049137
Revamped how the roster works. We're now using a channel to transmit snapshots
Chris Jones <chris@cjones.org>
parents:
55
diff
changeset
|
30 |
type rosterClient struct { |
e6cb3f049137
Revamped how the roster works. We're now using a channel to transmit snapshots
Chris Jones <chris@cjones.org>
parents:
55
diff
changeset
|
31 |
rosterChan <-chan []RosterItem |
e6cb3f049137
Revamped how the roster works. We're now using a channel to transmit snapshots
Chris Jones <chris@cjones.org>
parents:
55
diff
changeset
|
32 |
rosterUpdate chan<- RosterItem |
e6cb3f049137
Revamped how the roster works. We're now using a channel to transmit snapshots
Chris Jones <chris@cjones.org>
parents:
55
diff
changeset
|
33 |
} |
e6cb3f049137
Revamped how the roster works. We're now using a channel to transmit snapshots
Chris Jones <chris@cjones.org>
parents:
55
diff
changeset
|
34 |
|
e6cb3f049137
Revamped how the roster works. We're now using a channel to transmit snapshots
Chris Jones <chris@cjones.org>
parents:
55
diff
changeset
|
35 |
var ( |
e6cb3f049137
Revamped how the roster works. We're now using a channel to transmit snapshots
Chris Jones <chris@cjones.org>
parents:
55
diff
changeset
|
36 |
rosterClients = make(map[string] rosterClient) |
e6cb3f049137
Revamped how the roster works. We're now using a channel to transmit snapshots
Chris Jones <chris@cjones.org>
parents:
55
diff
changeset
|
37 |
) |
e6cb3f049137
Revamped how the roster works. We're now using a channel to transmit snapshots
Chris Jones <chris@cjones.org>
parents:
55
diff
changeset
|
38 |
|
36
9fe022261dcc
Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
39 |
// Implicitly becomes part of NewClient's extStanza arg. |
38 | 40 |
func newRosterQuery(name *xml.Name) interface{} { |
41 |
return &RosterQuery{} |
|
36
9fe022261dcc
Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
42 |
} |
9fe022261dcc
Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
43 |
|
9fe022261dcc
Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
44 |
// Synchronously fetch this entity's roster from the server and cache |
57
e6cb3f049137
Revamped how the roster works. We're now using a channel to transmit snapshots
Chris Jones <chris@cjones.org>
parents:
55
diff
changeset
|
45 |
// that information. This is called once from a fairly deep call stack |
e6cb3f049137
Revamped how the roster works. We're now using a channel to transmit snapshots
Chris Jones <chris@cjones.org>
parents:
55
diff
changeset
|
46 |
// as part of XMPP negotiation. |
e6cb3f049137
Revamped how the roster works. We're now using a channel to transmit snapshots
Chris Jones <chris@cjones.org>
parents:
55
diff
changeset
|
47 |
func fetchRoster(client *Client) os.Error { |
e6cb3f049137
Revamped how the roster works. We're now using a channel to transmit snapshots
Chris Jones <chris@cjones.org>
parents:
55
diff
changeset
|
48 |
rosterUpdate := rosterClients[client.Uid].rosterUpdate |
e6cb3f049137
Revamped how the roster works. We're now using a channel to transmit snapshots
Chris Jones <chris@cjones.org>
parents:
55
diff
changeset
|
49 |
|
e6cb3f049137
Revamped how the roster works. We're now using a channel to transmit snapshots
Chris Jones <chris@cjones.org>
parents:
55
diff
changeset
|
50 |
iq := &Iq{From: client.Jid.String(), Id: <- Id, Type: "get", |
39
4a06f7ccfa84
Use name tags for roster data structures.
Chris Jones <chris@cjones.org>
parents:
38
diff
changeset
|
51 |
Nested: RosterQuery{}} |
36
9fe022261dcc
Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
52 |
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
|
53 |
f := func(st Stanza) bool { |
57
e6cb3f049137
Revamped how the roster works. We're now using a channel to transmit snapshots
Chris Jones <chris@cjones.org>
parents:
55
diff
changeset
|
54 |
defer close(ch) |
36
9fe022261dcc
Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
55 |
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
|
56 |
ch <- iq.Error |
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 |
} |
42
f6bb47ca12f2
Renamed the somewhat obscure XTo(), etc. to GetTo(), etc.
Chris Jones <chris@cjones.org>
parents:
39
diff
changeset
|
59 |
rq, ok := st.GetNested().(*RosterQuery) |
38 | 60 |
if !ok { |
61 |
ch <- os.NewError(fmt.Sprintf( |
|
62 |
"Roster query result not query: %v", st)) |
|
63 |
return false |
|
64 |
} |
|
57
e6cb3f049137
Revamped how the roster works. We're now using a channel to transmit snapshots
Chris Jones <chris@cjones.org>
parents:
55
diff
changeset
|
65 |
for _, item := range(rq.Item) { |
e6cb3f049137
Revamped how the roster works. We're now using a channel to transmit snapshots
Chris Jones <chris@cjones.org>
parents:
55
diff
changeset
|
66 |
rosterUpdate <- item |
36
9fe022261dcc
Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
67 |
} |
9fe022261dcc
Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
68 |
ch <- nil |
9fe022261dcc
Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
69 |
return false |
9fe022261dcc
Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
70 |
} |
57
e6cb3f049137
Revamped how the roster works. We're now using a channel to transmit snapshots
Chris Jones <chris@cjones.org>
parents:
55
diff
changeset
|
71 |
client.HandleStanza(iq.Id, f) |
e6cb3f049137
Revamped how the roster works. We're now using a channel to transmit snapshots
Chris Jones <chris@cjones.org>
parents:
55
diff
changeset
|
72 |
client.Out <- iq |
36
9fe022261dcc
Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
73 |
// 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
|
74 |
return <- ch |
9fe022261dcc
Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
75 |
} |
9fe022261dcc
Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
76 |
|
46 | 77 |
// The roster filter updates the Client's representation of the |
57
e6cb3f049137
Revamped how the roster works. We're now using a channel to transmit snapshots
Chris Jones <chris@cjones.org>
parents:
55
diff
changeset
|
78 |
// roster, but it lets the relevant stanzas through. This also starts |
e6cb3f049137
Revamped how the roster works. We're now using a channel to transmit snapshots
Chris Jones <chris@cjones.org>
parents:
55
diff
changeset
|
79 |
// the roster feeder, which is the goroutine that provides data on |
e6cb3f049137
Revamped how the roster works. We're now using a channel to transmit snapshots
Chris Jones <chris@cjones.org>
parents:
55
diff
changeset
|
80 |
// client.Roster. |
e6cb3f049137
Revamped how the roster works. We're now using a channel to transmit snapshots
Chris Jones <chris@cjones.org>
parents:
55
diff
changeset
|
81 |
func startRosterFilter(client *Client) { |
46 | 82 |
out := make(chan Stanza) |
57
e6cb3f049137
Revamped how the roster works. We're now using a channel to transmit snapshots
Chris Jones <chris@cjones.org>
parents:
55
diff
changeset
|
83 |
in := client.AddFilter(out) |
50
08d2b9deb710
Simplified the roster filter.
Chris Jones <chris@cjones.org>
parents:
47
diff
changeset
|
84 |
go func(in <-chan Stanza, out chan<- Stanza) { |
46 | 85 |
defer close(out) |
50
08d2b9deb710
Simplified the roster filter.
Chris Jones <chris@cjones.org>
parents:
47
diff
changeset
|
86 |
for st := range(in) { |
57
e6cb3f049137
Revamped how the roster works. We're now using a channel to transmit snapshots
Chris Jones <chris@cjones.org>
parents:
55
diff
changeset
|
87 |
maybeUpdateRoster(client, st) |
50
08d2b9deb710
Simplified the roster filter.
Chris Jones <chris@cjones.org>
parents:
47
diff
changeset
|
88 |
out <- st |
46 | 89 |
} |
90 |
}(in, out) |
|
57
e6cb3f049137
Revamped how the roster works. We're now using a channel to transmit snapshots
Chris Jones <chris@cjones.org>
parents:
55
diff
changeset
|
91 |
|
e6cb3f049137
Revamped how the roster works. We're now using a channel to transmit snapshots
Chris Jones <chris@cjones.org>
parents:
55
diff
changeset
|
92 |
rosterCh := make(chan []RosterItem) |
e6cb3f049137
Revamped how the roster works. We're now using a channel to transmit snapshots
Chris Jones <chris@cjones.org>
parents:
55
diff
changeset
|
93 |
rosterUpdate := make(chan RosterItem) |
e6cb3f049137
Revamped how the roster works. We're now using a channel to transmit snapshots
Chris Jones <chris@cjones.org>
parents:
55
diff
changeset
|
94 |
rosterClients[client.Uid] = rosterClient{rosterChan: rosterCh, |
e6cb3f049137
Revamped how the roster works. We're now using a channel to transmit snapshots
Chris Jones <chris@cjones.org>
parents:
55
diff
changeset
|
95 |
rosterUpdate: rosterUpdate} |
e6cb3f049137
Revamped how the roster works. We're now using a channel to transmit snapshots
Chris Jones <chris@cjones.org>
parents:
55
diff
changeset
|
96 |
go feedRoster(rosterCh, rosterUpdate) |
46 | 97 |
} |
98 |
||
57
e6cb3f049137
Revamped how the roster works. We're now using a channel to transmit snapshots
Chris Jones <chris@cjones.org>
parents:
55
diff
changeset
|
99 |
func maybeUpdateRoster(client *Client, st Stanza) { |
e6cb3f049137
Revamped how the roster works. We're now using a channel to transmit snapshots
Chris Jones <chris@cjones.org>
parents:
55
diff
changeset
|
100 |
rosterUpdate := rosterClients[client.Uid].rosterUpdate |
e6cb3f049137
Revamped how the roster works. We're now using a channel to transmit snapshots
Chris Jones <chris@cjones.org>
parents:
55
diff
changeset
|
101 |
|
46 | 102 |
rq, ok := st.GetNested().(*RosterQuery) |
103 |
if st.GetName() == "iq" && st.GetType() == "set" && ok { |
|
57
e6cb3f049137
Revamped how the roster works. We're now using a channel to transmit snapshots
Chris Jones <chris@cjones.org>
parents:
55
diff
changeset
|
104 |
for _, item := range(rq.Item) { |
e6cb3f049137
Revamped how the roster works. We're now using a channel to transmit snapshots
Chris Jones <chris@cjones.org>
parents:
55
diff
changeset
|
105 |
rosterUpdate <- item |
46 | 106 |
} |
58
c0e8778bdb80
Sent acknowledgment when somebody sends us a roster iq.
Chris Jones <chris@cjones.org>
parents:
57
diff
changeset
|
107 |
// Send a reply. |
c0e8778bdb80
Sent acknowledgment when somebody sends us a roster iq.
Chris Jones <chris@cjones.org>
parents:
57
diff
changeset
|
108 |
iq := &Iq{To: st.GetFrom(), Id: st.GetId(), Type: |
c0e8778bdb80
Sent acknowledgment when somebody sends us a roster iq.
Chris Jones <chris@cjones.org>
parents:
57
diff
changeset
|
109 |
"result"} |
c0e8778bdb80
Sent acknowledgment when somebody sends us a roster iq.
Chris Jones <chris@cjones.org>
parents:
57
diff
changeset
|
110 |
client.Out <- iq |
46 | 111 |
} |
112 |
} |
|
57
e6cb3f049137
Revamped how the roster works. We're now using a channel to transmit snapshots
Chris Jones <chris@cjones.org>
parents:
55
diff
changeset
|
113 |
|
e6cb3f049137
Revamped how the roster works. We're now using a channel to transmit snapshots
Chris Jones <chris@cjones.org>
parents:
55
diff
changeset
|
114 |
func feedRoster(rosterCh chan<- []RosterItem, rosterUpdate <-chan RosterItem) { |
e6cb3f049137
Revamped how the roster works. We're now using a channel to transmit snapshots
Chris Jones <chris@cjones.org>
parents:
55
diff
changeset
|
115 |
roster := make(map[string] RosterItem) |
e6cb3f049137
Revamped how the roster works. We're now using a channel to transmit snapshots
Chris Jones <chris@cjones.org>
parents:
55
diff
changeset
|
116 |
snapshot := []RosterItem{} |
e6cb3f049137
Revamped how the roster works. We're now using a channel to transmit snapshots
Chris Jones <chris@cjones.org>
parents:
55
diff
changeset
|
117 |
for { |
e6cb3f049137
Revamped how the roster works. We're now using a channel to transmit snapshots
Chris Jones <chris@cjones.org>
parents:
55
diff
changeset
|
118 |
select { |
e6cb3f049137
Revamped how the roster works. We're now using a channel to transmit snapshots
Chris Jones <chris@cjones.org>
parents:
55
diff
changeset
|
119 |
case newIt := <-rosterUpdate: |
e6cb3f049137
Revamped how the roster works. We're now using a channel to transmit snapshots
Chris Jones <chris@cjones.org>
parents:
55
diff
changeset
|
120 |
if newIt.Subscription == "remove" { |
e6cb3f049137
Revamped how the roster works. We're now using a channel to transmit snapshots
Chris Jones <chris@cjones.org>
parents:
55
diff
changeset
|
121 |
roster[newIt.Jid] = RosterItem{}, false |
e6cb3f049137
Revamped how the roster works. We're now using a channel to transmit snapshots
Chris Jones <chris@cjones.org>
parents:
55
diff
changeset
|
122 |
} else { |
e6cb3f049137
Revamped how the roster works. We're now using a channel to transmit snapshots
Chris Jones <chris@cjones.org>
parents:
55
diff
changeset
|
123 |
roster[newIt.Jid] = newIt |
e6cb3f049137
Revamped how the roster works. We're now using a channel to transmit snapshots
Chris Jones <chris@cjones.org>
parents:
55
diff
changeset
|
124 |
} |
e6cb3f049137
Revamped how the roster works. We're now using a channel to transmit snapshots
Chris Jones <chris@cjones.org>
parents:
55
diff
changeset
|
125 |
case rosterCh <- snapshot: |
e6cb3f049137
Revamped how the roster works. We're now using a channel to transmit snapshots
Chris Jones <chris@cjones.org>
parents:
55
diff
changeset
|
126 |
} |
e6cb3f049137
Revamped how the roster works. We're now using a channel to transmit snapshots
Chris Jones <chris@cjones.org>
parents:
55
diff
changeset
|
127 |
snapshot = make([]RosterItem, 0, len(roster)) |
e6cb3f049137
Revamped how the roster works. We're now using a channel to transmit snapshots
Chris Jones <chris@cjones.org>
parents:
55
diff
changeset
|
128 |
for _, v := range(roster) { |
e6cb3f049137
Revamped how the roster works. We're now using a channel to transmit snapshots
Chris Jones <chris@cjones.org>
parents:
55
diff
changeset
|
129 |
snapshot = append(snapshot, v) |
e6cb3f049137
Revamped how the roster works. We're now using a channel to transmit snapshots
Chris Jones <chris@cjones.org>
parents:
55
diff
changeset
|
130 |
} |
e6cb3f049137
Revamped how the roster works. We're now using a channel to transmit snapshots
Chris Jones <chris@cjones.org>
parents:
55
diff
changeset
|
131 |
} |
e6cb3f049137
Revamped how the roster works. We're now using a channel to transmit snapshots
Chris Jones <chris@cjones.org>
parents:
55
diff
changeset
|
132 |
} |
e6cb3f049137
Revamped how the roster works. We're now using a channel to transmit snapshots
Chris Jones <chris@cjones.org>
parents:
55
diff
changeset
|
133 |
|
e6cb3f049137
Revamped how the roster works. We're now using a channel to transmit snapshots
Chris Jones <chris@cjones.org>
parents:
55
diff
changeset
|
134 |
// Retrieve a snapshot of the roster for the given Client. |
e6cb3f049137
Revamped how the roster works. We're now using a channel to transmit snapshots
Chris Jones <chris@cjones.org>
parents:
55
diff
changeset
|
135 |
func Roster(client *Client) []RosterItem { |
e6cb3f049137
Revamped how the roster works. We're now using a channel to transmit snapshots
Chris Jones <chris@cjones.org>
parents:
55
diff
changeset
|
136 |
rosterChan := rosterClients[client.Uid].rosterChan |
e6cb3f049137
Revamped how the roster works. We're now using a channel to transmit snapshots
Chris Jones <chris@cjones.org>
parents:
55
diff
changeset
|
137 |
return <- rosterChan |
e6cb3f049137
Revamped how the roster works. We're now using a channel to transmit snapshots
Chris Jones <chris@cjones.org>
parents:
55
diff
changeset
|
138 |
} |