paul@0 | 1 | # -*- coding: iso-8859-1 -*- |
paul@0 | 2 | """ |
paul@0 | 3 | MoinMoin - MoinShare library |
paul@0 | 4 | |
paul@56 | 5 | @copyright: 2011, 2012, 2013, 2014 by Paul Boddie <paul@boddie.org.uk> |
paul@34 | 6 | @copyright: 2003-2006 Edgewall Software |
paul@34 | 7 | @copyright: 2006 MoinMoin:AlexanderSchremmer |
paul@0 | 8 | @license: GNU GPL (v2 or later), see COPYING.txt for details. |
paul@0 | 9 | """ |
paul@0 | 10 | |
paul@17 | 11 | from ContentTypeSupport import getContentPreferences |
paul@58 | 12 | from DateSupport import getCurrentTime, getDateTimeFromRFC2822, \ |
paul@58 | 13 | getDateTimeFromISO8601, DateTime |
paul@0 | 14 | from MoinSupport import * |
paul@58 | 15 | from MoinRemoteSupport import * |
paul@51 | 16 | from ItemSupport import ItemStore |
paul@50 | 17 | from MoinMessage import GPG, is_encrypted, is_signed, MoinMessageError |
paul@54 | 18 | from MoinMessageSupport import get_homedir, get_username_for_fingerprint |
paul@37 | 19 | from MoinMoin.support.htmlmarkup import HTMLParseError, HTMLSanitizer, Markup |
paul@15 | 20 | from MoinMoin import wikiutil |
paul@33 | 21 | from email.parser import Parser |
paul@58 | 22 | from email.utils import parsedate |
paul@37 | 23 | from codecs import getwriter |
paul@58 | 24 | import xml.dom.pulldom |
paul@0 | 25 | |
paul@25 | 26 | try: |
paul@25 | 27 | from cStringIO import StringIO |
paul@25 | 28 | except ImportError: |
paul@25 | 29 | from StringIO import StringIO |
paul@25 | 30 | |
paul@16 | 31 | _getFragments = getFragments |
paul@0 | 32 | |
paul@0 | 33 | __version__ = "0.1" |
paul@0 | 34 | |
paul@58 | 35 | ATOM_NS = "http://www.w3.org/2005/Atom" |
paul@58 | 36 | |
paul@58 | 37 | # Utility functions. |
paul@58 | 38 | |
paul@58 | 39 | def text(element): |
paul@58 | 40 | nodes = [] |
paul@58 | 41 | for node in element.childNodes: |
paul@58 | 42 | if node.nodeType == node.TEXT_NODE: |
paul@58 | 43 | nodes.append(node.nodeValue) |
paul@58 | 44 | return "".join(nodes) |
paul@58 | 45 | |
paul@58 | 46 | def children(element): |
paul@58 | 47 | nodes = [] |
paul@58 | 48 | for node in element.childNodes: |
paul@58 | 49 | nodes.append(node.toxml()) |
paul@58 | 50 | return "".join(nodes) |
paul@58 | 51 | |
paul@58 | 52 | def unescape(text): |
paul@58 | 53 | return text.replace("<", "<").replace(">", ">").replace("&", "&") |
paul@58 | 54 | |
paul@58 | 55 | def linktext(element, feed_type): |
paul@58 | 56 | if feed_type == "rss": |
paul@58 | 57 | return text(element) |
paul@58 | 58 | else: |
paul@58 | 59 | return element.getAttribute("href") |
paul@58 | 60 | |
paul@58 | 61 | def need_content(show_content, tagname): |
paul@58 | 62 | return show_content in ("content", "description") and tagname in ("content", "description") |
paul@58 | 63 | |
paul@0 | 64 | # More Moin 1.9 compatibility functions. |
paul@0 | 65 | |
paul@0 | 66 | def has_member(request, groupname, username): |
paul@0 | 67 | if hasattr(request.dicts, "has_member"): |
paul@0 | 68 | return request.dicts.has_member(groupname, username) |
paul@0 | 69 | else: |
paul@0 | 70 | return username in request.dicts.get(groupname, []) |
paul@0 | 71 | |
paul@3 | 72 | # Fragments employ a "moinshare" attribute. |
paul@3 | 73 | |
paul@3 | 74 | fragment_attribute = "moinshare" |
paul@2 | 75 | |
paul@16 | 76 | def getFragments(s): |
paul@25 | 77 | |
paul@25 | 78 | "Return all fragments in 's' having the MoinShare fragment attribute." |
paul@25 | 79 | |
paul@2 | 80 | fragments = [] |
paul@16 | 81 | for format, attributes, body in _getFragments(s): |
paul@16 | 82 | if attributes.has_key(fragment_attribute): |
paul@16 | 83 | fragments.append((format, attributes, body)) |
paul@2 | 84 | return fragments |
paul@2 | 85 | |
paul@9 | 86 | def getPreferredOutputTypes(request, mimetypes): |
paul@9 | 87 | |
paul@9 | 88 | """ |
paul@9 | 89 | Using the 'request', perform content negotiation, obtaining mimetypes common |
paul@9 | 90 | to the fragment (given by 'mimetypes') and the client (found in the Accept |
paul@9 | 91 | header). |
paul@9 | 92 | """ |
paul@9 | 93 | |
paul@9 | 94 | accept = getHeader(request, "Accept", "HTTP") |
paul@12 | 95 | if accept: |
paul@12 | 96 | prefs = getContentPreferences(accept) |
paul@12 | 97 | return prefs.get_preferred_types(mimetypes) |
paul@12 | 98 | else: |
paul@12 | 99 | return mimetypes |
paul@9 | 100 | |
paul@9 | 101 | def getUpdatedTime(metadata): |
paul@9 | 102 | |
paul@9 | 103 | """ |
paul@9 | 104 | Return the last updated time based on the given 'metadata', using the |
paul@9 | 105 | current time if no explicit last modified time is specified. |
paul@9 | 106 | """ |
paul@9 | 107 | |
paul@9 | 108 | # NOTE: We could attempt to get the last edit time of a fragment. |
paul@9 | 109 | |
paul@9 | 110 | latest_timestamp = metadata.get("last-modified") |
paul@9 | 111 | if latest_timestamp: |
paul@33 | 112 | return latest_timestamp |
paul@9 | 113 | else: |
paul@33 | 114 | return getCurrentTime() |
paul@9 | 115 | |
paul@30 | 116 | # Entry/update classes. |
paul@30 | 117 | |
paul@30 | 118 | class Update: |
paul@30 | 119 | |
paul@30 | 120 | "A feed update entry." |
paul@30 | 121 | |
paul@30 | 122 | def __init__(self): |
paul@30 | 123 | self.title = None |
paul@30 | 124 | self.link = None |
paul@30 | 125 | self.content = None |
paul@30 | 126 | self.content_type = None |
paul@30 | 127 | self.updated = None |
paul@54 | 128 | self.author = None |
paul@30 | 129 | |
paul@30 | 130 | # Page-related attributes. |
paul@30 | 131 | |
paul@30 | 132 | self.fragment = None |
paul@30 | 133 | self.preferred = None |
paul@30 | 134 | |
paul@33 | 135 | # Message-related attributes. |
paul@33 | 136 | |
paul@34 | 137 | self.message_number = None |
paul@33 | 138 | self.parts = None |
paul@33 | 139 | |
paul@34 | 140 | # Message- and page-related attributes. |
paul@34 | 141 | |
paul@34 | 142 | self.page = None |
paul@34 | 143 | |
paul@40 | 144 | # Identification. |
paul@40 | 145 | |
paul@40 | 146 | self.path = [] |
paul@40 | 147 | |
paul@40 | 148 | def unique_id(self): |
paul@40 | 149 | return "moinshare-tab-%s-%s" % (self.message_number, "-".join(map(str, self.path))) |
paul@40 | 150 | |
paul@30 | 151 | def __cmp__(self, other): |
paul@30 | 152 | if self.updated is None and other.updated is not None: |
paul@30 | 153 | return 1 |
paul@30 | 154 | elif self.updated is not None and other.updated is None: |
paul@30 | 155 | return -1 |
paul@30 | 156 | else: |
paul@30 | 157 | return cmp(self.updated, other.updated) |
paul@30 | 158 | |
paul@40 | 159 | def copy(self, part_number=None): |
paul@40 | 160 | update = Update() |
paul@40 | 161 | update.title = self.title |
paul@40 | 162 | update.link = self.link |
paul@40 | 163 | update.updated = self.updated |
paul@54 | 164 | update.author = self.author |
paul@40 | 165 | update.fragment = self.fragment |
paul@40 | 166 | update.preferred = self.preferred |
paul@40 | 167 | update.message_number = self.message_number |
paul@40 | 168 | update.page = self.page |
paul@40 | 169 | update.path = self.path[:] |
paul@40 | 170 | if part_number is not None: |
paul@40 | 171 | update.path.append(part_number) |
paul@40 | 172 | return update |
paul@40 | 173 | |
paul@58 | 174 | # Error classes. |
paul@58 | 175 | |
paul@58 | 176 | class FeedError(Exception): |
paul@58 | 177 | pass |
paul@58 | 178 | |
paul@58 | 179 | class FeedMissingError(FeedError): |
paul@58 | 180 | pass |
paul@58 | 181 | |
paul@58 | 182 | class FeedContentTypeError(FeedError): |
paul@58 | 183 | pass |
paul@58 | 184 | |
paul@58 | 185 | # Feed retrieval from URLs. |
paul@58 | 186 | |
paul@58 | 187 | def getUpdates(request, feed_url, max_entries, show_content): |
paul@58 | 188 | |
paul@58 | 189 | """ |
paul@58 | 190 | Using the given 'request', retrieve from 'feed_url' up to the given number |
paul@58 | 191 | 'max_entries' of update entries. The 'show_content' parameter can indicate |
paul@58 | 192 | that a "summary" is to be obtained for each update, that the "content" of |
paul@58 | 193 | each update is to be obtained (falling back to a summary if no content is |
paul@58 | 194 | provided), or no content (indicated by a false value) is to be obtained. |
paul@58 | 195 | |
paul@58 | 196 | A tuple of the form ((feed_type, channel_title, channel_link), updates) is |
paul@58 | 197 | returned. |
paul@58 | 198 | """ |
paul@58 | 199 | |
paul@58 | 200 | feed_updates = [] |
paul@58 | 201 | |
paul@58 | 202 | # Obtain the resource, using a cached version if appropriate. |
paul@58 | 203 | |
paul@58 | 204 | max_cache_age = int(getattr(request.cfg, "moin_share_max_cache_age", "300")) |
paul@58 | 205 | data = getCachedResource(request, feed_url, "MoinShare", "wiki", max_cache_age) |
paul@58 | 206 | if not data: |
paul@58 | 207 | raise FeedMissingError |
paul@58 | 208 | |
paul@58 | 209 | # Interpret the cached feed. |
paul@58 | 210 | |
paul@58 | 211 | feed = StringIO(data) |
paul@58 | 212 | _url, content_type, _encoding, _metadata = getCachedResourceMetadata(feed) |
paul@58 | 213 | |
paul@58 | 214 | if content_type not in ("application/atom+xml", "application/rss+xml", "application/xml"): |
paul@58 | 215 | raise FeedContentTypeError |
paul@58 | 216 | |
paul@58 | 217 | try: |
paul@58 | 218 | # Parse each node from the feed. |
paul@58 | 219 | |
paul@58 | 220 | channel_title = channel_link = None |
paul@58 | 221 | |
paul@58 | 222 | feed_type = None |
paul@58 | 223 | update = None |
paul@58 | 224 | in_source = False |
paul@58 | 225 | |
paul@58 | 226 | events = xml.dom.pulldom.parse(feed) |
paul@58 | 227 | |
paul@58 | 228 | for event, value in events: |
paul@58 | 229 | |
paul@58 | 230 | if not in_source and event == xml.dom.pulldom.START_ELEMENT: |
paul@58 | 231 | tagname = value.localName |
paul@58 | 232 | |
paul@58 | 233 | # Detect the feed type and items. |
paul@58 | 234 | |
paul@58 | 235 | if tagname == "feed" and value.namespaceURI == ATOM_NS: |
paul@58 | 236 | feed_type = "atom" |
paul@58 | 237 | |
paul@58 | 238 | elif tagname == "rss": |
paul@58 | 239 | feed_type = "rss" |
paul@58 | 240 | |
paul@58 | 241 | # Detect items. |
paul@58 | 242 | |
paul@58 | 243 | elif feed_type == "rss" and tagname == "item" or \ |
paul@58 | 244 | feed_type == "atom" and tagname == "entry": |
paul@58 | 245 | |
paul@58 | 246 | update = Update() |
paul@58 | 247 | |
paul@58 | 248 | # Detect source declarations. |
paul@58 | 249 | |
paul@58 | 250 | elif feed_type == "atom" and tagname == "source": |
paul@58 | 251 | in_source = True |
paul@58 | 252 | |
paul@58 | 253 | # Handle item elements. |
paul@58 | 254 | |
paul@58 | 255 | elif tagname == "title": |
paul@58 | 256 | events.expandNode(value) |
paul@58 | 257 | if update: |
paul@58 | 258 | update.title = text(value) |
paul@58 | 259 | else: |
paul@58 | 260 | channel_title = text(value) |
paul@58 | 261 | |
paul@58 | 262 | elif tagname == "link": |
paul@58 | 263 | events.expandNode(value) |
paul@58 | 264 | if update: |
paul@58 | 265 | update.link = linktext(value, feed_type) |
paul@58 | 266 | else: |
paul@58 | 267 | channel_link = linktext(value, feed_type) |
paul@58 | 268 | |
paul@58 | 269 | elif show_content and ( |
paul@58 | 270 | feed_type == "atom" and tagname in ("content", "summary") or |
paul@58 | 271 | feed_type == "rss" and tagname == "description"): |
paul@58 | 272 | |
paul@58 | 273 | events.expandNode(value) |
paul@58 | 274 | |
paul@58 | 275 | # Obtain content where requested or, failing that, a |
paul@58 | 276 | # summary. |
paul@58 | 277 | |
paul@58 | 278 | if update and (need_content(show_content, tagname) or tagname == "summary" and not update.content): |
paul@58 | 279 | if feed_type == "atom": |
paul@58 | 280 | update.content_type = value.getAttribute("type") or "text" |
paul@58 | 281 | |
paul@58 | 282 | # Normalise the content types and extract the |
paul@58 | 283 | # content. |
paul@58 | 284 | |
paul@58 | 285 | if update.content_type in ("xhtml", "application/xhtml+xml", "application/xml"): |
paul@58 | 286 | update.content = children(value) |
paul@58 | 287 | update.content_type = "application/xhtml+xml" |
paul@58 | 288 | elif update.content_type in ("html", "text/html"): |
paul@58 | 289 | update.content = text(value) |
paul@58 | 290 | update.content_type = "text/html" |
paul@58 | 291 | else: |
paul@58 | 292 | update.content = text(value) |
paul@58 | 293 | update.content_type = "text/plain" |
paul@58 | 294 | else: |
paul@58 | 295 | update.content_type = "text/html" |
paul@58 | 296 | update.content = text(value) |
paul@58 | 297 | |
paul@58 | 298 | elif feed_type == "atom" and tagname == "updated" or \ |
paul@58 | 299 | feed_type == "rss" and tagname == "pubDate": |
paul@58 | 300 | |
paul@58 | 301 | events.expandNode(value) |
paul@58 | 302 | |
paul@58 | 303 | if update: |
paul@58 | 304 | if feed_type == "atom": |
paul@58 | 305 | value = getDateTimeFromISO8601(text(value)) |
paul@58 | 306 | else: |
paul@58 | 307 | value = DateTime(parsedate(text(value))) |
paul@58 | 308 | update.updated = value |
paul@58 | 309 | |
paul@58 | 310 | elif event == xml.dom.pulldom.END_ELEMENT: |
paul@58 | 311 | tagname = value.localName |
paul@58 | 312 | |
paul@58 | 313 | if feed_type == "rss" and tagname == "item" or \ |
paul@58 | 314 | feed_type == "atom" and tagname == "entry": |
paul@58 | 315 | |
paul@58 | 316 | feed_updates.append(update) |
paul@58 | 317 | |
paul@58 | 318 | update = None |
paul@58 | 319 | |
paul@58 | 320 | elif feed_type == "atom" and tagname == "source": |
paul@58 | 321 | in_source = False |
paul@58 | 322 | |
paul@58 | 323 | finally: |
paul@58 | 324 | feed.close() |
paul@58 | 325 | |
paul@58 | 326 | return (feed_type, channel_title, channel_link), feed_updates |
paul@58 | 327 | |
paul@30 | 328 | # Update retrieval from pages. |
paul@30 | 329 | |
paul@30 | 330 | def getUpdatesFromPage(page, request): |
paul@25 | 331 | |
paul@25 | 332 | """ |
paul@30 | 333 | Get updates from the given 'page' using the 'request'. A list of update |
paul@30 | 334 | objects is returned. |
paul@25 | 335 | """ |
paul@25 | 336 | |
paul@25 | 337 | updates = [] |
paul@25 | 338 | |
paul@25 | 339 | # NOTE: Use the updated datetime from the page for updates. |
paul@25 | 340 | # NOTE: The published and updated details would need to be deduced from |
paul@25 | 341 | # NOTE: the page history instead of being taken from the page as a whole. |
paul@25 | 342 | |
paul@25 | 343 | metadata = getMetadata(page) |
paul@25 | 344 | updated = getUpdatedTime(metadata) |
paul@25 | 345 | |
paul@25 | 346 | # Get the fragment regions for the page. |
paul@25 | 347 | |
paul@25 | 348 | for n, (format, attributes, body) in enumerate(getFragments(page.get_raw_body())): |
paul@25 | 349 | |
paul@33 | 350 | update = Update() |
paul@33 | 351 | |
paul@25 | 352 | # Produce a fragment identifier. |
paul@25 | 353 | # NOTE: Choose a more robust identifier where none is explicitly given. |
paul@25 | 354 | |
paul@30 | 355 | update.fragment = attributes.get("fragment", str(n)) |
paul@30 | 356 | update.title = attributes.get("summary", "Update #%d" % n) |
paul@25 | 357 | |
paul@25 | 358 | # Get the preferred content types available for the fragment. |
paul@25 | 359 | |
paul@30 | 360 | update.preferred = getPreferredOutputTypes(request, getOutputTypes(request, format)) |
paul@25 | 361 | |
paul@25 | 362 | # Try and obtain some suitable content for the entry. |
paul@25 | 363 | # NOTE: Could potentially get a summary for the fragment. |
paul@25 | 364 | |
paul@30 | 365 | update.content = None |
paul@25 | 366 | |
paul@30 | 367 | if "text/html" in update.preferred: |
paul@25 | 368 | parser_cls = getParserClass(request, format) |
paul@25 | 369 | |
paul@25 | 370 | if format == "html": |
paul@30 | 371 | update.content = body |
paul@39 | 372 | elif hasattr(parser_cls, "formatForOutputType"): |
paul@39 | 373 | update.content = formatTextForOutputType(body, request, parser_cls, "text/html") |
paul@25 | 374 | else: |
paul@25 | 375 | fmt = request.html_formatter |
paul@25 | 376 | fmt.setPage(page) |
paul@30 | 377 | update.content = formatText(body, request, fmt, parser_cls) |
paul@30 | 378 | |
paul@32 | 379 | update.content_type = "text/html" |
paul@25 | 380 | |
paul@34 | 381 | update.page = page |
paul@37 | 382 | |
paul@37 | 383 | # NOTE: The anchor would be supported in the page, but this requires |
paul@37 | 384 | # NOTE: formatter modifications for the regions providing updates. |
paul@37 | 385 | |
paul@37 | 386 | update.link = page.url(request, anchor=update.fragment) |
paul@30 | 387 | update.updated = updated |
paul@30 | 388 | |
paul@30 | 389 | updates.append(update) |
paul@25 | 390 | |
paul@25 | 391 | return updates |
paul@25 | 392 | |
paul@33 | 393 | # Update retrieval from message stores. |
paul@33 | 394 | |
paul@33 | 395 | def getUpdatesFromStore(page, request): |
paul@33 | 396 | |
paul@33 | 397 | """ |
paul@33 | 398 | Get updates from the message store associated with the given 'page' using |
paul@33 | 399 | the 'request'. A list of update objects is returned. |
paul@33 | 400 | """ |
paul@33 | 401 | |
paul@33 | 402 | updates = [] |
paul@33 | 403 | |
paul@33 | 404 | metadata = getMetadata(page) |
paul@33 | 405 | updated = getUpdatedTime(metadata) |
paul@33 | 406 | |
paul@33 | 407 | store = ItemStore(page, "messages", "message-locks") |
paul@33 | 408 | |
paul@57 | 409 | keys = store.keys() |
paul@57 | 410 | keys.sort() |
paul@57 | 411 | |
paul@57 | 412 | for key in keys: |
paul@57 | 413 | message_text = store[key] |
paul@57 | 414 | update = getUpdateFromMessageText(message_text, key, request) |
paul@34 | 415 | update.page = page |
paul@33 | 416 | updates.append(update) |
paul@33 | 417 | |
paul@33 | 418 | return updates |
paul@33 | 419 | |
paul@47 | 420 | def getUpdateFromMessageText(message_text, message_number, request): |
paul@46 | 421 | |
paul@46 | 422 | "Return an update for the given 'message_text' and 'message_number'." |
paul@46 | 423 | |
paul@46 | 424 | update = Update() |
paul@56 | 425 | message = Parser().parsestr(message_text) |
paul@46 | 426 | |
paul@46 | 427 | # Produce a fragment identifier. |
paul@46 | 428 | |
paul@46 | 429 | update.fragment = update.updated = getDateTimeFromRFC2822(message.get("date")) |
paul@46 | 430 | update.title = message.get("subject", "Update #%d" % message_number) |
paul@54 | 431 | update.author = message.get("moin-user") |
paul@46 | 432 | |
paul@46 | 433 | update.message_number = message_number |
paul@46 | 434 | |
paul@54 | 435 | update.content, update.content_type, update.parts, actual_author = \ |
paul@54 | 436 | getUpdateContentFromPart(message, request) |
paul@54 | 437 | |
paul@54 | 438 | if actual_author: |
paul@54 | 439 | update.author = actual_author |
paul@54 | 440 | |
paul@46 | 441 | return update |
paul@46 | 442 | |
paul@47 | 443 | def getUpdateContentFromPart(part, request): |
paul@40 | 444 | |
paul@40 | 445 | """ |
paul@54 | 446 | Return decoded content, the content type, any subparts, and any author |
paul@54 | 447 | identity in a tuple for a given 'part'. |
paul@40 | 448 | """ |
paul@40 | 449 | |
paul@40 | 450 | # Determine whether the part has several representations. |
paul@40 | 451 | |
paul@40 | 452 | # For a single part, use it as the update content. |
paul@40 | 453 | |
paul@40 | 454 | if not part.is_multipart(): |
paul@40 | 455 | content, content_type = getPartContent(part) |
paul@54 | 456 | return content, content_type, None, None |
paul@40 | 457 | |
paul@40 | 458 | # For a collection of related parts, use the first as the update content |
paul@40 | 459 | # and assume that the formatter will reference the other parts. |
paul@40 | 460 | |
paul@40 | 461 | elif part.get_content_subtype() == "related": |
paul@40 | 462 | main_part = part.get_payload()[0] |
paul@40 | 463 | content, content_type = getPartContent(main_part) |
paul@54 | 464 | return content, content_type, [main_part], None |
paul@40 | 465 | |
paul@46 | 466 | # Encrypted content cannot be meaningfully separated. |
paul@46 | 467 | |
paul@46 | 468 | elif part.get_content_subtype() == "encrypted": |
paul@50 | 469 | try: |
paul@54 | 470 | part, author = getDecryptedParts(part, request) |
paul@54 | 471 | content, content_type, parts, _author = getUpdateContentFromPart(part, request) |
paul@54 | 472 | return content, content_type, parts, author |
paul@50 | 473 | except MoinMessageError: |
paul@54 | 474 | return None, part.get_content_type(), part.get_payload(), None |
paul@46 | 475 | |
paul@40 | 476 | # Otherwise, just obtain the parts for separate display. |
paul@40 | 477 | |
paul@40 | 478 | else: |
paul@54 | 479 | return None, part.get_content_type(), part.get_payload(), None |
paul@40 | 480 | |
paul@47 | 481 | def getDecryptedParts(part, request): |
paul@47 | 482 | |
paul@47 | 483 | "Decrypt the given 'part', returning the decoded content." |
paul@47 | 484 | |
paul@47 | 485 | homedir = get_homedir(request) |
paul@47 | 486 | gpg = GPG(homedir) |
paul@47 | 487 | |
paul@47 | 488 | # Decrypt the part. |
paul@47 | 489 | |
paul@47 | 490 | if is_encrypted(part): |
paul@47 | 491 | text = gpg.decryptMessage(part) |
paul@56 | 492 | part = Parser().parsestr(text) |
paul@47 | 493 | |
paul@47 | 494 | # Extract any signature details. |
paul@47 | 495 | |
paul@47 | 496 | if is_signed(part): |
paul@47 | 497 | result = gpg.verifyMessage(part) |
paul@47 | 498 | if result: |
paul@47 | 499 | fingerprint, identity, content = result |
paul@54 | 500 | return content, get_username_for_fingerprint(request, fingerprint) |
paul@47 | 501 | |
paul@54 | 502 | return part, None |
paul@47 | 503 | |
paul@40 | 504 | def getPartContent(part): |
paul@40 | 505 | |
paul@40 | 506 | "Decode the 'part', returning the decoded payload and the content type." |
paul@40 | 507 | |
paul@40 | 508 | charset = part.get_content_charset() |
paul@40 | 509 | payload = part.get_payload(decode=True) |
paul@40 | 510 | return (charset and unicode(payload, charset) or payload), part.get_content_type() |
paul@40 | 511 | |
paul@47 | 512 | def getUpdateFromPart(parent, part, part_number, request): |
paul@40 | 513 | |
paul@40 | 514 | "Using the 'parent' update, return an update object for the given 'part'." |
paul@40 | 515 | |
paul@40 | 516 | update = parent.copy(part_number) |
paul@55 | 517 | update.content, update.content_type, update.parts, update.author = getUpdateContentFromPart(part, request) |
paul@40 | 518 | return update |
paul@40 | 519 | |
paul@47 | 520 | def getUpdatesForFormatting(update, request): |
paul@46 | 521 | |
paul@46 | 522 | "Get a list of updates for formatting given 'update'." |
paul@46 | 523 | |
paul@46 | 524 | updates = [] |
paul@46 | 525 | |
paul@46 | 526 | # Handle multipart/alternative and other non-related multiparts. |
paul@46 | 527 | |
paul@46 | 528 | if update.parts: |
paul@46 | 529 | for n, part in enumerate(update.parts): |
paul@47 | 530 | update_part = getUpdateFromPart(update, part, n, request) |
paul@47 | 531 | updates += getUpdatesForFormatting(update_part, request) |
paul@46 | 532 | else: |
paul@46 | 533 | updates.append(update) |
paul@46 | 534 | |
paul@46 | 535 | return updates |
paul@46 | 536 | |
paul@46 | 537 | # Update formatting. |
paul@46 | 538 | |
paul@46 | 539 | def getFormattedUpdate(update, request, fmt): |
paul@46 | 540 | |
paul@46 | 541 | """ |
paul@46 | 542 | Return the formatted form of the given 'update' using the given 'request' |
paul@46 | 543 | and 'fmt'. |
paul@46 | 544 | """ |
paul@46 | 545 | |
paul@46 | 546 | # NOTE: Some control over the HTML and XHTML should be exercised. |
paul@46 | 547 | |
paul@46 | 548 | if update.content: |
paul@46 | 549 | if update.content_type == "text/html" and update.message_number is not None: |
paul@46 | 550 | parsers = [get_make_parser(update.page, update.message_number)] |
paul@46 | 551 | else: |
paul@46 | 552 | parsers = getParsersForContentType(request.cfg, update.content_type) |
paul@46 | 553 | |
paul@46 | 554 | if parsers: |
paul@46 | 555 | for parser_cls in parsers: |
paul@46 | 556 | if hasattr(parser_cls, "formatForOutputType"): |
paul@46 | 557 | return formatTextForOutputType(update.content, request, parser_cls, "text/html") |
paul@46 | 558 | else: |
paul@46 | 559 | return formatText(update.content, request, fmt, parser_cls=parser_cls) |
paul@46 | 560 | break |
paul@46 | 561 | else: |
paul@46 | 562 | return None |
paul@46 | 563 | else: |
paul@46 | 564 | return None |
paul@46 | 565 | |
paul@46 | 566 | def formatUpdate(update, request, fmt): |
paul@46 | 567 | |
paul@46 | 568 | "Format the given 'update' using the given 'request' and 'fmt'." |
paul@46 | 569 | |
paul@46 | 570 | result = [] |
paul@46 | 571 | append = result.append |
paul@46 | 572 | |
paul@47 | 573 | updates = getUpdatesForFormatting(update, request) |
paul@46 | 574 | single = len(updates) == 1 |
paul@46 | 575 | |
paul@46 | 576 | # Format some navigation tabs. |
paul@46 | 577 | |
paul@46 | 578 | if not single: |
paul@46 | 579 | append(fmt.div(on=1, css_class="moinshare-alternatives")) |
paul@46 | 580 | |
paul@46 | 581 | first = True |
paul@46 | 582 | |
paul@46 | 583 | for update_part in updates: |
paul@46 | 584 | append(fmt.url(1, "#%s" % update_part.unique_id())) |
paul@46 | 585 | append(fmt.text(update_part.content_type)) |
paul@46 | 586 | append(fmt.url(0)) |
paul@46 | 587 | |
paul@46 | 588 | first = False |
paul@46 | 589 | |
paul@46 | 590 | append(fmt.div(on=0)) |
paul@46 | 591 | |
paul@46 | 592 | # Format the content. |
paul@46 | 593 | |
paul@46 | 594 | first = True |
paul@46 | 595 | |
paul@46 | 596 | for update_part in updates: |
paul@46 | 597 | |
paul@46 | 598 | # Encapsulate each alternative if many exist. |
paul@46 | 599 | |
paul@46 | 600 | if not single: |
paul@46 | 601 | css_class = first and "moinshare-default" or "moinshare-other" |
paul@46 | 602 | append(fmt.div(on=1, css_class="moinshare-alternative %s" % css_class, id=update_part.unique_id())) |
paul@46 | 603 | |
paul@46 | 604 | # Include the content. |
paul@46 | 605 | |
paul@46 | 606 | append(formatUpdatePart(update_part, request, fmt)) |
paul@46 | 607 | |
paul@46 | 608 | if not single: |
paul@46 | 609 | append(fmt.div(on=0)) |
paul@46 | 610 | |
paul@46 | 611 | first = False |
paul@46 | 612 | |
paul@46 | 613 | return "".join(result) |
paul@46 | 614 | |
paul@46 | 615 | def formatUpdatePart(update, request, fmt): |
paul@46 | 616 | |
paul@46 | 617 | "Format the given 'update' using the given 'request' and 'fmt'." |
paul@46 | 618 | |
paul@46 | 619 | _ = request.getText |
paul@46 | 620 | |
paul@46 | 621 | result = [] |
paul@46 | 622 | append = result.append |
paul@46 | 623 | |
paul@46 | 624 | # Encapsulate the content. |
paul@46 | 625 | |
paul@46 | 626 | append(fmt.div(on=1, css_class="moinshare-content")) |
paul@46 | 627 | text = getFormattedUpdate(update, request, fmt) |
paul@46 | 628 | if text: |
paul@46 | 629 | append(text) |
paul@46 | 630 | else: |
paul@46 | 631 | append(fmt.text(_("Update cannot be shown for content of type %s.") % update.content_type)) |
paul@46 | 632 | append(fmt.div(on=0)) |
paul@46 | 633 | |
paul@46 | 634 | return "".join(result) |
paul@46 | 635 | |
paul@31 | 636 | # Source management. |
paul@31 | 637 | |
paul@31 | 638 | def getUpdateSources(pagename, request): |
paul@31 | 639 | |
paul@31 | 640 | "Return the update sources from the given 'pagename' using the 'request'." |
paul@31 | 641 | |
paul@31 | 642 | sources = {} |
paul@31 | 643 | |
paul@31 | 644 | source_definitions = getWikiDict(pagename, request) |
paul@31 | 645 | |
paul@31 | 646 | if source_definitions: |
paul@31 | 647 | for name, value in source_definitions.items(): |
paul@31 | 648 | sources[name] = getSourceParameters(value) |
paul@31 | 649 | |
paul@31 | 650 | return sources |
paul@31 | 651 | |
paul@31 | 652 | def getSourceParameters(source_definition): |
paul@31 | 653 | |
paul@31 | 654 | "Return the parameters from the given 'source_definition' string." |
paul@31 | 655 | |
paul@43 | 656 | return parseDictEntry(source_definition, ("type", "location")) |
paul@31 | 657 | |
paul@34 | 658 | # HTML parsing support. |
paul@34 | 659 | |
paul@34 | 660 | class IncomingHTMLSanitizer(HTMLSanitizer): |
paul@34 | 661 | |
paul@34 | 662 | "An HTML parser that rewrites references to attachments." |
paul@34 | 663 | |
paul@34 | 664 | def __init__(self, out, request, page, message_number): |
paul@34 | 665 | HTMLSanitizer.__init__(self, out) |
paul@34 | 666 | self.request = request |
paul@34 | 667 | self.message_number = message_number |
paul@34 | 668 | self.page = page |
paul@34 | 669 | |
paul@34 | 670 | def rewrite_reference(self, ref): |
paul@34 | 671 | if ref.startswith("cid:"): |
paul@34 | 672 | part = ref[len("cid:"):] |
paul@34 | 673 | action_link = self.page.url(self.request, { |
paul@34 | 674 | "action" : "ReadMessage", "doit" : "1", |
paul@34 | 675 | "message" : self.message_number, "part" : part |
paul@34 | 676 | }) |
paul@34 | 677 | return action_link |
paul@34 | 678 | else: |
paul@34 | 679 | return ref |
paul@34 | 680 | |
paul@34 | 681 | def handle_starttag(self, tag, attrs): |
paul@34 | 682 | new_attrs = [] |
paul@34 | 683 | for attrname, attrvalue in attrs: |
paul@34 | 684 | if attrname in self.uri_attrs: |
paul@34 | 685 | new_attrs.append((attrname, self.rewrite_reference(attrvalue))) |
paul@34 | 686 | else: |
paul@34 | 687 | new_attrs.append((attrname, attrvalue)) |
paul@34 | 688 | HTMLSanitizer.handle_starttag(self, tag, new_attrs) |
paul@34 | 689 | |
paul@34 | 690 | class IncomingMarkup(Markup): |
paul@34 | 691 | |
paul@34 | 692 | "A special markup processor for incoming HTML." |
paul@34 | 693 | |
paul@34 | 694 | def sanitize(self, request, page, message_number): |
paul@37 | 695 | out = getwriter("utf-8")(StringIO()) |
paul@34 | 696 | sanitizer = IncomingHTMLSanitizer(out, request, page, message_number) |
paul@34 | 697 | sanitizer.feed(self.stripentities(keepxmlentities=True)) |
paul@37 | 698 | return IncomingMarkup(unicode(out.getvalue(), "utf-8")) |
paul@34 | 699 | |
paul@34 | 700 | class IncomingHTMLParser: |
paul@34 | 701 | |
paul@34 | 702 | "Filters and rewrites incoming HTML content." |
paul@34 | 703 | |
paul@34 | 704 | def __init__(self, raw, request, **kw): |
paul@34 | 705 | self.raw = raw |
paul@34 | 706 | self.request = request |
paul@34 | 707 | self.message_number = None |
paul@34 | 708 | self.page = None |
paul@34 | 709 | |
paul@34 | 710 | def format(self, formatter, **kw): |
paul@34 | 711 | |
paul@34 | 712 | "Send the text." |
paul@34 | 713 | |
paul@34 | 714 | try: |
paul@34 | 715 | self.request.write(formatter.rawHTML(IncomingMarkup(self.raw).sanitize(self.request, self.page, self.message_number))) |
paul@34 | 716 | except HTMLParseError, e: |
paul@34 | 717 | self.request.write(formatter.sysmsg(1) + |
paul@34 | 718 | formatter.text(u'HTML parsing error: %s in "%s"' % (e.msg, |
paul@34 | 719 | self.raw.splitlines()[e.lineno - 1].strip())) + |
paul@34 | 720 | formatter.sysmsg(0)) |
paul@34 | 721 | |
paul@34 | 722 | class MakeIncomingHTMLParser: |
paul@34 | 723 | |
paul@34 | 724 | "A class that makes parsers configured for messages." |
paul@34 | 725 | |
paul@34 | 726 | def __init__(self, page, message_number): |
paul@34 | 727 | |
paul@34 | 728 | "Initialise with state that is used to configure instantiated parsers." |
paul@34 | 729 | |
paul@34 | 730 | self.message_number = message_number |
paul@34 | 731 | self.page = page |
paul@34 | 732 | |
paul@34 | 733 | def __call__(self, *args, **kw): |
paul@34 | 734 | parser = IncomingHTMLParser(*args, **kw) |
paul@34 | 735 | parser.message_number = self.message_number |
paul@34 | 736 | parser.page = self.page |
paul@34 | 737 | return parser |
paul@34 | 738 | |
paul@34 | 739 | def get_make_parser(page, message_number): |
paul@34 | 740 | |
paul@34 | 741 | """ |
paul@34 | 742 | Return a callable that will return a parser configured for the message from |
paul@34 | 743 | the given 'page' with the given 'message_number'. |
paul@34 | 744 | """ |
paul@34 | 745 | |
paul@34 | 746 | return MakeIncomingHTMLParser(page, message_number) |
paul@34 | 747 | |
paul@0 | 748 | # vim: tabstop=4 expandtab shiftwidth=4 |