roster.go
author Chris Jones <chris@cjones.org>
Sun, 01 Jan 2012 17:22:48 -0700
changeset 42 f6bb47ca12f2
parent 39 4a06f7ccfa84
child 46 4a4530b8f622
permissions -rw-r--r--
Renamed the somewhat obscure XTo(), etc. to GetTo(), etc.
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
// BUG(cjyar) The roster isn't actually updated when things change.
9fe022261dcc Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff changeset
    66
9fe022261dcc Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff changeset
    67
// 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
    68
// 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
    69
// 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
    70
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
    71
	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
    72
	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
    73
		r[key] = val
9fe022261dcc Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff changeset
    74
	}
9fe022261dcc Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff changeset
    75
	return r
9fe022261dcc Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff changeset
    76
}