report.py 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755
  1. # This file is part of Radicale - CalDAV and CardDAV server
  2. # Copyright © 2008 Nicolas Kandel
  3. # Copyright © 2008 Pascal Halter
  4. # Copyright © 2008-2017 Guillaume Ayoub
  5. # Copyright © 2017-2021 Unrud <unrud@outlook.com>
  6. # Copyright © 2024-2024 Pieter Hijma <pieterhijma@users.noreply.github.com>
  7. # Copyright © 2024-2024 Ray <ray@react0r.com>
  8. # Copyright © 2024-2024 Georgiy <metallerok@gmail.com>
  9. # Copyright © 2024-2025 Peter Bieringer <pb@bieringer.de>
  10. #
  11. # This library 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 3 of the License, or
  14. # (at your option) any later version.
  15. #
  16. # This library 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 Radicale. If not, see <http://www.gnu.org/licenses/>.
  23. import contextlib
  24. import copy
  25. import datetime
  26. import posixpath
  27. import socket
  28. import xml.etree.ElementTree as ET
  29. from http import client
  30. from typing import (Callable, Iterable, Iterator, List, Optional, Sequence,
  31. Tuple, Union)
  32. from urllib.parse import unquote, urlparse
  33. import vobject
  34. import vobject.base
  35. from vobject.base import ContentLine
  36. import radicale.item as radicale_item
  37. from radicale import httputils, pathutils, storage, types, xmlutils
  38. from radicale.app.base import Access, ApplicationBase
  39. from radicale.item import filter as radicale_filter
  40. from radicale.log import logger
  41. def free_busy_report(base_prefix: str, path: str, xml_request: Optional[ET.Element],
  42. collection: storage.BaseCollection, encoding: str,
  43. unlock_storage_fn: Callable[[], None],
  44. max_occurrence: int
  45. ) -> Tuple[int, Union[ET.Element, str]]:
  46. # NOTE: this function returns both an Element and a string because
  47. # free-busy reports are an edge-case on the return type according
  48. # to the spec.
  49. multistatus = ET.Element(xmlutils.make_clark("D:multistatus"))
  50. if xml_request is None:
  51. return client.MULTI_STATUS, multistatus
  52. root = xml_request
  53. if (root.tag == xmlutils.make_clark("C:free-busy-query") and
  54. collection.tag != "VCALENDAR"):
  55. logger.warning("Invalid REPORT method %r on %r requested",
  56. xmlutils.make_human_tag(root.tag), path)
  57. return client.FORBIDDEN, xmlutils.webdav_error("D:supported-report")
  58. time_range_element = root.find(xmlutils.make_clark("C:time-range"))
  59. assert isinstance(time_range_element, ET.Element)
  60. # Build a single filter from the free busy query for retrieval
  61. # TODO: filter for VFREEBUSY in additional to VEVENT but
  62. # test_filter doesn't support that yet.
  63. vevent_cf_element = ET.Element(xmlutils.make_clark("C:comp-filter"),
  64. attrib={'name': 'VEVENT'})
  65. vevent_cf_element.append(time_range_element)
  66. vcalendar_cf_element = ET.Element(xmlutils.make_clark("C:comp-filter"),
  67. attrib={'name': 'VCALENDAR'})
  68. vcalendar_cf_element.append(vevent_cf_element)
  69. filter_element = ET.Element(xmlutils.make_clark("C:filter"))
  70. filter_element.append(vcalendar_cf_element)
  71. filters = (filter_element,)
  72. # First pull from storage
  73. retrieved_items = list(collection.get_filtered(filters))
  74. # !!! Don't access storage after this !!!
  75. unlock_storage_fn()
  76. cal = vobject.iCalendar()
  77. collection_tag = collection.tag
  78. while retrieved_items:
  79. # Second filtering before evaluating occurrences.
  80. # ``item.vobject_item`` might be accessed during filtering.
  81. # Don't keep reference to ``item``, because VObject requires a lot of
  82. # memory.
  83. item, filter_matched = retrieved_items.pop(0)
  84. if not filter_matched:
  85. try:
  86. if not test_filter(collection_tag, item, filter_element):
  87. continue
  88. except ValueError as e:
  89. raise ValueError("Failed to free-busy filter item %r from %r: %s" %
  90. (item.href, collection.path, e)) from e
  91. except Exception as e:
  92. raise RuntimeError("Failed to free-busy filter item %r from %r: %s" %
  93. (item.href, collection.path, e)) from e
  94. fbtype = None
  95. if item.component_name == 'VEVENT':
  96. transp = getattr(item.vobject_item.vevent, 'transp', None)
  97. if transp and transp.value != 'OPAQUE':
  98. continue
  99. status = getattr(item.vobject_item.vevent, 'status', None)
  100. if not status or status.value == 'CONFIRMED':
  101. fbtype = 'BUSY'
  102. elif status.value == 'CANCELLED':
  103. fbtype = 'FREE'
  104. elif status.value == 'TENTATIVE':
  105. fbtype = 'BUSY-TENTATIVE'
  106. else:
  107. # Could do fbtype = status.value for x-name, I prefer this
  108. fbtype = 'BUSY'
  109. # TODO: coalesce overlapping periods
  110. if max_occurrence > 0:
  111. n_occurrences = max_occurrence+1
  112. else:
  113. n_occurrences = 0
  114. occurrences = radicale_filter.time_range_fill(item.vobject_item,
  115. time_range_element,
  116. "VEVENT",
  117. n=n_occurrences)
  118. if len(occurrences) >= max_occurrence:
  119. raise ValueError("FREEBUSY occurrences limit of {} hit"
  120. .format(max_occurrence))
  121. for occurrence in occurrences:
  122. vfb = cal.add('vfreebusy')
  123. vfb.add('dtstamp').value = item.vobject_item.vevent.dtstamp.value
  124. vfb.add('dtstart').value, vfb.add('dtend').value = occurrence
  125. if fbtype:
  126. vfb.add('fbtype').value = fbtype
  127. return (client.OK, cal.serialize())
  128. def xml_report(base_prefix: str, path: str, xml_request: Optional[ET.Element],
  129. collection: storage.BaseCollection, encoding: str,
  130. unlock_storage_fn: Callable[[], None]
  131. ) -> Tuple[int, ET.Element]:
  132. """Read and answer REPORT requests that return XML.
  133. Read rfc3253-3.6 for info.
  134. """
  135. multistatus = ET.Element(xmlutils.make_clark("D:multistatus"))
  136. if xml_request is None:
  137. return client.MULTI_STATUS, multistatus
  138. root = xml_request
  139. if root.tag in (xmlutils.make_clark("D:principal-search-property-set"),
  140. xmlutils.make_clark("D:principal-property-search"),
  141. xmlutils.make_clark("D:expand-property")):
  142. # We don't support searching for principals or indirect retrieving of
  143. # properties, just return an empty result.
  144. # InfCloud asks for expand-property reports (even if we don't announce
  145. # support for them) and stops working if an error code is returned.
  146. logger.warning("Unsupported REPORT method %r on %r requested",
  147. xmlutils.make_human_tag(root.tag), path)
  148. return client.MULTI_STATUS, multistatus
  149. if (root.tag == xmlutils.make_clark("C:calendar-multiget") and
  150. collection.tag != "VCALENDAR" or
  151. root.tag == xmlutils.make_clark("CR:addressbook-multiget") and
  152. collection.tag != "VADDRESSBOOK" or
  153. root.tag == xmlutils.make_clark("D:sync-collection") and
  154. collection.tag not in ("VADDRESSBOOK", "VCALENDAR")):
  155. logger.warning("Invalid REPORT method %r on %r requested",
  156. xmlutils.make_human_tag(root.tag), path)
  157. return client.FORBIDDEN, xmlutils.webdav_error("D:supported-report")
  158. props: Union[ET.Element, List]
  159. if root.find(xmlutils.make_clark("D:prop")) is not None:
  160. props = root.find(xmlutils.make_clark("D:prop")) # type: ignore[assignment]
  161. else:
  162. props = []
  163. hreferences: Iterable[str]
  164. if root.tag in (
  165. xmlutils.make_clark("C:calendar-multiget"),
  166. xmlutils.make_clark("CR:addressbook-multiget")):
  167. # Read rfc4791-7.9 for info
  168. hreferences = set()
  169. for href_element in root.findall(xmlutils.make_clark("D:href")):
  170. temp_url_path = urlparse(href_element.text).path
  171. assert isinstance(temp_url_path, str)
  172. href_path = pathutils.sanitize_path(unquote(temp_url_path))
  173. if (href_path + "/").startswith(base_prefix + "/"):
  174. hreferences.add(href_path[len(base_prefix):])
  175. else:
  176. logger.warning("Skipping invalid path %r in REPORT request on "
  177. "%r", href_path, path)
  178. elif root.tag == xmlutils.make_clark("D:sync-collection"):
  179. old_sync_token_element = root.find(
  180. xmlutils.make_clark("D:sync-token"))
  181. old_sync_token = ""
  182. if old_sync_token_element is not None and old_sync_token_element.text:
  183. old_sync_token = old_sync_token_element.text.strip()
  184. logger.debug("Client provided sync token: %r", old_sync_token)
  185. try:
  186. sync_token, names = collection.sync(old_sync_token)
  187. except ValueError as e:
  188. # Invalid sync token
  189. logger.warning("Client provided invalid sync token %r: %s",
  190. old_sync_token, e, exc_info=True)
  191. # client.CONFLICT doesn't work with some clients (e.g. InfCloud)
  192. return (client.FORBIDDEN,
  193. xmlutils.webdav_error("D:valid-sync-token"))
  194. hreferences = (pathutils.unstrip_path(
  195. posixpath.join(collection.path, n)) for n in names)
  196. # Append current sync token to response
  197. sync_token_element = ET.Element(xmlutils.make_clark("D:sync-token"))
  198. sync_token_element.text = sync_token
  199. multistatus.append(sync_token_element)
  200. else:
  201. hreferences = (path,)
  202. filters = (
  203. root.findall(xmlutils.make_clark("C:filter")) +
  204. root.findall(xmlutils.make_clark("CR:filter")))
  205. expand = root.find(".//" + xmlutils.make_clark("C:expand"))
  206. # if we have expand prop we use "filter (except time range) -> expand -> filter (only time range)" approach
  207. time_range_element = None
  208. main_filters = []
  209. for filter_ in filters:
  210. # extract time-range filter for processing after main filters
  211. # for expand request
  212. time_range_element = filter_.find(".//" + xmlutils.make_clark("C:time-range"))
  213. if expand is None or time_range_element is None:
  214. main_filters.append(filter_)
  215. # Retrieve everything required for finishing the request.
  216. retrieved_items = list(retrieve_items(
  217. base_prefix, path, collection, hreferences, main_filters, multistatus))
  218. collection_tag = collection.tag
  219. # !!! Don't access storage after this !!!
  220. unlock_storage_fn()
  221. while retrieved_items:
  222. # ``item.vobject_item`` might be accessed during filtering.
  223. # Don't keep reference to ``item``, because VObject requires a lot of
  224. # memory.
  225. item, filters_matched = retrieved_items.pop(0)
  226. if filters and not filters_matched:
  227. try:
  228. if not all(test_filter(collection_tag, item, filter_)
  229. for filter_ in main_filters):
  230. continue
  231. except ValueError as e:
  232. raise ValueError("Failed to filter item %r from %r: %s" %
  233. (item.href, collection.path, e)) from e
  234. except Exception as e:
  235. raise RuntimeError("Failed to filter item %r from %r: %s" %
  236. (item.href, collection.path, e)) from e
  237. found_props = []
  238. not_found_props = []
  239. for prop in props:
  240. element = ET.Element(prop.tag)
  241. if prop.tag == xmlutils.make_clark("D:getetag"):
  242. element.text = item.etag
  243. found_props.append(element)
  244. elif prop.tag == xmlutils.make_clark("D:getcontenttype"):
  245. element.text = xmlutils.get_content_type(item, encoding)
  246. found_props.append(element)
  247. elif prop.tag in (
  248. xmlutils.make_clark("C:calendar-data"),
  249. xmlutils.make_clark("CR:address-data")):
  250. element.text = item.serialize()
  251. if (expand is not None) and item.component_name == 'VEVENT':
  252. start = expand.get('start')
  253. end = expand.get('end')
  254. if (start is None) or (end is None):
  255. return client.FORBIDDEN, \
  256. xmlutils.webdav_error("C:expand")
  257. start = datetime.datetime.strptime(
  258. start, '%Y%m%dT%H%M%SZ'
  259. ).replace(tzinfo=datetime.timezone.utc)
  260. end = datetime.datetime.strptime(
  261. end, '%Y%m%dT%H%M%SZ'
  262. ).replace(tzinfo=datetime.timezone.utc)
  263. time_range_start = None
  264. time_range_end = None
  265. if time_range_element is not None:
  266. time_range_start, time_range_end = radicale_filter.parse_time_range(time_range_element)
  267. expanded_element = _expand(
  268. element=element, item=copy.copy(item),
  269. start=start, end=end,
  270. time_range_start=time_range_start, time_range_end=time_range_end,
  271. )
  272. found_props.append(expanded_element)
  273. else:
  274. found_props.append(element)
  275. else:
  276. not_found_props.append(element)
  277. assert item.href
  278. uri = pathutils.unstrip_path(
  279. posixpath.join(collection.path, item.href))
  280. multistatus.append(xml_item_response(
  281. base_prefix, uri, found_props=found_props,
  282. not_found_props=not_found_props, found_item=True))
  283. return client.MULTI_STATUS, multistatus
  284. def _expand(
  285. element: ET.Element,
  286. item: radicale_item.Item,
  287. start: datetime.datetime,
  288. end: datetime.datetime,
  289. time_range_start: Optional[datetime.datetime] = None,
  290. time_range_end: Optional[datetime.datetime] = None,
  291. ) -> ET.Element:
  292. vevent_component: vobject.base.Component = copy.copy(item.vobject_item)
  293. logger.info("Expanding event %s", item.href)
  294. # Split the vevents included in the component into one that contains the
  295. # recurrence information and others that contain a recurrence id to
  296. # override instances.
  297. vevent_recurrence, vevents_overridden = _split_overridden_vevents(vevent_component)
  298. dt_format = '%Y%m%dT%H%M%SZ'
  299. all_day_event = False
  300. if type(vevent_recurrence.dtstart.value) is datetime.date:
  301. # If an event comes to us with a dtstart specified as a date
  302. # then in the response we return the date, not datetime
  303. dt_format = '%Y%m%d'
  304. all_day_event = True
  305. # In case of dates, we need to remove timezone information since
  306. # rruleset.between computes with datetimes without timezone information
  307. start = start.replace(tzinfo=None)
  308. end = end.replace(tzinfo=None)
  309. if time_range_start is not None and time_range_end is not None:
  310. time_range_start = time_range_start.replace(tzinfo=None)
  311. time_range_end = time_range_end.replace(tzinfo=None)
  312. for vevent in vevents_overridden:
  313. _strip_single_event(vevent, dt_format)
  314. duration = None
  315. if hasattr(vevent_recurrence, "dtend"):
  316. duration = vevent_recurrence.dtend.value - vevent_recurrence.dtstart.value
  317. elif hasattr(vevent_recurrence, "duration"):
  318. duration = vevent_recurrence.duration.value
  319. # Handle EXDATE to limit expansion range
  320. if hasattr(vevent_recurrence, 'exdate'):
  321. exdates = vevent_recurrence.exdate.value
  322. if not isinstance(exdates, list):
  323. exdates = [exdates]
  324. logger.debug("EXDATE values: %s", exdates)
  325. latest_exdate = max(exdates) if exdates else None
  326. if latest_exdate and end > latest_exdate:
  327. end = min(end, latest_exdate)
  328. rruleset = None
  329. if hasattr(vevent_recurrence, 'rrule'):
  330. rruleset = vevent_recurrence.getrruleset()
  331. filtered_vevents = []
  332. if rruleset:
  333. # This function uses datetimes internally without timezone info for dates
  334. # A vobject rruleset is for the event dtstart.
  335. # Expanded over a given time range this will not include
  336. # events which started before the time range but are still
  337. # ongoing at the start of the range
  338. # To accomodate this, reduce the start time by the duration of
  339. # the event. If this introduces an extra reccurence point then
  340. # that event should be included as it is still ongoing. If no
  341. # extra point is generated then it was a no-op.
  342. rstart = start - duration if duration else start
  343. recurrences = rruleset.between(rstart, end, inc=True)
  344. _strip_component(vevent_component)
  345. _strip_single_event(vevent_recurrence, dt_format)
  346. i_overridden = 0
  347. for recurrence_dt in recurrences:
  348. recurrence_utc = recurrence_dt if all_day_event else recurrence_dt.astimezone(datetime.timezone.utc)
  349. logger.debug("Processing recurrence: %s (all_day_event: %s)", recurrence_utc, all_day_event)
  350. # Apply time-range filter
  351. if time_range_start is not None and time_range_end is not None:
  352. dtstart = recurrence_utc
  353. dtend = dtstart + duration if duration else dtstart
  354. # Start includes the time, end does not
  355. if not (dtstart <= time_range_end and dtend > time_range_start):
  356. logger.debug("Recurrence %s filtered out by time-range", recurrence_utc)
  357. continue
  358. # Check for overridden instances
  359. i_overridden, vevent = _find_overridden(i_overridden, vevents_overridden, recurrence_utc, dt_format)
  360. if not vevent:
  361. # Create new instance from recurrence
  362. vevent = copy.deepcopy(vevent_recurrence)
  363. # For all day events, the system timezone may influence the
  364. # results, so use recurrence_dt
  365. recurrence_id = recurrence_dt if all_day_event else recurrence_utc
  366. logger.debug("Creating new VEVENT with RECURRENCE-ID: %s", recurrence_id)
  367. vevent.recurrence_id = ContentLine(
  368. name='RECURRENCE-ID',
  369. value=recurrence_id, params={}
  370. )
  371. _convert_to_utc(vevent, 'recurrence_id', dt_format)
  372. vevent.dtstart = ContentLine(
  373. name='DTSTART',
  374. value=recurrence_id.strftime(dt_format), params={}
  375. )
  376. # if there is a DTEND, override it. Duration does not need changing
  377. if hasattr(vevent, "dtend"):
  378. vevent.dtend = ContentLine(
  379. name='DTEND',
  380. value=(recurrence_id + duration).strftime(dt_format), params={}
  381. )
  382. filtered_vevents.append(vevent)
  383. # Filter overridden and recurrence base events
  384. if time_range_start is not None and time_range_end is not None:
  385. for vevent in vevents_overridden + [vevent_recurrence]:
  386. dtstart = vevent.dtstart.value
  387. dtend = vevent.dtend.value if hasattr(vevent, 'dtend') else dtstart
  388. logger.debug(
  389. "Filtering VEVENT with DTSTART: %s (type: %s), DTEND: %s (type: %s)",
  390. dtstart, type(dtstart), dtend, type(dtend))
  391. # Handle string values for DTSTART/DTEND
  392. if isinstance(dtstart, str):
  393. try:
  394. dtstart = datetime.datetime.strptime(dtstart, dt_format)
  395. if all_day_event:
  396. dtstart = dtstart.date()
  397. except ValueError as e:
  398. logger.warning("Invalid DTSTART format: %s, error: %s", dtstart, e)
  399. continue
  400. if isinstance(dtend, str):
  401. try:
  402. dtend = datetime.datetime.strptime(dtend, dt_format)
  403. if all_day_event:
  404. dtend = dtend.date()
  405. except ValueError as e:
  406. logger.warning("Invalid DTEND format: %s, error: %s", dtend, e)
  407. continue
  408. # Convert to datetime for comparison
  409. if all_day_event and isinstance(dtstart, datetime.date) and not isinstance(dtstart, datetime.datetime):
  410. dtstart = datetime.datetime.fromordinal(dtstart.toordinal()).replace(tzinfo=None)
  411. dtend = datetime.datetime.fromordinal(dtend.toordinal()).replace(tzinfo=None)
  412. elif not all_day_event and isinstance(dtstart, datetime.datetime) \
  413. and isinstance(dtend, datetime.datetime):
  414. dtstart = dtstart.replace(tzinfo=datetime.timezone.utc)
  415. dtend = dtend.replace(tzinfo=datetime.timezone.utc)
  416. else:
  417. logger.warning("Unexpected DTSTART/DTEND type: dtstart=%s, dtend=%s", type(dtstart), type(dtend))
  418. continue
  419. if dtstart < time_range_end and dtend > time_range_start:
  420. if vevent not in filtered_vevents: # Avoid duplicates
  421. logger.debug("VEVENT passed time-range filter: %s", dtstart)
  422. filtered_vevents.append(vevent)
  423. else:
  424. logger.debug("VEVENT filtered out: %s", dtstart)
  425. # Rebuild component
  426. # ToDo: Get rid of return vevent_recurrence if filtered_vevents is empty it's wrong behavior
  427. vevent_component.vevent_list = filtered_vevents if filtered_vevents else [vevent_recurrence]
  428. element.text = vevent_component.serialize()
  429. logger.debug("Returning %d VEVENTs", len(vevent_component.vevent_list))
  430. return element
  431. def _convert_timezone(vevent: vobject.icalendar.RecurringComponent,
  432. name_prop: str,
  433. name_content_line: str):
  434. prop = getattr(vevent, name_prop, None)
  435. if prop:
  436. if type(prop.value) is datetime.date:
  437. date_time = datetime.datetime.fromordinal(
  438. prop.value.toordinal()
  439. ).replace(tzinfo=datetime.timezone.utc)
  440. else:
  441. date_time = prop.value.astimezone(datetime.timezone.utc)
  442. setattr(vevent, name_prop, ContentLine(name=name_content_line, value=date_time, params=[]))
  443. def _convert_to_utc(vevent: vobject.icalendar.RecurringComponent,
  444. name_prop: str,
  445. dt_format: str):
  446. prop = getattr(vevent, name_prop, None)
  447. if prop:
  448. setattr(vevent, name_prop, ContentLine(name=prop.name, value=prop.value.strftime(dt_format), params=[]))
  449. def _strip_single_event(vevent: vobject.icalendar.RecurringComponent, dt_format: str) -> None:
  450. _convert_timezone(vevent, 'dtstart', 'DTSTART')
  451. _convert_timezone(vevent, 'dtend', 'DTEND')
  452. _convert_timezone(vevent, 'recurrence_id', 'RECURRENCE-ID')
  453. # There is something strange behaviour during serialization native datetime, so converting manually
  454. _convert_to_utc(vevent, 'dtstart', dt_format)
  455. _convert_to_utc(vevent, 'dtend', dt_format)
  456. _convert_to_utc(vevent, 'recurrence_id', dt_format)
  457. try:
  458. delattr(vevent, 'rrule')
  459. delattr(vevent, 'exdate')
  460. delattr(vevent, 'exrule')
  461. delattr(vevent, 'rdate')
  462. except AttributeError:
  463. pass
  464. def _strip_component(vevent: vobject.base.Component) -> None:
  465. timezones_to_remove = []
  466. for component in vevent.components():
  467. if component.name == 'VTIMEZONE':
  468. timezones_to_remove.append(component)
  469. for timezone in timezones_to_remove:
  470. vevent.remove(timezone)
  471. def _split_overridden_vevents(
  472. component: vobject.base.Component,
  473. ) -> Tuple[
  474. vobject.icalendar.RecurringComponent,
  475. List[vobject.icalendar.RecurringComponent]
  476. ]:
  477. vevent_recurrence = None
  478. vevents_overridden = []
  479. for vevent in component.vevent_list:
  480. if hasattr(vevent, 'recurrence_id'):
  481. vevents_overridden += [vevent]
  482. elif vevent_recurrence:
  483. raise ValueError(
  484. f"component with UID {vevent.uid} "
  485. f"has more than one vevent with recurrence information"
  486. )
  487. else:
  488. vevent_recurrence = vevent
  489. if vevent_recurrence:
  490. return (
  491. vevent_recurrence, sorted(
  492. vevents_overridden,
  493. key=lambda vevent: vevent.recurrence_id.value
  494. )
  495. )
  496. else:
  497. raise ValueError(
  498. f"component with UID {vevent.uid} "
  499. f"does not have a vevent without a recurrence_id"
  500. )
  501. def _find_overridden(
  502. start: int,
  503. vevents: List[vobject.icalendar.RecurringComponent],
  504. dt: datetime.datetime,
  505. dt_format: str
  506. ) -> Tuple[int, Optional[vobject.icalendar.RecurringComponent]]:
  507. for i in range(start, len(vevents)):
  508. dt_event = datetime.datetime.strptime(
  509. vevents[i].recurrence_id.value,
  510. dt_format
  511. ).replace(tzinfo=datetime.timezone.utc)
  512. if dt_event == dt:
  513. return (i + 1, vevents[i])
  514. return (start, None)
  515. def xml_item_response(base_prefix: str, href: str,
  516. found_props: Sequence[ET.Element] = (),
  517. not_found_props: Sequence[ET.Element] = (),
  518. found_item: bool = True) -> ET.Element:
  519. response = ET.Element(xmlutils.make_clark("D:response"))
  520. href_element = ET.Element(xmlutils.make_clark("D:href"))
  521. href_element.text = xmlutils.make_href(base_prefix, href)
  522. response.append(href_element)
  523. if found_item:
  524. for code, props in ((200, found_props), (404, not_found_props)):
  525. if props:
  526. propstat = ET.Element(xmlutils.make_clark("D:propstat"))
  527. status = ET.Element(xmlutils.make_clark("D:status"))
  528. status.text = xmlutils.make_response(code)
  529. prop_element = ET.Element(xmlutils.make_clark("D:prop"))
  530. for prop in props:
  531. prop_element.append(prop)
  532. propstat.append(prop_element)
  533. propstat.append(status)
  534. response.append(propstat)
  535. else:
  536. status = ET.Element(xmlutils.make_clark("D:status"))
  537. status.text = xmlutils.make_response(404)
  538. response.append(status)
  539. return response
  540. def retrieve_items(
  541. base_prefix: str, path: str, collection: storage.BaseCollection,
  542. hreferences: Iterable[str], filters: Sequence[ET.Element],
  543. multistatus: ET.Element) -> Iterator[Tuple[radicale_item.Item, bool]]:
  544. """Retrieves all items that are referenced in ``hreferences`` from
  545. ``collection`` and adds 404 responses for missing and invalid items
  546. to ``multistatus``."""
  547. collection_requested = False
  548. def get_names() -> Iterator[str]:
  549. """Extracts all names from references in ``hreferences`` and adds
  550. 404 responses for invalid references to ``multistatus``.
  551. If the whole collections is referenced ``collection_requested``
  552. gets set to ``True``."""
  553. nonlocal collection_requested
  554. for hreference in hreferences:
  555. try:
  556. name = pathutils.name_from_path(hreference, collection)
  557. except ValueError as e:
  558. logger.warning("Skipping invalid path %r in REPORT request on "
  559. "%r: %s", hreference, path, e)
  560. response = xml_item_response(base_prefix, hreference,
  561. found_item=False)
  562. multistatus.append(response)
  563. continue
  564. if name:
  565. # Reference is an item
  566. yield name
  567. else:
  568. # Reference is a collection
  569. collection_requested = True
  570. for name, item in collection.get_multi(get_names()):
  571. if not item:
  572. uri = pathutils.unstrip_path(posixpath.join(collection.path, name))
  573. response = xml_item_response(base_prefix, uri, found_item=False)
  574. multistatus.append(response)
  575. else:
  576. yield item, False
  577. if collection_requested:
  578. yield from collection.get_filtered(filters)
  579. def test_filter(collection_tag: str, item: radicale_item.Item,
  580. filter_: ET.Element) -> bool:
  581. """Match an item against a filter."""
  582. if (collection_tag == "VCALENDAR" and
  583. filter_.tag != xmlutils.make_clark("C:%s" % filter_)):
  584. if len(filter_) == 0:
  585. return True
  586. if len(filter_) > 1:
  587. raise ValueError("Filter with %d children" % len(filter_))
  588. if filter_[0].tag != xmlutils.make_clark("C:comp-filter"):
  589. raise ValueError("Unexpected %r in filter" % filter_[0].tag)
  590. return radicale_filter.comp_match(item, filter_[0])
  591. if (collection_tag == "VADDRESSBOOK" and
  592. filter_.tag != xmlutils.make_clark("CR:%s" % filter_)):
  593. for child in filter_:
  594. if child.tag != xmlutils.make_clark("CR:prop-filter"):
  595. raise ValueError("Unexpected %r in filter" % child.tag)
  596. test = filter_.get("test", "anyof")
  597. if test == "anyof":
  598. return any(radicale_filter.prop_match(item.vobject_item, f, "CR")
  599. for f in filter_)
  600. if test == "allof":
  601. return all(radicale_filter.prop_match(item.vobject_item, f, "CR")
  602. for f in filter_)
  603. raise ValueError("Unsupported filter test: %r" % test)
  604. raise ValueError("Unsupported filter %r for %r" %
  605. (filter_.tag, collection_tag))
  606. class ApplicationPartReport(ApplicationBase):
  607. def do_REPORT(self, environ: types.WSGIEnviron, base_prefix: str,
  608. path: str, user: str) -> types.WSGIResponse:
  609. """Manage REPORT request."""
  610. access = Access(self._rights, user, path)
  611. if not access.check("r"):
  612. return httputils.NOT_ALLOWED
  613. try:
  614. xml_content = self._read_xml_request_body(environ)
  615. except RuntimeError as e:
  616. logger.warning("Bad REPORT request on %r: %s", path, e,
  617. exc_info=True)
  618. return httputils.BAD_REQUEST
  619. except socket.timeout:
  620. logger.debug("Client timed out", exc_info=True)
  621. return httputils.REQUEST_TIMEOUT
  622. with contextlib.ExitStack() as lock_stack:
  623. lock_stack.enter_context(self._storage.acquire_lock("r", user))
  624. item = next(iter(self._storage.discover(path)), None)
  625. if not item:
  626. return httputils.NOT_FOUND
  627. if not access.check("r", item):
  628. return httputils.NOT_ALLOWED
  629. if isinstance(item, storage.BaseCollection):
  630. collection = item
  631. else:
  632. assert item.collection is not None
  633. collection = item.collection
  634. if xml_content is not None and \
  635. xml_content.tag == xmlutils.make_clark("C:free-busy-query"):
  636. max_occurrence = self.configuration.get("reporting", "max_freebusy_occurrence")
  637. try:
  638. status, body = free_busy_report(
  639. base_prefix, path, xml_content, collection, self._encoding,
  640. lock_stack.close, max_occurrence)
  641. except ValueError as e:
  642. logger.warning(
  643. "Bad REPORT request on %r: %s", path, e, exc_info=True)
  644. return httputils.BAD_REQUEST
  645. headers = {"Content-Type": "text/calendar; charset=%s" % self._encoding}
  646. return status, headers, str(body)
  647. else:
  648. try:
  649. status, xml_answer = xml_report(
  650. base_prefix, path, xml_content, collection, self._encoding,
  651. lock_stack.close)
  652. except ValueError as e:
  653. logger.warning(
  654. "Bad REPORT request on %r: %s", path, e, exc_info=True)
  655. return httputils.BAD_REQUEST
  656. headers = {"Content-Type": "text/xml; charset=%s" % self._encoding}
  657. return status, headers, self._xml_response(xml_answer)