Compare commits
6 Commits
65aeeda263
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
77498b6261 | ||
|
|
960c080f1c | ||
|
|
c420e03ca1 | ||
|
|
e496c29101 | ||
|
|
47f12834c1 | ||
|
|
b4a65a1af4 |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1,5 +1,6 @@
|
||||
.direnv
|
||||
server.log
|
||||
shell.nix
|
||||
mem.go
|
||||
nxcaldav
|
||||
in/
|
||||
|
||||
24
config.yaml
24
config.yaml
@@ -14,34 +14,10 @@ smtp:
|
||||
password_cmd: echo "Vastly-Wrinkle9-Corsage"
|
||||
|
||||
users:
|
||||
- name: alice
|
||||
password: 123
|
||||
groups:
|
||||
- family
|
||||
- name: bob
|
||||
password: abc
|
||||
groups:
|
||||
- family
|
||||
|
||||
calendars:
|
||||
- id: test
|
||||
owner: alice
|
||||
color: '#F6F5F4'
|
||||
- id: family
|
||||
owner: shared
|
||||
color: '#999999'
|
||||
access:
|
||||
- group: family
|
||||
mode: read-write
|
||||
|
||||
address_books:
|
||||
- id: contacts
|
||||
owner: alice
|
||||
- id: family
|
||||
owner: bob
|
||||
access:
|
||||
- group: family
|
||||
mode: read-write
|
||||
|
||||
aggregates:
|
||||
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
#!/usr/bin/env python
|
||||
import os
|
||||
import argparse
|
||||
import psycopg2
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
#!/usr/bin/env python
|
||||
import os
|
||||
import argparse
|
||||
import psycopg2
|
||||
|
||||
@@ -102,7 +102,7 @@ func (b *DBBackend) initSchema(ctx context.Context) error {
|
||||
)`,
|
||||
`CREATE TABLE IF NOT EXISTS calendar_objects (
|
||||
id SERIAL PRIMARY KEY,
|
||||
calendar_id INTEGER REFERENCES calendars(id) ON DELETE CASCADE,
|
||||
calendar_id INTEGER NOT NULL REFERENCES calendars(id) ON DELETE CASCADE,
|
||||
path TEXT NOT NULL,
|
||||
data TEXT NOT NULL,
|
||||
etag TEXT NOT NULL,
|
||||
@@ -110,20 +110,20 @@ func (b *DBBackend) initSchema(ctx context.Context) error {
|
||||
)`,
|
||||
`CREATE TABLE IF NOT EXISTS addressbooks (
|
||||
id SERIAL PRIMARY KEY,
|
||||
owner_id INTEGER REFERENCES users(id) ON DELETE CASCADE,
|
||||
owner_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
||||
path TEXT UNIQUE NOT NULL,
|
||||
name TEXT,
|
||||
description TEXT
|
||||
)`,
|
||||
`CREATE TABLE IF NOT EXISTS addressbook_access (
|
||||
addressbook_id INTEGER REFERENCES addressbooks(id) ON DELETE CASCADE,
|
||||
user_id INTEGER REFERENCES users(id) ON DELETE CASCADE,
|
||||
addressbook_id INTEGER NOT NULL REFERENCES addressbooks(id) ON DELETE CASCADE,
|
||||
user_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
||||
mode TEXT NOT NULL,
|
||||
PRIMARY KEY (addressbook_id, user_id)
|
||||
)`,
|
||||
`CREATE TABLE IF NOT EXISTS addressbook_objects (
|
||||
id SERIAL PRIMARY KEY,
|
||||
addressbook_id INTEGER REFERENCES addressbooks(id) ON DELETE CASCADE,
|
||||
addressbook_id INTEGER NOT NULL REFERENCES addressbooks(id) ON DELETE CASCADE,
|
||||
path TEXT NOT NULL,
|
||||
data TEXT NOT NULL,
|
||||
etag TEXT NOT NULL,
|
||||
@@ -229,6 +229,7 @@ func (b *DBBackend) syncConfig(ctx context.Context, cfg *config.Config) error {
|
||||
if a.User != "" {
|
||||
tUsers = append(tUsers, a.User)
|
||||
}
|
||||
tUsers = append(tUsers, a.Users...)
|
||||
if a.Group != "" {
|
||||
tUsers = append(tUsers, groupMembers[a.Group]...)
|
||||
}
|
||||
@@ -257,6 +258,7 @@ func (b *DBBackend) syncConfig(ctx context.Context, cfg *config.Config) error {
|
||||
for _, a := range c.Access {
|
||||
if a.ICS != "" {
|
||||
pubPath := prefix + fmt.Sprintf("/public/%s/%s.ics", c.Owner, c.ID)
|
||||
log.Printf("pp: %s", pubPath)
|
||||
b.publicAccess[pubPath] = publicInfo{
|
||||
InternalPath: path,
|
||||
Mode: a.ICS,
|
||||
@@ -299,6 +301,7 @@ func (b *DBBackend) syncConfig(ctx context.Context, cfg *config.Config) error {
|
||||
if a.User != "" {
|
||||
tUsers = append(tUsers, a.User)
|
||||
}
|
||||
tUsers = append(tUsers, a.Users...)
|
||||
if a.Group != "" {
|
||||
tUsers = append(tUsers, groupMembers[a.Group]...)
|
||||
}
|
||||
@@ -350,6 +353,7 @@ func (b *DBBackend) syncConfig(ctx context.Context, cfg *config.Config) error {
|
||||
if a.User != "" {
|
||||
tUsers = append(tUsers, a.User)
|
||||
}
|
||||
tUsers = append(tUsers, a.Users...)
|
||||
if a.Group != "" {
|
||||
tUsers = append(tUsers, groupMembers[a.Group]...)
|
||||
}
|
||||
@@ -765,14 +769,14 @@ func (b *DBBackend) listAggregateObjects(ctx context.Context, aggPath string, ag
|
||||
username, _ := b.getUsername(ctx)
|
||||
|
||||
for _, sourceID := range agg.Sources {
|
||||
sourcePath := b.prefix + fmt.Sprintf("/%s/calendars/%s/", agg.Owner, sourceID)
|
||||
|
||||
var calID int
|
||||
var ownerName string
|
||||
var sourcePath string
|
||||
err := b.pool.QueryRow(ctx, `
|
||||
SELECT c.id, u.name FROM calendars c
|
||||
SELECT c.id, u.name, c.path FROM calendars c
|
||||
JOIN users u ON c.owner_id = u.id
|
||||
WHERE c.path = $1`, sourcePath).Scan(&calID, &ownerName)
|
||||
WHERE c.name = $1`, sourceID).Scan(&calID, &ownerName, &sourcePath)
|
||||
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
@@ -797,7 +801,7 @@ func (b *DBBackend) listAggregateObjects(ctx context.Context, aggPath string, ag
|
||||
}
|
||||
|
||||
for _, obj := range objs {
|
||||
// Prepend source name so Diane knows this is a "[calendar]" event
|
||||
// Prepend source name so user knows this is a "[calendar]" event
|
||||
for _, child := range obj.Data.Children {
|
||||
if child.Name == "VEVENT" || child.Name == "VTODO" {
|
||||
descr := child.Props.Get("DESCRIPTION")
|
||||
@@ -828,7 +832,7 @@ func (b *DBBackend) GetCalendarObject(ctx context.Context, p string, req *caldav
|
||||
fileName := path.Base(p)
|
||||
|
||||
// Step 1: Check if this is a request for a virtual item in an aggregate
|
||||
if agg, ok := b.aggregates[dirPath]; ok {
|
||||
if _, ok := b.aggregates[dirPath]; ok {
|
||||
|
||||
hasAccess := slices.Contains(b.userAggs[username], dirPath)
|
||||
if !hasAccess {
|
||||
@@ -843,10 +847,10 @@ func (b *DBBackend) GetCalendarObject(ctx context.Context, p string, req *caldav
|
||||
sourceID := parts[0]
|
||||
realFileName := parts[1]
|
||||
|
||||
sourcePath := b.prefix + fmt.Sprintf("/%s/calendars/%s/", agg.Owner, sourceID)
|
||||
var calID int
|
||||
var ownerName string
|
||||
err := b.pool.QueryRow(ctx, "SELECT c.id, u.name FROM calendars c JOIN users u ON c.owner_id = u.id WHERE c.path = $1", sourcePath).Scan(&calID, &ownerName)
|
||||
var sourcePath string
|
||||
err := b.pool.QueryRow(ctx, "SELECT c.id, u.name, c.path FROM calendars c JOIN users u ON c.owner_id = u.id WHERE c.name = $1", sourceID).Scan(&calID, &ownerName, &sourcePath)
|
||||
if err != nil {
|
||||
return nil, webdav.NewHTTPError(http.StatusNotFound, errors.New("source calendar not found"))
|
||||
}
|
||||
@@ -1465,3 +1469,18 @@ func (b *DBBackend) DeleteAddressObject(ctx context.Context, p string) error {
|
||||
func (b *DBBackend) QueryAddressObjects(ctx context.Context, p string, query *carddav.AddressBookQuery) ([]carddav.AddressObject, error) {
|
||||
return b.ListAddressObjects(ctx, p, nil)
|
||||
}
|
||||
|
||||
func (b *DBBackend) HasAddressBooks(ctx context.Context) (bool, error) {
|
||||
username, err := b.getUsername(ctx)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
var exists bool
|
||||
err = b.pool.QueryRow(ctx, `
|
||||
SELECT EXISTS (
|
||||
SELECT 1 FROM addressbooks
|
||||
WHERE owner_id = (SELECT id FROM users WHERE name = $1)
|
||||
OR id IN (SELECT addressbook_id FROM addressbook_access WHERE user_id = (SELECT id FROM users WHERE name = $1))
|
||||
)`, username).Scan(&exists)
|
||||
return exists, err
|
||||
}
|
||||
|
||||
@@ -12,11 +12,12 @@ import (
|
||||
)
|
||||
|
||||
type Access struct {
|
||||
User string `yaml:"user,omitempty"`
|
||||
Group string `yaml:"group,omitempty"`
|
||||
Groups string `yaml:"groups,omitempty"`
|
||||
Mode string `yaml:"mode"` // "read-only" or "read-write"
|
||||
ICS string `yaml:"ics,omitempty"`
|
||||
User string `yaml:"user,omitempty"`
|
||||
Users []string `yaml:"users,omitempty"`
|
||||
Group string `yaml:"group,omitempty"`
|
||||
Groups string `yaml:"groups,omitempty"`
|
||||
Mode string `yaml:"mode"` // "read-only" or "read-write"
|
||||
ICS string `yaml:"ics,omitempty"`
|
||||
}
|
||||
|
||||
type Calendar struct {
|
||||
|
||||
@@ -39,9 +39,19 @@ func InjectColor(r *http.Request, ctx context.Context, handler http.Handler, w h
|
||||
body := buf.Bytes()
|
||||
|
||||
// this models after the Radicale Response, largely AI code
|
||||
// 1. Add namespaces to the root multistatus tag
|
||||
reMultistatus := regexp.MustCompile(`(<[a-zA-Z0-9]*:?multistatus)`)
|
||||
body = reMultistatus.ReplaceAll(body, []byte(`$1 xmlns:ICAL="http://apple.com/ns/ical/" xmlns:C="urn:ietf:params:xml:ns:caldav" xmlns:CS="http://calendarserver.org/ns/"`))
|
||||
// 1. Add namespaces to the root multistatus tag only if they are missing
|
||||
if !bytes.Contains(body, []byte("xmlns:ICAL=")) {
|
||||
reMultistatus := regexp.MustCompile(`(<[a-zA-Z0-9]*:?multistatus)`)
|
||||
body = reMultistatus.ReplaceAll(body, []byte(`$1 xmlns:ICAL="http://apple.com/ns/ical/"`))
|
||||
}
|
||||
if !bytes.Contains(body, []byte("xmlns:C=")) && !bytes.Contains(body, []byte("xmlns:c=")) {
|
||||
reMultistatus := regexp.MustCompile(`(<[a-zA-Z0-9]*:?multistatus)`)
|
||||
body = reMultistatus.ReplaceAll(body, []byte(`$1 xmlns:C="urn:ietf:params:xml:ns:caldav"`))
|
||||
}
|
||||
if !bytes.Contains(body, []byte("xmlns:CS=")) {
|
||||
reMultistatus := regexp.MustCompile(`(<[a-zA-Z0-9]*:?multistatus)`)
|
||||
body = reMultistatus.ReplaceAll(body, []byte(`$1 xmlns:CS="http://calendarserver.org/ns/"`))
|
||||
}
|
||||
|
||||
// 2. Response processing
|
||||
reResponse := regexp.MustCompile(`(?s)<[a-zA-Z0-9]*:?response.*?>.*?</[a-zA-Z0-9]*:?response>`)
|
||||
@@ -62,7 +72,7 @@ func InjectColor(r *http.Request, ctx context.Context, handler http.Handler, w h
|
||||
return resp
|
||||
}
|
||||
|
||||
// 1. Strip any existing conflicting tags that might be in 404 blocks
|
||||
// 1. Strip any existing conflicting tags that might be in 404 blocks (non-greedy)
|
||||
reStrip := regexp.MustCompile(`(?s)<[a-zA-Z0-9]*:?(calendar-color|getctag|calendar-order).*?/>|<[a-zA-Z0-9]*:?(calendar-color|getctag|calendar-order).*?>.*?</[a-zA-Z0-9]*:?(calendar-color|getctag|calendar-order)>`)
|
||||
resp = reStrip.ReplaceAll(resp, []byte(""))
|
||||
|
||||
@@ -107,10 +117,23 @@ func InjectColor(r *http.Request, ctx context.Context, handler http.Handler, w h
|
||||
w.Write(body)
|
||||
}
|
||||
|
||||
// modify caledar probfind
|
||||
//
|
||||
// this again modeled after the Radicale response
|
||||
// Largly AI generated agian
|
||||
func HandleDiscoveryOptions(r *http.Request, ctx context.Context, handler http.Handler, w http.ResponseWriter, be *backend.DBBackend) {
|
||||
buf := &bytes.Buffer{}
|
||||
rw := &responseWriter{w, buf, http.StatusOK}
|
||||
handler.ServeHTTP(rw, r.WithContext(ctx))
|
||||
|
||||
if has, _ := be.HasAddressBooks(ctx); has {
|
||||
dav := w.Header().Get("DAV")
|
||||
if dav == "" {
|
||||
w.Header().Set("DAV", "1, 3, addressbook, calendar-access")
|
||||
} else if !strings.Contains(dav, "addressbook") {
|
||||
w.Header().Set("DAV", dav+", addressbook")
|
||||
}
|
||||
}
|
||||
w.WriteHeader(rw.status)
|
||||
w.Write(buf.Bytes())
|
||||
}
|
||||
|
||||
func HandleDiscoveryPropfind(r *http.Request, ctx context.Context, handler http.Handler, w http.ResponseWriter, be *backend.DBBackend) {
|
||||
reqBody, _ := io.ReadAll(r.Body)
|
||||
r.Body = io.NopCloser(bytes.NewBuffer(reqBody))
|
||||
@@ -121,9 +144,19 @@ func HandleDiscoveryPropfind(r *http.Request, ctx context.Context, handler http.
|
||||
|
||||
body := buf.Bytes()
|
||||
|
||||
// 1. Unconditionally add namespaces to root tag
|
||||
reMultistatus := regexp.MustCompile(`(<[a-zA-Z0-9]*:?multistatus)`)
|
||||
body = reMultistatus.ReplaceAll(body, []byte(`$1 xmlns:C="urn:ietf:params:xml:ns:caldav" xmlns:card="urn:ietf:params:xml:ns:carddav"`))
|
||||
calHome, _ := be.CalendarHomeSetPath(ctx)
|
||||
cardHome, _ := be.AddressBookHomeSetPath(ctx)
|
||||
hasAddressBooks, _ := be.HasAddressBooks(ctx)
|
||||
|
||||
// Ensure DAV: addressbook header is present if user has address books
|
||||
if hasAddressBooks {
|
||||
dav := w.Header().Get("DAV")
|
||||
if dav == "" {
|
||||
w.Header().Set("DAV", "1, 3, addressbook, calendar-access")
|
||||
} else if !strings.Contains(dav, "addressbook") {
|
||||
w.Header().Set("DAV", dav+", addressbook")
|
||||
}
|
||||
}
|
||||
|
||||
// 2. Response processing
|
||||
reResponse := regexp.MustCompile(`(?s)<[a-zA-Z0-9]*:?response.*?>.*?</[a-zA-Z0-9]*:?response>`)
|
||||
@@ -139,10 +172,7 @@ func HandleDiscoveryPropfind(r *http.Request, ctx context.Context, handler http.
|
||||
return resp
|
||||
}
|
||||
|
||||
calHome, _ := be.CalendarHomeSetPath(ctx)
|
||||
cardHome, _ := be.AddressBookHomeSetPath(ctx)
|
||||
|
||||
// Strip these tags ONLY from non-200 propstats
|
||||
// Strip these tags ONLY from non-200 propstats to avoid duplicates or 404 overrides
|
||||
resp = rePropstat.ReplaceAllFunc(resp, func(ps []byte) []byte {
|
||||
if !reStatusOk.Match(ps) {
|
||||
reTags := regexp.MustCompile(`(?s)<[a-zA-Z0-9:]*(calendar-home-set|addressbook-home-set).*?/>|<[a-zA-Z0-9:]*(calendar-home-set|addressbook-home-set).*?>.*?</[a-zA-Z0-9:]*(calendar-home-set|addressbook-home-set)>`)
|
||||
@@ -152,11 +182,13 @@ func HandleDiscoveryPropfind(r *http.Request, ctx context.Context, handler http.
|
||||
})
|
||||
|
||||
props := ""
|
||||
// Inject calendar-home-set if missing (with local namespace definition for safety)
|
||||
if calHome != "" && !strings.Contains(string(resp), "calendar-home-set") {
|
||||
props += fmt.Sprintf("<C:calendar-home-set><href xmlns=\"DAV:\">%s</href></C:calendar-home-set>", calHome)
|
||||
props += fmt.Sprintf("<C:calendar-home-set xmlns:C=\"urn:ietf:params:xml:ns:caldav\"><href xmlns=\"DAV:\">%s</href></C:calendar-home-set>", calHome)
|
||||
}
|
||||
if cardHome != "" && !strings.Contains(string(resp), "addressbook-home-set") {
|
||||
props += fmt.Sprintf("<card:addressbook-home-set><href xmlns=\"DAV:\">%s</href></card:addressbook-home-set>", cardHome)
|
||||
// Inject addressbook-home-set if missing and user has address books
|
||||
if hasAddressBooks && cardHome != "" && !strings.Contains(string(resp), "addressbook-home-set") {
|
||||
props += fmt.Sprintf("<CARD:addressbook-home-set xmlns:CARD=\"urn:ietf:params:xml:ns:carddav\"><href xmlns=\"DAV:\">%s</href></CARD:addressbook-home-set>", cardHome)
|
||||
}
|
||||
|
||||
if props == "" {
|
||||
|
||||
25
main.go
25
main.go
@@ -106,20 +106,25 @@ func main() {
|
||||
principalPath := prefix + fmt.Sprintf("/%s/", user)
|
||||
ctx := context.WithValue(r.Context(), "principal", principalPath)
|
||||
|
||||
// set header for carddav
|
||||
// set header for carddav if user has address books
|
||||
if slices.Contains([]string{
|
||||
"/", prefix + "/",
|
||||
"/.well-known/carddav",
|
||||
prefix + "/.well-known/carddav",
|
||||
principalPath,
|
||||
strings.TrimSuffix(principalPath, "/"),
|
||||
}, r.URL.Path) || strings.Contains(r.URL.Path, "/addressbooks/") {
|
||||
w.Header().Add("DAV", "addressbook")
|
||||
if has, _ := be.HasAddressBooks(ctx); has {
|
||||
w.Header().Add("DAV", "addressbook")
|
||||
}
|
||||
}
|
||||
|
||||
// for caldav discovery
|
||||
// for caldav and carddav discovery
|
||||
if slices.Contains([]string{
|
||||
"/.well-known/caldav",
|
||||
prefix + "/.well-known/caldav",
|
||||
"/.well-known/carddav",
|
||||
prefix + "/.well-known/carddav",
|
||||
}, r.URL.Path) {
|
||||
http.Redirect(w, r, fmt.Sprintf("%s://%s%s", scheme, r.Host, principalPath), http.StatusMovedPermanently)
|
||||
return
|
||||
@@ -142,21 +147,17 @@ func main() {
|
||||
// catch weird requests
|
||||
} else {
|
||||
|
||||
if strings.HasSuffix(r.URL.Path, user+"/") || strings.HasSuffix(r.URL.Path, user) {
|
||||
// For principal path, use merged discovery handler
|
||||
if strings.HasSuffix(r.URL.Path, user+"/") || strings.HasSuffix(r.URL.Path, user) || r.URL.Path == "/" || r.URL.Path == prefix+"/" {
|
||||
// For principal path or root, use merged discovery handler
|
||||
if r.Method == "PROPFIND" {
|
||||
extra.HandleDiscoveryPropfind(r, ctx, caldavHandler, w, be)
|
||||
} else if r.Method == "OPTIONS" {
|
||||
extra.HandleDiscoveryOptions(r, ctx, caldavHandler, w, be)
|
||||
} else {
|
||||
caldavHandler.ServeHTTP(w, r.WithContext(ctx))
|
||||
// Ensure DAV header includes addressbook for OPTIONS
|
||||
if r.Method == "OPTIONS" {
|
||||
dav := w.Header().Get("DAV")
|
||||
if dav != "" && !strings.Contains(dav, "addressbook") {
|
||||
w.Header().Set("DAV", dav+", addressbook")
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
log.Printf("Not found: %s", r)
|
||||
http.NotFound(w, r)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user