diff --git a/caldav/caldav.go b/caldav/caldav.go
index 1df6c66..a13c7ba 100644
--- a/caldav/caldav.go
+++ b/caldav/caldav.go
@@ -70,7 +70,9 @@ type Calendar struct {
MaxResourceSize int64
SupportedComponentSet []string
// ReadOnly reports that the current user may only read this calendar. It
- // controls the DAV:current-user-privilege-set reported by the server.
+ // 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
}
diff --git a/caldav/client.go b/caldav/client.go
index ba52442..ca033f7 100644
--- a/caldav/client.go
+++ b/caldav/client.go
@@ -24,6 +24,7 @@ var calendarProps = []xml.Name{
calendarDescriptionName,
maxResourceSizeName,
supportedCalendarComponentSetName,
+ internal.CurrentUserPrivilegeSetName,
}
// DiscoverContextURL performs a DNS-based CardDAV service discovery as
@@ -141,6 +142,14 @@ func decodeCalendar(path string, resp *internal.Response) (*Calendar, error) {
return nil, err
}
+ var privSet internal.CurrentUserPrivilegeSet
+ readOnly := false
+ if err := resp.DecodeProp(&privSet); err == nil {
+ readOnly = privSet.ReadOnly()
+ } else if !internal.IsNotFound(err) {
+ return nil, err
+ }
+
compNames := make([]string, 0, len(supportedCompSet.Comp))
for _, comp := range supportedCompSet.Comp {
compNames = append(compNames, comp.Name)
@@ -152,6 +161,7 @@ func decodeCalendar(path string, resp *internal.Response) (*Calendar, error) {
Description: desc.Description,
MaxResourceSize: maxResSize.Size,
SupportedComponentSet: compNames,
+ ReadOnly: readOnly,
}, nil
}
diff --git a/caldav/client_test.go b/caldav/client_test.go
new file mode 100644
index 0000000..821243b
--- /dev/null
+++ b/caldav/client_test.go
@@ -0,0 +1,99 @@
+package caldav
+
+import (
+ "context"
+ "net/http"
+ "net/http/httptest"
+ "testing"
+)
+
+const findCalendarsReadOnlyResponse = `
+
+
+ /cal/read-only/
+
+
+
+
+
+
+
+ HTTP/1.1 200 OK
+
+
+
+ /cal/write-content/
+
+
+
+
+
+
+
+
+ HTTP/1.1 200 OK
+
+
+
+ /cal/write/
+
+
+
+
+
+
+
+
+
+ HTTP/1.1 200 OK
+
+
+
+ /cal/unknown/
+
+
+
+
+ HTTP/1.1 200 OK
+
+
+
+
+
+ HTTP/1.1 404 Not Found
+
+
+`
+
+func TestFindCalendarsReadOnly(t *testing.T) {
+ srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ w.Header().Set("Content-Type", "application/xml; charset=utf-8")
+ w.WriteHeader(http.StatusMultiStatus)
+ w.Write([]byte(findCalendarsReadOnlyResponse))
+ }))
+ defer srv.Close()
+
+ c, err := NewClient(http.DefaultClient, srv.URL)
+ if err != nil {
+ t.Fatalf("NewClient: %v", err)
+ }
+ cals, err := c.FindCalendars(context.Background(), "/cal/")
+ if err != nil {
+ t.Fatalf("FindCalendars: %v", err)
+ }
+
+ want := map[string]bool{
+ "/cal/read-only/": true,
+ "/cal/write-content/": true,
+ "/cal/write/": false,
+ "/cal/unknown/": false,
+ }
+ if len(cals) != len(want) {
+ t.Fatalf("got %d calendars, want %d", len(cals), len(want))
+ }
+ for _, cal := range cals {
+ if cal.ReadOnly != want[cal.Path] {
+ t.Errorf("%s: ReadOnly = %v, want %v", cal.Path, cal.ReadOnly, want[cal.Path])
+ }
+ }
+}
diff --git a/carddav/carddav.go b/carddav/carddav.go
index d6a1bf5..7471549 100644
--- a/carddav/carddav.go
+++ b/carddav/carddav.go
@@ -30,6 +30,8 @@ type AddressBook struct {
SupportedAddressData []AddressDataType
// ReadOnly reports that the current user may only read this address book.
// 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
}
diff --git a/carddav/client.go b/carddav/client.go
index c0d57fa..df19243 100644
--- a/carddav/client.go
+++ b/carddav/client.go
@@ -84,6 +84,7 @@ func (c *Client) FindAddressBooks(ctx context.Context, addressBookHomeSet string
addressBookDescriptionName,
maxResourceSizeName,
supportedAddressDataName,
+ internal.CurrentUserPrivilegeSetName,
)
ms, err := c.ic.PropFind(ctx, addressBookHomeSet, internal.DepthOne, propfind)
if err != nil {
@@ -130,12 +131,21 @@ func (c *Client) FindAddressBooks(ctx context.Context, addressBookHomeSet string
return nil, err
}
+ var privSet internal.CurrentUserPrivilegeSet
+ readOnly := false
+ if err := resp.DecodeProp(&privSet); err == nil {
+ readOnly = privSet.ReadOnly()
+ } else if !internal.IsNotFound(err) {
+ return nil, err
+ }
+
l = append(l, AddressBook{
Path: path,
Name: dispName.Name,
Description: desc.Description,
MaxResourceSize: maxResSize.Size,
SupportedAddressData: decodeSupportedAddressData(&supported),
+ ReadOnly: readOnly,
})
}
diff --git a/carddav/client_test.go b/carddav/client_test.go
new file mode 100644
index 0000000..3c67ee5
--- /dev/null
+++ b/carddav/client_test.go
@@ -0,0 +1,69 @@
+package carddav
+
+import (
+ "context"
+ "net/http"
+ "net/http/httptest"
+ "testing"
+)
+
+const findAddressBooksReadOnlyResponse = `
+
+
+ /contacts/read-only/
+
+
+
+
+
+
+
+ HTTP/1.1 200 OK
+
+
+
+ /contacts/all/
+
+
+
+
+
+
+
+
+
+ HTTP/1.1 200 OK
+
+
+`
+
+func TestFindAddressBooksReadOnly(t *testing.T) {
+ srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ w.Header().Set("Content-Type", "application/xml; charset=utf-8")
+ w.WriteHeader(http.StatusMultiStatus)
+ w.Write([]byte(findAddressBooksReadOnlyResponse))
+ }))
+ defer srv.Close()
+
+ c, err := NewClient(http.DefaultClient, srv.URL)
+ if err != nil {
+ t.Fatalf("NewClient: %v", err)
+ }
+ abs, err := c.FindAddressBooks(context.Background(), "/contacts/")
+ if err != nil {
+ t.Fatalf("FindAddressBooks: %v", err)
+ }
+
+ want := map[string]bool{
+ "/contacts/read-only/": true,
+ "/contacts/all/": false,
+ }
+ if len(abs) != len(want) {
+ t.Fatalf("got %d address books, want %d", len(abs), len(want))
+ }
+ for _, ab := range abs {
+ if ab.ReadOnly != want[ab.Path] {
+ t.Errorf("%s: ReadOnly = %v, want %v", ab.Path, ab.ReadOnly, want[ab.Path])
+ }
+ }
+}
diff --git a/internal/elements.go b/internal/elements.go
index c2bf6a9..3e72200 100644
--- a/internal/elements.go
+++ b/internal/elements.go
@@ -461,8 +461,8 @@ type Limit struct {
// https://tools.ietf.org/html/rfc3744#section-5.4
type CurrentUserPrivilegeSet struct {
- XMLName xml.Name `xml:"DAV: current-user-privilege-set"`
- Privilege []Privilege
+ XMLName xml.Name `xml:"DAV: current-user-privilege-set"`
+ Privilege []Privilege `xml:"DAV: privilege"`
}
// https://tools.ietf.org/html/rfc3744#section-5.4
@@ -481,3 +481,15 @@ func NewCurrentUserPrivilegeSet(readOnly bool) *CurrentUserPrivilegeSet {
}
return &CurrentUserPrivilegeSet{Privilege: []Privilege{priv}}
}
+
+// ReadOnly reports whether the set doesn't grant DAV:write. The set lists
+// aggregate privileges together with the ones they contain (RFC 3744 section
+// 5.4), so DAV:write is listed whenever it is granted, also through DAV:all.
+func (set *CurrentUserPrivilegeSet) ReadOnly() bool {
+ for _, priv := range set.Privilege {
+ if priv.Write != nil {
+ return false
+ }
+ }
+ return true
+}