go-webdav/caldav/caldav.go
Alex Dunmow bcc2aea1d9 caldav: serve principal-URL and, through PrincipalNameBackend, displayname on the principal
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-26 23:51:22 +08:00

215 lines
6.8 KiB
Go

// Package caldav provides a client and server CalDAV implementation.
//
// CalDAV is defined in RFC 4791.
package caldav
import (
"context"
"errors"
"fmt"
"time"
"github.com/emersion/go-ical"
"git.dev.alexdunmow.com/blissful/go-webdav"
"git.dev.alexdunmow.com/blissful/go-webdav/internal"
)
var CapabilityCalendar = webdav.Capability("calendar-access")
func NewCalendarHomeSet(path string) webdav.BackendSuppliedHomeSet {
return &calendarHomeSet{Href: internal.Href{Path: path}}
}
// ValidateCalendarObject checks the validity of a calendar object according to
// the contraints layed out in RFC 4791 section 4.1 and returns the only event
// type and UID occuring in this calendar, or an error if the calendar could
// not be validated.
func ValidateCalendarObject(cal *ical.Calendar) (eventType string, uid string, err error) {
// Calendar object resources contained in calendar collections
// MUST NOT specify the iCalendar METHOD property.
if prop := cal.Props.Get(ical.PropMethod); prop != nil {
return "", "", fmt.Errorf("calendar resource must not specify METHOD property")
}
for _, comp := range cal.Children {
// Calendar object resources contained in calendar collections
// MUST NOT contain more than one type of calendar component
// (e.g., VEVENT, VTODO, VJOURNAL, VFREEBUSY, etc.) with the
// exception of VTIMEZONE components, which MUST be specified
// for each unique TZID parameter value specified in the
// iCalendar object.
if comp.Name != ical.CompTimezone {
if eventType == "" {
eventType = comp.Name
}
if eventType != comp.Name {
return "", "", fmt.Errorf("conflicting event types in calendar: %s, %s", eventType, comp.Name)
}
// TODO check VTIMEZONE for each TZID?
}
// Calendar components in a calendar collection that have
// different UID property values MUST be stored in separate
// calendar object resources.
compUID, err := comp.Props.Text(ical.PropUID)
if err != nil {
return "", "", fmt.Errorf("error checking component UID: %v", err)
}
if uid == "" {
uid = compUID
}
if compUID != "" && uid != compUID {
return "", "", fmt.Errorf("conflicting UID values in calendar: %s, %s", uid, compUID)
}
}
return eventType, uid, nil
}
type Calendar struct {
Path string
Name string
Description string
MaxResourceSize int64
SupportedComponentSet []string
// Color is the value of the calendar-color property in the
// http://apple.com/ns/ical/ namespace. That property is not defined by
// CalDAV. The value is a hexadecimal string following the #rrggbb syntax,
// or #rrggbbaa with an alpha channel, as Apple Calendar writes it.
Color string
// ReadOnly reports that the current user may only read this calendar. It
// controls the DAV:current-user-privilege-set reported by the server. The
// client sets it when the server reports a DAV:current-user-privilege-set
// that doesn't grant DAV:write.
ReadOnly bool
// CTag is the value of the getctag property in the
// http://calendarserver.org/ns/ namespace. It changes whenever any object
// in the calendar changes.
CTag string
// SyncToken is the value of the DAV:sync-token property (RFC 6578). It
// must be a URI (RFC 6578 section 6.2).
SyncToken string
// Transparent marks a calendar whose events don't count as busy time. It
// is served as CALDAV:schedule-calendar-transp (RFC 6638 section 9.1).
Transparent bool
}
type CalendarCompRequest struct {
Name string
AllProps bool
Props []string
AllComps bool
Comps []CalendarCompRequest
Expand *CalendarExpandRequest
}
type CalendarExpandRequest struct {
Start, End time.Time
}
type CompFilter struct {
Name string
IsNotDefined bool
Start, End time.Time
Props []PropFilter
Comps []CompFilter
}
type ParamFilter struct {
Name string
IsNotDefined bool
TextMatch *TextMatch
}
type PropFilter struct {
Name string
IsNotDefined bool
Start, End time.Time
TextMatch *TextMatch
ParamFilter []ParamFilter
}
type TextMatch struct {
Text string
NegateCondition bool
}
type CalendarQuery struct {
CompRequest CalendarCompRequest
CompFilter CompFilter
}
type CalendarMultiGet struct {
Paths []string
CompRequest CalendarCompRequest
}
type CalendarObject struct {
Path string
ModTime time.Time
ContentLength int64
ETag string
Data *ical.Calendar
}
// SyncQuery is the query struct represents a sync-collection request
type SyncQuery struct {
CompRequest CalendarCompRequest
SyncToken string
Limit int // <= 0 means unlimited
}
// SyncResponse contains the returned sync-token for next time. A server's
// sync-token must be a URI (RFC 6578 section 6.2).
type SyncResponse struct {
SyncToken string
Updated []CalendarObject
Deleted []string
}
// ErrInvalidSyncToken is returned by SyncBackend.SyncCalendar when the token
// is unknown or no longer valid. The client then starts again with an empty
// token.
var ErrInvalidSyncToken = errors.New("caldav: invalid sync token")
// SyncBackend is an optional Backend interface answering the sync-collection
// REPORT (RFC 6578) on calendars.
type SyncBackend interface {
// SyncCalendar returns the objects changed and deleted in the calendar at
// path since token, and the token for the next sync. An empty token asks
// for every object. Tokens must be URIs (RFC 6578 section 6.2).
SyncCalendar(ctx context.Context, path, token string, req *CalendarCompRequest) (*SyncResponse, error)
}
// PrincipalBackend is an optional Backend interface serving the scheduling
// properties of the current user principal (RFC 6638 section 2).
type PrincipalBackend interface {
// CalendarUserAddresses returns the email addresses of the current user,
// served as mailto: URIs.
CalendarUserAddresses(ctx context.Context) ([]string, error)
ScheduleInboxPath(ctx context.Context) (string, error)
ScheduleOutboxPath(ctx context.Context) (string, error)
}
// PrincipalNameBackend is an optional Backend interface naming the current
// user principal, served as its DAV:displayname.
type PrincipalNameBackend interface {
PrincipalDisplayName(ctx context.Context) (string, error)
}
// FreeBusyBackend is an optional Backend interface answering free/busy
// requests POSTed to the schedule outbox (RFC 6638 section 5). The Backend
// must also implement PrincipalBackend.
//
// Implementing it advertises calendar-auto-schedule in the DAV header, so
// clients stop sending invitations themselves: the Backend then owns
// scheduling for every object it stores.
type FreeBusyBackend interface {
// FreeBusy answers a VFREEBUSY request with one VFREEBUSY per ATTENDEE of
// the request, each carrying that ATTENDEE. A VFREEBUSY may carry a
// REQUEST-STATUS; it defaults to "2.0;Success".
FreeBusy(ctx context.Context, req *ical.Calendar) (*ical.Calendar, error)
}