ical.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. # This file is part of Radicale Server - Calendar Server
  2. # Copyright © 2008 Nicolas Kandel
  3. # Copyright © 2008 Pascal Halter
  4. # Copyright © 2008-2016 Guillaume Ayoub
  5. #
  6. # This library is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # This library is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with Radicale. If not, see <http://www.gnu.org/licenses/>.
  18. """
  19. Radicale collection classes.
  20. Define the main classes of a collection as seen from the server.
  21. """
  22. import hashlib
  23. import os
  24. import posixpath
  25. import re
  26. from contextlib import contextmanager
  27. from random import randint
  28. from uuid import uuid4
  29. import vobject
  30. def serialize(tag, headers=(), items=()):
  31. """Return a text corresponding to given collection ``tag``.
  32. The text may have the given ``headers`` and ``items`` added around the
  33. items if needed (ie. for calendars).
  34. """
  35. items = sorted(items, key=lambda x: x.name)
  36. if tag == "VADDRESSBOOK":
  37. lines = [item.text.strip() for item in items]
  38. else:
  39. lines = ["BEGIN:%s" % tag]
  40. for part in (headers, items):
  41. if part:
  42. lines.append("\r\n".join(item.text.strip() for item in part))
  43. lines.append("END:%s" % tag)
  44. lines.append("")
  45. return "\r\n".join(lines)
  46. def sanitize_path(path):
  47. """Make path absolute with leading slash to prevent access to other data.
  48. Preserve a potential trailing slash.
  49. """
  50. trailing_slash = "/" if path.endswith("/") else ""
  51. path = posixpath.normpath(path)
  52. new_path = "/"
  53. for part in path.split("/"):
  54. if not part or part in (".", ".."):
  55. continue
  56. new_path = posixpath.join(new_path, part)
  57. trailing_slash = "" if new_path.endswith("/") else trailing_slash
  58. return new_path + trailing_slash
  59. def clean_name(name):
  60. """Clean an item name by removing slashes and leading/ending brackets."""
  61. # Remove leading and ending brackets that may have been put by Outlook
  62. name = name.strip("{}")
  63. # Remove slashes, mostly unwanted when saving on filesystems
  64. name = name.replace("/", "_")
  65. return name
  66. def unfold(text):
  67. """Unfold multi-lines attributes.
  68. Read rfc5545-3.1 for info.
  69. """
  70. return re.sub('\r\n( |\t)', '', text).splitlines()
  71. class Item(object):
  72. """Internal iCal item."""
  73. def __init__(self, text, name=None):
  74. """Initialize object from ``text`` and different ``kwargs``."""
  75. self.component = vobject.readOne(text)
  76. self._name = name
  77. if not self.component.name:
  78. # Header
  79. self._name = next(self.component.lines()).name.lower()
  80. return
  81. # We must synchronize the name in the text and in the object.
  82. # An item must have a name, determined in order by:
  83. #
  84. # - the ``name`` parameter
  85. # - the ``X-RADICALE-NAME`` iCal property (for Events, Todos, Journals)
  86. # - the ``UID`` iCal property (for Events, Todos, Journals)
  87. # - the ``TZID`` iCal property (for Timezones)
  88. if not self._name:
  89. for line in self.component.lines():
  90. if line.name in ("X-RADICALE-NAME", "UID", "TZID"):
  91. self._name = line.value
  92. if line.name == "X-RADICALE-NAME":
  93. break
  94. if self._name:
  95. self._name = clean_name(self._name)
  96. else:
  97. self._name = uuid4().hex
  98. if not hasattr(self.component, "x_radicale_name"):
  99. self.component.add("X-RADICALE-NAME")
  100. self.component.x_radicale_name.value = self._name
  101. def __hash__(self):
  102. return hash(self.text)
  103. def __eq__(self, item):
  104. return isinstance(item, Item) and self.text == item.text
  105. @property
  106. def etag(self):
  107. """Item etag.
  108. Etag is mainly used to know if an item has changed.
  109. """
  110. md5 = hashlib.md5()
  111. md5.update(self.text.encode("utf-8"))
  112. return '"%s"' % md5.hexdigest()
  113. @property
  114. def name(self):
  115. """Item name.
  116. Name is mainly used to give an URL to the item.
  117. """
  118. return self._name
  119. @property
  120. def text(self):
  121. """Item serialized text."""
  122. return self.component.serialize()
  123. class Header(Item):
  124. """Internal header class."""
  125. class Timezone(Item):
  126. """Internal timezone class."""
  127. tag = "VTIMEZONE"
  128. class Component(Item):
  129. """Internal main component of a collection."""
  130. class Event(Component):
  131. """Internal event class."""
  132. tag = "VEVENT"
  133. mimetype = "text/calendar"
  134. class Todo(Component):
  135. """Internal todo class."""
  136. tag = "VTODO" # pylint: disable=W0511
  137. mimetype = "text/calendar"
  138. class Journal(Component):
  139. """Internal journal class."""
  140. tag = "VJOURNAL"
  141. mimetype = "text/calendar"
  142. class Card(Component):
  143. """Internal card class."""
  144. tag = "VCARD"
  145. mimetype = "text/vcard"
  146. class Collection(object):
  147. """Internal collection item.
  148. This class must be overridden and replaced by a storage backend.
  149. """
  150. def __init__(self, path, principal=False):
  151. """Initialize the collection.
  152. ``path`` must be the normalized relative path of the collection, using
  153. the slash as the folder delimiter, with no leading nor trailing slash.
  154. """
  155. self.encoding = "utf-8"
  156. # path should already be sanitized
  157. self.path = sanitize_path(path).strip("/")
  158. split_path = self.path.split("/")
  159. if principal and split_path and self.is_node(self.path):
  160. # Already existing principal collection
  161. self.owner = split_path[0]
  162. elif len(split_path) > 1:
  163. # URL with at least one folder
  164. self.owner = split_path[0]
  165. else:
  166. self.owner = None
  167. self.is_principal = principal
  168. self._items = None
  169. @classmethod
  170. def from_path(cls, path, depth="1", include_container=True):
  171. """Return a list of collections and items under the given ``path``.
  172. If ``depth`` is "0", only the actual object under ``path`` is
  173. returned.
  174. If ``depth`` is anything but "0", it is considered as "1" and direct
  175. children are included in the result. If ``include_container`` is
  176. ``True`` (the default), the containing object is included in the
  177. result.
  178. The ``path`` is relative.
  179. """
  180. # path == None means wrong URL
  181. if path is None:
  182. return []
  183. # path should already be sanitized
  184. sane_path = sanitize_path(path).strip("/")
  185. attributes = sane_path.split("/")
  186. if not attributes:
  187. return []
  188. # Try to guess if the path leads to a collection or an item
  189. if cls.is_leaf("/".join(attributes[:-1])):
  190. attributes.pop()
  191. result = []
  192. path = "/".join(attributes)
  193. principal = len(attributes) <= 1
  194. if cls.is_node(path):
  195. if depth == "0":
  196. result.append(cls(path, principal))
  197. else:
  198. if include_container:
  199. result.append(cls(path, principal))
  200. for child in cls.children(path):
  201. result.append(child)
  202. else:
  203. if depth == "0":
  204. result.append(cls(path))
  205. else:
  206. collection = cls(path, principal)
  207. if include_container:
  208. result.append(collection)
  209. result.extend(collection.components)
  210. return result
  211. def save(self, text):
  212. """Save the text into the collection."""
  213. raise NotImplementedError
  214. def delete(self):
  215. """Delete the collection."""
  216. raise NotImplementedError
  217. @property
  218. def text(self):
  219. """Collection as plain text."""
  220. raise NotImplementedError
  221. @classmethod
  222. def children(cls, path):
  223. """Yield the children of the collection at local ``path``."""
  224. raise NotImplementedError
  225. @classmethod
  226. def is_node(cls, path):
  227. """Return ``True`` if relative ``path`` is a node.
  228. A node is a WebDAV collection whose members are other collections.
  229. """
  230. raise NotImplementedError
  231. @classmethod
  232. def is_leaf(cls, path):
  233. """Return ``True`` if relative ``path`` is a leaf.
  234. A leaf is a WebDAV collection whose members are not collections.
  235. """
  236. raise NotImplementedError
  237. @property
  238. def last_modified(self):
  239. """Get the last time the collection has been modified.
  240. The date is formatted according to rfc1123-5.2.14.
  241. """
  242. raise NotImplementedError
  243. @property
  244. @contextmanager
  245. def props(self):
  246. """Get the collection properties."""
  247. raise NotImplementedError
  248. @property
  249. def exists(self):
  250. """``True`` if the collection exists on the storage, else ``False``."""
  251. return self.is_node(self.path) or self.is_leaf(self.path)
  252. @staticmethod
  253. def _parse(text, item_types, name=None):
  254. """Find items with type in ``item_types`` in ``text``.
  255. If ``name`` is given, give this name to new items in ``text``.
  256. Return a dict of items.
  257. """
  258. item_tags = {item_type.tag: item_type for item_type in item_types}
  259. items = {}
  260. root = next(vobject.readComponents(text))
  261. components = (
  262. root.components() if root.name in ("VADDRESSBOOK", "VCALENDAR")
  263. else (root,))
  264. for component in components:
  265. item_name = None if component.name == "VTIMEZONE" else name
  266. item_type = item_tags[component.name]
  267. item = item_type(component.serialize(), item_name)
  268. if item.name in items:
  269. text = "\r\n".join((item.text, items[item.name].text))
  270. items[item.name] = item_type(text, item.name)
  271. else:
  272. items[item.name] = item
  273. return items
  274. def append(self, name, text):
  275. """Append items from ``text`` to collection.
  276. If ``name`` is given, give this name to new items in ``text``.
  277. """
  278. new_items = self._parse(
  279. text, (Timezone, Event, Todo, Journal, Card), name)
  280. for new_item in new_items.values():
  281. if new_item.name not in self.items:
  282. self.items[new_item.name] = new_item
  283. self.write()
  284. def remove(self, name):
  285. """Remove object named ``name`` from collection."""
  286. if name in self.items:
  287. del self.items[name]
  288. self.write()
  289. def replace(self, name, text):
  290. """Replace content by ``text`` in collection objet called ``name``."""
  291. self.remove(name)
  292. self.append(name, text)
  293. def write(self):
  294. """Write collection with given parameters."""
  295. text = serialize(self.tag, self.headers, self.items.values())
  296. self.save(text)
  297. def set_mimetype(self, mimetype):
  298. """Set the mimetype of the collection."""
  299. with self.props as props:
  300. if "tag" not in props:
  301. if mimetype == "text/vcard":
  302. props["tag"] = "VADDRESSBOOK"
  303. else:
  304. props["tag"] = "VCALENDAR"
  305. @property
  306. def tag(self):
  307. """Type of the collection."""
  308. with self.props as props:
  309. if "tag" not in props:
  310. try:
  311. tag = open(self.path).readlines()[0][6:].rstrip()
  312. except IOError:
  313. if self.path.endswith((".vcf", "/carddav")):
  314. props["tag"] = "VADDRESSBOOK"
  315. else:
  316. props["tag"] = "VCALENDAR"
  317. else:
  318. if tag in ("VADDRESSBOOK", "VCARD"):
  319. props["tag"] = "VADDRESSBOOK"
  320. else:
  321. props["tag"] = "VCALENDAR"
  322. return props["tag"]
  323. @property
  324. def mimetype(self):
  325. """Mimetype of the collection."""
  326. if self.tag == "VADDRESSBOOK":
  327. return "text/vcard"
  328. elif self.tag == "VCALENDAR":
  329. return "text/calendar"
  330. @property
  331. def resource_type(self):
  332. """Resource type of the collection."""
  333. if self.tag == "VADDRESSBOOK":
  334. return "addressbook"
  335. elif self.tag == "VCALENDAR":
  336. return "calendar"
  337. @property
  338. def etag(self):
  339. """Etag from collection."""
  340. md5 = hashlib.md5()
  341. md5.update(self.text.encode("utf-8"))
  342. return '"%s"' % md5.hexdigest()
  343. @property
  344. def name(self):
  345. """Collection name."""
  346. with self.props as props:
  347. return props.get("D:displayname", self.path.split(os.path.sep)[-1])
  348. @property
  349. def color(self):
  350. """Collection color."""
  351. with self.props as props:
  352. if "ICAL:calendar-color" not in props:
  353. props["ICAL:calendar-color"] = "#%x" % randint(0, 255 ** 3 - 1)
  354. return props["ICAL:calendar-color"]
  355. @property
  356. def headers(self):
  357. """Find headers items in collection."""
  358. header_lines = []
  359. lines = unfold(self.text)[1:]
  360. for line in lines:
  361. if line.startswith(("BEGIN:", "END:")):
  362. break
  363. header_lines.append(Header(line))
  364. return header_lines or (
  365. Header("PRODID:-//Radicale//NONSGML Radicale Server//EN"),
  366. Header("VERSION:%s" % self.version))
  367. @property
  368. def items(self):
  369. """Get list of all items in collection."""
  370. if self._items is None:
  371. self._items = self._parse(
  372. self.text, (Event, Todo, Journal, Card, Timezone))
  373. return self._items
  374. @property
  375. def timezones(self):
  376. """Get list of all timezones in collection."""
  377. return [
  378. item for item in self.items.values() if item.tag == Timezone.tag]
  379. @property
  380. def components(self):
  381. """Get list of all components in collection."""
  382. tags = [item_type.tag for item_type in (Event, Todo, Journal, Card)]
  383. return [item for item in self.items.values() if item.tag in tags]
  384. @property
  385. def owner_url(self):
  386. """Get the collection URL according to its owner."""
  387. return "/%s/" % self.owner if self.owner else None
  388. @property
  389. def url(self):
  390. """Get the standard collection URL."""
  391. return "%s/" % self.path
  392. @property
  393. def version(self):
  394. """Get the version of the collection type."""
  395. return "3.0" if self.tag == "VADDRESSBOOK" else "2.0"