author | Chris Jones <christian.jones@sri.com> |
Thu, 19 Jan 2012 12:25:36 -0600 | |
changeset 74 | e619e18dcec3 |
parent 72 | 53f15893a1a7 |
child 75 | 03a923eb5c01 |
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 ( |
74
e619e18dcec3
Ran gofix from weekly-2012-01-15.
Chris Jones <christian.jones@sri.com>
parents:
72
diff
changeset
|
8 |
"errors" |
36
9fe022261dcc
Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
9 |
"fmt" |
74
e619e18dcec3
Ran gofix from weekly-2012-01-15.
Chris Jones <christian.jones@sri.com>
parents:
72
diff
changeset
|
10 |
|
e619e18dcec3
Ran gofix from weekly-2012-01-15.
Chris Jones <christian.jones@sri.com>
parents:
72
diff
changeset
|
11 |
"encoding/xml" |
36
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 |
|
9fe022261dcc
Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
14 |
// 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
|
15 |
|
72 | 16 |
var rosterExt Extension = Extension{StanzaHandlers: map[string]func(*xml.Name) interface{}{NsRoster: newRosterQuery}, Start: startRosterFilter} |
60
6d4f43f7dc19
Made a generic extension interface.
Chris Jones <chris@cjones.org>
parents:
58
diff
changeset
|
17 |
|
36
9fe022261dcc
Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
18 |
// Roster query/result |
9fe022261dcc
Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
19 |
type RosterQuery struct { |
39
4a06f7ccfa84
Use name tags for roster data structures.
Chris Jones <chris@cjones.org>
parents:
38
diff
changeset
|
20 |
XMLName xml.Name `xml:"jabber:iq:roster query"` |
72 | 21 |
Item []RosterItem |
36
9fe022261dcc
Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
22 |
} |
9fe022261dcc
Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
23 |
|
9fe022261dcc
Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
24 |
// 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
|
25 |
type RosterItem struct { |
72 | 26 |
XMLName xml.Name `xml:"item"` |
27 |
Jid string `xml:"attr"` |
|
28 |
Subscription string `xml:"attr"` |
|
29 |
Name string `xml:"attr"` |
|
30 |
Group []string |
|
36
9fe022261dcc
Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
31 |
} |
9fe022261dcc
Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
32 |
|
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
|
33 |
type rosterClient struct { |
72 | 34 |
rosterChan <-chan []RosterItem |
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
|
35 |
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
|
36 |
} |
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 |
var ( |
72 | 39 |
rosterClients = make(map[string]rosterClient) |
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
|
40 |
) |
e6cb3f049137
Revamped how the roster works. We're now using a channel to transmit snapshots
Chris Jones <chris@cjones.org>
parents:
55
diff
changeset
|
41 |
|
36
9fe022261dcc
Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
42 |
// Implicitly becomes part of NewClient's extStanza arg. |
38 | 43 |
func newRosterQuery(name *xml.Name) interface{} { |
44 |
return &RosterQuery{} |
|
36
9fe022261dcc
Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
45 |
} |
9fe022261dcc
Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
46 |
|
9fe022261dcc
Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
47 |
// 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
|
48 |
// 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
|
49 |
// as part of XMPP negotiation. |
74
e619e18dcec3
Ran gofix from weekly-2012-01-15.
Chris Jones <christian.jones@sri.com>
parents:
72
diff
changeset
|
50 |
func fetchRoster(client *Client) error { |
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
|
51 |
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
|
52 |
|
72 | 53 |
iq := &Iq{From: client.Jid.String(), Id: <-Id, Type: "get", |
61
16513974d273
Stanzas can now contain multiple nested (extended) elements.
Chris Jones <chris@cjones.org>
parents:
60
diff
changeset
|
54 |
Nested: []interface{}{RosterQuery{}}} |
74
e619e18dcec3
Ran gofix from weekly-2012-01-15.
Chris Jones <christian.jones@sri.com>
parents:
72
diff
changeset
|
55 |
ch := make(chan error) |
36
9fe022261dcc
Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
56 |
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
|
57 |
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
|
58 |
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
|
59 |
ch <- iq.Error |
9fe022261dcc
Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
60 |
return false |
9fe022261dcc
Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
61 |
} |
61
16513974d273
Stanzas can now contain multiple nested (extended) elements.
Chris Jones <chris@cjones.org>
parents:
60
diff
changeset
|
62 |
var rq *RosterQuery |
72 | 63 |
for _, ele := range st.GetNested() { |
64 |
if q, ok := ele.(*RosterQuery); ok { |
|
61
16513974d273
Stanzas can now contain multiple nested (extended) elements.
Chris Jones <chris@cjones.org>
parents:
60
diff
changeset
|
65 |
rq = q |
16513974d273
Stanzas can now contain multiple nested (extended) elements.
Chris Jones <chris@cjones.org>
parents:
60
diff
changeset
|
66 |
break |
16513974d273
Stanzas can now contain multiple nested (extended) elements.
Chris Jones <chris@cjones.org>
parents:
60
diff
changeset
|
67 |
} |
16513974d273
Stanzas can now contain multiple nested (extended) elements.
Chris Jones <chris@cjones.org>
parents:
60
diff
changeset
|
68 |
} |
16513974d273
Stanzas can now contain multiple nested (extended) elements.
Chris Jones <chris@cjones.org>
parents:
60
diff
changeset
|
69 |
if rq == nil { |
74
e619e18dcec3
Ran gofix from weekly-2012-01-15.
Chris Jones <christian.jones@sri.com>
parents:
72
diff
changeset
|
70 |
ch <- errors.New(fmt.Sprintf( |
38 | 71 |
"Roster query result not query: %v", st)) |
72 |
return false |
|
73 |
} |
|
72 | 74 |
for _, item := range rq.Item { |
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
|
75 |
rosterUpdate <- item |
36
9fe022261dcc
Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
76 |
} |
9fe022261dcc
Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
77 |
ch <- nil |
9fe022261dcc
Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
78 |
return false |
9fe022261dcc
Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
79 |
} |
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
|
80 |
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
|
81 |
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
|
82 |
// Wait for f to complete. |
72 | 83 |
return <-ch |
36
9fe022261dcc
Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
84 |
} |
9fe022261dcc
Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
85 |
|
46 | 86 |
// 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
|
87 |
// 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
|
88 |
// 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
|
89 |
// 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
|
90 |
func startRosterFilter(client *Client) { |
46 | 91 |
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
|
92 |
in := client.AddFilter(out) |
50
08d2b9deb710
Simplified the roster filter.
Chris Jones <chris@cjones.org>
parents:
47
diff
changeset
|
93 |
go func(in <-chan Stanza, out chan<- Stanza) { |
46 | 94 |
defer close(out) |
72 | 95 |
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
|
96 |
maybeUpdateRoster(client, st) |
50
08d2b9deb710
Simplified the roster filter.
Chris Jones <chris@cjones.org>
parents:
47
diff
changeset
|
97 |
out <- st |
46 | 98 |
} |
99 |
}(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
|
100 |
|
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 |
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
|
102 |
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
|
103 |
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
|
104 |
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
|
105 |
go feedRoster(rosterCh, rosterUpdate) |
46 | 106 |
} |
107 |
||
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
|
108 |
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
|
109 |
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
|
110 |
|
61
16513974d273
Stanzas can now contain multiple nested (extended) elements.
Chris Jones <chris@cjones.org>
parents:
60
diff
changeset
|
111 |
var rq *RosterQuery |
72 | 112 |
for _, ele := range st.GetNested() { |
113 |
if q, ok := ele.(*RosterQuery); ok { |
|
61
16513974d273
Stanzas can now contain multiple nested (extended) elements.
Chris Jones <chris@cjones.org>
parents:
60
diff
changeset
|
114 |
rq = q |
16513974d273
Stanzas can now contain multiple nested (extended) elements.
Chris Jones <chris@cjones.org>
parents:
60
diff
changeset
|
115 |
break |
16513974d273
Stanzas can now contain multiple nested (extended) elements.
Chris Jones <chris@cjones.org>
parents:
60
diff
changeset
|
116 |
} |
16513974d273
Stanzas can now contain multiple nested (extended) elements.
Chris Jones <chris@cjones.org>
parents:
60
diff
changeset
|
117 |
} |
16513974d273
Stanzas can now contain multiple nested (extended) elements.
Chris Jones <chris@cjones.org>
parents:
60
diff
changeset
|
118 |
if st.GetName() == "iq" && st.GetType() == "set" && rq != nil { |
72 | 119 |
for _, item := range rq.Item { |
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
|
120 |
rosterUpdate <- item |
46 | 121 |
} |
58
c0e8778bdb80
Sent acknowledgment when somebody sends us a roster iq.
Chris Jones <chris@cjones.org>
parents:
57
diff
changeset
|
122 |
// Send a reply. |
72 | 123 |
iq := &Iq{To: st.GetFrom(), Id: st.GetId(), Type: "result"} |
58
c0e8778bdb80
Sent acknowledgment when somebody sends us a roster iq.
Chris Jones <chris@cjones.org>
parents:
57
diff
changeset
|
124 |
client.Out <- iq |
46 | 125 |
} |
126 |
} |
|
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
|
127 |
|
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 |
func feedRoster(rosterCh chan<- []RosterItem, rosterUpdate <-chan RosterItem) { |
72 | 129 |
roster := make(map[string]RosterItem) |
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
|
130 |
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
|
131 |
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
|
132 |
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
|
133 |
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
|
134 |
if newIt.Subscription == "remove" { |
74
e619e18dcec3
Ran gofix from weekly-2012-01-15.
Chris Jones <christian.jones@sri.com>
parents:
72
diff
changeset
|
135 |
delete(roster, newIt.Jid) |
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
|
136 |
} 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
|
137 |
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
|
138 |
} |
e6cb3f049137
Revamped how the roster works. We're now using a channel to transmit snapshots
Chris Jones <chris@cjones.org>
parents:
55
diff
changeset
|
139 |
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
|
140 |
} |
e6cb3f049137
Revamped how the roster works. We're now using a channel to transmit snapshots
Chris Jones <chris@cjones.org>
parents:
55
diff
changeset
|
141 |
snapshot = make([]RosterItem, 0, len(roster)) |
72 | 142 |
for _, v := range roster { |
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
|
143 |
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
|
144 |
} |
e6cb3f049137
Revamped how the roster works. We're now using a channel to transmit snapshots
Chris Jones <chris@cjones.org>
parents:
55
diff
changeset
|
145 |
} |
e6cb3f049137
Revamped how the roster works. We're now using a channel to transmit snapshots
Chris Jones <chris@cjones.org>
parents:
55
diff
changeset
|
146 |
} |
e6cb3f049137
Revamped how the roster works. We're now using a channel to transmit snapshots
Chris Jones <chris@cjones.org>
parents:
55
diff
changeset
|
147 |
|
e6cb3f049137
Revamped how the roster works. We're now using a channel to transmit snapshots
Chris Jones <chris@cjones.org>
parents:
55
diff
changeset
|
148 |
// 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
|
149 |
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
|
150 |
rosterChan := rosterClients[client.Uid].rosterChan |
72 | 151 |
return <-rosterChan |
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
|
152 |
} |