roster.go
author Chris Jones <chris@cjones.org>
Sat, 31 Dec 2011 10:11:01 -0700
changeset 37 fbda8e925fdf
parent 36 9fe022261dcc
child 38 2839fece923e
permissions -rw-r--r--
Minor fixups to make this come closer to working. It doesn't actually work, though, because the nesting in XML is backwards from how it is in Go. <iq><query/></iq> vs. RosterIq { Iq }
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
	"io"
9fe022261dcc Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff changeset
    10
	"os"
9fe022261dcc Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff changeset
    11
	"xml"
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
9fe022261dcc Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff changeset
    16
type RosterIq struct {
9fe022261dcc Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff changeset
    17
	Iq
9fe022261dcc Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff changeset
    18
	Query RosterQuery
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
var _ ExtendedStanza = &RosterIq{}
9fe022261dcc Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff changeset
    21
9fe022261dcc Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff changeset
    22
// Roster query/result
9fe022261dcc Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff changeset
    23
type RosterQuery struct {
9fe022261dcc Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff changeset
    24
	// Should always be NsRoster, "query"
9fe022261dcc Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff changeset
    25
	XMLName xml.Name
9fe022261dcc Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff changeset
    26
	Item []RosterItem
9fe022261dcc Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff changeset
    27
}
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
// 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
    30
type RosterItem struct {
9fe022261dcc Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff changeset
    31
	// Should always be "item"
9fe022261dcc Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff changeset
    32
	XMLName xml.Name
9fe022261dcc Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff changeset
    33
	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
    34
	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
    35
	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
    36
	Group []string
9fe022261dcc Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff changeset
    37
}
9fe022261dcc Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff changeset
    38
37
fbda8e925fdf Minor fixups to make this come closer to working. It doesn't actually
Chris Jones <chris@cjones.org>
parents: 36
diff changeset
    39
func (riq *RosterIq) MarshalXML() ([]byte, os.Error) {
fbda8e925fdf Minor fixups to make this come closer to working. It doesn't actually
Chris Jones <chris@cjones.org>
parents: 36
diff changeset
    40
	return marshalXML(riq)
fbda8e925fdf Minor fixups to make this come closer to working. It doesn't actually
Chris Jones <chris@cjones.org>
parents: 36
diff changeset
    41
}
fbda8e925fdf Minor fixups to make this come closer to working. It doesn't actually
Chris Jones <chris@cjones.org>
parents: 36
diff changeset
    42
36
9fe022261dcc Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff changeset
    43
func (riq *RosterIq) InnerMarshal(w io.Writer) os.Error {
9fe022261dcc Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff changeset
    44
	return xml.Marshal(w, riq.Query)
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
// Implicitly becomes part of NewClient's extStanza arg.
9fe022261dcc Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff changeset
    48
func rosterStanza(name *xml.Name) ExtendedStanza {
9fe022261dcc Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff changeset
    49
	return &RosterIq{}
9fe022261dcc Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff changeset
    50
}
9fe022261dcc Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff changeset
    51
9fe022261dcc Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff changeset
    52
// 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
    53
// that information.
9fe022261dcc Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff changeset
    54
func (cl *Client) fetchRoster() os.Error {
9fe022261dcc Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff changeset
    55
	iq := &RosterIq{Iq: Iq{From: cl.Jid.String(), Id: <- cl.Id,
9fe022261dcc Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff changeset
    56
		Type: "get"}, Query: RosterQuery{XMLName:
9fe022261dcc Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff changeset
    57
			xml.Name{Local: "query", Space: NsRoster}}}
9fe022261dcc Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff changeset
    58
	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
    59
	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
    60
		iq, ok := st.(*RosterIq)
9fe022261dcc Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff changeset
    61
		if !ok {
9fe022261dcc Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff changeset
    62
			ch <- os.NewError(fmt.Sprintf(
9fe022261dcc Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff changeset
    63
				"Roster query result not iq: %v", st))
9fe022261dcc Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff changeset
    64
			return false
9fe022261dcc Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff changeset
    65
		}
9fe022261dcc Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff changeset
    66
		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
    67
			ch <- iq.Error
9fe022261dcc Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff changeset
    68
			return false
9fe022261dcc Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff changeset
    69
		}
9fe022261dcc Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff changeset
    70
		q := iq.Query
9fe022261dcc Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff changeset
    71
		cl.roster = make(map[string] *RosterItem, len(q.Item))
9fe022261dcc Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff changeset
    72
		for _, item := range(q.Item) {
9fe022261dcc Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff changeset
    73
			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
    74
		}
9fe022261dcc Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff changeset
    75
		ch <- nil
9fe022261dcc Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff changeset
    76
		return false
9fe022261dcc Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff changeset
    77
	}
9fe022261dcc Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff changeset
    78
	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
    79
	cl.Out <- iq
9fe022261dcc Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff changeset
    80
	// 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
    81
	return <- ch
9fe022261dcc Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff changeset
    82
}
9fe022261dcc Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff changeset
    83
9fe022261dcc Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff changeset
    84
// 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
    85
9fe022261dcc Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff changeset
    86
// 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
    87
// 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
    88
// 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
    89
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
    90
	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
    91
	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
    92
		r[key] = val
9fe022261dcc Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff changeset
    93
	}
9fe022261dcc Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff changeset
    94
	return r
9fe022261dcc Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff changeset
    95
}