go-webdav/caldav/elements.go
Alex Dunmow b044a6de1a caldav: getctag, sync-collection, scheduling properties and free/busy
Calendar gains CTag (CS:getctag), SyncToken (DAV:sync-token) and
Transparent (CALDAV:schedule-calendar-transp). Three optional Backend
interfaces, checked with type assertions:

- SyncBackend answers the sync-collection REPORT (RFC 6578) and adds
  DAV:supported-report-set to calendars. ErrInvalidSyncToken gives 403
  with DAV:valid-sync-token.
- PrincipalBackend serves calendar-user-address-set, schedule-inbox-URL
  and schedule-outbox-URL on the principal, and the inbox and outbox
  collections themselves (RFC 6638 section 2).
- FreeBusyBackend answers a VFREEBUSY POST to the outbox with a
  schedule-response (RFC 6638 section 10.3), and adds
  calendar-auto-schedule to the DAV header.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-26 07:49:40 +08:00

317 lines
11 KiB
Go

package caldav
import (
"encoding/xml"
"fmt"
"time"
"git.dev.alexdunmow.com/blissful/go-webdav/internal"
)
const namespace = "urn:ietf:params:xml:ns:caldav"
var (
calendarHomeSetName = xml.Name{namespace, "calendar-home-set"}
calendarDescriptionName = xml.Name{namespace, "calendar-description"}
supportedCalendarDataName = xml.Name{namespace, "supported-calendar-data"}
supportedCalendarComponentSetName = xml.Name{namespace, "supported-calendar-component-set"}
maxResourceSizeName = xml.Name{namespace, "max-resource-size"}
calendarQueryName = xml.Name{namespace, "calendar-query"}
calendarMultigetName = xml.Name{namespace, "calendar-multiget"}
calendarName = xml.Name{namespace, "calendar"}
calendarDataName = xml.Name{namespace, "calendar-data"}
calendarColorName = xml.Name{"http://apple.com/ns/ical/", "calendar-color"}
getCTagName = xml.Name{Space: "http://calendarserver.org/ns/", Local: "getctag"}
calendarUserAddressSetName = xml.Name{Space: namespace, Local: "calendar-user-address-set"}
scheduleInboxURLName = xml.Name{Space: namespace, Local: "schedule-inbox-URL"}
scheduleOutboxURLName = xml.Name{Space: namespace, Local: "schedule-outbox-URL"}
scheduleInboxName = xml.Name{Space: namespace, Local: "schedule-inbox"}
scheduleOutboxName = xml.Name{Space: namespace, Local: "schedule-outbox"}
scheduleCalendarTranspName = xml.Name{Space: namespace, Local: "schedule-calendar-transp"}
)
// https://tools.ietf.org/html/rfc4791#section-6.2.1
type calendarHomeSet struct {
XMLName xml.Name `xml:"urn:ietf:params:xml:ns:caldav calendar-home-set"`
Href internal.Href `xml:"DAV: href"`
}
func (a *calendarHomeSet) GetXMLName() xml.Name {
return calendarHomeSetName
}
// https://tools.ietf.org/html/rfc4791#section-5.2.1
type calendarDescription struct {
XMLName xml.Name `xml:"urn:ietf:params:xml:ns:caldav calendar-description"`
Description string `xml:",chardata"`
}
// calendar-color is not defined by any RFC. It comes from Apple's
// http://apple.com/ns/ical/ namespace.
type calendarColor struct {
XMLName xml.Name `xml:"http://apple.com/ns/ical/ calendar-color"`
Color string `xml:",chardata"`
}
// getctag is not defined by any RFC. It comes from the
// http://calendarserver.org/ns/ namespace.
type getCTag struct {
XMLName xml.Name `xml:"http://calendarserver.org/ns/ getctag"`
CTag string `xml:",chardata"`
}
// https://tools.ietf.org/html/rfc6638#section-2.4.1
type calendarUserAddressSet struct {
XMLName xml.Name `xml:"urn:ietf:params:xml:ns:caldav calendar-user-address-set"`
Hrefs []internal.Href `xml:"DAV: href"`
}
// https://tools.ietf.org/html/rfc6638#section-2.2
type scheduleInboxURL struct {
XMLName xml.Name `xml:"urn:ietf:params:xml:ns:caldav schedule-inbox-URL"`
Href internal.Href `xml:"DAV: href"`
}
// https://tools.ietf.org/html/rfc6638#section-2.1
type scheduleOutboxURL struct {
XMLName xml.Name `xml:"urn:ietf:params:xml:ns:caldav schedule-outbox-URL"`
Href internal.Href `xml:"DAV: href"`
}
// https://tools.ietf.org/html/rfc6638#section-9.1
type scheduleCalendarTransp struct {
XMLName xml.Name `xml:"urn:ietf:params:xml:ns:caldav schedule-calendar-transp"`
Transparent *struct{} `xml:"urn:ietf:params:xml:ns:caldav transparent,omitempty"`
}
// https://tools.ietf.org/html/rfc6638#section-10.3
type scheduleResponse struct {
XMLName xml.Name `xml:"urn:ietf:params:xml:ns:caldav schedule-response"`
Responses []scheduleResponseStatus `xml:"response"`
}
// https://tools.ietf.org/html/rfc6638#section-10.4
type scheduleResponseStatus struct {
XMLName xml.Name `xml:"urn:ietf:params:xml:ns:caldav response"`
Recipient recipient `xml:"recipient"`
RequestStatus string `xml:"urn:ietf:params:xml:ns:caldav request-status"`
CalendarData *calendarDataResp `xml:"calendar-data,omitempty"`
}
// https://tools.ietf.org/html/rfc6638#section-10.5
type recipient struct {
XMLName xml.Name `xml:"urn:ietf:params:xml:ns:caldav recipient"`
Href internal.Href `xml:"DAV: href"`
}
// https://tools.ietf.org/html/rfc4791#section-5.2.4
type supportedCalendarData struct {
XMLName xml.Name `xml:"urn:ietf:params:xml:ns:caldav supported-calendar-data"`
Types []calendarDataType `xml:"calendar-data"`
}
// https://tools.ietf.org/html/rfc4791#section-5.2.3
type supportedCalendarComponentSet struct {
XMLName xml.Name `xml:"urn:ietf:params:xml:ns:caldav supported-calendar-component-set"`
Comp []comp `xml:"comp"`
}
// https://tools.ietf.org/html/rfc4791#section-9.6
type calendarDataType struct {
XMLName xml.Name `xml:"urn:ietf:params:xml:ns:caldav calendar-data"`
ContentType string `xml:"content-type,attr"`
Version string `xml:"version,attr"`
}
// https://tools.ietf.org/html/rfc4791#section-5.2.5
type maxResourceSize struct {
XMLName xml.Name `xml:"urn:ietf:params:xml:ns:caldav max-resource-size"`
Size int64 `xml:",chardata"`
}
// https://tools.ietf.org/html/rfc4791#section-9.5
type calendarQuery struct {
XMLName xml.Name `xml:"urn:ietf:params:xml:ns:caldav calendar-query"`
Prop *internal.Prop `xml:"DAV: prop,omitempty"`
AllProp *struct{} `xml:"DAV: allprop,omitempty"`
PropName *struct{} `xml:"DAV: propname,omitempty"`
Filter filter `xml:"filter"`
// TODO: timezone
}
// https://tools.ietf.org/html/rfc4791#section-9.10
type calendarMultiget struct {
XMLName xml.Name `xml:"urn:ietf:params:xml:ns:caldav calendar-multiget"`
Hrefs []internal.Href `xml:"DAV: href"`
Prop *internal.Prop `xml:"DAV: prop,omitempty"`
AllProp *struct{} `xml:"DAV: allprop,omitempty"`
PropName *struct{} `xml:"DAV: propname,omitempty"`
}
// https://tools.ietf.org/html/rfc4791#section-9.7
type filter struct {
XMLName xml.Name `xml:"urn:ietf:params:xml:ns:caldav filter"`
CompFilter compFilter `xml:"comp-filter"`
}
// https://tools.ietf.org/html/rfc4791#section-9.7.1
type compFilter struct {
XMLName xml.Name `xml:"urn:ietf:params:xml:ns:caldav comp-filter"`
Name string `xml:"name,attr"`
IsNotDefined *struct{} `xml:"is-not-defined,omitempty"`
TimeRange *timeRange `xml:"time-range,omitempty"`
PropFilters []propFilter `xml:"prop-filter,omitempty"`
CompFilters []compFilter `xml:"comp-filter,omitempty"`
}
// https://tools.ietf.org/html/rfc4791#section-9.7.2
type propFilter struct {
XMLName xml.Name `xml:"urn:ietf:params:xml:ns:caldav prop-filter"`
Name string `xml:"name,attr"`
IsNotDefined *struct{} `xml:"is-not-defined,omitempty"`
TimeRange *timeRange `xml:"time-range,omitempty"`
TextMatch *textMatch `xml:"text-match,omitempty"`
ParamFilter []paramFilter `xml:"param-filter,omitempty"`
}
// https://tools.ietf.org/html/rfc4791#section-9.7.3
type paramFilter struct {
XMLName xml.Name `xml:"urn:ietf:params:xml:ns:caldav param-filter"`
Name string `xml:"name,attr"`
IsNotDefined *struct{} `xml:"is-not-defined,omitempty"`
TextMatch *textMatch `xml:"text-match,omitempty"`
}
// https://tools.ietf.org/html/rfc4791#section-9.7.5
type textMatch struct {
XMLName xml.Name `xml:"urn:ietf:params:xml:ns:caldav text-match"`
Text string `xml:",chardata"`
Collation string `xml:"collation,attr,omitempty"`
NegateCondition negateCondition `xml:"negate-condition,attr,omitempty"`
}
type negateCondition bool
func (nc *negateCondition) UnmarshalText(b []byte) error {
switch s := string(b); s {
case "yes":
*nc = true
case "no":
*nc = false
default:
return fmt.Errorf("caldav: invalid negate-condition value: %q", s)
}
return nil
}
func (nc negateCondition) MarshalText() ([]byte, error) {
if nc {
return []byte("yes"), nil
}
return nil, nil
}
// https://tools.ietf.org/html/rfc4791#section-9.9
type timeRange struct {
XMLName xml.Name `xml:"urn:ietf:params:xml:ns:caldav time-range"`
Start dateWithUTCTime `xml:"start,attr,omitempty"`
End dateWithUTCTime `xml:"end,attr,omitempty"`
}
const dateWithUTCTimeLayout = "20060102T150405Z"
// dateWithUTCTime is the "date with UTC time" format defined in RFC 5545 page
// 34.
type dateWithUTCTime time.Time
func (t *dateWithUTCTime) UnmarshalText(b []byte) error {
tt, err := time.Parse(dateWithUTCTimeLayout, string(b))
if err != nil {
return err
}
*t = dateWithUTCTime(tt)
return nil
}
func (t *dateWithUTCTime) MarshalText() ([]byte, error) {
s := time.Time(*t).Format(dateWithUTCTimeLayout)
return []byte(s), nil
}
// Request variant of https://tools.ietf.org/html/rfc4791#section-9.6
type calendarDataReq struct {
XMLName xml.Name `xml:"urn:ietf:params:xml:ns:caldav calendar-data"`
Comp *comp `xml:"comp,omitempty"`
Expand *expand `xml:"expand,omitempty"`
// TODO: limit-recurrence-set, limit-freebusy-set
}
// https://tools.ietf.org/html/rfc4791#section-9.6.1
type comp struct {
XMLName xml.Name `xml:"urn:ietf:params:xml:ns:caldav comp"`
Name string `xml:"name,attr"`
Allprop *struct{} `xml:"allprop,omitempty"`
Prop []prop `xml:"prop,omitempty"`
Allcomp *struct{} `xml:"allcomp,omitempty"`
Comp []comp `xml:"comp,omitempty"`
}
type expand struct {
XMLName xml.Name `xml:"urn:ietf:params:xml:ns:caldav expand"`
Start dateWithUTCTime `xml:"start,attr"`
End dateWithUTCTime `xml:"end,attr"`
}
// https://tools.ietf.org/html/rfc4791#section-9.6.4
type prop struct {
XMLName xml.Name `xml:"urn:ietf:params:xml:ns:caldav prop"`
Name string `xml:"name,attr"`
// TODO: novalue
}
// Response variant of https://tools.ietf.org/html/rfc4791#section-9.6
type calendarDataResp struct {
XMLName xml.Name `xml:"urn:ietf:params:xml:ns:caldav calendar-data"`
Data []byte `xml:",chardata"`
}
type reportReq struct {
Query *calendarQuery
Multiget *calendarMultiget
SyncCollection *internal.SyncCollectionQuery
// TODO: CALDAV:free-busy-query
}
func (r *reportReq) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
var v interface{}
switch start.Name {
case calendarQueryName:
r.Query = &calendarQuery{}
v = r.Query
case calendarMultigetName:
r.Multiget = &calendarMultiget{}
v = r.Multiget
case internal.SyncCollectionName:
r.SyncCollection = &internal.SyncCollectionQuery{}
v = r.SyncCollection
default:
return fmt.Errorf("caldav: unsupported REPORT root %q %q", start.Name.Space, start.Name.Local)
}
return d.DecodeElement(v, &start)
}
type mkcolReq struct {
XMLName xml.Name `xml:"DAV: mkcol"`
ResourceType internal.ResourceType `xml:"set>prop>resourcetype"`
DisplayName string `xml:"set>prop>displayname"`
// TODO this could theoretically contain all addressbook properties?
}