2020-01-30 12:18:05 +00:00
|
|
|
package caldav
|
|
|
|
|
|
|
|
import (
|
2020-02-05 17:05:48 +00:00
|
|
|
"bytes"
|
2020-01-30 12:51:02 +00:00
|
|
|
"fmt"
|
2020-02-24 17:19:39 +00:00
|
|
|
"mime"
|
2020-02-24 17:13:24 +00:00
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"strconv"
|
2020-02-24 17:19:39 +00:00
|
|
|
"strings"
|
2020-02-03 16:26:55 +00:00
|
|
|
"time"
|
2020-01-30 12:18:05 +00:00
|
|
|
|
2020-02-24 16:52:25 +00:00
|
|
|
"github.com/emersion/go-ical"
|
2020-01-30 12:18:05 +00:00
|
|
|
"github.com/emersion/go-webdav"
|
|
|
|
"github.com/emersion/go-webdav/internal"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Client provides access to a remote CardDAV server.
|
|
|
|
type Client struct {
|
|
|
|
*webdav.Client
|
|
|
|
|
|
|
|
ic *internal.Client
|
|
|
|
}
|
|
|
|
|
2020-02-19 15:02:49 +00:00
|
|
|
func NewClient(c webdav.HTTPClient, endpoint string) (*Client, error) {
|
2020-01-30 12:18:05 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) FindCalendarHomeSet(principal string) (string, error) {
|
|
|
|
propfind := internal.NewPropNamePropfind(calendarHomeSetName)
|
|
|
|
resp, err := c.ic.PropfindFlat(principal, propfind)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
var prop calendarHomeSet
|
|
|
|
if err := resp.DecodeProp(&prop); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return prop.Href.Path, nil
|
|
|
|
}
|
2020-01-30 12:51:02 +00:00
|
|
|
|
|
|
|
func (c *Client) FindCalendars(calendarHomeSet string) ([]Calendar, error) {
|
|
|
|
propfind := internal.NewPropNamePropfind(
|
|
|
|
internal.ResourceTypeName,
|
|
|
|
internal.DisplayNameName,
|
|
|
|
calendarDescriptionName,
|
|
|
|
maxResourceSizeName,
|
2022-04-12 08:18:59 +01:00
|
|
|
supportedCalendarComponentSetName,
|
2020-01-30 12:51:02 +00:00
|
|
|
)
|
|
|
|
ms, err := c.ic.Propfind(calendarHomeSet, internal.DepthOne, propfind)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
l := make([]Calendar, 0, len(ms.Responses))
|
|
|
|
for _, resp := range ms.Responses {
|
|
|
|
path, err := resp.Path()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var resType internal.ResourceType
|
|
|
|
if err := resp.DecodeProp(&resType); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if !resType.Is(calendarName) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
var desc calendarDescription
|
|
|
|
if err := resp.DecodeProp(&desc); err != nil && !internal.IsNotFound(err) {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
2022-04-12 08:18:59 +01:00
|
|
|
var supportedCompSet supportedCalendarComponentSet
|
|
|
|
if err := resp.DecodeProp(&supportedCompSet); err != nil && !internal.IsNotFound(err) {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
compNames := make([]string, 0, len(supportedCompSet.Comp))
|
|
|
|
for _, comp := range supportedCompSet.Comp {
|
|
|
|
compNames = append(compNames, comp.Name)
|
|
|
|
}
|
|
|
|
|
2020-01-30 12:51:02 +00:00
|
|
|
l = append(l, Calendar{
|
2022-04-12 08:18:59 +01:00
|
|
|
Path: path,
|
|
|
|
Name: dispName.Name,
|
|
|
|
Description: desc.Description,
|
|
|
|
MaxResourceSize: maxResSize.Size,
|
|
|
|
SupportedComponentSet: compNames,
|
2020-01-30 12:51:02 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return l, nil
|
|
|
|
}
|
2020-02-03 16:26:55 +00:00
|
|
|
|
|
|
|
func encodeCalendarCompReq(c *CalendarCompRequest) (*comp, error) {
|
|
|
|
encoded := comp{Name: c.Name}
|
|
|
|
|
|
|
|
if c.AllProps {
|
|
|
|
encoded.Allprop = &struct{}{}
|
|
|
|
}
|
|
|
|
for _, name := range c.Props {
|
|
|
|
encoded.Prop = append(encoded.Prop, prop{Name: name})
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.AllComps {
|
|
|
|
encoded.Allcomp = &struct{}{}
|
|
|
|
}
|
|
|
|
for _, child := range c.Comps {
|
|
|
|
encodedChild, err := encodeCalendarCompReq(&child)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
encoded.Comp = append(encoded.Comp, *encodedChild)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &encoded, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func encodeCalendarReq(c *CalendarCompRequest) (*internal.Prop, error) {
|
|
|
|
compReq, err := encodeCalendarCompReq(c)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
calDataReq := calendarDataReq{Comp: compReq}
|
|
|
|
|
|
|
|
getLastModReq := internal.NewRawXMLElement(internal.GetLastModifiedName, nil, nil)
|
|
|
|
getETagReq := internal.NewRawXMLElement(internal.GetETagName, nil, nil)
|
|
|
|
return internal.EncodeProp(&calDataReq, getLastModReq, getETagReq)
|
|
|
|
}
|
|
|
|
|
2020-02-05 16:36:18 +00:00
|
|
|
func encodeCompFilter(filter *CompFilter) *compFilter {
|
|
|
|
encoded := compFilter{Name: filter.Name}
|
|
|
|
if !filter.Start.IsZero() || !filter.End.IsZero() {
|
|
|
|
encoded.TimeRange = &timeRange{
|
|
|
|
Start: dateWithUTCTime(filter.Start),
|
|
|
|
End: dateWithUTCTime(filter.End),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, child := range filter.Comps {
|
|
|
|
encoded.CompFilters = append(encoded.CompFilters, *encodeCompFilter(&child))
|
|
|
|
}
|
|
|
|
return &encoded
|
|
|
|
}
|
|
|
|
|
2020-02-03 16:26:55 +00:00
|
|
|
func decodeCalendarObjectList(ms *internal.Multistatus) ([]CalendarObject, error) {
|
|
|
|
addrs := make([]CalendarObject, 0, len(ms.Responses))
|
|
|
|
for _, resp := range ms.Responses {
|
|
|
|
path, err := resp.Path()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var calData calendarDataResp
|
|
|
|
if err := resp.DecodeProp(&calData); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var getLastMod internal.GetLastModified
|
|
|
|
if err := resp.DecodeProp(&getLastMod); err != nil && !internal.IsNotFound(err) {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var getETag internal.GetETag
|
|
|
|
if err := resp.DecodeProp(&getETag); err != nil && !internal.IsNotFound(err) {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-02-24 16:52:25 +00:00
|
|
|
r := bytes.NewReader(calData.Data)
|
2020-02-24 20:16:45 +00:00
|
|
|
data, err := ical.NewDecoder(r).Decode()
|
2020-02-05 17:05:48 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-02-03 16:26:55 +00:00
|
|
|
addrs = append(addrs, CalendarObject{
|
|
|
|
Path: path,
|
|
|
|
ModTime: time.Time(getLastMod.LastModified),
|
2020-02-03 20:48:31 +00:00
|
|
|
ETag: string(getETag.ETag),
|
2020-02-05 17:05:48 +00:00
|
|
|
Data: data,
|
2020-02-03 16:26:55 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return addrs, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) QueryCalendar(calendar string, query *CalendarQuery) ([]CalendarObject, error) {
|
2020-02-05 16:36:18 +00:00
|
|
|
propReq, err := encodeCalendarReq(&query.CompRequest)
|
2020-02-03 16:26:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
calendarQuery := calendarQuery{Prop: propReq}
|
2020-02-05 16:36:18 +00:00
|
|
|
calendarQuery.Filter.CompFilter = *encodeCompFilter(&query.CompFilter)
|
2020-02-03 16:26:55 +00:00
|
|
|
req, err := c.ic.NewXMLRequest("REPORT", calendar, &calendarQuery)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-05-13 14:06:16 +01:00
|
|
|
req.Header.Add("Depth", "1")
|
2020-02-03 16:26:55 +00:00
|
|
|
|
|
|
|
ms, err := c.ic.DoMultiStatus(req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return decodeCalendarObjectList(ms)
|
|
|
|
}
|
2020-02-24 17:13:24 +00:00
|
|
|
|
2020-05-13 15:45:10 +01:00
|
|
|
func (c *Client) MultiGetCalendar(path string, multiGet *CalendarMultiGet) ([]CalendarObject, error) {
|
|
|
|
propReq, err := encodeCalendarReq(&multiGet.CompRequest)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
calendarMultiget := calendarMultiget{Prop: propReq}
|
|
|
|
|
|
|
|
if multiGet == nil || len(multiGet.Paths) == 0 {
|
|
|
|
href := internal.Href{Path: path}
|
|
|
|
calendarMultiget.Hrefs = []internal.Href{href}
|
|
|
|
} else {
|
|
|
|
calendarMultiget.Hrefs = make([]internal.Href, len(multiGet.Paths))
|
|
|
|
for i, p := range multiGet.Paths {
|
|
|
|
calendarMultiget.Hrefs[i] = internal.Href{Path: p}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
req, err := c.ic.NewXMLRequest("REPORT", path, &calendarMultiget)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
req.Header.Add("Depth", "1")
|
|
|
|
|
|
|
|
ms, err := c.ic.DoMultiStatus(req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return decodeCalendarObjectList(ms)
|
|
|
|
}
|
|
|
|
|
2020-02-24 17:13:24 +00:00
|
|
|
func populateCalendarObject(co *CalendarObject, resp *http.Response) error {
|
|
|
|
if loc := resp.Header.Get("Location"); loc != "" {
|
|
|
|
u, err := url.Parse(loc)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
co.Path = u.Path
|
|
|
|
}
|
|
|
|
if etag := resp.Header.Get("ETag"); etag != "" {
|
|
|
|
etag, err := strconv.Unquote(etag)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
co.ETag = etag
|
|
|
|
}
|
|
|
|
if lastModified := resp.Header.Get("Last-Modified"); lastModified != "" {
|
|
|
|
t, err := http.ParseTime(lastModified)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
co.ModTime = t
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-02-24 17:19:39 +00:00
|
|
|
func (c *Client) GetCalendarObject(path string) (*CalendarObject, error) {
|
|
|
|
req, err := c.ic.NewRequest(http.MethodGet, path, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
req.Header.Set("Accept", ical.MIMEType)
|
|
|
|
|
|
|
|
resp, err := c.ic.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
mediaType, _, err := mime.ParseMediaType(resp.Header.Get("Content-Type"))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if !strings.EqualFold(mediaType, ical.MIMEType) {
|
|
|
|
return nil, fmt.Errorf("caldav: expected Content-Type %q, got %q", ical.MIMEType, mediaType)
|
|
|
|
}
|
|
|
|
|
2020-02-24 20:16:45 +00:00
|
|
|
cal, err := ical.NewDecoder(resp.Body).Decode()
|
2020-02-24 17:19:39 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
co := &CalendarObject{
|
|
|
|
Path: resp.Request.URL.Path,
|
|
|
|
Data: cal,
|
|
|
|
}
|
|
|
|
if err := populateCalendarObject(co, resp); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return co, nil
|
|
|
|
}
|
|
|
|
|
2020-02-24 17:13:24 +00:00
|
|
|
func (c *Client) PutCalendarObject(path string, cal *ical.Calendar) (*CalendarObject, error) {
|
|
|
|
// TODO: add support for If-None-Match and If-Match
|
|
|
|
|
|
|
|
// TODO: some servers want a Content-Length header, so we can't stream the
|
|
|
|
// request body here. See the Radicale issue:
|
|
|
|
// https://github.com/Kozea/Radicale/issues/1016
|
|
|
|
|
|
|
|
var buf bytes.Buffer
|
2020-02-24 20:16:45 +00:00
|
|
|
if err := ical.NewEncoder(&buf).Encode(cal); err != nil {
|
2020-02-24 17:13:24 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
req, err := c.ic.NewRequest(http.MethodPut, path, &buf)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
req.Header.Set("Content-Type", ical.MIMEType)
|
|
|
|
|
|
|
|
resp, err := c.ic.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
resp.Body.Close()
|
|
|
|
|
|
|
|
co := &CalendarObject{Path: path}
|
|
|
|
if err := populateCalendarObject(co, resp); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return co, nil
|
|
|
|
}
|