ical.py 16 KB

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