go-webdav/carddav/client.go

240 lines
5.4 KiB
Go
Raw Normal View History

2020-01-14 21:19:54 +00:00
package carddav
import (
2020-01-14 22:44:21 +00:00
"bytes"
"fmt"
2020-01-20 10:15:22 +00:00
"net"
2020-01-14 21:19:54 +00:00
"net/http"
2020-01-20 10:15:22 +00:00
"net/url"
2020-01-20 12:17:19 +00:00
"strings"
2020-01-14 21:19:54 +00:00
2020-01-14 22:44:21 +00:00
"github.com/emersion/go-vcard"
2020-01-14 21:19:54 +00:00
"github.com/emersion/go-webdav"
"github.com/emersion/go-webdav/internal"
)
2020-01-20 10:15:22 +00:00
// Discover performs a DNS-based CardDAV service discovery as described in
// RFC 6352 section 11. It returns the URL to the CardDAV server.
func Discover(domain string) (string, error) {
// Only lookup carddavs (not carddav), plaintext connections are insecure
_, addrs, err := net.LookupSRV("carddavs", "tcp", domain)
if dnsErr, ok := err.(*net.DNSError); ok {
if dnsErr.IsTemporary {
return "", err
}
} else if err != nil {
return "", err
}
if len(addrs) == 0 {
return "", fmt.Errorf("carddav: domain doesn't have an SRV record")
}
addr := addrs[0]
2020-01-20 12:17:19 +00:00
target := strings.TrimSuffix(addr.Target, ".")
if target == "" {
return "", nil
}
2020-01-20 10:15:22 +00:00
u := url.URL{Scheme: "https"}
if addr.Port == 443 {
u.Host = addr.Target
} else {
2020-01-20 12:17:19 +00:00
u.Host = fmt.Sprintf("%v:%v", target, addr.Port)
2020-01-20 10:15:22 +00:00
}
return u.String(), nil
}
2020-01-21 20:01:18 +00:00
// Client provides access to a remote CardDAV server.
2020-01-14 21:19:54 +00:00
type Client struct {
*webdav.Client
ic *internal.Client
}
func NewClient(c *http.Client, endpoint string) (*Client, error) {
wc, err := webdav.NewClient(c, endpoint)
if err != nil {
return nil, err
}
ic, err := internal.NewClient(c, endpoint)
if err != nil {
return nil, err
}
return &Client{wc, ic}, nil
}
2020-01-15 22:45:37 +00:00
func (c *Client) SetBasicAuth(username, password string) {
c.Client.SetBasicAuth(username, password)
c.ic.SetBasicAuth(username, password)
}
2020-01-14 22:13:23 +00:00
func (c *Client) FindAddressBookHomeSet(principal string) (string, error) {
2020-01-18 11:43:47 +00:00
propfind := internal.NewPropNamePropfind(addressBookHomeSetName)
2020-01-14 21:19:54 +00:00
resp, err := c.ic.PropfindFlat(principal, propfind)
if err != nil {
return "", err
}
var prop addressbookHomeSet
if err := resp.DecodeProp(&prop); err != nil {
2020-01-14 21:19:54 +00:00
return "", err
}
return prop.Href.Path, nil
2020-01-14 21:19:54 +00:00
}
2020-01-14 22:13:23 +00:00
func (c *Client) FindAddressBooks(addressBookHomeSet string) ([]AddressBook, error) {
propfind := internal.NewPropNamePropfind(
internal.ResourceTypeName,
internal.DisplayNameName,
addressBookDescriptionName,
maxResourceSizeName,
)
2020-01-15 11:30:42 +00:00
ms, err := c.ic.Propfind(addressBookHomeSet, internal.DepthOne, propfind)
2020-01-14 22:13:23 +00:00
if err != nil {
return nil, err
}
l := make([]AddressBook, 0, len(ms.Responses))
2020-01-14 22:44:21 +00:00
for _, resp := range ms.Responses {
path, err := resp.Path()
2020-01-14 22:13:23 +00:00
if err != nil {
return nil, err
}
2020-01-21 20:04:19 +00:00
var resType internal.ResourceType
if err := resp.DecodeProp(&resType); err != nil {
2020-01-14 22:13:23 +00:00
return nil, err
}
2020-01-21 20:04:19 +00:00
if !resType.Is(addressBookName) {
2020-01-14 22:13:23 +00:00
continue
}
2020-01-21 20:04:19 +00:00
var desc addressbookDescription
if err := resp.DecodeProp(&desc); err != nil && !internal.IsNotFound(err) {
2020-01-14 22:13:23 +00:00
return nil, err
}
2020-01-21 20:04:19 +00:00
var dispName internal.DisplayName
if err := resp.DecodeProp(&dispName); err != nil && !internal.IsNotFound(err) {
return nil, err
}
var maxResSize maxResourceSize
if err := resp.DecodeProp(&maxResSize); err != nil && !internal.IsNotFound(err) {
return nil, err
}
if maxResSize.Size < 0 {
return nil, fmt.Errorf("carddav: max-resource-size must be a positive integer")
}
2020-01-14 22:13:23 +00:00
l = append(l, AddressBook{
Path: path,
2020-01-21 20:04:19 +00:00
Name: dispName.Name,
Description: desc.Description,
MaxResourceSize: maxResSize.Size,
2020-01-14 22:13:23 +00:00
})
}
return l, nil
}
2020-01-14 22:44:21 +00:00
func decodeAddressList(ms *internal.Multistatus) ([]AddressObject, error) {
addrs := make([]AddressObject, 0, len(ms.Responses))
for _, resp := range ms.Responses {
path, err := resp.Path()
if err != nil {
return nil, err
}
var addrData addressDataResp
if err := resp.DecodeProp(&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, AddressObject{
Path: path,
Card: card,
})
}
return addrs, nil
}
func (c *Client) QueryAddressBook(addressBook string, query *AddressBookQuery) ([]AddressObject, error) {
var addrDataReq addressDataReq
if query != nil {
for _, name := range query.Props {
addrDataReq.Props = append(addrDataReq.Props, prop{Name: name})
}
2020-01-14 22:44:21 +00:00
}
propReq, err := internal.EncodeProp(&addrDataReq)
if err != nil {
return nil, err
2020-01-14 22:44:21 +00:00
}
addressbookQuery := addressbookQuery{Prop: propReq}
2020-01-14 22:44:21 +00:00
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
}
return decodeAddressList(ms)
}
2020-01-14 22:44:21 +00:00
func (c *Client) MultiGetAddressBook(path string, multiGet *AddressBookMultiGet) ([]AddressObject, error) {
var addrDataReq addressDataReq
if multiGet != nil {
for _, name := range multiGet.Props {
addrDataReq.Props = append(addrDataReq.Props, prop{Name: name})
2020-01-14 22:44:21 +00:00
}
}
2020-01-14 22:44:21 +00:00
propReq, err := internal.EncodeProp(&addrDataReq)
if err != nil {
return nil, err
}
2020-01-14 22:44:21 +00:00
addressbookMultiget := addressbookMultiget{Prop: propReq}
if multiGet == nil || len(multiGet.Paths) == 0 {
href := internal.Href{Path: path}
addressbookMultiget.Hrefs = []internal.Href{href}
} else {
addressbookMultiget.Hrefs = make([]internal.Href, len(multiGet.Paths))
for i, p := range multiGet.Paths {
addressbookMultiget.Hrefs[i] = internal.Href{Path: p}
}
2020-01-14 22:44:21 +00:00
}
req, err := c.ic.NewXMLRequest("REPORT", path, &addressbookMultiget)
if err != nil {
return nil, err
}
req.Header.Add("Depth", "1")
ms, err := c.ic.DoMultiStatus(req)
if err != nil {
return nil, err
}
return decodeAddressList(ms)
2020-01-14 22:44:21 +00:00
}