ical.py 15 KB

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