1 # -*- coding: iso-8859-1 -*- 2 """ 3 MoinMoin - MoinShare library 4 5 @copyright: 2011, 2012, 2013 by Paul Boddie <paul@boddie.org.uk> 6 @copyright: 2003-2006 Edgewall Software 7 @copyright: 2006 MoinMoin:AlexanderSchremmer 8 @license: GNU GPL (v2 or later), see COPYING.txt for details. 9 """ 10 11 from ContentTypeSupport import getContentPreferences 12 from DateSupport import getCurrentTime, getDateTimeFromRFC2822 13 from MoinSupport import * 14 from MoinMessage import GPG, is_encrypted, is_signed 15 from MoinMessageSupport import get_homedir 16 from MoinMoin.support.htmlmarkup import HTMLParseError, HTMLSanitizer, Markup 17 from MoinMoin import wikiutil 18 from email.parser import Parser 19 from codecs import getwriter 20 21 try: 22 from cStringIO import StringIO 23 except ImportError: 24 from StringIO import StringIO 25 26 _getFragments = getFragments 27 28 __version__ = "0.1" 29 30 # More Moin 1.9 compatibility functions. 31 32 def has_member(request, groupname, username): 33 if hasattr(request.dicts, "has_member"): 34 return request.dicts.has_member(groupname, username) 35 else: 36 return username in request.dicts.get(groupname, []) 37 38 # Fragments employ a "moinshare" attribute. 39 40 fragment_attribute = "moinshare" 41 42 def getFragments(s): 43 44 "Return all fragments in 's' having the MoinShare fragment attribute." 45 46 fragments = [] 47 for format, attributes, body in _getFragments(s): 48 if attributes.has_key(fragment_attribute): 49 fragments.append((format, attributes, body)) 50 return fragments 51 52 def getPreferredOutputTypes(request, mimetypes): 53 54 """ 55 Using the 'request', perform content negotiation, obtaining mimetypes common 56 to the fragment (given by 'mimetypes') and the client (found in the Accept 57 header). 58 """ 59 60 accept = getHeader(request, "Accept", "HTTP") 61 if accept: 62 prefs = getContentPreferences(accept) 63 return prefs.get_preferred_types(mimetypes) 64 else: 65 return mimetypes 66 67 def getUpdatedTime(metadata): 68 69 """ 70 Return the last updated time based on the given 'metadata', using the 71 current time if no explicit last modified time is specified. 72 """ 73 74 # NOTE: We could attempt to get the last edit time of a fragment. 75 76 latest_timestamp = metadata.get("last-modified") 77 if latest_timestamp: 78 return latest_timestamp 79 else: 80 return getCurrentTime() 81 82 # Entry/update classes. 83 84 class Update: 85 86 "A feed update entry." 87 88 def __init__(self): 89 self.title = None 90 self.link = None 91 self.content = None 92 self.content_type = None 93 self.updated = None 94 95 # Page-related attributes. 96 97 self.fragment = None 98 self.preferred = None 99 100 # Message-related attributes. 101 102 self.message_number = None 103 self.parts = None 104 105 # Message- and page-related attributes. 106 107 self.page = None 108 109 # Identification. 110 111 self.path = [] 112 113 def unique_id(self): 114 return "moinshare-tab-%s-%s" % (self.message_number, "-".join(map(str, self.path))) 115 116 def __cmp__(self, other): 117 if self.updated is None and other.updated is not None: 118 return 1 119 elif self.updated is not None and other.updated is None: 120 return -1 121 else: 122 return cmp(self.updated, other.updated) 123 124 def copy(self, part_number=None): 125 update = Update() 126 update.title = self.title 127 update.link = self.link 128 update.updated = self.updated 129 update.fragment = self.fragment 130 update.preferred = self.preferred 131 update.message_number = self.message_number 132 update.page = self.page 133 update.path = self.path[:] 134 if part_number is not None: 135 update.path.append(part_number) 136 return update 137 138 # Update retrieval from pages. 139 140 def getUpdatesFromPage(page, request): 141 142 """ 143 Get updates from the given 'page' using the 'request'. A list of update 144 objects is returned. 145 """ 146 147 updates = [] 148 149 # NOTE: Use the updated datetime from the page for updates. 150 # NOTE: The published and updated details would need to be deduced from 151 # NOTE: the page history instead of being taken from the page as a whole. 152 153 metadata = getMetadata(page) 154 updated = getUpdatedTime(metadata) 155 156 # Get the fragment regions for the page. 157 158 for n, (format, attributes, body) in enumerate(getFragments(page.get_raw_body())): 159 160 update = Update() 161 162 # Produce a fragment identifier. 163 # NOTE: Choose a more robust identifier where none is explicitly given. 164 165 update.fragment = attributes.get("fragment", str(n)) 166 update.title = attributes.get("summary", "Update #%d" % n) 167 168 # Get the preferred content types available for the fragment. 169 170 update.preferred = getPreferredOutputTypes(request, getOutputTypes(request, format)) 171 172 # Try and obtain some suitable content for the entry. 173 # NOTE: Could potentially get a summary for the fragment. 174 175 update.content = None 176 177 if "text/html" in update.preferred: 178 parser_cls = getParserClass(request, format) 179 180 if format == "html": 181 update.content = body 182 elif hasattr(parser_cls, "formatForOutputType"): 183 update.content = formatTextForOutputType(body, request, parser_cls, "text/html") 184 else: 185 fmt = request.html_formatter 186 fmt.setPage(page) 187 update.content = formatText(body, request, fmt, parser_cls) 188 189 update.content_type = "text/html" 190 191 update.page = page 192 193 # NOTE: The anchor would be supported in the page, but this requires 194 # NOTE: formatter modifications for the regions providing updates. 195 196 update.link = page.url(request, anchor=update.fragment) 197 update.updated = updated 198 199 updates.append(update) 200 201 return updates 202 203 # Update retrieval from message stores. 204 205 def getUpdatesFromStore(page, request): 206 207 """ 208 Get updates from the message store associated with the given 'page' using 209 the 'request'. A list of update objects is returned. 210 """ 211 212 updates = [] 213 214 metadata = getMetadata(page) 215 updated = getUpdatedTime(metadata) 216 217 store = ItemStore(page, "messages", "message-locks") 218 219 for n, message_text in enumerate(iter(store)): 220 update = getUpdateFromMessageText(message_text, n, request) 221 update.page = page 222 updates.append(update) 223 224 return updates 225 226 def getUpdateFromMessageText(message_text, message_number, request): 227 228 "Return an update for the given 'message_text' and 'message_number'." 229 230 update = Update() 231 message = Parser().parse(StringIO(message_text)) 232 233 # Produce a fragment identifier. 234 235 update.fragment = update.updated = getDateTimeFromRFC2822(message.get("date")) 236 update.title = message.get("subject", "Update #%d" % message_number) 237 238 update.message_number = message_number 239 240 update.content, update.content_type, update.parts = getUpdateContentFromPart(message, request) 241 return update 242 243 def getUpdateContentFromPart(part, request): 244 245 """ 246 Return decoded content, the content type and any subparts in a tuple for a 247 given 'part'. 248 """ 249 250 # Determine whether the part has several representations. 251 252 # For a single part, use it as the update content. 253 254 if not part.is_multipart(): 255 content, content_type = getPartContent(part) 256 return content, content_type, None 257 258 # For a collection of related parts, use the first as the update content 259 # and assume that the formatter will reference the other parts. 260 261 elif part.get_content_subtype() == "related": 262 main_part = part.get_payload()[0] 263 content, content_type = getPartContent(main_part) 264 return content, content_type, [main_part] 265 266 # Encrypted content cannot be meaningfully separated. 267 268 elif part.get_content_subtype() == "encrypted": 269 return getUpdateContentFromPart(getDecryptedParts(part, request), request) 270 271 # Otherwise, just obtain the parts for separate display. 272 273 else: 274 return None, part.get_content_type(), part.get_payload() 275 276 def getDecryptedParts(part, request): 277 278 "Decrypt the given 'part', returning the decoded content." 279 280 homedir = get_homedir(request) 281 gpg = GPG(homedir) 282 283 # Decrypt the part. 284 285 if is_encrypted(part): 286 text = gpg.decryptMessage(part) 287 part = Parser().parse(StringIO(text)) 288 289 # Extract any signature details. 290 # NOTE: Incorporate the signature into the output. 291 292 if is_signed(part): 293 result = gpg.verifyMessage(part) 294 if result: 295 fingerprint, identity, content = result 296 return content 297 298 return part 299 300 def getPartContent(part): 301 302 "Decode the 'part', returning the decoded payload and the content type." 303 304 charset = part.get_content_charset() 305 payload = part.get_payload(decode=True) 306 return (charset and unicode(payload, charset) or payload), part.get_content_type() 307 308 def getUpdateFromPart(parent, part, part_number, request): 309 310 "Using the 'parent' update, return an update object for the given 'part'." 311 312 update = parent.copy(part_number) 313 update.content, update.content_type, update.parts = getUpdateContentFromPart(part, request) 314 return update 315 316 def getUpdatesForFormatting(update, request): 317 318 "Get a list of updates for formatting given 'update'." 319 320 updates = [] 321 322 # Handle multipart/alternative and other non-related multiparts. 323 324 if update.parts: 325 for n, part in enumerate(update.parts): 326 update_part = getUpdateFromPart(update, part, n, request) 327 updates += getUpdatesForFormatting(update_part, request) 328 else: 329 updates.append(update) 330 331 return updates 332 333 # Update formatting. 334 335 def getFormattedUpdate(update, request, fmt): 336 337 """ 338 Return the formatted form of the given 'update' using the given 'request' 339 and 'fmt'. 340 """ 341 342 # NOTE: Some control over the HTML and XHTML should be exercised. 343 344 if update.content: 345 if update.content_type == "text/html" and update.message_number is not None: 346 parsers = [get_make_parser(update.page, update.message_number)] 347 else: 348 parsers = getParsersForContentType(request.cfg, update.content_type) 349 350 if parsers: 351 for parser_cls in parsers: 352 if hasattr(parser_cls, "formatForOutputType"): 353 return formatTextForOutputType(update.content, request, parser_cls, "text/html") 354 else: 355 return formatText(update.content, request, fmt, parser_cls=parser_cls) 356 break 357 else: 358 return None 359 else: 360 return None 361 362 def formatUpdate(update, request, fmt): 363 364 "Format the given 'update' using the given 'request' and 'fmt'." 365 366 result = [] 367 append = result.append 368 369 updates = getUpdatesForFormatting(update, request) 370 single = len(updates) == 1 371 372 # Format some navigation tabs. 373 374 if not single: 375 append(fmt.div(on=1, css_class="moinshare-alternatives")) 376 377 first = True 378 379 for update_part in updates: 380 append(fmt.url(1, "#%s" % update_part.unique_id())) 381 append(fmt.text(update_part.content_type)) 382 append(fmt.url(0)) 383 384 first = False 385 386 append(fmt.div(on=0)) 387 388 # Format the content. 389 390 first = True 391 392 for update_part in updates: 393 394 # Encapsulate each alternative if many exist. 395 396 if not single: 397 css_class = first and "moinshare-default" or "moinshare-other" 398 append(fmt.div(on=1, css_class="moinshare-alternative %s" % css_class, id=update_part.unique_id())) 399 400 # Include the content. 401 402 append(formatUpdatePart(update_part, request, fmt)) 403 404 if not single: 405 append(fmt.div(on=0)) 406 407 first = False 408 409 return "".join(result) 410 411 def formatUpdatePart(update, request, fmt): 412 413 "Format the given 'update' using the given 'request' and 'fmt'." 414 415 _ = request.getText 416 417 result = [] 418 append = result.append 419 420 # Encapsulate the content. 421 422 append(fmt.div(on=1, css_class="moinshare-content")) 423 text = getFormattedUpdate(update, request, fmt) 424 if text: 425 append(text) 426 else: 427 append(fmt.text(_("Update cannot be shown for content of type %s.") % update.content_type)) 428 append(fmt.div(on=0)) 429 430 return "".join(result) 431 432 # Source management. 433 434 def getUpdateSources(pagename, request): 435 436 "Return the update sources from the given 'pagename' using the 'request'." 437 438 sources = {} 439 440 source_definitions = getWikiDict(pagename, request) 441 442 if source_definitions: 443 for name, value in source_definitions.items(): 444 sources[name] = getSourceParameters(value) 445 446 return sources 447 448 def getSourceParameters(source_definition): 449 450 "Return the parameters from the given 'source_definition' string." 451 452 return parseDictEntry(source_definition, ("type", "location")) 453 454 # HTML parsing support. 455 456 class IncomingHTMLSanitizer(HTMLSanitizer): 457 458 "An HTML parser that rewrites references to attachments." 459 460 def __init__(self, out, request, page, message_number): 461 HTMLSanitizer.__init__(self, out) 462 self.request = request 463 self.message_number = message_number 464 self.page = page 465 466 def rewrite_reference(self, ref): 467 if ref.startswith("cid:"): 468 part = ref[len("cid:"):] 469 action_link = self.page.url(self.request, { 470 "action" : "ReadMessage", "doit" : "1", 471 "message" : self.message_number, "part" : part 472 }) 473 return action_link 474 else: 475 return ref 476 477 def handle_starttag(self, tag, attrs): 478 new_attrs = [] 479 for attrname, attrvalue in attrs: 480 if attrname in self.uri_attrs: 481 new_attrs.append((attrname, self.rewrite_reference(attrvalue))) 482 else: 483 new_attrs.append((attrname, attrvalue)) 484 HTMLSanitizer.handle_starttag(self, tag, new_attrs) 485 486 class IncomingMarkup(Markup): 487 488 "A special markup processor for incoming HTML." 489 490 def sanitize(self, request, page, message_number): 491 out = getwriter("utf-8")(StringIO()) 492 sanitizer = IncomingHTMLSanitizer(out, request, page, message_number) 493 sanitizer.feed(self.stripentities(keepxmlentities=True)) 494 return IncomingMarkup(unicode(out.getvalue(), "utf-8")) 495 496 class IncomingHTMLParser: 497 498 "Filters and rewrites incoming HTML content." 499 500 def __init__(self, raw, request, **kw): 501 self.raw = raw 502 self.request = request 503 self.message_number = None 504 self.page = None 505 506 def format(self, formatter, **kw): 507 508 "Send the text." 509 510 try: 511 self.request.write(formatter.rawHTML(IncomingMarkup(self.raw).sanitize(self.request, self.page, self.message_number))) 512 except HTMLParseError, e: 513 self.request.write(formatter.sysmsg(1) + 514 formatter.text(u'HTML parsing error: %s in "%s"' % (e.msg, 515 self.raw.splitlines()[e.lineno - 1].strip())) + 516 formatter.sysmsg(0)) 517 518 class MakeIncomingHTMLParser: 519 520 "A class that makes parsers configured for messages." 521 522 def __init__(self, page, message_number): 523 524 "Initialise with state that is used to configure instantiated parsers." 525 526 self.message_number = message_number 527 self.page = page 528 529 def __call__(self, *args, **kw): 530 parser = IncomingHTMLParser(*args, **kw) 531 parser.message_number = self.message_number 532 parser.page = self.page 533 return parser 534 535 def get_make_parser(page, message_number): 536 537 """ 538 Return a callable that will return a parser configured for the message from 539 the given 'page' with the given 'message_number'. 540 """ 541 542 return MakeIncomingHTMLParser(page, message_number) 543 544 # vim: tabstop=4 expandtab shiftwidth=4