(diff-end-of-hunk): Be more careful with unified hunks.
[bpt/emacs.git] / lisp / url / url-dav.el
CommitLineData
8c8b8430
SM
1;;; url-dav.el --- WebDAV support
2
76827ca4 3;; Copyright (C) 2001, 2004 Free Software Foundation, Inc.
8c8b8430
SM
4
5;; Author: Bill Perry <wmperry@gnu.org>
6;; Maintainer: Bill Perry <wmperry@gnu.org>
8c8b8430
SM
7;; Keywords: url, vc
8
76827ca4
SM
9;; This file is part of GNU Emacs.
10
8c8b8430
SM
11;; GNU Emacs is free software; you can redistribute it and/or modify
12;; it under the terms of the GNU General Public License as published by
13;; the Free Software Foundation; either version 2, or (at your option)
14;; any later version.
15
16;; GNU Emacs is distributed in the hope that it will be useful,
17;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19;; GNU General Public License for more details.
20
21;; You should have received a copy of the GNU General Public License
22;; along with GNU Emacs; see the file COPYING. If not, write to the
23;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24;; Boston, MA 02111-1307, USA.
25
0aa70d32
SM
26;; DAV is in RFC 2518.
27
76827ca4
SM
28;;; Commentary:
29
30;;; Code:
31
8c8b8430
SM
32(eval-when-compile
33 (require 'cl))
34
35(require 'xml)
36(require 'url-util)
37(require 'url-handlers)
38
39(defvar url-dav-supported-protocols '(1 2)
40 "List of supported DAV versions.")
41
0aa70d32
SM
42(defun url-intersection (l1 l2)
43 "Return a list of the elements occuring in both of the lists L1 and L2."
44 (if (null l2)
45 l2
46 (let (result)
47 (while l1
48 (if (member (car l1) l2)
49 (setq result (cons (pop l1) result))
50 (pop l1)))
51 (nreverse result))))
52
8c8b8430
SM
53;;;###autoload
54(defun url-dav-supported-p (url)
55 (and (featurep 'xml)
56 (fboundp 'xml-expand-namespace)
0aa70d32
SM
57 (url-intersection url-dav-supported-protocols
58 (plist-get (url-http-options url) 'dav))))
8c8b8430
SM
59
60(defun url-dav-node-text (node)
61 "Return the text data from the XML node NODE."
62 (mapconcat (lambda (txt)
63 (if (stringp txt)
64 txt
65 "")) (xml-node-children node) " "))
66
67\f
68;;; Parsing routines for the actual node contents.
76827ca4
SM
69;;
70;; I am not incredibly happy with how this code looks/works right
71;; now, but it DOES work, and if we get the API right, our callers
72;; won't have to worry about the internal representation.
8c8b8430
SM
73
74(defconst url-dav-datatype-attribute
75 'urn:uuid:c2f41010-65b3-11d1-a29f-00aa00c14882/dt)
76
77(defun url-dav-process-integer-property (node)
78 (truncate (string-to-number (url-dav-node-text node))))
79
80(defun url-dav-process-number-property (node)
81 (string-to-number (url-dav-node-text node)))
82
83(defconst url-dav-iso8601-regexp
84 (let* ((dash "-?")
85 (colon ":?")
86 (4digit "\\([0-9][0-9][0-9][0-9]\\)")
87 (2digit "\\([0-9][0-9]\\)")
88 (date-fullyear 4digit)
89 (date-month 2digit)
90 (date-mday 2digit)
91 (time-hour 2digit)
92 (time-minute 2digit)
93 (time-second 2digit)
94 (time-secfrac "\\(\\.[0-9]+\\)?")
95 (time-numoffset (concat "[-+]\\(" time-hour "\\):" time-minute))
96 (time-offset (concat "Z" time-numoffset))
97 (partial-time (concat time-hour colon time-minute colon time-second
98 time-secfrac))
99 (full-date (concat date-fullyear dash date-month dash date-mday))
100 (full-time (concat partial-time time-offset))
101 (date-time (concat full-date "T" full-time)))
102 (list (concat "^" full-date)
103 (concat "T" partial-time)
104 (concat "Z" time-numoffset)))
105 "List of regular expressions matching iso8601 dates.
1061st regular expression matches the date.
1072nd regular expression matches the time.
76827ca4 1083rd regular expression matches the (optional) timezone specification.")
8c8b8430
SM
109
110(defun url-dav-process-date-property (node)
111 (require 'parse-time)
112 (let* ((date-re (nth 0 url-dav-iso8601-regexp))
113 (time-re (nth 1 url-dav-iso8601-regexp))
114 (tz-re (nth 2 url-dav-iso8601-regexp))
115 (date-string (url-dav-node-text node))
116 re-start
117 time seconds minute hour fractional-seconds
118 day month year day-of-week dst tz)
119 ;; We need to populate 'time' with
120 ;; (SEC MIN HOUR DAY MON YEAR DOW DST TZ)
121
122 ;; Nobody else handles iso8601 correctly, lets do it ourselves.
123 (when (string-match date-re date-string re-start)
124 (setq year (string-to-int (match-string 1 date-string))
125 month (string-to-int (match-string 2 date-string))
126 day (string-to-int (match-string 3 date-string))
127 re-start (match-end 0))
128 (when (string-match time-re date-string re-start)
129 (setq hour (string-to-int (match-string 1 date-string))
130 minute (string-to-int (match-string 2 date-string))
131 seconds (string-to-int (match-string 3 date-string))
132 fractional-seconds (string-to-int (or
133 (match-string 4 date-string)
134 "0"))
135 re-start (match-end 0))
136 (when (string-match tz-re date-string re-start)
137 (setq tz (match-string 1 date-string)))
138 (url-debug 'dav "Parsed iso8601%s date" (if tz "tz" ""))
139 (setq time (list seconds minute hour day month year day-of-week dst tz))))
140
141 ;; Fall back to having Gnus do fancy things for us.
142 (when (not time)
143 (setq time (parse-time-string date-string)))
144
145 (if time
146 (setq time (apply 'encode-time time))
147 (url-debug 'dav "Unable to decode date (%S) (%s)"
148 (xml-node-name node) date-string))
149 time))
150
151(defun url-dav-process-boolean-property (node)
152 (/= 0 (string-to-int (url-dav-node-text node))))
153
154(defun url-dav-process-uri-property (node)
155 ;; Returns a parsed representation of the URL...
156 (url-generic-parse-url (url-dav-node-text node)))
157
158(defun url-dav-find-parser (node)
159 "Find a function to parse the XML node NODE."
160 (or (get (xml-node-name node) 'dav-parser)
161 (let ((fn (intern (format "url-dav-process-%s" (xml-node-name node)))))
162 (if (not (fboundp fn))
163 (setq fn 'url-dav-node-text)
164 (put (xml-node-name node) 'dav-parser fn))
165 fn)))
166
167(defmacro url-dav-dispatch-node (node)
168 `(funcall (url-dav-find-parser ,node) ,node))
169
170(defun url-dav-process-DAV:prop (node)
171 ;; A prop node has content model of ANY
172 ;;
173 ;; Some predefined nodes have special meanings though.
174 ;;
175 ;; DAV:supportedlock - list of DAV:lockentry
176 ;; DAV:source
177 ;; DAV:iscollection - boolean
178 ;; DAV:getcontentlength - integer
179 ;; DAV:ishidden - boolean
180 ;; DAV:getcontenttype - string
181 ;; DAV:resourcetype - node who's name is the resource type
182 ;; DAV:getlastmodified - date
183 ;; DAV:creationdate - date
184 ;; DAV:displayname - string
185 ;; DAV:getetag - unknown
186 (let ((children (xml-node-children node))
187 (node-type nil)
188 (props nil)
189 (value nil)
190 (handler-func nil))
191 (when (not children)
192 (error "No child nodes in DAV:prop"))
193
194 (while children
195 (setq node (car children)
196 node-type (intern
197 (or
198 (cdr-safe (assq url-dav-datatype-attribute
199 (xml-node-attributes node)))
200 "unknown"))
201 value nil)
202
203 (case node-type
204 ((dateTime.iso8601tz
205 dateTime.iso8601
206 dateTime.tz
207 dateTime.rfc1123
208 dateTime
209 date) ; date is our 'special' one...
210 ;; Some type of date/time string.
211 (setq value (url-dav-process-date-property node)))
212 (int
213 ;; Integer type...
214 (setq value (url-dav-process-integer-property node)))
215 ((number float)
216 (setq value (url-dav-process-number-property node)))
217 (boolean
218 (setq value (url-dav-process-boolean-property node)))
219 (uri
220 (setq value (url-dav-process-uri-property node)))
221 (otherwise
222 (if (not (eq node-type 'unknown))
223 (url-debug 'dav "Unknown data type in url-dav-process-prop: %s"
224 node-type))
225 (setq value (url-dav-dispatch-node node))))
226
227 (setq props (plist-put props (xml-node-name node) value)
228 children (cdr children)))
229 props))
230
231(defun url-dav-process-DAV:supportedlock (node)
232 ;; DAV:supportedlock is a list of DAV:lockentry items.
233 ;; DAV:lockentry in turn contains a DAV:lockscope and DAV:locktype.
234 ;; The DAV:lockscope must have a single node beneath it, ditto for
235 ;; DAV:locktype.
236 (let ((children (xml-node-children node))
237 (results nil)
238 scope type)
239 (while children
240 (when (and (not (stringp (car children)))
241 (eq (xml-node-name (car children)) 'DAV:lockentry))
242 (setq scope (assq 'DAV:lockscope (xml-node-children (car children)))
243 type (assq 'DAV:locktype (xml-node-children (car children))))
244 (when (and scope type)
245 (setq scope (xml-node-name (car (xml-node-children scope)))
246 type (xml-node-name (car (xml-node-children type))))
247 (push (cons type scope) results)))
248 (setq children (cdr children)))
249 results))
250
251(defun url-dav-process-subnode-property (node)
252 ;; Returns a list of child node names.
253 (delq nil (mapcar 'car-safe (xml-node-children node))))
254
255(defalias 'url-dav-process-DAV:depth 'url-dav-process-integer-property)
256(defalias 'url-dav-process-DAV:resourcetype 'url-dav-process-subnode-property)
257(defalias 'url-dav-process-DAV:locktype 'url-dav-process-subnode-property)
258(defalias 'url-dav-process-DAV:lockscope 'url-dav-process-subnode-property)
259(defalias 'url-dav-process-DAV:getcontentlength 'url-dav-process-integer-property)
260(defalias 'url-dav-process-DAV:getlastmodified 'url-dav-process-date-property)
261(defalias 'url-dav-process-DAV:creationdate 'url-dav-process-date-property)
262(defalias 'url-dav-process-DAV:iscollection 'url-dav-process-boolean-property)
263(defalias 'url-dav-process-DAV:ishidden 'url-dav-process-boolean-property)
264
265(defun url-dav-process-DAV:locktoken (node)
266 ;; DAV:locktoken can have one or more DAV:href children.
267 (delq nil (mapcar (lambda (n)
268 (if (stringp n)
269 n
270 (url-dav-dispatch-node n)))
271 (xml-node-children node))))
272
273(defun url-dav-process-DAV:owner (node)
274 ;; DAV:owner can contain anything.
275 (delq nil (mapcar (lambda (n)
276 (if (stringp n)
277 n
278 (url-dav-dispatch-node n)))
279 (xml-node-children node))))
280
281(defun url-dav-process-DAV:activelock (node)
282 ;; DAV:activelock can contain:
283 ;; DAV:lockscope
284 ;; DAV:locktype
285 ;; DAV:depth
286 ;; DAV:owner (optional)
287 ;; DAV:timeout (optional)
288 ;; DAV:locktoken (optional)
289 (let ((children (xml-node-children node))
290 (results nil))
291 (while children
292 (if (listp (car children))
293 (push (cons (xml-node-name (car children))
294 (url-dav-dispatch-node (car children)))
295 results))
296 (setq children (cdr children)))
297 results))
298
299(defun url-dav-process-DAV:lockdiscovery (node)
300 ;; Can only contain a list of DAV:activelock objects.
301 (let ((children (xml-node-children node))
302 (results nil))
303 (while children
304 (cond
305 ((stringp (car children))
306 ;; text node? why?
307 nil)
308 ((eq (xml-node-name (car children)) 'DAV:activelock)
309 (push (url-dav-dispatch-node (car children)) results))
310 (t
311 ;; Ignore unknown nodes...
312 nil))
313 (setq children (cdr children)))
314 results))
315
316(defun url-dav-process-DAV:status (node)
317 ;; The node contains a standard HTTP/1.1 response line... we really
318 ;; only care about the numeric status code.
319 (let ((status (url-dav-node-text node)))
320 (if (string-match "\\`[ \r\t\n]*HTTP/[0-9.]+ \\([0-9]+\\)" status)
321 (string-to-int (match-string 1 status))
322 500)))
323
324(defun url-dav-process-DAV:propstat (node)
325 ;; A propstate node can have the following children...
326 ;;
327 ;; DAV:prop - a list of properties and values
328 ;; DAV:status - An HTTP/1.1 status line
329 (let ((children (xml-node-children node))
330 (props nil)
331 (status nil))
332 (when (not children)
333 (error "No child nodes in DAV:propstat"))
334
335 (setq props (url-dav-dispatch-node (assq 'DAV:prop children))
336 status (url-dav-dispatch-node (assq 'DAV:status children)))
337
338 ;; Need to parse out the HTTP status
339 (setq props (plist-put props 'DAV:status status))
340 props))
341
342(defun url-dav-process-DAV:response (node)
343 (let ((children (xml-node-children node))
344 (propstat nil)
345 (href))
346 (when (not children)
347 (error "No child nodes in DAV:response"))
348
349 ;; A response node can have the following children...
350 ;;
351 ;; DAV:href - URL the response is for.
352 ;; DAV:propstat - see url-dav-process-propstat
353 ;; DAV:responsedescription - text description of the response
354 (setq propstat (assq 'DAV:propstat children)
355 href (assq 'DAV:href children))
356
357 (when (not href)
358 (error "No href in DAV:response"))
359
360 (when (not propstat)
361 (error "No propstat in DAV:response"))
362
363 (setq propstat (url-dav-dispatch-node propstat)
364 href (url-dav-dispatch-node href))
365 (cons href propstat)))
366
367(defun url-dav-process-DAV:multistatus (node)
368 (let ((children (xml-node-children node))
369 (results nil))
370 (while children
371 (push (url-dav-dispatch-node (car children)) results)
372 (setq children (cdr children)))
373 results))
374
375\f
376;;; DAV request/response generation/processing
377(defun url-dav-process-response (buffer url)
76827ca4 378 "Parse a WebDAV response from BUFFER, interpreting it relative to URL.
8c8b8430
SM
379
380The buffer must have been retrieved by HTTP or HTTPS and contain an
76827ca4 381XML document."
8c8b8430
SM
382 (declare (special url-http-content-type
383 url-http-response-status
384 url-http-end-of-headers))
385 (let ((tree nil)
386 (overall-status nil))
387 (when buffer
388 (unwind-protect
389 (save-excursion
390 (set-buffer buffer)
391 (goto-char url-http-end-of-headers)
392 (setq overall-status url-http-response-status)
393
394 ;; XML documents can be transferred as either text/xml or
395 ;; application/xml, and we are required to accept both of
396 ;; them.
397 (if (and
398 url-http-content-type
399 (or (string-match "^text/xml" url-http-content-type)
400 (string-match "^application/xml" url-http-content-type)))
401 (setq tree (xml-parse-region (point) (point-max)))))
402 ;; Clean up after ourselves.
403 '(kill-buffer buffer)))
404
405 ;; We should now be
406 (if (eq (xml-node-name (car tree)) 'DAV:multistatus)
407 (url-dav-dispatch-node (car tree))
408 (url-debug 'dav "Got back singleton response for URL(%S)" url)
409 (let ((properties (url-dav-dispatch-node (car tree))))
410 ;; We need to make sure we have a DAV:status node in there for
411 ;; higher-level code;
412 (setq properties (plist-put properties 'DAV:status overall-status))
413 ;; Make this look like a DAV:multistatus parse tree so that
414 ;; nobody but us needs to know the difference.
415 (list (cons url properties))))))
416
417(defun url-dav-request (url method tag body
418 &optional depth headers namespaces)
76827ca4 419 "Perform WebDAV operation METHOD on URL. Return the parsed responses.
8c8b8430
SM
420Automatically creates an XML request body if TAG is non-nil.
421BODY is the XML document fragment to be enclosed by <TAG></TAG>.
422
423DEPTH is how deep the request should propogate. Default is 0, meaning
424it should apply only to URL. A negative number means to use
425`Infinity' for the depth. Not all WebDAV servers support this depth
426though.
427
428HEADERS is an assoc list of extra headers to send in the request.
429
430NAMESPACES is an assoc list of (NAMESPACE . EXPANSION), and these are
431added to the <TAG> element. The DAV=DAV: namespace is automatically
76827ca4 432added to this list, so most requests can just pass in nil."
8c8b8430
SM
433 ;; Take care of the default value for depth...
434 (setq depth (or depth 0))
435
436 ;; Now lets translate it into something webdav can understand.
437 (if (< depth 0)
438 (setq depth "Infinity")
439 (setq depth (int-to-string depth)))
440 (if (not (assoc "DAV" namespaces))
441 (setq namespaces (cons '("DAV" . "DAV:") namespaces)))
442
443 (let* ((url-request-extra-headers `(("Depth" . ,depth)
444 ("Content-type" . "text/xml")
445 ,@headers))
446 (url-request-method method)
447 (url-request-data
448 (if tag
449 (concat
450 "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n"
451 "<" (symbol-name tag) " "
452 ;; add in the appropriate namespaces...
453 (mapconcat (lambda (ns)
454 (concat "xmlns:" (car ns) "='" (cdr ns) "'"))
455 namespaces "\n ")
456 ">\n"
457 body
458 "</" (symbol-name tag) ">\n"))))
459 (url-dav-process-response (url-retrieve-synchronously url) url)))
460
461;;;###autoload
462(defun url-dav-get-properties (url &optional attributes depth namespaces)
463 "Return properties for URL, up to DEPTH levels deep.
464
465Returns an assoc list, where the key is the filename (possibly a full
466URI), and the value is a standard property list of DAV property
76827ca4 467names (ie: DAV:resourcetype)."
8c8b8430
SM
468 (url-dav-request url "PROPFIND" 'DAV:propfind
469 (if attributes
470 (mapconcat (lambda (attr)
471 (concat "<DAV:prop><"
472 (symbol-name attr)
473 "/></DAV:prop>"))
474 attributes "\n ")
475 " <DAV:allprop/>")
476 depth nil namespaces))
477
478(defmacro url-dav-http-success-p (status)
479 "Return whether PROPERTIES was the result of a successful DAV request."
480 `(= (/ (or ,status 500) 100) 2))
481
482\f
483;;; Locking support
484(defvar url-dav-lock-identifier (concat "mailto:" user-mail-address)
485 "*URL used as contact information when creating locks in DAV.
486This will be used as the contents of the DAV:owner/DAV:href tag to
487identify the owner of a LOCK when requesting it. This will be shown
488to other users when the DAV:lockdiscovery property is requested, so
76827ca4 489make sure you are comfortable with it leaking to the outside world.")
8c8b8430
SM
490
491;;;###autoload
492(defun url-dav-lock-resource (url exclusive &optional depth)
493 "Request a lock on URL. If EXCLUSIVE is non-nil, get an exclusive lock.
494Optional 3rd argument DEPTH says how deep the lock should go, default is 0
495\(lock only the resource and none of its children\).
496
497Returns a cons-cell of (SUCCESSFUL-RESULTS . FAILURE-RESULTS).
498SUCCESSFUL-RESULTS is a list of (URL STATUS locktoken).
76827ca4 499FAILURE-RESULTS is a list of (URL STATUS)."
8c8b8430
SM
500 (setq exclusive (if exclusive "<DAV:exclusive/>" "<DAV:shared/>"))
501 (let* ((body
502 (concat
503 " <DAV:lockscope>" exclusive "</DAV:lockscope>\n"
504 " <DAV:locktype> <DAV:write/> </DAV:locktype>\n"
505 " <DAV:owner>\n"
506 " <DAV:href>" url-dav-lock-identifier "</DAV:href>\n"
507 " </DAV:owner>\n"))
508 (response nil) ; Responses to the LOCK request
509 (result nil) ; For walking thru the response list
510 (child-url nil)
511 (child-status nil)
512 (failures nil) ; List of failure cases (URL . STATUS)
513 (successes nil)) ; List of success cases (URL . STATUS)
514 (setq response (url-dav-request url "LOCK" 'DAV:lockinfo body
515 depth '(("Timeout" . "Infinite"))))
516
517 ;; Get the parent URL ready for expand-file-name
518 (if (not (vectorp url))
519 (setq url (url-generic-parse-url url)))
520
521 ;; Walk thru the response list, fully expand the URL, and grab the
522 ;; status code.
523 (while response
524 (setq result (pop response)
525 child-url (url-expand-file-name (pop result) url)
526 child-status (or (plist-get result 'DAV:status) 500))
527 (if (url-dav-http-success-p child-status)
528 (push (list url child-status "huh") successes)
529 (push (list url child-status) failures)))
530 (cons successes failures)))
531
532;;;###autoload
533(defun url-dav-active-locks (url &optional depth)
534 "Return an assoc list of all active locks on URL."
535 (let ((response (url-dav-get-properties url '(DAV:lockdiscovery) depth))
536 (properties nil)
537 (child nil)
538 (child-url nil)
539 (child-results nil)
540 (results nil))
541 (if (not (vectorp url))
542 (setq url (url-generic-parse-url url)))
543
544 (while response
545 (setq child (pop response)
546 child-url (pop child)
547 child-results nil)
548 (when (and (url-dav-http-success-p (plist-get child 'DAV:status))
549 (setq child (plist-get child 'DAV:lockdiscovery)))
550 ;; After our parser has had its way with it, The
551 ;; DAV:lockdiscovery property is a list of DAV:activelock
552 ;; objects, which are comprised of DAV:activelocks, which
553 ;; assoc lists of properties and values.
554 (while child
555 (if (assq 'DAV:locktoken (car child))
556 (let ((tokens (cdr (assq 'DAV:locktoken (car child))))
557 (owners (cdr (assq 'DAV:owner (car child)))))
558 (dolist (token tokens)
559 (dolist (owner owners)
560 (push (cons token owner) child-results)))))
561 (pop child)))
562 (if child-results
563 (push (cons (url-expand-file-name child-url url) child-results)
564 results)))
565 results))
566
567;;;###autoload
568(defun url-dav-unlock-resource (url lock-token)
569 "Release the lock on URL represented by LOCK-TOKEN.
76827ca4 570Returns t iff the lock was successfully released."
8c8b8430
SM
571 (declare (special url-http-response-status))
572 (let* ((url-request-extra-headers (list (cons "Lock-Token"
573 (concat "<" lock-token ">"))))
574 (url-request-method "UNLOCK")
575 (url-request-data nil)
576 (buffer (url-retrieve-synchronously url))
577 (result nil))
578 (when buffer
579 (unwind-protect
580 (save-excursion
581 (set-buffer buffer)
582 (setq result (url-dav-http-success-p url-http-response-status)))
583 (kill-buffer buffer)))
584 result))
585
586\f
587;;; file-name-handler stuff
588(defun url-dav-file-attributes-mode-string (properties)
589 (let ((modes (make-string 10 ?-))
590 (supported-locks (plist-get properties 'DAV:supportedlock))
591 (executable-p (equal (plist-get properties 'http://apache.org/dav/props/executable)
592 "T"))
593 (directory-p (memq 'DAV:collection (plist-get properties 'DAV:resourcetype)))
594 (readable t)
595 (lock nil))
596 ;; Assume we can read this, otherwise the PROPFIND would have
597 ;; failed.
598 (when readable
599 (aset modes 1 ?r)
600 (aset modes 4 ?r)
601 (aset modes 7 ?r))
602
603 (when directory-p
604 (aset modes 0 ?d))
605
606 (when executable-p
607 (aset modes 3 ?x)
608 (aset modes 6 ?x)
609 (aset modes 9 ?x))
610
611 (while supported-locks
612 (setq lock (car supported-locks)
613 supported-locks (cdr supported-locks))
614 (case (car lock)
615 (DAV:write
616 (case (cdr lock)
617 (DAV:shared ; group permissions (possibly world)
618 (aset modes 5 ?w))
619 (DAV:exclusive
620 (aset modes 2 ?w)) ; owner permissions?
621 (otherwise
622 (url-debug 'dav "Unrecognized DAV:lockscope (%S)" (cdr lock)))))
623 (otherwise
624 (url-debug 'dav "Unrecognized DAV:locktype (%S)" (car lock)))))
625 modes))
626
0aa70d32
SM
627(autoload 'url-http-head-file-attributes "url-http")
628
8c8b8430
SM
629;;;###autoload
630(defun url-dav-file-attributes (url)
631 (let ((properties (cdar (url-dav-get-properties url)))
632 (attributes nil))
633 (if (and properties
634 (url-dav-http-success-p (plist-get properties 'DAV:status)))
635 ;; We got a good DAV response back..
636 (setq attributes
637 (list
638 ;; t for directory, string for symbolic link, or nil
639 ;; Need to support DAV Bindings to figure out the
640 ;; symbolic link issues.
641 (if (memq 'DAV:collection (plist-get properties 'DAV:resourcetype)) t nil)
642
643 ;; Number of links to file... Needs DAV Bindings.
644 1
645
646 ;; File uid - no way to figure out?
647 0
648
649 ;; File gid - no way to figure out?
650 0
651
652 ;; Last access time - ???
653 nil
654
655 ;; Last modification time
656 (plist-get properties 'DAV:getlastmodified)
657
658 ;; Last status change time... just reuse last-modified
659 ;; for now.
660 (plist-get properties 'DAV:getlastmodified)
661
662 ;; size in bytes
663 (or (plist-get properties 'DAV:getcontentlength) 0)
664
665 ;; file modes as a string like `ls -l'
666 ;;
667 ;; Should be able to build this up from the
668 ;; DAV:supportedlock attribute pretty easily. Getting
669 ;; the group info could be impossible though.
670 (url-dav-file-attributes-mode-string properties)
671
672 ;; t iff file's gid would change if it were deleted &
673 ;; recreated. No way for us to know that thru DAV.
674 nil
675
676 ;; inode number - meaningless
677 nil
678
679 ;; device number - meaningless
680 nil))
681 ;; Fall back to just the normal http way of doing things.
682 (setq attributes (url-http-head-file-attributes url)))
683 attributes))
684
685;;;###autoload
686(defun url-dav-save-resource (url obj &optional content-type lock-token)
687 "Save OBJ as URL using WebDAV.
688URL must be a fully qualified URL.
689OBJ may be a buffer or a string."
0aa70d32 690 (declare (special url-http-response-status))
8c8b8430
SM
691 (let ((buffer nil)
692 (result nil)
693 (url-request-extra-headers nil)
694 (url-request-method "PUT")
695 (url-request-data
696 (cond
697 ((bufferp obj)
698 (save-excursion
699 (set-buffer obj)
700 (buffer-string)))
701 ((stringp obj)
702 obj)
703 (t
704 (error "Invalid object to url-dav-save-resource")))))
705
706 (if lock-token
707 (push
708 (cons "If" (concat "(<" lock-token ">)"))
709 url-request-extra-headers))
710
711 ;; Everything must always have a content-type when we submit it.
712 (push
713 (cons "Content-type" (or content-type "application/octet-stream"))
714 url-request-extra-headers)
715
716 ;; Do the save...
717 (setq buffer (url-retrieve-synchronously url))
718
719 ;; Sanity checking
720 (when buffer
721 (unwind-protect
722 (save-excursion
723 (set-buffer buffer)
724 (setq result (url-dav-http-success-p url-http-response-status)))
725 (kill-buffer buffer)))
726 result))
727
728(eval-when-compile
729 (defmacro url-dav-delete-something (url lock-token &rest error-checking)
730 "Delete URL completely, with no sanity checking whatsoever. DO NOT USE.
731This is defined as a macro that will not be visible from compiled files.
732Use with care, and even then think three times.
733"
734 `(progn
735 ,@error-checking
736 (url-dav-request ,url "DELETE" nil nil -1
737 (if ,lock-token
738 (list
739 (cons "If"
740 (concat "(<" ,lock-token ">)"))))))))
741
742
743;;;###autoload
744(defun url-dav-delete-directory (url &optional recursive lock-token)
745 "Delete the WebDAV collection URL.
746If optional second argument RECURSIVE is non-nil, then delete all
76827ca4 747files in the collection as well."
8c8b8430
SM
748 (let ((status nil)
749 (props nil)
750 (props nil))
751 (setq props (url-dav-delete-something
752 url lock-token
753 (setq props (url-dav-get-properties url '(DAV:getcontenttype) 1))
754 (if (and (not recursive)
755 (/= (length props) 1))
756 (signal 'file-error (list "Removing directory"
757 "directory not empty" url)))))
758
759 (mapc (lambda (result)
760 (setq status (plist-get (cdr result) 'DAV:status))
761 (if (not (url-dav-http-success-p status))
762 (signal 'file-error (list "Removing directory"
763 "Errror removing"
764 (car result) status))))
765 props))
766 nil)
767
768;;;###autoload
769(defun url-dav-delete-file (url &optional lock-token)
770 "Delete file named URL."
771 (let ((props nil)
772 (status nil))
773 (setq props (url-dav-delete-something
774 url lock-token
775 (setq props (url-dav-get-properties url))
776 (if (eq (plist-get (cdar props) 'DAV:resourcetype) 'DAV:collection)
777 (signal 'file-error (list "Removing old name" "is a collection" url)))))
778
779 (mapc (lambda (result)
780 (setq status (plist-get (cdr result) 'DAV:status))
781 (if (not (url-dav-http-success-p status))
782 (signal 'file-error (list "Removing old name"
783 "Errror removing"
784 (car result) status))))
785 props))
786 nil)
787
788;;;###autoload
789(defun url-dav-directory-files (url &optional full match nosort files-only)
790 "Return a list of names of files in DIRECTORY.
791There are three optional arguments:
792If FULL is non-nil, return absolute file names. Otherwise return names
793 that are relative to the specified directory.
794If MATCH is non-nil, mention only file names that match the regexp MATCH.
795If NOSORT is non-nil, the list is not sorted--its order is unpredictable.
76827ca4 796 NOSORT is useful if you plan to sort the result yourself."
8c8b8430
SM
797 (let ((properties (url-dav-get-properties url '(DAV:resourcetype) 1))
798 (child-url nil)
799 (child-props nil)
800 (files nil)
801 (parsed-url (url-generic-parse-url url)))
802
803 (if (= (length properties) 1)
804 (signal 'file-error (list "Opening directory" "not a directory" url)))
805
806 (while properties
807 (setq child-props (pop properties)
808 child-url (pop child-props))
809 (if (and (eq (plist-get child-props 'DAV:resourcetype) 'DAV:collection)
810 files-only)
811 ;; It is a directory, and we were told to return just files.
812 nil
813
814 ;; Fully expand the URL and then rip off the beginning if we
815 ;; are not supposed to return fully-qualified names.
816 (setq child-url (url-expand-file-name child-url parsed-url))
817 (if (not full)
818 (setq child-url (substring child-url (length url))))
819
820 ;; We don't want '/' as the last character in filenames...
821 (if (string-match "/$" child-url)
822 (setq child-url (substring child-url 0 -1)))
823
824 ;; If we have a match criteria, then apply it.
825 (if (or (and match (not (string-match match child-url)))
826 (string= child-url "")
827 (string= child-url url))
828 nil
829 (push child-url files))))
830
831 (if nosort
832 files
833 (sort files 'string-lessp))))
834
835;;;###autoload
836(defun url-dav-file-directory-p (url)
837 "Return t if URL names an existing DAV collection."
838 (let ((properties (cdar (url-dav-get-properties url '(DAV:resourcetype)))))
839 (eq (plist-get properties 'DAV:resourcetype) 'DAV:collection)))
840
841;;;###autoload
842(defun url-dav-make-directory (url &optional parents)
843 "Create the directory DIR and any nonexistent parent dirs."
844 (declare (special url-http-response-status))
845 (let* ((url-request-extra-headers nil)
846 (url-request-method "MKCOL")
847 (url-request-data nil)
848 (buffer (url-retrieve-synchronously url))
849 (result nil))
850 (when buffer
851 (unwind-protect
852 (save-excursion
853 (set-buffer buffer)
854 (case url-http-response-status
855 (201 ; Collection created in its entirety
856 (setq result t))
857 (403 ; Forbidden
858 nil)
859 (405 ; Method not allowed
860 nil)
861 (409 ; Conflict
862 nil)
863 (415 ; Unsupported media type (WTF?)
864 nil)
865 (507 ; Insufficient storage
866 nil)
867 (otherwise
868 nil)))
869 (kill-buffer buffer)))
870 result))
871
872;;;###autoload
873(defun url-dav-rename-file (oldname newname &optional overwrite)
874 (if (not (and (string-match url-handler-regexp oldname)
875 (string-match url-handler-regexp newname)))
0aa70d32
SM
876 (signal 'file-error
877 (list "Cannot rename between different URL backends"
878 oldname newname)))
8c8b8430
SM
879
880 (let* ((headers nil)
881 (props nil)
882 (status nil)
883 (directory-p (url-dav-file-directory-p oldname))
884 (exists-p (url-http-file-exists-p newname)))
885
886 (if (and exists-p
887 (or
888 (null overwrite)
889 (and (numberp overwrite)
890 (not (yes-or-no-p
891 (format "File %s already exists; rename to it anyway? "
892 newname))))))
893 (signal 'file-already-exists (list "File already exists" newname)))
894
895 ;; Honor the overwrite flag...
896 (if overwrite (push '("Overwrite" . "T") headers))
897
898 ;; Have to tell them where to copy it to!
899 (push (cons "Destination" newname) headers)
900
901 ;; Always send a depth of -1 in case we are moving a collection.
902 (setq props (url-dav-request oldname "MOVE" nil nil (if directory-p -1 0)
903 headers))
904
905 (mapc (lambda (result)
906 (setq status (plist-get (cdr result) 'DAV:status))
907
908 (if (not (url-dav-http-success-p status))
909 (signal 'file-error (list "Renaming" oldname newname status))))
910 props)
911 t))
912
913;;;###autoload
914(defun url-dav-file-name-all-completions (file url)
915 "Return a list of all completions of file name FILE in directory DIRECTORY.
76827ca4 916These are all file names in directory DIRECTORY which begin with FILE."
8c8b8430
SM
917 (url-dav-directory-files url nil (concat "^" file ".*")))
918
919;;;###autoload
920(defun url-dav-file-name-completion (file url)
921 "Complete file name FILE in directory DIRECTORY.
922Returns the longest string
923common to all file names in DIRECTORY that start with FILE.
924If there is only one and FILE matches it exactly, returns t.
76827ca4 925Returns nil if DIR contains no name starting with FILE."
8c8b8430
SM
926 (let ((matches (url-dav-file-name-all-completions file url))
927 (result nil))
928 (cond
929 ((null matches)
930 ;; No matches
931 nil)
932 ((and (= (length matches) 1)
933 (string= file (car matches)))
934 ;; Only one file and FILE matches it exactly...
935 t)
936 (t
937 ;; Need to figure out the longest string that they have in commmon
938 (setq matches (sort matches (lambda (a b) (> (length a) (length b)))))
939 (let ((n (length file))
940 (searching t)
941 (regexp nil)
942 (failed nil))
943 (while (and searching
944 (< n (length (car matches))))
945 (setq regexp (concat "^" (substring (car matches) 0 (1+ n)))
946 failed nil)
947 (dolist (potential matches)
948 (if (not (string-match regexp potential))
949 (setq failed t)))
950 (if failed
951 (setq searching nil)
952 (incf n)))
953 (substring (car matches) 0 n))))))
954
955(defun url-dav-register-handler (op)
956 (put op 'url-file-handlers (intern-soft (format "url-dav-%s" op))))
957
958(mapcar 'url-dav-register-handler
959 '(file-name-all-completions
960 file-name-completion
961 rename-file
962 make-directory
963 file-directory-p
964 directory-files
965 delete-file
966 delete-directory
967 file-attributes))
968
969\f
970;;; Version Control backend cruft
971
972;(put 'vc-registered 'url-file-handlers 'url-dav-vc-registered)
973
974;;;###autoload
975(defun url-dav-vc-registered (url)
976 (if (and (string-match "\\`https?" url)
977 (plist-get (url-http-options url) 'dav))
978 (progn
979 (vc-file-setprop url 'vc-backend 'dav)
980 t)))
981
982\f
983;;; Miscellaneous stuff.
984
985(provide 'url-dav)
e5566bd5 986
76827ca4
SM
987;; arch-tag: 2b14b7b3-888a-49b8-a490-17276a40e78e
988;;; url-dav.el ends here