roster.go
author Chris Jones <chris@cjones.org>
Mon, 02 Jan 2012 10:01:41 -0700
changeset 50 08d2b9deb710
parent 47 22e575eff35a
child 52 9b664fde0ec3
permissions -rw-r--r--
Simplified the roster filter.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
2839fece923e Extended stanzas work now.
Chris Jones <chris@cjones.org>
parents: 37
diff changeset
    31
func newRosterQuery(name *xml.Name) interface{} {
2839fece923e Extended stanzas work now.
Chris Jones <chris@cjones.org>
parents: 37
diff changeset
    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
2839fece923e Extended stanzas work now.
Chris Jones <chris@cjones.org>
parents: 37
diff changeset
    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
2839fece923e Extended stanzas work now.
Chris Jones <chris@cjones.org>
parents: 37
diff changeset
    47
		if !ok {
2839fece923e Extended stanzas work now.
Chris Jones <chris@cjones.org>
parents: 37
diff changeset
    48
			ch <- os.NewError(fmt.Sprintf(
2839fece923e Extended stanzas work now.
Chris Jones <chris@cjones.org>
parents: 37
diff changeset
    49
				"Roster query result not query: %v", st))
2839fece923e Extended stanzas work now.
Chris Jones <chris@cjones.org>
parents: 37
diff changeset
    50
			return false
2839fece923e Extended stanzas work now.
Chris Jones <chris@cjones.org>
parents: 37
diff changeset
    51
		}
2839fece923e Extended stanzas work now.
Chris Jones <chris@cjones.org>
parents: 37
diff changeset
    52
		cl.roster = make(map[string] *RosterItem, len(rq.Item))
2839fece923e Extended stanzas work now.
Chris Jones <chris@cjones.org>
parents: 37
diff changeset
    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
4a4530b8f622 Added roster updating.
Chris Jones <chris@cjones.org>
parents: 42
diff changeset
    75
4a4530b8f622 Added roster updating.
Chris Jones <chris@cjones.org>
parents: 42
diff changeset
    76
// The roster filter updates the Client's representation of the
4a4530b8f622 Added roster updating.
Chris Jones <chris@cjones.org>
parents: 42
diff changeset
    77
// roster, but it lets the relevant stanzas through.
4a4530b8f622 Added roster updating.
Chris Jones <chris@cjones.org>
parents: 42
diff changeset
    78
func (cl *Client) startRosterFilter() {
4a4530b8f622 Added roster updating.
Chris Jones <chris@cjones.org>
parents: 42
diff changeset
    79
	out := make(chan Stanza)
4a4530b8f622 Added roster updating.
Chris Jones <chris@cjones.org>
parents: 42
diff changeset
    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
4a4530b8f622 Added roster updating.
Chris Jones <chris@cjones.org>
parents: 42
diff changeset
    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
4a4530b8f622 Added roster updating.
Chris Jones <chris@cjones.org>
parents: 42
diff changeset
    86
		}
4a4530b8f622 Added roster updating.
Chris Jones <chris@cjones.org>
parents: 42
diff changeset
    87
	}(in, out)
4a4530b8f622 Added roster updating.
Chris Jones <chris@cjones.org>
parents: 42
diff changeset
    88
}
4a4530b8f622 Added roster updating.
Chris Jones <chris@cjones.org>
parents: 42
diff changeset
    89
47
22e575eff35a Implemented roster item delete, and added another BUG comment.
Chris Jones <chris@cjones.org>
parents: 46
diff changeset
    90
// 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
    91
// 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
    92
// 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
    93
// of via a method call.
46
4a4530b8f622 Added roster updating.
Chris Jones <chris@cjones.org>
parents: 42
diff changeset
    94
func (cl *Client) maybeUpdateRoster(st Stanza) {
4a4530b8f622 Added roster updating.
Chris Jones <chris@cjones.org>
parents: 42
diff changeset
    95
	rq, ok := st.GetNested().(*RosterQuery)
4a4530b8f622 Added roster updating.
Chris Jones <chris@cjones.org>
parents: 42
diff changeset
    96
	if st.GetName() == "iq" && st.GetType() == "set" && ok {
4a4530b8f622 Added roster updating.
Chris Jones <chris@cjones.org>
parents: 42
diff changeset
    97
		for _, item := range(rq.Item) {
47
22e575eff35a Implemented roster item delete, and added another BUG comment.
Chris Jones <chris@cjones.org>
parents: 46
diff changeset
    98
			if item.Subscription == "remove" {
22e575eff35a Implemented roster item delete, and added another BUG comment.
Chris Jones <chris@cjones.org>
parents: 46
diff changeset
    99
				cl.roster[item.Jid] = nil
22e575eff35a Implemented roster item delete, and added another BUG comment.
Chris Jones <chris@cjones.org>
parents: 46
diff changeset
   100
			} else {
22e575eff35a Implemented roster item delete, and added another BUG comment.
Chris Jones <chris@cjones.org>
parents: 46
diff changeset
   101
				cl.roster[item.Jid] = &item
22e575eff35a Implemented roster item delete, and added another BUG comment.
Chris Jones <chris@cjones.org>
parents: 46
diff changeset
   102
			}
46
4a4530b8f622 Added roster updating.
Chris Jones <chris@cjones.org>
parents: 42
diff changeset
   103
		}
4a4530b8f622 Added roster updating.
Chris Jones <chris@cjones.org>
parents: 42
diff changeset
   104
	}
4a4530b8f622 Added roster updating.
Chris Jones <chris@cjones.org>
parents: 42
diff changeset
   105
}