1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# CardDAV Server Architecture

Phase 1: Read-only CardDAV server for Apple Contacts.

## RFCs

| RFC | Name | Purpose |
|-----|------|---------|
| [RFC 4918](https://datatracker.ietf.org/doc/html/rfc4918) | WebDAV | HTTP extensions for distributed authoring |
| [RFC 6352](https://datatracker.ietf.org/doc/html/rfc6352) | CardDAV | vCard extensions to WebDAV |
| [RFC 6350](https://datatracker.ietf.org/doc/html/rfc6350) | vCard 4.0 | Contact data format |
| [RFC 3253](https://datatracker.ietf.org/doc/html/rfc3253) | REPORT | Required for addressbook-multiget |

## HTTP Methods

### Phase 1 (read-only)

| Method | Purpose |
|--------|---------|
| `OPTIONS` | Advertise capabilities |
| `PROPFIND` | Discovery + collection listing |
| `GET` | Retrieve individual vCards |
| `REPORT` | addressbook-multiget (batch fetch) |

### Deferred

| Method | Purpose |
|--------|---------|
| `PUT` | Create/update vCards |
| `DELETE` | Remove vCards |
| `MKCOL` | Create address books |
| `PROPPATCH` | Modify properties |
| `addressbook-query` | Search/filter contacts |

## URL Structure

```
/.well-known/carddav        → redirect to /principal/
/principal/                 → user's principal resource
/addressbooks/              → addressbook-home-set
/addressbooks/contacts/     → the address book collection
/addressbooks/contacts/{id}.vcf → individual vCard
```

User identity from auth, not URL path.

## Discovery Flow

```
1. OPTIONS /.well-known/carddav
   → 301 Redirect to /principal/

2. OPTIONS /principal/
   → 200 OK
   → DAV: 1, 3, addressbook

3. PROPFIND /principal/ (Depth: 0)
   → current-user-principal: /principal/
   → addressbook-home-set: /addressbooks/

4. PROPFIND /addressbooks/ (Depth: 1)
   → Lists /addressbooks/contacts/ as type addressbook

5. REPORT /addressbooks/contacts/ (addressbook-multiget)
   → Returns vCards
```

### Key Properties

| Property | Location | Value |
|----------|----------|-------|
| `current-user-principal` | `/principal/` | `/principal/` |
| `addressbook-home-set` | `/principal/` | `/addressbooks/` |
| `resourcetype` | `/addressbooks/contacts/` | collection, addressbook |
| `displayname` | `/addressbooks/contacts/` | "Contacts" |
| `getctag` | `/addressbooks/contacts/` | change tag for sync |

## Storage

### Directory Structure

```
data/
└── contacts/
    ├── kqmtnwpxlrvszoyp.kdl
    ├── nzxwvtslqpomkrny.kdl
    └── plokmnzxwvtsrqky.kdl
```

### Contact File Format (KDL)

One bare KDL document per file — no `contact {}` wrapper, since the
file itself is the contact:

```kdl
name "John Smith"
phone "+1-555-1234" type="mobile"
email "john@example.com"
```

### Contact IDs

- 16 characters using k-z (16 letters)
- 64 bits of entropy
- Filename is the ID: `{id}.kdl`
- Maps to vCard UID

### Change Detection

- `getctag`: hash of directory listing or newest mtime
- `getetag`: mtime or content hash of .kdl file

### Deferred

- Per-user addressbook files (`addressbooks/{user}.kdl`)
- Groups with shared attributes
- Database migration

## Roda Architecture

### File Structure

```
lib/
└── pro_tacts/
    └── app.rb
```

Extract classes when needed.

### Routing

```ruby
module ProTacts
  class App < Roda
    plugin :all_verbs

    route do |r|
      r.on ".well-known/carddav" do
        r.redirect "/principal/"
      end

      r.on "principal" do
        r.is do
          r.options { dav_options }
          r.propfind { principal_propfind(r) }
        end
      end

      r.on "addressbooks" do
        r.is do
          r.propfind { home_propfind(r) }
        end

        r.on "contacts" do
          r.is do
            r.options { dav_options }
            r.propfind { collection_propfind(r) }
            r.report { addressbook_multiget(r) }
          end

          r.on String do |id|
            r.get { serve_vcard(id) }
          end
        end
      end
    end
  end
end
```

## Translation

KDL → vCard on read. The server parses KDL contact files into vCard 3.0
(`lib/pro_tacts/vcard.rb`), the version Apple clients speak.