carddav: add Client.QueryAddressBook

This commit is contained in:
Simon Ser 2020-01-14 23:44:21 +01:00
parent 9dfabd89c8
commit 56c162197b
No known key found for this signature in database
GPG Key ID: 0FDE7BE0E88F5E48
4 changed files with 98 additions and 3 deletions

View File

@ -2,6 +2,8 @@ package carddav
import ( import (
"encoding/xml" "encoding/xml"
"github.com/emersion/go-vcard"
) )
const namespace = "urn:ietf:params:xml:ns:carddav" const namespace = "urn:ietf:params:xml:ns:carddav"
@ -14,5 +16,10 @@ type AddressBook struct {
var addressBookName = xml.Name{namespace, "addressbook"} var addressBookName = xml.Name{namespace, "addressbook"}
type AddressBookQuery struct { type AddressBookQuery struct {
AddressDataProps []string Props []string
}
type Address struct {
Href string
Card vcard.Card
} }

View File

@ -1,9 +1,11 @@
package carddav package carddav
import ( import (
"bytes"
"encoding/xml" "encoding/xml"
"net/http" "net/http"
"github.com/emersion/go-vcard"
"github.com/emersion/go-webdav" "github.com/emersion/go-webdav"
"github.com/emersion/go-webdav/internal" "github.com/emersion/go-webdav/internal"
) )
@ -61,8 +63,7 @@ func (c *Client) FindAddressBooks(addressBookHomeSet string) ([]AddressBook, err
} }
l := make([]AddressBook, 0, len(ms.Responses)) l := make([]AddressBook, 0, len(ms.Responses))
for i := range ms.Responses { for _, resp := range ms.Responses {
resp := &ms.Responses[i]
href, err := resp.Href() href, err := resp.Href()
if err != nil { if err != nil {
return nil, err return nil, err
@ -89,3 +90,56 @@ func (c *Client) FindAddressBooks(addressBookHomeSet string) ([]AddressBook, err
return l, nil return l, nil
} }
func (c *Client) QueryAddressBook(addressBook string, query *AddressBookQuery) ([]Address, error) {
// TODO: add a better way to format the request
addrProps := make([]internal.RawXMLValue, 0, len(query.Props))
for _, name := range query.Props {
addrProps = append(addrProps, *newProp(name, false))
}
addrDataName := xml.Name{namespace, "address-data"}
addrDataReq := internal.NewRawXMLElement(addrDataName, nil, addrProps)
addressbookQuery := addressbookQuery{
Prop: &internal.Prop{Raw: []internal.RawXMLValue{*addrDataReq}},
}
req, err := c.ic.NewXMLRequest("REPORT", addressBook, &addressbookQuery)
if err != nil {
return nil, err
}
req.Header.Add("Depth", "1")
ms, err := c.ic.DoMultiStatus(req)
if err != nil {
return nil, err
}
addrs := make([]Address, 0, len(ms.Responses))
for _, resp := range ms.Responses {
href, err := resp.Href()
if err != nil {
return nil, err
}
var addrData addressDataResp
if err := resp.DecodeProp(addrDataName, &addrData); err != nil {
return nil, err
}
r := bytes.NewReader(addrData.Data)
card, err := vcard.NewDecoder(r).Decode()
if err != nil {
return nil, err
}
addrs = append(addrs, Address{
Href: href,
Card: card,
})
}
return addrs, nil
}

View File

@ -2,6 +2,8 @@ package carddav
import ( import (
"encoding/xml" "encoding/xml"
"github.com/emersion/go-webdav/internal"
) )
type addressbookHomeSet struct { type addressbookHomeSet struct {
@ -21,3 +23,33 @@ type addressbookQuery struct {
// TODO: DAV:allprop | DAV:propname // TODO: DAV:allprop | DAV:propname
// TODO: filter, limit? // TODO: filter, limit?
} }
func newProp(name string, noValue bool) *internal.RawXMLValue {
attrs := []xml.Attr{{Name: xml.Name{namespace, "name"}, Value: name}}
if noValue {
attrs = append(attrs, xml.Attr{Name: xml.Name{namespace, "novalue"}, Value: "yes"})
}
xmlName := xml.Name{namespace, "prop"}
return internal.NewRawXMLElement(xmlName, attrs, nil)
}
// https://tools.ietf.org/html/rfc6352#section-10.4
type addressDataReq struct {
XMLName xml.Name `xml:"urn:ietf:params:xml:ns:carddav address-data"`
Props []prop `xml:"prop"`
// TODO: allprop
}
// https://tools.ietf.org/html/rfc6352#section-10.4.2
type prop struct {
XMLName xml.Name `xml:"urn:ietf:params:xml:ns:carddav prop"`
Name string `xml:"name,attr"`
// TODO: novalue
}
// https://tools.ietf.org/html/rfc6352#section-10.4
type addressDataResp struct {
XMLName xml.Name `xml:"urn:ietf:params:xml:ns:carddav address-data"`
Data []byte `xml:",chardata"`
}

2
go.mod
View File

@ -1,3 +1,5 @@
module github.com/emersion/go-webdav module github.com/emersion/go-webdav
go 1.13 go 1.13
require github.com/emersion/go-vcard v0.0.0-20191221110513-5f81fa0d3cc7